//EditorCamera.c #include "EditorCamera.h" void Camera_Init(EditorCamera* camera, int…
- User
//EditorCamera.c
#include "EditorCamera.h"
void Camera_Init(EditorCamera* camera, int imageWidth, int imageHeight, int windowWidth, int windowHeight) { camera->zoom = 1.0f; camera->minZoom = 0.1f; // Minimum zoom level camera->maxZoom = 10.0f; // Maximum zoom level
// Initialize camera dimensions based on initial zoom float aspectRatio = (float)windowWidth / windowHeight; camera->w = imageWidth / camera->zoom; camera->h = camera->w / aspectRatio; // Center the camera on the image camera->x = (imageWidth - camera->w) / 2.0f; camera->y = (imageHeight - camera->h) / 2.0f;}
void Camera_HandleEvent(EditorCamera* camera, SDL_Event* event, int imageWidth, int imageHeight) { if (event->type == SDL_MOUSEWHEEL) { // Store previous zoom level for calculations float prevZoom = camera->zoom;
if (event->wheel.y > 0) { // Scroll up - zoom in camera->zoom *= 1.1f; if (camera->zoom > camera->maxZoom) { camera->zoom = camera->maxZoom; } } else if (event->wheel.y < 0) { // Scroll down - zoom out camera->zoom /= 1.1f; if (camera->zoom < camera->minZoom) { camera->zoom = camera->minZoom; } } }}
void Camera_Update(EditorCamera* camera, int windowWidth, int windowHeight, int imageWidth, int imageHeight) { // Update camera dimensions based on zoom and window aspect ratio float aspectRatio = (float)windowWidth / windowHeight;
// Compute desired camera width and height based on zoom camera->w = imageWidth / camera->zoom; camera->h = camera->w / aspectRatio; // Ensure camera size does not exceed image bounds if (camera->w > imageWidth) { camera->w = imageWidth; camera->h = camera->w / aspectRatio; } if (camera->h > imageHeight) { camera->h = imageHeight; camera->w = camera->h * aspectRatio; } // Center the camera on the image camera->x = (imageWidth - camera->w) / 2.0f; camera->y = (imageHeight - camera->h) / 2.0f;} //MapEditor.c
#include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include <stdio.h> #include "EditorCamera.h"
int main(int argc, char* argv[]) { // Инициализация SDL if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Ошибка инициализации SDL: %s\n", SDL_GetError()); return 1; }
// Инициализация SDL_image if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) { printf("Ошибка инициализации SDL_image: %s\n", IMG_GetError()); SDL_Quit(); return 1; } // Создание окна с возможностью изменения размера SDL_Window* window = SDL_CreateWindow("Отображение изображения", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_RESIZABLE); if (!window) { printf("Ошибка создания окна: %s\n", SDL_GetError()); IMG_Quit(); SDL_Quit(); return 1; } // Создание рендерера SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (!renderer) { printf("Ошибка создания рендерера: %s\n", SDL_GetError()); SDL_DestroyWindow(window); IMG_Quit(); SDL_Quit(); return 1; } // Загрузка изображения SDL_Surface* imageSurface = IMG_Load("../Maps/world/provinces.png"); if (!imageSurface) { printf("Ошибка загрузки изображения: %s\n", IMG_GetError()); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); IMG_Quit(); SDL_Quit(); return 1; } // Создание текстуры из поверхности изображения SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, imageSurface); SDL_FreeSurface(imageSurface); // Освобождение поверхности if (!texture) { printf("Ошибка создания текстуры: %s\n", SDL_GetError()); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); IMG_Quit(); SDL_Quit(); return 1; } // Получение размеров изображения int imageWidth, imageHeight; SDL_QueryTexture(texture, NULL, NULL, &imageWidth, &imageHeight); // Инициализация переменных размера окна int windowWidth = 800, windowHeight = 600; // Получение размеров окна SDL_GetWindowSize(window, &windowWidth, &windowHeight); // Создание и инициализация камеры EditorCamera camera; Camera_Init(&camera, imageWidth, imageHeight, windowWidth, windowHeight); // Основной цикл приложения int running = 1; while (running) { SDL_Event event; // Обработка событий while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { running = 0; } else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { // Обновляем размеры окна SDL_GetWindowSize(window, &windowWidth, &windowHeight); } else { // Обработка событий камеры (зум) Camera_HandleEvent(&camera, &event, imageWidth, imageHeight); } } // Обновление камеры Camera_Update(&camera, windowWidth, windowHeight, imageWidth, imageHeight); // Установка исходного прямоугольника на основе позиции и размера камеры SDL_Rect srcRect = { (int)camera.x, (int)camera.y, (int)camera.w, (int)camera.h }; SDL_Rect destRect = { 0, 0, windowWidth, windowHeight }; // Очистка рендерера SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Черный фон SDL_RenderClear(renderer); // Отрисовка текстуры с масштабированием SDL_RenderCopy(renderer, texture, &srcRect, &destRect); // Обновление экрана SDL_RenderPresent(renderer); } // Освобождение ресурсов SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); IMG_Quit(); SDL_Quit(); return 0; - Assistant