diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 18 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 10 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 18 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 14 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 14 |
5 files changed, 34 insertions, 40 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index eacb10a..daf0050 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -12,12 +12,12 @@ #include <stdexcept> #include <string> +#include "../api/Camera.h" #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" +#include "../api/Vector2.h" #include "../util/Log.h" -#include "api/Camera.h" -#include "api/Vector2.h" #include "SDLContext.h" @@ -96,8 +96,7 @@ 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()); } - -SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) { +SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { return SDL_Rect{ .x = sprite.sprite_rect.x, .y = sprite.sprite_rect.y, @@ -106,7 +105,7 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) { }; } SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, - const double & scale, const Camera & cam) { + const double & scale, const Camera & cam) const { double adjusted_x = (pos.x - cam.x) * cam.zoom; double adjusted_y = (pos.y - cam.y) * cam.zoom; @@ -123,20 +122,21 @@ 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 double & scale, - const Camera & camera) { + const Camera & camera) const { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) - | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); + | (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, camera); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, - &dstrect, angle, NULL, render_flip); + &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, + const Camera & cam) const { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 287ad5d..718c40f 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -124,9 +124,10 @@ 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 Camera & camera); + void draw(const Sprite & sprite, const Transform & transform, const Camera & camera) const; - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const double & scale, const Camera & camera); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, + const double & scale, const Camera & camera) const; //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -147,7 +148,7 @@ private: * \param sprite Reference to the sprite to calculate the rectangle * \return sdl rectangle to draw a src image */ - SDL_Rect get_src_rect(const Sprite & sprite); + SDL_Rect get_src_rect(const Sprite & sprite) const; /** * \brief calculates the sqaure size of the image for an destination * @@ -158,7 +159,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 double & scale, const Camera & cam); + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const double & scale, + const Camera & cam) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1ae5ca7..28bcf56 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,11 +2,11 @@ #include <vector> #include "../ComponentManager.h" +#include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Transform.h" -#include "../facade/SDLContext.h" -#include "../api/ParticleEmitter.h" #include "../api/Vector2.h" +#include "../facade/SDLContext.h" #include "RenderSystem.h" @@ -21,13 +21,12 @@ void RenderSystem::update_camera() { auto cameras = mgr.get_components_by_type<Camera>(); for (Camera & cam : cameras) { - SDLContext::get_instance().camera(cam); + SDLContext::get_instance().set_camera(cam); this->curr_cam = &cam; } } -bool RenderSystem::render_particle(const Sprite & sprite, - const double & scale) { +bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) const { ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); @@ -49,22 +48,23 @@ bool RenderSystem::render_particle(const Sprite & sprite, } return rendering_particles; } -void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { +void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) const { ComponentManager & mgr = this->component_manager; SDLContext & render = SDLContext::get_instance(); - + render.draw(sprite, tm, *curr_cam); } -void RenderSystem::render() { +void RenderSystem::render() const { ComponentManager & mgr = this->component_manager; auto sprites = mgr.get_components_by_type<Sprite>(); for (const Sprite & sprite : sprites) { if (!sprite.active) continue; - auto transform = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get(); + const Transform & transform + = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get(); bool rendered_particles = this->render_particle(sprite, transform.scale); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 6643084..8841f72 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -25,13 +25,7 @@ public: * This method is called to perform all rendering operations for the current game frame. */ void update() override; - - - RenderSystem(const RenderSystem &) = delete; - RenderSystem(RenderSystem &&) = delete; - RenderSystem & operator=(const RenderSystem &) = delete; - RenderSystem & operator=(RenderSystem &&) = delete; - + private: //! Clears the screen in preparation for rendering. void clear_screen() const; @@ -43,7 +37,7 @@ private: void update_camera(); //! Renders the whole screen - void render(); + void render() const; /** * \brief Renders all the particles on the screen from a given sprite. @@ -52,7 +46,7 @@ private: * \param tm the Transform component for scale * \return true if particles have been rendered */ - bool render_particle(const Sprite &, const double & scale); + bool render_particle(const Sprite & sprite, const double & scale) const; /** * \brief renders a sprite with a Transform component on the screen @@ -60,7 +54,7 @@ private: * \param sprite the sprite component that holds all the data * \param tm the Transform component that holds the position,rotation and scale */ - void render_normal(const Sprite &, const Transform & tm); + void render_normal(const Sprite & sprite, const Transform & tm) const; /** * \todo Include color handling for sprites. diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 71b50ba..3f71750 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -4,15 +4,15 @@ #include <crepe/ComponentManager.h> #include <crepe/Component.h> +#include <crepe/api/Color.h> #include <crepe/api/GameObject.h> #include <crepe/api/ParticleEmitter.h> #include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> #include <crepe/api/Texture.h> #include <crepe/api/Transform.h> -#include <crepe/system/RenderSystem.h> -#include <crepe/api/Color.h> -#include <crepe/api/Sprite.h> #include <crepe/api/Vector2.h> +#include <crepe/system/RenderSystem.h> #include <chrono> @@ -20,15 +20,14 @@ using namespace crepe; using namespace std; int main(int argc, char * argv[]) { - ComponentManager mgr; + ComponentManager mgr; GameObject game_object = mgr.new_object("", "", Vector2{100, 100}, 0, 0.1); RenderSystem sys{mgr}; ParticleSystem psys{mgr}; Color color(255, 255, 255, 255); Sprite test_sprite = game_object.add_component<Sprite>( - make_shared<Texture>("../asset/texture/img.png"), color, - FlipSettings{false, false}); + make_shared<Texture>("../asset/texture/img.png"), color, FlipSettings{false, false}); game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{ .position = {0, 0}, .max_particles = 10, @@ -54,9 +53,8 @@ int main(int argc, char * argv[]) { while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { psys.update(); sys.update(); - SDL_Delay(10 ); + SDL_Delay(10); } return 0; } - |