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;
+3 -2
View File
@@ -5,6 +5,7 @@
#include "SDL_platform.h"
#include "SDL_rect.h"
#include "SDL_render.h"
#include "SDL_surface.h"
#include "SDL_video.h"
#include "aliases.h"
#include "engine_build_config.h"
@@ -85,7 +86,7 @@ void fill_rect(const Renderer *renderer, SDL_Color color, f32 x, f32 y, f32 w,
}
void render_surface(const Renderer *renderer, SDL_Surface *surface,
const RenderRect *dest) {
const RenderRect *src, const RenderRect *dest) {
SDL_Texture *texture =
SDL_CreateTextureFromSurface(renderer->SDL_base_renderer, surface);
@@ -93,7 +94,7 @@ void render_surface(const Renderer *renderer, SDL_Surface *surface,
return;
}
render_texture(renderer, texture, NULL, dest);
render_texture(renderer, texture, src, dest);
SDL_DestroyTexture(texture);
texture = NULL;