Skip to content

Commit 0e62c4f

Browse files
committed
- add border to font
1 parent 0b1a7ed commit 0e62c4f

File tree

2 files changed

+57
-68
lines changed

2 files changed

+57
-68
lines changed

graphicslib.cpp

+53-68
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,13 @@ bool graphicsLib::initGraphics()
156156
return false;
157157
} else {
158158
font = TTF_OpenFontRW(fileRW, 1, FONT_SIZE);
159+
// outline-font
160+
outline_font = TTF_OpenFontRW(fileRW, 1, FONT_SIZE);
161+
TTF_SetFontOutline(outline_font, 1);
159162
}
160163
delete[] buffer;
161164

165+
162166
printf(">> WII.DEBUG.INIT_GRAPHICS #3 <<\n");
163167
fflush(stdout);
164168

@@ -937,27 +941,70 @@ void graphicsLib::draw_text(short x, short y, string text, st_color color)
937941
if (text.length() <= 0) {
938942
return;
939943
}
944+
945+
render_text(x, y, text, color, gameScreen, false);
946+
}
947+
948+
949+
void graphicsLib::draw_text(short x, short y, string text, graphicsLib_gSurface &surface)
950+
{
951+
render_text(x, y, text, st_color(255, 255, 255), surface, false);
952+
}
953+
954+
void graphicsLib::draw_centered_text(short y, string text, st_color font_color)
955+
{
956+
draw_centered_text(y, text, gameScreen, font_color);
957+
}
958+
959+
void graphicsLib::draw_centered_text(short y, string text)
960+
{
961+
draw_centered_text(y, text, gameScreen, st_color(TEXT_DEFAUL_COLOR_VALUE, TEXT_DEFAUL_COLOR_VALUE, TEXT_DEFAUL_COLOR_VALUE));
962+
}
963+
964+
void graphicsLib::draw_centered_text(short y, string text, graphicsLib_gSurface &surface, st_color temp_font_color)
965+
{
966+
render_text(0, y, text, temp_font_color, surface, true);
967+
}
968+
969+
void graphicsLib::render_text(short x, short y, string text, st_color color, graphicsLib_gSurface &surface, bool centered)
970+
{
971+
SDL_Rect text_pos={x, y, 0, 0};
940972
SDL_Color font_color = SDL_Color();
941973
font_color.r = color.r;
942974
font_color.g = color.g;
943975
font_color.b = color.b;
944976
x += _screen_resolution_adjust.x;
945977
y += _screen_resolution_adjust.y;
946-
SDL_Rect text_pos={x, y, 0, 0};
978+
947979
if (!font) {
948980
printf("graphicsLib::draw_text - TTF_OpenFont: %s\n", TTF_GetError());
949981
show_debug_msg("EXIT #10");
950982
exception_manager::throw_file_not_found_exception(std::string("graphicsLib::draw_text, fount is NULL"), std::string(TTF_GetError()));
951983
// handle error
952984
}
953985

954-
//std::cout << "RENDER #1[" << text << "]" << std::endl;
955-
/*
956-
if (text.length() == 1) {
957-
std::cout << "DEBUG #9" << std::endl;
986+
if (outline_font) {
987+
SDL_Color black = {0, 0, 0};
988+
SDL_Surface* text_outlineSF = TTF_RenderUTF8_Solid(outline_font, text.c_str(), black);
989+
990+
if (text_outlineSF) {
991+
SDL_Surface* text_outlineSF_format = SDL_DisplayFormat(text_outlineSF);
992+
SDL_FreeSurface(text_outlineSF);
993+
994+
if (text_outlineSF_format) {
995+
if (centered == true && text.size() > 0) {
996+
text_pos.x = RES_W/2 - text_outlineSF_format->w/2;
997+
}
998+
SDL_BlitSurface(text_outlineSF_format, 0, game_screen, &text_pos);
999+
SDL_FreeSurface(text_outlineSF_format);
1000+
}
1001+
}
1002+
9581003
}
959-
*/
9601004
SDL_Surface* textSF = TTF_RenderUTF8_Solid(font, text.c_str(), font_color);
1005+
if (centered == true && text.size() > 0) {
1006+
text_pos.x = RES_W/2 - textSF->w/2;
1007+
}
9611008

9621009
if (!textSF) {
9631010
return;
@@ -973,68 +1020,6 @@ void graphicsLib::draw_text(short x, short y, string text, st_color color)
9731020
}
9741021

9751022

976-
void graphicsLib::draw_text(short x, short y, string text, graphicsLib_gSurface &surface)
977-
{
978-
SDL_Color font_color;
979-
font_color.r = 255;
980-
font_color.g = 255;
981-
font_color.b = 255;
982-
SDL_Rect text_pos={x, y, 0, 0};
983-
if (!font) {
984-
printf("graphicsLib::draw_text - TTF_OpenFont: %s\n", TTF_GetError());
985-
show_debug_msg("EXIT #11");
986-
exception_manager::throw_file_not_found_exception(std::string("graphicsLib::draw_text #2, fount is NULL"), std::string(TTF_GetError()));
987-
// handle error
988-
}
989-
text_pos.x += _screen_resolution_adjust.x;
990-
text_pos.y += _screen_resolution_adjust.y;
991-
//std::cout << "RENDER #2[" << text << "]" << std::endl;
992-
SDL_Surface* textSF = TTF_RenderUTF8_Solid(font, text.c_str(), font_color);
993-
SDL_Surface* textSF_format = SDL_DisplayFormat(textSF);
994-
SDL_FreeSurface(textSF);
995-
SDL_BlitSurface(textSF_format, 0, surface.get_surface(), &text_pos);
996-
SDL_FreeSurface(textSF_format);
997-
}
998-
999-
void graphicsLib::draw_centered_text(short y, string text, st_color font_color)
1000-
{
1001-
draw_centered_text(y, text, gameScreen, font_color);
1002-
}
1003-
1004-
void graphicsLib::draw_centered_text(short y, string text)
1005-
{
1006-
draw_centered_text(y, text, gameScreen, st_color(TEXT_DEFAUL_COLOR_VALUE, TEXT_DEFAUL_COLOR_VALUE, TEXT_DEFAUL_COLOR_VALUE));
1007-
}
1008-
1009-
void graphicsLib::draw_centered_text(short y, string text, graphicsLib_gSurface &surface, st_color temp_font_color)
1010-
{
1011-
SDL_Color font_color;
1012-
font_color.r = temp_font_color.r;
1013-
font_color.g = temp_font_color.g;
1014-
font_color.b = temp_font_color.b;
1015-
SDL_Rect text_pos={0, y, 0, 0};
1016-
if (!font) {
1017-
printf("graphicsLib::draw_centered_text - TTF_OpenFont: %s\n", TTF_GetError());
1018-
show_debug_msg("EXIT #12");
1019-
exception_manager::throw_file_not_found_exception(std::string("graphicsLib::draw_centered_text, fount is NULL"), std::string(TTF_GetError()));
1020-
}
1021-
SDL_Surface* textSF = TTF_RenderUTF8_Solid(font, text.c_str(), font_color);
1022-
if (textSF == NULL) {
1023-
return;
1024-
}
1025-
if (text.size() > 0) {
1026-
text_pos.x = RES_W/2 - textSF->w/2;
1027-
}
1028-
text_pos.y = y;
1029-
text_pos.x += _screen_resolution_adjust.x;
1030-
text_pos.y += _screen_resolution_adjust.y;
1031-
SDL_Surface* textSF_format = SDL_DisplayFormat(textSF);
1032-
SDL_FreeSurface(textSF);
1033-
SDL_BlitSurface(textSF_format, 0, surface.get_surface(), &text_pos);
1034-
SDL_FreeSurface(textSF_format);
1035-
}
1036-
1037-
10381023

10391024
Uint8 graphicsLib::getColorNumber(Uint8 r, Uint8 g, Uint8 b) {
10401025
if (game_screen == NULL || game_screen->format == NULL) {

graphicslib.h

+4
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ class graphicsLib
193193
void draw_centered_text(short int y, std::string text, st_color font_color);
194194
void draw_centered_text(short int y, std::string text);
195195
void draw_centered_text(short int y, std::string text, struct graphicsLib_gSurface& surface, st_color temp_font_color);
196+
197+
196198
Uint8 getColorNumber(Uint8 r, Uint8 g, Uint8 b);
197199
void drawCursor(st_position);
198200
void eraseCursor(st_position);
@@ -261,6 +263,7 @@ class graphicsLib
261263
void set_video_mode();
262264
void preload_images();
263265
void preload_anim_tiles();
266+
void render_text(short int x, short int y, std::string text, st_color color, struct graphicsLib_gSurface& surface, bool centered);
264267

265268

266269

@@ -303,6 +306,7 @@ class graphicsLib
303306
// TODO: free those pointers
304307
TTF_Font *font;
305308
TTF_Font *lowercase_font;
309+
TTF_Font *outline_font;
306310

307311
SDL_Surface *game_screen; // we do not put this into a graphicsLib_gSurface because this is meant to be used only internally
308312
SDL_Surface *game_screen_scaled;

0 commit comments

Comments
 (0)