From 88f613cbb2e4aaf3ed55ac0c6490706dd6f6f19d Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 18 Nov 2024 20:12:31 +0100 Subject: rendering based on world unites instead of pixels --- src/crepe/api/Animator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/crepe/api/Animator.cpp') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 464b0fd..f3d809c 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + animator_rect.y = (col_animator - 1)* animator_rect.h; this->active = false; } Animator::~Animator() { dbg_trace(); } -- cgit v1.2.3 From bdc81e355e5bee5d2a3e29346ba08f7bc55196ca Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 20 Nov 2024 12:07:07 +0100 Subject: adjusted branch based on feedback --- mwe/events/include/event.h | 2 +- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/Camera.h | 18 +++++------------- src/crepe/api/Vector2.cpp | 4 +++- src/crepe/api/Vector2.h | 2 ++ src/crepe/facade/SDLContext.cpp | 7 ++----- src/crepe/system/AnimatorSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 1 + src/example/rendering.cpp | 10 +++++----- 9 files changed, 21 insertions(+), 27 deletions(-) (limited to 'src/crepe/api/Animator.cpp') 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/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index f3d809c..d206428 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = (col_animator - 1)* animator_rect.h; + animator_rect.y = (col_animator - 1) * animator_rect.h; this->active = false; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index dfccd24..d7292ef 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -28,25 +28,17 @@ public: //! Background color of the camera view. Color bg_color; - /** - * \pos The position of the camera in world units - */ + //! pos The position of the camera in world units Vector2 pos = {0, 0}; - /** - * \screen the display size in pixels ( output resolution ) - */ + //! screen the display size in pixels ( output resolution ) Vector2 screen = {640, 480}; - /** - * \viewport is the area of the world visible through the camera (in world units) - */ + //! viewport is the area of the world visible through the camera (in world units) Vector2 viewport = {500, 500}; - /** - * \scale scaling factor from world units to pixel coordinates - */ - Vector2 scale = {0,0}; + //! scale scaling factor from world units to pixel coordinates + Vector2 scale = {0, 0}; //! Zoom level of the camera view. double zoom = 1.0f; diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index c3a49b7..8658c00 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -8,14 +8,16 @@ Vector2 Vector2::operator+(const Vector2 & other) const { return {x + other.x, y Vector2 Vector2::operator*(double scalar) const { return {x * scalar, y * scalar}; } -Vector2 Vector2::operator*(const Vector2 & other) const{ +Vector2 Vector2::operator*(const Vector2 & other) const { return {this->x * other.x, this->y * other.y}; } + Vector2 & Vector2::operator*=(const Vector2 & other) { x *= other.x; y *= other.y; return *this; } + Vector2 & Vector2::operator*=(const double & other) { x *= other; y *= other; diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 2a5db1d..790160d 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -18,6 +18,7 @@ struct Vector2 { //! Multiplies this vector by a scalar and returns the result. Vector2 operator*(double scalar) const; + //! Multiplies this vector by another vector element-wise and updates this vector. Vector2 operator*(const Vector2 & other) const; //! Multiplies this vector by another vector element-wise and updates this vector. @@ -26,6 +27,7 @@ struct Vector2 { //! Multiplies a scalar value to both components of this vector and updates this vector. Vector2 & operator*=(const double & other); + //! Divides this vector by another vector element-wise and updates this vector. Vector2 operator/(const Vector2 & other) const; //! Adds another vector to this vector and updates this vector. diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 785b285..0cb7be9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -38,7 +38,7 @@ SDLContext::SDLContext() { } SDL_Window * tmp_window = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - this->viewport.w, this->viewport.h, SDL_WINDOW_RESIZABLE); + this->viewport.w, this->viewport.h, 0); if (!tmp_window) { // FIXME: throw exception std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl; @@ -104,13 +104,11 @@ void SDLContext::handle_events(bool & running) { void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } - void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); - Vector2 pixel_coord = (transform.position - cam.pos) * cam.scale; double pixel_w = sprite.sprite_rect.w * transform.scale * cam.scale.x; @@ -148,11 +146,10 @@ void SDLContext::camera(Camera & cam) { cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; } - if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) { this->viewport.w = cam.screen.x; this->viewport.h = cam.screen.y; - SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y); + SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y); } SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index ceb5bfd..efe0e48 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,8 +3,8 @@ #include #include "api/Animator.h" - #include "facade/SDLContext.h" + #include "AnimatorSystem.h" #include "ComponentManager.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 989a82f..7538e20 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -13,6 +13,7 @@ using namespace crepe; void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } + void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 1db57e5..418e03d 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -24,7 +24,7 @@ int main() { ComponentManager mgr{}; RenderSystem sys{mgr}; - AnimatorSystem anim_sys {mgr}; + AnimatorSystem anim_sys{mgr}; GameObject obj = mgr.new_object("name", "tag", Vector2{250, 0}, 0, 1); GameObject obj1 = mgr.new_object("name", "tag", Vector2{500, 0}, 1, 0.1); @@ -33,10 +33,11 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - Sprite & sprite = obj.add_component(make_shared("../asset/spritesheet/spritesheet_test.png"), color, - FlipSettings{false, false}); + Sprite & sprite = obj.add_component( + make_shared("../asset/spritesheet/spritesheet_test.png"), color, + FlipSettings{false, false}); Camera & cam = obj.add_component(Color::get_red()); - obj.add_component(sprite, 4,1,1).active = true; + obj.add_component(sprite, 4, 1, 1).active = true; } /* { @@ -54,7 +55,6 @@ int main() { } */ - auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { anim_sys.update(); -- cgit v1.2.3 From 8513b2dad0ec43678f17cca0510db4d1a938279a Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 22 Nov 2024 17:08:55 +0100 Subject: working moving camera, flip animations, resizing with animations, and black bars --- src/crepe/api/Animator.cpp | 5 ++++- src/crepe/api/Sprite.h | 10 ++++++++-- src/crepe/facade/SDLContext.cpp | 25 +++++++++++++------------ src/crepe/facade/SDLContext.h | 6 +++--- src/crepe/system/RenderSystem.cpp | 4 ++-- src/example/rendering_particle.cpp | 11 ++++++++--- 6 files changed, 38 insertions(+), 23 deletions(-) (limited to 'src/crepe/api/Animator.cpp') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index d206428..178b165 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,10 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = (col_animator - 1) * animator_rect.h; + animator_rect.y = col_animator * animator_rect.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 = (double)animator_rect.w / (double)animator_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 66599c9..89f9121 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -68,7 +68,13 @@ public: //! height in world units int height = 0; - const double aspect_ratio; + /** + * \aspect_ratio ratio of the img so that scaling will not become weird + * + * cannot be const because if Animator component is addded then ratio becomes scuffed and + * does it need to be calculated again in the Animator + */ + double aspect_ratio; public: /** @@ -90,7 +96,7 @@ private: friend class AnimatorSystem; //! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator - // object is present in GameObject + // object is present in GameObject. this is in sprite pixels Rect sprite_rect; }; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index f49539c..29a8195 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -5,11 +5,9 @@ #include #include #include -#include #include #include #include -#include #include #include @@ -109,21 +107,24 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.sprite_rect.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, - const Vector2 & scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, + const double & img_scale, const Vector2 & cam_scale) const { int pixel_width, pixel_height; if (sprite.sprite_rect.w > sprite.sprite_rect.h) { - pixel_width = static_cast(sprite.width * scale.x); + pixel_width = static_cast(sprite.width * cam_scale.x); pixel_height = static_cast(pixel_width / sprite.aspect_ratio); } else { - pixel_height = static_cast(sprite.height * scale.y); + pixel_height = static_cast(sprite.height * cam_scale.y); pixel_width = static_cast(pixel_height * sprite.aspect_ratio); } - int pixel_x = static_cast((pos.x - pixel_width / 2)); - int pixel_y = static_cast((pos.y - pixel_height / 2)); + pixel_width *= img_scale; + pixel_height *= img_scale; + + int pixel_x = static_cast((pos.x - cam_pos.x - pixel_width / 2)); + int pixel_y = static_cast((pos.y - cam_pos.y - pixel_height / 2)); return SDL_Rect{ .x = pixel_x, @@ -134,27 +135,27 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, } void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const Vector2 & scale) { + const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam_pos, img_scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 68d1630..04c60cc 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,9 +118,9 @@ private: * \param transform Reference to the Transform for positioning. * \param camera Reference to the Camera for view adjustments. */ - void draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale); + void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale); - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & scale); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -152,7 +152,7 @@ private: * on the camera * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, const double & img_scale , const Vector2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index a16fbb5..676ded6 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -72,13 +72,13 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, this->scale * scale); + this->context.draw_particle(sprite, p.position ,p.angle, this->curr_cam_ref->pos ,scale, this->scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, this->scale * tm.scale); + this->context.draw(sprite, tm , this->curr_cam_ref->pos,this->scale); } void RenderSystem::render() { diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 741c985..6a91c19 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,4 +1,6 @@ +#include "api/Animator.h" #include "api/Camera.h" +#include "system/AnimatorSystem.h" #include "system/ParticleSystem.h" #include #include @@ -15,7 +17,6 @@ #include #include -#include #include using namespace crepe; @@ -23,18 +24,21 @@ using namespace std; int main(int argc, char * argv[]) { ComponentManager mgr; - GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 0, 2); + GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 0, 1); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; + AnimatorSystem asys{mgr}; Color color(255, 255, 255, 255); Sprite & test_sprite = game_object.add_component( - make_shared("asset/texture/test_ap43.png"), color, FlipSettings{false, false}); + make_shared("asset/spritesheet/spritesheet_test.png"), color, FlipSettings{true, true}); test_sprite.order_in_layer = 5; test_sprite.width = 1000; test_sprite.height = 500; + game_object.add_component(test_sprite, 4,1,0).active = true; + /* auto & test = game_object.add_component(ParticleEmitter::Data{ @@ -71,6 +75,7 @@ int main(int argc, char * argv[]) { 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); } -- cgit v1.2.3 From aed904bdc2ddaf669940d922611229a171a1f220 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 22 Nov 2024 17:09:40 +0100 Subject: make format --- asset/texture/test_ap43.png | Bin 0 -> 2394 bytes src/crepe/api/Animator.cpp | 4 ++-- src/crepe/api/Sprite.cpp | 3 +-- src/crepe/facade/SDLContext.cpp | 14 +++++++++----- src/crepe/facade/SDLContext.h | 10 +++++++--- src/crepe/system/RenderSystem.cpp | 5 +++-- src/crepe/system/RenderSystem.h | 6 ++---- src/example/rendering_particle.cpp | 6 +++--- 8 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 asset/texture/test_ap43.png (limited to 'src/crepe/api/Animator.cpp') diff --git a/asset/texture/test_ap43.png b/asset/texture/test_ap43.png new file mode 100644 index 0000000..e758ed7 Binary files /dev/null and b/asset/texture/test_ap43.png differ diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 178b165..0043896 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,10 +18,10 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + animator_rect.y = col_animator * animator_rect.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 = (double)animator_rect.w / (double)animator_rect.h; + this->spritesheet.aspect_ratio = (double) animator_rect.w / (double) animator_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 3853aab..c219dd0 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -16,8 +16,7 @@ Sprite::Sprite(game_object_id_t id, const shared_ptr image, const Color color(color), flip(flip), sprite_image(image), - aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) -{ + aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) { dbg_trace(); this->sprite_rect.w = sprite_image->get_width(); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 29a8195..4619c46 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -107,8 +107,9 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.sprite_rect.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, - const double & img_scale, const Vector2 & cam_scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, + const Vector2 & cam_pos, const double & img_scale, + const Vector2 & cam_scale) const { int pixel_width, pixel_height; @@ -135,7 +136,8 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, co } void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale) { + const double & angle, const Vector2 & cam_pos, + const double & img_scale, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) @@ -148,14 +150,16 @@ void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, + const Vector2 & cam_pos, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); + SDL_Rect dstrect + = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 04c60cc..4d97699 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,9 +118,12 @@ private: * \param transform Reference to the Transform for positioning. * \param camera Reference to the Camera for view adjustments. */ - void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale); + void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, + const Vector2 & cam_scale); - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, + const Vector2 & cam_pos, const double & img_scale, + const Vector2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -152,7 +155,8 @@ private: * on the camera * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, const double & img_scale , const Vector2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, + const double & img_scale, const Vector2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 676ded6..1dd1699 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -72,13 +72,14 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position ,p.angle, this->curr_cam_ref->pos ,scale, this->scale); + this->context.draw_particle(sprite, p.position, p.angle, this->curr_cam_ref->pos, + scale, this->scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm , this->curr_cam_ref->pos,this->scale); + this->context.draw(sprite, tm, this->curr_cam_ref->pos, this->scale); } void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 19edc02..f010a83 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -81,12 +81,10 @@ private: //! Pointer to the current active camera for rendering Camera * curr_cam_ref = nullptr; // TODO: needs a better solution - - Vector2 scale; - - SDLContext & context = SDLContext::get_instance(); + Vector2 scale; + SDLContext & context = SDLContext::get_instance(); }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 6a91c19..fad174e 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -32,13 +32,13 @@ int main(int argc, char * argv[]) { Color color(255, 255, 255, 255); Sprite & test_sprite = game_object.add_component( - make_shared("asset/spritesheet/spritesheet_test.png"), color, FlipSettings{true, true}); + make_shared("asset/spritesheet/spritesheet_test.png"), color, + FlipSettings{true, true}); test_sprite.order_in_layer = 5; test_sprite.width = 1000; test_sprite.height = 500; - game_object.add_component(test_sprite, 4,1,0).active = true; - + game_object.add_component(test_sprite, 4, 1, 0).active = true; /* auto & test = game_object.add_component(ParticleEmitter::Data{ -- cgit v1.2.3 From 72e605528ebe8347e1457358fe9025c215afe163 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Wed, 27 Nov 2024 11:08:50 +0100 Subject: implemented feedback, removed width so that user only can set height. --- src/crepe/api/Animator.cpp | 2 +- src/crepe/api/Sprite.cpp | 1 - src/crepe/api/Sprite.h | 2 -- src/crepe/facade/SDLContext.cpp | 13 ++----------- src/example/rendering_particle.cpp | 2 +- 5 files changed, 4 insertions(+), 16 deletions(-) (limited to 'src/crepe/api/Animator.cpp') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 0043896..b6540cf 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -22,6 +22,6 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio = (double) animator_rect.w / (double) animator_rect.h; + this->spritesheet.aspect_ratio = static_cast(animator_rect.w) / animator_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 27f219c..58e0884 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -2,7 +2,6 @@ #include #include "../util/Log.h" -#include "facade/SDLContext.h" #include "Component.h" #include "Sprite.h" diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 89f9121..7d5f4c3 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -63,8 +63,6 @@ public: //! Order within the sorting layer uint8_t order_in_layer = 0; - //! width in world units - int width = 0; //! height in world units int height = 0; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 6e22316..abb3cb7 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -112,17 +112,8 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const double & img_scale) const { - int width, height; - - if (sprite.width > sprite.height) { - width = sprite.width; - height = sprite.width / sprite.aspect_ratio; - } else { - height = sprite.height; - width = sprite.height * sprite.aspect_ratio; - } - - cout << width << " " << height << " " << " " << sprite.aspect_ratio << endl; + int width = sprite.height * sprite.aspect_ratio; + int height = sprite.height; width *= img_scale * cam.zoom; height *= img_scale * cam.zoom; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 680c0ac..afa064d 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -35,10 +35,10 @@ int main(int argc, char * argv[]) { make_shared("asset/texture/test_ap43.png"), color, FlipSettings{true, true}); test_sprite.order_in_layer = 5; - test_sprite.width = 259; test_sprite.height = 195; //game_object.add_component(test_sprite, 4, 1, 0).active = true; + game_object.add_component(test_sprite, 1, 1, 0).active = true; /* auto & test = game_object.add_component(ParticleEmitter::Data{ -- cgit v1.2.3 From 2d623522db0677ca5e88a53e3705a10ce59ba8b6 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 10:04:14 +0100 Subject: removed animator rectangle --- src/crepe/api/Animator.cpp | 11 +++++------ src/crepe/api/Animator.h | 2 -- src/crepe/api/Camera.h | 6 +++--- src/crepe/system/AnimatorSystem.cpp | 11 +++++------ src/crepe/system/RenderSystem.h | 2 +- 5 files changed, 14 insertions(+), 18 deletions(-) (limited to 'src/crepe/api/Animator.cpp') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index b6540cf..31b9632 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -14,14 +14,13 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a col(col) { dbg_trace(); - animator_rect = spritesheet.sprite_rect; - animator_rect.h /= col; - animator_rect.w /= row; - animator_rect.x = 0; - animator_rect.y = col_animator * animator_rect.h; + this->spritesheet.sprite_rect.h /= col; + this->spritesheet.sprite_rect.w /= row; + this->spritesheet.sprite_rect.x = 0; + this->spritesheet.sprite_rect.y = col_animator * this->spritesheet.sprite_rect.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 = static_cast(animator_rect.w) / animator_rect.h; + this->spritesheet.aspect_ratio = static_cast(this->spritesheet.sprite_rect.w) / this->spritesheet.sprite_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 19c9ebd..6c506aa 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -57,8 +57,6 @@ private: //! The current row being animated. int curr_row = 0; - Rect animator_rect; - //TODO: Is this necessary? //int fps; diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index ac56495..3682222 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -33,13 +33,13 @@ public: vec2 offset; //! screen the display size in pixels ( output resolution ) - const ivec2 screen = {1080, 720}; + const ivec2 screen; //! viewport is the area of the world visible through the camera (in world units) - const ivec2 viewport = {500, 1000}; + const ivec2 viewport; //! Zoom level of the camera view. - const double zoom = 1.0f; + const double zoom; public: /** diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index bc94253..e5b277f 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -15,11 +15,10 @@ void AnimatorSystem::update() { uint64_t tick = SDLContext::get_instance().get_ticks(); for (Animator & a : animations) { - if (a.active) { - // (10 frames per second) - a.curr_row = (tick / 100) % a.row; - a.animator_rect.x = (a.curr_row * a.animator_rect.w) + a.curr_col; - a.spritesheet.sprite_rect = a.animator_rect; - } + if (!a.active) continue; + // (10 frames per second) + a.curr_row = (tick / 100) % a.row; + a.spritesheet.sprite_rect.x = (a.curr_row * a.spritesheet.sprite_rect.w) + a.curr_col; + a.spritesheet.sprite_rect = a.spritesheet.sprite_rect; } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 4667424..7279b5c 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -79,7 +79,7 @@ private: SDLContext & context = SDLContext::get_instance(); //! camera postion in the current scene - vec2 cam_pos = {0,0}; + vec2 cam_pos; }; -- cgit v1.2.3 From ca81cf4cdb99cfe42359526ff3840f58f4cf214a Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 10:21:22 +0100 Subject: make format --- src/crepe/api/Animator.cpp | 3 ++- src/crepe/api/Texture.cpp | 16 +++++++--------- src/crepe/api/Texture.h | 10 +++++----- src/crepe/facade/SDLContext.h | 9 +++------ src/crepe/system/RenderSystem.cpp | 2 +- src/crepe/system/RenderSystem.h | 2 -- src/example/rendering_particle.cpp | 4 ++-- src/test/ParticleTest.cpp | 3 +-- src/test/RenderSystemTest.cpp | 20 ++++++++++---------- 9 files changed, 31 insertions(+), 38 deletions(-) (limited to 'src/crepe/api/Animator.cpp') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 31b9632..2b21c6c 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -21,6 +21,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio = static_cast(this->spritesheet.sprite_rect.w) / this->spritesheet.sprite_rect.h; + this->spritesheet.aspect_ratio = static_cast(this->spritesheet.sprite_rect.w) + / this->spritesheet.sprite_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 576bdc3..e43bdaa 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -17,15 +17,13 @@ Texture::~Texture() { this->texture.reset(); } -Texture::Texture(Texture&& other) noexcept - : texture(std::move(other.texture)){ -} - -Texture& Texture::operator=(Texture&& other) noexcept { - if (this != &other) { - texture = std::move(other.texture); - } - return *this; +Texture::Texture(Texture && other) noexcept : texture(std::move(other.texture)) {} + +Texture & Texture::operator=(Texture && other) noexcept { + if (this != &other) { + texture = std::move(other.texture); + } + return *this; } void Texture::load(const Asset & res) { diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index dc4a6d7..7206a66 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -35,11 +35,11 @@ public: */ ~Texture(); // FIXME: this constructor shouldn't be necessary because this class doesn't manage memory - - Texture(Texture&& other) noexcept; - Texture& operator=(Texture&& other) noexcept; - Texture(const Texture&) = delete; - Texture& operator=(const Texture&) = delete; + + Texture(Texture && other) noexcept; + Texture & operator=(Texture && other) noexcept; + Texture(const Texture &) = delete; + Texture & operator=(const Texture &) = delete; /** * \brief Gets the width of the texture. diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 25f2818..5f141be 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -16,14 +16,13 @@ namespace crepe { -struct RenderCtx{ +struct RenderCtx { const Sprite & sprite; const Camera & cam; const vec2 & cam_pos; const vec2 & pos; const double & angle; const double & scale; - }; // TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this @@ -39,8 +38,6 @@ typedef SDL_Keycode CREPE_KEYCODES; */ class SDLContext { - - public: /** * \brief Gets the singleton instance of SDLContext. @@ -161,8 +158,8 @@ private: * \param img_scale the image multiplier for increasing img size * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, const vec2 & cam_pos, - const double & img_scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const vec2 & cam_pos, const double & img_scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 0bef69b..8895f02 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -6,10 +6,10 @@ #include #include "../ComponentManager.h" +#include "../api/Camera.h" #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../api/Camera.h" #include "../facade/SDLContext.h" #include "RenderSystem.h" diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 7279b5c..e70831e 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -13,7 +13,6 @@ class Camera; class Sprite; class Transform; - /** * \class RenderSystem * \brief Manages rendering operations for all game objects. @@ -80,7 +79,6 @@ private: //! camera postion in the current scene vec2 cam_pos; - }; } // namespace crepe diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 68dadd7..8fdbb58 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -31,8 +31,8 @@ int main(int argc, char * argv[]) { Color color(255, 255, 255, 255); auto img = Texture("asset/texture/test_ap43.png"); - Sprite & test_sprite - = game_object.add_component(img, color, Sprite::FlipSettings{true, true}, 1, 1, 500); + Sprite & test_sprite = game_object.add_component( + img, color, Sprite::FlipSettings{true, true}, 1, 1, 500); //game_object.add_component(test_sprite, 4, 1, 0).active = true; game_object.add_component(test_sprite, 1, 1, 0).active = true; diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 1ac058f..976f9a1 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -30,8 +30,7 @@ public: Color color(0, 0, 0, 0); auto s1 = Texture("asset/texture/img.png"); Sprite & test_sprite = game_object.add_component( - s1, color, - Sprite::FlipSettings{true, true}, 1,1,100); + s1, color, Sprite::FlipSettings{true, true}, 1, 1, 100); game_object.add_component(ParticleEmitter::Data{ .position = {0, 0}, diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index 138aa36..e0bd953 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -7,12 +7,12 @@ #define private public #define protected public +#include "crepe/api/Camera.h" #include #include #include #include #include -#include "crepe/api/Camera.h" #include @@ -34,25 +34,25 @@ public: auto s2 = Texture("asset/texture/img.png"); auto s3 = Texture("asset/texture/img.png"); auto s4 = Texture("asset/texture/img.png"); - auto & sprite1 = entity1.add_component(s1, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false}, 5, 5, 100); + auto & sprite1 = entity1.add_component( + s1, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 5, 5, 100); ASSERT_NE(sprite1.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite1.order_in_layer, 5); EXPECT_EQ(sprite1.sorting_in_layer, 5); - auto & sprite2 = entity2.add_component(s2, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false}, 2, 1, 100); + auto & sprite2 = entity2.add_component( + s2, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 2, 1, 100); ASSERT_NE(sprite2.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite2.sorting_in_layer, 2); EXPECT_EQ(sprite2.order_in_layer, 1); - auto & sprite3 = entity3.add_component(s3, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false}, 1, 2, 100); + auto & sprite3 = entity3.add_component( + s3, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 1, 2, 100); ASSERT_NE(sprite3.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite3.sorting_in_layer, 1); EXPECT_EQ(sprite3.order_in_layer, 2); - auto & sprite4 = entity4.add_component(s4, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false}, 1, 1, 100); + auto & sprite4 = entity4.add_component( + s4, Color(0, 0, 0, 0), Sprite::FlipSettings{false, false}, 1, 1, 100); ASSERT_NE(sprite4.sprite_image.texture.get(), nullptr); EXPECT_EQ(sprite4.sorting_in_layer, 1); EXPECT_EQ(sprite4.order_in_layer, 1); @@ -66,7 +66,7 @@ TEST_F(RenderSystemTest, expected_throws) { EXPECT_ANY_THROW({ auto test = Texture(""); entity1.add_component(test, Color(0, 0, 0, 0), - Sprite::FlipSettings{false, false},1,1,100); + Sprite::FlipSettings{false, false}, 1, 1, 100); }); // No camera -- cgit v1.2.3 From 9d9c4fc4565f0ef0fc81c8baeef804389f07afc2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Thu, 28 Nov 2024 12:53:15 +0100 Subject: implemented loek feedback --- src/crepe/api/Animator.cpp | 12 ++++++------ src/crepe/api/Camera.cpp | 4 ++-- src/crepe/api/Camera.h | 4 ++-- src/crepe/api/Color.cpp | 2 +- src/crepe/api/Config.h | 7 +++++-- src/crepe/api/Sprite.cpp | 8 ++++---- src/crepe/api/Sprite.h | 14 +++++++------- src/crepe/facade/SDLContext.cpp | 32 ++++++++++++++++---------------- src/crepe/facade/SDLContext.h | 19 ++++++++++--------- src/crepe/system/AnimatorSystem.cpp | 4 ++-- src/crepe/system/RenderSystem.cpp | 11 ++++------- src/example/rendering_particle.cpp | 2 +- src/test/RenderSystemTest.cpp | 6 +++--- 13 files changed, 63 insertions(+), 62 deletions(-) (limited to 'src/crepe/api/Animator.cpp') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index 2b21c6c..0fdab0e 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -14,14 +14,14 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a col(col) { dbg_trace(); - this->spritesheet.sprite_rect.h /= col; - this->spritesheet.sprite_rect.w /= row; - this->spritesheet.sprite_rect.x = 0; - this->spritesheet.sprite_rect.y = col_animator * this->spritesheet.sprite_rect.h; + this->spritesheet.mask.h /= col; + 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 = static_cast(this->spritesheet.sprite_rect.w) - / this->spritesheet.sprite_rect.h; + this->spritesheet.aspect_ratio = static_cast(this->spritesheet.mask.w) + / this->spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp index 0831f45..39d8ab0 100644 --- a/src/crepe/api/Camera.cpp +++ b/src/crepe/api/Camera.cpp @@ -8,12 +8,12 @@ using namespace crepe; Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const ivec2 & viewport, const double & zoom, const vec2 & offset) + const vec2 & viewport_size, const double & zoom, const vec2 & offset) : Component(id), bg_color(bg_color), offset(offset), screen(screen), - viewport(viewport), + viewport_size(viewport_size), zoom(zoom) { dbg_trace(); } diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index 3682222..2d8fa48 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -22,7 +22,7 @@ public: * \param bg_color Background color for the camera view. */ Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen, - const ivec2 & viewport, const double & zoom, const vec2 & offset = {0, 0}); + const vec2 & viewport_size, const double & zoom, const vec2 & offset = {0, 0}); ~Camera(); // dbg_trace only public: @@ -36,7 +36,7 @@ public: const ivec2 screen; //! viewport is the area of the world visible through the camera (in world units) - const ivec2 viewport; + const vec2 viewport_size; //! Zoom level of the camera view. const double zoom; diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp index dc7c15f..29bd77a 100644 --- a/src/crepe/api/Color.cpp +++ b/src/crepe/api/Color.cpp @@ -2,7 +2,7 @@ using namespace crepe; -const Color Color::WHITE{0xff, 0xff, 0xff, 0xff}; +const Color Color::WHITE{0xff, 0xff, 0xff}; const Color Color::RED{0xff, 0x00, 0x00}; const Color Color::GREEN{0x00, 0xff, 0x00}; const Color Color::BLUE{0x00, 0x00, 0xff}; diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index d73e488..225e9b9 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -2,6 +2,7 @@ #include "../util/Log.h" #include "types.h" +#include namespace crepe { @@ -66,8 +67,10 @@ public: //! default window settings struct { //TODO make this constexpr because this will never change - ivec2 def_size = {1080, 720}; - } win_set; + ivec2 default_size = {1080, 720}; + std::string window_title = "Jetpack joyride clone"; + + } window_settings; //! Asset loading options struct { diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 65c6cc3..8647794 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -11,7 +11,7 @@ using namespace std; using namespace crepe; Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, - const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height) + const FlipSettings & flip, int sort_layer, int order_layer, int height) : Component(id), color(color), flip(flip), @@ -22,9 +22,9 @@ Sprite::Sprite(game_object_id_t id, Texture & image, const Color & color, dbg_trace(); - this->sprite_rect.w = sprite_image.get_width(); - this->sprite_rect.h = sprite_image.get_height(); - this->aspect_ratio = static_cast(this->sprite_rect.w) / this->sprite_rect.h; + this->mask.w = sprite_image.get_width(); + this->mask.h = sprite_image.get_height(); + this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index e40ce24..a0e90a0 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -41,7 +41,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, uint8_t sort_layer, uint8_t order_layer, int height); + const FlipSettings & flip, int sort_layer, int order_layer, int height); /** * \brief Destroys the Sprite instance. @@ -58,9 +58,9 @@ public: FlipSettings flip; //! Layer sorting level of the sprite - const uint8_t sorting_in_layer; + const int sorting_in_layer; //! Order within the sorting layer - const uint8_t order_in_layer; + const int order_in_layer; //! height in world units const int height; @@ -74,13 +74,13 @@ public: double aspect_ratio; private: - //! Reads the sprite_rect of sprite + //! Reads the mask of sprite friend class SDLContext; - //! Reads the all the variables plus the sprite_rect + //! Reads the all the variables plus the mask friend class Animator; - //! Reads the all the variables plus the sprite_rect + //! Reads the all the variables plus the mask friend class AnimatorSystem; struct Rect { @@ -91,7 +91,7 @@ private: }; //! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator // object is present in GameObject. this is in sprite pixels - Rect sprite_rect; + Rect mask; }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 7317a77..9f60285 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -15,7 +15,6 @@ #include "../api/Config.h" #include "../api/Sprite.h" #include "../api/Texture.h" -#include "../api/Transform.h" #include "../util/Log.h" #include "SDLContext.h" @@ -36,10 +35,10 @@ SDLContext::SDLContext() { throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError())); } - auto & cfg = Config::get_instance().win_set; + auto & cfg = Config::get_instance().window_settings; SDL_Window * tmp_window - = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - cfg.def_size.x, cfg.def_size.y, 0); + = SDL_CreateWindow(cfg.window_title.c_str(), SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, cfg.default_size.x, cfg.default_size.y, 0); if (!tmp_window) { throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError())); } @@ -102,10 +101,10 @@ 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.sprite_rect.x, - .y = sprite.sprite_rect.y, - .w = sprite.sprite_rect.w, - .h = sprite.sprite_rect.h, + .x = sprite.mask.x, + .y = sprite.mask.y, + .w = sprite.mask.w, + .h = sprite.mask.h, }; } SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, @@ -118,14 +117,14 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const height *= img_scale * cam.zoom; return SDL_Rect{ - .x = static_cast((pos.x - cam_pos.x + (cam.viewport.x / 2) - width / 2)), - .y = static_cast((pos.y - cam_pos.y + (cam.viewport.y / 2) - height / 2)), + .x = static_cast((pos.x - cam_pos.x + (cam.viewport_size.x / 2) - width / 2)), + .y = static_cast((pos.y - cam_pos.y + (cam.viewport_size.y / 2) - height / 2)), .w = width, .h = height, }; } -void SDLContext::draw(const RenderCtx & ctx) { +void SDLContext::draw(const RenderContext & ctx) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * ctx.sprite.flip.flip_x) @@ -149,12 +148,12 @@ void SDLContext::set_camera(const Camera & cam) { } double screen_aspect = cam.screen.x / cam.screen.y; - double viewport_aspect = cam.viewport.x / cam.viewport.y; + double viewport_aspect = cam.viewport_size.x / cam.viewport_size.y; SDL_Rect view; // calculate black bars if (screen_aspect > viewport_aspect) { - // lettorboxing + // letterboxing view.h = cam.screen.y / cam.zoom; view.w = cam.screen.y * viewport_aspect; view.x = (cam.screen.x - view.w) / 2; @@ -169,7 +168,8 @@ void SDLContext::set_camera(const Camera & cam) { // set drawing area SDL_RenderSetViewport(this->game_renderer.get(), &view); - SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y); + SDL_RenderSetLogicalSize(this->game_renderer.get(), static_cast(cam.viewport_size.x), + static_cast(cam.viewport_size.y)); // set bg color SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, @@ -178,8 +178,8 @@ void SDLContext::set_camera(const Camera & cam) { SDL_Rect bg = { .x = 0, .y = 0, - .w = cam.viewport.x, - .h = cam.viewport.y, + .w = static_cast(cam.viewport_size.x), + .h = static_cast(cam.viewport_size.y), }; // fill bg color SDL_RenderFillRect(this->game_renderer.get(), &bg); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 5f141be..8b38111 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -16,14 +16,6 @@ namespace crepe { -struct RenderCtx { - const Sprite & sprite; - const Camera & cam; - const vec2 & cam_pos; - const vec2 & pos; - const double & angle; - const double & scale; -}; // TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this // typedef is unusable when crepe is packaged. Wouter will fix this later. @@ -37,6 +29,15 @@ typedef SDL_Keycode CREPE_KEYCODES; * event handling, and rendering to the screen. It is never used directly by the user */ class SDLContext { +public: + struct RenderContext { + const Sprite & sprite; + const Camera & cam; + const vec2 & cam_pos; + const vec2 & pos; + const double & angle; + const double & scale; + }; public: /** @@ -125,7 +126,7 @@ private: * \brief Draws a sprite to the screen using the specified transform and camera. * \param RenderCtx Reference to rendering data to draw */ - void draw(const RenderCtx & ctx); + void draw(const RenderContext & ctx); //! Clears the screen, preparing for a new frame. void clear_screen(); diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index e5b277f..4c40940 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -18,7 +18,7 @@ void AnimatorSystem::update() { if (!a.active) continue; // (10 frames per second) a.curr_row = (tick / 100) % a.row; - a.spritesheet.sprite_rect.x = (a.curr_row * a.spritesheet.sprite_rect.w) + a.curr_col; - a.spritesheet.sprite_rect = a.spritesheet.sprite_rect; + a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col; + a.spritesheet.mask = a.spritesheet.mask; } } diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 8895f02..c196bb1 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -77,31 +77,28 @@ bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam, for (const Particle & p : em.data.particles) { if (!p.active) continue; - RenderCtx ctx{ + this->context.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, .cam_pos = this->cam_pos, .pos = p.position, .angle = p.angle, .scale = scale, - }; - this->context.draw(ctx); + }); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm) { - - RenderCtx ctx{ + this->context.draw(SDLContext::RenderContext{ .sprite = sprite, .cam = cam, .cam_pos = this->cam_pos, .pos = tm.position, .angle = tm.rotation, .scale = tm.scale, - }; - this->context.draw(ctx); + }); } void RenderSystem::render() { diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 8fdbb58..3a12144 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -60,7 +60,7 @@ int main(int argc, char * argv[]) { */ auto & cam = game_object.add_component(Color::WHITE, ivec2{1080, 720}, - ivec2{2000, 2000}, 1.0f); + vec2{2000, 2000}, 1.0f); /* game_object diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index e0bd953..bb5b81a 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -113,7 +113,7 @@ TEST_F(RenderSystemTest, sorting_sprites) { } TEST_F(RenderSystemTest, Update) { - entity1.add_component(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); + entity1.add_component(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); { vector> sprites = this->mgr.get_components_by_type(); ASSERT_EQ(sprites.size(), 4); @@ -141,7 +141,7 @@ TEST_F(RenderSystemTest, Camera) { EXPECT_NE(cameras.size(), 1); } { - entity1.add_component(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); + entity1.add_component(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); auto cameras = this->mgr.get_components_by_type(); EXPECT_EQ(cameras.size(), 1); } @@ -149,7 +149,7 @@ TEST_F(RenderSystemTest, Camera) { //TODO improve with newer version } TEST_F(RenderSystemTest, Color) { - entity1.add_component(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); + entity1.add_component(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); auto & sprite = this->mgr.get_components_by_id(entity1.id).front().get(); ASSERT_NE(sprite.sprite_image.texture.get(), nullptr); -- cgit v1.2.3 From 85aa57e33d493502b05984ce44db6c68f627a6f2 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 28 Nov 2024 12:54:27 +0100 Subject: `make format` --- mwe/events/include/event.h | 2 +- src/crepe/api/Animator.cpp | 4 ++-- src/crepe/facade/SDLContext.h | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) (limited to 'src/crepe/api/Animator.cpp') 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/api/Animator.cpp b/src/crepe/api/Animator.cpp index 0fdab0e..45f67f6 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -21,7 +21,7 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a this->active = false; // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio = static_cast(this->spritesheet.mask.w) - / this->spritesheet.mask.h; + this->spritesheet.aspect_ratio + = static_cast(this->spritesheet.mask.w) / this->spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 8b38111..6030a6e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -16,7 +16,6 @@ namespace crepe { - // TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this // typedef is unusable when crepe is packaged. Wouter will fix this later. typedef SDL_Keycode CREPE_KEYCODES; -- cgit v1.2.3