aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-02 21:00:54 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-12-02 21:00:54 +0100
commit0a1739de21e5ab270cb45efb6fc0aed092d81227 (patch)
treeab4019d595fce2410c5ca5d0fd98ad6d8205c079 /src/crepe
parenta84ca09e97d466643f022acfffcf4c6a77f42052 (diff)
added all the contructor strutcts for animator,sprite and camera
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/Animator.cpp15
-rw-r--r--src/crepe/api/Animator.h8
-rw-r--r--src/crepe/api/Camera.cpp1
-rw-r--r--src/crepe/facade/SDLContext.cpp35
-rw-r--r--src/crepe/facade/SDLContext.h2
-rw-r--r--src/crepe/system/AnimatorSystem.cpp6
-rw-r--r--src/crepe/system/RenderSystem.cpp2
7 files changed, 37 insertions, 32 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index ce824e6..1234967 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -9,8 +9,7 @@ using namespace crepe;
Animator::Animator(game_object_id_t id, const Animator::Data & ctx)
: Component(id),
- data(ctx)
-{
+ data(ctx) {
dbg_trace();
this->data.spritesheet.mask.h /= this->data.col;
@@ -23,21 +22,33 @@ Animator::Animator(game_object_id_t id, const Animator::Data & ctx)
ss.data.aspect_ratio
= static_cast<double>(this->data.spritesheet.mask.w) / this->data.spritesheet.mask.h;
}
+
Animator::~Animator() { dbg_trace(); }
void Animator::loop() { this->data.looping = true; }
+
void Animator::play() { this->active = true; }
+
void Animator::pause() { this->active = false; }
+
void Animator::stop() {
this->active = false;
this->data.curr_col = 0;
this->data.curr_row = 0;
}
void Animator::set_fps(int fps) { this->data.fps = fps; }
+
void Animator::set_cycle_range(int start, int end) {
this->data.cycle_start = start, this->data.cycle_end = end;
}
+
void Animator::set_anim(int col) {
this->data.curr_row = 0;
this->data.curr_col = col;
}
+
+void Animator::next_anim() {
+ Animator::Data & ctx = this->data;
+ ctx.curr_row = ctx.curr_row++ % ctx.row;
+ ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w;
+}
diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h
index 194a9cf..36bc9f4 100644
--- a/src/crepe/api/Animator.h
+++ b/src/crepe/api/Animator.h
@@ -47,6 +47,7 @@ public:
//! offset in pixels.
+ // TODO implement
int offset_x = 0;
};
@@ -108,12 +109,7 @@ public:
* \brief Constructs an Animator object that will control animations for a sprite sheet.
*
* \param id The unique identifier for the component, typically assigned automatically.
- * \param spritesheet A reference to the Sprite object which holds the sprite sheet for
- * animation.
- * \param row The maximum number of rows in the sprite sheet.
- * \param col The maximum number of columns in the sprite sheet.
- * \param col_animate The specific col index of the sprite sheet to animate. This allows
- * selecting which col to animate from multiple col in the sheet.
+ * \param ctx animator data
*
* This constructor sets up the Animator with the given parameters, and initializes the
* animation system.
diff --git a/src/crepe/api/Camera.cpp b/src/crepe/api/Camera.cpp
index 27bcb35..ff41710 100644
--- a/src/crepe/api/Camera.cpp
+++ b/src/crepe/api/Camera.cpp
@@ -7,7 +7,6 @@
using namespace crepe;
Camera::Camera(game_object_id_t id, const Data & ctx) : Component(id), data(ctx) {
-
dbg_trace();
}
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index ab3fa45..32461f2 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -156,53 +156,54 @@ void SDLContext::draw(const RenderContext & ctx) {
void SDLContext::set_camera(const Camera & cam, CameraValues & ctx) {
+ const Camera::Data & cam_data = cam.data;
// 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);
+ if (w != cam_data.screen.x || h != cam_data.screen.y) {
+ SDL_SetWindowSize(this->game_window.get(), cam_data.screen.x, cam_data.screen.y);
}
vec2 & zoomed_viewport = ctx.zoomed_viewport;
vec2 & bar_size = ctx.bar_size;
vec2 & render_scale = ctx.render_scale;
- zoomed_viewport = cam.viewport_size * cam.zoom;
- double screen_aspect = static_cast<double>(cam.screen.x) / cam.screen.y;
+ zoomed_viewport = cam_data.viewport_size * cam_data.zoom;
+ double screen_aspect = static_cast<double>(cam_data.screen.x) / cam_data.screen.y;
double viewport_aspect = zoomed_viewport.x / zoomed_viewport.y;
// calculate black bars
if (screen_aspect > viewport_aspect) {
// pillarboxing
- float scale = cam.screen.y / zoomed_viewport.y;
+ float scale = cam_data.screen.y / zoomed_viewport.y;
float adj_width = zoomed_viewport.x * scale;
- float bar_width = (cam.screen.x - adj_width) / 2;
- this->black_bars[0] = {0, 0, bar_width, (float) cam.screen.y};
- this->black_bars[1] = {(cam.screen.x - bar_width), 0, bar_width, (float) cam.screen.y};
+ float bar_width = (cam_data.screen.x - adj_width) / 2;
+ this->black_bars[0] = {0, 0, bar_width, (float) cam_data.screen.y};
+ this->black_bars[1] = {(cam_data.screen.x - bar_width), 0, bar_width, (float) cam_data.screen.y};
bar_size = {bar_width, 0};
render_scale.x = render_scale.y = scale;
} else {
// letterboxing
- float scale = cam.screen.x / (cam.viewport_size.x * cam.zoom);
- float adj_height = cam.viewport_size.y * scale;
- float bar_height = (cam.screen.y - adj_height) / 2;
- this->black_bars[0] = {0, 0, (float) cam.screen.x, bar_height};
+ float scale = cam_data.screen.x / (cam_data.viewport_size.x * cam_data.zoom);
+ float adj_height = cam_data.viewport_size.y * scale;
+ float bar_height = (cam_data.screen.y - adj_height) / 2;
+ this->black_bars[0] = {0, 0, (float) cam_data.screen.x, bar_height};
this->black_bars[1]
- = {0, (cam.screen.y - bar_height), (float) cam.screen.x, bar_height};
+ = {0, (cam_data.screen.y - bar_height), (float) cam_data.screen.x, bar_height};
bar_size = {0, bar_height};
render_scale.x = render_scale.y = scale;
}
- SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
- cam.bg_color.b, cam.bg_color.a);
+ SDL_SetRenderDrawColor(this->game_renderer.get(), cam_data.bg_color.r, cam_data.bg_color.g,
+ cam_data.bg_color.b, cam_data.bg_color.a);
SDL_Rect bg = {
.x = 0,
.y = 0,
- .w = cam.screen.x,
- .h = cam.screen.y,
+ .w = cam_data.screen.x,
+ .h = cam_data.screen.y,
};
// fill bg color
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 0a6fce6..a0d1da8 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -128,7 +128,7 @@ private:
/**
* \brief Draws a sprite to the screen using the specified transform and camera.
- * \param RenderCtx Reference to rendering data to draw
+ * \param RenderContext Reference to rendering data to draw
*/
void draw(const RenderContext & ctx);
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp
index b1f23d1..ff8d2ce 100644
--- a/src/crepe/system/AnimatorSystem.cpp
+++ b/src/crepe/system/AnimatorSystem.cpp
@@ -4,7 +4,6 @@
#include "AnimatorSystem.h"
#include "ComponentManager.h"
-#include <iostream>
using namespace crepe;
@@ -21,7 +20,7 @@ void AnimatorSystem::update() {
Animator::Data & ctx = a.data;
double frame_duration = 1.0f / ctx.fps;
- int cycle_end = (ctx.cycle_end == -1) ? ctx.row : cycle_end;
+ int cycle_end = (ctx.cycle_end == -1) ? ctx.row : ctx.cycle_end;
int total_frames = cycle_end - ctx.cycle_start;
int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames;
@@ -30,8 +29,7 @@ void AnimatorSystem::update() {
ctx.spritesheet.mask.x = ctx.curr_row * ctx.spritesheet.mask.w;
ctx.spritesheet.mask.y = (ctx.curr_col * ctx.spritesheet.mask.h);
- std::cout << curr_frame << " " << total_frames << std::endl;
- if (!ctx.looping && curr_frame == 0) {
+ if (!ctx.looping && curr_frame == total_frames - 1) {
a.active = false;
}
}
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 08f254f..1bf5f65 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -33,7 +33,7 @@ const Camera & RenderSystem::update_camera() {
const Transform & transform
= mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
this->context.set_camera(cam, this->cam_ctx);
- this->cam_ctx.cam_pos = transform.position + cam.offset;
+ this->cam_ctx.cam_pos = transform.position + cam.data.offset;
return cam;
}
throw std::runtime_error("No active cameras in current scene");