From 33f673741b084f3a9737a951b2b8f90f3566b0fe Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Sun, 16 Apr 2023 02:43:05 +0100 Subject: [PATCH] Remove draw scale from Camera struct. --- camera.h | 1 - level.cpp | 6 ++++++ level.h | 1 + minimap.cpp | 18 ++++++++++++++---- view.cpp | 4 ++-- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/camera.h b/camera.h index 145b011..158cc04 100644 --- a/camera.h +++ b/camera.h @@ -9,7 +9,6 @@ typedef struct float direction; unsigned int resolution; float fov; - float drawScale; } Camera; void camera_update(Camera* camera, float t); diff --git a/level.cpp b/level.cpp index b1d1842..24d5ce0 100644 --- a/level.cpp +++ b/level.cpp @@ -45,6 +45,12 @@ float level_rayCastDistance(sf::Vector2f point, float direction) return castRay(point, direction); } +void level_getDimensions(unsigned int* width, unsigned int* height) +{ + *width = WIDTH; + *height = HEIGHT; +} + static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize) { for (unsigned int x = 0; x < WIDTH; x++) { diff --git a/level.h b/level.h index 5724dc7..7b97746 100644 --- a/level.h +++ b/level.h @@ -7,5 +7,6 @@ int level_init(); void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize); void level_end(); float level_rayCastDistance(sf::Vector2f point, float direction); +void level_getDimensions(unsigned int* width, unsigned int* height); #endif diff --git a/minimap.cpp b/minimap.cpp index 34d6d9d..ce10320 100644 --- a/minimap.cpp +++ b/minimap.cpp @@ -10,19 +10,29 @@ static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float ang static sf::RenderTexture minimap; +static bool init = false; static unsigned int minimapSize; +static float drawScale; int minimap_init(unsigned int size) { printf("minimap_init()\n"); - minimapSize = size; if (!minimap.create(size, size)) return 0; level_init(); + + unsigned int width, height; + level_getDimensions(&width, &height); + + minimapSize = size; + drawScale = (float)size/(float)width; + + init = true; return 1; } void minimap_update(sf::RenderTarget* renderTarget, Camera* camera) { + if (!init) return; if (!renderTarget || !camera) return; minimap.clear(); level_update(&minimap, minimapSize); @@ -35,7 +45,7 @@ void minimap_update(sf::RenderTarget* renderTarget, Camera* camera) static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera) { - const sf::Vector2f scaledPos = camera->pos * camera->drawScale; + const sf::Vector2f scaledPos = camera->pos * drawScale; const float circleRadius = 0.02f * minimapSize; sf::CircleShape circle(circleRadius); circle.setPosition(scaledPos); @@ -49,7 +59,7 @@ static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera) static void drawRays(sf::RenderTarget* renderTarget, Camera* camera) { - const sf::Vector2f scaledPos = camera->pos * camera->drawScale; + const sf::Vector2f scaledPos = camera->pos * drawScale; float rayDirection = 0; float rayDirectionStep = camera->fov / (float)camera->resolution; @@ -64,7 +74,7 @@ static void drawRays(sf::RenderTarget* renderTarget, Camera* camera) else rayDirection = camera->direction + rayDirectionOffset; - float distance = level_rayCastDistance(camera->pos, rayDirection) * camera->drawScale; + float distance = level_rayCastDistance(camera->pos, rayDirection) * drawScale; drawLine(renderTarget, scaledPos, rayDirection, distance, sf::Color(150, 150, 100)); diff --git a/view.cpp b/view.cpp index 69482ff..2bf6472 100644 --- a/view.cpp +++ b/view.cpp @@ -6,13 +6,13 @@ #include "camera.h" #include "minimap.h" -#define MINIMAP_SIZE 300 +#define MINIMAP_SIZE 250 #define HALF_MINIMAP_SIZE MINIMAP_SIZE/2.f #define DRAW_SCALE MINIMAP_SIZE/5.f static int handleKeyCode(sf::Keyboard::Key key); -static Camera camera = { sf::Vector2f(5.f/2.f, 5.f/2.f), 0.f, 300, 0.5f*PI, DRAW_SCALE }; +static Camera camera = { sf::Vector2f(5.f/2.f, 5.f/2.f), 0.f, 300, 0.5f*PI }; static sf::Uint32 style = sf::Style::Titlebar; static sf::RenderWindow window(sf::VideoMode(MINIMAP_SIZE + camera.resolution, MINIMAP_SIZE), "Raycasting", style);