-
Notifications
You must be signed in to change notification settings - Fork 0
Code_font_and_text
lemmiix edited this page Mar 5, 2026
·
4 revisions
bool font_init() {
const char* font_path = PATH_FONT;
TTF_Init();
Font = TTF_OpenFont(font_path, FONT_SIZE);
if (Font == 0) {
return 0;
} else {
Font_color.r = 255;
Font_color.g = 255;
Font_color.b = 255;
Font_color.a = 255;
return 1;
}
}This initiates the font using a predefined font inside /resources/fonts/.
@return: FALSE if failed, otherwise TRUE
---
void print_text_to_screen_with_color(const char* string, int x, int y, SDL_Renderer* renderer, SDL_Color color) {
Font_surface = TTF_RenderText_Solid_Wrapped(Font, string, 0, color, 0);
Font_texture = SDL_CreateTextureFromSurface(renderer, Font_surface);
Font_rect.x = x;
Font_rect.y = y;
Font_rect.h = Font_surface->h;
Font_rect.w = Font_surface->w;
SDL_RenderTexture(renderer, Font_texture, NULL, &Font_rect);
SDL_DestroySurface(Font_surface);
SDL_DestroyTexture(Font_texture);
}This puts a string on the screen inside the provided renderer with color.
@param string: text to be displayed
@param x: coordinate on X-axis
@param y: coordinate on Y-axis
@param renderer: active renderer
@param color: SDL color to be applied to the text
Usage:
print_text_to_screen_with_color("ballin\0", 50, 50, &renderer, some_sdl_color);
---
void print_text_to_screen(const char* string, int x, int y, SDL_Renderer* renderer) {
Font_surface = TTF_RenderText_Solid_Wrapped(Font, string, 0, Font_color, 0);
Font_texture = SDL_CreateTextureFromSurface(renderer, Font_surface);
Font_rect.x = x;
Font_rect.y = y;
Font_rect.h = Font_surface->h;
Font_rect.w = Font_surface->w;
SDL_RenderTexture(renderer, Font_texture, NULL, &Font_rect);
SDL_DestroySurface(Font_surface);
SDL_DestroyTexture(Font_texture);
}This puts a string on the screen inside the provided renderer.
@param string: text to be displayed
@param x: coordinate on X-axis
@param y: coordinate on Y-axis
@param renderer: active renderer
Usage:
print_text_to_screen_with_color("ballin\0", 50, 50, &renderer, some_sdl_color);
---
void kill_font() {
TTF_CloseFont(Font);
}This properly ends font usage.
Usage:
/**
* This SHOULD be called before ending the program
*/
kill_font();
