Modified the text rendering functions to allow for clipping the text

This commit is contained in:
abdelrahman committed 2023-02-04 18:47:02 +00:00
1 parent 76875ed81f
commit d429262aaa
4 files changed
+27 -15

No files matched your search

+19 -9
View File
@@ -230,7 +230,8 @@ void fill_rectangle(RenderRect rect, SDL_Color color) {
rect.height);
}
void clip_render_rects_to_drawing_area(RenderRect *src, RenderRect *dest) {
void clip_texture_render_rects_to_drawing_area(RenderRect *src,
RenderRect *dest) {
if (!dest) {
return;
}
@@ -371,8 +372,8 @@ TextSize get_text_size_from_font_asset(AssetID id, const char *text) {
return output;
}
void render_text_with_font_asset(AssetID id, const char *text, f32 x, f32 y,
SDL_Color color) {
void render_text_with_font_asset(AssetID id, const char *text, RenderRect dest,
SDL_Color color, const RenderRect *clip) {
if (id < 0) {
return;
}
@@ -387,14 +388,23 @@ void render_text_with_font_asset(AssetID id, const char *text, f32 x, f32 y,
return;
}
// Get the width and height, in pixels, of the text rendered using the font
i32 w = 0;
i32 h = 0;
TTF_SizeUTF8(font, text, &w, &h);
RenderRect src = {};
RenderRect rect = {x, y, w, h};
bool clip_text = clip && (dest.width > clip->width);
render_surface(GameEngine.window.renderer, surface, &rect);
if (clip_text) {
f32 width_difference = dest.width - clip->width;
// Offset the src rect so the text input displays where the text cursor is
src.x += width_difference;
src.width = clip->width;
src.height = clip->height;
dest.width = clip->width;
}
render_surface(GameEngine.window.renderer, surface, clip_text ? &src : NULL,
&dest);
SDL_FreeSurface(surface);
surface = NULL;