Added Text.cpp
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 4.2)
|
|||||||
|
|
||||||
project(Text-Adventure)
|
project(Text-Adventure)
|
||||||
|
|
||||||
set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ui/window.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Common/fileCommon.cpp)
|
set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ui/window.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Common/fileCommon.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ui/Text.cpp)
|
||||||
|
|
||||||
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
|
||||||
find_package(SDL3_ttf REQUIRED)
|
find_package(SDL3_ttf REQUIRED)
|
||||||
@@ -16,4 +16,4 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Editor)
|
|||||||
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/)
|
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/)
|
||||||
|
|
||||||
target_link_libraries(Text-Adventure PUBLIC Engine SDL3::SDL3 SDL3_ttf::SDL3_ttf)
|
target_link_libraries(Text-Adventure PUBLIC Engine SDL3::SDL3 SDL3_ttf::SDL3_ttf)
|
||||||
target_include_directories(Text-Adventure PUBLIC ${CMAKE_SOURCE_DIR}/Extern ${CMAKE_SOURCE_DIR}/ui ${CMAKE_SOURCE_DIR}/Common)
|
target_include_directories(Text-Adventure PUBLIC ${CMAKE_SOURCE_DIR}/Extern ${CMAKE_SOURCE_DIR}/ui ${CMAKE_SOURCE_DIR}/Common)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
|
|||||||
@@ -10,17 +10,15 @@
|
|||||||
#include <SDL3/SDL_render.h>
|
#include <SDL3/SDL_render.h>
|
||||||
#include <SDL3/SDL_video.h>
|
#include <SDL3/SDL_video.h>
|
||||||
#include <SDL3_ttf/SDL_ttf.h>
|
#include <SDL3_ttf/SDL_ttf.h>
|
||||||
|
#include "Text.h"
|
||||||
#include "fileCommon.h"
|
#include "fileCommon.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
|
||||||
Game game;
|
Game game;
|
||||||
Window window;
|
Window window;
|
||||||
TTF_Font *font = nullptr;
|
|
||||||
SDL_Texture *font_texture = nullptr;
|
|
||||||
|
|
||||||
fs::path fontPath("Resources/Pixelzone-E4MV4.ttf");
|
|
||||||
|
|
||||||
|
Text text;
|
||||||
|
|
||||||
void EditRooms() {
|
void EditRooms() {
|
||||||
|
|
||||||
@@ -62,8 +60,6 @@ void EditRooms() {
|
|||||||
|
|
||||||
|
|
||||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
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) {
|
if (SDL_Init(SDL_INIT_VIDEO) == false) {
|
||||||
SDL_Log("SDL could not initialize! SDL error: %s\n", SDL_GetError());
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!TTF_Init()) {
|
result = text.OpenFont("Resources/Pixelzone-E4MV4.ttf");
|
||||||
SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError());
|
|
||||||
return SDL_APP_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
font = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f);
|
if (result != SDL_APP_CONTINUE) {
|
||||||
text = TTF_RenderText_Blended(font, "Hello World!", 0, color);
|
return result;
|
||||||
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 (!game) {
|
if (!game) {
|
||||||
@@ -101,6 +87,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
|||||||
return SDL_APP_FAILURE;
|
return SDL_APP_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
text.SetText("Hello There", &window);
|
||||||
|
|
||||||
return SDL_APP_CONTINUE;
|
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_SetRenderDrawColor(window.GetRenderer(), 0, 0, 0, 255); // Set render draw color to black
|
||||||
SDL_RenderClear(window.GetRenderer()); // Clear the renderer
|
SDL_RenderClear(window.GetRenderer()); // Clear the renderer
|
||||||
int w = 0, h = 0;
|
text.Render();
|
||||||
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);
|
|
||||||
SDL_RenderPresent(window.GetRenderer()); // Render the screen
|
SDL_RenderPresent(window.GetRenderer()); // Render the screen
|
||||||
|
|
||||||
// game.Run();
|
// game.Run();
|
||||||
@@ -152,7 +132,7 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
|
|||||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||||
if (result == SDL_APP_SUCCESS) {
|
if (result == SDL_APP_SUCCESS) {
|
||||||
window.Close();
|
window.Close();
|
||||||
TTF_CloseFont(font);
|
text.CloseFont();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+53
@@ -0,0 +1,53 @@
|
|||||||
|
#include "Text.h"
|
||||||
|
#include <SDL3/SDL_init.h>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
|
||||||
|
SDL_AppResult Text::OpenFont(fs::path fontPath) {
|
||||||
|
|
||||||
|
if (!TTF_Init()) {
|
||||||
|
SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError());
|
||||||
|
return SDL_APP_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
loadedFont = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f);
|
||||||
|
if (!loadedFont) {
|
||||||
|
printf("Font not found! \n");
|
||||||
|
return SDL_APP_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
color = WHITE;
|
||||||
|
return SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Text::SetText(std::string text, Window *window) {
|
||||||
|
buffer += text;
|
||||||
|
|
||||||
|
SDL_DestroySurface(textRenderingSurface);
|
||||||
|
textRenderingSurface = TTF_RenderText_Blended(loadedFont, buffer.c_str(), 0, color);
|
||||||
|
|
||||||
|
if (textRenderingSurface) {
|
||||||
|
font_texture = SDL_CreateTextureFromSurface(window->GetRenderer(), textRenderingSurface);
|
||||||
|
SDL_DestroySurface(textRenderingSurface);
|
||||||
|
}
|
||||||
|
if (!font_texture) {
|
||||||
|
SDL_Log("Couldn't create text: %s\n", SDL_GetError());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::Render() {
|
||||||
|
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);
|
||||||
|
dst.x = ((w / scale) - dst.w) / 2;
|
||||||
|
dst.y = ((h / scale) - dst.h) / 2;
|
||||||
|
SDL_RenderTexture(window.GetRenderer(), font_texture, NULL, &dst);
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_init.h>
|
||||||
|
#include <SDL3_ttf/SDL_textengine.h>
|
||||||
|
#include <SDL3_ttf/SDL_ttf.h>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
#include "fileCommon.h"
|
||||||
|
#include "window.h"
|
||||||
|
|
||||||
|
#define WHITE {255, 255, 255, SDL_ALPHA_OPAQUE}
|
||||||
|
|
||||||
|
|
||||||
|
class Text {
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string buffer;
|
||||||
|
TTF_Font *loadedFont = nullptr;
|
||||||
|
|
||||||
|
SDL_Surface *textRenderingSurface = nullptr;
|
||||||
|
SDL_Color color;
|
||||||
|
|
||||||
|
SDL_Texture *font_texture = nullptr;
|
||||||
|
|
||||||
|
/// ToDo: Replace with dynamic settings / loaded font
|
||||||
|
fs::path fontPath;
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
SDL_AppResult OpenFont(fs::path fontPath);
|
||||||
|
bool SetText(std::string text, Window *window);
|
||||||
|
void Render();
|
||||||
|
void CloseFont() { TTF_CloseFont(loadedFont); }
|
||||||
|
};
|
||||||
+4
-2
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3/SDL_init.h>
|
#include <SDL3/SDL_init.h>
|
||||||
#include <SDL3/SDL_render.h>
|
#include <SDL3/SDL_render.h>
|
||||||
@@ -14,8 +16,8 @@ public:
|
|||||||
void Resize(int w, int h);
|
void Resize(int w, int h);
|
||||||
void Close();
|
void Close();
|
||||||
|
|
||||||
SDL_Window *GetWindow() { return window; }
|
SDL_Window *GetWindow() const { return window; }
|
||||||
SDL_Renderer *GetRenderer() { return renderer; }
|
SDL_Renderer *GetRenderer() const { return renderer; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user