Remove draw scale from Camera struct.

This commit is contained in:
Sheldon Lee 2023-04-16 02:43:05 +01:00
parent bce640219f
commit 33f673741b
5 changed files with 23 additions and 7 deletions

View File

@ -9,7 +9,6 @@ typedef struct
float direction; float direction;
unsigned int resolution; unsigned int resolution;
float fov; float fov;
float drawScale;
} Camera; } Camera;
void camera_update(Camera* camera, float t); void camera_update(Camera* camera, float t);

View File

@ -45,6 +45,12 @@ float level_rayCastDistance(sf::Vector2f point, float direction)
return castRay(point, 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) static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize)
{ {
for (unsigned int x = 0; x < WIDTH; x++) { for (unsigned int x = 0; x < WIDTH; x++) {

View File

@ -7,5 +7,6 @@ int level_init();
void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize); void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize);
void level_end(); void level_end();
float level_rayCastDistance(sf::Vector2f point, float direction); float level_rayCastDistance(sf::Vector2f point, float direction);
void level_getDimensions(unsigned int* width, unsigned int* height);
#endif #endif

View File

@ -10,19 +10,29 @@ static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float ang
static sf::RenderTexture minimap; static sf::RenderTexture minimap;
static bool init = false;
static unsigned int minimapSize; static unsigned int minimapSize;
static float drawScale;
int minimap_init(unsigned int size) int minimap_init(unsigned int size)
{ {
printf("minimap_init()\n"); printf("minimap_init()\n");
minimapSize = size;
if (!minimap.create(size, size)) return 0; if (!minimap.create(size, size)) return 0;
level_init(); level_init();
unsigned int width, height;
level_getDimensions(&width, &height);
minimapSize = size;
drawScale = (float)size/(float)width;
init = true;
return 1; return 1;
} }
void minimap_update(sf::RenderTarget* renderTarget, Camera* camera) void minimap_update(sf::RenderTarget* renderTarget, Camera* camera)
{ {
if (!init) return;
if (!renderTarget || !camera) return; if (!renderTarget || !camera) return;
minimap.clear(); minimap.clear();
level_update(&minimap, minimapSize); level_update(&minimap, minimapSize);
@ -35,7 +45,7 @@ void minimap_update(sf::RenderTarget* renderTarget, Camera* camera)
static void drawCamera(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; const float circleRadius = 0.02f * minimapSize;
sf::CircleShape circle(circleRadius); sf::CircleShape circle(circleRadius);
circle.setPosition(scaledPos); circle.setPosition(scaledPos);
@ -49,7 +59,7 @@ static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera)
static void drawRays(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 rayDirection = 0;
float rayDirectionStep = camera->fov / (float)camera->resolution; float rayDirectionStep = camera->fov / (float)camera->resolution;
@ -64,7 +74,7 @@ static void drawRays(sf::RenderTarget* renderTarget, Camera* camera)
else else
rayDirection = camera->direction + rayDirectionOffset; 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)); drawLine(renderTarget, scaledPos, rayDirection, distance, sf::Color(150, 150, 100));

View File

@ -6,13 +6,13 @@
#include "camera.h" #include "camera.h"
#include "minimap.h" #include "minimap.h"
#define MINIMAP_SIZE 300 #define MINIMAP_SIZE 250
#define HALF_MINIMAP_SIZE MINIMAP_SIZE/2.f #define HALF_MINIMAP_SIZE MINIMAP_SIZE/2.f
#define DRAW_SCALE MINIMAP_SIZE/5.f #define DRAW_SCALE MINIMAP_SIZE/5.f
static int handleKeyCode(sf::Keyboard::Key key); 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::Uint32 style = sf::Style::Titlebar;
static sf::RenderWindow window(sf::VideoMode(MINIMAP_SIZE + camera.resolution, MINIMAP_SIZE), "Raycasting", style); static sf::RenderWindow window(sf::VideoMode(MINIMAP_SIZE + camera.resolution, MINIMAP_SIZE), "Raycasting", style);