Compare commits
No commits in common. "bce640219f209eab9aee266a80747218dec4a1de" and "ccfe9fd786ec7b3289eb454b64a904dc32792e26" have entirely different histories.
bce640219f
...
ccfe9fd786
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
||||
# ignore binaries
|
||||
obj
|
||||
main
|
||||
tags
|
||||
|
62
camera.cpp
62
camera.cpp
@ -4,17 +4,73 @@
|
||||
#include "level.h"
|
||||
|
||||
#define ROTATION_SPEED PI
|
||||
#define TRANSLATIONAL_SPEED 1.f
|
||||
#define TRANSLATIONAL_SPEED 100.f
|
||||
|
||||
static void draw(Camera* camera, sf::RenderWindow* window);
|
||||
static void drawRays(Camera* camera, sf::RenderWindow* window);
|
||||
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color);
|
||||
static void move(Camera* camera, float t);
|
||||
|
||||
void camera_update(Camera* camera, float t)
|
||||
void camera_update(Camera* camera, sf::RenderWindow* window, float t)
|
||||
{
|
||||
if (!camera) return;
|
||||
if (!camera || !window) return;
|
||||
|
||||
draw(camera, window);
|
||||
move(camera, t);
|
||||
}
|
||||
|
||||
static void draw(Camera* camera, sf::RenderWindow* window)
|
||||
{
|
||||
const float circleRadius = 5.f;
|
||||
sf::CircleShape circle(circleRadius);
|
||||
circle.setPosition(camera->pos);
|
||||
circle.setOrigin(circleRadius, circleRadius);
|
||||
circle.setFillColor(sf::Color::Green);
|
||||
|
||||
drawRays(camera, window);
|
||||
drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red);
|
||||
window->draw(circle);
|
||||
}
|
||||
|
||||
static void drawRays(Camera* camera, sf::RenderWindow* window)
|
||||
{
|
||||
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;
|
||||
|
||||
float distance = level_rayCastDistance(camera->pos, rayDirection);
|
||||
|
||||
drawLine(window, camera->pos, rayDirection, distance, sf::Color(150, 150, 100));
|
||||
|
||||
if ((i + isOddResolution) % 2) rayDirectionOffset += rayDirectionStep;
|
||||
}
|
||||
}
|
||||
|
||||
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color)
|
||||
{
|
||||
if (!window) 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 };
|
||||
|
||||
window->draw(line, 2, sf::Lines);
|
||||
}
|
||||
|
||||
static void move(Camera* camera, float t)
|
||||
{
|
||||
int forward = 0;
|
||||
|
3
camera.h
3
camera.h
@ -9,9 +9,8 @@ typedef struct
|
||||
float direction;
|
||||
unsigned int resolution;
|
||||
float fov;
|
||||
float drawScale;
|
||||
} Camera;
|
||||
|
||||
void camera_update(Camera* camera, float t);
|
||||
void camera_update(Camera* camera, sf::RenderWindow* window, float t);
|
||||
|
||||
#endif
|
||||
|
88
level.cpp
88
level.cpp
@ -6,32 +6,35 @@
|
||||
#define WIDTH 5
|
||||
#define HEIGHT 5
|
||||
|
||||
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize);
|
||||
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal);
|
||||
static sf::Vertex getGridLineVertex(float n, float maxDimension, bool isStart, bool isHorizontal);
|
||||
static void drawGrid();
|
||||
static void drawGridLine(unsigned int step, bool isHorizontal);
|
||||
static sf::Vertex getGridLineVertex(unsigned int n, unsigned int maxDimension, bool isStart, bool isHorizontal);
|
||||
|
||||
static float castRay(sf::Vector2f point, float direction);
|
||||
static void getGridIndex(sf::Vector2f point, int* x, int* y);
|
||||
|
||||
static sf::RenderWindow* window = nullptr;
|
||||
|
||||
static unsigned int level[WIDTH * HEIGHT] = {
|
||||
0, 0, 1, 1, 1,
|
||||
1, 1, 1, 1, 1,
|
||||
0, 0, 0, 0, 0,
|
||||
1, 0, 1, 0, 1,
|
||||
1, 0, 0, 0, 1,
|
||||
1, 0, 1, 1, 1,
|
||||
0, 0, 1, 0, 1,
|
||||
0, 0, 0, 0, 0,
|
||||
0, 0, 1, 0, 1,
|
||||
};
|
||||
|
||||
int level_init()
|
||||
int level_init(sf::RenderWindow* renderWindow)
|
||||
{
|
||||
printf("level_init()\n");
|
||||
window = renderWindow;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize)
|
||||
int level_update()
|
||||
{
|
||||
if (!renderTarget) return;
|
||||
|
||||
drawGrid(renderTarget, drawSize/WIDTH);
|
||||
if (!window) return 0;
|
||||
drawGrid();
|
||||
return 1;
|
||||
}
|
||||
|
||||
void level_end()
|
||||
@ -45,41 +48,46 @@ float level_rayCastDistance(sf::Vector2f point, float direction)
|
||||
return castRay(point, direction);
|
||||
}
|
||||
|
||||
static void drawGrid(sf::RenderTarget* renderTarget, unsigned int tileSize)
|
||||
static void drawGrid()
|
||||
{
|
||||
const sf::Vector2u windowSize = window->getSize();
|
||||
const unsigned int stepX = windowSize.x/WIDTH;
|
||||
const unsigned int stepY = windowSize.y/HEIGHT;
|
||||
|
||||
for (unsigned int x = 0; x < WIDTH; x++) {
|
||||
for (unsigned int y = 0; y < HEIGHT; y++) {
|
||||
if (!level[y * HEIGHT + x]) continue;
|
||||
|
||||
sf::RectangleShape rectangle(sf::Vector2f(tileSize, tileSize));
|
||||
rectangle.setPosition((float)x * tileSize, (float)y * tileSize);
|
||||
renderTarget->draw(rectangle);
|
||||
sf::RectangleShape rectangle(sf::Vector2f(stepY, stepY));
|
||||
rectangle.setPosition(x * stepX, y * stepY);
|
||||
|
||||
window->draw(rectangle);
|
||||
}
|
||||
}
|
||||
|
||||
drawGridLine(renderTarget, tileSize, true);
|
||||
drawGridLine(renderTarget, tileSize, false);
|
||||
drawGridLine(stepX, true);
|
||||
drawGridLine(stepY, false);
|
||||
}
|
||||
|
||||
static void drawGridLine(sf::RenderTarget* renderTarget, float step, bool isHorizontal)
|
||||
static void drawGridLine(unsigned int step, bool isHorizontal)
|
||||
{
|
||||
unsigned int lines = isHorizontal? WIDTH : HEIGHT;
|
||||
|
||||
for (unsigned int n = 0; n < lines; n++) {
|
||||
if (n == 0) continue;
|
||||
float offset = (float)n * step;
|
||||
float maxDimension = (float)lines * step;
|
||||
unsigned int offset = n * step;
|
||||
unsigned int maxDimension = lines * step;
|
||||
sf::Vertex line[] =
|
||||
{
|
||||
getGridLineVertex(offset, maxDimension, true, isHorizontal),
|
||||
getGridLineVertex(offset, maxDimension, false, isHorizontal)
|
||||
};
|
||||
|
||||
renderTarget->draw(line, 2, sf::Lines);
|
||||
window->draw(line, 2, sf::Lines);
|
||||
}
|
||||
}
|
||||
|
||||
static sf::Vertex getGridLineVertex(float offset, float maxDimension, bool isStart, bool isHorizontal)
|
||||
static sf::Vertex getGridLineVertex(unsigned int offset, unsigned int maxDimension, bool isStart, bool isHorizontal)
|
||||
{
|
||||
sf::Vertex start;
|
||||
sf::Vertex end;
|
||||
@ -93,14 +101,17 @@ static sf::Vertex getGridLineVertex(float offset, float maxDimension, bool isSta
|
||||
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
|
||||
}
|
||||
|
||||
sf::Color color(100, 100, 100);
|
||||
start.color = color;
|
||||
end.color = color;
|
||||
start.color = sf::Color(100, 100, 100);
|
||||
end.color = sf::Color(100, 100, 100);
|
||||
return isStart? start : end;
|
||||
}
|
||||
|
||||
static float castRay(sf::Vector2f point, float direction)
|
||||
{
|
||||
const sf::Vector2u windowSize = window->getSize();
|
||||
const unsigned int tileWidth = windowSize.x/WIDTH;
|
||||
const unsigned int tileHeight = windowSize.y/HEIGHT;
|
||||
|
||||
int indexX, indexY;
|
||||
getGridIndex(point, &indexX, &indexY);
|
||||
|
||||
@ -124,13 +135,13 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
bool goingDown = direction < PI;
|
||||
int signDown = goingDown? 1 : -1;
|
||||
|
||||
float horizontalDy = (float)(indexY + goingDown) - point.y;
|
||||
float horizontalDy = (float)((indexY + goingDown) * tileHeight) - point.y;
|
||||
float horizontalDx = horizontalDy/tan(direction);
|
||||
|
||||
float horizontalStepX = ((float)signDown * (1.f/tan(direction)));
|
||||
float horizontalStepY = (float)signDown;
|
||||
float horizontalStepX = (float)(signDown * (tileWidth/tan(direction)));
|
||||
float horizontalStepY = (float)(signDown * (int)tileHeight);
|
||||
float horizontalProjectedX = point.x + horizontalDx;
|
||||
float horizontalProjectedY = indexY + goingDown;
|
||||
float horizontalProjectedY = (indexY + goingDown) * tileHeight;
|
||||
|
||||
float horizontalDistCoeff = sin(direction);
|
||||
float horizontalRayDist = std::abs(horizontalDy/horizontalDistCoeff);
|
||||
@ -139,19 +150,18 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
bool goingRight = direction < PI;
|
||||
int signRight = goingRight? 1 : -1;
|
||||
|
||||
float verticalDx = (float)(indexX + goingRight) - point.x;
|
||||
float verticalDx = (float)((indexX + goingRight) * tileWidth) - point.x;
|
||||
float verticalDy = -verticalDx/tan(direction); // y axis needs to be flipped
|
||||
|
||||
float verticalStepY = -((float)signRight * (1.f/tan(direction))); // y axis also flipped here
|
||||
float verticalStepX = (float)signRight;
|
||||
float verticalStepY = -(float)(signRight * (tileHeight/tan(direction))); // y axis also flipped here
|
||||
float verticalStepX = (float)(signRight * (int)tileHeight);
|
||||
float verticalProjectedY = point.y + verticalDy;
|
||||
float verticalProjectedX = indexX + goingRight;
|
||||
float verticalProjectedX = (indexX + goingRight) * tileWidth;
|
||||
|
||||
float verticalDistCoeff = sin(direction);
|
||||
float verticalRayDist = std::abs(verticalDx/verticalDistCoeff);
|
||||
|
||||
unsigned int tries = WIDTH * HEIGHT;
|
||||
while (tries--) {
|
||||
while (true) {
|
||||
int indexX0, indexY0; // store grid indices for horizontal intersections
|
||||
int indexX1, indexY1; // store grid indices for vertical intersections
|
||||
getGridIndex(sf::Vector2f(horizontalProjectedX, horizontalProjectedY), &indexX0, &indexY0);
|
||||
@ -191,8 +201,10 @@ static float castRay(sf::Vector2f point, float direction)
|
||||
|
||||
static void getGridIndex(sf::Vector2f point, int* x, int* y)
|
||||
{
|
||||
*x = point.x;
|
||||
*y = point.y;
|
||||
const sf::Vector2u windowSize = window->getSize();
|
||||
|
||||
*x = point.x / (int)(windowSize.x / WIDTH);
|
||||
*y = point.y / (int)(windowSize.y / HEIGHT);
|
||||
|
||||
if (*x < 0 || WIDTH <= *x) *x = -1;
|
||||
if (*y < 0 || HEIGHT <= *y) *y = -1;
|
||||
|
4
level.h
4
level.h
@ -3,8 +3,8 @@
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
int level_init();
|
||||
void level_update(sf::RenderTarget* renderTarget, unsigned int drawSize);
|
||||
int level_init(sf::RenderWindow* renderWindow);
|
||||
int level_update();
|
||||
void level_end();
|
||||
float level_rayCastDistance(sf::Vector2f point, float direction);
|
||||
|
||||
|
2
main.cpp
2
main.cpp
@ -4,7 +4,7 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
if (!view_init()) return 1;
|
||||
view_init();
|
||||
while (view_update());
|
||||
view_end();
|
||||
|
||||
|
90
minimap.cpp
90
minimap.cpp
@ -1,90 +0,0 @@
|
||||
#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;
|
||||
|
||||
static unsigned int minimapSize;
|
||||
|
||||
int minimap_init(unsigned int size)
|
||||
{
|
||||
printf("minimap_init()\n");
|
||||
minimapSize = size;
|
||||
if (!minimap.create(size, size)) return 0;
|
||||
level_init();
|
||||
return 1;
|
||||
}
|
||||
|
||||
void minimap_update(sf::RenderTarget* renderTarget, Camera* camera)
|
||||
{
|
||||
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)
|
||||
{
|
||||
const sf::Vector2f scaledPos = camera->pos * camera->drawScale;
|
||||
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)
|
||||
{
|
||||
const sf::Vector2f scaledPos = camera->pos * camera->drawScale;
|
||||
|
||||
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;
|
||||
|
||||
float distance = level_rayCastDistance(camera->pos, rayDirection) * camera->drawScale;
|
||||
|
||||
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);
|
||||
}
|
||||
|
10
minimap.h
10
minimap.h
@ -1,10 +0,0 @@
|
||||
#ifndef MINIMAP_H
|
||||
#define MINIMAP_H
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "camera.h"
|
||||
|
||||
int minimap_init(unsigned int size);
|
||||
void minimap_update(sf::RenderTarget* renderTarget, Camera* camera);
|
||||
|
||||
#endif
|
26
view.cpp
26
view.cpp
@ -2,26 +2,22 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "maths.h"
|
||||
#include "level.h"
|
||||
#include "camera.h"
|
||||
#include "minimap.h"
|
||||
|
||||
#define MINIMAP_SIZE 300
|
||||
#define HALF_MINIMAP_SIZE MINIMAP_SIZE/2.f
|
||||
#define DRAW_SCALE MINIMAP_SIZE/5.f
|
||||
#include "maths.h"
|
||||
|
||||
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 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(500, 500), "Raycasting", style);
|
||||
static sf::Clock timer;
|
||||
|
||||
static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 100, 2.0f*PI };
|
||||
|
||||
int view_init()
|
||||
{
|
||||
printf("view_init()\n");
|
||||
minimap_init(MINIMAP_SIZE);
|
||||
level_init(&window);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -42,12 +38,11 @@ int view_update()
|
||||
}
|
||||
}
|
||||
|
||||
sf::Time t = timer.restart();
|
||||
|
||||
camera_update(&camera, t.asSeconds());
|
||||
|
||||
window.clear();
|
||||
minimap_update(&window, &camera);
|
||||
|
||||
sf::Time t = timer.restart();
|
||||
if (!level_update()) return 0;
|
||||
camera_update(&camera, &window, t.asSeconds());
|
||||
window.display();
|
||||
|
||||
return 1;
|
||||
@ -56,6 +51,7 @@ int view_update()
|
||||
void view_end()
|
||||
{
|
||||
printf("view_end()\n");
|
||||
level_end();
|
||||
|
||||
window.close();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user