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

created textmanager class

used to load fonts, will contain wrapper functions of sdl ttf functions
This commit is contained in:
ineslelin 2025-01-23 17:00:06 +01:00
parent 7c50c8d1fb
commit d4374ef46d
7 changed files with 92 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <SDL3/SDL_render.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <map>
#include <string>
#include <functional>
@ -33,15 +34,19 @@ public:
// sound management
void addSoundEffect(std::string id, const char* path);
void addMusic(std::string id, const char* path);
Mix_Chunk* getSound(std::string id);
void addFont(std::string id, const char* path);
Mix_Chunk* getSound(std::string id); // ??? do we not have a getsound implemented????? too busy to look now, where do we get the soundeffects???
Mix_Music* getMusic(std::string id);
TTF_Font* getFont(std::string id);
private:
Manager* man;
std::map<std::string, Mix_Chunk*> soundEffects;
std::map<std::string, Mix_Music*> music;
std::map<std::string, TTF_Font*> fonts;
};

15
include/DialogHandler.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef CHICKENGAME_DIALOGHANDLER_H
#define CHICKENGAME_DIALOGHANDLER_H
class DialogHandler
{
public:
void displayDialog();
private:
};
#endif //CHICKENGAME_DIALOGHANDLER_H

View File

@ -18,6 +18,7 @@ class AssetManager;
class CollisionHandler;
class TextureManager;
class SoundManager;
class TextManager;
class Map;
class Game;
@ -43,6 +44,7 @@ public:
/* static */ AssetManager* assets;
/* static */ TextureManager* textureManager;
/* static */ SoundManager* soundManager;
/* static */ TextManager* textManager;
Manager manager;
RenderManager renderManager;

26
include/TextManager.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef CHICKENGAME_TEXTMANAGER_H
#define CHICKENGAME_TEXTMANAGER_H
#include <SDL3_ttf/SDL_ttf.h>
#include <map>
class TextManager
{
public:
TextManager() {}
~TextManager() {
for (auto& it : this->font_cache) {
TTF_CloseFont(it.second);
}
}
TextManager(TextManager const&) = delete;
void operator=(TextManager const&) = delete;
TTF_Font* loadFont(const char* filepath);
std::map<const char*, TTF_Font*> font_cache;
private:
};
#endif //CHICKENGAME_TEXTMANAGER_H

View File

@ -2,6 +2,7 @@
#include "TextureManager.h"
#include "SoundManager.h"
#include "TextManager.h"
#include "ProjectileComponent.h"
#include "GameInternal.h"
@ -31,9 +32,9 @@ void AssetManager::addMusic(std::string id, const char* path)
music.emplace(id, this->man->getGame()->soundManager->loadMusic(path));
}
Mix_Chunk* AssetManager::getSound(std::string id) {
return soundEffects.at(id);
void AssetManager::addFont(std::string id, const char *path)
{
fonts.emplace(id, this->man->getGame()->textManager->loadFont(path));
}
Mix_Music* AssetManager::getMusic(std::string id)
@ -41,6 +42,16 @@ Mix_Music* AssetManager::getMusic(std::string id)
return music.at(id);
}
Mix_Chunk* AssetManager::getSound(std::string id)
{
return soundEffects.at(id);
}
TTF_Font* AssetManager::getFont(std::string id)
{
return fonts.at(id);
}
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, float speed, Textures textureEnum, Entity* owner) {
auto& projectile(man->addEntity());

5
src/DialogHandler.cpp Normal file
View File

@ -0,0 +1,5 @@
//
// Created by PC on 21.01.2025.
//
#include "../include/DialogHandler.h"

23
src/TextManager.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "TextManager.h"
#include <iostream>
TTF_Font* TextManager::loadFont(const char *filepath)
{
auto it = this->font_cache.find(filepath);
if (it != this->font_cache.end()) {
return it->second;
}
auto font = TTF_OpenFont(filepath, 12); // setting fontsize to 12 for now
if (font == NULL)
std::cerr << "Couldn't load font '" << filepath << "'" << std::endl;
this->font_cache.emplace(filepath, font);
std::cout << "Loaded font at " << filepath << std::endl;
return font;
}