aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-20 12:01:36 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-20 12:01:36 +0100
commit79d3a9f4311e6684b6df83a15ca7844f58c1959c (patch)
treeb2883e83f61cee9edf290a6a7228c7f0b1fbae8a /src/crepe/facade
parent9140b73e4af7aa925b53e4fb4e6aa7f4ea2e3385 (diff)
parent03aea832aa0bc2edba2cc5ab4d9f8eba42d355be (diff)
Merge remote-tracking branch 'origin/master' into max/game
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/CMakeLists.txt4
-rw-r--r--src/crepe/facade/DB.cpp14
-rw-r--r--src/crepe/facade/DB.h2
-rw-r--r--src/crepe/facade/Font.cpp21
-rw-r--r--src/crepe/facade/Font.h42
-rw-r--r--src/crepe/facade/FontFacade.cpp43
-rw-r--r--src/crepe/facade/FontFacade.h34
-rw-r--r--src/crepe/facade/SDLContext.cpp84
-rw-r--r--src/crepe/facade/SDLContext.h39
-rw-r--r--src/crepe/facade/Sound.cpp2
-rw-r--r--src/crepe/facade/SoundContext.cpp2
-rw-r--r--src/crepe/facade/Texture.cpp12
12 files changed, 264 insertions, 35 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
index 0598e16..243ae46 100644
--- a/src/crepe/facade/CMakeLists.txt
+++ b/src/crepe/facade/CMakeLists.txt
@@ -4,6 +4,8 @@ target_sources(crepe PUBLIC
SoundContext.cpp
SDLContext.cpp
DB.cpp
+ FontFacade.cpp
+ Font.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -12,5 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
SoundContext.h
SDLContext.h
DB.h
+ FontFacade.h
+ Font.h
)
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
index 95cf606..7a3e473 100644
--- a/src/crepe/facade/DB.cpp
+++ b/src/crepe/facade/DB.cpp
@@ -1,6 +1,6 @@
#include <cstring>
-#include "util/Log.h"
+#include "util/dbg.h"
#include "DB.h"
@@ -21,12 +21,6 @@ DB::DB(const string & path) {
const char * file = path.empty() ? NULL : path.c_str();
ret = this->db->open(this->db.get(), NULL, file, NULL, libdb::DB_BTREE, DB_CREATE, 0);
if (ret != 0) throw runtime_error(format("db->open: {}", libdb::db_strerror(ret)));
-
- // create cursor
- libdb::DBC * cursor;
- ret = this->db->cursor(this->db.get(), NULL, &cursor, 0);
- if (ret != 0) throw runtime_error(format("db->cursor: {}", libdb::db_strerror(ret)));
- this->cursor = {cursor, [](libdb::DBC * cursor) { cursor->close(cursor); }};
}
libdb::DBT DB::to_thing(const string & thing) const noexcept {
@@ -42,10 +36,10 @@ string DB::get(const string & key) {
libdb::DBT db_val;
memset(&db_val, 0, sizeof(libdb::DBT));
- int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST);
+ int ret = this->db->get(this->db.get(), NULL, &db_key, &db_val, 0);
if (ret == 0) return {static_cast<char *>(db_val.data), db_val.size};
- string err = format("cursor->get: {}", libdb::db_strerror(ret));
+ string err = format("db->get: {}", libdb::db_strerror(ret));
if (ret == DB_NOTFOUND) throw out_of_range(err);
else throw runtime_error(err);
}
@@ -54,7 +48,7 @@ void DB::set(const string & key, const string & value) {
libdb::DBT db_key = this->to_thing(key);
libdb::DBT db_val = this->to_thing(value);
int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0);
- if (ret != 0) throw runtime_error(format("cursor->get: {}", libdb::db_strerror(ret)));
+ if (ret != 0) throw runtime_error(format("db->get: {}", libdb::db_strerror(ret)));
}
bool DB::has(const std::string & key) {
diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h
index 115c0f1..84cdf19 100644
--- a/src/crepe/facade/DB.h
+++ b/src/crepe/facade/DB.h
@@ -61,8 +61,6 @@ public:
private:
//! RAII wrapper around \c DB struct
std::unique_ptr<libdb::DB, std::function<void(libdb::DB *)>> db;
- //! RAII wrapper around \c DBC struct
- std::unique_ptr<libdb::DBC, std::function<void(libdb::DBC *)>> cursor;
private:
/**
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp
new file mode 100644
index 0000000..771002f
--- /dev/null
+++ b/src/crepe/facade/Font.cpp
@@ -0,0 +1,21 @@
+#include <SDL2/SDL_ttf.h>
+
+#include "../api/Asset.h"
+#include "../api/Config.h"
+
+#include "Font.h"
+
+using namespace std;
+using namespace crepe;
+
+Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) {
+ const Config & config = Config::get_instance();
+ const std::string FONT_PATH = src.get_path();
+ TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size);
+ if (loaded_font == NULL) {
+ throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH));
+ }
+ this->font = {loaded_font, [](TTF_Font * close_font) { TTF_CloseFont(close_font); }};
+}
+
+TTF_Font * Font::get_font() const { return this->font.get(); }
diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h
new file mode 100644
index 0000000..b208d96
--- /dev/null
+++ b/src/crepe/facade/Font.h
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <SDL2/SDL_ttf.h>
+#include <memory>
+
+#include "../Resource.h"
+#include "../api/Config.h"
+
+namespace crepe {
+
+class Asset;
+/**
+ * \brief Resource for managing font creation and destruction
+ *
+ * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage.
+ * It loads a font from an Asset and manages its lifecycle. The font is automatically unloaded
+ * when this object is destroyed.
+ */
+class Font : public Resource {
+
+public:
+ /**
+ * \param src The Asset containing the font file path and metadata to load the font.
+ * \param mediator The Mediator object used for managing the SDL context or related systems.
+ */
+ Font(const Asset & src, Mediator & mediator);
+ /**
+ * \brief Gets the underlying TTF_Font resource.
+ *
+ * This function returns the raw pointer to the SDL_ttf TTF_Font object that represents
+ * the loaded font. This can be used with SDL_ttf functions to render text.
+ *
+ * \return The raw TTF_Font object wrapped in a unique pointer.
+ */
+ TTF_Font * get_font() const;
+
+private:
+ //! The SDL_ttf font object with custom deleter.
+ std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font = nullptr;
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp
new file mode 100644
index 0000000..87f95ab
--- /dev/null
+++ b/src/crepe/facade/FontFacade.cpp
@@ -0,0 +1,43 @@
+#include <fontconfig/fontconfig.h>
+#include <functional>
+#include <memory>
+#include <stdexcept>
+#include <string>
+
+#include "FontFacade.h"
+
+using namespace std;
+using namespace crepe;
+
+FontFacade::FontFacade() {
+ if (!FcInit()) throw runtime_error("Failed to initialize Fontconfig.");
+}
+
+FontFacade::~FontFacade() { FcFini(); }
+
+Asset FontFacade::get_font_asset(const string & font_family) {
+ FcPattern * raw_pattern
+ = FcNameParse(reinterpret_cast<const FcChar8 *>(font_family.c_str()));
+ if (raw_pattern == NULL) throw runtime_error("Failed to create font pattern.");
+
+ unique_ptr<FcPattern, function<void(FcPattern *)>> pattern{
+ raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }};
+
+ FcConfig * config = FcConfigGetCurrent();
+ if (config == NULL) throw runtime_error("Failed to get current Fontconfig configuration.");
+
+ FcResult result;
+ FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result);
+ if (raw_matched_pattern == NULL) throw runtime_error("No matching font found.");
+
+ unique_ptr<FcPattern, function<void(FcPattern *)>> matched_pattern
+ = {raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }};
+
+ FcChar8 * file_path = nullptr;
+ FcResult res = FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path);
+ if (res != FcResultMatch || file_path == NULL)
+ throw runtime_error("Failed to get font file path.");
+
+ string font_file_path = reinterpret_cast<const char *>(file_path);
+ return Asset(font_file_path);
+}
diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h
new file mode 100644
index 0000000..9761070
--- /dev/null
+++ b/src/crepe/facade/FontFacade.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <memory>
+
+#include "../api/Asset.h"
+
+namespace crepe {
+
+/**
+ *
+ * \brief Font facade class for converting font family names to absolute file paths
+ *
+ */
+class FontFacade {
+public:
+ FontFacade();
+ ~FontFacade();
+ FontFacade(const FontFacade & other) = delete;
+ FontFacade & operator=(const FontFacade & other) = delete;
+ FontFacade(FontFacade && other) noexcept = delete;
+ FontFacade & operator=(FontFacade && other) noexcept = delete;
+ /**
+ *
+ * \brief Facade function to convert a font_family into an asset.
+ *
+ * This function uses the FontConfig library to convert a font family name (Arial, Inter, Helvetica) and converts it to the font source path.
+ * This function returns a default font path if the font_family name doesnt exist or cant be found
+ * \param font_family Name of the font family name.
+ * \return Asset with filepath to the corresponding font.
+ */
+ Asset get_font_asset(const std::string & font_family);
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index a75adb2..64c1fe2 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -7,10 +7,11 @@
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
+#include <SDL2/SDL_ttf.h>
+#include <SDL2/SDL_video.h>
#include <array>
#include <cmath>
#include <cstddef>
-#include <cstdint>
#include <functional>
#include <memory>
#include <stdexcept>
@@ -19,8 +20,12 @@
#include "../api/Color.h"
#include "../api/Config.h"
#include "../api/Sprite.h"
-#include "../util/Log.h"
+#include "../util/dbg.h"
+#include "api/Text.h"
+#include "api/Transform.h"
+#include "facade/Font.h"
#include "manager/Mediator.h"
+#include "util/AbsolutePosition.h"
#include "SDLContext.h"
#include "Texture.h"
@@ -59,6 +64,10 @@ SDLContext::SDLContext(Mediator & mediator) {
throw runtime_error("SDLContext: SDL_image could not initialize!");
}
+ if (TTF_Init() == -1) {
+ throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError()));
+ }
+
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
mediator.sdl_context = *this;
@@ -73,14 +82,14 @@ SDLContext::~SDLContext() {
// TODO: how are we going to ensure that these are called from the same
// thread that SDL_Init() was called on? This has caused problems for me
// before.
+ TTF_Quit();
IMG_Quit();
SDL_Quit();
}
Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) {
- if (!LOOKUP_TABLE.contains(sdl_key)) return Keycode::NONE;
-
- return LOOKUP_TABLE.at(sdl_key);
+ if (!lookup_table.contains(sdl_key)) return Keycode::NONE;
+ return lookup_table.at(sdl_key);
}
const keyboard_state_t & SDLContext::get_keyboard_state() {
@@ -135,6 +144,8 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {
= (ctx.sprite.aspect_ratio == 0) ? ctx.texture.get_ratio() : ctx.sprite.aspect_ratio;
vec2 size = data.size;
+ vec2 screen_pos = ctx.pos;
+
if (data.size.x == 0 && data.size.y != 0) {
size.x = data.size.y * aspect_ratio;
}
@@ -143,10 +154,15 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {
}
size *= cam_aux_data.render_scale * ctx.img_scale * data.scale_offset;
- vec2 screen_pos = (ctx.pos + data.position_offset - cam_aux_data.cam_pos
- + (cam_aux_data.zoomed_viewport) / 2)
- * cam_aux_data.render_scale
- - size / 2 + cam_aux_data.bar_size;
+ if (ctx.sprite.data.world_space) {
+ screen_pos = (screen_pos - cam_aux_data.cam_pos + cam_aux_data.zoomed_viewport / 2)
+ * cam_aux_data.render_scale
+ - size / 2 + cam_aux_data.bar_size;
+ } else {
+ screen_pos
+ = (screen_pos + cam_aux_data.zoomed_viewport / 2) * cam_aux_data.render_scale
+ - size / 2 + cam_aux_data.bar_size;
+ }
return SDL_FRect{
.x = screen_pos.x,
@@ -186,6 +202,52 @@ void SDLContext::draw(const RenderContext & ctx) {
angle, NULL, render_flip);
}
+void SDLContext::draw_text(const RenderText & data) {
+
+ const Text & text = data.text;
+ const Font & font = data.font;
+ vec2 absoluut_pos = AbsolutePosition::get_position(data.transform, data.text.offset);
+ std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>> font_surface;
+ std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> font_texture;
+
+ SDL_Color color{
+ .r = text.data.text_color.r,
+ .g = text.data.text_color.g,
+ .b = text.data.text_color.b,
+ .a = text.data.text_color.a,
+ };
+ SDL_Surface * tmp_font_surface
+ = TTF_RenderText_Solid(font.get_font(), text.text.c_str(), color);
+ if (tmp_font_surface == NULL) {
+ throw runtime_error(format("draw_text: font surface error: {}", SDL_GetError()));
+ }
+ font_surface = {tmp_font_surface, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }};
+
+ SDL_Texture * tmp_font_texture
+ = SDL_CreateTextureFromSurface(this->game_renderer.get(), font_surface.get());
+ if (tmp_font_texture == NULL) {
+ throw runtime_error(format("draw_text: font texture error: {}", SDL_GetError()));
+ }
+ font_texture
+ = {tmp_font_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }};
+
+ vec2 size = text.dimensions * cam_aux_data.render_scale * data.transform.scale;
+ vec2 screen_pos
+ = (absoluut_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2)
+ * cam_aux_data.render_scale
+ - size / 2 + cam_aux_data.bar_size;
+
+ SDL_FRect dstrect{
+ .x = screen_pos.x,
+ .y = screen_pos.y,
+ .w = size.x,
+ .h = size.y,
+ };
+
+ SDL_RenderCopyExF(this->game_renderer.get(), font_texture.get(), NULL, &dstrect,
+ data.transform.rotation, NULL, SDL_FLIP_NONE);
+}
+
void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) {
const Camera::Data & cam_data = cam.data;
@@ -411,3 +473,7 @@ void SDLContext::set_color_texture(const Texture & texture, const Color & color)
SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b);
SDL_SetTextureAlphaMod(texture.get_img(), color.a);
}
+
+Asset SDLContext::get_font_from_name(const std::string & font_family) {
+ return this->font_facade.get_font_asset(font_family);
+}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 8f4760e..e570073 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -12,6 +12,7 @@
#include <unordered_map>
#include "../types.h"
+#include "EventData.h"
#include "api/Camera.h"
#include "api/Color.h"
#include "api/KeyCodes.h"
@@ -19,9 +20,13 @@
#include "api/Transform.h"
#include "EventData.h"
+#include "FontFacade.h"
+#include "types.h"
namespace crepe {
class Texture;
+class Text;
+class Font;
class Mediator;
/**
@@ -69,12 +74,11 @@ public:
const double & scale;
};
-public:
- /**
- * \brief Gets the singleton instance of SDLContext.
- * \return Reference to the SDLContext instance.
- */
- static SDLContext & get_instance();
+ struct RenderText {
+ const Text & text;
+ const Font & font;
+ const Transform & transform;
+ };
public:
SDLContext(const SDLContext &) = delete;
@@ -184,6 +188,13 @@ public:
*/
void draw(const RenderContext & ctx);
+ /**
+ * \brief draws a text to the screen
+ *
+ * \param data Reference to the rendering data needed to draw
+ */
+ void draw_text(const RenderText & data);
+
//! Clears the screen, preparing for a new frame.
void clear_screen();
@@ -242,10 +253,24 @@ private:
CameraAuxiliaryData cam_aux_data;
private:
+ //! instance of the font_facade
+ FontFacade font_facade{};
+
+public:
+ /**
+ * \brief Function to Get asset from font_family
+ *
+ * This function uses the FontFacade function to convert a font_family to an asset.
+ *
+ * \param font_family name of the font style that needs to be used (will return an asset with default font path of the font_family doesnt exist)
+ *
+ * \return asset with the font style absolute path
+ */
+ Asset get_font_from_name(const std::string & font_family);
//! variable to store the state of each key (true = pressed, false = not pressed)
keyboard_state_t keyboard_state;
//! lookup table for converting SDL_SCANCODES to Keycodes
- const std::unordered_map<SDL_Scancode, Keycode> LOOKUP_TABLE
+ const std::unordered_map<SDL_Scancode, Keycode> lookup_table
= {{SDL_SCANCODE_SPACE, Keycode::SPACE},
{SDL_SCANCODE_APOSTROPHE, Keycode::APOSTROPHE},
{SDL_SCANCODE_COMMA, Keycode::COMMA},
diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp
index 97e455e..b1e6463 100644
--- a/src/crepe/facade/Sound.cpp
+++ b/src/crepe/facade/Sound.cpp
@@ -1,5 +1,5 @@
#include "../api/Asset.h"
-#include "../util/Log.h"
+#include "../util/dbg.h"
#include "Sound.h"
diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp
index b1f8cb3..5091e07 100644
--- a/src/crepe/facade/SoundContext.cpp
+++ b/src/crepe/facade/SoundContext.cpp
@@ -1,4 +1,4 @@
-#include "../util/Log.h"
+#include "../util/dbg.h"
#include "SoundContext.h"
diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp
index b63403d..06caa54 100644
--- a/src/crepe/facade/Texture.cpp
+++ b/src/crepe/facade/Texture.cpp
@@ -1,10 +1,11 @@
-#include "../util/Log.h"
-#include "facade/SDLContext.h"
-#include "manager/Mediator.h"
+#include "../Resource.h"
+#include "../facade/SDLContext.h"
+#include "../manager/Mediator.h"
+#include "../types.h"
+#include "../util/dbg.h"
-#include "Resource.h"
+#include "SDLContext.h"
#include "Texture.h"
-#include "types.h"
using namespace crepe;
using namespace std;
@@ -23,6 +24,7 @@ Texture::~Texture() {
}
const ivec2 & Texture::get_size() const noexcept { return this->size; }
+
const float & Texture::get_ratio() const noexcept { return this->aspect_ratio; }
SDL_Texture * Texture::get_img() const noexcept { return this->texture.get(); }