aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/facade/SDLContext.cpp51
-rw-r--r--src/crepe/facade/SDLContext.h26
-rw-r--r--src/crepe/system/RenderSystem.cpp8
-rw-r--r--src/crepe/system/RenderSystem.h3
-rw-r--r--src/example/rendering_particle.cpp11
5 files changed, 74 insertions, 25 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 40189f6..8131df2 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -1,5 +1,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
+#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
@@ -15,6 +16,8 @@
#include "../api/Texture.h"
#include "../api/Transform.h"
#include "../util/Log.h"
+#include "api/Camera.h"
+#include "api/Vector2.h"
#include "SDLContext.h"
@@ -93,30 +96,54 @@ 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()); }
-void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) {
-
- SDL_RendererFlip render_flip
- = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
- | (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
-
- double adjusted_x = (transform.position.x - cam.x) * cam.zoom;
- double adjusted_y = (transform.position.y - cam.y) * cam.zoom;
- double adjusted_w = sprite.sprite_rect.w * transform.scale * cam.zoom;
- double adjusted_h = sprite.sprite_rect.h * transform.scale * cam.zoom;
- SDL_Rect srcrect = {
+SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) {
+ return SDL_Rect{
.x = sprite.sprite_rect.x,
.y = sprite.sprite_rect.y,
.w = sprite.sprite_rect.w,
.h = sprite.sprite_rect.h,
};
+}
+SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos,
+ const double & scale, const Camera & cam) {
+
+ double adjusted_x = (pos.x - cam.x) * cam.zoom;
+ double adjusted_y = (pos.y - cam.y) * cam.zoom;
+ double adjusted_w = sprite.sprite_rect.w * scale * cam.zoom;
+ double adjusted_h = sprite.sprite_rect.h * scale * cam.zoom;
- SDL_Rect dstrect = {
+ 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),
};
+}
+
+void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos,
+ const double & angle, const double & scale,
+ const Camera & camera) {
+
+ 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_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) {
+
+ 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_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 78ac56b..fb09015 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -1,8 +1,10 @@
#pragma once
#include <SDL2/SDL_keycode.h>
+#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
+#include <cmath>
#include <functional>
#include <memory>
#include <string>
@@ -10,6 +12,7 @@
#include "../api/Sprite.h"
#include "../api/Transform.h"
#include "api/Camera.h"
+#include "api/Vector2.h"
namespace crepe {
@@ -122,8 +125,7 @@ private:
* \param camera Reference to the Camera for view adjustments.
*/
void draw(const Sprite & sprite, const Transform & transform, const Camera & camera);
-
- void draw_particle(const Vector2 & pos, const Camera & camera);
+ void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const double & scale, const Camera & camera);
//! Clears the screen, preparing for a new frame.
void clear_screen();
@@ -138,6 +140,26 @@ private:
void camera(const Camera & camera);
private:
+ /**
+ * \brief calculates the sqaure size of the image
+ *
+ * \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);
+ /**
+ * \brief calculates the sqaure size of the image for an 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
+ * \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);
+
+private:
//! sdl Window
std::unique_ptr<SDL_Window, std::function<void(SDL_Window *)>> game_window;
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 39e0e3f..1ae5ca7 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -27,7 +27,7 @@ void RenderSystem::update_camera() {
}
bool RenderSystem::render_particle(const Sprite & sprite,
- Transform tm) {
+ const double & scale) {
ComponentManager & mgr = this->component_manager;
SDLContext & render = SDLContext::get_instance();
@@ -44,9 +44,7 @@ bool RenderSystem::render_particle(const Sprite & sprite,
for (const Particle & p : em.data.particles) {
if (!p.active) continue;
- tm.position = p.position;
- tm.rotation = p.angle;
- render.draw(em.data.sprite, tm, *curr_cam);
+ render.draw_particle(sprite, p.position, p.angle, scale, *this->curr_cam);
}
}
return rendering_particles;
@@ -68,7 +66,7 @@ void RenderSystem::render() {
if (!sprite.active) continue;
auto transform = mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
- bool rendered_particles = this->render_particle(sprite, transform);
+ bool rendered_particles = this->render_particle(sprite, transform.scale);
if (rendered_particles) continue;
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index c18b80b..6643084 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -5,6 +5,7 @@
#include "api/Transform.h"
#include "System.h"
+#include <cmath>
namespace crepe {
@@ -51,7 +52,7 @@ private:
* \param tm the Transform component for scale
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite &, Transform tm);
+ bool render_particle(const Sprite &, const double & scale);
/**
* \brief renders a sprite with a Transform component on the screen
diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp
index 33013ef..71b50ba 100644
--- a/src/example/rendering_particle.cpp
+++ b/src/example/rendering_particle.cpp
@@ -10,7 +10,6 @@
#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
#include <crepe/system/RenderSystem.h>
-#include <crepe/util/log.h>
#include <crepe/api/Color.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Vector2.h>
@@ -21,7 +20,11 @@ using namespace crepe;
using namespace std;
int main(int argc, char * argv[]) {
- GameObject game_object(0, "", "", Vector2{100, 100}, 0, 0.1);
+ 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,
@@ -47,11 +50,9 @@ int main(int argc, char * argv[]) {
});
game_object.add_component<Camera>(Color::get_white());
- auto & sys = crepe::RenderSystem::get_instance();
- auto sys_part = crepe::ParticleSystem();
auto start = std::chrono::steady_clock::now();
while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
- sys_part.update();
+ psys.update();
sys.update();
SDL_Delay(10 );
}