Compare commits

..

No commits in common. "ca50b6f4b7106e83ef04b8c7b16c73b75b90c136" and "73412b27f8e2ad89582a24f78186edebac22e37e" have entirely different histories.

6 changed files with 21 additions and 85 deletions

View File

@ -1,57 +1,24 @@
#include "camera.h"
#include <cmath>
#include "level.h"
#include <SFML/Graphics/Color.hpp>
#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 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;
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 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)
{
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 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);
}
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;
}

View File

@ -1,16 +1,17 @@
#ifndef CAMERA_H
#define CAMERA_H
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Vector2.hpp>
typedef struct
{
sf::Vector2f pos;
float direction;
unsigned int resolution;
float resolution;
float fov;
} Camera;
void camera_update(Camera* camera, sf::RenderWindow* window, float t);
void camera_update(Camera* camera, sf::RenderWindow* window);
#endif

View File

@ -12,9 +12,9 @@ static sf::RenderWindow* window = nullptr;
static unsigned int level[WIDTH * HEIGHT] = {
1, 1, 1, 1, 1,
1, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 0, 1, 0, 1,
1, 0, 0, 0, 0,
0, 0, 0, 0, 0,
1, 0, 1, 0, 1,
};
@ -38,30 +38,26 @@ void level_end()
return;
}
float level_rayCastDistance(sf::Vector2f point, float direction)
{
return 100.f;
}
static void drawGrid()
{
const sf::Vector2u windowSize = window->getSize();
unsigned int padding = 5;
unsigned int stepX = windowSize.x/WIDTH;
unsigned int stepY = windowSize.y/HEIGHT;
drawGridLine(stepX, true);
drawGridLine(stepY, false);
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(stepY, stepY));
rectangle.setPosition(x * stepX, y * stepY);
sf::RectangleShape rectangle(sf::Vector2f(stepY - padding*2, stepY - padding*2));
rectangle.setPosition(x * stepX + padding, y * stepY + padding);
window->draw(rectangle);
}
}
drawGridLine(stepX, true);
drawGridLine(stepY, false);
}
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));
}
start.color = sf::Color(100, 100, 100);
end.color = sf::Color(100, 100, 100);
return isStart? start : end;
}

View File

@ -6,6 +6,5 @@
int level_init(sf::RenderWindow* renderWindow);
int level_update();
void level_end();
float level_rayCastDistance(sf::Vector2f point, float direction);
#endif

View File

@ -9,9 +9,9 @@ OBJS := $(SRCS:%.cpp=$(OBJD)/%.o)
CCFLAGS = -Wall
LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system
.PHONY: all run clean tags
.PHONY: all run clean
all: $(TARGET) tags
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) $^ -o $(TARGET) $(LDFLAGS) $(CCFLAGS)
@ -23,8 +23,6 @@ $(OBJS): $(OBJD)/%.o: %.cpp
clean:
rm -r $(TARGET) $(OBJD)
run: $(TARGET) tags
run: main
./$(TARGET)
tags:
ctags -R

View File

@ -9,9 +9,8 @@ static int handleKeyCode(sf::Keyboard::Key key);
static sf::Uint32 style = sf::Style::Titlebar;
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()
{
@ -38,10 +37,8 @@ int view_update()
}
window.clear();
sf::Time t = timer.restart();
if (!level_update()) return 0;
camera_update(&camera, &window, t.asSeconds());
camera_update(&camera, &window);
window.display();
return 1;