aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Animator.cpp3
-rw-r--r--src/crepe/api/Animator.h4
-rw-r--r--src/crepe/api/Camera.cpp10
-rw-r--r--src/crepe/api/Camera.h27
-rw-r--r--src/crepe/api/Color.cpp2
-rw-r--r--src/crepe/api/Config.h7
-rw-r--r--src/crepe/api/Sprite.cpp19
-rw-r--r--src/crepe/api/Sprite.h33
-rw-r--r--src/crepe/api/Texture.cpp13
-rw-r--r--src/crepe/api/Texture.h5
-rw-r--r--src/crepe/api/Vector2.h13
-rw-r--r--src/crepe/facade/SDLContext.cpp89
-rw-r--r--src/crepe/facade/SDLContext.h40
-rw-r--r--src/crepe/system/AnimatorSystem.cpp1
-rw-r--r--src/crepe/system/AnimatorSystem.h3
-rw-r--r--src/crepe/system/RenderSystem.cpp31
-rw-r--r--src/crepe/system/RenderSystem.h17
-rw-r--r--src/example/rendering_particle.cpp28
18 files changed, 223 insertions, 122 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index 464b0fd..b6540cf 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -20,5 +20,8 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a
animator_rect.x = 0;
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 = static_cast<double>(animator_rect.w) / animator_rect.h;
}
Animator::~Animator() { dbg_trace(); }
diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h
index 53f4b91..19c9ebd 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -40,10 +40,6 @@ public:
Animator(uint32_t id, Sprite & spritesheet, int row, int col, int col_animate);
~Animator(); // dbg_trace
- Animator(const Animator &) = delete;
- Animator(Animator &&) = delete;
- Animator & operator=(const Animator &) = delete;
- Animator & operator=(Animator &&) = delete;
private:
//! A reference to the Sprite sheet containing the animation frames.
diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp
index 5835bdd..0831f45 100644
--- a/src/crepe/api/Camera.cpp
+++ b/src/crepe/api/Camera.cpp
@@ -1,3 +1,4 @@
+#include "types.h"
#include "util/Log.h"
#include "Camera.h"
@@ -6,9 +7,14 @@
using namespace crepe;
-Camera::Camera(game_object_id_t id, const Color & bg_color)
+Camera::Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen,
+ const ivec2 & viewport, const double & zoom, const vec2 & offset)
: Component(id),
- bg_color(bg_color) {
+ bg_color(bg_color),
+ offset(offset),
+ screen(screen),
+ viewport(viewport),
+ zoom(zoom) {
dbg_trace();
}
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
index e0cda34..c7b2d08 100644
--- a/src/crepe/api/Camera.h
+++ b/src/crepe/api/Camera.h
@@ -2,6 +2,7 @@
#include "Color.h"
#include "Component.h"
+#include "types.h"
namespace crepe {
@@ -20,33 +21,35 @@ public:
* \param id Unique identifier for the camera component.
* \param bg_color Background color for the camera view.
*/
- Camera(game_object_id_t id, const Color & bg_color);
+ Camera(game_object_id_t id, const Color & bg_color, const ivec2 & screen,
+ const ivec2 & viewport, const double & zoom, const vec2 & offset = {0, 0});
~Camera(); // dbg_trace only
public:
//! Background color of the camera view.
- Color bg_color;
+ const Color bg_color;
- //! Aspect ratio height for the camera.
- double aspect_height = 480;
+ //! offset postion from the game object transform component
+ vec2 offset;
- //! Aspect ratio width for the camera.
- double aspect_width = 640;
+ //! pos the postion of the camera in world space this will be filled with
+ //pos = transform + offset
+ vec2 pos = {0, 0};
- //! X-coordinate of the camera position.
- double x = 0.0;
+ //! screen the display size in pixels ( output resolution )
+ const ivec2 screen = {1080, 720};
- //! Y-coordinate of the camera position.
- double y = 0.0;
+ //! viewport is the area of the world visible through the camera (in world units)
+ const ivec2 viewport = {500, 1000};
//! Zoom level of the camera view.
- double zoom = 1.0;
+ const double zoom = 1.0f;
public:
/**
* \brief Gets the maximum number of camera instances allowed.
* \return Maximum instance count as an integer.
*/
- virtual int get_instances_max() const { return 10; }
+ virtual int get_instances_max() const { return 1; }
};
} // namespace crepe
diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp
index 29bd77a..dc7c15f 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};
+const Color Color::WHITE{0xff, 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 0c9d116..2525120 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -1,6 +1,7 @@
#pragma once
#include "../util/Log.h"
+#include "types.h"
namespace crepe {
@@ -62,6 +63,12 @@ public:
double gravity = 1;
} physics;
+ //! default window settings
+ struct {
+ //TODO make this constexpr because this will never change
+ const ivec2 def_size = {1080, 720};
+ } win_set;
+
//! Asset loading options
struct {
/**
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index bd2d5cf..65c6cc3 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -1,7 +1,7 @@
-#include <memory>
+#include <cmath>
+#include <utility>
#include "../util/Log.h"
-#include "facade/SDLContext.h"
#include "Component.h"
#include "Sprite.h"
@@ -10,16 +10,21 @@
using namespace std;
using namespace crepe;
-Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, const Color & color,
- const FlipSettings & flip)
+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)
: Component(id),
color(color),
flip(flip),
- sprite_image(image) {
+ sprite_image(std::move(image)),
+ sorting_in_layer(sort_layer),
+ order_in_layer(order_layer),
+ height(height) {
+
dbg_trace();
- this->sprite_rect.w = sprite_image->get_width();
- this->sprite_rect.h = sprite_image->get_height();
+ 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;
}
Sprite::~Sprite() { dbg_trace(); }
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index 74a55d4..9d75ab6 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,11 +1,10 @@
#pragma once
-#include <memory>
-
#include "../Component.h"
#include "Color.h"
#include "Texture.h"
+#include <cstdint>
namespace crepe {
@@ -42,9 +41,12 @@ public:
* \param image Shared pointer to the texture for this sprite.
* \param color Color tint applied to the sprite.
* \param flip Flip settings for horizontal and vertical orientation.
+ * \param order_layer decides the sorting in layer of the sprite.
+ * \param sort_layer decides the order in layer of the sprite.
+ * \param height the height of the image in game units
*/
- Sprite(game_object_id_t id, const std::shared_ptr<Texture> image, const Color & color,
- const FlipSettings & flip);
+ Sprite(game_object_id_t id, Texture & image, const Color & color,
+ const FlipSettings & flip, uint8_t sort_layer, uint8_t order_layer, int height);
/**
* \brief Destroys the Sprite instance.
@@ -52,24 +54,29 @@ public:
~Sprite();
//! Texture used for the sprite
- const std::shared_ptr<Texture> sprite_image;
+ const Texture sprite_image;
+
//! Color tint of the sprite
Color color;
+
//! Flip settings for the sprite
FlipSettings flip;
+
//! Layer sorting level of the sprite
- uint8_t sorting_in_layer = 0;
+ const uint8_t sorting_in_layer;
//! Order within the sorting layer
- uint8_t order_in_layer = 0;
+ const uint8_t order_in_layer;
+
+ //! height in world units
+ const int height;
-public:
/**
- * \brief Gets the maximum number of instances allowed for this sprite.
- * \return Maximum instance count as an integer.
+ * \aspect_ratio ratio of the img so that scaling will not become weird
*
- * For now is this number randomly picked. I think it will eventually be 1.
+ * cannot be const because if Animator component is addded then ratio becomes scuffed and
+ * does it need to be calculated again in the Animator
*/
- virtual int get_instances_max() const { return 10; }
+ double aspect_ratio;
private:
//! Reads the sprite_rect of sprite
@@ -82,7 +89,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/api/Texture.cpp b/src/crepe/api/Texture.cpp
index 264d7b1..576bdc3 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -1,5 +1,3 @@
-#include <SDL2/SDL_render.h>
-
#include "../facade/SDLContext.h"
#include "../util/Log.h"
@@ -19,6 +17,17 @@ 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;
+}
+
void Texture::load(const Asset & res) {
SDLContext & ctx = SDLContext::get_instance();
this->texture = ctx.texture_from_path(res.get_path());
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
index b4f7d07..dc4a6d7 100644
--- a/src/crepe/api/Texture.h
+++ b/src/crepe/api/Texture.h
@@ -35,6 +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;
/**
* \brief Gets the width of the texture.
diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h
index c278c87..2b31d90 100644
--- a/src/crepe/api/Vector2.h
+++ b/src/crepe/api/Vector2.h
@@ -31,8 +31,14 @@ struct Vector2 {
//! Divides this vector by another vector element-wise and returns the result.
Vector2 operator/(const Vector2<T> & other) const;
- //! Divides this vector by a scalar and returns the result.
- Vector2 operator/(T scalar) const;
+ //! Multiplies this vector by another vector element-wise and updates this vector.
+ Vector2 & operator*=(const Vector2<T> & other);
+
+ //! Divides a scalar value to both components of this vector and updates this vector.
+ Vector2 operator/(const T & other) const;
+
+ //! Divides a scalar value to both components of this vector and updates this vector.
+ Vector2 operator/(T other) const;
//! Adds another vector to this vector and updates this vector.
Vector2 & operator+=(const Vector2<T> & other);
@@ -46,9 +52,6 @@ struct Vector2 {
//! Subtracts a scalar value from both components of this vector and updates this vector.
Vector2 & operator-=(T other);
- //! Multiplies this vector by another vector element-wise and updates this vector.
- Vector2 & operator*=(const Vector2<T> & other);
-
//! Multiplies this vector by a scalar and updates this vector.
Vector2 & operator*=(T other);
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index b3298a7..0d42a4c 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -10,16 +10,16 @@
#include <functional>
#include <memory>
#include <stdexcept>
-#include <string>
#include "../api/Camera.h"
+#include "../api/Config.h"
#include "../api/Sprite.h"
#include "../api/Texture.h"
#include "../api/Transform.h"
-#include "../api/Vector2.h"
#include "../util/Log.h"
#include "SDLContext.h"
+#include "types.h"
using namespace crepe;
using namespace std;
@@ -31,14 +31,15 @@ SDLContext & SDLContext::get_instance() {
SDLContext::SDLContext() {
dbg_trace();
- // FIXME: read window defaults from config manager
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
throw runtime_error(format("SDLContext: SDL_Init error: {}", SDL_GetError()));
}
+
+ auto & cfg = Config::get_instance().win_set;
SDL_Window * tmp_window
= SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- this->viewport.w, this->viewport.h, 0);
+ cfg.def_size.x, cfg.def_size.y, 0);
if (!tmp_window) {
throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError()));
}
@@ -93,7 +94,10 @@ void SDLContext::handle_events(bool & running) {
*/
}
-void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
+void SDLContext::clear_screen() {
+ SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255);
+ 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) const {
@@ -104,33 +108,34 @@ 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 vec2 & pos,
- const double & scale, const Camera & cam) const {
+SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam,
+ const double & img_scale) const {
+
+ int width = sprite.height * sprite.aspect_ratio;
+ int height = sprite.height;
- double adjusted_w = sprite.sprite_rect.w * scale * cam.zoom;
- double adjusted_h = sprite.sprite_rect.h * scale * cam.zoom;
- double adjusted_x = (pos.x - cam.x) * cam.zoom - adjusted_w / 2;
- double adjusted_y = (pos.y - cam.y) * cam.zoom - adjusted_h / 2;
+ width *= img_scale * cam.zoom;
+ height *= img_scale * cam.zoom;
return SDL_Rect{
- .x = static_cast<int>(adjusted_x),
- .y = static_cast<int>(adjusted_y),
- .w = static_cast<int>(adjusted_w),
- .h = static_cast<int>(adjusted_h),
+ .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)),
+ .w = width,
+ .h = height,
};
}
void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle,
- const double & scale, const Camera & camera) {
+ const double & img_scale, const Camera & cam) {
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, camera);
+ SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam, img_scale);
- SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,
+ SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect,
&dstrect, angle, NULL, render_flip);
}
@@ -141,20 +146,56 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
| (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, transform.scale, cam);
+ SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam, transform.scale);
- SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,
+ SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect,
&dstrect, transform.rotation, NULL, render_flip);
}
void SDLContext::set_camera(const Camera & cam) {
- this->viewport.w = static_cast<int>(cam.aspect_width);
- this->viewport.h = static_cast<int>(cam.aspect_height);
- this->viewport.x = static_cast<int>(cam.x) - (this->viewport.w / 2);
- this->viewport.y = static_cast<int>(cam.y) - (this->viewport.h / 2);
+ // resize window
+ int w, h;
+ SDL_GetWindowSize(this->game_window.get(), &w, &h);
+ if (w != cam.screen.x || h != cam.screen.y) {
+ SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y);
+ }
+
+ double screen_aspect = cam.screen.x / cam.screen.y;
+ double viewport_aspect = cam.viewport.x / cam.viewport.y;
+
+ SDL_Rect view;
+ // calculate black bars
+ if (screen_aspect > viewport_aspect) {
+ // lettorboxing
+ view.h = cam.screen.y / cam.zoom;
+ view.w = cam.screen.y * viewport_aspect;
+ view.x = (cam.screen.x - view.w) / 2;
+ view.y = 0;
+ } else {
+ // pillarboxing
+ view.h = cam.screen.x / viewport_aspect;
+ view.w = cam.screen.x / cam.zoom;
+ view.x = 0;
+ view.y = (cam.screen.y - view.h) / 2;
+ }
+ // set drawing area
+ SDL_RenderSetViewport(this->game_renderer.get(), &view);
+
+ SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y);
+
+ // set bg color
SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
cam.bg_color.b, cam.bg_color.a);
+
+ SDL_Rect bg = {
+ .x = 0,
+ .y = 0,
+ .w = cam.viewport.x,
+ .h = cam.viewport.y,
+ };
+ // fill bg color
+ SDL_RenderFillRect(this->game_renderer.get(), &bg);
}
uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); }
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 20e30b3..35d667d 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -9,9 +9,9 @@
#include <memory>
#include <string>
+#include "../api/Camera.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
-#include "api/Camera.h"
#include "types.h"
@@ -100,14 +100,14 @@ private:
* \param texture Reference to the Texture object.
* \return Width of the texture as an integer.
*/
- int get_width(const Texture &) const;
+ int get_width(const Texture & texture) const;
/**
* \brief Gets the height of a texture.
* \param texture Reference to the Texture object.
* \return Height of the texture as an integer.
*/
- int get_height(const Texture &) const;
+ int get_height(const Texture & texture) const;
private:
//! Will use draw,clear_screen, present_screen, camera.
@@ -117,12 +117,21 @@ private:
* \brief Draws a sprite to the screen using the specified transform and camera.
* \param sprite Reference to the Sprite to draw.
* \param transform Reference to the Transform for positioning.
- * \param camera Reference to the Camera for view adjustments.
+ * \param cam camera of the current scene
*/
- void draw(const Sprite & sprite, const Transform & transform, const Camera & camera);
+ void draw(const Sprite & sprite, const Transform & transform, const Camera & cam);
+ /**
+ * \brief Draws a particle to the screen using the specified parameters
+ *
+ * \param sprite Referenceto the sprite to draw
+ * \param pos particle position in world units
+ * \param angle particle angle in degrees
+ * \param img_scale scalar multiplier to increase image size
+ * \param cam camera of the current scene
+ */
void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle,
- const double & scale, const Camera & camera);
+ const double & img_scale, const Camera & cam);
//! Clears the screen, preparing for a new frame.
void clear_screen();
@@ -144,18 +153,18 @@ private:
* \return sdl rectangle to draw a src image
*/
SDL_Rect get_src_rect(const Sprite & sprite) const;
+
/**
- * \brief calculates the sqaure size of the image for an destination
+ * \brief calculates the sqaure size of the image for destination
*
- * \param sprite Reference to the sprite to calculate the rectangle
- * \param pos the pos in pixel positions
- * \param scale the multiplier to increase of decrease for the specified sprite
- * \param cam Reference to the current camera in the scene to calculate the position based
- * on the camera
+ * \param sprite Reference to the sprite to calculate rectangle
+ * \param pos the pos in world units
+ * \param cam the camera of the current scene
+ * \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 double & scale,
- const Camera & cam) const;
+ SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam,
+ const double & img_scale) const;
private:
//! sdl Window
@@ -163,9 +172,6 @@ private:
//! renderer for the crepe engine
std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer;
-
- //! viewport for the camera window
- SDL_Rect viewport = {0, 0, 640, 480};
};
} // namespace crepe
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp
index 676e485..bc94253 100644
--- a/src/crepe/system/AnimatorSystem.cpp
+++ b/src/crepe/system/AnimatorSystem.cpp
@@ -16,6 +16,7 @@ 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;
diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h
index 56cc7b3..f8179a9 100644
--- a/src/crepe/system/AnimatorSystem.h
+++ b/src/crepe/system/AnimatorSystem.h
@@ -21,12 +21,11 @@ public:
/**
* \brief Updates the Animator components.
*
- * This method is called periodically (likely every frame) to update the state of all
+ * This method is called to update the state of all
* Animator components, moving the animations forward and managing their behavior (e.g.,
* looping).
*/
void update() override;
- // FIXME: never say "likely" in the documentation lmao
};
} // namespace crepe
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index ad510f5..a1443cd 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -2,7 +2,6 @@
#include <cassert>
#include <cmath>
#include <functional>
-#include <iostream>
#include <stdexcept>
#include <vector>
@@ -10,8 +9,8 @@
#include "../api/ParticleEmitter.h"
#include "../api/Sprite.h"
#include "../api/Transform.h"
-#include "../api/Vector2.h"
#include "../facade/SDLContext.h"
+#include "api/Camera.h"
#include "RenderSystem.h"
@@ -21,7 +20,8 @@ using namespace std;
void RenderSystem::clear_screen() { this->context.clear_screen(); }
void RenderSystem::present_screen() { this->context.present_screen(); }
-void RenderSystem::update_camera() {
+
+const Camera & RenderSystem::update_camera() {
ComponentManager & mgr = this->component_manager;
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
@@ -30,9 +30,13 @@ void RenderSystem::update_camera() {
for (Camera & cam : cameras) {
if (!cam.active) continue;
+ const Transform & transform
+ = mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
this->context.set_camera(cam);
- this->curr_cam_ref = &cam;
+ cam.pos = transform.position + cam.offset;
+ return cam;
}
+ throw std::runtime_error("No active cameras in current scene");
}
bool sorting_comparison(const Sprite & a, const Sprite & b) {
@@ -51,12 +55,12 @@ RefVector<Sprite> RenderSystem::sort(RefVector<Sprite> & objs) const {
void RenderSystem::update() {
this->clear_screen();
- this->update_camera();
this->render();
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
+bool RenderSystem::render_particle(const Sprite & sprite, const Camera & cam,
+ const double & scale) {
ComponentManager & mgr = this->component_manager;
@@ -72,19 +76,20 @@ 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, scale,
- *this->curr_cam_ref);
+ this->context.draw_particle(sprite, p.position, p.angle, scale, cam);
}
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) {
- this->context.draw(sprite, tm, *this->curr_cam_ref);
+void RenderSystem::render_normal(const Sprite & sprite, const Camera & cam,
+ const Transform & tm) {
+ this->context.draw(sprite, tm, cam);
}
void RenderSystem::render() {
-
ComponentManager & mgr = this->component_manager;
+ const Camera & cam = this->update_camera();
+
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
RefVector<Sprite> sorted_sprites = this->sort(sprites);
@@ -93,10 +98,10 @@ void RenderSystem::render() {
const Transform & transform
= mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
- bool rendered_particles = this->render_particle(sprite, transform.scale);
+ bool rendered_particles = this->render_particle(sprite, cam, transform.scale);
if (rendered_particles) continue;
- this->render_normal(sprite, transform);
+ this->render_normal(sprite, cam, transform);
}
}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index 30b41cf..46ebb92 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -1,12 +1,11 @@
#pragma once
-#include <functional>
-#include <vector>
+#include <cmath>
#include "facade/SDLContext.h"
#include "System.h"
-#include <cmath>
+#include "types.h"
namespace crepe {
@@ -37,7 +36,7 @@ private:
void present_screen();
//! Updates the active camera used for rendering.
- void update_camera();
+ const Camera & update_camera();
//! Renders the whole screen
void render();
@@ -49,7 +48,7 @@ private:
* \param tm the Transform component for scale
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite & sprite, const double & scale);
+ bool render_particle(const Sprite & sprite, const Camera & cam, const double & scale);
/**
* \brief renders a sprite with a Transform component on the screen
@@ -57,7 +56,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 & sprite, const Transform & tm);
+ void render_normal(const Sprite & sprite, const Camera & cam, const Transform & tm);
/**
* \brief sort a vector sprite objects with
@@ -71,16 +70,10 @@ private:
* \todo Include color handling for sprites.
* \todo Add text rendering using SDL_ttf for text components.
* \todo Implement a text component and a button component.
- * \todo Ensure each sprite is checked for active status before rendering.
- * \todo Sort all layers by order before rendering.
* \todo Consider adding text input functionality.
*/
private:
- //! Pointer to the current active camera for rendering
- Camera * curr_cam_ref = nullptr;
- // TODO: needs a better solution
-
SDLContext & context = SDLContext::get_instance();
};
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index b38e860..6e426cf 100644
--- a/src/example/rendering_particle.cpp
+++ b/src/example/rendering_particle.cpp
@@ -1,6 +1,7 @@
+#include "api/Animator.h"
#include "api/Camera.h"
+#include "system/AnimatorSystem.h"
#include "system/ParticleSystem.h"
-#include "types.h"
#include <SDL2/SDL_timer.h>
#include <crepe/ComponentManager.h>
@@ -12,11 +13,10 @@
#include <crepe/api/Sprite.h>
#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
-#include <crepe/api/Vector2.h>
#include <crepe/system/RenderSystem.h>
+#include <crepe/types.h>
#include <chrono>
-#include <iostream>
#include <memory>
using namespace crepe;
@@ -24,16 +24,21 @@ using namespace std;
int main(int argc, char * argv[]) {
ComponentManager mgr;
- GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0.1);
+ 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);
- Sprite & test_sprite = game_object.add_component<Sprite>(
- make_shared<Texture>("asset/texture/img.png"), color, FlipSettings{false, false});
- test_sprite.order_in_layer = 5;
+ auto img = Texture("asset/texture/test_ap43.png");
+ Sprite & test_sprite
+ = game_object.add_component<Sprite>(img, color, FlipSettings{true, true}, 1, 1, 500);
+ //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,
@@ -53,17 +58,24 @@ int main(int argc, char * argv[]) {
},
.sprite = test_sprite,
});
- game_object.add_component<Camera>(Color::WHITE);
+ */
+
+ auto & cam = game_object.add_component<Camera>(Color::WHITE, ivec2{1080, 720},
+ ivec2{2000, 2000}, 1.0f);
+ /*
game_object
.add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color,
+ .add_component<Sprite>(make_shared<Texture>("asset/texture/img.png"), color,
FlipSettings{false, false})
.order_in_layer
= 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);
}