2023-04-16 09:17:11 +08:00
|
|
|
#include "minimap.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "maths.h"
|
|
|
|
#include "level.h"
|
|
|
|
|
|
|
|
static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera);
|
|
|
|
static void drawRays(sf::RenderTarget* renderTarget, Camera* camera);
|
|
|
|
static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float angle, float length, sf::Color color);
|
|
|
|
|
|
|
|
static sf::RenderTexture minimap;
|
|
|
|
|
2023-04-16 09:43:05 +08:00
|
|
|
static bool init = false;
|
2023-04-16 09:17:11 +08:00
|
|
|
static unsigned int minimapSize;
|
2023-04-16 09:43:05 +08:00
|
|
|
static float drawScale;
|
2023-04-16 09:17:11 +08:00
|
|
|
|
|
|
|
int minimap_init(unsigned int size)
|
|
|
|
{
|
|
|
|
printf("minimap_init()\n");
|
|
|
|
if (!minimap.create(size, size)) return 0;
|
|
|
|
level_init();
|
2023-04-16 09:43:05 +08:00
|
|
|
|
|
|
|
unsigned int width, height;
|
|
|
|
level_getDimensions(&width, &height);
|
|
|
|
|
|
|
|
minimapSize = size;
|
|
|
|
drawScale = (float)size/(float)width;
|
|
|
|
|
|
|
|
init = true;
|
2023-04-16 09:17:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void minimap_update(sf::RenderTarget* renderTarget, Camera* camera)
|
|
|
|
{
|
2023-04-16 09:43:05 +08:00
|
|
|
if (!init) return;
|
2023-04-16 09:17:11 +08:00
|
|
|
if (!renderTarget || !camera) return;
|
|
|
|
minimap.clear();
|
|
|
|
level_update(&minimap, minimapSize);
|
|
|
|
drawCamera(&minimap, camera);
|
|
|
|
minimap.display();
|
|
|
|
|
|
|
|
sf::Sprite sprite(minimap.getTexture());
|
|
|
|
renderTarget->draw(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawCamera(sf::RenderTarget* renderTarget, Camera* camera)
|
|
|
|
{
|
2023-04-16 09:43:05 +08:00
|
|
|
const sf::Vector2f scaledPos = camera->pos * drawScale;
|
2023-04-16 09:17:11 +08:00
|
|
|
const float circleRadius = 0.02f * minimapSize;
|
|
|
|
sf::CircleShape circle(circleRadius);
|
|
|
|
circle.setPosition(scaledPos);
|
|
|
|
circle.setOrigin(circleRadius, circleRadius);
|
|
|
|
circle.setFillColor(sf::Color::Green);
|
|
|
|
|
|
|
|
drawRays(renderTarget, camera);
|
|
|
|
drawLine(renderTarget, scaledPos, camera->direction, minimapSize / 5.f, sf::Color::Red);
|
|
|
|
renderTarget->draw(circle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawRays(sf::RenderTarget* renderTarget, Camera* camera)
|
|
|
|
{
|
2023-04-16 09:43:05 +08:00
|
|
|
const sf::Vector2f scaledPos = camera->pos * drawScale;
|
2023-04-16 09:17:11 +08:00
|
|
|
|
|
|
|
float rayDirection = 0;
|
|
|
|
float rayDirectionStep = camera->fov / (float)camera->resolution;
|
|
|
|
bool isOddResolution = (camera->resolution % 2);
|
|
|
|
float rayDirectionOffset = isOddResolution? 0 : rayDirectionStep / 2.f;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < camera->resolution; i++) {
|
|
|
|
if (isOddResolution && i == 0)
|
|
|
|
rayDirection = camera->direction;
|
|
|
|
else if (i % 2)
|
|
|
|
rayDirection = camera->direction - rayDirectionOffset;
|
|
|
|
else
|
|
|
|
rayDirection = camera->direction + rayDirectionOffset;
|
|
|
|
|
2023-04-16 09:43:05 +08:00
|
|
|
float distance = level_rayCastDistance(camera->pos, rayDirection) * drawScale;
|
2023-04-16 09:17:11 +08:00
|
|
|
|
|
|
|
drawLine(renderTarget, scaledPos, rayDirection, distance, sf::Color(150, 150, 100));
|
|
|
|
|
|
|
|
if ((i + isOddResolution) % 2) rayDirectionOffset += rayDirectionStep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drawLine(sf::RenderTarget* renderTarget, sf::Vector2f pos, float angle, float length, sf::Color color)
|
|
|
|
{
|
|
|
|
if (!renderTarget) return;
|
|
|
|
|
|
|
|
sf::Vector2f endOffset(length * cos(angle), length * sin(angle));
|
|
|
|
sf::Vertex start(pos);
|
|
|
|
sf::Vertex end(pos + endOffset);
|
|
|
|
|
|
|
|
start.color = color;
|
|
|
|
end.color = color;
|
|
|
|
|
|
|
|
sf::Vertex line[] = { start, end };
|
|
|
|
|
|
|
|
renderTarget->draw(line, 2, sf::Lines);
|
|
|
|
}
|
|
|
|
|