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/Camera.h21
-rw-r--r--src/crepe/api/Color.cpp2
-rw-r--r--src/crepe/api/Config.h2
-rw-r--r--src/crepe/api/Sprite.cpp3
-rw-r--r--src/crepe/api/Sprite.h16
-rw-r--r--src/crepe/api/Vector2.h13
-rw-r--r--src/crepe/facade/SDLContext.cpp102
-rw-r--r--src/crepe/facade/SDLContext.h43
-rw-r--r--src/crepe/system/AnimatorSystem.cpp1
-rw-r--r--src/crepe/system/AnimatorSystem.h3
-rw-r--r--src/crepe/system/RenderSystem.cpp8
-rw-r--r--src/crepe/system/RenderSystem.h3
-rw-r--r--src/example/rendering_particle.cpp23
14 files changed, 181 insertions, 62 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index 464b0fd..0043896 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 = (double) animator_rect.w / (double) animator_rect.h;
}
Animator::~Animator() { dbg_trace(); }
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
index e0cda34..137c8ed 100644
--- a/src/crepe/api/Camera.h
+++ b/src/crepe/api/Camera.h
@@ -2,6 +2,7 @@
#include "Color.h"
#include "Component.h"
+#include "api/Vector2.h"
namespace crepe {
@@ -27,20 +28,20 @@ public:
//! Background color of the camera view.
Color bg_color;
- //! Aspect ratio height for the camera.
- double aspect_height = 480;
+ //! pos The position of the camera in world units
+ Vector2 pos = {0, 0};
- //! Aspect ratio width for the camera.
- double aspect_width = 640;
+ //! screen the display size in pixels ( output resolution )
+ //Vector2 screen = {720, 480};
+ Vector2 screen = {1080, 720};
- //! X-coordinate of the camera position.
- double x = 0.0;
-
- //! Y-coordinate of the camera position.
- double y = 0.0;
+ //! viewport is the area of the world visible through the camera (in world units)
+ //Vector2 viewport = {720, 480};
+ Vector2 viewport = {2000, 1000};
+ //! scale scaling factor from world units to pixel coordinates
//! Zoom level of the camera view.
- double zoom = 1.0;
+ double zoom = 1.0f;
public:
/**
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..671fd02 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -32,7 +32,7 @@ public:
*
* Only messages with equal or higher priority than this value will be logged.
*/
- Log::Level level = Log::Level::INFO;
+ Log::Level level = Log::Level::DEBUG;
/**
* \brief Colored log output
*
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index bd2d5cf..c219dd0 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -15,7 +15,8 @@ Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, const Color
: Component(id),
color(color),
flip(flip),
- sprite_image(image) {
+ sprite_image(image),
+ 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/api/Sprite.h b/src/crepe/api/Sprite.h
index 74a55d4..89f9121 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -1,6 +1,7 @@
#pragma once
#include <memory>
+#include <sys/types.h>
#include "../Component.h"
@@ -62,6 +63,19 @@ 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;
+
+ /**
+ * \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:
/**
* \brief Gets the maximum number of instances allowed for this sprite.
@@ -82,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/api/Vector2.h b/src/crepe/api/Vector2.h
index c278c87..019d849 100644
--- a/src/crepe/api/Vector2.h
+++ b/src/crepe/api/Vector2.h
@@ -31,8 +31,17 @@ 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);
+
+ //! Multiplies a scalar value to both components of this vector and updates this vector.
+ Vector2 & operator*=(const Vector2<T> & other);
+
+ //! Divides this vector by another vector element-wise and updates this vector.
+ Vector2 operator/(const Vector2<T> & other) const;
+
+ //! Divides a scalar value to both components of this vector and updates this vector.
+ Vector2 operator/(const T & other) const;
//! Adds another vector to this vector and updates this vector.
Vector2 & operator+=(const Vector2<T> & other);
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index b3298a7..de7d08f 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -8,9 +8,9 @@
#include <cmath>
#include <cstddef>
#include <functional>
+#include <iostream>
#include <memory>
#include <stdexcept>
-#include <string>
#include "../api/Camera.h"
#include "../api/Sprite.h"
@@ -18,6 +18,7 @@
#include "../api/Transform.h"
#include "../api/Vector2.h"
#include "../util/Log.h"
+#include "api/Vector2.h"
#include "SDLContext.h"
@@ -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,57 +108,111 @@ 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 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<int>(sprite.width * cam_scale.x);
+ pixel_height = static_cast<int>(pixel_width / sprite.aspect_ratio);
+ } else {
+ pixel_height = static_cast<int>(sprite.height * cam_scale.y);
+ pixel_width = static_cast<int>(pixel_height * sprite.aspect_ratio);
+ }
+
+ pixel_width *= img_scale;
+ pixel_height *= img_scale;
- 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;
+ int pixel_x = static_cast<int>((pos.x - cam_pos.x + this->viewport.w / 2 - pixel_width / 2));
+ int pixel_y = static_cast<int>((pos.y - cam_pos.y + this->viewport.h / 2 - pixel_height / 2));
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 = pixel_x,
+ .y = pixel_y,
+ .w = pixel_width,
+ .h = pixel_height,
};
}
-void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle,
- const double & scale, const Camera & camera) {
+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) {
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_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 Camera & cam) {
+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, transform.scale, cam);
+ 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);
}
-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);
+void SDLContext::set_camera(const Camera & cam, Vector2 & scale) {
+
+ // resize window
+ if (this->viewport.w != (int) cam.screen.x || this->viewport.h != (int) cam.screen.y) {
+ SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y);
+ this->viewport.h = (int)cam.screen.y;
+ this->viewport.w = (int)cam.screen.x;
+ }
+
+ double screen_aspect = cam.screen.x / cam.screen.y;
+ double viewport_aspect = cam.viewport.x / cam.viewport.y;
+
+ // decide scaling factor for world to screen
+ scale = cam.screen / cam.viewport * cam.zoom;
+
+ SDL_Rect view;
+
+ // calculate black bars
+ if (screen_aspect > viewport_aspect) {
+ // lettorboxing
+ view.w = static_cast<int>(cam.screen.y * viewport_aspect);
+ view.x = static_cast<int>(cam.screen.x - view.w) / 2;
+ view.y = 0;
+ } else {
+ // pillarboxing
+ view.h = static_cast<int>(cam.screen.x / viewport_aspect);
+ view.w = static_cast<int>(cam.screen.x / cam.zoom);
+ view.x = 0;
+ view.y = static_cast<int>(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 = static_cast<int>(cam.viewport.x),
+ .h = static_cast<int>(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..aed5797 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -117,12 +117,25 @@ 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_pos position of the current camera in the scene
+ * \param cam_scale multiplier for the world to screen
*/
- void draw(const Sprite & sprite, const Transform & transform, const Camera & camera);
+ void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos,
+ const Vector2 & cam_scale);
- void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle,
- const double & scale, const Camera & camera);
+ /**
+ * \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 cam_pos camera position in world units
+ * \param img_scale scalar multiplier to increase image size
+ * \param cam_scale camera scalar for world to screen
+ */
+ 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();
@@ -134,7 +147,7 @@ private:
* \brief sets the background of the camera (will be adjusted in future PR)
* \param camera Reference to the Camera object.
*/
- void set_camera(const Camera & camera);
+ void set_camera(const Camera & camera, Vector2 & scale);
private:
/**
@@ -144,18 +157,19 @@ 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_pos the camera position in world units
+ * \param img_scale the image multiplier for increasing img size
+ * \param scale the multiplier for world to screen
* \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 Vector2 & pos, const Vector2 & cam_pos,
+ const double & img_scale, const Vector2 & scale) const;
private:
//! sdl Window
@@ -165,7 +179,8 @@ private:
std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer;
//! viewport for the camera window
- SDL_Rect viewport = {0, 0, 640, 480};
+ //todo change this so that it becomes a vec2 for only width and height
+ SDL_Rect viewport = {0, 0, 1200, 1200};
};
} // 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..1dd1699 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -30,7 +30,7 @@ void RenderSystem::update_camera() {
for (Camera & cam : cameras) {
if (!cam.active) continue;
- this->context.set_camera(cam);
+ this->context.set_camera(cam, this->scale);
this->curr_cam_ref = &cam;
}
}
@@ -72,14 +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, scale,
- *this->curr_cam_ref);
+ 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);
+ 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 30b41cf..f010a83 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -3,6 +3,7 @@
#include <functional>
#include <vector>
+#include "api/Vector2.h"
#include "facade/SDLContext.h"
#include "System.h"
@@ -81,6 +82,8 @@ private:
Camera * curr_cam_ref = nullptr;
// TODO: needs a better solution
+ Vector2 scale;
+
SDLContext & context = SDLContext::get_instance();
};
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index b38e860..49e8e9d 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 "types.h"
#include <SDL2/SDL_timer.h>
@@ -16,7 +18,6 @@
#include <crepe/system/RenderSystem.h>
#include <chrono>
-#include <iostream>
#include <memory>
using namespace crepe;
@@ -24,16 +25,23 @@ 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("", "", Vector2{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});
+ make_shared<Texture>("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<Animator>(test_sprite, 4, 1, 0).active = true;
+
+ /*
auto & test = game_object.add_component<ParticleEmitter>(ParticleEmitter::Data{
.position = {0, 0},
.max_particles = 10,
@@ -53,17 +61,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);
+ cam.pos = {500, 200};
+ /*
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);
}