[6348] | 1 | |
---|
| 2 | #include <SDL/SDL.h> |
---|
| 3 | #include <SDL/SDL_ttf.h> |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | TTF_Font* fontTTF = NULL; |
---|
| 8 | |
---|
| 9 | int getMaxHeight() |
---|
| 10 | { |
---|
| 11 | if (fontTTF != NULL) |
---|
| 12 | return TTF_FontHeight(fontTTF); |
---|
| 13 | else |
---|
| 14 | return 0; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | void createAsciiImage(const char* fileName, unsigned int size) |
---|
| 19 | { |
---|
| 20 | if (fontTTF == NULL) |
---|
| 21 | return; |
---|
| 22 | int height = getMaxHeight(); |
---|
| 23 | |
---|
| 24 | // |
---|
| 25 | SDL_Color tmpColor = {0, 0, 0}; |
---|
| 26 | // Surface definition. |
---|
| 27 | SDL_Rect tmpRect; // this represents a Rectangle for blitting. |
---|
| 28 | SDL_Surface* tmpSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
| 29 | height*16, height*16, |
---|
| 30 | 32, |
---|
| 31 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
| 32 | 0x000000FF, |
---|
| 33 | 0x0000FF00, |
---|
| 34 | 0x00FF0000, |
---|
| 35 | 0xFF000000 |
---|
| 36 | #else |
---|
| 37 | 0xFF000000, |
---|
| 38 | 0x00FF0000, |
---|
| 39 | 0x0000FF00, |
---|
| 40 | 0x000000FF |
---|
| 41 | #endif |
---|
| 42 | ); |
---|
| 43 | tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h; |
---|
| 44 | SDL_SetClipRect(tmpSurf, &tmpRect); |
---|
| 45 | int maxLineHeight = 0; |
---|
| 46 | |
---|
| 47 | int posX, posY; |
---|
| 48 | // all the interessting Glyphs |
---|
| 49 | for (posY = 0; posY < 16; posY++) |
---|
| 50 | { |
---|
| 51 | for (posX = 0; posX < 16; posX++) |
---|
| 52 | { |
---|
| 53 | SDL_Surface* glyphSurf = NULL; |
---|
| 54 | if (fontTTF != NULL) |
---|
| 55 | { |
---|
| 56 | SDL_Color white = {255, 255, 255}; |
---|
| 57 | glyphSurf = TTF_RenderGlyph_Blended(fontTTF, posX+16*posY, white); |
---|
| 58 | } |
---|
| 59 | if( glyphSurf != NULL ) |
---|
| 60 | { |
---|
| 61 | tmpRect.x = height*posX; |
---|
| 62 | tmpRect.y = height*posY; |
---|
| 63 | SDL_SetAlpha(glyphSurf, 0, 0); |
---|
| 64 | |
---|
| 65 | SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect); |
---|
| 66 | SDL_FreeSurface(glyphSurf); |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | SDL_SaveBMP(tmpSurf, fileName); |
---|
| 71 | SDL_FreeSurface(tmpSurf); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | int main (int argc, const char** argv) |
---|
| 77 | { |
---|
| 78 | SDL_Init(0); |
---|
| 79 | TTF_Init(); |
---|
| 80 | unsigned int renderSize = 32; |
---|
| 81 | |
---|
| 82 | fontTTF = TTF_OpenFont(argv[1], renderSize); |
---|
| 83 | |
---|
| 84 | createAsciiImage(argv[2], renderSize); |
---|
| 85 | |
---|
| 86 | // delete fontTTF; |
---|
| 87 | TTF_Quit(); |
---|
| 88 | SDL_Quit(); |
---|
| 89 | } |
---|