kubo/kubo_camera.c

47 lines
1.5 KiB
C

/*
* Copyright Luka Jankovic 2025
*
* This file is part of Kubo.
*
* Kubo is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Kubo is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Kubo. If not, see <https://www.gnu.org/licenses/>.
*/
#include "kubo_camera.h"
void kubo_camera_zoom(Camera2D *camera) {
float wheel = GetMouseWheelMove();
if (wheel != 0) {
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), *camera);
camera->offset = GetMousePosition();
camera->target = mouseWorldPos;
float scale = 0.2f * wheel;
camera->zoom = Clamp(expf(logf(camera->zoom) + scale), 0.125f, 64.0f);
}
}
void kubo_camera_pan(Camera2D *camera) {
Vector2 delta = GetMouseDelta();
delta = Vector2Scale(delta, -1.0f / camera->zoom);
kubo_camera_shift(camera, delta);
}
void kubo_camera_shift(Camera2D *camera, Vector2 delta) {
camera->target = Vector2Add(camera->target, delta);
}
void kubo_camera_reset(Camera2D *camera) {
camera->target = (Vector2){.x = 0, .y = 0};
camera->offset = (Vector2){.x = 0, .y = 0};
camera->zoom = 1.0f;
}