attampt at adding zoom

This commit is contained in:
PoliEcho 2025-02-19 16:48:08 +01:00
parent d3cb22a278
commit d24f097223
3 changed files with 52 additions and 15 deletions

View File

@ -41,4 +41,6 @@ void init_entity(Entity &entity, SDL_Renderer *renderer, std::string type,
entities[type][name]["height"].get<int>()};
entity.speed = entities[type][name]["speed"].get<float>();
entity.gotoT = false;
}

View File

@ -5,9 +5,10 @@
#include <SDL3_image/SDL_image.h>
#include <cmath>
#include <csignal>
#include <cstdint>
#include <iostream>
#include <vector>
#include <ctime>
#include <iomanip>
std::vector<Entity *> loaded_entities;
sdl_session main_sdl_session;
@ -38,7 +39,7 @@ int main() {
Uint64 lastFrameTime = SDL_GetTicksNS();
float deltaTime = 0.0f;
time_t start_time = std::time(nullptr);
while (running) {
const Uint64 frameStart = SDL_GetTicksNS();
@ -47,16 +48,31 @@ int main() {
// Event handling
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT)
if (event.type == SDL_EVENT_QUIT) {
running = false;
}
if (event.type == SDL_EVENT_MOUSE_WHEEL) {
float zoom_factor = (event.wheel.y > 0) ? 0.9f : 1.1f;
camera.view.w *= zoom_factor;
camera.view.h *= zoom_factor;
// Zoom towards mouse position
SDL_FPoint mouse_world_pos = {
camera.view.x + event.wheel.mouse_x,
camera.view.y + event.wheel.mouse_y
};
camera.view.x = mouse_world_pos.x - (event.wheel.mouse_x * zoom_factor);
camera.view.y = mouse_world_pos.y - (event.wheel.mouse_y * zoom_factor);
}
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
// Check specific button (e.g., left mouse button)
if (event.button.button == SDL_BUTTON_LEFT) {
printf("Left mouse clicked at (%.1f, %.1f)\nCamera position: (%.1f, "
"%.1f)\nTarget position: (%.1f, %.1f)\n",
event.button.x, event.button.y, camera.view.x, camera.view.y,
camera.view.x + event.button.x,
camera.view.y + event.button.y);
std::clog << "Left mouse clicked at ("
<< std::fixed << std::setprecision(1) << event.button.x << ", " << event.button.y << ")\n"
<< "Camera position: (" << camera.view.x << ", " << camera.view.y << ")\n"
<< "Target position: (" << (camera.view.x + event.button.x) << ", "
<< (camera.view.y + event.button.y) << ")\ntime since start: " << (std::time(nullptr) - start_time) << "\n";
destroyer.Tposition.x = camera.view.x + event.button.x;
destroyer.Tposition.y = camera.view.y + event.button.y;
destroyer.gotoT = true;
@ -137,9 +153,6 @@ int main() {
destroyer.angle = std::atan2(dy, dx) * 180 / M_PI + 90;
}
std::clog << "angle: " << destroyer.angle
<< "\ncalc: " << std::atan2(dy, dx) * 180 / M_PI << "\n";
float distance = std::sqrt(dx * dx + dy * dy);
if (distance > 0) {
dx /= distance;
@ -159,7 +172,10 @@ int main() {
}
}
// Camera movement
// Camera handling
camera.view.w = SCREEN_WIDTH / camera.zoom;
camera.view.h = SCREEN_HEIGHT / camera.zoom;
if (camera.followPlayer) {
float targetX =
destroyer.position.x + destroyer.position.w / 2 - camera.view.w / 2;
@ -179,9 +195,11 @@ int main() {
WORLD_WIDTH - destroyer.position.w);
destroyer.position.y = SDL_clamp(destroyer.position.y, 0.0f,
WORLD_HEIGHT - destroyer.position.h);
camera.view.w = SDL_clamp(camera.view.w, SCREEN_WIDTH * camera.zoom, SCREEN_WIDTH * 4.0f);
camera.view.h = SDL_clamp(camera.view.h,SCREEN_HEIGHT * camera.zoom,SCREEN_HEIGHT * 4.0f);
camera.view.x = SDL_clamp(camera.view.x, 0.0f, WORLD_WIDTH - camera.view.w);
camera.view.y =
SDL_clamp(camera.view.y, 0.0f, WORLD_HEIGHT - camera.view.h);
camera.view.y = SDL_clamp(camera.view.y, 0.0f, WORLD_HEIGHT - camera.view.h);
// ANIMATION HERE

View File

@ -59,7 +59,7 @@ struct basic_cords {
struct Entity {
SDL_FRect position;
SDL_FRect Tposition;
bool gotoT;
bool gotoT = false;
SDL_Texture *texture;
SDL_Rect srcRect;
float speed;
@ -80,9 +80,26 @@ struct Entity {
struct Camera {
SDL_FRect view = {0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT};
basic_cords Center_of_view(std::optional<float> x = std::nullopt,
std::optional<float> y = std::nullopt) {
if (x != std::nullopt) {
view.x = *x - view.w / 2;
}
if (y != std::nullopt) {
view.y = *y - view.h / 2;
}
return {view.x + view.w / 2, view.y + view.h / 2};
}
bool followPlayer = false;
float speed = 500.0f;
float smoothness = 0.1f;
float zoom = 1.0f;
float minZoom = 0.5f;
float maxZoom = 3.0f;
float zoomSpeed = 0.1f;
};
struct sdl_session {