diff options
-rw-r--r-- | src/crepe/api/Animator.cpp | 12 | ||||
-rw-r--r-- | src/crepe/api/Camera.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Camera.h | 4 | ||||
-rw-r--r-- | src/crepe/api/Color.cpp | 2 | ||||
-rw-r--r-- | src/crepe/api/Config.h | 7 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 8 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 14 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 32 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 19 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 4 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 11 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 2 | ||||
-rw-r--r-- | src/test/RenderSystemTest.cpp | 6 |
13 files changed, 63 insertions, 62 deletions
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<double>(this->spritesheet.sprite_rect.w) - / this->spritesheet.sprite_rect.h; + this->spritesheet.aspect_ratio = static_cast<double>(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 <string> 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<double>(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<double>(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<int>((pos.x - cam_pos.x + (cam.viewport.x / 2) - width / 2)), - .y = static_cast<int>((pos.y - cam_pos.y + (cam.viewport.y / 2) - height / 2)), + .x = static_cast<int>((pos.x - cam_pos.x + (cam.viewport_size.x / 2) - width / 2)), + .y = static_cast<int>((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<int>(cam.viewport_size.x), + static_cast<int>(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<int>(cam.viewport_size.x), + .h = static_cast<int>(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<Camera>(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<Camera>(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); + entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); { vector<reference_wrapper<Sprite>> sprites = this->mgr.get_components_by_type<Sprite>(); ASSERT_EQ(sprites.size(), 4); @@ -141,7 +141,7 @@ TEST_F(RenderSystemTest, Camera) { EXPECT_NE(cameras.size(), 1); } { - entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); + entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); auto cameras = this->mgr.get_components_by_type<Camera>(); 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<Camera>(Color::WHITE, ivec2{1080, 720}, ivec2{2000, 2000}, 1.0f); + entity1.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); auto & sprite = this->mgr.get_components_by_id<Sprite>(entity1.id).front().get(); ASSERT_NE(sprite.sprite_image.texture.get(), nullptr); |