0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

add in json library

This commit is contained in:
ineslelin 2025-01-24 22:43:02 +01:00
parent 3fd7b383b9
commit f5bbabab8d
5 changed files with 55 additions and 19 deletions

3
.gitmodules vendored
View File

@ -17,3 +17,6 @@
path = docs/doxygen-awesome-css path = docs/doxygen-awesome-css
url = https://github.com/jothepro/doxygen-awesome-css.git url = https://github.com/jothepro/doxygen-awesome-css.git
[submodule "extern/json"]
path = extern/json
url = https://github.com/nlohmann/json.git

View File

@ -25,11 +25,13 @@ add_subdirectory(extern/SDL_mixer EXCLUDE_FROM_ALL)
add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL) add_subdirectory(extern/SDL_ttf EXCLUDE_FROM_ALL)
add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL) add_subdirectory(extern/tmxlite/tmxlite EXCLUDE_FROM_ALL)
file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp) file(GLOB_RECURSE SOURCES ${ENGINE_SOURCE_DIR}/src/*.cpp)
add_library(${PROJECT_NAME} ${SOURCES}) add_library(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) target_include_directories(${PROJECT_NAME} PUBLIC
${ENGINE_INCLUDE_DIR}
${ENGINE_SOURCE_DIR}/extern/json/include
)
target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL functionality has a wrapper target_link_libraries(${PROJECT_NAME} PUBLIC # should be private when all SDL functionality has a wrapper
SDL3::SDL3-static SDL3::SDL3-static

1
extern/json vendored Submodule

@ -0,0 +1 @@
Subproject commit d0789e365da6e410f52387f19eb218dc5b6ccbf2

View File

@ -4,6 +4,7 @@
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include <SDL3/SDL_surface.h> #include <SDL3/SDL_surface.h>
#include <map> #include <map>
#include <string>
enum DisplayOptions enum DisplayOptions
{ {
@ -49,14 +50,18 @@ public:
* */ * */
// TODO: change the sdl surface, i just dont know what i gotta change it to lol // TODO: change the sdl surface, i just dont know what i gotta change it to lol
SDL_Surface* RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth); SDL_Surface* RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth);
/*
* takes everything + a filepath to a dialogfile and the id for the dialog
* */
SDL_Surface* RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth);
std::map<const char*, TTF_Font*> font_cache; std::map<const char*, TTF_Font*> font_cache;
private: private:
SDL_Surface* RenderSolid(TTF_Font* font, std::string text, Color fg, int wrapWidth); SDL_Surface* RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth);
SDL_Surface* RenderShaded(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth); SDL_Surface* RenderShaded(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth);
SDL_Surface* RenderBlended(TTF_Font* font, std::string text, Color fg, int wrapWidth); SDL_Surface* RenderBlended(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth);
SDL_Surface* RenderLCD(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth); SDL_Surface* RenderLCD(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth);
}; };
#endif //CHICKENGAME_TEXTMANAGER_H #endif //CHICKENGAME_TEXTMANAGER_H

View File

