Skip to content

Commit

Permalink
Added the source code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kofybrek committed Oct 3, 2021
1 parent aa4d3c9 commit 0e9d57f
Show file tree
Hide file tree
Showing 25 changed files with 892 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Source/ConvertSketch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <array>
#include <chrono>
#include <SFML/Graphics.hpp>

#include "Headers/Global.hpp"
#include "Headers/Steven.hpp"
#include "Headers/Player.hpp"
#include "Headers/ConvertSketch.hpp"

std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH> convert_sketch(Player& i_player, Steven& i_steven)
{
std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH> output_map{};

sf::Image map_sketch;
map_sketch.loadFromFile("Resources/Images/MapSketch.png");

for (unsigned char a = 0; a < MAP_WIDTH; a++)
{
for (unsigned char b = 0; b < MAP_HEIGHT; b++)
{
sf::Color pixel = map_sketch.getPixel(a, b);

if (pixel == sf::Color(0, 0, 0))
{
output_map[a][b] = Cell::Wall;
}
else if (pixel == sf::Color(255, 0, 0))
{
i_player.set_position(static_cast<float>(CELL_SIZE * a), static_cast<float>(CELL_SIZE * b));
}
else if (pixel == sf::Color(0, 0, 255))
{
i_steven.set_position(static_cast<float>(CELL_SIZE * a), static_cast<float>(CELL_SIZE * b));
}
else
{
output_map[a][b] = Cell::Empty;
}
}
}

return output_map;
}
11 changes: 11 additions & 0 deletions Source/DegToRad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <chrono>

#include "Headers/DegToRad.hpp"
#include "Headers/Global.hpp"

float deg_to_rad(float i_degrees)
{
//After googling I found out that smart people use radians because of calculus.
//I don't know what that word means so I'll keep using degrees.
return PI * i_degrees / 180;
}
10 changes: 10 additions & 0 deletions Source/GetDegrees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <chrono>

#include "Headers/GetDegrees.hpp"
#include "Headers/Global.hpp"

float get_degrees(float i_degrees)
{
//Thank you, Stackoverflow!
return static_cast<float>(fmod(360 + fmod(i_degrees, 360), 360));
}
3 changes: 3 additions & 0 deletions Source/Headers/ConvertSketch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH> convert_sketch(Player& i_player, Steven& i_steven);
3 changes: 3 additions & 0 deletions Source/Headers/DegToRad.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

float deg_to_rad(float i_degrees);
3 changes: 3 additions & 0 deletions Source/Headers/GetDegrees.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

float get_degrees(float i_degrees);
31 changes: 31 additions & 0 deletions Source/Headers/Global.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

constexpr float FOV_HORIZONTAL = 90;
constexpr float FOV_VERTICAL = 58.75f;
constexpr float MOVEMENT_SPEED = 2;
//Since C++ stores floating numbers differently, this is the closest PI value we can use.
constexpr float PI = 3.141592653589793116f;
constexpr float RENDER_DISTANCE = 1024;
constexpr float STEVEN_ROTATION_SPEED = 2;

//The size of the cell in the game.
constexpr unsigned char CELL_SIZE = 64;
//The size of the cell in the minimap.
constexpr unsigned char MAP_CELL_SIZE = 8;
constexpr unsigned char MAP_GRID_CELL_SIZE = 16;
constexpr unsigned char MAP_HEIGHT = 24;
constexpr unsigned char MAP_WIDTH = 40;
constexpr unsigned char SCREEN_RESIZE = 1;

constexpr unsigned short SCREEN_HEIGHT = 720;
constexpr unsigned short SCREEN_WIDTH = 1280;

//If we divide 1 second by 60 frames (60 FPS), the duration of each frame will be 16.667 ms.
constexpr std::chrono::microseconds FRAME_DURATION(16667);

//When I started this project, I thought I would have tons of different cells, so I decided to use enum. I only used 2.
enum Cell
{
Empty,
Wall
};
3 changes: 3 additions & 0 deletions Source/Headers/MapCollision.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

bool map_collision(float i_x, float i_y, const std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH>& i_map);
27 changes: 27 additions & 0 deletions Source/Headers/Player.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

class Player
{
float direction_horizontal;
float direction_vertical;
float x;
float y;

//Using SCREEN_WIDTH here might be wrong since we're skipping some rays when drawing the screen.
std::array<float, SCREEN_WIDTH> view_rays;

sf::Sprite map_player_sprite;
sf::Sprite steven_sprite;
sf::Sprite wall_sprite;

sf::Texture map_player_texture;
sf::Texture steven_texture;
sf::Texture wall_texture;
public:
Player(float i_x, float i_y);

void draw_map(sf::RenderWindow& i_window);
void draw_screen(sf::RenderWindow& i_window, const Steven& i_steven);
void set_position(float i_x, float i_y);
void update(const std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH>& i_map, const sf::RenderWindow& i_window);
};
3 changes: 3 additions & 0 deletions Source/Headers/RadToDeg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

float rad_to_deg(float i_radians);
22 changes: 22 additions & 0 deletions Source/Headers/Steven.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

