// ECamera.c #include "ECamera.h" #include <math.h> // Линейная интерполяция между a и b…
- User
// ECamera.c #include "ECamera.h" #include <math.h>
// Линейная интерполяция между a и b на t static float lerp(float a, float b, float t) { return a + (b - a) * t; }
void Camera_Init(Camera* cam, int windowW, int windowH, int imgW, int imgH) { // Расчет минимального зума, чтобы окно просмотра не превышало размер изображения float minZoomWidth = (float)windowW / imgW; float minZoomHeight = (float)windowH / imgH; cam->minZoom = minZoomWidth > minZoomHeight ? minZoomWidth : minZoomHeight;
cam->zoom = cam->minZoom; cam->targetZoom = cam->zoom; cam->maxZoom = 3.0f; // Можно настроить по необходимости // Установка начальной позиции камеры в центр изображения cam->x = (imgW - windowW / cam->zoom) / 2.0f; cam->y = (imgH - windowH / cam->zoom) / 2.0f; cam->windowWidth = windowW; cam->windowHeight = windowH; cam->imageWidth = imgW; cam->imageHeight = imgH;}
void Camera_SetTargetZoom(Camera* cam, float zoom) { if (zoom < cam->minZoom) zoom = cam->minZoom; if (zoom > cam->maxZoom) zoom = cam->maxZoom; cam->targetZoom = zoom; }
void Camera_Update(Camera* cam, float deltaTime) { // Параметры плавности float zoomSpeed = 5.0f; // Скорость изменения масштаба
// Плавное изменение зума с использованием LERP if (fabsf(cam->targetZoom - cam->zoom) > 0.001f) { cam->zoom = lerp(cam->zoom, cam->targetZoom, zoomSpeed * deltaTime); // Ограничение зума if ((cam->zoom > cam->targetZoom && cam->zoom < cam->targetZoom + 0.001f) || (cam->zoom < cam->targetZoom && cam->zoom > cam->targetZoom - 0.001f)) { cam->zoom = cam->targetZoom; } // После зумирования необходимо ограничить позицию камеры float viewW = cam->windowWidth / cam->zoom; float viewH = cam->windowHeight / cam->zoom; if (cam->x < 0) cam->x = 0; if (cam->y < 0) cam->y = 0; if (cam->x + viewW > cam->imageWidth) cam->x = cam->imageWidth - viewW; if (cam->y + viewH > cam->imageHeight) cam->y = cam->imageHeight - viewH; }}
void Camera_Move(Camera* cam, float dx, float dy) { // Скорость перемещения обратно-пропорциональна зуму float speed = 1.0f / cam->zoom; cam->x += dx * speed; cam->y += dy * speed;
// Ограничение перемещения камеры внутри границ изображения float viewW = cam->windowWidth / cam->zoom; float viewH = cam->windowHeight / cam->zoom; if (cam->x < 0) cam->x = 0; if (cam->y < 0) cam->y = 0; if (cam->x + viewW > cam->imageWidth) cam->x = cam->imageWidth - viewW; if (cam->y + viewH > cam->imageHeight) cam->y = cam->imageHeight - viewH;}
SDL_Rect Camera_GetViewRect(Camera* cam) { SDL_Rect view; view.x = (int)cam->x; view.y = (int)cam->y; view.w = (int)(cam->windowWidth / cam->zoom); view.h = (int)(cam->windowHeight / cam->zoom);
// Корректировка, если размер выходит за границы изображения if (view.x + view.w > cam->imageWidth) { view.x = cam->imageWidth - view.w; } if (view.y + view.h > cam->imageHeight) { view.y = cam->imageHeight - view.h; } if (view.x < 0) view.x = 0; if (view.y < 0) view.y = 0; return view; - Assistant