Files
sinbad/src/engineCamera.c
T

61 lines
1.7 KiB
C

#include "engineCamera.h"
#include <raymath.h>
Rectangle GetCameraRectangle(Camera2D *camera)
{
float screenWidth = GetScreenWidth();
float screenHeight = GetScreenHeight();
float viewWidth = 0.0f;
float viewHeight = 0.0f;
float viewX = 0.0f;
float viewY = 0.0f;
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
};
}
Camera2D CreateCamera(Vector2 playerPosition, float initZoom){
int windowWidth = GetScreenWidth();
int windowHeight = GetScreenHeight();
return (Camera2D){
.offset = (Vector2){windowWidth / 2.0f, windowHeight / 2.0f},
.target = playerPosition,
.rotation = 0.0f,
.zoom = initZoom
};
}
void UpdateCameraTarget(Camera2D *camera, Vector2 *position, const float smoothness){
camera->target = Vector2Lerp(camera->target, *position, smoothness * GetFrameTime());
}
void UpdateCameraZoom(Camera2D *camera, const float *scaleFactor, const float *minZoom, const float *maxZoom){
float targetZoom = Clamp(camera->zoom * *scaleFactor, *minZoom, *maxZoom);
camera->zoom = Lerp(camera->zoom, targetZoom, 0.1f);
}
void UpdateCamera2D(Camera2D *camera, Vector2* position, Mouse* mouse){
UpdateCameraTarget(camera, position, 5.0f);
UpdateCameraZoom(camera, &mouse->scaleFactor, &mouse->minZoom, &mouse->maxZoom);
}