@ -1,10 +1,14 @@
#include "TextManager.h" #include "TextManager.h"
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp>
#include <fstream>
#include "GameInternal.h" #include "GameInternal.h"
#include "AssetManager.h" #include "AssetManager.h"
using json = nlohmann::json;
TTF_Font* TextManager::loadFont(const char *filepath) TTF_Font* TextManager::loadFont(const char *filepath)
{ {
if(!TTF_Init()) if(!TTF_Init())
@ -31,28 +35,49 @@ TTF_Font* TextManager::loadFont(const char *filepath)
SDL_Surface* TextManager::RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth) SDL_Surface* TextManager::RenderText(GameInternal* game, std::string font, std::string text, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth)
{ {
TTF_Font* ttfFont = game->assets->getFont(font); TTF_Font* ttfFont = game->assets->getFont(font);
SDL_Surface* surface = nullptr;
SDL_Color sdlFg = {fg.r, fg.g, fg.b, fg.a};
SDL_Color sdlBg = {bg.r, bg.g, bg.b, bg.a};
switch(displayOptions) switch(displayOptions)
{ {
case SOLID: case SOLID:
RenderSolid(font, text, fg, wrapWidth); surface = RenderSolid(ttfFont, text, sdlFg, wrapWidth);
return; break;
case SHADED: case SHADED:
RenderShaded(font, text, fg, bg, wrapWidth); surface = RenderShaded(ttfFont, text, sdlFg, sdlBg, wrapWidth);
return; break;
case BLENDED: case BLENDED:
RenderBlended(font, text, fg, wrapWidth) surface = RenderBlended(ttfFont, text, sdlFg, wrapWidth);
return; break;
case LCD: case LCD:
RenderLCD(font, text, fg, bg, wrapWidth); surface = RenderLCD(ttfFont, text, sdlFg, sdlBg, wrapWidth);
return; break;
default: default:
std::cerr << "Invalid display option!" << std::endl; std::cerr << "Invalid display option!" << std::endl;
return; break;
}
} }
SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, Color fg, int wrapWidth) if(!surface)
std::cerr << "Error when rendering text!" << std::endl;
return surface;
}
SDL_Surface* TextManager::RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth)
{
std::ifstream f(filepath);
json data = json::parse(f);
// logic to parse dialogline
std::string text = "placeholder so i dont get compile errors :3";
return RenderText(game, font, text, displayOptions, fg, bg, wrapWidth);
}
SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth)
{ {
if(wrapWidth == -1) if(wrapWidth == -1)
return TTF_RenderText_Solid(font, text.c_str(), text.length(), fg); return TTF_RenderText_Solid(font, text.c_str(), text.length(), fg);
@ -60,7 +85,7 @@ SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, Color fg
return TTF_RenderText_Solid_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth); return TTF_RenderText_Solid_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth);
} }
SDL_Surface* TextManager::RenderShaded(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth) SDL_Surface* TextManager::RenderShaded(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth)
{ {
if(wrapWidth == -1) if(wrapWidth == -1)
return TTF_RenderText_Shaded(font, text.c_str(), text.length(), fg, bg); return TTF_RenderText_Shaded(font, text.c_str(), text.length(), fg, bg);
@ -68,7 +93,7 @@ SDL_Surface* TextManager::RenderShaded(TTF_Font* font, std::string text, Color f
return TTF_RenderText_Shaded_Wrapped(font, text.c_str(), text.length(), fg, bg, wrapWidth); return TTF_RenderText_Shaded_Wrapped(font, text.c_str(), text.length(), fg, bg, wrapWidth);
} }
SDL_Surface* TextManager::RenderBlended(TTF_Font* font, std::string text, Color fg, int wrapWidth) SDL_Surface* TextManager::RenderBlended(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth)
{ {
if(wrapWidth == -1) if(wrapWidth == -1)
return TTF_RenderText_Blended(font, text.c_str(), text.length(), fg); return TTF_RenderText_Blended(font, text.c_str(), text.length(), fg);
@ -76,7 +101,7 @@ SDL_Surface* TextManager::RenderBlended(TTF_Font* font, std::string text, Color
return TTF_RenderText_Blended_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth); return TTF_RenderText_Blended_Wrapped(font, text.c_str(), text.length(), fg, wrapWidth);
} }
SDL_Surface* TextManager::RenderLCD(TTF_Font* font, std::string text, Color fg, Color bg, int wrapWidth) SDL_Surface* TextManager::RenderLCD(TTF_Font* font, std::string text, SDL_Color fg, SDL_Color bg, int wrapWidth)
{ {
if(wrapWidth == -1) if(wrapWidth == -1)
return TTF_RenderText_LCD(font, text.c_str(), text.length(), fg, bg); return TTF_RenderText_LCD(font, text.c_str(), text.length(), fg, bg);