Improved text rendering and fixed a bug with the texture rendering

This commit is contained in:
abdelrahman committed 2022-10-28 00:40:29 +01:00
1 parent 4659a4cd49
commit 8a4697e121
4 files changed
+44 -8

No files matched your search

+24 -4
View File
@@ -10,6 +10,7 @@
#include "SDL_mixer.h"
#include "SDL_pixels.h"
#include "SDL_platform.h"
#include "SDL_rect.h"
#include "SDL_render.h"
#include "SDL_surface.h"
#include "SDL_timer.h"
@@ -322,8 +323,25 @@ void render_texture_asset(AssetID id, RenderRect *src, RenderRect *dest) {
render_texture(GameEngine.window.renderer, texture, src, dest);
}
void render_text_with_font_asset(AssetID id, const char *text, float x,
float y) {
TextSize get_text_size_from_font_asset(AssetID id, const char *text) {
TextSize output = {};
if (id < 0) {
return output;
}
TTF_Font *font = get_font(GameEngine.asset_manager, id);
if (!font) {
return output;
}
TTF_SizeText(font, text, &(output.width), &(output.height));
return output;
}
void render_text_with_font_asset(AssetID id, const char *text, float x, float y,
SDL_Color color) {
if (id < 0) {
return;
}
@@ -333,8 +351,10 @@ void render_text_with_font_asset(AssetID id, const char *text, float x,
return;
}
SDL_Surface *surface =
TTF_RenderText_Solid(font, text, (SDL_Color){0, 0, 0, 255});
SDL_Surface *surface = TTF_RenderText_Solid(font, text, color);
if (!surface) {
return;
}
// Get the width and height, in pixels, of the text rendered using the font
int32_t w = 0;
+10 -2
View File
@@ -1,5 +1,6 @@
#include "Renderer.h"
#include "SDL.h"
#include "SDL_blendmode.h"
#include "SDL_image.h"
#include "SDL_platform.h"
#include "SDL_rect.h"
@@ -31,6 +32,8 @@ Renderer *create_renderer(SDL_Window *window) {
return NULL;
}
SDL_SetRenderDrawBlendMode(renderer->SDL_base_renderer, SDL_BLENDMODE_BLEND);
return renderer;
}
@@ -38,6 +41,11 @@ void get_render_output_size(Renderer *renderer, int32_t *w, int32_t *h) {
SDL_GetRendererOutputSize(renderer->SDL_base_renderer, w, h);
}
void set_render_draw_color(const Renderer *renderer, SDL_Color color) {
SDL_SetRenderDrawColor(renderer->SDL_base_renderer, color.r, color.g, color.b,
color.a);
}
void render_clear(const Renderer *renderer, SDL_Color color) {
if (!renderer || !(renderer->SDL_base_renderer)) {
return;
@@ -84,7 +92,7 @@ void render_surface(const Renderer *renderer, SDL_Surface *surface,
void render_texture(const Renderer *renderer, SDL_Texture *texture,
const RenderRect *src, const RenderRect *dest) {
if (!renderer || !texture) {
if (!renderer || !texture || !dest) {
return;
}
@@ -100,7 +108,7 @@ void render_texture(const Renderer *renderer, SDL_Texture *texture,
// TODO (Abdelrahman): Look into using the SDL_RenderCopyExF instead to
// support rotating the texture
SDL_RenderCopyF(renderer->SDL_base_renderer, texture, &src_frect,
SDL_RenderCopyF(renderer->SDL_base_renderer, texture, src ? &src_frect : NULL,
&dest_frect);
}