class Steven
{
float direction;
float x;
float y;

sf::Sprite map_steven_sprite;

sf::Texture map_steven_texture;
public:
Steven(float i_x, float i_y);

float get_center_x() const;
float get_center_y() const;
float get_direction() const;

void draw_map(sf::RenderWindow& i_window);
void set_position(float i_x, float i_y);
void update(const std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH>& i_map, const sf::RenderWindow& i_window);
};
132 changes: 132 additions & 0 deletions Source/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#include <array>
#include <chrono>
#include <SFML/Graphics.hpp>

#include "Headers/Global.hpp"
#include "Headers/Steven.hpp"
#include "Headers/Player.hpp"
#include "Headers/ConvertSketch.hpp"

int main()
{
//We can hide the HUD.
bool draw_map = 1;

std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH> map{};

//We'll use this variable to make the game framerate-independent.
std::chrono::microseconds lag(0);

std::chrono::steady_clock::time_point previous_time;

sf::Event event;

//I called this project "FPS" even though there's no "S".
sf::RenderWindow window(sf::VideoMode(SCREEN_RESIZE * SCREEN_WIDTH, SCREEN_RESIZE * SCREEN_HEIGHT), "FPS", sf::Style::Close);
window.setMouseCursorVisible(0);
window.setView(sf::View(sf::FloatRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)));

sf::Sprite map_grid_cell_sprite;
sf::Sprite map_wall_sprite;

sf::Texture map_grid_cell_texture;
map_grid_cell_texture.loadFromFile("Resources/Images/MapGridCell.png");

sf::Texture map_wall_texture;
map_wall_texture.loadFromFile("Resources/Images/MapWall" + std::to_string(MAP_CELL_SIZE) + ".png");

Player player(0, 0);

Steven steven(0, 0);

map = convert_sketch(player, steven);

map_grid_cell_sprite.setTexture(map_grid_cell_texture);
map_grid_cell_sprite.setTextureRect(sf::IntRect(0, 0, MAP_GRID_CELL_SIZE, MAP_GRID_CELL_SIZE));
map_wall_sprite.setTexture(map_wall_texture);

previous_time = std::chrono::steady_clock::now();

while (1 == window.isOpen())
{
std::chrono::microseconds delta_time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - previous_time);

lag += delta_time;

previous_time += delta_time;

while (FRAME_DURATION <= lag)
{
lag -= FRAME_DURATION;

while (1 == window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
{
window.close();

break;
}
case sf::Event::KeyPressed:
{
switch (event.key.code)
{
case sf::Keyboard::H:
{
draw_map = 1 - draw_map;
}
}
}
}
}

player.update(map, window);

steven.update(map, window);

if (FRAME_DURATION > lag)
{
//Drawing the sky.
//You might say, "Hey, you're just changing the color of the screen."
//And to that I'll say, "Shut up."
window.clear(sf::Color(73, 255, 255));

player.draw_screen(window, steven);

if (1 == draw_map)
{
for (unsigned short a = 0; a < ceil(MAP_CELL_SIZE * MAP_WIDTH / static_cast<float>(MAP_GRID_CELL_SIZE)); a++)
{
for (unsigned short b = 0; b < ceil(MAP_CELL_SIZE * MAP_HEIGHT / static_cast<float>(MAP_GRID_CELL_SIZE)); b++)
{
map_grid_cell_sprite.setPosition(static_cast<float>(MAP_GRID_CELL_SIZE * a), static_cast<float>(MAP_GRID_CELL_SIZE * b));

window.draw(map_grid_cell_sprite);
}
}

for (unsigned short a = 0; a < MAP_WIDTH; a++)
{
for (unsigned short b = 0; b < MAP_HEIGHT; b++)
{
if (Cell::Wall == map[a][b])
{
map_wall_sprite.setPosition(static_cast<float>(MAP_CELL_SIZE * a), static_cast<float>(MAP_CELL_SIZE * b));

window.draw(map_wall_sprite);
}
}
}

player.draw_map(window);

steven.draw_map(window);
}

window.display();
}
}
}
}
58 changes: 58 additions & 0 deletions Source/MapCollision.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <array>
#include <chrono>

#include "Headers/Global.hpp"
#include "Headers/MapCollision.hpp"

//I took this function from my Pac-Man project.
bool map_collision(float i_x, float i_y, const std::array<std::array<Cell, MAP_HEIGHT>, MAP_WIDTH>& i_map)
{
float cell_x = i_x / CELL_SIZE;
float cell_y = i_y / CELL_SIZE;

for (unsigned char a = 0; a < 4; a++)
{
short x = 0;
short y = 0;

switch (a)
{
case 0:
{
x = static_cast<short>(floor(cell_x));
y = static_cast<short>(floor(cell_y));

break;
}
case 1:
{
x = static_cast<short>(ceil(cell_x));
y = static_cast<short>(floor(cell_y));

break;
}
case 2:
{
x = static_cast<short>(floor(cell_x));
y = static_cast<short>(ceil(cell_y));

break;
}
case 3:
{
x = static_cast<short>(ceil(cell_x));
y = static_cast<short>(ceil(cell_y));
}
}

if (0 <= x && 0 <= y && MAP_HEIGHT > y && MAP_WIDTH > x)
{
if (Cell::Wall == i_map[x][y])
{
return 1;
}
}
}

return 0;
}
Loading

0 comments on commit 0e9d57f

Please sign in to comment.