Compare commits
4 Commits
73412b27f8
...
ca50b6f4b7
Author | SHA1 | Date | |
---|---|---|---|
ca50b6f4b7 | |||
f1cb03bc22 | |||
0cd0d410a1 | |||
15c3fe7604 |
61
camera.cpp
61
camera.cpp
@ -1,24 +1,57 @@
|
||||
#include "camera.h"
|
||||
#include <cmath>
|
||||
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <cmath>
|
||||
#include "level.h"
|
||||
|
||||
#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)
|
||||
void camera_update(Camera* camera, sf::RenderWindow* window, float t)
|
||||
{
|
||||
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 * PI/180.f), length * sin(angle * PI/180.3));
|
||||
sf::Vector2f endOffset(length * cos(angle * DEG_RAD), length * sin(angle * DEG_RAD));
|
||||
sf::Vertex start(pos);
|
||||
sf::Vertex end(pos + endOffset);
|
||||
|
||||
@ -29,3 +62,23 @@ 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;
|
||||
}
|
||||
|
7
camera.h
7
camera.h
@ -1,17 +1,16 @@
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
sf::Vector2f pos;
|
||||
float direction;
|
||||
float resolution;
|
||||
unsigned int resolution;
|
||||
float fov;
|
||||
} Camera;
|
||||
|
||||
void camera_update(Camera* camera, sf::RenderWindow* window);
|
||||
void camera_update(Camera* camera, sf::RenderWindow* window, float t);
|
||||
|
||||
#endif
|
||||
|
22
level.cpp
22
level.cpp
@ -12,9 +12,9 @@ static sf::RenderWindow* window = nullptr;
|
||||
|
||||
static unsigned int level[WIDTH * HEIGHT] = {
|
||||
1, 1, 1, 1, 1,
|
||||
0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0,
|
||||
1, 0, 1, 0, 1,
|
||||
0, 0, 0, 0, 0,
|
||||
1, 0, 0, 0, 0,
|
||||
1, 0, 1, 0, 1,
|
||||
};
|
||||
|
||||
@ -38,26 +38,30 @@ 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 - padding*2, stepY - padding*2));
|
||||
rectangle.setPosition(x * stepX + padding, y * stepY + padding);
|
||||
sf::RectangleShape rectangle(sf::Vector2f(stepY, stepY));
|
||||
rectangle.setPosition(x * stepX, y * stepY);
|
||||
|
||||
window->draw(rectangle);
|
||||
}
|
||||
}
|
||||
|
||||
drawGridLine(stepX, true);
|
||||
drawGridLine(stepY, false);
|
||||
}
|
||||
|
||||
static void drawGridLine(unsigned int step, bool isHorizontal)
|
||||
@ -92,5 +96,7 @@ 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;
|
||||
}
|
||||
|
1
level.h
1
level.h
@ -6,5 +6,6 @@
|
||||
int level_init(sf::RenderWindow* renderWindow);
|
||||
int level_update();
|
||||
void level_end();
|
||||
float level_rayCastDistance(sf::Vector2f point, float direction);
|
||||
|
||||
#endif
|
||||
|
8
makefile
8
makefile
@ -9,9 +9,9 @@ OBJS := $(SRCS:%.cpp=$(OBJD)/%.o)
|
||||
CCFLAGS = -Wall
|
||||
LDFLAGS = -lsfml-graphics -lsfml-window -lsfml-system
|
||||
|
||||
.PHONY: all run clean
|
||||
.PHONY: all run clean tags
|
||||
|
||||
all: $(TARGET)
|
||||
all: $(TARGET) tags
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $^ -o $(TARGET) $(LDFLAGS) $(CCFLAGS)
|
||||
@ -23,6 +23,8 @@ $(OBJS): $(OBJD)/%.o: %.cpp
|
||||
clean:
|
||||
rm -r $(TARGET) $(OBJD)
|
||||
|
||||
run: main
|
||||
run: $(TARGET) tags
|
||||
./$(TARGET)
|
||||
|
||||
tags:
|
||||
ctags -R
|
||||
|
7
view.cpp
7
view.cpp
@ -9,8 +9,9 @@ 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, 1.f, 70.f };
|
||||
static Camera camera = { sf::Vector2f(300.f, 250.f), 0.f, 20, 360.f };
|
||||
|
||||
int view_init()
|
||||
{
|
||||
@ -37,8 +38,10 @@ int view_update()
|
||||
}
|
||||
|
||||
window.clear();
|
||||
|
||||
sf::Time t = timer.restart();
|
||||
if (!level_update()) return 0;
|
||||
camera_update(&camera, &window);
|
||||
camera_update(&camera, &window, t.asSeconds());
|
||||
window.display();
|
||||
|
||||
return 1;
|
||||
|
Loading…
Reference in New Issue
Block a user