Created
June 2, 2017 08:32
-
-
Save ilario-pierbattista/be6b967b05fa2f1eb322f35988a33ad0 to your computer and use it in GitHub Desktop.
SDL ttf devanagari support (bug)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SDL.h" | |
#include "SDL_ttf.h" | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
void DrawImage (SDL_Surface *source, SDL_Surface *destination, int x, | |
int y) { | |
if (!source || !destination) | |
return; | |
SDL_Rect destRect; | |
destRect.x = x; | |
destRect.y = y; | |
SDL_BlitSurface(source, NULL, destination, &destRect); | |
} | |
std::string Int2String (int i) { | |
std::stringstream sstream; | |
sstream << i; | |
return sstream.str(); | |
} | |
int main (int argc, char *argv[]) { | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
std::cerr << "Error: Unable to initialize SDL video" | |
<< std::endl; | |
return -1; | |
} | |
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 0, SDL_SWSURFACE); | |
if (!screen) { | |
std::cerr << "Error: Unable to set requested video mode" | |
<< std::endl; | |
return -1; | |
} | |
if (TTF_Init() < 0) { | |
std::cerr << "Error: Unable to initialize SDL_ttf" << std::endl; | |
return -1; | |
} | |
SDL_Surface *text_solid = NULL; | |
SDL_Surface *text_shaded = NULL; | |
SDL_Surface *text_blended = NULL; | |
SDL_Surface *kerning_on = NULL; | |
SDL_Surface *outline_width = NULL; | |
SDL_Event event; | |
SDL_Color red = {255, 0, 0}; | |
SDL_Color blue = {0, 0, 255}; | |
bool running = true; | |
bool kerning = true; | |
std::string kerningString = ""; | |
bool wantToSetOutline = false; | |
int outlineWidth = 0; | |
int renderingType = 1; // Solid | |
bool hasChanged = false; | |
TTF_Font *font = NULL; | |
TTF_Font *infoFont = NULL; | |
std::string infofontfile = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"; | |
std::string fontfile = "/usr/share/fonts/truetype/lohit-devanagari/Lohit-Devanagari.ttf"; | |
if (!(font = TTF_OpenFont(fontfile.c_str(), 45)) || | |
!(infoFont = TTF_OpenFont(infofontfile.c_str(), 30))) { | |
std::cerr << "Error: Unable to open font" << std::endl; | |
return -1; | |
} | |
int w = 0; | |
int h = 0; | |
std::string text = "कोिझकोड कोझिकोड"; | |
std::cout << text << std::endl; | |
if (TTF_SizeText(font, text.c_str(), &w, &h) < 0) { | |
std::cerr << "Error: Unable to retrieve size of text" | |
<< std::endl; | |
return -1; | |
} | |
int lineSkip = TTF_FontLineSkip(font); | |
text_solid = TTF_RenderUTF8_Solid(font, text.c_str(), red); | |
text_shaded = TTF_RenderUTF8_Shaded(font, text.c_str(), red, blue); | |
text_blended = TTF_RenderUTF8_Blended(font, text.c_str(), red); | |
kerning_on = TTF_RenderText_Solid(infoFont, "Kerning: ON", red); | |
outline_width = TTF_RenderText_Solid(infoFont, "Outline width: 0", | |
red); | |
int x = 0; | |
int y = 300; | |
while (running) { | |
// Handle input | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
running = false; | |
break; | |
case SDL_KEYDOWN: | |
switch (event.key.keysym.sym) { | |
case SDLK_s: // Solid rendering (1) | |
renderingType = 1; | |
break; | |
case SDLK_d: // Shaded rendering (2) | |
renderingType = 2; | |
break; | |
case SDLK_b: // Blended rendering (3) | |
renderingType = 3; | |
break; | |
case SDLK_k: // Toggle kerning | |
kerning = !kerning; | |
TTF_SetFontKerning(font, (kerning ? 1 : 0)); | |
hasChanged = true; | |
SDL_FreeSurface(kerning_on); | |
kerningString = "Kerning: "; | |
if (kerning) | |
kerningString += "ON"; | |
else | |
kerningString += "OFF"; | |
kerning_on = TTF_RenderText_Solid(infoFont, | |
kerningString.c_str(), | |
red); | |
break; | |
case SDLK_o: | |
wantToSetOutline = true; | |
break; | |
case SDLK_0: | |
if (wantToSetOutline) { | |
outlineWidth = 0; | |
TTF_SetFontOutline(font, outlineWidth); | |
hasChanged = true; | |
wantToSetOutline = false; | |
SDL_FreeSurface(outline_width); | |
std::string outlineString = | |
"Outline width: " + | |
Int2String(outlineWidth); | |
outline_width = TTF_RenderText_Solid( | |
infoFont, outlineString.c_str(), | |
red); | |
} | |
break; | |
case SDLK_1: | |
if (wantToSetOutline) { | |
outlineWidth = 1; | |
TTF_SetFontOutline(font, outlineWidth); | |
hasChanged = true; | |
wantToSetOutline = false; | |
SDL_FreeSurface(outline_width); | |
std::string outlineString = | |
"Outline width: " + | |
Int2String(outlineWidth); | |
outline_width = TTF_RenderText_Solid( | |
infoFont, outlineString.c_str(), | |
red); | |
} | |
break; | |
case SDLK_2: | |
if (wantToSetOutline) { | |
outlineWidth = 2; | |
TTF_SetFontOutline(font, outlineWidth); | |
hasChanged = true; | |
wantToSetOutline = false; | |
SDL_FreeSurface(outline_width); | |
std::string outlineString = | |
"Outline width: " + | |
Int2String(outlineWidth); | |
outline_width = TTF_RenderText_Solid( | |
infoFont, outlineString.c_str(), | |
red); | |
} | |
break; | |
case SDLK_3: | |
if (wantToSetOutline) { | |
outlineWidth = 3; | |
TTF_SetFontOutline(font, outlineWidth); | |
hasChanged = true; | |
wantToSetOutline = false; | |
SDL_FreeSurface(outline_width); | |
std::string outlineString = | |
"Outline width: " + | |
Int2String(outlineWidth); | |
outline_width = TTF_RenderText_Solid( | |
infoFont, outlineString.c_str(), | |
red); | |
} | |
break; | |
case SDLK_ESCAPE: | |
running = false; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
/* | |
if (hasChanged) { | |
text_solid = TTF_RenderText_Solid(font, text.c_str(), red); | |
text_shaded = TTF_RenderText_Shaded(font, text.c_str(), red, | |
blue); | |
text_blended = TTF_RenderText_Blended(font, text.c_str(), | |
red); | |
hasChanged = false; | |
}*/ | |
// Draw images | |
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); | |
if (renderingType == 1) | |
DrawImage(text_solid, screen, x, y); | |
else if (renderingType == 2) | |
DrawImage(text_shaded, screen, x, y); | |
else if (renderingType == 3) | |
DrawImage(text_blended, screen, x, y); | |
DrawImage(kerning_on, screen, 5, 5); | |
DrawImage(outline_width, screen, 5, 5 + lineSkip); | |
SDL_Flip(screen); | |
} | |
// Free resources and quit | |
SDL_FreeSurface(text_solid); | |
SDL_FreeSurface(text_shaded); | |
SDL_FreeSurface(text_blended); | |
SDL_FreeSurface(kerning_on); | |
SDL_FreeSurface(outline_width); | |
text_solid = NULL; | |
text_shaded = NULL; | |
text_blended = NULL; | |
kerning_on = NULL; | |
outline_width = NULL; | |
TTF_CloseFont(font); | |
TTF_CloseFont(infoFont); | |
TTF_Quit(); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment