diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Animator.cpp | 1 | ||||
-rw-r--r-- | src/crepe/api/Animator.h | 32 | ||||
-rw-r--r-- | src/crepe/api/LoopManager.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 5 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 8 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 6 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 26 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 7 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 63 |
9 files changed, 93 insertions, 61 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 45f67f6..7fe49ee 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,6 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a this->spritesheet.mask.w /= row; this->spritesheet.mask.x = 0; this->spritesheet.mask.y = col_animator * this->spritesheet.mask.h; - this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet this->spritesheet.aspect_ratio diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 6c506aa..ede450a 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -18,11 +18,6 @@ class SDLContext; class Animator : public Component { public: - //TODO: need to implement this - void loop(); - void stop(); - -public: /** * \brief Constructs an Animator object that will control animations for a sprite sheet. * @@ -57,15 +52,32 @@ private: //! The current row being animated. int curr_row = 0; - //TODO: Is this necessary? - //int fps; + //! should the animation loop + bool looping = false; + + //! starting frame for cycling + int cycle_start = 0; + + //! end frame for cycling (-1 --> use last frame) + int cycle_end = -1; + + //! frames per second for animation + int fps = 1; + + int offset_x = 0; + +public: + void loop() { this->looping = true; } + void play() {this->active = true;} + void pause() {this->active = false;} + void stop() {this->active = false; this->curr_col = 0; this->curr_row = 0;} + void set_fps(int fps) {this->fps = fps;} + void set_cycle_range(int start, int end) {this->cycle_start = start, this->cycle_end = end;} + void set_anim(int col) {this->curr_row = 0; this->curr_col = col; } private: //! AnimatorSystem adjust the private member parameters of Animator; friend class AnimatorSystem; - - //! SDLContext reads the Animator member var's - friend class SDLContext; }; } // namespace crepe // diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 7edf4d1..88ca704 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -58,6 +58,7 @@ void LoopManager::setup() { this->game_running = true; LoopTimer::get_instance().start(); LoopTimer::get_instance().set_fps(200); + this->scene_manager.load_next_scene(); } void LoopManager::render() { @@ -66,4 +67,7 @@ void LoopManager::render() { } } -void LoopManager::update() { LoopTimer & timer = LoopTimer::get_instance(); } +void LoopManager::update() { + LoopTimer & timer = LoopTimer::get_instance(); + this->get_system<AnimatorSystem>().update(); +} diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 1d57b53..29e415f 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -6,19 +6,20 @@ #include "Component.h" #include "Sprite.h" #include "Texture.h" +#include "types.h" using namespace std; using namespace crepe; Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, - const FlipSettings & flip, int sort_layer, int order_layer, float height) + const FlipSettings & flip, int sort_layer, int order_layer, const vec2 & size) : Component(id), color(color), flip(flip), sprite_image(std::move(image)), sorting_in_layer(sort_layer), order_in_layer(order_layer), - height(height) { + size(size) { dbg_trace(); diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 7d9c14b..f04f70c 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,11 +1,10 @@ #pragma once -#include <cstdint> - #include "../Component.h" #include "Color.h" #include "Texture.h" +#include "types.h" namespace crepe { @@ -41,7 +40,7 @@ public: * \param height the height of the image in game units */ Sprite(game_object_id_t id, Texture & image, const Color & color, - const FlipSettings & flip, int sort_layer, int order_layer, float height); + const FlipSettings & flip, int sort_layer, int order_layer, const vec2 & size); /** * \brief Destroys the Sprite instance. @@ -62,8 +61,7 @@ public: //! Order within the sorting layer const int order_in_layer; - //! height in world units - const float height; + vec2 size; /** * \aspect_ratio ratio of the img so that scaling will not become weird diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 1f729b2..f0a21d5 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -113,7 +113,11 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_FRect SDLContext::get_dst_rect(const DstRect & ctx) const { - vec2 size = {(ctx.sprite.height * ctx.sprite.aspect_ratio), ctx.sprite.height}; + + vec2 size = { + ctx.sprite.size.x == 0 && ctx.sprite.size.y != 0 ? ctx.sprite.size.y * ctx.sprite.aspect_ratio : ctx.sprite.size.x, + ctx.sprite.size.y == 0 && ctx.sprite.size.x != 0 ? ctx.sprite.size.x / ctx.sprite.aspect_ratio : ctx.sprite.size.y + }; const CameraValues & cam = ctx.cam; diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 4c40940..6a84150 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,7 +1,6 @@ -#include <cstdint> + #include "api/Animator.h" -#include "facade/SDLContext.h" #include "AnimatorSystem.h" #include "ComponentManager.h" @@ -13,12 +12,25 @@ void AnimatorSystem::update() { RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); - uint64_t tick = SDLContext::get_instance().get_ticks(); + double elapsed_time = this->timer.get_current_time(); + for (Animator & a : animations) { if (!a.active) continue; - // (10 frames per second) - a.curr_row = (tick / 100) % a.row; - a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col; - a.spritesheet.mask = a.spritesheet.mask; + + double frame_duration = 1.0f / a.fps; + + int cycle_end = (a.cycle_end == -1) ? a.row : cycle_end; + int total_frames = cycle_end - a.cycle_start; + + + int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; + + a.curr_row = a.cycle_start + curr_frame; + a.spritesheet.mask.x = std::clamp((a.curr_row * a.spritesheet.mask.w - a.offset_x), 0, a.spritesheet.mask.w); + a.spritesheet.mask.y = (a.curr_col * a.spritesheet.mask.h); + + if (!a.looping && curr_frame == total_frames) { + a.active = false; + } } } diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index f8179a9..e8a5158 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -1,9 +1,7 @@ #pragma once #include "System.h" - -//TODO: -// control if flip works with animation system +#include "api/LoopTimer.h" namespace crepe { @@ -26,6 +24,9 @@ public: * looping). */ void update() override; + +private: + LoopTimer & timer = LoopTimer::get_instance(); }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 9dcaa49..026c9ce 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,5 +1,7 @@ #include "api/Animator.h" #include "api/Camera.h" +#include "api/LoopManager.h" +#include "api/LoopTimer.h" #include "system/AnimatorSystem.h" #include "system/ParticleSystem.h" #include <SDL2/SDL_timer.h> @@ -16,29 +18,10 @@ #include <crepe/system/RenderSystem.h> #include <crepe/types.h> -#include <chrono> - using namespace crepe; using namespace std; -int main(int argc, char * argv[]) { - ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); - RenderSystem sys{mgr}; - ParticleSystem psys{mgr}; - AnimatorSystem asys{mgr}; - - Color color(255, 255, 255, 255); - - auto img = Texture("asset/texture/test_ap43.png"); - - Sprite & test_sprite = game_object.add_component<Sprite>( - img, color, Sprite::FlipSettings{true, true}, 1, 1, 1.95); - - //game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true; - game_object.add_component<Animator>(test_sprite, 1, 1, 0).active = true; - - /* +/* auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, @@ -60,9 +43,35 @@ int main(int argc, char * argv[]) { }); */ - auto & cam = game_object.add_component<Camera>(Color::RED, ivec2{1280, 720}, - vec2{2.59,1.95}, 2.0); - cam.offset.x = 1; +class TestScene : public Scene { +public: + using Scene::Scene; + + void load_scene() { + ComponentManager & mgr = this->component_manager; + GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); + + Color color(255, 255, 255, 255); + + auto img = Texture("asset/spritesheet/spritesheet_test.png"); + + Sprite & test_sprite = game_object.add_component<Sprite>( + img, color, Sprite::FlipSettings{true, true}, 1, 1, vec2{1, 1}); + + //game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true; + game_object.add_component<Animator>(test_sprite, 4, 1, 0).active = true; + + auto & cam = game_object.add_component<Camera>(Color::RED, ivec2{1280, 720}, + vec2{2.59, 1.95}, 2.0); + } + + string get_name() const { return "TestScene"; }; +}; + +int main(int argc, char * argv[]) { + LoopManager engine; + engine.add_scene<TestScene>(); + engine.start(); /* game_object @@ -73,13 +82,5 @@ int main(int argc, char * argv[]) { = 6; */ - auto start = std::chrono::steady_clock::now(); - while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { - psys.update(); - asys.update(); - sys.update(); - SDL_Delay(10); - } - return 0; } |