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

+4 -3
View File
@@ -70,15 +70,16 @@ void fill_rectangle(RenderRect rect, SDL_Color color);
* Ensures that a rendered rectangle is clipped to the drawing area when it is * Ensures that a rendered rectangle is clipped to the drawing area when it is
* smaller than the actual size of the window. * smaller than the actual size of the window.
*/ */
void clip_render_rects_to_drawing_area(RenderRect *src, RenderRect *dest); void clip_texture_render_rects_to_drawing_area(RenderRect *src,
RenderRect *dest);
RenderRect get_texture_asset_rect(AssetID id); RenderRect get_texture_asset_rect(AssetID id);
void render_texture_asset(AssetID id, RenderRect *src, RenderRect *dest); void render_texture_asset(AssetID id, RenderRect *src, RenderRect *dest);
void modulate_texture_color(AssetID id, SDL_Color color); void modulate_texture_color(AssetID id, SDL_Color color);
void modulate_texture_alpha(AssetID id, u8 alpha); void modulate_texture_alpha(AssetID id, u8 alpha);
TextSize get_text_size_from_font_asset(AssetID id, const char *text); TextSize get_text_size_from_font_asset(AssetID id, const char *text);
void render_text_with_font_asset(AssetID id, const char *text, f32 x, f32 y, void render_text_with_font_asset(AssetID id, const char *text, RenderRect dest,
SDL_Color color); SDL_Color color, const RenderRect *clip);
void set_font_asset_size(AssetID id, u32 size); void set_font_asset_size(AssetID id, u32 size);
i32 set_font_direction(AssetID id, FontDirection direction); i32 set_font_direction(AssetID id, FontDirection direction);
i32 set_font_script_name(AssetID id, const char *script); i32 set_font_script_name(AssetID id, const char *script);
+1 -1
View File
@@ -59,7 +59,7 @@ void draw_rect(const Renderer *renderer, SDL_Color color, f32 x, f32 y, f32 w,
void fill_rect(const Renderer *renderer, SDL_Color color, f32 x, f32 y, f32 w, void fill_rect(const Renderer *renderer, SDL_Color color, f32 x, f32 y, f32 w,
f32 h); f32 h);
void render_surface(const Renderer *renderer, SDL_Surface *surface, void render_surface(const Renderer *renderer, SDL_Surface *surface,
const RenderRect *dest); const RenderRect *src, const RenderRect *dest);
void render_texture(const Renderer *renderer, SDL_Texture *texture, void render_texture(const Renderer *renderer, SDL_Texture *texture,
const RenderRect *src, const RenderRect *dest); const RenderRect *src, const RenderRect *dest);
void swap_buffers(const Renderer *renderer); void swap_buffers(const Renderer *renderer);
+19 -9
View File
@@ -230,7 +230,8 @@ void fill_rectangle(RenderRect rect, SDL_Color color) {
rect.height); 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) { if (!dest) {
return; return;
} }
@@ -371,8 +372,8 @@ TextSize get_text_size_from_font_asset(AssetID id, const char *text) {
return output; return output;
} }
void render_text_with_font_asset(AssetID id, const char *text, f32 x, f32 y, void render_text_with_font_asset(AssetID id, const char *text, RenderRect dest,
SDL_Color color) { SDL_Color color, const RenderRect *clip) {
if (id < 0) { if (id < 0) {
return; return;
} }
@@ -387,14 +388,23 @@ void render_text_with_font_asset(AssetID id, const char *text, f32 x, f32 y,
return; return;
} }
// Get the width and height, in pixels, of the text rendered using the font RenderRect src = {};
i32 w = 0;
i32 h = 0;
TTF_SizeUTF8(font, text, &w, &h);
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); SDL_FreeSurface(surface);
surface = NULL; surface = NULL;
+3 -2
View File
@@ -5,6 +5,7 @@
#include "SDL_platform.h" #include "SDL_platform.h"
#include "SDL_rect.h" #include "SDL_rect.h"
#include "SDL_render.h" #include "SDL_render.h"
#include "SDL_surface.h"
#include "SDL_video.h" #include "SDL_video.h"
#include "aliases.h" #include "aliases.h"
#include "engine_build_config.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, void render_surface(const Renderer *renderer, SDL_Surface *surface,
const RenderRect *dest) { const RenderRect *src, const RenderRect *dest) {
SDL_Texture *texture = SDL_Texture *texture =
SDL_CreateTextureFromSurface(renderer->SDL_base_renderer, surface); SDL_CreateTextureFromSurface(renderer->SDL_base_renderer, surface);
@@ -93,7 +94,7 @@ void render_surface(const Renderer *renderer, SDL_Surface *surface,
return; return;
} }
render_texture(renderer, texture, NULL, dest); render_texture(renderer, texture, src, dest);
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
texture = NULL; texture = NULL;