aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/facade/SDLContext.cpp39
-rw-r--r--src/crepe/facade/SDLContext.h23
-rw-r--r--src/crepe/system/RenderSystem.cpp23
-rw-r--r--src/crepe/system/RenderSystem.h11
4 files changed, 47 insertions, 49 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index d71a9c8..20bb030 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -2,6 +2,7 @@
#include <SDL2/SDL_blendmode.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_keycode.h>
+#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
@@ -232,15 +233,12 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const {
if (data.size.y == 0 && data.size.x != 0) {
size.y = data.size.x / aspect_ratio;
}
+ size *= cam_aux_data.render_scale * ctx.img_scale * data.scale_offset;
- const CameraValues & cam = ctx.cam;
-
- size *= cam.render_scale * ctx.img_scale * data.scale_offset;
-
- vec2 screen_pos
- = (ctx.pos + data.position_offset - cam.cam_pos + (cam.zoomed_viewport) / 2)
- * cam.render_scale
- - size / 2 + cam.bar_size;
+ vec2 screen_pos = (ctx.pos + data.position_offset - cam_aux_data.cam_pos
+ + (cam_aux_data.zoomed_viewport) / 2)
+ * cam_aux_data.render_scale
+ - size / 2 + cam_aux_data.bar_size;
return SDL_FRect{
.x = screen_pos.x,
@@ -269,7 +267,6 @@ void SDLContext::draw(const RenderContext & ctx) {
SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{
.sprite = ctx.sprite,
.texture = ctx.texture,
- .cam = ctx.cam,
.pos = ctx.pos,
.img_scale = ctx.scale,
});
@@ -281,10 +278,9 @@ void SDLContext::draw(const RenderContext & ctx) {
angle, NULL, render_flip);
}
-SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
+void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) {
const Camera::Data & cam_data = cam.data;
- CameraValues ret_cam;
// resize window
int w, h;
SDL_GetWindowSize(this->game_window.get(), &w, &h);
@@ -292,9 +288,10 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y);
}
- vec2 & zoomed_viewport = ret_cam.zoomed_viewport;
- vec2 & bar_size = ret_cam.bar_size;
- vec2 & render_scale = ret_cam.render_scale;
+ vec2 & zoomed_viewport = this->cam_aux_data.zoomed_viewport;
+ vec2 & bar_size = this->cam_aux_data.bar_size;
+ vec2 & render_scale = this->cam_aux_data.render_scale;
+ this->cam_aux_data.cam_pos = new_pos;
zoomed_viewport = cam.viewport_size * cam_data.zoom;
float screen_aspect = static_cast<float>(cam.screen.x) / cam.screen.y;
@@ -336,8 +333,6 @@ SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) {
// fill bg color
SDL_RenderFillRect(this->game_renderer.get(), &bg);
-
- return ret_cam;
}
std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>>
@@ -373,7 +368,11 @@ ivec2 SDLContext::get_size(const Texture & ctx) {
std::vector<SDLContext::EventData> SDLContext::get_events() {
std::vector<SDLContext::EventData> event_list;
SDL_Event event;
+ const CameraAuxiliaryData & cam = this->cam_aux_data;
while (SDL_PollEvent(&event)) {
+ ivec2 mouse_pos;
+ mouse_pos.x = (event.button.x - cam.bar_size.x) / cam.render_scale.x;
+ mouse_pos.y = (event.button.y - cam.bar_size.y) / cam.render_scale.y;
switch (event.type) {
case SDL_QUIT:
event_list.push_back(EventData{
@@ -397,7 +396,7 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEDOWN,
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x, event.button.y},
+ .mouse_position = mouse_pos,
});
break;
case SDL_MOUSEBUTTONUP: {
@@ -406,21 +405,21 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEUP,
.mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x, event.button.y},
+ .mouse_position = mouse_pos,
});
} break;
case SDL_MOUSEMOTION: {
event_list.push_back(
EventData{.event_type = SDLContext::EventType::MOUSEMOVE,
- .mouse_position = {event.motion.x, event.motion.y},
+ .mouse_position = mouse_pos,
.rel_mouse_move = {event.motion.xrel, event.motion.yrel}});
} break;
case SDL_MOUSEWHEEL: {
event_list.push_back(EventData{
.event_type = SDLContext::EventType::MOUSEWHEEL,
- .mouse_position = {event.motion.x, event.motion.y},
+ .mouse_position = mouse_pos,
// TODO: why is this needed?
.scroll_direction = event.wheel.y < 0 ? -1 : 1,
.scroll_delta = event.wheel.preciseY,
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 46b779f..bcadf87 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -24,7 +24,6 @@ class Texture;
class Mediator;
/**
- * \class SDLContext
* \brief Facade for the SDL library
*
* SDLContext is a singleton that handles the SDL window and renderer, provides methods for
@@ -33,7 +32,7 @@ class Mediator;
class SDLContext {
public:
//! data that the camera component cannot hold
- struct CameraValues {
+ struct CameraAuxiliaryData {
//! zoomed in viewport in game_units
vec2 zoomed_viewport;
@@ -64,7 +63,6 @@ public:
struct RenderContext {
const Sprite & sprite;
const Texture & texture;
- const CameraValues & cam;
const vec2 & pos;
const double & angle;
const double & scale;
@@ -193,18 +191,20 @@ public:
void present_screen();
/**
- * \brief sets the background of the camera (will be adjusted in future PR)
- * \param camera Reference to the Camera object.
- * \return camera data the component cannot store
+ * \brief calculates camera view settings. such as black_bars, zoomed_viewport, scaling and
+ * adjusting window size.
+ *
+ * \note only supports windowed mode.
+ * \param camera Reference to the current Camera object in the scene.
+ * \param new_pos new camera position from transform and offset
*/
- CameraValues set_camera(const Camera & camera);
+ void update_camera_view(const Camera & camera, const vec2 & new_pos);
public:
//! the data needed to construct a sdl dst rectangle
struct DestinationRectangleData {
const Sprite & sprite;
const Texture & texture;
- const CameraValues & cam;
const vec2 & pos;
const double & img_scale;
};
@@ -233,6 +233,13 @@ private:
//! black bars rectangle to draw
SDL_FRect black_bars[2] = {};
+
+ /**
+ * \cam_aux_data extra data that the component cannot hold.
+ *
+ * - this is defined in this class because get_events() needs this information aswell
+ */
+ CameraAuxiliaryData cam_aux_data;
};
} // namespace crepe
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 51340fb..afd9548 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -15,6 +15,7 @@
#include "../manager/ResourceManager.h"
#include "RenderSystem.h"
+#include "types.h"
using namespace crepe;
using namespace std;
@@ -29,7 +30,7 @@ void RenderSystem::present_screen() {
ctx.present_screen();
}
-SDLContext::CameraValues RenderSystem::update_camera() {
+void RenderSystem::update_camera() {
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & ctx = this->mediator.sdl_context;
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
@@ -40,9 +41,9 @@ SDLContext::CameraValues RenderSystem::update_camera() {
if (!cam.active) continue;
const Transform & transform
= mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
- SDLContext::CameraValues cam_val = ctx.set_camera(cam);
- cam_val.cam_pos = transform.position + cam.data.postion_offset;
- return cam_val;
+ vec2 new_camera_pos = transform.position + cam.data.postion_offset;
+ ctx.update_camera_view(cam, new_camera_pos);
+ return;
}
throw std::runtime_error("No active cameras in current scene");
}
@@ -69,8 +70,7 @@ void RenderSystem::update() {
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const double & scale) {
+bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & ctx = this->mediator.sdl_context;
@@ -93,7 +93,6 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
.texture = res,
- .cam = cam,
.pos = p.position,
.angle = p.angle,
.scale = scale,
@@ -102,8 +101,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const Transform & tm) {
+void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) {
SDLContext & ctx = this->mediator.sdl_context;
ResourceManager & resource_manager = this->mediator.resource_manager;
const Texture & res = resource_manager.get<Texture>(sprite.source);
@@ -111,7 +109,6 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera
ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
.texture = res,
- .cam = cam,
.pos = tm.position,
.angle = tm.rotation,
.scale = tm.scale,
@@ -120,7 +117,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera
void RenderSystem::render() {
ComponentManager & mgr = this->mediator.component_manager;
- const SDLContext::CameraValues & cam = this->update_camera();
+ this->update_camera();
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
RefVector<Sprite> sorted_sprites = this->sort(sprites);
@@ -130,10 +127,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, cam, transform.scale);
+ bool rendered_particles = this->render_particle(sprite, transform.scale);
if (rendered_particles) continue;
- this->render_normal(sprite, cam, transform);
+ this->render_normal(sprite, transform);
}
}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index e270a6b..fc7b46e 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -2,8 +2,6 @@
#include <cmath>
-#include "facade/SDLContext.h"
-
#include "System.h"
#include "types.h"
@@ -14,7 +12,6 @@ class Sprite;
class Transform;
/**
- * \class RenderSystem
* \brief Manages rendering operations for all game objects.
*
* RenderSystem is responsible for rendering, clearing and presenting the screen, and
@@ -37,7 +34,7 @@ private:
void present_screen();
//! Updates the active camera used for rendering.
- SDLContext::CameraValues update_camera();
+ void update_camera();
//! Renders the whole screen
void render();
@@ -52,8 +49,7 @@ private:
* constructor is now protected i cannot make tmp inside
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const double & scale);
+ bool render_particle(const Sprite & sprite, const double & scale);
/**
* \brief renders a sprite with a Transform component on the screen
@@ -61,8 +57,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 SDLContext::CameraValues & cam,
- const Transform & tm);
+ void render_normal(const Sprite & sprite, const Transform & tm);
/**
* \brief sort a vector sprite objects with