feat: add camera singleton

This commit is contained in:
huaqo-games
2025-11-06 11:27:43 +01:00
parent 55005b0801
commit b059993c29
+49 -7
View File
@@ -1,9 +1,49 @@
#include "engine.h" #include "engine.h"
Camera2D CreateCamera(Vector2 playerPosition, float initZoom){ Camera2D* GetCamera(void){
static Camera2D camera;
return &camera;
}
Rectangle GetCameraRectangle(void)
{
Camera2D *camera = GetCamera();
float screenWidth = GetScreenWidth();
float screenHeight = GetScreenHeight();
float viewWidth = 0.0f;
float viewHeight = 0.0f;
float viewX = 0.0f;
float viewY = 0.0f;
printf("\n CameraZoom: %f \n", camera->zoom);
if (camera->zoom < 0.5f)
{
viewWidth = screenWidth;
viewHeight = screenHeight;
viewX = camera->target.x - camera->offset.x;
viewY = camera->target.y - camera->offset.y;
} else
{
viewWidth = screenWidth / camera->zoom;
viewHeight = screenHeight / camera->zoom;
viewX = camera->target.x - (camera->offset.x / camera->zoom);
viewY = camera->target.y - (camera->offset.y / camera->zoom);
}
return (Rectangle){
.x = viewX,
.y = viewY,
.width = viewWidth,
.height = viewHeight
};
}
void CreateCamera(Vector2 playerPosition, float initZoom){
Camera2D* camera = GetCamera();
int windowWidth = GetScreenWidth(); int windowWidth = GetScreenWidth();
int windowHeight = GetScreenHeight(); int windowHeight = GetScreenHeight();
return (Camera2D){ *camera = (Camera2D){
.offset = (Vector2){windowWidth / 2.0f, windowHeight / 2.0f}, .offset = (Vector2){windowWidth / 2.0f, windowHeight / 2.0f},
.target = playerPosition, .target = playerPosition,
.rotation = 0.0f, .rotation = 0.0f,
@@ -11,17 +51,19 @@ Camera2D CreateCamera(Vector2 playerPosition, float initZoom){
}; };
} }
void UpdateCameraTarget(Camera2D *camera, Vector2 *position, const float smoothness){ void UpdateCameraTarget(Vector2 *position, const float smoothness){
Camera2D* camera = GetCamera();
camera->target = Vector2Lerp(camera->target, *position, smoothness * GetFrameTime()); camera->target = Vector2Lerp(camera->target, *position, smoothness * GetFrameTime());
} }
void UpdateCameraZoom(Camera2D *camera, const float *scaleFactor, const float *minZoom, const float *maxZoom){ void UpdateCameraZoom(const float *scaleFactor, const float *minZoom, const float *maxZoom){
Camera2D *camera = GetCamera();
float targetZoom = Clamp(camera->zoom * *scaleFactor, *minZoom, *maxZoom); float targetZoom = Clamp(camera->zoom * *scaleFactor, *minZoom, *maxZoom);
camera->zoom = Lerp(camera->zoom, targetZoom, 0.1f); camera->zoom = Lerp(camera->zoom, targetZoom, 0.1f);
} }
void UpdateCamera2D(Camera2D* camera, Vector2* position, Mouse* mouse){ void UpdateCamera2D(Vector2* position, Mouse* mouse){
UpdateCameraTarget(camera, position, 5.0f); UpdateCameraTarget(position, 5.0f);
UpdateCameraZoom(camera, &mouse->scaleFactor, &mouse->minZoom, &mouse->maxZoom); UpdateCameraZoom(&mouse->scaleFactor, &mouse->minZoom, &mouse->maxZoom);
} }