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

drawing to screen now works

This commit is contained in:
ineslelin 2025-01-28 17:49:51 +01:00
parent 5b14a229af
commit 639c3bb38a
3 changed files with 56 additions and 28 deletions

View File

@ -5,8 +5,7 @@
#include <SDL3/SDL_surface.h> #include <SDL3/SDL_surface.h>
#include <map> #include <map>
#include <string> #include <string>
#include <iostream>
#include <VEGO.h>
enum DisplayOptions enum DisplayOptions
{ {
@ -18,10 +17,10 @@ enum DisplayOptions
struct Color struct Color
{ {
int r; Uint8 r;
int g; Uint8 g;
int b; Uint8 b;
int a; Uint8 a;
}; };
struct Rect struct Rect
@ -37,7 +36,7 @@ class GameInternal;
class TextManager class TextManager
{ {
public: public:
TextManager() {} TextManager() : font_cache() { }
~TextManager() { ~TextManager() {
for (auto& it : this->font_cache) { for (auto& it : this->font_cache) {
TTF_CloseFont(it.second); TTF_CloseFont(it.second);
@ -49,6 +48,8 @@ public:
TextManager(TextManager const&) = delete; TextManager(TextManager const&) = delete;
void operator=(TextManager const&) = delete; void operator=(TextManager const&) = delete;
bool isTextRendered = false;
TTF_Font* loadFont(const char* filepath); TTF_Font* loadFont(const char* filepath);
// TODO: probably gotta change sdl surface since this is a wrapper func for the dev // TODO: probably gotta change sdl surface since this is a wrapper func for the dev
@ -58,7 +59,7 @@ public:
std::map<const char*, TTF_Font*> font_cache; std::map<const char*, TTF_Font*> font_cache;
private: private:
SDL_Texture* CreateRenderedTexture(TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth); SDL_Texture* CreateRenderedTexture(GameInternal* game, TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth);
SDL_Surface* RenderSolid(TTF_Font* font, std::string text, SDL_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, SDL_Color fg, SDL_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, SDL_Color fg, int wrapWidth); SDL_Surface* RenderBlended(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth);

View File

@ -4,6 +4,7 @@
#include "AssetManager.h" #include "AssetManager.h"
#include "RenderManager.h" #include "RenderManager.h"
#include <SDL3_mixer/SDL_mixer.h> #include <SDL3_mixer/SDL_mixer.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "SDL3/SDL_init.h" #include "SDL3/SDL_init.h"
#include "SoundManager.h" #include "SoundManager.h"
#include "Entity.h" #include "Entity.h"
@ -88,6 +89,13 @@ SDL_AppResult GameInternal::init(const char* title, int xpos, int ypos, int widt
Mix_Volume(-1, MIX_MAX_VOLUME); Mix_Volume(-1, MIX_MAX_VOLUME);
Mix_AllocateChannels(16); Mix_AllocateChannels(16);
if(!TTF_Init())
{
std::cout << "ERROR: SDL_TTF couldnt be initialized! " << SDL_GetError() << std::endl;
SDL_ClearError();
return SDL_APP_FAILURE;
}
// loading sounds // loading sounds
// assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav"); // assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
// assets->addSoundEffect("steps", "assets/sound/steps.wav"); // assets->addSoundEffect("steps", "assets/sound/steps.wav");

View File

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fstream> #include <fstream>
#include <string>
#include "GameInternal.h" #include "GameInternal.h"
#include "AssetManager.h" #include "AssetManager.h"
@ -11,21 +12,18 @@ using json = nlohmann::json;
TTF_Font* TextManager::loadFont(const char *filepath) TTF_Font* TextManager::loadFont(const char *filepath)
{ {
if(!TTF_Init()) // auto it = this->font_cache.find(filepath);
std::cerr << "Failed to initialize SDL_TTF'" << std::endl; //
// if (it != this->font_cache.end()) {
auto it = this->font_cache.find(filepath); // return it->second;
// }
if (it != this->font_cache.end()) {
return it->second;
}
auto font = TTF_OpenFont(filepath, 12); // setting fontsize to 12 for now auto font = TTF_OpenFont(filepath, 12); // setting fontsize to 12 for now
if (font == NULL) if (font == NULL)
std::cerr << "Couldn't load font '" << filepath << "'" << std::endl; std::cerr << "Couldn't load font '" << filepath << "'" << std::endl;
this->font_cache.emplace(filepath, font); // this->font_cache.emplace(filepath, font);
std::cout << "Loaded font at " << filepath << std::endl; std::cout << "Loaded font at " << filepath << std::endl;
@ -41,7 +39,8 @@ void TextManager::RenderText(GameInternal* game, std::string font, std::string t
SDL_FRect sdlSrcRect = { src.x, src.y, src.w, src.h }; SDL_FRect sdlSrcRect = { src.x, src.y, src.w, src.h };
SDL_FRect sdlDstRect = { dst.x, dst.y, dst.w, dst.h }; SDL_FRect sdlDstRect = { dst.x, dst.y, dst.w, dst.h };
SDL_RenderTexture(VEGO_Game().renderer, CreateRenderedTexture(ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect); SDL_RenderTexture(game->renderer, CreateRenderedTexture(game, ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect);
SDL_RenderPresent(game->renderer);
} }
void TextManager::RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth, Rect src, Rect dst) void TextManager::RenderTextFromFile(GameInternal* game, std::string font, std::string filepath, int id, DisplayOptions displayOptions, Color fg, Color bg, int wrapWidth, Rect src, Rect dst)
@ -54,21 +53,41 @@ void TextManager::RenderTextFromFile(GameInternal* game, std::string font, std::
SDL_FRect sdlDstRect = { dst.x, dst.y, dst.w, dst.h }; SDL_FRect sdlDstRect = { dst.x, dst.y, dst.w, dst.h };
std::ifstream f(filepath); std::ifstream f(filepath);
json data = json::parse(f);; if (!f.is_open()) {
std::cerr << "Failed to open file: " << filepath << std::endl;
return;
}
auto it = std::find_if(data.begin(), data.end(), [id](const nlohmann::json& obj){ json data;
return obj.contains("id") && obj["id"] == id; try {
}); data = json::parse(f);
} catch (const json::parse_error& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return;
}
if(it == data.end() || !it->contains("line")) std::string text;
std::cerr << "Object with id " << id << " not found or 'line' not present!" << std::endl; bool found = false;
for (auto it = data.begin(); it != data.end(); ++it) {
std::cout << "Key: " << it.key() << ", Value: " << it.value() << std::endl;
if (it.key() == std::to_string(id)) {
text = it.value();
found = true;
break;
}
}
std::string text = (*it)["line"]; if(!found)
{
std::cerr << "Object with id " << id << " not found!" << std::endl;
return;
}
SDL_RenderTexture(VEGO_Game().renderer, CreateRenderedTexture(ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect); SDL_RenderTexture(game->renderer, CreateRenderedTexture(game, ttfFont, text, displayOptions, sdlFg, sdlBg, wrapWidth), &sdlSrcRect, &sdlDstRect);
SDL_RenderPresent(game->renderer);
} }
SDL_Texture* TextManager::CreateRenderedTexture(TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth) SDL_Texture* TextManager::CreateRenderedTexture(GameInternal* game, TTF_Font* font, std::string text, DisplayOptions displayOptions, SDL_Color fg, SDL_Color bg, int wrapWidth)
{ {
SDL_Surface* surface = nullptr; SDL_Surface* surface = nullptr;
@ -94,7 +113,7 @@ SDL_Texture* TextManager::CreateRenderedTexture(TTF_Font* font, std::string text
if(!surface) if(!surface)
std::cerr << "Error when rendering text!" << std::endl; std::cerr << "Error when rendering text!" << std::endl;
return SDL_CreateTextureFromSurface(VEGO_Game().renderer, surface); return SDL_CreateTextureFromSurface(game->renderer, surface);
} }
SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth) SDL_Surface* TextManager::RenderSolid(TTF_Font* font, std::string text, SDL_Color fg, int wrapWidth)