Compare commits
No commits in common. "ca50b6f4b7106e83ef04b8c7b16c73b75b90c136" and "73412b27f8e2ad89582a24f78186edebac22e37e" have entirely different histories.
ca50b6f4b7
...
73412b27f8
61
camera.cpp
61
camera.cpp
@ -1,57 +1,24 @@
|
|||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "level.h"
|
|
||||||
|
#include <SFML/Graphics/Color.hpp>
|
||||||
|
|
||||||
#define PI 3.14159265
|
#define PI 3.14159265
|
||||||
#define DEG_RAD PI/180.f
|
|
||||||
#define ROTATION_SPEED 180
|
|
||||||
#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 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, sf::RenderWindow* window, float t)
|
void camera_update(Camera* camera, sf::RenderWindow* window)
|
||||||
{
|
{
|
||||||
if (!camera || !window) 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);
|
drawLine(window, camera->pos, camera->direction, 100, sf::Color::Red);
|
||||||
window->draw(circle);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void drawRays(Camera* camera, sf::RenderWindow* window)
|
|
||||||
{
|
|
||||||
float halfFOV = camera->fov/2.f;
|
|
||||||
float rayDirection = camera->direction - halfFOV;
|
|
||||||
float rayDirectionStep = camera->fov / (float)camera->resolution;
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < camera->resolution; i++) {
|
|
||||||
float distance = level_rayCastDistance(camera->pos, rayDirection);
|
|
||||||
drawLine(window, camera->pos, rayDirection, distance, sf::Color::Blue);
|
|
||||||
rayDirection += rayDirectionStep;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color)
|
static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, float length, sf::Color color)
|
||||||
{
|
{
|
||||||
if (!window) return;
|
if (!window) return;
|
||||||
|
|
||||||
sf::Vector2f endOffset(length * cos(angle * DEG_RAD), length * sin(angle * DEG_RAD));
|
sf::Vector2f endOffset(length * cos(angle * PI/180.f), length * sin(angle * PI/180.3));
|
||||||
sf::Vertex start(pos);
|
sf::Vertex start(pos);
|
||||||
sf::Vertex end(pos + endOffset);
|
sf::Vertex end(pos + endOffset);
|
||||||
|
|
||||||
@ -62,23 +29,3 @@ static void drawLine(sf::RenderWindow* window, sf::Vector2f pos, float angle, fl
|
|||||||
|
|
||||||
window->draw(line, 2, sf::Lines);
|
window->draw(line, 2, sf::Lines);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void move(Camera* camera, float t)
|
|
||||||
{
|
|
||||||
int forward = 0;
|
|
||||||
int rotation = 0;
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F))
|
|
||||||
forward += 1;
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
|
|
||||||
forward -= 1;
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::T))
|
|
||||||
rotation +=1;
|
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
|
|
||||||
rotation -=1;
|
|
||||||
|
|
||||||
float magnitude = forward * t * TRANSLATIONAL_SPEED;
|
|
||||||
sf::Vector2f offset(magnitude * cos(camera->direction * DEG_RAD), magnitude *sin(camera->direction * DEG_RAD));
|
|
||||||
|
|
||||||
camera->pos += offset;
|
|
||||||
camera->direction += rotation * t * ROTATION_SPEED;
|
|
||||||
}
|
|
||||||
|
7
camera.h
7
camera.h
@ -1,16 +1,17 @@
|
|||||||
#ifndef CAMERA_H
|
#ifndef CAMERA_H
|
||||||
#define CAMERA_H
|
#define CAMERA_H
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics/RenderWindow.hpp>
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
sf::Vector2f pos;
|
sf::Vector2f pos;
|
||||||
float direction;
|
float direction;
|
||||||
unsigned int resolution;
|
float resolution;
|
||||||
float fov;
|
float fov;
|
||||||
} Camera;
|
} Camera;
|
||||||
|
|
||||||
void camera_update(Camera* camera, sf::RenderWindow* window, float t);
|
void camera_update(Camera* camera, sf::RenderWindow* window);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
22
level.cpp
22
level.cpp
@ -12,9 +12,9 @@ static sf::RenderWindow* window = nullptr;
|
|||||||
|
|
||||||
static unsigned int level[WIDTH * HEIGHT] = {
|
static unsigned int level[WIDTH * HEIGHT] = {
|
||||||
1, 1, 1, 1, 1,
|
1, 1, 1, 1, 1,
|
||||||
1, 0, 0, 0, 0,
|
0, 0, 0, 0, 0,
|
||||||
1, 0, 1, 0, 1,
|
1, 0, 1, 0, 1,
|
||||||
1, 0, 0, 0, 0,
|
0, 0, 0, 0, 0,
|
||||||
1, 0, 1, 0, 1,
|
1, 0, 1, 0, 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -38,30 +38,26 @@ void level_end()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
float level_rayCastDistance(sf::Vector2f point, float direction)
|
|
||||||
{
|
|
||||||
return 100.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void drawGrid()
|
static void drawGrid()
|
||||||
{
|
{
|
||||||
const sf::Vector2u windowSize = window->getSize();
|
const sf::Vector2u windowSize = window->getSize();
|
||||||
|
unsigned int padding = 5;
|
||||||
unsigned int stepX = windowSize.x/WIDTH;
|
unsigned int stepX = windowSize.x/WIDTH;
|
||||||
unsigned int stepY = windowSize.y/HEIGHT;
|
unsigned int stepY = windowSize.y/HEIGHT;
|
||||||
|
|
||||||
|
drawGridLine(stepX, true);
|
||||||
|
drawGridLine(stepY, false);
|
||||||
|
|
||||||
for (unsigned int x = 0; x < WIDTH; x++) {
|
for (unsigned int x = 0; x < WIDTH; x++) {
|
||||||
for (unsigned int y = 0; y < HEIGHT; y++) {
|
for (unsigned int y = 0; y < HEIGHT; y++) {
|
||||||
if (!level[y * HEIGHT + x]) continue;
|
if (!level[y * HEIGHT + x]) continue;
|
||||||
|
|
||||||
sf::RectangleShape rectangle(sf::Vector2f(stepY, stepY));
|
sf::RectangleShape rectangle(sf::Vector2f(stepY - padding*2, stepY - padding*2));
|
||||||
rectangle.setPosition(x * stepX, y * stepY);
|
rectangle.setPosition(x * stepX + padding, y * stepY + padding);
|
||||||
|
|
||||||
window->draw(rectangle);
|
window->draw(rectangle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
drawGridLine(stepX, true);
|
|
||||||
drawGridLine(stepY, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void drawGridLine(unsigned int step, bool isHorizontal)
|
static void drawGridLine(unsigned int step, bool isHorizontal)
|
||||||
@ -96,7 +92,5 @@ static sf::Vertex getGridLineVertex(unsigned int offset, unsigned int maxDimensi
|
|||||||
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
|
end = sf::Vertex(sf::Vector2f(maxDimension, offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
start.color = sf::Color(100, 100, 100);
|
|
||||||
end.color = sf::Color(100, 100, 100);
|
|
||||||
return isStart? start : end;
|
return isStart? start : end;
|
||||||
}
|
}
|
||||||
|
1
level.h
1
level.h
@ -6,6 +6,5 @@
|
|||||||
int level_init(sf::RenderWindow* renderWindow);
|
int level_init(sf::RenderWindow* renderWindow);
|
||||||
int level_update();
|
int level_update();
|
||||||
void level_end();
|
void level_end();
|
||||||
float level_rayCastDistance(sf::Vector2f point, float direction);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
8
makefile
8
makefile
@ -9,9 +9,9 @@ OBJS := $(SRCS:%.cpp=$(OBJD)/%.o)
|
|||||||
CCFLAGS = -Wall
|
CCFLAGS = -Wall
|
||||||
LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system
|
LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system
|
||||||
|
|
||||||
.PHONY: all run clean tags
|
.PHONY: all run clean
|
||||||
|
|
||||||
all: $(TARGET) tags
|
all: $(TARGET)
|
||||||
|
|
||||||
$(TARGET): $(OBJS)
|
$(TARGET): $(OBJS)
|
||||||
$(CC) $^ -o $(TARGET) $(LDFLAGS) $(CCFLAGS)
|
$(CC) $^ -o $(TARGET) $(LDFLAGS) $(CCFLAGS)
|
||||||
@ -23,8 +23,6 @@ $(OBJS): $(OBJD)/%.o: %.cpp
|
|||||||
clean:
|
clean:
|
||||||
rm -r $(TARGET) $(OBJD)
|
rm -r $(TARGET) $(OBJD)
|
||||||
|
|
||||||
run: $(TARGET) tags
|
run: main
|
||||||
./$(TARGET)
|
./$(TARGET)
|
||||||
|
|
||||||
tags:
|
|
||||||
ctags -R
|
|
||||||
|
7
view.cpp
7
view.cpp
@ -9,9 +9,8 @@ static int handleKeyCode(sf::Keyboard::Key key);
|
|||||||
|
|
||||||
static sf::Uint32 style = sf::Style::Titlebar;
|
static sf::Uint32 style = sf::Style::Titlebar;
|
||||||
static sf::RenderWindow window(sf::VideoMode(500, 500), "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, 20, 360.f };
|
static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 1.f, 70.f };
|
||||||
|
|
||||||
int view_init()
|
int view_init()
|
||||||
{
|
{
|
||||||
@ -38,10 +37,8 @@ int view_update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.clear();
|
window.clear();
|
||||||
|
|
||||||
sf::Time t = timer.restart();
|
|
||||||
if (!level_update()) return 0;
|
if (!level_update()) return 0;
|
||||||
camera_update(&camera, &window, t.asSeconds());
|
camera_update(&camera, &window);
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user