diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Animator.h | 7 | ||||
-rw-r--r-- | src/crepe/api/Button.cpp | 7 | ||||
-rw-r--r-- | src/crepe/api/Button.h | 18 | ||||
-rw-r--r-- | src/crepe/api/Text.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/Text.h | 24 | ||||
-rw-r--r-- | src/crepe/api/UIObject.h | 4 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 17 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 30 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 6 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 5 |
10 files changed, 71 insertions, 53 deletions
diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 102894d..95539d3 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,5 +1,6 @@ #pragma once +#include "../manager/LoopTimerManager.h" #include "../types.h" #include "Component.h" @@ -99,6 +100,12 @@ private: //! The maximum number of rows and columns inside the spritesheet const uvec2 grid_size; + // the time elapsed from a frame duration + duration_t elapsed_time = {}; + + // frame counter + unsigned int frame = 0; + //! Uses the spritesheet friend AnimatorSystem; }; diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 40153c9..8eadd89 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,7 +2,10 @@ namespace crepe { -Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset) - : UIObject(id, dimensions, offset) {} +Button::Button( + game_object_id_t id, const vec2 & dimensions, const Data & data, const vec2 & offset +) + : UIObject(id, dimensions, offset), + data(data) {} } // namespace crepe diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index d42527e..e986c04 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,8 +1,7 @@ #pragma once -#include <functional> +#include "../types.h" -#include "Event.h" #include "UIObject.h" namespace crepe { @@ -21,14 +20,24 @@ namespace crepe { */ class Button : public UIObject { public: + struct Data { + //! variable indicating if transform is relative to camera(false) or world(true) + bool world_space = false; + }; + +public: /** * \brief Constructs a Button with the specified game object ID and dimensions. * * \param id The unique ID of the game object associated with this button. * \param dimensions The width and height of the UIObject * \param offset The offset relative this GameObjects Transform + * \param data additional data the button has */ - Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset); + Button( + game_object_id_t id, const vec2 & dimensions, const Data & data, + const vec2 & offset = {0, 0} + ); /** * \brief Get the maximum number of instances for this component * @@ -38,6 +47,9 @@ public: */ virtual int get_instances_max() const { return 1; } +public: + Data data; + private: //! friend relation hover variable friend class InputSystem; diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index b24f0ac..e5b623d 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,10 +1,12 @@ +#include "../types.h" + #include "Text.h" 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 + game_object_id_t id, const vec2 & dimensions, const std::string & font_family, + const Data & data, const vec2 & offset, const std::string & text ) : UIObject(id, dimensions, offset), text(text), diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 0289b85..8b3d53e 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -3,6 +3,8 @@ #include <optional> #include <string> +#include "../types.h" + #include "Asset.h" #include "Color.h" #include "UIObject.h" @@ -17,22 +19,8 @@ class Text : public UIObject { public: //! Text data that does not have to be set in the constructor struct Data { - /** - * \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; + //! variable indicating if transform is relative to camera(false) or world(true) + bool world_space = false; //! Label text color. Color text_color = Color::BLACK; @@ -49,8 +37,8 @@ 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 = "" + game_object_id_t id, const vec2 & dimensions, const std::string & font_family, + const Data & data, const vec2 & offset = {0, 0}, const std::string & text = "" ); //! Label text. diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h index f1318ab..0d9b1f7 100644 --- a/src/crepe/api/UIObject.h +++ b/src/crepe/api/UIObject.h @@ -15,13 +15,11 @@ public: * \param dimensions width and height of the UIObject * \param offset Offset relative to the GameObject Transform */ - UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset); + UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset = {0, 0}); //! Width and height of the UIObject vec2 dimensions; //! Position offset relative to this GameObjects Transform vec2 offset; - //! variable indicating if transform is relative to camera(false) or world(true) - bool world_space = false; }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 859f966..6c93fb2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -69,8 +69,6 @@ SDLContext::SDLContext(Mediator & mediator) { throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); } - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); - mediator.sdl_context = *this; } @@ -174,6 +172,7 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } void SDLContext::draw(const RenderContext & ctx) { + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) @@ -206,6 +205,7 @@ void SDLContext::draw(const RenderContext & ctx) { } void SDLContext::draw_text(const RenderText & data) { + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); const Text & text = data.text; const Font & font = data.font; @@ -235,9 +235,16 @@ void SDLContext::draw_text(const RenderText & data) { = {tmp_font_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; vec2 size = text.dimensions * cam_aux_data.render_scale * data.transform.scale; - vec2 screen_pos = (absoluut_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2 - ) * cam_aux_data.render_scale - - size / 2 + cam_aux_data.bar_size; + vec2 screen_pos = absoluut_pos; + if (text.data.world_space) { + screen_pos = (screen_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2) + * cam_aux_data.render_scale + - size / 2 + cam_aux_data.bar_size; + } else { + screen_pos + = (screen_pos + (cam_aux_data.zoomed_viewport) / 2) * cam_aux_data.render_scale + - size / 2 + cam_aux_data.bar_size; + } SDL_FRect dstrect { .x = screen_pos.x, diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index e5ab2fa..143d5d6 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,7 +1,8 @@ +#include <chrono> + #include "../api/Animator.h" #include "../manager/ComponentManager.h" #include "../manager/LoopTimerManager.h" -#include <chrono> #include "AnimatorSystem.h" @@ -13,28 +14,31 @@ void AnimatorSystem::frame_update() { LoopTimerManager & timer = this->mediator.loop_timer; RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); - float elapsed_time = duration_cast<duration<float>>(timer.get_elapsed_time()).count(); + duration_t elapsed_time = timer.get_delta_time(); for (Animator & a : animations) { if (!a.active) continue; if (a.data.fps == 0) continue; Animator::Data & ctx = a.data; - float frame_duration = 1.0f / ctx.fps; - int last_frame = ctx.row; + a.elapsed_time += elapsed_time; + duration_t frame_duration = 1000ms / ctx.fps; int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; - int total_frames = cycle_end - ctx.cycle_start; - - int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; + if (a.elapsed_time >= frame_duration) { + a.elapsed_time = 0ms; + a.frame++; + if (a.frame == cycle_end) { + a.frame = ctx.cycle_start; + if (!ctx.looping) { + a.active = false; + continue; + } + } + } - ctx.row = ctx.cycle_start + curr_frame; + ctx.row = ctx.cycle_start + a.frame; a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; - a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h); - - if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { - a.active = false; - } } } diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index b4a0633..be7eda6 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,8 +1,8 @@ #include "../api/Button.h" +#include "../api/Config.h" #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" -#include "util/Log.h" #include "InputSystem.h" @@ -213,10 +213,10 @@ bool InputSystem::is_mouse_inside_button( const Transform & cam_transform ) { vec2 actual_pos = transform.position + button.offset; - if (!button.world_space) { + if (!button.data.world_space) { actual_pos += cam_transform.position; } - vec2 half_dimensions = button.dimensions / 2; + vec2 half_dimensions = button.dimensions * transform.scale / 2; return mouse_pos.x >= actual_pos.x - half_dimensions.x && mouse_pos.x <= actual_pos.x + half_dimensions.x diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 37311cc..be62367 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,12 +1,9 @@ #pragma once -#include "../api/Config.h" -#include "../facade/EventData.h" - #include "../api/Event.h" #include "../api/Metadata.h" +#include "../facade/EventData.h" #include "../types.h" -#include "../util/OptionalRef.h" #include "System.h" |