From 2b35e8f51a3536b62ea21dc82deec1e3b65568f6 Mon Sep 17 00:00:00 2001
From: heavydemon21 <nielsstunnebrink1@gmail.com>
Date: Wed, 27 Nov 2024 11:56:48 +0100
Subject: make format and offset for the cameara relative from the transform
 gameobject

---
 src/crepe/api/Camera.h            | 12 +++++++-----
 src/crepe/facade/SDLContext.cpp   | 11 +++++------
 src/crepe/facade/SDLContext.h     |  3 +--
 src/crepe/system/RenderSystem.cpp |  3 +++
 4 files changed, 16 insertions(+), 13 deletions(-)

(limited to 'src/crepe')

diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h
index 151e5d9..1505107 100644
--- a/src/crepe/api/Camera.h
+++ b/src/crepe/api/Camera.h
@@ -28,17 +28,19 @@ public:
 	//! Background color of the camera view.
 	Color bg_color;
 
-	//! pos The position of the camera in world units
-	vec2 pos = {0,0};
+	//! offset postion from the game object transform component
+	vec2 offset = {0, 0};
+
+	//! pos the postion of the camera in world space this will be filled with
+	//pos = transform + offset
+	vec2 pos = {0, 0};
 
 	//! screen the display size in pixels ( output resolution )
-	ivec2 screen = {1080,720};
+	ivec2 screen = {1080, 720};
 
 	//! viewport is the area of the world visible through the camera (in world units)
-	//vec2 viewport = {1000, 2000};
 	ivec2 viewport = {500, 1000};
 
-	//! scale scaling factor from world units to pixel coordinates
 	//! Zoom level of the camera view.
 	double zoom = 1.0f;
 
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 72542e8..4887d35 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -40,7 +40,7 @@ SDLContext::SDLContext() {
 	auto & cfg = Config::get_instance().win_set;
 	SDL_Window * tmp_window
 		= SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
-						   cfg.def_size.x,cfg.def_size.y, 0);
+						   cfg.def_size.x, cfg.def_size.y, 0);
 	if (!tmp_window) {
 		throw runtime_error(format("SDLContext: SDL_Window error: {}", SDL_GetError()));
 	}
@@ -134,7 +134,7 @@ void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const do
 							  | (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
 
 	SDL_Rect srcrect = this->get_src_rect(sprite);
-	SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , img_scale);
+	SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam, img_scale);
 
 	SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect,
 					 &dstrect, angle, NULL, render_flip);
@@ -147,8 +147,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
 							  | (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, cam, transform.scale);
+	SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam, transform.scale);
 
 	SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image.texture.get(), &srcrect,
 					 &dstrect, transform.rotation, NULL, render_flip);
@@ -157,9 +156,9 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
 void SDLContext::set_camera(const Camera & cam) {
 
 	// resize window
-	int w,h;
+	int w, h;
 	SDL_GetWindowSize(this->game_window.get(), &w, &h);
-	if ( w != cam.screen.x || h != cam.screen.y) {
+	if (w != cam.screen.x || h != cam.screen.y) {
 		SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y);
 	}
 
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 2e40b6f..35d667d 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -9,9 +9,9 @@
 #include <memory>
 #include <string>
 
+#include "../api/Camera.h"
 #include "../api/Sprite.h"
 #include "../api/Transform.h"
-#include "../api/Camera.h"
 
 #include "types.h"
 
@@ -172,7 +172,6 @@ private:
 
 	//! renderer for the crepe engine
 	std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer;
-
 };
 
 } // namespace crepe
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index beed9ce..9a8c1ba 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -28,8 +28,11 @@ void RenderSystem::update_camera() {
 
 	for (Camera & cam : cameras) {
 		if (!cam.active) continue;
+		const Transform & transform
+			= mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
 		this->context.set_camera(cam);
 		this->curr_cam_ref = &cam;
+		this->curr_cam_ref->pos = transform.position + this->curr_cam_ref->offset;
 	}
 }
 
-- 
cgit v1.2.3