From 289ecc3a3829e9b3acff0b1778f75bc526173977 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 7 Dec 2024 19:14:34 +0100 Subject: changed double to float --- src/crepe/Particle.cpp | 4 ++-- src/crepe/Particle.h | 8 ++++---- src/crepe/api/ParticleEmitter.cpp | 5 +++-- src/crepe/api/ParticleEmitter.h | 29 +++++++++++++++------------- src/crepe/system/ParticleSystem.cpp | 38 ++++++++++++++++++------------------- src/crepe/system/ParticleSystem.h | 6 +++--- 6 files changed, 47 insertions(+), 43 deletions(-) diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index 485a0d4..ce000a1 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -2,8 +2,8 @@ using namespace crepe; -void Particle::reset(uint32_t lifespan, const vec2 & position, const vec2 & velocity, - double angle) { +void Particle::reset(unsigned int lifespan, const vec2 & position, const vec2 & velocity, + float angle) { // Initialize the particle state this->time_in_life = 0; this->lifespan = lifespan; diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index d0397c9..4fa93a1 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -24,13 +24,13 @@ public: //! Accumulated force affecting the particle over time. vec2 force_over_time; //! Total lifespan of the particle in milliseconds. - uint32_t lifespan; + unsigned int lifespan; //! Active state of the particle; true if it is in use, false otherwise. bool active = false; //! The time the particle has been alive, in milliseconds. - uint32_t time_in_life = 0; + unsigned int time_in_life = 0; //! The angle at which the particle is oriented or moving. - double angle = 0; + float angle = 0; /** * \brief Resets the particle with new properties. @@ -43,7 +43,7 @@ public: * \param velocity The initial velocity of the particle. * \param angle The angle of the particle's trajectory or orientation. */ - void reset(uint32_t lifespan, const vec2 & position, const vec2 & velocity, double angle); + void reset(unsigned int lifespan, const vec2 & position, const vec2 & velocity, float angle); /** * \brief Updates the particle's state. * diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 90b77a0..1cfdceb 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -1,10 +1,11 @@ #include "ParticleEmitter.h" +#include "api/Sprite.h" using namespace crepe; -ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Data & data) +ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, const Data & data) : Component(game_object_id), - data(data) { + sprite(sprite), data(data) { for (size_t i = 0; i < this->data.max_particles; i++) { this->data.particles.emplace_back(); } diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index b83fd61..e8fa15e 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "Component.h" @@ -26,15 +27,18 @@ public: */ struct Boundary { //! boundary width (midpoint is emitter location) - double width = 0.0; + float width = INFINITY; //! boundary height (midpoint is emitter location) - double height = 0.0; + float height = INFINITY; //! boundary offset from particle emitter location vec2 offset; //! reset on exit or stop velocity and set max postion bool reset_on_exit = false; }; + //! sprite reference of displayed sprite + const Sprite & sprite; + /** * \brief Holds parameters that control particle emission. * @@ -45,29 +49,28 @@ public: //! position of the emitter vec2 position; //! maximum number of particles - const unsigned int max_particles = 0; + const unsigned int max_particles = 256; //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) - double emission_rate = 0; + float emission_rate = 1; //! min speed of the particles - double min_speed = 0; + float min_speed = 1; //! min speed of the particles - double max_speed = 0; + float max_speed = 2; //! min angle of particle emission - double min_angle = 0; + float min_angle = 0; //! max angle of particle emission - double max_angle = 0; + float max_angle = 0; //! begin Lifespan of particle (only visual) - double begin_lifespan = 0.0; + float begin_lifespan = 0.0; //! end Lifespan of particle - double end_lifespan = 0.0; + float end_lifespan = 10.0; //! force over time (physics) vec2 force_over_time; //! particle boundary Boundary boundary; //! collection of particles std::vector particles; - //! sprite reference - const Sprite & sprite; + }; public: @@ -75,7 +78,7 @@ public: * \param game_object_id Identifier for the game object using this emitter. * \param data Configuration data defining particle properties. */ - ParticleEmitter(game_object_id_t game_object_id, const Data & data); + ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite,const Data & data); public: //! Configuration data for particle emission settings. diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index b14c52f..596b4b0 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -21,9 +21,9 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - int updates = calculate_update(this->update_count, emitter.data.emission_rate); + int updates = this->calculate_update(this->update_count, emitter.data.emission_rate); for (size_t i = 0; i < updates; i++) { - emit_particle(emitter, transform); + this->emit_particle(emitter, transform); } // Update all particles @@ -34,7 +34,7 @@ void ParticleSystem::update() { } // Check if within boundary - check_bounds(emitter, transform); + this->check_bounds(emitter, transform); } this->update_count = (this->update_count + 1) % this->MAX_UPDATE_COUNT; @@ -61,9 +61,9 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & } } -int ParticleSystem::calculate_update(int count, double emission) const { - double integer_part = std::floor(emission); - double fractional_part = emission - integer_part; +int ParticleSystem::calculate_update(int count, float emission) const { + float integer_part = std::floor(emission); + float fractional_part = emission - integer_part; if (fractional_part > 0) { int denominator = static_cast(1.0 / fractional_part); @@ -75,13 +75,13 @@ int ParticleSystem::calculate_update(int count, double emission) const { void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; - double half_width = emitter.data.boundary.width / 2.0; - double half_height = emitter.data.boundary.height / 2.0; + float half_width = emitter.data.boundary.width / 2.0; + float half_height = emitter.data.boundary.height / 2.0; - const double LEFT = offset.x - half_width; - const double RIGHT = offset.x + half_width; - const double TOP = offset.y - half_height; - const double BOTTOM = offset.y + half_height; + const float LEFT = offset.x - half_width; + const float RIGHT = offset.x + half_width; + const float TOP = offset.y - half_height; + const float BOTTOM = offset.y + half_height; for (Particle & particle : emitter.data.particles) { const vec2 & position = particle.position; @@ -102,25 +102,25 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t } } -double ParticleSystem::generate_random_angle(double min_angle, double max_angle) const { +float ParticleSystem::generate_random_angle(float min_angle, float max_angle) const { if (min_angle == max_angle) { return min_angle; } else if (min_angle < max_angle) { return min_angle - + static_cast(std::rand() % static_cast(max_angle - min_angle)); + + static_cast(std::rand() % static_cast(max_angle - min_angle)); } else { - double angle_offset = (360 - min_angle) + max_angle; - double random_angle - = min_angle + static_cast(std::rand() % static_cast(angle_offset)); + float angle_offset = (360 - min_angle) + max_angle; + float random_angle + = min_angle + static_cast(std::rand() % static_cast(angle_offset)); return (random_angle >= 360) ? random_angle - 360 : random_angle; } } -double ParticleSystem::generate_random_speed(double min_speed, double max_speed) const { +float ParticleSystem::generate_random_speed(float min_speed, float max_speed) const { if (min_speed == max_speed) { return min_speed; } else { return min_speed - + static_cast(std::rand() % static_cast(max_speed - min_speed)); + + static_cast(std::rand() % static_cast(max_speed - min_speed)); } } diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 068f01c..2adb0f0 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -39,7 +39,7 @@ private: * \param emission Emission rate. * \return The number of particles to emit. */ - int calculate_update(int count, double emission) const; + int calculate_update(int count, float emission) const; /** * \brief Checks whether particles are within the emitter’s boundary, resets or stops @@ -57,7 +57,7 @@ private: * \param max_angle Maximum emission angle in degrees. * \return Random angle in degrees. */ - double generate_random_angle(double min_angle, double max_angle) const; + float generate_random_angle(float min_angle, float max_angle) const; /** * \brief Generates a random speed for particle emission within the specified range. @@ -66,7 +66,7 @@ private: * \param max_speed Maximum emission speed. * \return Random speed. */ - double generate_random_speed(double min_speed, double max_speed) const; + float generate_random_speed(float min_speed, float max_speed) const; private: //! Counter to count updates to determine how many times emit_particle is -- cgit v1.2.3 From 8c81bf4a33a13fc21dca7e3fe78a6dc334ac964b Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 7 Dec 2024 22:44:35 +0100 Subject: changed spawnrate of particles (bound to delta time) --- src/crepe/Particle.cpp | 9 +++++---- src/crepe/Particle.h | 7 ++++--- src/crepe/api/ParticleEmitter.h | 2 ++ src/crepe/system/ParticleSystem.cpp | 26 +++++++++----------------- src/crepe/system/ParticleSystem.h | 11 +---------- 5 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index ce000a1..b340826 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -15,16 +15,17 @@ void Particle::reset(unsigned int lifespan, const vec2 & position, const vec2 & this->force_over_time = {0, 0}; } -void Particle::update() { +void Particle::update(double dt) { // Deactivate particle if it has exceeded its lifespan - if (++time_in_life >= lifespan) { + time_in_life += dt; + if (time_in_life >= lifespan) { this->active = false; return; } // Update velocity based on accumulated force and update position - this->velocity += force_over_time; - this->position += velocity; + this->velocity += force_over_time * dt; + this->position += velocity * dt; } void Particle::stop_movement() { diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index 4fa93a1..49fec1f 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -24,11 +24,11 @@ public: //! Accumulated force affecting the particle over time. vec2 force_over_time; //! Total lifespan of the particle in milliseconds. - unsigned int lifespan; + float lifespan; //! Active state of the particle; true if it is in use, false otherwise. bool active = false; //! The time the particle has been alive, in milliseconds. - unsigned int time_in_life = 0; + float time_in_life = 0; //! The angle at which the particle is oriented or moving. float angle = 0; @@ -49,8 +49,9 @@ public: * * Advances the particle's position based on its velocity and applies accumulated forces. * Deactivates the particle if its lifespan has expired. + * \param dt The amount of fixed delta time that has passed. */ - void update(); + void update(double dt); /** * \brief Stops the particle's movement. * diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index e8fa15e..48c7625 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -52,6 +52,8 @@ public: const unsigned int max_particles = 256; //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) float emission_rate = 1; + //! Saves time left over from last update event. + float spawn_accumulator = 0; //! min speed of the particles float min_speed = 1; //! min speed of the particles diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 596b4b0..db4bcaf 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -12,8 +12,10 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters + ComponentManager & mgr = this->mediator.component_manager; RefVector emitters = mgr.get_components_by_type(); + double dt = LoopTimer::get_instance().get_fixed_delta_time(); for (ParticleEmitter & emitter : emitters) { // Get transform linked to emitter @@ -21,15 +23,17 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - int updates = this->calculate_update(this->update_count, emitter.data.emission_rate); - for (size_t i = 0; i < updates; i++) { - this->emit_particle(emitter, transform); - } + int spawn_amount = emitter.data.emission_rate * dt; + while (emitter.data.spawn_accumulator >= 1.0) { + emit_particle(emitter, transform); + emitter.data.spawn_accumulator -= 1.0; + } + // Update all particles for (Particle & particle : emitter.data.particles) { if (particle.active) { - particle.update(); + particle.update(dt); } } @@ -61,18 +65,6 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & } } -int ParticleSystem::calculate_update(int count, float emission) const { - float integer_part = std::floor(emission); - float fractional_part = emission - integer_part; - - if (fractional_part > 0) { - int denominator = static_cast(1.0 / fractional_part); - return (count % denominator == 0) ? 1 : 0; - } - - return static_cast(emission); -} - void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; float half_width = emitter.data.boundary.width / 2.0; diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 2adb0f0..454b65f 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -23,6 +23,7 @@ public: void update() override; private: + /** * \brief Emits a particle from the specified emitter based on its emission properties. * @@ -31,16 +32,6 @@ private: */ void emit_particle(ParticleEmitter & emitter, const Transform & transform); - /** - * \brief Calculates the number of times particles should be emitted based on emission rate - * and update count. - * - * \param count Current update count. - * \param emission Emission rate. - * \return The number of particles to emit. - */ - int calculate_update(int count, float emission) const; - /** * \brief Checks whether particles are within the emitter’s boundary, resets or stops * particles if they exit. -- cgit v1.2.3 From dd7d5cf6b01b8a6a4238b66c27861ee76522067e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 7 Dec 2024 22:48:05 +0100 Subject: added this --- src/crepe/system/ParticleSystem.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index db4bcaf..3befb03 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -23,13 +23,12 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - int spawn_amount = emitter.data.emission_rate * dt; + emitter.data.spawn_accumulator = emitter.data.emission_rate * dt; while (emitter.data.spawn_accumulator >= 1.0) { - emit_particle(emitter, transform); + this->emit_particle(emitter, transform); emitter.data.spawn_accumulator -= 1.0; } - // Update all particles for (Particle & particle : emitter.data.particles) { if (particle.active) { @@ -48,9 +47,9 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & constexpr float DEG_TO_RAD = M_PI / 180.0; vec2 initial_position = emitter.data.position + transform.position; - float random_angle = generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); + float random_angle = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); - float random_speed = generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); + float random_speed = this->generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); float angle_radians = random_angle * DEG_TO_RAD; vec2 velocity @@ -79,7 +78,6 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t const vec2 & position = particle.position; bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP && position.y <= BOTTOM); - if (!within_bounds) { if (emitter.data.boundary.reset_on_exit) { particle.active = false; -- cgit v1.2.3 From 29f2d4537cf4d4d4c74c16447a45a5d7034161f4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 09:47:46 +0100 Subject: sdlfontcontext --- src/CMakeLists.txt | 2 ++ src/crepe/facade/CMakeLists.txt | 2 ++ src/crepe/facade/SDLFontContext.cpp | 51 +++++++++++++++++++++++++++++++++++++ src/crepe/facade/SDLFontContext.h | 15 +++++++++++ 4 files changed, 70 insertions(+) create mode 100644 src/crepe/facade/SDLFontContext.cpp create mode 100644 src/crepe/facade/SDLFontContext.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 97b21f0..2b8873f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,7 @@ find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) find_package(whereami REQUIRED) find_library(BERKELEY_DB db) +find_package(Fontconfig REQUIRED) add_library(crepe SHARED) add_executable(test_main EXCLUDE_FROM_ALL) @@ -27,6 +28,7 @@ target_link_libraries(crepe PUBLIC SDL2_image PUBLIC ${BERKELEY_DB} PUBLIC whereami + PUBLIC ${Fontconfig_LIBRARIES} ) add_subdirectory(crepe) diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index 4cc53bc..9b73323 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(crepe PUBLIC SoundContext.cpp SDLContext.cpp DB.cpp + SDLFontContext.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -10,5 +11,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h SDLContext.h DB.h + SDLFontContext.h ) diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp new file mode 100644 index 0000000..6ce5efd --- /dev/null +++ b/src/crepe/facade/SDLFontContext.cpp @@ -0,0 +1,51 @@ +#include "SDLFontContext.h" +#include + +using namespace crepe; +using namespace std; + +SDLFontContext::SDLFontContext(){ + if (!FcInit()) { + throw std::runtime_error("Failed to initialize Fontconfig."); + } +} + +SDLFontContext::~SDLFontContext(){ + FcFini(); +} +Asset SDLFontContext::get_font_asset(const std::string & font_family) { + // Create a pattern to search for the font family + FcPattern* pattern = FcNameParse(reinterpret_cast(font_family.c_str())); + if (!pattern) { + throw std::runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig* config = FcConfigGetCurrent(); + if (!config) { + FcPatternDestroy(pattern); + throw std::runtime_error("Failed to get current Fontconfig configuration."); + } + + // Match the font pattern + FcResult result; + FcPattern* matched_pattern = FcFontMatch(config, pattern, &result); + FcPatternDestroy(pattern); + + if (!matched_pattern) { + throw std::runtime_error("No matching font found."); + } + + // Extract the file path + FcChar8* file_path = nullptr; + if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch || !file_path) { + FcPatternDestroy(matched_pattern); + throw std::runtime_error("Failed to get font file path."); + } + + // Convert the file path to a std::string + std::string font_file_path(reinterpret_cast(file_path)); + FcPatternDestroy(matched_pattern); + + return Asset(font_file_path); +} diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h new file mode 100644 index 0000000..3b2e162 --- /dev/null +++ b/src/crepe/facade/SDLFontContext.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include "../api/Asset.h" +namespace crepe { + class SDLFontContext{ + public: + SDLFontContext(); + ~SDLFontContext(); + Asset get_font_asset(const std::string & font_family); + private: + }; + +} -- cgit v1.2.3 From b2c72ee8ce282bb13b0fbeb2ddb01fdfd6ad1280 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 12:27:59 +0100 Subject: font progress --- src/crepe/api/Config.h | 11 +++++++++ src/crepe/facade/SDLFontContext.cpp | 19 ++++++++------- src/crepe/facade/SDLFontContext.h | 5 +++- src/crepe/facade/font.cpp | 21 +++++++++++++++++ src/crepe/facade/font.h | 47 +++++++++++++++++++++++++++++++++++++ src/example/CMakeLists.txt | 1 + src/example/loadfont.cpp | 13 ++++++++++ 7 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 src/crepe/facade/font.cpp create mode 100644 src/crepe/facade/font.h create mode 100644 src/example/loadfont.cpp diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index a9745c3..e4808fe 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -86,6 +86,17 @@ public: */ std::string root_pattern = ".crepe-root"; } asset; + //! Default font options + struct { + /** + * \brief Default font size + * + * using the SDL_ttf library the font size needs to be set when loading the font. + * This config option is the font size at which all fonts will be loaded initially. + * + */ + int font_size = 16; + } font; }; } // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 6ce5efd..a4be143 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -1,19 +1,22 @@ -#include "SDLFontContext.h" #include +#include "SDLFontContext.h" + + using namespace crepe; using namespace std; SDLFontContext::SDLFontContext(){ - if (!FcInit()) { - throw std::runtime_error("Failed to initialize Fontconfig."); - } + } SDLFontContext::~SDLFontContext(){ - FcFini(); + } -Asset SDLFontContext::get_font_asset(const std::string & font_family) { +unique_ptr SDLFontContext::get_font_asset(const std::string & font_family) { + if (!FcInit()) { + throw std::runtime_error("Failed to initialize Fontconfig."); + } // Create a pattern to search for the font family FcPattern* pattern = FcNameParse(reinterpret_cast(font_family.c_str())); if (!pattern) { @@ -46,6 +49,6 @@ Asset SDLFontContext::get_font_asset(const std::string & font_family) { // Convert the file path to a std::string std::string font_file_path(reinterpret_cast(file_path)); FcPatternDestroy(matched_pattern); - - return Asset(font_file_path); + FcFini(); + return std::move(make_unique(font_file_path)); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index 3b2e162..cd91383 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -1,14 +1,17 @@ #pragma once +#include #include #include + #include "../api/Asset.h" + namespace crepe { class SDLFontContext{ public: SDLFontContext(); ~SDLFontContext(); - Asset get_font_asset(const std::string & font_family); + std::unique_ptr get_font_asset(const std::string & font_family); private: }; diff --git a/src/crepe/facade/font.cpp b/src/crepe/facade/font.cpp new file mode 100644 index 0000000..259a6cd --- /dev/null +++ b/src/crepe/facade/font.cpp @@ -0,0 +1,21 @@ +#include "font.h" +#include "../api/Config.h" +using namespace std; +using namespace crepe; + +void Font::load(unique_ptr res){ + const char* font_path = res->get_path(); + int font_size = Config::get_instance().font.font_size; + this->font = std::unique_ptr( + TTF_OpenFont(font_path, font_size), &TTF_CloseFont); + + if (!font) { + throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); + } +} +Font::Font(const char* src){ + this->load(make_unique(src)); +} +Font::Font(std::unique_ptr res){ + this->load(std::move(res)); +} diff --git a/src/crepe/facade/font.h b/src/crepe/facade/font.h new file mode 100644 index 0000000..f5c7785 --- /dev/null +++ b/src/crepe/facade/font.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include + +#include "../api/Asset.h" + +namespace crepe { + +/** + * \brief Font resource facade + * + * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. + */ +class Font { +public: + /** + * \param src The file path to the font file. + */ + Font(const char* src); + + /** + * \param res A unique pointer to an Asset holding the font resource. + */ + Font(std::unique_ptr res); + + /** + * \brief Destructor to clean up font resources. + */ + ~Font() = default; + + void draw(const vec2& pos, const vec2&) +private: + /** + * \brief Load the font from the given resource. + * + * This method is used by both constructors to load the font resource. + * + * \param res A unique pointer to an Asset holding the font resource. + */ + void load(std::unique_ptr res); + +private: + std::unique_ptr font; ///< The SDL_ttf font object with custom deleter. +}; + +} // namespace crepe diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 8ef71bb..7fc81d7 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -21,3 +21,4 @@ add_example(savemgr) add_example(rendering_particle) add_example(game) add_example(button) +add_example(loadfont) diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp new file mode 100644 index 0000000..8931ce3 --- /dev/null +++ b/src/example/loadfont.cpp @@ -0,0 +1,13 @@ +#include +#include + +#include + +using namespace crepe; + +int main(){ + SDLFontContext font_facade; + std::unique_ptr asset = font_facade.get_font_asset("OpenSymbol"); + std::cout << "path: " << asset->get_path() << std::endl; + return 0; +} -- cgit v1.2.3 From 4bb6e89d93596c9c53798b1b9a01c588cabfe881 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 16:45:05 +0100 Subject: save --- src/crepe/api/Text.h | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 5c527af..9dd275b 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -1,12 +1,35 @@ #pragma once -#include "Asset.h" +#include + #include "../Component.h" -class Text : public Component{ + +#include "Asset.h" +#include "Color.h" +#include "UIObject.h" + +namespace crepe{ +class Text : public UIObject{ public: - Text(game_object_id_t id, const Asset & font, int font_size); + Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font, int font_size); + //! Label text. + std::string text; + //! Label text color + Color text_color = Color::BLACK; + /** + * \brief fontsize for text rendering + * + * \note this is not the actual font size that is loaded in. + * + * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size. + * The default font size that is loaded is set in the Config. + * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. + */ int font_size = 16; + const Asset source; private: }; + +} // namespace crepe -- cgit v1.2.3 From 007fe1ecb5e9f76539cdffd6a96afe22c8b2d214 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 18:21:03 +0100 Subject: save --- src/crepe/facade/Font.cpp | 24 ++++++++++++++++++++++++ src/crepe/facade/Font.h | 33 +++++++++++++++++++++++++++++++++ src/crepe/facade/font.cpp | 21 --------------------- src/crepe/facade/font.h | 31 ------------------------------- 4 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 src/crepe/facade/Font.cpp create mode 100644 src/crepe/facade/Font.h delete mode 100644 src/crepe/facade/font.cpp delete mode 100644 src/crepe/facade/font.h diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp new file mode 100644 index 0000000..2ad176a --- /dev/null +++ b/src/crepe/facade/Font.cpp @@ -0,0 +1,24 @@ +#include "font.h" +#include "../api/Config.h" +using namespace std; +using namespace crepe; + +void Font::load(unique_ptr res){ + const char* font_path = res->get_path(); + int font_size = Config::get_instance().font.font_size; + this->font = std::unique_ptr( + TTF_OpenFont(font_path, font_size), &TTF_CloseFont); + + if (!font) { + throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); + } +} +Font::Font(const char* src){ + this->load(make_unique(src)); +} +Font::Font(std::unique_ptr res){ + this->load(std::move(res)); +} +const TTF_Font& Font::get_font() const{ + return this->font; +} diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h new file mode 100644 index 0000000..e34ac63 --- /dev/null +++ b/src/crepe/facade/Font.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +#include "../api/Asset.h" +#include "../api/Config.h" + +namespace crepe { + +/** + * \brief Font resource facade + * + * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. + */ +class Font : public Resource{ +public: + /**. + * \param src Asset with texture data to load. + * \param mediator use the SDLContext reference to load the image + */ + Font(const Asset & src, Mediator & mediator); + + + ~Font() = default + const TTF_Font& get_font() const; +private: + //! The SDL_ttf font object with custom deleter. + std::unique_ptr font; + int default_font_size = Config::get_instance().font.font_size; +}; + +} // namespace crepe diff --git a/src/crepe/facade/font.cpp b/src/crepe/facade/font.cpp deleted file mode 100644 index 259a6cd..0000000 --- a/src/crepe/facade/font.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "font.h" -#include "../api/Config.h" -using namespace std; -using namespace crepe; - -void Font::load(unique_ptr res){ - const char* font_path = res->get_path(); - int font_size = Config::get_instance().font.font_size; - this->font = std::unique_ptr( - TTF_OpenFont(font_path, font_size), &TTF_CloseFont); - - if (!font) { - throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); - } -} -Font::Font(const char* src){ - this->load(make_unique(src)); -} -Font::Font(std::unique_ptr res){ - this->load(std::move(res)); -} diff --git a/src/crepe/facade/font.h b/src/crepe/facade/font.h deleted file mode 100644 index a8d8040..0000000 --- a/src/crepe/facade/font.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include - -#include "../api/Asset.h" - -namespace crepe { - -/** - * \brief Font resource facade - * - * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. - */ -class Font : public Resource{ -public: - /** - * \param res A unique pointer to an Asset holding the font resource. - */ - Font(const Asset & src, Mediator & mediator); - - /** - * \brief Destructor to clean up font resources. - */ - ~Font() = default; -private: - //! The SDL_ttf font object with custom deleter. - std::unique_ptr font; -}; - -} // namespace crepe -- cgit v1.2.3 From e75e355c3a59d53a1d64fd8fae3331b2234083e2 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 11 Dec 2024 22:17:43 +0100 Subject: text component pretty much finished --- src/crepe/api/CMakeLists.txt | 2 ++ src/crepe/api/Config.h | 4 ++-- src/crepe/api/Text.cpp | 6 ++++++ src/crepe/api/Text.h | 8 ++++---- src/crepe/facade/Font.cpp | 10 +++++++--- src/crepe/facade/Font.h | 12 ++++++------ src/crepe/facade/SDLFontContext.cpp | 11 +++++------ src/crepe/facade/SDLFontContext.h | 8 ++++++++ src/example/loadfont.cpp | 23 +++++++++++++++++------ 9 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index fb11c8d..90e4d1f 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -20,6 +20,7 @@ target_sources(crepe PUBLIC Button.cpp UIObject.cpp AI.cpp + Text.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -49,4 +50,5 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Button.h UIObject.h AI.h + Text.h ) diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index def4c49..159be99 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -81,11 +81,11 @@ struct Config final { /** * \brief Default font size * - * using the SDL_ttf library the font size needs to be set when loading the font. + * Using the SDL_ttf library the font size needs to be set when loading the font. * This config option is the font size at which all fonts will be loaded initially. * */ - int font_size = 16; + unsigned int size = 16; } font; //! Audio system settings diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index e69de29..dd44dd9 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -0,0 +1,6 @@ +#include "Text.h" + +using namespace crepe; + +Text::Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font) : UIObject(id,dimensions,offset),source(font){} + diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 9dd275b..d5f47fa 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -11,11 +11,11 @@ namespace crepe{ class Text : public UIObject{ public: - Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font, int font_size); + Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font); //! Label text. std::string text; - //! Label text color + //! Label text color. Color text_color = Color::BLACK; /** * \brief fontsize for text rendering @@ -26,8 +26,8 @@ public: * The default font size that is loaded is set in the Config. * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. */ - int font_size = 16; - + unsigned int font_size = 16; + //! Font asset const Asset source; private: }; diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 2ad176a..b600b01 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,24 +1,28 @@ -#include "font.h" #include "../api/Config.h" + +#include "font.h" + using namespace std; using namespace crepe; void Font::load(unique_ptr res){ const char* font_path = res->get_path(); - int font_size = Config::get_instance().font.font_size; this->font = std::unique_ptr( - TTF_OpenFont(font_path, font_size), &TTF_CloseFont); + TTF_OpenFont(font_path, this->default_font_size), &TTF_CloseFont); if (!font) { throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); } } + Font::Font(const char* src){ this->load(make_unique(src)); } + Font::Font(std::unique_ptr res){ this->load(std::move(res)); } + const TTF_Font& Font::get_font() const{ return this->font; } diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index e34ac63..8bf9fc9 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -9,17 +9,17 @@ namespace crepe { /** - * \brief Font resource facade + * \brief Resource for managing font creation and destruction * * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. */ class Font : public Resource{ public: - /**. - * \param src Asset with texture data to load. - * \param mediator use the SDLContext reference to load the image + /** + * \param src Asset with font data to load. + * \param mediator use the SDLContext reference to load text */ - Font(const Asset & src, Mediator & mediator); + Font( src, Mediator & mediator); ~Font() = default @@ -27,7 +27,7 @@ public: private: //! The SDL_ttf font object with custom deleter. std::unique_ptr font; - int default_font_size = Config::get_instance().font.font_size; + unsigned int default_font_size = Config::get_instance().font.size; }; } // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index a4be143..d7a0bff 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -7,16 +7,16 @@ using namespace crepe; using namespace std; SDLFontContext::SDLFontContext(){ - + if (!FcInit()) { + throw std::runtime_error("Failed to initialize Fontconfig."); + } } SDLFontContext::~SDLFontContext(){ - + FcFini(); } unique_ptr SDLFontContext::get_font_asset(const std::string & font_family) { - if (!FcInit()) { - throw std::runtime_error("Failed to initialize Fontconfig."); - } + // Create a pattern to search for the font family FcPattern* pattern = FcNameParse(reinterpret_cast(font_family.c_str())); if (!pattern) { @@ -49,6 +49,5 @@ unique_ptr SDLFontContext::get_font_asset(const std::string & font_family // Convert the file path to a std::string std::string font_file_path(reinterpret_cast(file_path)); FcPatternDestroy(matched_pattern); - FcFini(); return std::move(make_unique(font_file_path)); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index cd91383..c890b2d 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -11,6 +11,14 @@ namespace crepe { public: SDLFontContext(); ~SDLFontContext(); + /** + * + * \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. + * + * \param font_family Name of the font family name. + */ std::unique_ptr get_font_asset(const std::string & font_family); private: }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 8931ce3..0002026 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,13 +1,24 @@ #include +#include #include - #include +#include using namespace crepe; -int main(){ - SDLFontContext font_facade; - std::unique_ptr asset = font_facade.get_font_asset("OpenSymbol"); - std::cout << "path: " << asset->get_path() << std::endl; - return 0; +int main() { + SDLFontContext font_facade; + + // Create a unique pointer to the font asset + std::unique_ptr asset = font_facade.get_font_asset("OpenSymbol"); + std::cout << "path: " << asset->get_path() << std::endl; + try{ + // Correct way to create a unique pointer for Text + std::unique_ptr label = std::make_unique(1, vec2(100, 100), vec2(0, 0), *asset); + }catch (const std::exception& e) { + std::cout << "Standard exception thrown: " << e.what() << std::endl; + } + + + return 0; } -- cgit v1.2.3 From afd81007153f05c8f8b42bcf08a8cdf8ce13a98c Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 15:32:07 +0100 Subject: updated particle emitter --- src/crepe/api/LoopManager.cpp | 1 + src/crepe/api/ParticleEmitter.h | 7 +++++-- src/crepe/system/ParticleSystem.cpp | 16 ++++++++++------ src/crepe/system/RenderSystem.cpp | 2 +- src/example/game.cpp | 21 +++++++++++++++++++++ src/example/rendering_particle.cpp | 23 +---------------------- 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index b5e5ff7..7a78019 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -65,6 +65,7 @@ void LoopManager::fixed_update() { this->get_system().update(); this->event_manager.dispatch_events(); this->get_system().update(); + this->get_system().update(); this->get_system().update(); this->get_system().update(); this->get_system().update(); diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 48c7625..cdea69a 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -5,6 +5,7 @@ #include "Component.h" #include "Particle.h" +#include "system/ParticleSystem.h" #include "types.h" namespace crepe { @@ -52,8 +53,6 @@ public: const unsigned int max_particles = 256; //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) float emission_rate = 1; - //! Saves time left over from last update event. - float spawn_accumulator = 0; //! min speed of the particles float min_speed = 1; //! min speed of the particles @@ -85,6 +84,10 @@ public: public: //! Configuration data for particle emission settings. Data data; +private: + //! Saves time left over from last update event. + friend ParticleSystem; + float spawn_accumulator = 0; }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 3befb03..941e502 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,7 @@ #include "../api/ParticleEmitter.h" #include "../api/Transform.h" #include "../manager/ComponentManager.h" +#include "../manager/LoopTimerManager.h" #include "ParticleSystem.h" @@ -12,10 +14,12 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters - - ComponentManager & mgr = this->mediator.component_manager; + const Mediator & mediator = this->mediator; + LoopTimerManager & loop_timer = mediator.loop_timer; + ComponentManager & mgr = mediator.component_manager; + float dt = std::chrono::duration(loop_timer.get_scaled_fixed_delta_time()).count(); + RefVector emitters = mgr.get_components_by_type(); - double dt = LoopTimer::get_instance().get_fixed_delta_time(); for (ParticleEmitter & emitter : emitters) { // Get transform linked to emitter @@ -23,10 +27,10 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - emitter.data.spawn_accumulator = emitter.data.emission_rate * dt; - while (emitter.data.spawn_accumulator >= 1.0) { + emitter.spawn_accumulator = emitter.data.emission_rate * dt; + while (emitter.spawn_accumulator >= 1.0) { this->emit_particle(emitter, transform); - emitter.data.spawn_accumulator -= 1.0; + emitter.spawn_accumulator -= 1.0; } // Update all particles diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index afd9548..cc633e8 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -83,7 +83,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { - if (&em.data.sprite != &sprite) continue; + if (&em.sprite != &sprite) continue; rendering_particles = true; if (!em.active) continue; diff --git a/src/example/game.cpp b/src/example/game.cpp index 5361f3a..279648e 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,4 +1,5 @@ #include "api/CircleCollider.h" +#include "api/ParticleEmitter.h" #include "api/Scene.h" #include "manager/ComponentManager.h" #include "manager/Mediator.h" @@ -258,6 +259,26 @@ public: }) .active = false; + Asset img5{"asset/texture/square.png"}; + + + + GameObject particle = mgr.new_object( + "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); + auto & particle_image = particle.add_component(img5, Sprite::Data{.size = {5, 5},}); + auto & test = particle.add_component(particle_image,ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 256, + .emission_rate = 50, + .min_speed = 10, + .max_speed = 20, + .min_angle = -20, + .max_angle = 20, + .begin_lifespan = 0, + .end_lifespan = 5, + } + ); + } string get_name() const { return "scene1"; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 13e625f..2b5c041 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -17,28 +17,7 @@ using namespace crepe; using namespace std; - -/* - auto & test = game_object.add_component(ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 10, - .emission_rate = 0.1, - .min_speed = 6, - .max_speed = 20, - .min_angle = -20, - .max_angle = 20, - .begin_lifespan = 0, - .end_lifespan = 60, - .force_over_time = vec2{0, 0}, - .boundary{ - .width = 1000, - .height = 1000, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); - */ + class TestScene : public Scene { public: -- cgit v1.2.3 From 5b1c45c2c3bd695ee8d8b5baa4b1988e24596559 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 15:42:03 +0100 Subject: restore file --- src/crepe/system/CollisionSystem.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 0483693..952ed0a 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -290,6 +290,7 @@ vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_co // Compute penetration depth float penetration_depth = circle_collider.radius - distance; + // Compute the resolution vector vec2 resolution = collision_normal * penetration_depth; -- cgit v1.2.3 From d4b4a54f919872ef1295890cd36ac46d89a4426d Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 15:51:29 +0100 Subject: removed old code --- src/crepe/system/ParticleSystem.cpp | 2 -- src/crepe/system/ParticleSystem.h | 8 -------- 2 files changed, 10 deletions(-) diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 941e502..204afaf 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -43,8 +43,6 @@ void ParticleSystem::update() { // Check if within boundary this->check_bounds(emitter, transform); } - - this->update_count = (this->update_count + 1) % this->MAX_UPDATE_COUNT; } void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & transform) { diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 454b65f..95a2bd5 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -58,14 +58,6 @@ private: * \return Random speed. */ float generate_random_speed(float min_speed, float max_speed) const; - -private: - //! Counter to count updates to determine how many times emit_particle is - // called. - unsigned int update_count = 0; - //! Determines the lowest amount of emission rate (1000 = 0.001 = 1 particle per 1000 - // updates). - static constexpr unsigned int MAX_UPDATE_COUNT = 100; }; } // namespace crepe -- cgit v1.2.3 From 9e1c0d08a8e1813799cf5002562d3f503524d718 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:14:08 +0100 Subject: updated comment --- src/crepe/api/ParticleEmitter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index cdea69a..1251a30 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -51,7 +51,7 @@ public: vec2 position; //! maximum number of particles const unsigned int max_particles = 256; - //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) + //! rate of particle emission per second float emission_rate = 1; //! min speed of the particles float min_speed = 1; -- cgit v1.2.3 From 1e42d6669b462a3d1490788d3b20141d15c881db Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:14:47 +0100 Subject: update comment --- src/crepe/api/ParticleEmitter.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 1251a30..cc54ffb 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -61,9 +61,9 @@ public: float min_angle = 0; //! max angle of particle emission float max_angle = 0; - //! begin Lifespan of particle (only visual) + //! begin Lifespan of particle in seconds (only visual) float begin_lifespan = 0.0; - //! end Lifespan of particle + //! end Lifespan of particle in seconds float end_lifespan = 10.0; //! force over time (physics) vec2 force_over_time; -- cgit v1.2.3 From bef438ff7790acbe2dc376097e1782e60356bf45 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:25:11 +0100 Subject: changed get dt --- src/crepe/system/ParticleSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 204afaf..a58edf0 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -17,7 +17,7 @@ void ParticleSystem::update() { const Mediator & mediator = this->mediator; LoopTimerManager & loop_timer = mediator.loop_timer; ComponentManager & mgr = mediator.component_manager; - float dt = std::chrono::duration(loop_timer.get_scaled_fixed_delta_time()).count(); + float dt = loop_timer.get_scaled_fixed_delta_time(); RefVector emitters = mgr.get_components_by_type(); -- cgit v1.2.3 From feb27ebdc4086dec62e45e3a6c83bdd021568dd5 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:47:18 +0100 Subject: reverted dt --- src/crepe/system/ParticleSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index a58edf0..85b8248 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -17,7 +17,7 @@ void ParticleSystem::update() { const Mediator & mediator = this->mediator; LoopTimerManager & loop_timer = mediator.loop_timer; ComponentManager & mgr = mediator.component_manager; - float dt = loop_timer.get_scaled_fixed_delta_time(); + float dt = loop_timer.get_scaled_fixed_delta_time().count(); RefVector emitters = mgr.get_components_by_type(); -- cgit v1.2.3 From 927aab8cf424d1f3bf25056ce4b4126ce9f4edab Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:49:50 +0100 Subject: reverted --- src/crepe/system/CollisionSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 952ed0a..44a0431 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -290,7 +290,7 @@ vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_co // Compute penetration depth float penetration_depth = circle_collider.radius - distance; - + // Compute the resolution vector vec2 resolution = collision_normal * penetration_depth; -- cgit v1.2.3 From 431d7cceb25db86f31a8c89440f3cdd2c7bf061f Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 20:45:49 +0100 Subject: load font almost working --- src/crepe/api/Text.cpp | 2 +- src/crepe/api/Text.h | 49 ++++++---- src/crepe/facade/Font.cpp | 4 +- src/crepe/facade/Font.h | 15 +-- src/crepe/facade/SDLContext.cpp | 202 ++++++++++++++++++++++++++-------------- src/example/loadfont.cpp | 13 +-- 6 files changed, 181 insertions(+), 104 deletions(-) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index dd44dd9..12d01f1 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -2,5 +2,5 @@ using namespace crepe; -Text::Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font) : UIObject(id,dimensions,offset),source(font){} +Text::Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset,std::string text,std::string font_family) : UIObject(id,dimensions,offset),text(text), font_family(font_family){} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index d5f47fa..51bab98 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -9,27 +9,42 @@ #include "UIObject.h" namespace crepe{ + +/** + * \brief Text UIObject component for displaying text + * + * This class can be used to display text on screen. By setting the font_family to a font already stored on the current device it will automatically be loaded in. + */ class Text : public UIObject{ public: - Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font); + Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset,std::string text,std::string font_family); + //! Text data that does not have to be set in the constructor + struct Data { + //! Label text color. + Color text_color = Color::BLACK; + /** + * \brief fontsize for text rendering + * + * \note this is not the actual font size that is loaded in. + * + * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size. + * The default font size that is loaded is set in the Config. + * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. + */ + unsigned int font_size = 16; + //! Layer sorting level of the text + const int sorting_in_layer = 0; + //! Order within the sorting text + const int order_in_layer = 0; + }; +public: + //! font family name such as (Arial,Helvetica,Inter) + std::string font_family = ""; //! Label text. - std::string text; - //! Label text color. - Color text_color = Color::BLACK; - /** - * \brief fontsize for text rendering - * - * \note this is not the actual font size that is loaded in. - * - * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size. - * The default font size that is loaded is set in the Config. - * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. - */ - unsigned int font_size = 16; - //! Font asset - const Asset source; -private: + std::string text = ""; + // Data instance for data not gotten from constructor + Data data; }; } // namespace crepe diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index b600b01..66835ee 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -23,6 +23,6 @@ Font::Font(std::unique_ptr res){ this->load(std::move(res)); } -const TTF_Font& Font::get_font() const{ - return this->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 index 8bf9fc9..dd1cebf 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -17,17 +17,20 @@ class Font : public Resource{ public: /** * \param src Asset with font data to load. - * \param mediator use the SDLContext reference to load text + * \param mediator use the SDLContext reference to get_font() */ - Font( src, Mediator & mediator); + Font(const Asset & src, Mediator & mediator); - - ~Font() = default - const TTF_Font& get_font() const; + /** + * \brief getter for TTF_Font + * + * \param src Asset with font data to load. + * \param mediator use the SDLContext reference to get_font() + */ + TTF_Font* get_font() const; private: //! The SDL_ttf font object with custom deleter. std::unique_ptr font; - unsigned int default_font_size = Config::get_instance().font.size; }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index d71a9c8..8f6c02c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -18,18 +18,23 @@ #include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" +#include "../api/Texture.h" #include "../util/Log.h" -#include "manager/Mediator.h" #include "SDLContext.h" -#include "Texture.h" #include "types.h" using namespace crepe; using namespace std; -SDLContext::SDLContext(Mediator & mediator) { +SDLContext & SDLContext::get_instance() { + static SDLContext instance; + return instance; +} + +SDLContext::SDLContext() { dbg_trace(); + if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } @@ -57,8 +62,6 @@ SDLContext::SDLContext(Mediator & mediator) { if (!(IMG_Init(img_flags) & img_flags)) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } - - mediator.sdl_context = *this; } SDLContext::~SDLContext() { @@ -74,11 +77,12 @@ SDLContext::~SDLContext() { SDL_Quit(); } -Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { +Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { static const std::array LOOKUP_TABLE = [] { std::array table{}; table.fill(Keycode::NONE); + // Map all SDL scancodes to Keycodes table[SDL_SCANCODE_SPACE] = Keycode::SPACE; table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE; table[SDL_SCANCODE_COMMA] = Keycode::COMMA; @@ -180,13 +184,27 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { return table; }(); - if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) { return Keycode::NONE; } - return LOOKUP_TABLE[sdl_key]; } +std::array SDLContext::get_keyboard_state() { + // Array to hold the key states (true if pressed, false if not) + std::array keyState{}; + SDL_PumpEvents(); + const Uint8 * current_state = SDL_GetKeyboardState(nullptr); + + for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { + Keycode key = sdl_to_keycode(static_cast(i)); + + if (key != Keycode::NONE) { + keyState[key] = current_state[i] != 0; + } + } + + return keyState; +} MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { static const std::array MOUSE_BUTTON_LOOKUP_TABLE = [] { @@ -218,19 +236,25 @@ void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } +SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { + return SDL_Rect{ + .x = sprite.mask.x, + .y = sprite.mask.y, + .w = sprite.mask.w, + .h = sprite.mask.h, + }; +} + SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; - float aspect_ratio - = (ctx.sprite.aspect_ratio == 0) ? ctx.texture.get_ratio() : ctx.sprite.aspect_ratio; - vec2 size = data.size; if (data.size.x == 0 && data.size.y != 0) { - size.x = data.size.y * aspect_ratio; + size.x = data.size.y * ctx.sprite.aspect_ratio; } if (data.size.y == 0 && data.size.x != 0) { - size.y = data.size.x / aspect_ratio; + size.y = data.size.x / ctx.sprite.aspect_ratio; } const CameraValues & cam = ctx.cam; @@ -251,24 +275,15 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } void SDLContext::draw(const RenderContext & ctx) { + const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) | (SDL_FLIP_VERTICAL * data.flip.flip_y)); - SDL_Rect srcrect; - SDL_Rect * srcrect_ptr = NULL; - if (ctx.sprite.mask.w != 0 || ctx.sprite.mask.h != 0) { - srcrect.w = ctx.sprite.mask.w; - srcrect.h = ctx.sprite.mask.h; - srcrect.x = ctx.sprite.mask.x; - srcrect.y = ctx.sprite.mask.y; - srcrect_ptr = &srcrect; - } - + SDL_Rect srcrect = this->get_src_rect(ctx.sprite); SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, - .texture = ctx.texture, .cam = ctx.cam, .pos = ctx.pos, .img_scale = ctx.scale, @@ -276,9 +291,9 @@ void SDLContext::draw(const RenderContext & ctx) { double angle = ctx.angle + data.angle_offset; - this->set_color_texture(ctx.texture, ctx.sprite.data.color); - SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect_ptr, &dstrect, - angle, NULL, render_flip); + this->set_color_texture(ctx.sprite.texture, ctx.sprite.data.color); + SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.texture.texture.get(), &srcrect, + &dstrect, angle, NULL, render_flip); } SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { @@ -340,6 +355,8 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { return ret_cam; } +uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } + std::unique_ptr> SDLContext::texture_from_path(const std::string & path) { @@ -366,71 +383,112 @@ SDLContext::texture_from_path(const std::string & path) { ivec2 SDLContext::get_size(const Texture & ctx) { ivec2 size; - SDL_QueryTexture(ctx.get_img(), NULL, NULL, &size.x, &size.y); + SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); return size; } +void SDLContext::delay(int ms) const { SDL_Delay(ms); } + std::vector SDLContext::get_events() { std::vector event_list; SDL_Event event; + + // Handle general SDL events while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::SHUTDOWN, - }); + event_list.push_back({SDLContext::EventType::SHUTDOWN, {}, {}, {}}); break; case SDL_KEYDOWN: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::KEYDOWN, - .key = sdl_to_keycode(event.key.keysym.scancode), - .key_repeat = (event.key.repeat != 0), - }); + event_list.push_back( + {SDLContext::EventType::KEYDOWN, + {sdl_to_keycode(event.key.keysym.scancode), event.key.repeat != 0}, + {}, + {}}); break; case SDL_KEYUP: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::KEYUP, - .key = sdl_to_keycode(event.key.keysym.scancode), - }); + event_list.push_back({SDLContext::EventType::KEYUP, + {sdl_to_keycode(event.key.keysym.scancode), false}, + {}, + {}}); break; case SDL_MOUSEBUTTONDOWN: - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::MOUSEDOWN, - .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {event.button.x, event.button.y}, - }); + event_list.push_back({SDLContext::EventType::MOUSEDOWN, + {}, + {sdl_to_mousebutton(event.button.button), + {event.button.x, event.button.y}}, + {}}); + break; + case SDL_MOUSEBUTTONUP: + event_list.push_back({SDLContext::EventType::MOUSEUP, + {}, + {sdl_to_mousebutton(event.button.button), + {event.button.x, event.button.y}}, + {}}); + break; + case SDL_MOUSEMOTION: + event_list.push_back({SDLContext::EventType::MOUSEMOVE, + {}, + {{}, + {event.motion.x, event.motion.y}, + -1, + INFINITY, + {event.motion.xrel, event.motion.yrel}}, + {}}); break; - case SDL_MOUSEBUTTONUP: { - int x, y; - SDL_GetMouseState(&x, &y); - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::MOUSEUP, - .mouse_button = sdl_to_mousebutton(event.button.button), - .mouse_position = {event.button.x, event.button.y}, - }); - } break; - - case SDL_MOUSEMOTION: { + case SDL_MOUSEWHEEL: event_list.push_back( - EventData{.event_type = SDLContext::EventType::MOUSEMOVE, - .mouse_position = {event.motion.x, event.motion.y}, - .rel_mouse_move = {event.motion.xrel, event.motion.yrel}}); - } break; - - case SDL_MOUSEWHEEL: { - event_list.push_back(EventData{ - .event_type = SDLContext::EventType::MOUSEWHEEL, - .mouse_position = {event.motion.x, event.motion.y}, - // TODO: why is this needed? - .scroll_direction = event.wheel.y < 0 ? -1 : 1, - .scroll_delta = event.wheel.preciseY, - }); - } break; + {SDLContext::EventType::MOUSEWHEEL, + {}, + {{}, {}, event.wheel.y < 0 ? -1 : 1, event.wheel.preciseY, {}}, + {}}); + break; + + // Forward window events for further processing + case SDL_WINDOWEVENT: + this->handle_window_event(event.window, event_list); + break; } } + return event_list; } + +// Separate function for SDL_WINDOWEVENT subtypes +void SDLContext::handle_window_event(const SDL_WindowEvent & window_event, + std::vector & event_list) { + switch (window_event.event) { + case SDL_WINDOWEVENT_EXPOSED: + event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_RESIZED: + event_list.push_back({SDLContext::EventType::WINDOW_RESIZE, + {}, + {}, + {{}, {window_event.data1, window_event.data2}}}); + break; + case SDL_WINDOWEVENT_MOVED: + event_list.push_back({SDLContext::EventType::WINDOW_MOVE, + {}, + {}, + {{window_event.data1, window_event.data2}, {}}}); + break; + case SDL_WINDOWEVENT_MINIMIZED: + event_list.push_back({SDLContext::EventType::WINDOW_MINIMIZE, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_MAXIMIZED: + event_list.push_back({SDLContext::EventType::WINDOW_MAXIMIZE, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_GAIN, {}, {}, {}}); + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_LOST, {}, {}, {}}); + break; + } +} + 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); + SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b); + SDL_SetTextureAlphaMod(texture.texture.get(), color.a); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 0002026..fe5466f 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -3,18 +3,19 @@ #include #include #include - +#include +#include using namespace crepe; int main() { SDLFontContext font_facade; - - // Create a unique pointer to the font asset - std::unique_ptr asset = font_facade.get_font_asset("OpenSymbol"); - std::cout << "path: " << asset->get_path() << std::endl; + Mediator mediator; try{ // Correct way to create a unique pointer for Text - std::unique_ptr label = std::make_unique(1, vec2(100, 100), vec2(0, 0), *asset); + std::unique_ptr label = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test","OpenSymbol"); + std::unique_ptr asset = font_facade.get_font_asset(label->font_family); + std::cout << "path: " << asset->get_path() << std::endl; + std::unique_ptr font = make_unique(asset,mediator) }catch (const std::exception& e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From 3ed4e51e23b5093a44a166b2f11ff66164e5cff1 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:09:55 +0100 Subject: font working --- src/CMakeLists.txt | 2 + src/crepe/facade/CMakeLists.txt | 2 + src/crepe/facade/Font.cpp | 31 +++--- src/crepe/facade/Font.h | 2 +- src/crepe/facade/SDLContext.cpp | 206 +++++++++++++++------------------------- src/crepe/facade/SDLContext.h | 1 + src/example/loadfont.cpp | 6 +- 7 files changed, 100 insertions(+), 150 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2b8873f..a525c74 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ project(crepe C CXX) find_package(SDL2 REQUIRED) find_package(SDL2_image REQUIRED) +find_package(SDL2_ttf REQUIRED) find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) find_package(whereami REQUIRED) @@ -25,6 +26,7 @@ target_include_directories(crepe target_link_libraries(crepe PRIVATE soloud PUBLIC SDL2 + PUBLIC SDL2_ttf PUBLIC SDL2_image PUBLIC ${BERKELEY_DB} PUBLIC whereami diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index f4d71ff..e61b680 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources(crepe PUBLIC SDLContext.cpp DB.cpp SDLFontContext.cpp + Font.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -14,5 +15,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SDLContext.h DB.h SDLFontContext.h + Font.h ) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 66835ee..37dee3a 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,28 +1,25 @@ #include "../api/Config.h" -#include "font.h" +#include "Font.h" using namespace std; using namespace crepe; -void Font::load(unique_ptr res){ - const char* font_path = res->get_path(); - this->font = std::unique_ptr( - TTF_OpenFont(font_path, this->default_font_size), &TTF_CloseFont); +Font::Font(const Asset& src, Mediator& mediator) + : Resource(src, mediator), font(nullptr, TTF_CloseFont) { + // Get the font file path from the Asset + const std::string font_path = src.get_path(); - if (!font) { - throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); - } -} + // Attempt to load the font + this->font.reset(TTF_OpenFont(font_path.c_str(), Config::get_instance().font.size)); -Font::Font(const char* src){ - this->load(make_unique(src)); -} - -Font::Font(std::unique_ptr res){ - this->load(std::move(res)); + // Check if font loading failed + if (!this->font) { + throw runtime_error(format("Failed to load font from path: {}" + ". SDL_ttf error: {}",font_path, TTF_GetError())); + } } -TTF_Font* Font::get_font() const{ - return this->font.get(); +TTF_Font* Font::get_font() const { + return this->font.get(); } diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index dd1cebf..a27676b 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -5,7 +5,7 @@ #include "../api/Asset.h" #include "../api/Config.h" - +#include "../Resource.h" namespace crepe { /** diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 8f6c02c..38d12f6 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -18,23 +18,21 @@ #include "../api/Color.h" #include "../api/Config.h" #include "../api/Sprite.h" -#include "../api/Texture.h" #include "../util/Log.h" +#include "manager/Mediator.h" #include "SDLContext.h" +#include "Texture.h" #include "types.h" using namespace crepe; using namespace std; -SDLContext & SDLContext::get_instance() { - static SDLContext instance; - return instance; -} - -SDLContext::SDLContext() { +SDLContext::SDLContext(Mediator & mediator) { dbg_trace(); - + if (TTF_Init() == -1) { + throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); + } if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } @@ -62,6 +60,8 @@ SDLContext::SDLContext() { if (!(IMG_Init(img_flags) & img_flags)) { throw runtime_error("SDLContext: SDL_image could not initialize!"); } + + mediator.sdl_context = *this; } SDLContext::~SDLContext() { @@ -75,14 +75,14 @@ SDLContext::~SDLContext() { // before. IMG_Quit(); SDL_Quit(); + TTF_Quit(); } -Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { +Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { static const std::array LOOKUP_TABLE = [] { std::array table{}; table.fill(Keycode::NONE); - // Map all SDL scancodes to Keycodes table[SDL_SCANCODE_SPACE] = Keycode::SPACE; table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE; table[SDL_SCANCODE_COMMA] = Keycode::COMMA; @@ -184,26 +184,12 @@ Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { return table; }(); + if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) { return Keycode::NONE; } - return LOOKUP_TABLE[sdl_key]; -} -std::array SDLContext::get_keyboard_state() { - // Array to hold the key states (true if pressed, false if not) - std::array keyState{}; - SDL_PumpEvents(); - const Uint8 * current_state = SDL_GetKeyboardState(nullptr); - for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { - Keycode key = sdl_to_keycode(static_cast(i)); - - if (key != Keycode::NONE) { - keyState[key] = current_state[i] != 0; - } - } - - return keyState; + return LOOKUP_TABLE[sdl_key]; } MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { @@ -236,25 +222,19 @@ void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } -SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { - return SDL_Rect{ - .x = sprite.mask.x, - .y = sprite.mask.y, - .w = sprite.mask.w, - .h = sprite.mask.h, - }; -} - SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; + float aspect_ratio + = (ctx.sprite.aspect_ratio == 0) ? ctx.texture.get_ratio() : ctx.sprite.aspect_ratio; + vec2 size = data.size; if (data.size.x == 0 && data.size.y != 0) { - size.x = data.size.y * ctx.sprite.aspect_ratio; + size.x = data.size.y * aspect_ratio; } if (data.size.y == 0 && data.size.x != 0) { - size.y = data.size.x / ctx.sprite.aspect_ratio; + size.y = data.size.x / aspect_ratio; } const CameraValues & cam = ctx.cam; @@ -275,15 +255,24 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } void SDLContext::draw(const RenderContext & ctx) { - const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) | (SDL_FLIP_VERTICAL * data.flip.flip_y)); - SDL_Rect srcrect = this->get_src_rect(ctx.sprite); + SDL_Rect srcrect; + SDL_Rect * srcrect_ptr = NULL; + if (ctx.sprite.mask.w != 0 || ctx.sprite.mask.h != 0) { + srcrect.w = ctx.sprite.mask.w; + srcrect.h = ctx.sprite.mask.h; + srcrect.x = ctx.sprite.mask.x; + srcrect.y = ctx.sprite.mask.y; + srcrect_ptr = &srcrect; + } + SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, + .texture = ctx.texture, .cam = ctx.cam, .pos = ctx.pos, .img_scale = ctx.scale, @@ -291,9 +280,9 @@ void SDLContext::draw(const RenderContext & ctx) { double angle = ctx.angle + data.angle_offset; - this->set_color_texture(ctx.sprite.texture, ctx.sprite.data.color); - SDL_RenderCopyExF(this->game_renderer.get(), ctx.sprite.texture.texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); + this->set_color_texture(ctx.texture, ctx.sprite.data.color); + SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect_ptr, &dstrect, + angle, NULL, render_flip); } SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { @@ -355,8 +344,6 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { return ret_cam; } -uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } - std::unique_ptr> SDLContext::texture_from_path(const std::string & path) { @@ -383,112 +370,71 @@ SDLContext::texture_from_path(const std::string & path) { ivec2 SDLContext::get_size(const Texture & ctx) { ivec2 size; - SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); + SDL_QueryTexture(ctx.get_img(), NULL, NULL, &size.x, &size.y); return size; } -void SDLContext::delay(int ms) const { SDL_Delay(ms); } - std::vector SDLContext::get_events() { std::vector event_list; SDL_Event event; - - // Handle general SDL events while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: - event_list.push_back({SDLContext::EventType::SHUTDOWN, {}, {}, {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::SHUTDOWN, + }); break; case SDL_KEYDOWN: - event_list.push_back( - {SDLContext::EventType::KEYDOWN, - {sdl_to_keycode(event.key.keysym.scancode), event.key.repeat != 0}, - {}, - {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::KEYDOWN, + .key = sdl_to_keycode(event.key.keysym.scancode), + .key_repeat = (event.key.repeat != 0), + }); break; case SDL_KEYUP: - event_list.push_back({SDLContext::EventType::KEYUP, - {sdl_to_keycode(event.key.keysym.scancode), false}, - {}, - {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::KEYUP, + .key = sdl_to_keycode(event.key.keysym.scancode), + }); break; case SDL_MOUSEBUTTONDOWN: - event_list.push_back({SDLContext::EventType::MOUSEDOWN, - {}, - {sdl_to_mousebutton(event.button.button), - {event.button.x, event.button.y}}, - {}}); - break; - case SDL_MOUSEBUTTONUP: - event_list.push_back({SDLContext::EventType::MOUSEUP, - {}, - {sdl_to_mousebutton(event.button.button), - {event.button.x, event.button.y}}, - {}}); - break; - case SDL_MOUSEMOTION: - event_list.push_back({SDLContext::EventType::MOUSEMOVE, - {}, - {{}, - {event.motion.x, event.motion.y}, - -1, - INFINITY, - {event.motion.xrel, event.motion.yrel}}, - {}}); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::MOUSEDOWN, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {event.button.x, event.button.y}, + }); break; - case SDL_MOUSEWHEEL: + case SDL_MOUSEBUTTONUP: { + int x, y; + SDL_GetMouseState(&x, &y); + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::MOUSEUP, + .mouse_button = sdl_to_mousebutton(event.button.button), + .mouse_position = {event.button.x, event.button.y}, + }); + } break; + + case SDL_MOUSEMOTION: { event_list.push_back( - {SDLContext::EventType::MOUSEWHEEL, - {}, - {{}, {}, event.wheel.y < 0 ? -1 : 1, event.wheel.preciseY, {}}, - {}}); - break; - - // Forward window events for further processing - case SDL_WINDOWEVENT: - this->handle_window_event(event.window, event_list); - break; + EventData{.event_type = SDLContext::EventType::MOUSEMOVE, + .mouse_position = {event.motion.x, event.motion.y}, + .rel_mouse_move = {event.motion.xrel, event.motion.yrel}}); + } break; + + case SDL_MOUSEWHEEL: { + event_list.push_back(EventData{ + .event_type = SDLContext::EventType::MOUSEWHEEL, + .mouse_position = {event.motion.x, event.motion.y}, + // TODO: why is this needed? + .scroll_direction = event.wheel.y < 0 ? -1 : 1, + .scroll_delta = event.wheel.preciseY, + }); + } break; } } - return event_list; } - -// Separate function for SDL_WINDOWEVENT subtypes -void SDLContext::handle_window_event(const SDL_WindowEvent & window_event, - std::vector & event_list) { - switch (window_event.event) { - case SDL_WINDOWEVENT_EXPOSED: - event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_RESIZED: - event_list.push_back({SDLContext::EventType::WINDOW_RESIZE, - {}, - {}, - {{}, {window_event.data1, window_event.data2}}}); - break; - case SDL_WINDOWEVENT_MOVED: - event_list.push_back({SDLContext::EventType::WINDOW_MOVE, - {}, - {}, - {{window_event.data1, window_event.data2}, {}}}); - break; - case SDL_WINDOWEVENT_MINIMIZED: - event_list.push_back({SDLContext::EventType::WINDOW_MINIMIZE, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_MAXIMIZED: - event_list.push_back({SDLContext::EventType::WINDOW_MAXIMIZE, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_FOCUS_GAINED: - event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_GAIN, {}, {}, {}}); - break; - case SDL_WINDOWEVENT_FOCUS_LOST: - event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_LOST, {}, {}, {}}); - break; - } -} - void SDLContext::set_color_texture(const Texture & texture, const Color & color) { - SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b); - SDL_SetTextureAlphaMod(texture.texture.get(), color.a); + SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b); + SDL_SetTextureAlphaMod(texture.get_img(), color.a); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 46b779f..6b725e3 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index fe5466f..52454a1 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -5,17 +5,19 @@ #include #include #include +#include using namespace crepe; - int main() { + SDLFontContext font_facade; Mediator mediator; + SDLContext sdl_context{mediator}; try{ // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test","OpenSymbol"); std::unique_ptr asset = font_facade.get_font_asset(label->font_family); std::cout << "path: " << asset->get_path() << std::endl; - std::unique_ptr font = make_unique(asset,mediator) + std::unique_ptr font = std::make_unique(*asset,mediator); }catch (const std::exception& e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From 054b1ebea645bb8916f87e0d8f0f85d59e998eaf Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:16:19 +0100 Subject: font facade added to SDLContext --- src/crepe/api/Text.h | 6 ++++-- src/crepe/facade/Font.h | 31 ++++++++++++++++++------------- src/crepe/facade/SDLContext.h | 5 ++++- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 51bab98..8436611 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -20,8 +20,6 @@ public: Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset,std::string text,std::string font_family); //! Text data that does not have to be set in the constructor struct Data { - //! Label text color. - Color text_color = Color::BLACK; /** * \brief fontsize for text rendering * @@ -32,11 +30,15 @@ public: * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. */ unsigned int font_size = 16; + //! Layer sorting level of the text const int sorting_in_layer = 0; //! Order within the sorting text const int order_in_layer = 0; + + //! Label text color. + Color text_color = Color::BLACK; }; public: //! font family name such as (Arial,Helvetica,Inter) diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index a27676b..fbc1b8f 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -1,35 +1,40 @@ #pragma once - -#include #include +#include #include "../api/Asset.h" #include "../api/Config.h" #include "../Resource.h" + namespace crepe { /** * \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 Asset with font data to load. - * \param mediator use the SDLContext reference to get_font() - */ + * \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 getter for TTF_Font - * - * \param src Asset with font data to load. - * \param mediator use the SDLContext reference to get_font() - */ - TTF_Font* get_font() const; + /** + * \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. + //! The SDL_ttf font object with custom deleter. std::unique_ptr font; }; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 6b725e3..bbbb542 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -16,9 +16,10 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" - #include "types.h" +#include "SDLFontContext.h" + namespace crepe { class Texture; @@ -226,6 +227,7 @@ public: void set_color_texture(const Texture & texture, const Color & color); private: + //! sdl Window std::unique_ptr> game_window; @@ -234,6 +236,7 @@ private: //! black bars rectangle to draw SDL_FRect black_bars[2] = {}; + SDLFontContext font_facade{}; }; } // namespace crepe -- cgit v1.2.3 From 24818173c970829553d75787ed6a60e6c95e16c6 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:17:29 +0100 Subject: fontcontext removed from sdlcontext --- src/crepe/facade/SDLContext.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index bbbb542..957ee32 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -18,7 +18,6 @@ #include "api/Transform.h" #include "types.h" -#include "SDLFontContext.h" namespace crepe { @@ -236,7 +235,6 @@ private: //! black bars rectangle to draw SDL_FRect black_bars[2] = {}; - SDLFontContext font_facade{}; }; } // namespace crepe -- cgit v1.2.3 From 2eaebb2a79ef71589a0be89cfe419f874fbadd09 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:21:16 +0100 Subject: removed asset manager --- src/crepe/api/AssetManager.h | 62 ------------------------------------- src/crepe/facade/SDLFontContext.cpp | 18 +++++------ 2 files changed, 9 insertions(+), 71 deletions(-) delete mode 100644 src/crepe/api/AssetManager.h diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h deleted file mode 100644 index 3b1cc4b..0000000 --- a/src/crepe/api/AssetManager.h +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace crepe { - -/** - * \brief The AssetManager is responsible for storing and managing assets over multiple scenes. - * - * The AssetManager ensures that assets are loaded once and can be accessed across different - * scenes. It caches assets to avoid reloading them every time a scene is loaded. Assets are - * retained in memory until the AssetManager is destroyed, at which point the cached assets are - * cleared. - */ -class AssetManager { - -private: - //! A cache that holds all the assets, accessible by their file path, over multiple scenes. - std::unordered_map asset_cache; - -private: - AssetManager(); - virtual ~AssetManager(); - -public: - AssetManager(const AssetManager &) = delete; - AssetManager(AssetManager &&) = delete; - AssetManager & operator=(const AssetManager &) = delete; - AssetManager & operator=(AssetManager &&) = delete; - - /** - * \brief Retrieves the singleton instance of the AssetManager. - * - * \return A reference to the single instance of the AssetManager. - */ - static AssetManager & get_instance(); - -public: - /** - * \brief Caches an asset by loading it from the given file path. - * - * \param file_path The path to the asset file to load. - * \param reload If true, the asset will be reloaded from the file, even if it is already - * cached. - * \tparam T The type of asset to cache (e.g., texture, sound, etc.). - * - * \return A shared pointer to the cached asset. - * - * This template function caches the asset at the given file path. If the asset is already - * cached and `reload` is false, the existing cached version will be returned. Otherwise, the - * asset will be reloaded and added to the cache. - */ - template - std::shared_ptr cache(const std::string & file_path, bool reload = false); -}; - -} // namespace crepe - -#include "AssetManager.hpp" diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index d7a0bff..e7f04cc 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -8,26 +8,26 @@ using namespace std; SDLFontContext::SDLFontContext(){ if (!FcInit()) { - throw std::runtime_error("Failed to initialize Fontconfig."); + throw runtime_error("Failed to initialize Fontconfig."); } } SDLFontContext::~SDLFontContext(){ FcFini(); } -unique_ptr SDLFontContext::get_font_asset(const std::string & font_family) { +unique_ptr SDLFontContext::get_font_asset(const string & font_family) { // Create a pattern to search for the font family FcPattern* pattern = FcNameParse(reinterpret_cast(font_family.c_str())); if (!pattern) { - throw std::runtime_error("Failed to create font pattern."); + throw runtime_error("Failed to create font pattern."); } // Default configuration FcConfig* config = FcConfigGetCurrent(); if (!config) { FcPatternDestroy(pattern); - throw std::runtime_error("Failed to get current Fontconfig configuration."); + throw runtime_error("Failed to get current Fontconfig configuration."); } // Match the font pattern @@ -36,18 +36,18 @@ unique_ptr SDLFontContext::get_font_asset(const std::string & font_family FcPatternDestroy(pattern); if (!matched_pattern) { - throw std::runtime_error("No matching font found."); + throw runtime_error("No matching font found."); } // Extract the file path FcChar8* file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch || !file_path) { FcPatternDestroy(matched_pattern); - throw std::runtime_error("Failed to get font file path."); + throw runtime_error("Failed to get font file path."); } - // Convert the file path to a std::string - std::string font_file_path(reinterpret_cast(file_path)); + // Convert the file path to a string + string font_file_path(reinterpret_cast(file_path)); FcPatternDestroy(matched_pattern); - return std::move(make_unique(font_file_path)); + return move(make_unique(font_file_path)); } -- cgit v1.2.3 From 1658c71bb7d3676699e9519131d6ea542e74fc83 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:22:06 +0100 Subject: added enter between functions --- src/crepe/facade/SDLContext.h | 1 - src/crepe/facade/SDLFontContext.cpp | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 554880f..d7b8365 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -18,7 +18,6 @@ #include "api/Transform.h" #include "types.h" - namespace crepe { class Texture; diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index e7f04cc..4fb0e88 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -15,6 +15,7 @@ SDLFontContext::SDLFontContext(){ SDLFontContext::~SDLFontContext(){ FcFini(); } + unique_ptr SDLFontContext::get_font_asset(const string & font_family) { // Create a pattern to search for the font family -- cgit v1.2.3 From f9f06f54dd1a66350afd42f6eec761565a4d3d77 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:23:46 +0100 Subject: added pattern removal on exception --- src/crepe/facade/SDLFontContext.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 4fb0e88..851016b 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -37,6 +37,7 @@ unique_ptr SDLFontContext::get_font_asset(const string & font_family) { FcPatternDestroy(pattern); if (!matched_pattern) { + FcPatternDestroy(matched_pattern); throw runtime_error("No matching font found."); } -- cgit v1.2.3 From 5ef7c56e44a864e580810952450c43c0f9a7b6e0 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:26:03 +0100 Subject: make format --- src/crepe/api/Text.cpp | 7 +++- src/crepe/api/Text.h | 8 ++-- src/crepe/facade/Font.cpp | 28 ++++++------- src/crepe/facade/Font.h | 16 ++++---- src/crepe/facade/SDLContext.cpp | 4 +- src/crepe/facade/SDLContext.h | 3 +- src/crepe/facade/SDLFontContext.cpp | 80 ++++++++++++++++++------------------- src/crepe/facade/SDLFontContext.h | 21 +++++----- src/example/loadfont.cpp | 34 ++++++++-------- 9 files changed, 102 insertions(+), 99 deletions(-) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 12d01f1..b30ccf6 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -2,5 +2,8 @@ using namespace crepe; -Text::Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset,std::string text,std::string font_family) : UIObject(id,dimensions,offset),text(text), font_family(font_family){} - +Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, std::string text, + std::string font_family) + : UIObject(id, dimensions, offset), + text(text), + font_family(font_family) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 8436611..ebf413b 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -8,16 +8,17 @@ #include "Color.h" #include "UIObject.h" -namespace crepe{ +namespace crepe { /** * \brief Text UIObject component for displaying text * * This class can be used to display text on screen. By setting the font_family to a font already stored on the current device it will automatically be loaded in. */ -class Text : public UIObject{ +class Text : public UIObject { public: - Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset,std::string text,std::string font_family); + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, std::string text, + std::string font_family); //! Text data that does not have to be set in the constructor struct Data { /** @@ -40,6 +41,7 @@ public: //! Label text color. Color text_color = Color::BLACK; }; + public: //! font family name such as (Arial,Helvetica,Inter) std::string font_family = ""; diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 37dee3a..85b0e13 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -5,21 +5,21 @@ using namespace std; using namespace crepe; -Font::Font(const Asset& src, Mediator& mediator) - : Resource(src, mediator), font(nullptr, TTF_CloseFont) { - // Get the font file path from the Asset - const std::string font_path = src.get_path(); +Font::Font(const Asset & src, Mediator & mediator) + : Resource(src, mediator), + font(nullptr, TTF_CloseFont) { + // Get the font file path from the Asset + const std::string font_path = src.get_path(); - // Attempt to load the font - this->font.reset(TTF_OpenFont(font_path.c_str(), Config::get_instance().font.size)); + // Attempt to load the font + this->font.reset(TTF_OpenFont(font_path.c_str(), Config::get_instance().font.size)); - // Check if font loading failed - if (!this->font) { - throw runtime_error(format("Failed to load font from path: {}" - ". SDL_ttf error: {}",font_path, TTF_GetError())); - } + // Check if font loading failed + if (!this->font) { + throw runtime_error(format("Failed to load font from path: {}" + ". SDL_ttf error: {}", + font_path, TTF_GetError())); + } } -TTF_Font* Font::get_font() const { - return this->font.get(); -} +TTF_Font * Font::get_font() const { return this->font.get(); } diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index fbc1b8f..983ef31 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -2,9 +2,9 @@ #include #include +#include "../Resource.h" #include "../api/Asset.h" #include "../api/Config.h" -#include "../Resource.h" namespace crepe { @@ -15,15 +15,15 @@ namespace crepe { * 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{ +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); + 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 @@ -31,11 +31,11 @@ public: * * \return The raw TTF_Font object wrapped in a unique pointer. */ - TTF_Font* get_font() const; + TTF_Font * get_font() const; private: - //! The SDL_ttf font object with custom deleter. - std::unique_ptr font; + //! The SDL_ttf font object with custom deleter. + std::unique_ptr font; }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 274c14a..cbb0f3b 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -32,8 +32,8 @@ using namespace std; SDLContext::SDLContext(Mediator & mediator) { dbg_trace(); if (TTF_Init() == -1) { - throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); - } + throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); + } if (SDL_Init(SDL_INIT_VIDEO) != 0) { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index d7b8365..76cd99a 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -4,8 +4,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -225,7 +225,6 @@ public: void set_color_texture(const Texture & texture, const Color & color); private: - //! sdl Window std::unique_ptr> game_window; diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 851016b..2bccac4 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -2,54 +2,52 @@ #include "SDLFontContext.h" - using namespace crepe; using namespace std; -SDLFontContext::SDLFontContext(){ +SDLFontContext::SDLFontContext() { if (!FcInit()) { - throw runtime_error("Failed to initialize Fontconfig."); - } + throw runtime_error("Failed to initialize Fontconfig."); + } } -SDLFontContext::~SDLFontContext(){ - FcFini(); -} +SDLFontContext::~SDLFontContext() { FcFini(); } unique_ptr SDLFontContext::get_font_asset(const string & font_family) { - - // Create a pattern to search for the font family - FcPattern* pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (!pattern) { - throw runtime_error("Failed to create font pattern."); - } - - // Default configuration - FcConfig* config = FcConfigGetCurrent(); - if (!config) { - FcPatternDestroy(pattern); - throw runtime_error("Failed to get current Fontconfig configuration."); - } - - // Match the font pattern - FcResult result; - FcPattern* matched_pattern = FcFontMatch(config, pattern, &result); - FcPatternDestroy(pattern); - - if (!matched_pattern) { + + // Create a pattern to search for the font family + FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); + if (!pattern) { + throw runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig * config = FcConfigGetCurrent(); + if (!config) { + FcPatternDestroy(pattern); + throw runtime_error("Failed to get current Fontconfig configuration."); + } + + // Match the font pattern + FcResult result; + FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); + FcPatternDestroy(pattern); + + if (!matched_pattern) { FcPatternDestroy(matched_pattern); - throw runtime_error("No matching font found."); - } - - // Extract the file path - FcChar8* file_path = nullptr; - if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch || !file_path) { - FcPatternDestroy(matched_pattern); - throw runtime_error("Failed to get font file path."); - } - - // Convert the file path to a string - string font_file_path(reinterpret_cast(file_path)); - FcPatternDestroy(matched_pattern); - return move(make_unique(font_file_path)); + throw runtime_error("No matching font found."); + } + + // Extract the file path + FcChar8 * file_path = nullptr; + if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch + || !file_path) { + FcPatternDestroy(matched_pattern); + throw runtime_error("Failed to get font file path."); + } + + // Convert the file path to a string + string font_file_path(reinterpret_cast(file_path)); + FcPatternDestroy(matched_pattern); + return move(make_unique(font_file_path)); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index c890b2d..b9e1f23 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -1,17 +1,17 @@ #pragma once -#include #include #include +#include #include "../api/Asset.h" namespace crepe { - class SDLFontContext{ - public: - SDLFontContext(); - ~SDLFontContext(); - /** +class SDLFontContext { +public: + SDLFontContext(); + ~SDLFontContext(); + /** * * \brief Facade function to convert a font_family into an asset. * @@ -19,8 +19,9 @@ namespace crepe { * * \param font_family Name of the font family name. */ - std::unique_ptr get_font_asset(const std::string & font_family); - private: - }; + std::unique_ptr get_font_asset(const std::string & font_family); + +private: +}; -} +} // namespace crepe diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 52454a1..efd5a98 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,27 +1,27 @@ -#include -#include -#include -#include #include #include -#include #include +#include +#include +#include +#include +#include using namespace crepe; int main() { - - SDLFontContext font_facade; + + SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; - try{ - // Correct way to create a unique pointer for Text - std::unique_ptr label = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test","OpenSymbol"); - std::unique_ptr asset = font_facade.get_font_asset(label->font_family); - std::cout << "path: " << asset->get_path() << std::endl; - std::unique_ptr font = std::make_unique(*asset,mediator); - }catch (const std::exception& e) { - std::cout << "Standard exception thrown: " << e.what() << std::endl; + try { + // Correct way to create a unique pointer for Text + std::unique_ptr label + = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol"); + std::unique_ptr asset = font_facade.get_font_asset(label->font_family); + std::cout << "path: " << asset->get_path() << std::endl; + std::unique_ptr font = std::make_unique(*asset, mediator); + } catch (const std::exception & e) { + std::cout << "Standard exception thrown: " << e.what() << std::endl; } - - return 0; + return 0; } -- cgit v1.2.3 From cb8af2747fa6999d1a4b97c4785a9cfb37b535c4 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:26:31 +0100 Subject: removed reference --- src/crepe/facade/SDLFontContext.cpp | 2 +- src/crepe/facade/SDLFontContext.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 2bccac4..1037ac4 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -13,7 +13,7 @@ SDLFontContext::SDLFontContext() { SDLFontContext::~SDLFontContext() { FcFini(); } -unique_ptr SDLFontContext::get_font_asset(const string & font_family) { +unique_ptr SDLFontContext::get_font_asset(const string font_family) { // Create a pattern to search for the font family FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index b9e1f23..efeaa33 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -19,7 +19,7 @@ public: * * \param font_family Name of the font family name. */ - std::unique_ptr get_font_asset(const std::string & font_family); + std::unique_ptr get_font_asset(const std::string font_family); private: }; -- cgit v1.2.3 From fb33482a14915373bdfdaeb7d4856cde31d922ab Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:28:29 +0100 Subject: added small check to the example --- src/example/loadfont.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index efd5a98..3cbe559 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -19,6 +19,15 @@ int main() { std::unique_ptr asset = font_facade.get_font_asset(label->font_family); std::cout << "path: " << asset->get_path() << std::endl; std::unique_ptr font = std::make_unique(*asset, mediator); + // Get the TTF_Font from the Font object + TTF_Font* ttf_font = font->get_font(); + + // Check if the font is loaded properly + if (ttf_font != nullptr) { + std::cout << "Font successfully loaded!" << std::endl; + } else { + std::cout << "Failed to load font." << std::endl; + } } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From a5810032c3cf012280271b5425411e08438d72d2 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:45:29 +0100 Subject: removed asset include --- src/crepe/api/Text.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index ebf413b..f8ce845 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -4,12 +4,10 @@ #include "../Component.h" -#include "Asset.h" #include "Color.h" #include "UIObject.h" namespace crepe { - /** * \brief Text UIObject component for displaying text * -- cgit v1.2.3 From 55329845de3aceef8b3672779f3930cee18e2298 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:47:46 +0100 Subject: added const std::string& --- src/crepe/api/Text.cpp | 4 ++-- src/crepe/api/Text.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index b30ccf6..c072ee7 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -2,8 +2,8 @@ using namespace crepe; -Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, std::string text, - std::string font_family) +Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, + const std::string & font_family) : UIObject(id, dimensions, offset), text(text), font_family(font_family) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index f8ce845..2ad1db3 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -15,8 +15,8 @@ namespace crepe { */ class Text : public UIObject { public: - Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, std::string text, - std::string font_family); + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string &, + const std::string & font_family); //! Text data that does not have to be set in the constructor struct Data { /** -- cgit v1.2.3 From 86e3dcbc1e5b2fe07d89d6cde21f4b9e687962fa Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 21:49:39 +0100 Subject: fixed format --- src/crepe/facade/Font.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 85b0e13..a39af75 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -16,8 +16,7 @@ Font::Font(const Asset & src, Mediator & mediator) // Check if font loading failed if (!this->font) { - throw runtime_error(format("Failed to load font from path: {}" - ". SDL_ttf error: {}", + throw runtime_error(format("Failed to load font from path: {}. SDL_ttf error: {}", font_path, TTF_GetError())); } } -- cgit v1.2.3 From d3a04fb1e8b119017375caab74c43674006a7348 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 22:04:49 +0100 Subject: broken particle test --- src/crepe/api/ParticleEmitter.cpp | 2 +- src/crepe/api/ParticleEmitter.h | 10 ++++++---- src/crepe/system/ParticleSystem.cpp | 6 +++--- src/crepe/system/RenderSystem.cpp | 2 +- src/test/ParticleTest.cpp | 37 +++++++++++++++++++++---------------- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 1cfdceb..1e9cfaa 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -7,6 +7,6 @@ ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & : Component(game_object_id), sprite(sprite), data(data) { for (size_t i = 0; i < this->data.max_particles; i++) { - this->data.particles.emplace_back(); + this->particles.emplace_back(); } } diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index cc54ffb..e0b117a 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -3,9 +3,11 @@ #include #include +#include "system/ParticleSystem.h" +#include "system/RenderSystem.h" + #include "Component.h" #include "Particle.h" -#include "system/ParticleSystem.h" #include "types.h" namespace crepe { @@ -69,9 +71,6 @@ public: vec2 force_over_time; //! particle boundary Boundary boundary; - //! collection of particles - std::vector particles; - }; public: @@ -87,7 +86,10 @@ public: private: //! Saves time left over from last update event. friend ParticleSystem; + friend RenderSystem; float spawn_accumulator = 0; + //! collection of particles + std::vector particles; }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 85b8248..a56de60 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -34,7 +34,7 @@ void ParticleSystem::update() { } // Update all particles - for (Particle & particle : emitter.data.particles) { + for (Particle & particle : emitter.particles) { if (particle.active) { particle.update(dt); } @@ -57,7 +57,7 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & vec2 velocity = {random_speed * std::cos(angle_radians), random_speed * std::sin(angle_radians)}; - for (Particle & particle : emitter.data.particles) { + for (Particle & particle : emitter.particles) { if (!particle.active) { particle.reset(emitter.data.end_lifespan, initial_position, velocity, random_angle); @@ -76,7 +76,7 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t const float TOP = offset.y - half_height; const float BOTTOM = offset.y + half_height; - for (Particle & particle : emitter.data.particles) { + for (Particle & particle : emitter.particles) { const vec2 & position = particle.position; bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP && position.y <= BOTTOM); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index cc633e8..505433a 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -87,7 +87,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) rendering_particles = true; if (!em.active) continue; - for (const Particle & p : em.data.particles) { + for (const Particle & p : em.particles) { if (!p.active) continue; ctx.draw(SDLContext::RenderContext{ diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 9112a3f..70534f3 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -11,10 +11,14 @@ #include #include +#define protected public +#define private public + using namespace std; using namespace std::chrono_literals; using namespace crepe; + class ParticlesTest : public ::testing::Test { Mediator m; @@ -57,6 +61,7 @@ public: }, .sprite = test_sprite, }); + } transforms = mgr.get_components_by_id(0); Transform & transform = transforms.front().get(); @@ -76,7 +81,7 @@ public: emitter.data.end_lifespan = 0; emitter.data.force_over_time = vec2{0, 0}; emitter.data.boundary = {0, 0, vec2{0, 0}, false}; - for (auto & particle : emitter.data.particles) { + for (auto & particle : emitter.particles) { particle.active = false; } } @@ -95,19 +100,19 @@ TEST_F(ParticlesTest, spawnParticle) { emitter.data.max_angle = 10; particle_system.update(); //check if nothing happend - EXPECT_EQ(emitter.data.particles[0].active, false); + EXPECT_EQ(emitter.particles[0].active, false); emitter.data.emission_rate = 1; //check particle spawnes particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].active, true); + EXPECT_EQ(emitter.particles[0].active, true); particle_system.update(); - EXPECT_EQ(emitter.data.particles[1].active, true); + EXPECT_EQ(emitter.particles[1].active, true); particle_system.update(); - EXPECT_EQ(emitter.data.particles[2].active, true); + EXPECT_EQ(emitter.particles[2].active, true); particle_system.update(); - EXPECT_EQ(emitter.data.particles[3].active, true); + EXPECT_EQ(emitter.particles[3].active, true); - for (auto & particle : emitter.data.particles) { + for (auto & particle : emitter.particles) { // Check velocity range EXPECT_GE(particle.velocity.x, emitter.data.min_speed); // Speed should be greater than or equal to min_speed @@ -139,7 +144,7 @@ TEST_F(ParticlesTest, moveParticleHorizontal) { emitter.data.emission_rate = 1; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].position.x, a); + EXPECT_EQ(emitter.particles[0].position.x, a); } } @@ -157,7 +162,7 @@ TEST_F(ParticlesTest, moveParticleVertical) { emitter.data.emission_rate = 1; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].position.y, a); + EXPECT_EQ(emitter.particles[0].position.y, a); } } @@ -177,7 +182,7 @@ TEST_F(ParticlesTest, boundaryParticleReset) { for (int a = 0; a < emitter.data.boundary.width / 2 + 1; a++) { particle_system.update(); } - EXPECT_EQ(emitter.data.particles[0].active, false); + EXPECT_EQ(emitter.particles[0].active, false); } TEST_F(ParticlesTest, boundaryParticleStop) { @@ -197,12 +202,12 @@ TEST_F(ParticlesTest, boundaryParticleStop) { particle_system.update(); } const double TOLERANCE = 0.01; - EXPECT_NEAR(emitter.data.particles[0].velocity.x, 0, TOLERANCE); - EXPECT_NEAR(emitter.data.particles[0].velocity.y, 0, TOLERANCE); - if (emitter.data.particles[0].velocity.x != 0) - EXPECT_NEAR(std::abs(emitter.data.particles[0].position.x), + EXPECT_NEAR(emitter.particles[0].velocity.x, 0, TOLERANCE); + EXPECT_NEAR(emitter.particles[0].velocity.y, 0, TOLERANCE); + if (emitter.particles[0].velocity.x != 0) + EXPECT_NEAR(std::abs(emitter.particles[0].position.x), emitter.data.boundary.height / 2, TOLERANCE); - if (emitter.data.particles[0].velocity.y != 0) - EXPECT_NEAR(std::abs(emitter.data.particles[0].position.y), + if (emitter.particles[0].velocity.y != 0) + EXPECT_NEAR(std::abs(emitter.particles[0].position.y), emitter.data.boundary.width / 2, TOLERANCE); } -- cgit v1.2.3 From 68f7ea611af5b9a4c19375ab3d4a351facdea9a9 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 18:37:48 +0100 Subject: updated test --- src/test/CMakeLists.txt | 46 +++++++++++++++++++++++----------------------- src/test/ParticleTest.cpp | 31 ++++++++++++++++--------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 11b4ca9..ed0be5c 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,27 +1,27 @@ target_sources(test_main PUBLIC main.cpp - CollisionTest.cpp - PhysicsTest.cpp - ScriptTest.cpp + # CollisionTest.cpp + # PhysicsTest.cpp + # ScriptTest.cpp ParticleTest.cpp - AudioTest.cpp - AssetTest.cpp - ResourceManagerTest.cpp - OptionalRefTest.cpp - RenderSystemTest.cpp - EventTest.cpp - ECSTest.cpp - SceneManagerTest.cpp - ValueBrokerTest.cpp - DBTest.cpp - Vector2Test.cpp - LoopManagerTest.cpp - LoopTimerTest.cpp - InputTest.cpp - ScriptEventTest.cpp - ScriptSceneTest.cpp - Profiling.cpp - SaveManagerTest.cpp - ScriptSaveManagerTest.cpp - ScriptECSTest.cpp + # AudioTest.cpp + # AssetTest.cpp + # ResourceManagerTest.cpp + # OptionalRefTest.cpp + # RenderSystemTest.cpp + # EventTest.cpp + # ECSTest.cpp + # SceneManagerTest.cpp + # ValueBrokerTest.cpp + # DBTest.cpp + # Vector2Test.cpp + # LoopManagerTest.cpp + # LoopTimerTest.cpp + # InputTest.cpp + # ScriptEventTest.cpp + # ScriptSceneTest.cpp + # Profiling.cpp + # SaveManagerTest.cpp + # ScriptSaveManagerTest.cpp + # ScriptECSTest.cpp ) diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 70534f3..a9a26c6 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,18 +1,19 @@ #include "api/Asset.h" -#include +#include +#include #include #include -#include #include #include #include #include -#include -#include -#include - +#include #define protected public #define private public +#include +#include +#include + using namespace std; using namespace std::chrono_literals; @@ -25,6 +26,7 @@ class ParticlesTest : public ::testing::Test { public: ComponentManager component_manager{m}; ParticleSystem particle_system{m}; + LoopTimerManager loop_timer{m}; void SetUp() override { ComponentManager & mgr = this->component_manager; @@ -42,7 +44,7 @@ public: .size = {10, 10}, }); - game_object.add_component(ParticleEmitter::Data{ + game_object.add_component(test_sprite,ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 100, .emission_rate = 0, @@ -59,7 +61,6 @@ public: .offset = vec2{0, 0}, .reset_on_exit = false, }, - .sprite = test_sprite, }); } @@ -101,7 +102,7 @@ TEST_F(ParticlesTest, spawnParticle) { particle_system.update(); //check if nothing happend EXPECT_EQ(emitter.particles[0].active, false); - emitter.data.emission_rate = 1; + emitter.data.emission_rate = 50; //check particle spawnes particle_system.update(); EXPECT_EQ(emitter.particles[0].active, true); @@ -138,10 +139,10 @@ TEST_F(ParticlesTest, moveParticleHorizontal) { emitter.data.end_lifespan = 100; emitter.data.boundary.height = 100; emitter.data.boundary.width = 100; - emitter.data.min_speed = 1; - emitter.data.max_speed = 1; + emitter.data.min_speed = 50; + emitter.data.max_speed = 50; emitter.data.max_angle = 0; - emitter.data.emission_rate = 1; + emitter.data.emission_rate = 50; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { particle_system.update(); EXPECT_EQ(emitter.particles[0].position.x, a); @@ -155,11 +156,11 @@ TEST_F(ParticlesTest, moveParticleVertical) { emitter.data.end_lifespan = 100; emitter.data.boundary.height = 100; emitter.data.boundary.width = 100; - emitter.data.min_speed = 1; - emitter.data.max_speed = 1; + emitter.data.min_speed = 50; + emitter.data.max_speed = 50; emitter.data.min_angle = 90; emitter.data.max_angle = 90; - emitter.data.emission_rate = 1; + emitter.data.emission_rate = 50; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { particle_system.update(); EXPECT_EQ(emitter.particles[0].position.y, a); -- cgit v1.2.3 From 5ad9f5c81ae20f4b7aa2c1773bce8906096754f3 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 18:49:51 +0100 Subject: friend explained --- src/crepe/Particle.h | 2 -- src/crepe/api/ParticleEmitter.h | 10 ++++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index 49fec1f..0170117 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -14,8 +14,6 @@ namespace crepe { * can also be reset or stopped. */ class Particle { - // TODO: add friend particleSsytem and rendersystem. Unit test will fail. - public: //! Position of the particle in 2D space. vec2 position; diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index e0b117a..5b8e8e3 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -54,11 +54,11 @@ public: //! maximum number of particles const unsigned int max_particles = 256; //! rate of particle emission per second - float emission_rate = 1; + float emission_rate = 50; //! min speed of the particles - float min_speed = 1; + float min_speed = 100; //! min speed of the particles - float max_speed = 2; + float max_speed = 100; //! min angle of particle emission float min_angle = 0; //! max angle of particle emission @@ -84,9 +84,11 @@ public: //! Configuration data for particle emission settings. Data data; private: - //! Saves time left over from last update event. + //! Only ParticleSystem can move and read particles friend ParticleSystem; + //! Only RenderSystem can read particles friend RenderSystem; + //! Saves time left over from last update event. float spawn_accumulator = 0; //! collection of particles std::vector particles; -- cgit v1.2.3 From 79741613fdf915bfb8b7517826205d2cb1715971 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 18:54:30 +0100 Subject: removed some not needed const --- src/crepe/system/ParticleSystem.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index a56de60..f98f245 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -71,24 +71,25 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t float half_width = emitter.data.boundary.width / 2.0; float half_height = emitter.data.boundary.height / 2.0; - const float LEFT = offset.x - half_width; - const float RIGHT = offset.x + half_width; - const float TOP = offset.y - half_height; - const float BOTTOM = offset.y + half_height; + float left = offset.x - half_width; + float right = offset.x + half_width; + float top = offset.y - half_height; + float bottom = offset.y + half_height; for (Particle & particle : emitter.particles) { const vec2 & position = particle.position; - bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP - && position.y <= BOTTOM); + bool within_bounds = (position.x >= left && position.x <= right && position.y >= top + && position.y <= bottom); + //if not within bounds do a reset or stop velocity if (!within_bounds) { if (emitter.data.boundary.reset_on_exit) { particle.active = false; } else { particle.velocity = {0, 0}; - if (position.x < LEFT) particle.position.x = LEFT; - else if (position.x > RIGHT) particle.position.x = RIGHT; - if (position.y < TOP) particle.position.y = TOP; - else if (position.y > BOTTOM) particle.position.y = BOTTOM; + if (position.x < left) particle.position.x = left; + else if (position.x > right) particle.position.x = right; + if (position.y < top) particle.position.y = top; + else if (position.y > bottom) particle.position.y = bottom; } } } -- cgit v1.2.3 From 512aa7f54b88994d3095971b2001930b4e612947 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:44:32 +0100 Subject: make format --- src/crepe/Particle.h | 3 ++- src/crepe/api/ParticleEmitter.cpp | 6 +++-- src/crepe/api/ParticleEmitter.h | 5 ++-- src/crepe/system/ParticleSystem.cpp | 10 ++++---- src/crepe/system/ParticleSystem.h | 1 - src/example/game.cpp | 31 ++++++++++++------------ src/example/rendering_particle.cpp | 1 - src/test/ParticleTest.cpp | 48 ++++++++++++++++++------------------- 8 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index 0170117..ee0cd66 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -41,7 +41,8 @@ public: * \param velocity The initial velocity of the particle. * \param angle The angle of the particle's trajectory or orientation. */ - void reset(unsigned int lifespan, const vec2 & position, const vec2 & velocity, float angle); + void reset(unsigned int lifespan, const vec2 & position, const vec2 & velocity, + float angle); /** * \brief Updates the particle's state. * diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 1e9cfaa..4f54bbd 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -3,9 +3,11 @@ using namespace crepe; -ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, const Data & data) +ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, + const Data & data) : Component(game_object_id), - sprite(sprite), data(data) { + sprite(sprite), + data(data) { for (size_t i = 0; i < this->data.max_particles; i++) { this->particles.emplace_back(); } diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 5b8e8e3..be970f5 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -78,18 +78,19 @@ public: * \param game_object_id Identifier for the game object using this emitter. * \param data Configuration data defining particle properties. */ - ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite,const Data & data); + ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, const Data & data); public: //! Configuration data for particle emission settings. Data data; + private: //! Only ParticleSystem can move and read particles friend ParticleSystem; //! Only RenderSystem can read particles friend RenderSystem; //! Saves time left over from last update event. - float spawn_accumulator = 0; + float spawn_accumulator = 0; //! collection of particles std::vector particles; }; diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index f98f245..31a4e9e 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -31,8 +31,8 @@ void ParticleSystem::update() { while (emitter.spawn_accumulator >= 1.0) { this->emit_particle(emitter, transform); emitter.spawn_accumulator -= 1.0; - } - + } + // Update all particles for (Particle & particle : emitter.particles) { if (particle.active) { @@ -49,9 +49,11 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & constexpr float DEG_TO_RAD = M_PI / 180.0; vec2 initial_position = emitter.data.position + transform.position; - float random_angle = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); + float random_angle + = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); - float random_speed = this->generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); + float random_speed + = this->generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); float angle_radians = random_angle * DEG_TO_RAD; vec2 velocity diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 95a2bd5..154521d 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -23,7 +23,6 @@ public: void update() override; private: - /** * \brief Emits a particle from the specified emitter based on its emission properties. * diff --git a/src/example/game.cpp b/src/example/game.cpp index 279648e..2d25153 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -261,24 +261,23 @@ public: = false; Asset img5{"asset/texture/square.png"}; - - GameObject particle = mgr.new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); - auto & particle_image = particle.add_component(img5, Sprite::Data{.size = {5, 5},}); - auto & test = particle.add_component(particle_image,ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 256, - .emission_rate = 50, - .min_speed = 10, - .max_speed = 20, - .min_angle = -20, - .max_angle = 20, - .begin_lifespan = 0, - .end_lifespan = 5, - } - ); - + auto & particle_image = particle.add_component(img5, Sprite::Data{ + .size = {5, 5}, + }); + auto & test + = particle.add_component(particle_image, ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 256, + .emission_rate = 50, + .min_speed = 10, + .max_speed = 20, + .min_angle = -20, + .max_angle = 20, + .begin_lifespan = 0, + .end_lifespan = 5, + }); } string get_name() const { return "scene1"; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 2b5c041..add43f4 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -17,7 +17,6 @@ using namespace crepe; using namespace std; - class TestScene : public Scene { public: diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index a9a26c6..8ffb140 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,6 +1,4 @@ #include "api/Asset.h" -#include -#include #include #include #include @@ -8,18 +6,18 @@ #include #include #include +#include +#include #define protected public #define private public #include #include #include - using namespace std; using namespace std::chrono_literals; using namespace crepe; - class ParticlesTest : public ::testing::Test { Mediator m; @@ -44,25 +42,25 @@ public: .size = {10, 10}, }); - game_object.add_component(test_sprite,ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 100, - .emission_rate = 0, - .min_speed = 0, - .max_speed = 0, - .min_angle = 0, - .max_angle = 0, - .begin_lifespan = 0, - .end_lifespan = 0, - .force_over_time = vec2{0, 0}, - .boundary{ - .width = 0, - .height = 0, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - }); - + game_object.add_component(test_sprite, + ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 100, + .emission_rate = 0, + .min_speed = 0, + .max_speed = 0, + .min_angle = 0, + .max_angle = 0, + .begin_lifespan = 0, + .end_lifespan = 0, + .force_over_time = vec2{0, 0}, + .boundary{ + .width = 0, + .height = 0, + .offset = vec2{0, 0}, + .reset_on_exit = false, + }, + }); } transforms = mgr.get_components_by_id(0); Transform & transform = transforms.front().get(); @@ -209,6 +207,6 @@ TEST_F(ParticlesTest, boundaryParticleStop) { EXPECT_NEAR(std::abs(emitter.particles[0].position.x), emitter.data.boundary.height / 2, TOLERANCE); if (emitter.particles[0].velocity.y != 0) - EXPECT_NEAR(std::abs(emitter.particles[0].position.y), - emitter.data.boundary.width / 2, TOLERANCE); + EXPECT_NEAR(std::abs(emitter.particles[0].position.y), emitter.data.boundary.width / 2, + TOLERANCE); } -- cgit v1.2.3 From d3a77a80fbda1f40d1f46552c1e3c2f6cd767668 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 11:51:04 +0100 Subject: reverted --- src/test/CMakeLists.txt | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index ed0be5c..11b4ca9 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,27 +1,27 @@ target_sources(test_main PUBLIC main.cpp - # CollisionTest.cpp - # PhysicsTest.cpp - # ScriptTest.cpp + CollisionTest.cpp + PhysicsTest.cpp + ScriptTest.cpp ParticleTest.cpp - # AudioTest.cpp - # AssetTest.cpp - # ResourceManagerTest.cpp - # OptionalRefTest.cpp - # RenderSystemTest.cpp - # EventTest.cpp - # ECSTest.cpp - # SceneManagerTest.cpp - # ValueBrokerTest.cpp - # DBTest.cpp - # Vector2Test.cpp - # LoopManagerTest.cpp - # LoopTimerTest.cpp - # InputTest.cpp - # ScriptEventTest.cpp - # ScriptSceneTest.cpp - # Profiling.cpp - # SaveManagerTest.cpp - # ScriptSaveManagerTest.cpp - # ScriptECSTest.cpp + AudioTest.cpp + AssetTest.cpp + ResourceManagerTest.cpp + OptionalRefTest.cpp + RenderSystemTest.cpp + EventTest.cpp + ECSTest.cpp + SceneManagerTest.cpp + ValueBrokerTest.cpp + DBTest.cpp + Vector2Test.cpp + LoopManagerTest.cpp + LoopTimerTest.cpp + InputTest.cpp + ScriptEventTest.cpp + ScriptSceneTest.cpp + Profiling.cpp + SaveManagerTest.cpp + ScriptSaveManagerTest.cpp + ScriptECSTest.cpp ) -- cgit v1.2.3 From e30057abadb5372d84230fa4ce4238528177a9bf Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 11:53:40 +0100 Subject: fixed game example --- src/example/game.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/example/game.cpp b/src/example/game.cpp index f8520f4..7c9a227 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -262,7 +262,7 @@ public: = false; Asset img5{"asset/texture/square.png"}; - GameObject particle = mgr.new_object( + GameObject particle = new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); auto & particle_image = particle.add_component(img5, Sprite::Data{ .size = {5, 5}, -- cgit v1.2.3 From 76d3108c44d6920c8f6f245c80c10a15267f1d3b Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 13:14:01 +0100 Subject: change position to offset --- src/crepe/api/ParticleEmitter.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index be970f5..8ac2e72 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -49,8 +49,8 @@ public: * and the sprite used for rendering particles. */ struct Data { - //! position of the emitter - vec2 position; + //! offset of the emitter relative to transform + vec2 offset; //! maximum number of particles const unsigned int max_particles = 256; //! rate of particle emission per second -- cgit v1.2.3 From aa1f1f9460fa713e00fd1830b08be743395110ce Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 14 Dec 2024 14:37:55 +0100 Subject: sdl_ttf to submodules + feedback changes --- .vscode/launch.json | 28 ++++++++++++++++++++++++++++ mwe/events/include/event.h | 2 +- readme.md | 1 + src/CMakeLists.txt | 4 ++-- src/crepe/api/Text.cpp | 3 ++- src/crepe/api/Text.h | 4 ++-- src/crepe/facade/Font.cpp | 17 ++++++----------- src/crepe/facade/Font.h | 2 +- src/crepe/facade/SDLFontContext.cpp | 4 ++-- src/crepe/facade/SDLFontContext.h | 6 +++++- src/example/loadfont.cpp | 8 ++++---- 11 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..07bd65f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug test_main", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/src/build/test_main", // Path to your executable + "args": [], // Optional: add any arguments here if needed + "stopAtEntry": false, // Set to true if you want to break at the entry point + "cwd": "${workspaceFolder}", // Working directory for the program + "environment": [], + "externalConsole": false, // Set to true to use an external console + "MIMode": "gdb", // Use "lldb" if you're using LLDB instead of GDB + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "miDebuggerPath": "/usr/bin/gdb", // Path to gdb, adjust based on your system + "preLaunchTask": "", // Optional: specify a pre-launch task like building your project + "sourceFileMap": {}, + "logging": { "moduleLoad": false, "engineLogging": false } + } + ] +} diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index ee1bf52..e1b220b 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent") {}; + ShutDownEvent() : Event("ShutDownEvent"){}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/readme.md b/readme.md index d309b30..984a368 100644 --- a/readme.md +++ b/readme.md @@ -60,6 +60,7 @@ Then, follow these steps for each library you want to install: $ cd lib/googletest $ cd lib/sdl2 $ cd lib/soloud/contrib + $ cd lib/sdl_ttf $ cd lib/sdl_image $ cd lib/sdl_ttf $ cd lib/whereami diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a525c74..696856c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,7 +14,7 @@ find_package(SoLoud REQUIRED) find_package(GTest REQUIRED) find_package(whereami REQUIRED) find_library(BERKELEY_DB db) -find_package(Fontconfig REQUIRED) +find_library(FONTCONFIG_LIB fontconfig) add_library(crepe SHARED) add_executable(test_main EXCLUDE_FROM_ALL) @@ -30,7 +30,7 @@ target_link_libraries(crepe PUBLIC SDL2_image PUBLIC ${BERKELEY_DB} PUBLIC whereami - PUBLIC ${Fontconfig_LIBRARIES} + PUBLIC ${FONTCONFIG_LIB} ) add_subdirectory(crepe) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index c072ee7..9de9038 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -3,7 +3,8 @@ using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, - const std::string & font_family) + const std::string & font_family,const Data & data) : UIObject(id, dimensions, offset), text(text), + data(data), font_family(font_family) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 2ad1db3..96e1265 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -15,8 +15,6 @@ namespace crepe { */ class Text : public UIObject { public: - Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string &, - const std::string & font_family); //! Text data that does not have to be set in the constructor struct Data { /** @@ -41,6 +39,8 @@ public: }; public: + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string &, + const std::string & font_family, const Data& data); //! font family name such as (Arial,Helvetica,Inter) std::string font_family = ""; //! Label text. diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index a39af75..f111af9 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -8,17 +8,12 @@ using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator), font(nullptr, TTF_CloseFont) { - // Get the font file path from the Asset - const std::string font_path = src.get_path(); - - // Attempt to load the font - this->font.reset(TTF_OpenFont(font_path.c_str(), Config::get_instance().font.size)); - - // Check if font loading failed - if (!this->font) { - throw runtime_error(format("Failed to load font from path: {}. SDL_ttf error: {}", - font_path, TTF_GetError())); - } + Config & config = Config::get_instance(); + const std::string FONT_PATH = src.get_path(); + TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); + if (font == NULL) + throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); + this->font = { font, [] (TTF_Font * font) { TTF_CloseFont(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 index 983ef31..f7d5b50 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -35,7 +35,7 @@ public: private: //! The SDL_ttf font object with custom deleter. - std::unique_ptr font; + std::unique_ptr> font; }; } // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 1037ac4..5123b3b 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -13,7 +13,7 @@ SDLFontContext::SDLFontContext() { SDLFontContext::~SDLFontContext() { FcFini(); } -unique_ptr SDLFontContext::get_font_asset(const string font_family) { +Asset SDLFontContext::get_font_asset(const string font_family) { // Create a pattern to search for the font family FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); @@ -49,5 +49,5 @@ unique_ptr SDLFontContext::get_font_asset(const string font_family) { // Convert the file path to a string string font_file_path(reinterpret_cast(file_path)); FcPatternDestroy(matched_pattern); - return move(make_unique(font_file_path)); + return Asset(font_file_path); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index efeaa33..cb3ca9d 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -11,6 +11,10 @@ class SDLFontContext { public: SDLFontContext(); ~SDLFontContext(); + SDLFontContext(const SDLFontContext &) = delete; + SDLFontContext(SDLFontContext &&) = delete; + SDLFontContext & operator=(const SDLFontContext &) = delete; + SDLFontContext & operator=(SDLFontContext &&) = delete; /** * * \brief Facade function to convert a font_family into an asset. @@ -19,7 +23,7 @@ public: * * \param font_family Name of the font family name. */ - std::unique_ptr get_font_asset(const std::string font_family); + Asset get_font_asset(const std::string font_family); private: }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 3cbe559..a52e7f0 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -15,10 +15,10 @@ int main() { try { // Correct way to create a unique pointer for Text std::unique_ptr label - = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol"); - std::unique_ptr asset = font_facade.get_font_asset(label->font_family); - std::cout << "path: " << asset->get_path() << std::endl; - std::unique_ptr font = std::make_unique(*asset, mediator); + = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol",Text::Data{}); + Asset asset = font_facade.get_font_asset(label->font_family); + std::cout << "path: " << asset.get_path() << std::endl; + std::unique_ptr font = std::make_unique(asset, mediator); // Get the TTF_Font from the Font object TTF_Font* ttf_font = font->get_font(); -- cgit v1.2.3 From fda61be91cd8667fa2b51e3b8a17ba6b93b728c8 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 14 Dec 2024 14:45:10 +0100 Subject: feedback changes --- src/crepe/facade/SDLContext.cpp | 3 ++- src/crepe/facade/SDLContext.h | 1 - src/crepe/facade/SDLFontContext.cpp | 8 ++++---- src/example/loadfont.cpp | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index cbb0f3b..47dda81 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -75,8 +76,8 @@ SDLContext::~SDLContext() { // thread that SDL_Init() was called on? This has caused problems for me // before. IMG_Quit(); - SDL_Quit(); TTF_Quit(); + SDL_Quit(); } Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 76cd99a..efdd6fe 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 5123b3b..e0b9a89 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -17,13 +17,13 @@ Asset SDLFontContext::get_font_asset(const string font_family) { // Create a pattern to search for the font family FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (!pattern) { + if (pattern == NULL) { throw runtime_error("Failed to create font pattern."); } // Default configuration FcConfig * config = FcConfigGetCurrent(); - if (!config) { + if (config == NULL) { FcPatternDestroy(pattern); throw runtime_error("Failed to get current Fontconfig configuration."); } @@ -33,7 +33,7 @@ Asset SDLFontContext::get_font_asset(const string font_family) { FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); FcPatternDestroy(pattern); - if (!matched_pattern) { + if (matched_pattern == NULL) { FcPatternDestroy(matched_pattern); throw runtime_error("No matching font found."); } @@ -41,7 +41,7 @@ Asset SDLFontContext::get_font_asset(const string font_family) { // Extract the file path FcChar8 * file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch - || !file_path) { + || file_path == NULL) { FcPatternDestroy(matched_pattern); throw runtime_error("Failed to get font file path."); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index a52e7f0..6020908 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -21,7 +21,8 @@ int main() { std::unique_ptr font = std::make_unique(asset, mediator); // Get the TTF_Font from the Font object TTF_Font* ttf_font = font->get_font(); - + //example if the asset is not correct for font + //std::unique_ptr fontThrow = std::make_unique(Asset("../help.txt"), mediator); // Check if the font is loaded properly if (ttf_font != nullptr) { std::cout << "Font successfully loaded!" << std::endl; -- cgit v1.2.3 From 9579f2a385de42cf74d50038c79a58c14eb0d980 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 14 Dec 2024 14:45:28 +0100 Subject: make format --- src/crepe/api/Text.cpp | 4 ++-- src/crepe/api/Text.h | 4 ++-- src/crepe/facade/Font.cpp | 2 +- src/crepe/facade/Font.h | 2 +- src/crepe/facade/SDLContext.cpp | 2 +- src/example/loadfont.cpp | 6 +++--- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 9de9038..568af7f 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -2,8 +2,8 @@ using namespace crepe; -Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, - const std::string & font_family,const Data & data) +Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::string & text, const std::string & font_family, const Data & data) : UIObject(id, dimensions, offset), text(text), data(data), diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 96e1265..64b2008 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -39,8 +39,8 @@ public: }; public: - Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string &, - const std::string & font_family, const Data& data); + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::string &, const std::string & font_family, const Data & data); //! font family name such as (Arial,Helvetica,Inter) std::string font_family = ""; //! Label text. diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index f111af9..1ee3de1 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -13,7 +13,7 @@ Font::Font(const Asset & src, Mediator & mediator) TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); if (font == NULL) throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); - this->font = { font, [] (TTF_Font * font) { TTF_CloseFont(font); } }; + this->font = {font, [](TTF_Font * font) { TTF_CloseFont(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 index f7d5b50..e93bfe9 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -35,7 +35,7 @@ public: private: //! The SDL_ttf font object with custom deleter. - std::unique_ptr> font; + std::unique_ptr> font; }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 47dda81..d1d109c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -6,8 +6,8 @@ #include #include #include -#include #include +#include #include #include #include diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 6020908..dae64bc 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -14,13 +14,13 @@ int main() { SDLContext sdl_context{mediator}; try { // Correct way to create a unique pointer for Text - std::unique_ptr label - = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol",Text::Data{}); + std::unique_ptr label = std::make_unique( + 1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); Asset asset = font_facade.get_font_asset(label->font_family); std::cout << "path: " << asset.get_path() << std::endl; std::unique_ptr font = std::make_unique(asset, mediator); // Get the TTF_Font from the Font object - TTF_Font* ttf_font = font->get_font(); + TTF_Font * ttf_font = font->get_font(); //example if the asset is not correct for font //std::unique_ptr fontThrow = std::make_unique(Asset("../help.txt"), mediator); // Check if the font is loaded properly -- cgit v1.2.3 From 1c201e9bd0c1017386c8eb6bd97cd638d84ff1ab Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sat, 14 Dec 2024 14:47:04 +0100 Subject: remove launch --- .vscode/launch.json | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 07bd65f..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "Debug test_main", - "type": "cppdbg", - "request": "launch", - "program": "${workspaceFolder}/src/build/test_main", // Path to your executable - "args": [], // Optional: add any arguments here if needed - "stopAtEntry": false, // Set to true if you want to break at the entry point - "cwd": "${workspaceFolder}", // Working directory for the program - "environment": [], - "externalConsole": false, // Set to true to use an external console - "MIMode": "gdb", // Use "lldb" if you're using LLDB instead of GDB - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ], - "miDebuggerPath": "/usr/bin/gdb", // Path to gdb, adjust based on your system - "preLaunchTask": "", // Optional: specify a pre-launch task like building your project - "sourceFileMap": {}, - "logging": { "moduleLoad": false, "engineLogging": false } - } - ] -} -- cgit v1.2.3 From 22476a51e00089f8c7b810937250ff4d7a14329e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 19:59:32 +0100 Subject: updated test and game.cpp --- src/example/game.cpp | 2 +- src/test/ParticleTest.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/example/game.cpp b/src/example/game.cpp index 7c9a227..ed6afac 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -269,7 +269,7 @@ public: }); auto & test = particle.add_component(particle_image, ParticleEmitter::Data{ - .position = {0, 0}, + .offset = {0, 0}, .max_particles = 256, .emission_rate = 50, .min_speed = 10, diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 8ffb140..9263e00 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -44,7 +44,7 @@ public: game_object.add_component(test_sprite, ParticleEmitter::Data{ - .position = {0, 0}, + .offset = {0, 0}, .max_particles = 100, .emission_rate = 0, .min_speed = 0, @@ -70,7 +70,7 @@ public: std::vector> rigidbodies = mgr.get_components_by_id(0); ParticleEmitter & emitter = rigidbodies.front().get(); - emitter.data.position = {0, 0}; + emitter.data.offset = {0, 0}; emitter.data.emission_rate = 0; emitter.data.min_speed = 0; emitter.data.max_speed = 0; -- cgit v1.2.3 From ff2f5120b53792fe5e8cfd45a4335680baf487cf Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 20:00:40 +0100 Subject: updated offset --- src/crepe/system/ParticleSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 31a4e9e..e61f0ce 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -48,7 +48,7 @@ void ParticleSystem::update() { void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & transform) { constexpr float DEG_TO_RAD = M_PI / 180.0; - vec2 initial_position = emitter.data.position + transform.position; + vec2 initial_position = emitter.data.offset + transform.position; float random_angle = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); @@ -69,7 +69,7 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & } void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { - vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; + vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.offset; float half_width = emitter.data.boundary.width / 2.0; float half_height = emitter.data.boundary.height / 2.0; -- cgit v1.2.3 From 723063e8cb49e3452a85c4fc9ce82380b87bc73e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 20:02:25 +0100 Subject: updated test --- src/test/Profiling.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index 35f52dc..1585061 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -219,7 +219,7 @@ TEST_F(DISABLED_ProfilingTest, Profiling_3) { .order_in_layer = 1, .size = {.y = 500}, }); - auto & test = gameobject.add_component(ParticleEmitter::Data{ + auto & test = gameobject.add_component(test_sprite,ParticleEmitter::Data{ .max_particles = 10, .emission_rate = 100, .end_lifespan = 100000, @@ -229,8 +229,9 @@ TEST_F(DISABLED_ProfilingTest, Profiling_3) { .offset = vec2{0, 0}, .reset_on_exit = false, }, - .sprite = test_sprite, - }); + + } + ); } render_sys.update(); this->game_object_count++; -- cgit v1.2.3 From 20d19e3c714d3e8ca3e35c170c07c563ecc719bb Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 21:30:32 +0100 Subject: fixed bug with lower than one particle per second --- src/crepe/system/ParticleSystem.cpp | 2 +- src/example/game.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index e61f0ce..35a1d41 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -27,7 +27,7 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - emitter.spawn_accumulator = emitter.data.emission_rate * dt; + emitter.spawn_accumulator += emitter.data.emission_rate * dt; while (emitter.spawn_accumulator >= 1.0) { this->emit_particle(emitter, transform); emitter.spawn_accumulator -= 1.0; diff --git a/src/example/game.cpp b/src/example/game.cpp index ed6afac..db0d04b 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -271,7 +271,7 @@ public: = particle.add_component(particle_image, ParticleEmitter::Data{ .offset = {0, 0}, .max_particles = 256, - .emission_rate = 50, + .emission_rate = 1, .min_speed = 10, .max_speed = 20, .min_angle = -20, -- cgit v1.2.3 From 7f7f1237532b4318361e69349cc38f34fe981803 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 18:28:56 +0100 Subject: headers to c file change --- src/crepe/facade/SDLFontContext.cpp | 2 ++ src/crepe/facade/SDLFontContext.h | 4 +--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index e0b9a89..45d70cb 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "SDLFontContext.h" diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index cb3ca9d..d15e1a3 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -1,7 +1,6 @@ #pragma once -#include -#include + #include #include "../api/Asset.h" @@ -25,7 +24,6 @@ public: */ Asset get_font_asset(const std::string font_family); -private: }; } // namespace crepe -- cgit v1.2.3 From 55f4aaf179cf723c5b703b0c4d848bf059ae190a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 19:08:22 +0100 Subject: font family implemented in the text component --- src/crepe/api/Text.cpp | 8 ++++-- src/crepe/api/Text.h | 17 +++++++++--- src/crepe/facade/CMakeLists.txt | 4 +-- src/crepe/facade/FontFacade.cpp | 50 +++++++++++++++++++++++++++++++++ src/crepe/facade/FontFacade.h | 28 +++++++++++++++++++ src/crepe/facade/SDLFontContext.cpp | 55 ------------------------------------- src/crepe/facade/SDLFontContext.h | 29 ------------------- src/example/loadfont.cpp | 21 ++++---------- 8 files changed, 105 insertions(+), 107 deletions(-) create mode 100644 src/crepe/facade/FontFacade.cpp create mode 100644 src/crepe/facade/FontFacade.h delete mode 100644 src/crepe/facade/SDLFontContext.cpp delete mode 100644 src/crepe/facade/SDLFontContext.h diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 568af7f..e6db140 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,10 +1,14 @@ +#include "../facade/FontFacade.h" + #include "Text.h" + using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, const std::string & font_family, const Data & data) : UIObject(id, dimensions, offset), text(text), - data(data), - font_family(font_family) {} + data(data), + font(FontFacade::get_font_asset(font_family)) { +} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 64b2008..13b4375 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -5,6 +5,7 @@ #include "../Component.h" #include "Color.h" +#include "Asset.h" #include "UIObject.h" namespace crepe { @@ -39,13 +40,21 @@ public: }; public: + /** + * + * \param dimensions Width and height of the UIObject. + * \param offset Offset of the UIObject relative to its transform + * \param text The text to be displayed. + * \param font_family The font style name to be displayed. + * \param data Data struct containing extra text parameters. + */ Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string &, const std::string & font_family, const Data & data); - //! font family name such as (Arial,Helvetica,Inter) - std::string font_family = ""; + const std::string & text, const std::string & font_family, const Data & data); //! Label text. std::string text = ""; - // Data instance for data not gotten from constructor + //! Font asset variable + Asset font; + //! Data instance Data data; }; diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt index e61b680..243ae46 100644 --- a/src/crepe/facade/CMakeLists.txt +++ b/src/crepe/facade/CMakeLists.txt @@ -4,7 +4,7 @@ target_sources(crepe PUBLIC SoundContext.cpp SDLContext.cpp DB.cpp - SDLFontContext.cpp + FontFacade.cpp Font.cpp ) @@ -14,7 +14,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES SoundContext.h SDLContext.h DB.h - SDLFontContext.h + FontFacade.h Font.h ) diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp new file mode 100644 index 0000000..12e10ed --- /dev/null +++ b/src/crepe/facade/FontFacade.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +#include "FontFacade.h" + +using namespace crepe; +using namespace std; + +Asset FontFacade::get_font_asset(const string font_family) { + if (!FcInit()) { + throw runtime_error("Failed to initialize Fontconfig."); + } + // Create a pattern to search for the font family + FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); + if (pattern == NULL) { + throw runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig * config = FcConfigGetCurrent(); + if (config == NULL) { + FcPatternDestroy(pattern); + throw runtime_error("Failed to get current Fontconfig configuration."); + } + + // Match the font pattern + FcResult result; + FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); + FcPatternDestroy(pattern); + + if (matched_pattern == NULL) { + FcPatternDestroy(matched_pattern); + throw runtime_error("No matching font found."); + } + + // Extract the file path + FcChar8 * file_path = nullptr; + if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch + || file_path == NULL) { + FcPatternDestroy(matched_pattern); + throw runtime_error("Failed to get font file path."); + } + + // Convert the file path to a string + string font_file_path(reinterpret_cast(file_path)); + FcPatternDestroy(matched_pattern); + FcFini(); + 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..1b835d4 --- /dev/null +++ b/src/crepe/facade/FontFacade.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "../api/Asset.h" + +namespace crepe { + +/** + * + * \brief Font facade class for converting font family names to absolute file paths + * + */ +class FontFacade { +public: + /** + * + * \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 is static so the member function can be used without create a FontFacade object. This way it can be used in a constructor as FontFacade::get_font_asset(). + * \param font_family Name of the font family name. + * \return Asset with filepath to the font. + */ + static Asset get_font_asset(const std::string font_family); +}; + +} // namespace crepe diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp deleted file mode 100644 index 45d70cb..0000000 --- a/src/crepe/facade/SDLFontContext.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include - -#include "SDLFontContext.h" - -using namespace crepe; -using namespace std; - -SDLFontContext::SDLFontContext() { - if (!FcInit()) { - throw runtime_error("Failed to initialize Fontconfig."); - } -} - -SDLFontContext::~SDLFontContext() { FcFini(); } - -Asset SDLFontContext::get_font_asset(const string font_family) { - - // Create a pattern to search for the font family - FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (pattern == NULL) { - throw runtime_error("Failed to create font pattern."); - } - - // Default configuration - FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { - FcPatternDestroy(pattern); - throw runtime_error("Failed to get current Fontconfig configuration."); - } - - // Match the font pattern - FcResult result; - FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); - FcPatternDestroy(pattern); - - if (matched_pattern == NULL) { - FcPatternDestroy(matched_pattern); - throw runtime_error("No matching font found."); - } - - // Extract the file path - FcChar8 * file_path = nullptr; - if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch - || file_path == NULL) { - FcPatternDestroy(matched_pattern); - throw runtime_error("Failed to get font file path."); - } - - // Convert the file path to a string - string font_file_path(reinterpret_cast(file_path)); - FcPatternDestroy(matched_pattern); - return Asset(font_file_path); -} diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h deleted file mode 100644 index d15e1a3..0000000 --- a/src/crepe/facade/SDLFontContext.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - - -#include - -#include "../api/Asset.h" - -namespace crepe { -class SDLFontContext { -public: - SDLFontContext(); - ~SDLFontContext(); - SDLFontContext(const SDLFontContext &) = delete; - SDLFontContext(SDLFontContext &&) = delete; - SDLFontContext & operator=(const SDLFontContext &) = delete; - SDLFontContext & operator=(SDLFontContext &&) = 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. - * - * \param font_family Name of the font family name. - */ - Asset get_font_asset(const std::string font_family); - -}; - -} // namespace crepe diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index dae64bc..0ea6d86 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -9,26 +8,18 @@ using namespace crepe; int main() { - SDLFontContext font_facade; + // SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( 1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); - Asset asset = font_facade.get_font_asset(label->font_family); - std::cout << "path: " << asset.get_path() << std::endl; - std::unique_ptr font = std::make_unique(asset, mediator); - // Get the TTF_Font from the Font object - TTF_Font * ttf_font = font->get_font(); - //example if the asset is not correct for font - //std::unique_ptr fontThrow = std::make_unique(Asset("../help.txt"), mediator); - // Check if the font is loaded properly - if (ttf_font != nullptr) { - std::cout << "Font successfully loaded!" << std::endl; - } else { - std::cout << "Failed to load font." << std::endl; - } + std::cout << "Path: " << label->font.get_path() << std::endl; + + std::unique_ptr label2 = std::make_unique( + 1, vec2(100, 100), vec2(0, 0), "test test", "fsaafdafsdafsdafsdasfdds", Text::Data{}); + std::cout << "Path: " << label2->font.get_path() << std::endl; } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From 56342ffab9daade7802b015ac83b9c47a8b9b18b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 19:36:40 +0100 Subject: make format --- src/crepe/api/Text.cpp | 6 ++---- src/crepe/api/Text.h | 2 +- src/crepe/facade/FontFacade.cpp | 2 +- src/crepe/facade/FontFacade.h | 2 +- src/example/loadfont.cpp | 5 +++-- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index e6db140..5b2befe 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -2,13 +2,11 @@ #include "Text.h" - using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const std::string & text, const std::string & font_family, const Data & data) : UIObject(id, dimensions, offset), text(text), - data(data), - font(FontFacade::get_font_asset(font_family)) { -} + data(data), + font(FontFacade::get_font_asset(font_family)) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 13b4375..fbb1ad6 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -4,8 +4,8 @@ #include "../Component.h" -#include "Color.h" #include "Asset.h" +#include "Color.h" #include "UIObject.h" namespace crepe { diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 12e10ed..708b2ff 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include "FontFacade.h" diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index 1b835d4..0e6b7da 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -22,7 +22,7 @@ public: * \param font_family Name of the font family name. * \return Asset with filepath to the font. */ - static Asset get_font_asset(const std::string font_family); + static Asset get_font_asset(const std::string font_family); }; } // namespace crepe diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 0ea6d86..508197a 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -17,8 +17,9 @@ int main() { 1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); std::cout << "Path: " << label->font.get_path() << std::endl; - std::unique_ptr label2 = std::make_unique( - 1, vec2(100, 100), vec2(0, 0), "test test", "fsaafdafsdafsdafsdasfdds", Text::Data{}); + std::unique_ptr label2 + = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", + "fsaafdafsdafsdafsdasfdds", Text::Data{}); std::cout << "Path: " << label2->font.get_path() << std::endl; } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; -- cgit v1.2.3 From e84717feb5a9435b96b6d10e77e699cea5d8bff9 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 20:46:00 +0100 Subject: retrieving font with resource manager added --- src/example/loadfont.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 508197a..35afdf7 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,7 +1,9 @@ #include +#include #include #include #include +#include #include #include #include @@ -11,6 +13,7 @@ int main() { // SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; + ResourceManager resource_manager{mediator}; try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( @@ -21,6 +24,14 @@ int main() { = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "fsaafdafsdafsdafsdasfdds", Text::Data{}); std::cout << "Path: " << label2->font.get_path() << std::endl; + ResourceManager & resource_mgr = mediator.resource_manager; + const Font & res = resource_manager.get(label->font); + TTF_Font * test_font = res.get_font(); + if(test_font == NULL){ + std::cout << "error with font" << std::endl; + }else{ + std::cout << "correct font retrieved" << std::endl; + } } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From 9f3af4431853288c6e1ec0ff5b8f5bb0e6379b66 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 20:58:02 +0100 Subject: added const --- src/crepe/api/Text.h | 2 +- src/example/loadfont.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index fbb1ad6..ec0bf74 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -53,7 +53,7 @@ public: //! Label text. std::string text = ""; //! Font asset variable - Asset font; + const Asset font; //! Data instance Data data; }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 35afdf7..a77e457 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -13,6 +13,7 @@ int main() { // SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; + ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; try { // Correct way to create a unique pointer for Text -- cgit v1.2.3 From 58c57578cae755422c35acfbbdf9ad399b4fd826 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 21:49:15 +0100 Subject: very illegal commit do not push --- src/crepe/api/Script.h | 3 ++- src/crepe/facade/FontFacade.cpp | 4 ++-- src/example/CMakeLists.txt | 1 + src/example/FontExample.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ src/example/loadfont.cpp | 2 +- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/example/FontExample.cpp diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 668e5d1..ee45b8d 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -156,6 +156,7 @@ private: void subscribe_internal(const EventHandler & callback, event_channel_t channel); protected: + OptionalRef mediator; // NOTE: This must be the only constructor on Script, see "Late references" below Script() = default; //! Only \c BehaviorScript instantiates Script @@ -190,7 +191,7 @@ private: //! Reference to parent component OptionalRef active; //! Mediator reference - OptionalRef mediator; + //! \} private: diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 708b2ff..a63b022 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -16,7 +16,7 @@ Asset FontFacade::get_font_asset(const string font_family) { if (pattern == NULL) { throw runtime_error("Failed to create font pattern."); } - + // Default configuration FcConfig * config = FcConfigGetCurrent(); if (config == NULL) { @@ -43,7 +43,7 @@ Asset FontFacade::get_font_asset(const string font_family) { } // Convert the file path to a string - string font_file_path(reinterpret_cast(file_path)); + string font_file_path = reinterpret_cast(file_path); FcPatternDestroy(matched_pattern); FcFini(); return Asset(font_file_path); diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 0e2d5e2..f62414e 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -20,4 +20,5 @@ add_example(rendering_particle) add_example(game) add_example(button) add_example(loadfont) +add_example(FontExample) add_example(AITest) diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp new file mode 100644 index 0000000..620cf8e --- /dev/null +++ b/src/example/FontExample.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace crepe; +using namespace std; +using namespace std::chrono; +class TestScript : public Script{ + public: + steady_clock::time_point start_time; + virtual void init() override{ + start_time = steady_clock::now(); + } + virtual void update() override{ + auto now = steady_clock::now(); + auto elapsed = duration_cast(now - start_time).count(); + + if (elapsed >= 1) { + Mediator& med = mediator; + EventManager& event_mgr = med.event_manager; + event_mgr.trigger_event(); + } + } +}; +class TestScene : public Scene{ + public: + void load_scene() override{ + GameObject text_object = this->new_object("test","test",vec2{0,0},0,1); + text_object.add_component(vec2(100, 100), vec2(0, 0), "test test", + "OpenSymbol", Text::Data{}); + text_object.add_component(); + + } + std::string get_name(){ return "hey";} +}; +int main() { + + + + return 0; +} diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index a77e457..f863053 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -13,7 +13,7 @@ int main() { // SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; - ComponentManager component_manager{mediator}; + // ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; try { // Correct way to create a unique pointer for Text -- cgit v1.2.3 From d33dbdff59693377d06d83225b0fe78c3168c464 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 22:56:42 +0100 Subject: segmentation fault fixed --- src/crepe/facade/Font.cpp | 7 ++++--- src/example/FontExample.cpp | 17 +++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 1ee3de1..333e500 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -10,10 +10,11 @@ Font::Font(const Asset & src, Mediator & mediator) font(nullptr, TTF_CloseFont) { Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); - TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); - if (font == NULL) + 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 = {font, [](TTF_Font * font) { TTF_CloseFont(font); }}; + } + this->font = {loaded_font, [](TTF_Font * font) { TTF_CloseFont(font); }}; } TTF_Font * Font::get_font() const { return this->font.get(); } diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index 620cf8e..c2f21c4 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -25,7 +27,7 @@ class TestScript : public Script{ auto now = steady_clock::now(); auto elapsed = duration_cast(now - start_time).count(); - if (elapsed >= 1) { + if (elapsed >= 5) { Mediator& med = mediator; EventManager& event_mgr = med.event_manager; event_mgr.trigger_event(); @@ -37,15 +39,18 @@ class TestScene : public Scene{ void load_scene() override{ GameObject text_object = this->new_object("test","test",vec2{0,0},0,1); text_object.add_component(vec2(100, 100), vec2(0, 0), "test test", - "OpenSymbol", Text::Data{}); - text_object.add_component(); + "Noto Sans", Text::Data{}); + text_object.add_component().set_script(); + text_object.add_component(ivec2{300,300},vec2{100,100},Camera::Data{ + }); } - std::string get_name(){ return "hey";} + std::string get_name() const override { return "hey"; } }; int main() { - - + LoopManager engine; + engine.add_scene(); + engine.start(); return 0; } -- cgit v1.2.3 From fce10251d772af129531896965a908dc6d881c4b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 23:00:13 +0100 Subject: small changes --- src/crepe/facade/Font.cpp | 1 + src/crepe/facade/Font.h | 4 +++- src/crepe/facade/FontFacade.cpp | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 333e500..74dfe18 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,4 +1,5 @@ #include "../api/Config.h" +#include "../api/Asset.h" #include "Font.h" diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index e93bfe9..3ff156f 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -1,13 +1,14 @@ #pragma once + #include #include #include "../Resource.h" -#include "../api/Asset.h" #include "../api/Config.h" namespace crepe { +class Asset; /** * \brief Resource for managing font creation and destruction * @@ -16,6 +17,7 @@ namespace crepe { * 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. diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index a63b022..4a991c6 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,3 @@ -#include #include #include -- cgit v1.2.3 From d63eb7302d05fbe9b4c044ece3444e8ac4e56e02 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 23:00:33 +0100 Subject: make format --- src/crepe/api/Script.h | 2 +- src/crepe/facade/Font.cpp | 2 +- src/crepe/facade/FontFacade.cpp | 2 +- src/example/FontExample.cpp | 52 +++++++++++++++++++---------------------- src/example/loadfont.cpp | 18 +++++++------- 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index ee45b8d..a24e32e 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -191,7 +191,7 @@ private: //! Reference to parent component OptionalRef active; //! Mediator reference - + //! \} private: diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 74dfe18..d419974 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,5 +1,5 @@ -#include "../api/Config.h" #include "../api/Asset.h" +#include "../api/Config.h" #include "Font.h" diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 4a991c6..aa9d00c 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -15,7 +15,7 @@ Asset FontFacade::get_font_asset(const string font_family) { if (pattern == NULL) { throw runtime_error("Failed to create font pattern."); } - + // Default configuration FcConfig * config = FcConfigGetCurrent(); if (config == NULL) { diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index c2f21c4..3f5af48 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -1,49 +1,45 @@ -#include #include +#include +#include +#include +#include +#include +#include +#include #include #include +#include #include #include -#include -#include -#include -#include -#include -#include #include #include #include -#include using namespace crepe; using namespace std; using namespace std::chrono; -class TestScript : public Script{ - public: +class TestScript : public Script { +public: steady_clock::time_point start_time; - virtual void init() override{ - start_time = steady_clock::now(); - } - virtual void update() override{ + virtual void init() override { start_time = steady_clock::now(); } + virtual void update() override { auto now = steady_clock::now(); - auto elapsed = duration_cast(now - start_time).count(); + auto elapsed = duration_cast(now - start_time).count(); - if (elapsed >= 5) { - Mediator& med = mediator; - EventManager& event_mgr = med.event_manager; + if (elapsed >= 5) { + Mediator & med = mediator; + EventManager & event_mgr = med.event_manager; event_mgr.trigger_event(); - } + } } }; -class TestScene : public Scene{ - public: - void load_scene() override{ - GameObject text_object = this->new_object("test","test",vec2{0,0},0,1); - text_object.add_component(vec2(100, 100), vec2(0, 0), "test test", - "Noto Sans", Text::Data{}); +class TestScene : public Scene { +public: + void load_scene() override { + GameObject text_object = this->new_object("test", "test", vec2{0, 0}, 0, 1); + text_object.add_component(vec2(100, 100), vec2(0, 0), "test test", "Noto Sans", + Text::Data{}); text_object.add_component().set_script(); - text_object.add_component(ivec2{300,300},vec2{100,100},Camera::Data{ - }); - + text_object.add_component(ivec2{300, 300}, vec2{100, 100}, Camera::Data{}); } std::string get_name() const override { return "hey"; } }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index f863053..ce287b4 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -25,14 +25,14 @@ int main() { = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", "fsaafdafsdafsdafsdasfdds", Text::Data{}); std::cout << "Path: " << label2->font.get_path() << std::endl; - ResourceManager & resource_mgr = mediator.resource_manager; - const Font & res = resource_manager.get(label->font); - TTF_Font * test_font = res.get_font(); - if(test_font == NULL){ - std::cout << "error with font" << std::endl; - }else{ - std::cout << "correct font retrieved" << std::endl; - } + ResourceManager & resource_mgr = mediator.resource_manager; + const Font & res = resource_manager.get(label->font); + TTF_Font * test_font = res.get_font(); + if (test_font == NULL) { + std::cout << "error with font" << std::endl; + } else { + std::cout << "correct font retrieved" << std::endl; + } } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From 3b5b5258b0f46a3492a7fd777908dfb01e15417b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 10:48:43 +0100 Subject: code not working --- src/crepe/facade/Font.cpp | 4 +++- src/crepe/facade/Font.h | 7 +++++++ src/crepe/facade/FontFacade.cpp | 6 +++--- src/crepe/facade/FontFacade.h | 2 +- src/crepe/system/RenderSystem.cpp | 10 ++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index d419974..5af943d 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,3 +1,5 @@ +#include + #include "../api/Asset.h" #include "../api/Config.h" @@ -15,7 +17,7 @@ Font::Font(const Asset & src, Mediator & mediator) if (loaded_font == NULL) { throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); } - this->font = {loaded_font, [](TTF_Font * font) { TTF_CloseFont(font); }}; + 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 index 3ff156f..16f8cb6 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -24,7 +24,14 @@ public: * \param mediator The Mediator object used for managing the SDL context or related systems. */ Font(const Asset & src, Mediator & mediator); + Font(const Font &) = delete; + Font &operator=(const Font &) = delete; + // Default move constructor and move assignment operator + Font(Font &&) noexcept = delete; + Font &operator=(Font &&) noexcept = delete; + + ~Font() = default; /** * \brief Gets the underlying TTF_Font resource. * diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index aa9d00c..d447b6d 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -6,7 +6,7 @@ using namespace crepe; using namespace std; -Asset FontFacade::get_font_asset(const string font_family) { +Asset FontFacade::get_font_asset(const string& font_family) { if (!FcInit()) { throw runtime_error("Failed to initialize Fontconfig."); } @@ -19,7 +19,7 @@ Asset FontFacade::get_font_asset(const string font_family) { // Default configuration FcConfig * config = FcConfigGetCurrent(); if (config == NULL) { - FcPatternDestroy(pattern); + // FcPatternDestroy(pattern); throw runtime_error("Failed to get current Fontconfig configuration."); } @@ -37,7 +37,7 @@ Asset FontFacade::get_font_asset(const string font_family) { FcChar8 * file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch || file_path == NULL) { - FcPatternDestroy(matched_pattern); + // FcPatternDestroy(matched_pattern); throw runtime_error("Failed to get font file path."); } diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index 0e6b7da..fc200d6 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -22,7 +22,7 @@ public: * \param font_family Name of the font family name. * \return Asset with filepath to the font. */ - static Asset get_font_asset(const std::string font_family); + static Asset get_font_asset(const std::string& font_family); }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index afd9548..5aa00b5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -8,6 +8,8 @@ #include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" +#include "../api/Text.h" +#include "../facade/Font.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" #include "../facade/Texture.h" @@ -120,8 +122,13 @@ void RenderSystem::render() { this->update_camera(); RefVector sprites = mgr.get_components_by_type(); + ResourceManager & resource_manager = this->mediator.resource_manager; RefVector sorted_sprites = this->sort(sprites); + RefVector texts = mgr.get_components_by_type(); + for(const Text& text : texts){ + const Font & res = resource_manager.get(text.font); + } for (const Sprite & sprite : sorted_sprites) { if (!sprite.active) continue; const Transform & transform @@ -132,5 +139,8 @@ void RenderSystem::render() { if (rendered_particles) continue; this->render_normal(sprite, transform); + + + } } -- cgit v1.2.3 From b99f5fc0f52fdd4ec96be844e643060503a8860b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:22:34 +0100 Subject: text now working with optional --- src/crepe/api/Asset.h | 2 +- src/crepe/api/LoopManager.h | 7 ++++--- src/crepe/api/Script.h | 5 +++-- src/crepe/api/Text.cpp | 8 +++++--- src/crepe/api/Text.h | 15 +++++++++++---- src/crepe/facade/Font.cpp | 3 +-- src/crepe/facade/Font.h | 10 +--------- src/crepe/facade/FontFacade.cpp | 11 ++++++++--- src/crepe/facade/FontFacade.h | 12 +++++++++--- src/crepe/facade/SDLContext.cpp | 5 +++++ src/crepe/facade/SDLContext.h | 15 +++++++++++++++ src/crepe/system/RenderSystem.cpp | 28 +++++++++++++++++++++++++--- src/crepe/system/RenderSystem.h | 10 ++++++++-- src/example/FontExample.cpp | 6 ++++-- src/example/loadfont.cpp | 31 ++++++++++++++++++------------- 15 files changed, 118 insertions(+), 50 deletions(-) diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index bfd0ac7..b367a92 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -37,7 +37,7 @@ public: private: //! path to asset - const std::string src; + std::string src; private: /** diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 40e6b38..1725810 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -71,7 +71,9 @@ private: private: //! Global context Mediator mediator; - + + //! SDLContext instance + SDLContext sdl_context{mediator}; //! Component manager instance ComponentManager component_manager{mediator}; //! Scene manager instance @@ -84,8 +86,7 @@ private: ResourceManager resource_manager{mediator}; //! Save manager instance SaveManager save_manager{mediator}; - //! SDLContext instance - SDLContext sdl_context{mediator}; + private: /** diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index a24e32e..4503525 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -156,7 +156,7 @@ private: void subscribe_internal(const EventHandler & callback, event_channel_t channel); protected: - OptionalRef mediator; + // NOTE: This must be the only constructor on Script, see "Late references" below Script() = default; //! Only \c BehaviorScript instantiates Script @@ -186,12 +186,13 @@ private: * * \{ */ + //! Game object ID of game object parent BehaviorScript is attached to game_object_id_t game_object_id; //! Reference to parent component OptionalRef active; //! Mediator reference - + OptionalRef mediator; //! \} private: diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 5b2befe..0624c98 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -4,9 +4,11 @@ using namespace crepe; -Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string & text, const std::string & font_family, const Data & data) +Text::Text(game_object_id_t id, const vec2 & dimensions,const vec2 & offset, const std::string & font_family, + const Data & data, const std::string & text, std::optional font) : UIObject(id, dimensions, offset), text(text), data(data), - font(FontFacade::get_font_asset(font_family)) {} + font_family(font_family), + font(font) { +} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index ec0bf74..ab72bc0 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "../Component.h" @@ -47,15 +48,21 @@ public: * \param text The text to be displayed. * \param font_family The font style name to be displayed. * \param data Data struct containing extra text parameters. + * \param font Optional font asset that can be passed or left empty. */ - Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string & text, const std::string & font_family, const Data & data); + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::string & font_family, const Data & data, + const std::string & text = "", std::optional font = std::nullopt); + //! Label text. std::string text = ""; - //! Font asset variable - const Asset font; + //! font family name + std::string font_family = ""; + //! Font asset variable if this is not set, it will use the font_family to create an asset. + std::optional font; //! Data instance Data data; + }; } // namespace crepe diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 5af943d..558641f 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -9,8 +9,7 @@ using namespace std; using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) - : Resource(src, mediator), - font(nullptr, TTF_CloseFont) { + : Resource(src, mediator){ 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); diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index 16f8cb6..b208d96 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -24,14 +24,6 @@ public: * \param mediator The Mediator object used for managing the SDL context or related systems. */ Font(const Asset & src, Mediator & mediator); - Font(const Font &) = delete; - Font &operator=(const Font &) = delete; - - // Default move constructor and move assignment operator - Font(Font &&) noexcept = delete; - Font &operator=(Font &&) noexcept = delete; - - ~Font() = default; /** * \brief Gets the underlying TTF_Font resource. * @@ -44,7 +36,7 @@ public: private: //! The SDL_ttf font object with custom deleter. - std::unique_ptr> font; + std::unique_ptr> font = nullptr; }; } // namespace crepe diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index d447b6d..0a8ba5f 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "FontFacade.h" @@ -6,10 +7,16 @@ using namespace crepe; using namespace std; -Asset FontFacade::get_font_asset(const string& font_family) { +FontFacade::FontFacade(){ if (!FcInit()) { throw runtime_error("Failed to initialize Fontconfig."); } +} +FontFacade::~FontFacade(){ + FcFini(); +} +Asset FontFacade::get_font_asset(const string& font_family) { + // Create a pattern to search for the font family FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); if (pattern == NULL) { @@ -32,7 +39,6 @@ Asset FontFacade::get_font_asset(const string& font_family) { FcPatternDestroy(matched_pattern); throw runtime_error("No matching font found."); } - // Extract the file path FcChar8 * file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch @@ -44,6 +50,5 @@ Asset FontFacade::get_font_asset(const string& font_family) { // Convert the file path to a string string font_file_path = reinterpret_cast(file_path); FcPatternDestroy(matched_pattern); - FcFini(); return Asset(font_file_path); } diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index fc200d6..2e08f3f 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -13,16 +13,22 @@ namespace crepe { */ 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 is static so the member function can be used without create a FontFacade object. This way it can be used in a constructor as FontFacade::get_font_asset(). + * 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 font. + * \return Asset with filepath to the corresponding font. */ - static Asset get_font_asset(const std::string& font_family); + 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 d1d109c..8ffaad9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -434,7 +434,12 @@ std::vector SDLContext::get_events() { } return event_list; } + 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 efdd6fe..ffa3cc0 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -17,6 +17,7 @@ #include "api/Transform.h" #include "types.h" +#include "FontFacade.h" namespace crepe { class Texture; @@ -239,6 +240,20 @@ private: * - this is defined in this class because get_events() needs this information aswell */ 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); }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 5aa00b5..18f6393 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -22,6 +23,7 @@ using namespace crepe; using namespace std; + void RenderSystem::clear_screen() { SDLContext & ctx = this->mediator.sdl_context; ctx.clear_screen(); @@ -124,9 +126,11 @@ void RenderSystem::render() { RefVector sprites = mgr.get_components_by_type(); ResourceManager & resource_manager = this->mediator.resource_manager; RefVector sorted_sprites = this->sort(sprites); - RefVector texts = mgr.get_components_by_type(); - for(const Text& text : texts){ - const Font & res = resource_manager.get(text.font); + RefVector text_components = mgr.get_components_by_type(); + for(Text& text : text_components){ + const Transform & transform + = mgr.get_components_by_id(text.game_object_id).front().get(); + this->render_text(text,transform); } for (const Sprite & sprite : sorted_sprites) { @@ -143,4 +147,22 @@ void RenderSystem::render() { } + } +void RenderSystem::render_text(Text & text, const Transform & tm) { + SDLContext & ctx = this->mediator.sdl_context; + + // Check if font is available in text + if (!text.font.has_value()) { + } + + ResourceManager & resource_manager = this->mediator.resource_manager; + + if (text.font.has_value()) { + const Asset& font_asset = text.font.value(); + const Font & res = resource_manager.get(font_asset); + } +} + + + diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index fc7b46e..56a0553 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -10,7 +10,7 @@ namespace crepe { class Camera; class Sprite; class Transform; - +class Text; /** * \brief Manages rendering operations for all game objects. * @@ -50,7 +50,13 @@ private: * \return true if particles have been rendered */ bool render_particle(const Sprite & sprite, const double & scale); - + /** + * \brief Renders all Text components + * + * \param text The text component to be rendered. + * \param tm the Transform component that holds the position,rotation and scale + */ + void render_text(Text & text, const Transform & tm); /** * \brief renders a sprite with a Transform component on the screen * diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index 3f5af48..7b2dadb 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -36,14 +37,15 @@ class TestScene : public Scene { public: void load_scene() override { GameObject text_object = this->new_object("test", "test", vec2{0, 0}, 0, 1); - text_object.add_component(vec2(100, 100), vec2(0, 0), "test test", "Noto Sans", - Text::Data{}); + text_object.add_component(vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}); text_object.add_component().set_script(); text_object.add_component(ivec2{300, 300}, vec2{100, 100}, Camera::Data{}); } std::string get_name() const override { return "hey"; } }; int main() { + // Config& config = Config::get_instance(); + // config.log.level = Log::Level::TRACE; LoopManager engine; engine.add_scene(); engine.start(); diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index ce287b4..9d59afc 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -2,10 +2,12 @@ #include #include #include +#include #include #include #include #include +#include #include using namespace crepe; int main() { @@ -18,21 +20,24 @@ int main() { try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( - 1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); - std::cout << "Path: " << label->font.get_path() << std::endl; + 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{},"test text", Asset("")); + // std::cout << "Path: " << label->font.get_path() << std::endl; std::unique_ptr label2 - = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", - "fsaafdafsdafsdafsdasfdds", Text::Data{}); - std::cout << "Path: " << label2->font.get_path() << std::endl; - ResourceManager & resource_mgr = mediator.resource_manager; - const Font & res = resource_manager.get(label->font); - TTF_Font * test_font = res.get_font(); - if (test_font == NULL) { - std::cout << "error with font" << std::endl; - } else { - std::cout << "correct font retrieved" << std::endl; - } + = std::make_unique(1, vec2(100, 100), vec2(0, 0),"fsaafdafsdafsdafsdasfdds", Text::Data{}); + Asset asset = Asset("test test"); + label->font = std::make_optional(asset); + std::cout << label->font.value().get_path() << std::endl; + // label2->font = std::make_optional(asset); + // std::cout << "Path: " << label2->font.get_path() << std::endl; + // ResourceManager & resource_mgr = mediator.resource_manager; + // const Font & res = resource_manager.get(label->font); + // TTF_Font * test_font = res.get_font(); + // if (test_font == NULL) { + // std::cout << "error with font" << std::endl; + // } else { + // std::cout << "correct font retrieved" << std::endl; + // } } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; } -- cgit v1.2.3 From e3c7542b56d80e0e9c8baf85f226c9d4da01845c Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:27:59 +0100 Subject: testing something --- src/example/loadfont.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 9d59afc..36d00dd 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -26,7 +26,7 @@ int main() { std::unique_ptr label2 = std::make_unique(1, vec2(100, 100), vec2(0, 0),"fsaafdafsdafsdafsdasfdds", Text::Data{}); Asset asset = Asset("test test"); - label->font = std::make_optional(asset); + label->font = asset; std::cout << label->font.value().get_path() << std::endl; // label2->font = std::make_optional(asset); // std::cout << "Path: " << label2->font.get_path() << std::endl; -- cgit v1.2.3 From 707db8c94b6bb3921105f40658aab13511d8df07 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:29:02 +0100 Subject: removed iostream --- src/crepe/facade/FontFacade.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 0a8ba5f..08ff31c 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,5 +1,4 @@ #include -#include #include #include "FontFacade.h" -- cgit v1.2.3 From 34a773eb0f01379419900a3fe26527702146b890 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:33:53 +0100 Subject: last changes --- src/crepe/facade/SDLContext.h | 1 + src/crepe/system/RenderSystem.cpp | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index ffa3cc0..e348ff6 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -18,6 +18,7 @@ #include "types.h" #include "FontFacade.h" + namespace crepe { class Texture; diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 18f6393..5fc98a9 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -152,16 +152,15 @@ void RenderSystem::render() { void RenderSystem::render_text(Text & text, const Transform & tm) { SDLContext & ctx = this->mediator.sdl_context; - // Check if font is available in text if (!text.font.has_value()) { + text.font = ctx.get_font_from_name(text.font_family); } ResourceManager & resource_manager = this->mediator.resource_manager; - if (text.font.has_value()) { + if (!text.font.has_value()) {return;} const Asset& font_asset = text.font.value(); const Font & res = resource_manager.get(font_asset); - } } -- cgit v1.2.3 From 69ca7fdd738fd4ed98aefc07bab5a43486a55619 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:34:19 +0100 Subject: final changes --- src/crepe/api/LoopManager.h | 3 +-- src/crepe/api/Script.h | 1 - src/crepe/api/Text.cpp | 8 ++++---- src/crepe/api/Text.h | 11 +++++------ src/crepe/facade/Font.cpp | 3 +-- src/crepe/facade/FontFacade.cpp | 10 ++++------ src/crepe/facade/FontFacade.h | 10 +++++----- src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/facade/SDLContext.h | 6 ++++-- src/crepe/system/RenderSystem.cpp | 35 ++++++++++++++--------------------- src/example/FontExample.cpp | 5 +++-- src/example/loadfont.cpp | 10 +++++----- 12 files changed, 47 insertions(+), 57 deletions(-) diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 1725810..1d23cbf 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -71,7 +71,7 @@ private: private: //! Global context Mediator mediator; - + //! SDLContext instance SDLContext sdl_context{mediator}; //! Component manager instance @@ -86,7 +86,6 @@ private: ResourceManager resource_manager{mediator}; //! Save manager instance SaveManager save_manager{mediator}; - private: /** diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 4503525..8bca38a 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -156,7 +156,6 @@ private: void subscribe_internal(const EventHandler & callback, event_channel_t channel); protected: - // NOTE: This must be the only constructor on Script, see "Late references" below Script() = default; //! Only \c BehaviorScript instantiates Script diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 0624c98..58dc6c6 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -4,11 +4,11 @@ using namespace crepe; -Text::Text(game_object_id_t id, const vec2 & dimensions,const vec2 & offset, const std::string & font_family, - const Data & data, const std::string & text, std::optional font) +Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::string & font_family, const Data & data, const std::string & text, + std::optional font) : UIObject(id, dimensions, offset), text(text), data(data), font_family(font_family), - font(font) { -} + font(font) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index ab72bc0..92cca18 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -1,7 +1,7 @@ #pragma once +#include #include -#include #include "../Component.h" @@ -50,10 +50,10 @@ public: * \param data Data struct containing extra text parameters. * \param font Optional font asset that can be passed or left empty. */ - Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string & font_family, const Data & data, - const std::string & text = "", std::optional font = std::nullopt); - + Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, + const std::string & font_family, const Data & data, const std::string & text = "", + std::optional font = std::nullopt); + //! Label text. std::string text = ""; //! font family name @@ -62,7 +62,6 @@ public: std::optional font; //! Data instance Data data; - }; } // namespace crepe diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 558641f..f202c05 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -8,8 +8,7 @@ using namespace std; using namespace crepe; -Font::Font(const Asset & src, Mediator & mediator) - : Resource(src, mediator){ +Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { 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); diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 08ff31c..7edfeb8 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -6,16 +6,14 @@ using namespace crepe; using namespace std; -FontFacade::FontFacade(){ +FontFacade::FontFacade() { if (!FcInit()) { throw runtime_error("Failed to initialize Fontconfig."); } } -FontFacade::~FontFacade(){ - FcFini(); -} -Asset FontFacade::get_font_asset(const string& font_family) { - +FontFacade::~FontFacade() { FcFini(); } +Asset FontFacade::get_font_asset(const string & font_family) { + // Create a pattern to search for the font family FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); if (pattern == NULL) { diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index 2e08f3f..9761070 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -15,10 +15,10 @@ 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; + 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. @@ -28,7 +28,7 @@ public: * \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); + 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 8ffaad9..c88687e 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -440,6 +440,6 @@ void SDLContext::set_color_texture(const Texture & texture, const Color & color) SDL_SetTextureAlphaMod(texture.get_img(), color.a); } -Asset SDLContext::get_font_from_name(const std::string& font_family){ +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 e348ff6..6f6eddb 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -241,9 +241,11 @@ private: * - this is defined in this class because get_events() needs this information aswell */ CameraAuxiliaryData cam_aux_data; -private: + +private: //! instance of the font_facade FontFacade font_facade{}; + public: /** * \brief Function to Get asset from font_family @@ -254,7 +256,7 @@ public: * * \return asset with the font style absolute path */ - Asset get_font_from_name(const std::string& font_family); + Asset get_font_from_name(const std::string & font_family); }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 5fc98a9..a03c636 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -10,8 +10,8 @@ #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Text.h" -#include "../facade/Font.h" #include "../api/Transform.h" +#include "../facade/Font.h" #include "../facade/SDLContext.h" #include "../facade/Texture.h" #include "../manager/ComponentManager.h" @@ -23,7 +23,6 @@ using namespace crepe; using namespace std; - void RenderSystem::clear_screen() { SDLContext & ctx = this->mediator.sdl_context; ctx.clear_screen(); @@ -126,12 +125,11 @@ void RenderSystem::render() { RefVector sprites = mgr.get_components_by_type(); ResourceManager & resource_manager = this->mediator.resource_manager; RefVector sorted_sprites = this->sort(sprites); - RefVector text_components = mgr.get_components_by_type(); - for(Text& text : text_components){ + RefVector text_components = mgr.get_components_by_type(); + for (Text & text : text_components) { const Transform & transform = mgr.get_components_by_id(text.game_object_id).front().get(); - this->render_text(text,transform); - + this->render_text(text, transform); } for (const Sprite & sprite : sorted_sprites) { if (!sprite.active) continue; @@ -143,25 +141,20 @@ void RenderSystem::render() { if (rendered_particles) continue; this->render_normal(sprite, transform); - - - } - } void RenderSystem::render_text(Text & text, const Transform & tm) { - SDLContext & ctx = this->mediator.sdl_context; + SDLContext & ctx = this->mediator.sdl_context; - if (!text.font.has_value()) { - text.font = ctx.get_font_from_name(text.font_family); - } + if (!text.font.has_value()) { + text.font = ctx.get_font_from_name(text.font_family); + } - ResourceManager & resource_manager = this->mediator.resource_manager; + ResourceManager & resource_manager = this->mediator.resource_manager; - if (!text.font.has_value()) {return;} - const Asset& font_asset = text.font.value(); - const Font & res = resource_manager.get(font_asset); + if (!text.font.has_value()) { + return; + } + const Asset & font_asset = text.font.value(); + const Font & res = resource_manager.get(font_asset); } - - - diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index 7b2dadb..6a334b1 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -11,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -37,7 +37,8 @@ class TestScene : public Scene { public: void load_scene() override { GameObject text_object = this->new_object("test", "test", vec2{0, 0}, 0, 1); - text_object.add_component(vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}); + text_object.add_component(vec2(100, 100), vec2(0, 0), "OpenSymbol", + Text::Data{}); text_object.add_component().set_script(); text_object.add_component(ivec2{300, 300}, vec2{100, 100}, Camera::Data{}); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 36d00dd..ed67ffa 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,14 +1,14 @@ #include +#include #include #include #include -#include #include #include #include #include -#include #include +#include using namespace crepe; int main() { @@ -20,11 +20,11 @@ int main() { try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( - 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{},"test text", Asset("")); + 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text", Asset("")); // std::cout << "Path: " << label->font.get_path() << std::endl; - std::unique_ptr label2 - = std::make_unique(1, vec2(100, 100), vec2(0, 0),"fsaafdafsdafsdafsdasfdds", Text::Data{}); + std::unique_ptr label2 = std::make_unique( + 1, vec2(100, 100), vec2(0, 0), "fsaafdafsdafsdafsdasfdds", Text::Data{}); Asset asset = Asset("test test"); label->font = asset; std::cout << label->font.value().get_path() << std::endl; -- cgit v1.2.3 From 9957da232e7c96f7f88a7ab0edfa4aaf560d1c00 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 17 Dec 2024 14:06:28 +0100 Subject: `make format` --- src/test/Profiling.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index 1585061..16736b8 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -219,19 +219,19 @@ TEST_F(DISABLED_ProfilingTest, Profiling_3) { .order_in_layer = 1, .size = {.y = 500}, }); - auto & test = gameobject.add_component(test_sprite,ParticleEmitter::Data{ - .max_particles = 10, - .emission_rate = 100, - .end_lifespan = 100000, - .boundary{ - .width = 1000, - .height = 1000, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - - } - ); + auto & test = gameobject.add_component( + test_sprite, ParticleEmitter::Data{ + .max_particles = 10, + .emission_rate = 100, + .end_lifespan = 100000, + .boundary{ + .width = 1000, + .height = 1000, + .offset = vec2{0, 0}, + .reset_on_exit = false, + }, + + }); } render_sys.update(); this->game_object_count++; -- cgit v1.2.3 From c96ef5f62a1369d66e8eba9bf8ed192e3cf8e716 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 17 Dec 2024 14:19:09 +0100 Subject: add noexcept qualifier to Script::get_key_state --- src/crepe/api/Script.cpp | 2 +- src/crepe/api/Script.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp index d4eaae9..583c04f 100644 --- a/src/crepe/api/Script.cpp +++ b/src/crepe/api/Script.cpp @@ -31,7 +31,7 @@ const keyboard_state_t & Script::get_keyboard_state() const { return sdl_context.get_keyboard_state(); } -bool Script::get_key_state(Keycode key) const { +bool Script::get_key_state(Keycode key) const noexcept { try { return this->get_keyboard_state().at(key); } catch (...) { diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index b5d3dd2..65306cd 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -150,7 +150,7 @@ protected: * \return Keycode state (true if pressed, false if not pressed). * */ - bool get_key_state(Keycode key) const; + bool get_key_state(Keycode key) const noexcept; //! \} private: -- cgit v1.2.3 From 0ce74416985061dee60253221a77e6b06b4ed12b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 14:38:31 +0100 Subject: readded const and used emplace instead --- src/crepe/api/Asset.h | 2 +- src/crepe/system/RenderSystem.cpp | 2 +- src/example/loadfont.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index b367a92..bfd0ac7 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -37,7 +37,7 @@ public: private: //! path to asset - std::string src; + const std::string src; private: /** diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index a03c636..5b2528a 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -147,7 +147,7 @@ void RenderSystem::render_text(Text & text, const Transform & tm) { SDLContext & ctx = this->mediator.sdl_context; if (!text.font.has_value()) { - text.font = ctx.get_font_from_name(text.font_family); + text.font.emplace(ctx.get_font_from_name(text.font_family)); } ResourceManager & resource_manager = this->mediator.resource_manager; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index ed67ffa..6fb9760 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -26,7 +26,7 @@ int main() { std::unique_ptr label2 = std::make_unique( 1, vec2(100, 100), vec2(0, 0), "fsaafdafsdafsdafsdasfdds", Text::Data{}); Asset asset = Asset("test test"); - label->font = asset; + label->font.emplace(asset); std::cout << label->font.value().get_path() << std::endl; // label2->font = std::make_optional(asset); // std::cout << "Path: " << label2->font.get_path() << std::endl; -- cgit v1.2.3 From 9232a98b72eee7af4f7f2153c1b2ccedbfa4cc65 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 17 Dec 2024 14:42:29 +0100 Subject: fix code standard --- src/crepe/facade/SDLContext.cpp | 5 ++--- src/crepe/facade/SDLContext.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 7ccc243..f331517 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -75,9 +75,8 @@ SDLContext::~SDLContext() { } 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() { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 8f4760e..b9c7fbd 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -245,7 +245,7 @@ private: //! 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 LOOKUP_TABLE + const std::unordered_map lookup_table = {{SDL_SCANCODE_SPACE, Keycode::SPACE}, {SDL_SCANCODE_APOSTROPHE, Keycode::APOSTROPHE}, {SDL_SCANCODE_COMMA, Keycode::COMMA}, -- cgit v1.2.3 From 4080a2eec207b35b36f7d0ceae7c2afcfcdfd977 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 14:44:48 +0100 Subject: small changes --- src/crepe/api/Script.h | 1 - src/crepe/facade/Font.cpp | 2 +- src/example/loadfont.cpp | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 8bca38a..668e5d1 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -185,7 +185,6 @@ private: * * \{ */ - //! Game object ID of game object parent BehaviorScript is attached to game_object_id_t game_object_id; //! Reference to parent component diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index f202c05..771002f 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -9,7 +9,7 @@ using namespace std; using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { - Config & config = Config::get_instance(); + 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) { diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 6fb9760..78b3adb 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -30,8 +30,8 @@ int main() { std::cout << label->font.value().get_path() << std::endl; // label2->font = std::make_optional(asset); // std::cout << "Path: " << label2->font.get_path() << std::endl; - // ResourceManager & resource_mgr = mediator.resource_manager; - // const Font & res = resource_manager.get(label->font); + ResourceManager & resource_mgr = mediator.resource_manager; + const Font & res = resource_manager.get(label->font.value()); // TTF_Font * test_font = res.get_font(); // if (test_font == NULL) { // std::cout << "error with font" << std::endl; -- cgit v1.2.3 From e1e7ead5df44383c78c8da65fc5916be13d111b6 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 15:14:32 +0100 Subject: feedback changes --- src/crepe/api/Text.cpp | 6 ++---- src/crepe/api/Text.h | 3 +-- src/crepe/facade/FontFacade.cpp | 48 ++++++++++++++++++++++------------------- src/example/loadfont.cpp | 7 ++++-- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 58dc6c6..6c632f3 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -5,10 +5,8 @@ using namespace crepe; Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string & font_family, const Data & data, const std::string & text, - std::optional font) + const std::string & font_family, const Data & data, const std::string & text) : UIObject(id, dimensions, offset), text(text), data(data), - font_family(font_family), - font(font) {} + font_family(font_family){} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 92cca18..c30dc80 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -51,8 +51,7 @@ public: * \param font Optional font asset that can be passed or left empty. */ Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::string & font_family, const Data & data, const std::string & text = "", - std::optional font = std::nullopt); + const std::string & font_family, const Data & data, const std::string & text = ""); //! Label text. std::string text = ""; diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 7edfeb8..5382f1a 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,51 +1,55 @@ #include #include +#include +#include +#include #include "FontFacade.h" -using namespace crepe; 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) { +Asset FontFacade::get_font_asset(const string& font_family) { // Create a pattern to search for the font family - FcPattern * pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (pattern == NULL) { + FcPattern* raw_pattern = FcNameParse(reinterpret_cast(font_family.c_str())); + if (!raw_pattern) { throw runtime_error("Failed to create font pattern."); } - // Default configuration - FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { - // FcPatternDestroy(pattern); + std::unique_ptr> pattern( + raw_pattern, + [](FcPattern* p) { FcPatternDestroy(p); } + ); + + FcConfig* config = FcConfigGetCurrent(); + if (!config) { throw runtime_error("Failed to get current Fontconfig configuration."); } - // Match the font pattern FcResult result; - FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); - FcPatternDestroy(pattern); - - if (matched_pattern == NULL) { - FcPatternDestroy(matched_pattern); + FcPattern* raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); + if (!raw_matched_pattern) { throw runtime_error("No matching font found."); } - // Extract the file path - FcChar8 * file_path = nullptr; - if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch - || file_path == NULL) { - // FcPatternDestroy(matched_pattern); + + std::unique_ptr> matched_pattern( + raw_matched_pattern, + [](FcPattern* p) { FcPatternDestroy(p); } + ); + + FcChar8* file_path = nullptr; + if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch || !file_path) { throw runtime_error("Failed to get font file path."); } - // Convert the file path to a string - string font_file_path = reinterpret_cast(file_path); - FcPatternDestroy(matched_pattern); + string font_file_path = reinterpret_cast(file_path); return Asset(font_file_path); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 78b3adb..788fac4 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -14,15 +15,17 @@ int main() { // SDLFontContext font_facade; Mediator mediator; + FontFacade font_facade{}; SDLContext sdl_context{mediator}; // ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( - 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text", Asset("")); + 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text"); // std::cout << "Path: " << label->font.get_path() << std::endl; - + Asset asset1 = font_facade.get_font_asset("OpenSymbol"); + std::cout << asset1.get_path() << std::endl; std::unique_ptr label2 = std::make_unique( 1, vec2(100, 100), vec2(0, 0), "fsaafdafsdafsdafsdasfdds", Text::Data{}); Asset asset = Asset("test test"); -- cgit v1.2.3 From 2772bb158f10fc051d35006041303f2e5418e817 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 15:15:12 +0100 Subject: make format --- src/crepe/api/Text.cpp | 2 +- src/crepe/facade/FontFacade.cpp | 34 ++++++++++++++++------------------ src/example/loadfont.cpp | 2 +- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 6c632f3..54a4370 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -9,4 +9,4 @@ Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, : UIObject(id, dimensions, offset), text(text), data(data), - font_family(font_family){} + font_family(font_family) {} diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 5382f1a..5db06d2 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,7 +1,7 @@ #include -#include -#include #include +#include +#include #include #include "FontFacade.h" @@ -17,39 +17,37 @@ FontFacade::FontFacade() { FontFacade::~FontFacade() { FcFini(); } -Asset FontFacade::get_font_asset(const string& font_family) { - // Create a pattern to search for the font family - FcPattern* raw_pattern = FcNameParse(reinterpret_cast(font_family.c_str())); +Asset FontFacade::get_font_asset(const string & font_family) { + + FcPattern * raw_pattern + = FcNameParse(reinterpret_cast(font_family.c_str())); if (!raw_pattern) { throw runtime_error("Failed to create font pattern."); } - std::unique_ptr> pattern( - raw_pattern, - [](FcPattern* p) { FcPatternDestroy(p); } - ); + std::unique_ptr> pattern( + raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); - FcConfig* config = FcConfigGetCurrent(); + FcConfig * config = FcConfigGetCurrent(); if (!config) { throw runtime_error("Failed to get current Fontconfig configuration."); } FcResult result; - FcPattern* raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); + FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); if (!raw_matched_pattern) { throw runtime_error("No matching font found."); } - std::unique_ptr> matched_pattern( - raw_matched_pattern, - [](FcPattern* p) { FcPatternDestroy(p); } - ); + std::unique_ptr> matched_pattern( + raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); - FcChar8* file_path = nullptr; - if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch || !file_path) { + FcChar8 * file_path = nullptr; + if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch + || !file_path) { throw runtime_error("Failed to get font file path."); } - string font_file_path = reinterpret_cast(file_path); + string font_file_path = reinterpret_cast(file_path); return Asset(font_file_path); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 788fac4..e459332 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -2,8 +2,8 @@ #include #include #include -#include #include +#include #include #include #include -- cgit v1.2.3 From b4335bd670cb7f3f622e6ab701123cfbabd15d37 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 17 Dec 2024 15:32:57 +0100 Subject: fix tests + nitpick #77 --- src/crepe/facade/FontFacade.cpp | 32 +++++++++++++------------------- src/test/InputTest.cpp | 7 ++++++- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 5db06d2..d47095f 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -10,44 +10,38 @@ using namespace std; using namespace crepe; FontFacade::FontFacade() { - if (!FcInit()) { + 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(font_family.c_str())); - if (!raw_pattern) { - throw runtime_error("Failed to create font pattern."); - } + if (raw_pattern == NULL) throw runtime_error("Failed to create font pattern."); - std::unique_ptr> pattern( - raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); + unique_ptr> pattern{ + raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); } + }; FcConfig * config = FcConfigGetCurrent(); - if (!config) { - throw runtime_error("Failed to get current Fontconfig configuration."); - } + 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) { - throw runtime_error("No matching font found."); - } + if (raw_matched_pattern == NULL) throw runtime_error("No matching font found."); - std::unique_ptr> matched_pattern( - raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); + unique_ptr> matched_pattern = { + raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); } + }; FcChar8 * file_path = nullptr; - if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch - || !file_path) { + 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(file_path); return Asset(font_file_path); } + diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index d893276..2d844d4 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -1,7 +1,11 @@ -#include "system/RenderSystem.h" #include + +#include +#include + #define protected public #define private public + #include "api/KeyCodes.h" #include "manager/ComponentManager.h" #include "manager/EventManager.h" @@ -29,6 +33,7 @@ public: SDLContext sdl_context{mediator}; InputSystem input_system{mediator}; + ResourceManager resman{mediator}; RenderSystem render{mediator}; EventManager event_manager{mediator}; //GameObject camera; -- cgit v1.2.3 From a8ccf7fe8662086bb223aa4eafd0f85e717d16cf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 17 Dec 2024 15:33:31 +0100 Subject: `make format` --- mwe/events/include/event.h | 2 +- src/crepe/facade/FontFacade.cpp | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index e1b220b..ee1bf52 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent"){}; + ShutDownEvent() : Event("ShutDownEvent") {}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index d47095f..87f95ab 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -10,8 +10,7 @@ using namespace std; using namespace crepe; FontFacade::FontFacade() { - if (!FcInit()) - throw runtime_error("Failed to initialize Fontconfig."); + if (!FcInit()) throw runtime_error("Failed to initialize Fontconfig."); } FontFacade::~FontFacade() { FcFini(); } @@ -22,8 +21,7 @@ Asset FontFacade::get_font_asset(const string & font_family) { if (raw_pattern == NULL) throw runtime_error("Failed to create font pattern."); unique_ptr> pattern{ - raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); } - }; + raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }}; FcConfig * config = FcConfigGetCurrent(); if (config == NULL) throw runtime_error("Failed to get current Fontconfig configuration."); @@ -32,9 +30,8 @@ Asset FontFacade::get_font_asset(const string & font_family) { FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); if (raw_matched_pattern == NULL) throw runtime_error("No matching font found."); - unique_ptr> matched_pattern = { - raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); } - }; + unique_ptr> matched_pattern + = {raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }}; FcChar8 * file_path = nullptr; FcResult res = FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path); @@ -44,4 +41,3 @@ Asset FontFacade::get_font_asset(const string & font_family) { string font_file_path = reinterpret_cast(file_path); return Asset(font_file_path); } - -- cgit v1.2.3