ttfDraw.cpp
00001
00002
00003
00004
00005
00006
00007 #include "ttfDraw.h"
00008 #include "detect_os.h"
00009 #ifndef MSC
00010 #include <unistd.h>
00011 #else
00012 #include <windows.h>
00013 #include <direct.h>
00014 #include <io.h>
00015 #ifndef F_OK
00016 #define F_OK 06
00017 #endif
00018 #endif
00019 #include <stdlib.h>
00020 #include <memory>
00021
00022
00023 bool TTF_Draw::ttf_initialized = false;
00024
00025
00026 void TTF_Draw::initTTF(void) {
00027 #if !HAVE_CONFIG_H || HAVE_LIBSDL_TTF
00028 if (TTF_Init() < 0) {
00029 throw TTF_Exception(TTF_GetError());
00030 }
00031 #endif
00032 }
00033
00034
00035 TTF_Draw::TTF_Draw(void) : fileName(NULL) {
00036 }
00037
00038
00039 TTF_Draw::~TTF_Draw(void) {
00040 #if !HAVE_CONFIG_H || HAVE_LIBSDL_TTF
00041 for (std::map<int,TTF_Font*>::iterator it = fonts.begin();
00042 it != fonts.end(); ++it) {
00043 TTF_CloseFont(it->second);
00044 it->second = NULL;
00045 }
00046 free(fileName);
00047 TTF_Quit();
00048 #endif
00049 }
00050
00051
00052 void TTF_Draw::createFontResource(int pxSize) {
00053 #if !HAVE_CONFIG_H || HAVE_LIBSDL_TTF
00054 #if 1
00055 if (pxSize <= 11) {
00056 fprintf(stderr, "font size is too small: %d\n", pxSize);
00057 }
00058 #endif
00059 TTF_Font* font = TTF_OpenFont(fileName, pxSize);
00060 if (!font) {
00061 throw TTF_Exception(TTF_GetError());
00062 }
00063 fonts[pxSize] = font;
00064 #endif
00065 }
00066
00067
00068 bool TTF_Draw::load(const char* file) {
00069 if (!ttf_initialized) {
00070 initTTF();
00071 ttf_initialized = true;
00072 }
00073
00074 if (access(file, F_OK) < 0) {
00075 std::string message = std::string("TTF file is not found: ") + file;
00076 throw TTF_Exception(message.c_str());
00077 }
00078
00079 int length = static_cast<int>(strlen(file) + 1);
00080 fileName = (char*)malloc(length);
00081 strncpy(fileName, file, length);
00082
00083 return true;
00084 }
00085
00086
00087 SDL_Surface* TTF_Draw::createText(const unsigned short* text, int pxSize,
00088 unsigned long color,
00089 unsigned long background) {
00090 #if !HAVE_CONFIG_H || HAVE_LIBSDL_TTF
00091 if (!fileName) {
00092 return NULL;
00093 }
00094
00095 std::map<int,TTF_Font*>::iterator it = fonts.find(pxSize);
00096 if (it == fonts.end()) {
00097 createFontResource(pxSize);
00098 }
00099
00100
00101 if (!text || text[0] == 0x00) {
00102 return NULL;
00103 }
00104 SDL_Color font_color = {
00105 static_cast<Uint8>((color >> 24) & 0xff),
00106 static_cast<Uint8>((color >> 16) & 0xff),
00107 static_cast<Uint8>((color >> 8) & 0xff), 0 };
00108 SDL_Color back_color = {
00109 static_cast<Uint8>((background >> 24) & 0xff),
00110 static_cast<Uint8>((background >> 16) & 0xff),
00111 static_cast<Uint8>((background >> 8) & 0xff), 0 };
00112
00113 return TTF_RenderUNICODE_Shaded(fonts[pxSize], text, font_color, back_color);
00114 #else
00115 return NULL;
00116 #endif
00117 }
00118
00119
00120 SDL_Surface* TTF_Draw::createText(const char* text, int pxSize,
00121 unsigned long color,
00122 unsigned long background) {
00123 int length = static_cast<int>(strlen(text)) +1;
00124 unsigned short* p = new unsigned short [length];
00125 for (int i = 0; i < length; ++i) {
00126 p[i] = text[i];
00127 }
00128 SDL_Surface* draw = createText(static_cast<const unsigned short*>(p),
00129 pxSize, color, background);
00130 delete [] p;
00131 return draw;
00132 }
00133