aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-22 17:08:55 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-22 17:08:55 +0100
commit8513b2dad0ec43678f17cca0510db4d1a938279a (patch)
tree8437473da7eb9c7fae6369d2767fcd305fab856f /src
parent4ce924b1b1322ee4da3ba50d6da856ad13a2190b (diff)
working moving camera, flip animations, resizing with animations, and black bars
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Animator.cpp5
-rw-r--r--src/crepe/api/Sprite.h10
-rw-r--r--src/crepe/facade/SDLContext.cpp25
-rw-r--r--src/crepe/facade/SDLContext.h6
-rw-r--r--src/crepe/system/RenderSystem.cpp4
-rw-r--r--src/example/rendering_particle.cpp11
6 files changed, 38 insertions, 23 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp
index d206428..178b165 100644
--- a/src/crepe/api/Animator.cpp
+++ b/src/crepe/api/Animator.cpp
@@ -18,7 +18,10 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a
animator_rect.h /= col;
animator_rect.w /= row;
animator_rect.x = 0;
- animator_rect.y = (col_animator - 1) * animator_rect.h;
+ 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/Sprite.h b/src/crepe/api/Sprite.h
index 66599c9..89f9121 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -68,7 +68,13 @@ public:
//! height in world units
int height = 0;
- const double aspect_ratio;
+ /**
+ * \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:
/**
@@ -90,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/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index f49539c..29a8195 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -5,11 +5,9 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
-#include <algorithm>
#include <cmath>
#include <cstddef>
#include <functional>
-#include <iostream>
#include <memory>
#include <stdexcept>
@@ -109,21 +107,24 @@ 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 Vector2 & pos,
- const Vector2 & scale) 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 * scale.x);
+ 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 * scale.y);
+ pixel_height = static_cast<int>(sprite.height * cam_scale.y);
pixel_width = static_cast<int>(pixel_height * sprite.aspect_ratio);
}
- int pixel_x = static_cast<int>((pos.x - pixel_width / 2));
- int pixel_y = static_cast<int>((pos.y - pixel_height / 2));
+ pixel_width *= img_scale;
+ pixel_height *= img_scale;
+
+ int pixel_x = static_cast<int>((pos.x - cam_pos.x - pixel_width / 2));
+ int pixel_y = static_cast<int>((pos.y - cam_pos.y - pixel_height / 2));
return SDL_Rect{
.x = pixel_x,
@@ -134,27 +135,27 @@ 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 Vector2 & scale) {
+ 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);
+ 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 Vector2 & scale) {
+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, scale);
+ 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);
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 68d1630..04c60cc 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -118,9 +118,9 @@ 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 Vector2 & scale);
+ void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale);
- void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & scale);
+ 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();
@@ -152,7 +152,7 @@ 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 Vector2 & scale) 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
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index a16fbb5..676ded6 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -72,13 +72,13 @@ 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, this->scale * scale);
+ 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->scale * tm.scale);
+ this->context.draw(sprite, tm , this->curr_cam_ref->pos,this->scale);
}
void RenderSystem::render() {
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index 741c985..6a91c19 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 <SDL2/SDL_timer.h>
#include <crepe/ComponentManager.h>
@@ -15,7 +17,6 @@
#include <crepe/system/RenderSystem.h>
#include <chrono>
-#include <iostream>
#include <memory>
using namespace crepe;
@@ -23,18 +24,21 @@ using namespace std;
int main(int argc, char * argv[]) {
ComponentManager mgr;
- GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 0, 2);
+ GameObject game_object = mgr.new_object("", "", Vector2{1000, 500}, 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/test_ap43.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{
@@ -71,6 +75,7 @@ int main(int argc, char * argv[]) {
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);
}