Added Text.cpp

This commit is contained in:
2026-09-27 21:57:14 -05:00
parent 42c59530d3
commit 1a69261952
6 changed files with 105 additions and 33 deletions
+9 -29
View File
@@ -10,17 +10,15 @@
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "Text.h"
#include "fileCommon.h"
#include "game.h"
#include "window.h"
Game game;
Window window;
TTF_Font *font = nullptr;
SDL_Texture *font_texture = nullptr;
fs::path fontPath("Resources/Pixelzone-E4MV4.ttf");
Text text;
void EditRooms() {
@@ -62,8 +60,6 @@ void EditRooms() {
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
SDL_Surface *text;
SDL_Color color = {255, 255, 255, SDL_ALPHA_OPAQUE};
if (SDL_Init(SDL_INIT_VIDEO) == false) {
SDL_Log("SDL could not initialize! SDL error: %s\n", SDL_GetError());
@@ -75,20 +71,10 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
return result;
}
if (!TTF_Init()) {
SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError());
return SDL_APP_FAILURE;
}
result = text.OpenFont("Resources/Pixelzone-E4MV4.ttf");
font = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f);
text = TTF_RenderText_Blended(font, "Hello World!", 0, color);
if (text) {
font_texture = SDL_CreateTextureFromSurface(window.GetRenderer(), text);
SDL_DestroySurface(text);
}
if (!font_texture) {
SDL_Log("Couldn't create text: %s\n", SDL_GetError());
return SDL_APP_FAILURE;
if (result != SDL_APP_CONTINUE) {
return result;
}
if (!game) {
@@ -101,6 +87,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
return SDL_APP_FAILURE;
}
text.SetText("Hello There", &window);
return SDL_APP_CONTINUE;
}
@@ -132,15 +120,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
SDL_SetRenderDrawColor(window.GetRenderer(), 0, 0, 0, 255); // Set render draw color to black
SDL_RenderClear(window.GetRenderer()); // Clear the renderer
int w = 0, h = 0;
SDL_FRect dst;
const float scale = 4.0f;
/* Center the text and scale it up */
SDL_GetRenderOutputSize(window.GetRenderer(), &w, &h);
SDL_SetRenderScale(window.GetRenderer(), scale, scale);
SDL_GetTextureSize(font_texture, &dst.w, &dst.h);
SDL_RenderTexture(window.GetRenderer(), font_texture, NULL, &dst);
text.Render();
SDL_RenderPresent(window.GetRenderer()); // Render the screen
// game.Run();
@@ -152,7 +132,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
if (result == SDL_APP_SUCCESS) {
window.Close();
TTF_CloseFont(font);
text.CloseFont();
SDL_Quit();
}
}