Skip to content

Commit

Permalink
Display downscaled textures on overview map along with more accurate …
Browse files Browse the repository at this point in the history
…player position and camera debug lines
  • Loading branch information
balintkissdev committed Sep 12, 2021
1 parent 86ef050 commit 92b311d
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 154 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

My take on making a raycasting pseudo-3D engine in C++, also with my own tiny template linear algebra types. One of the goals was to make raycasting computation equations more explicit and readable.

## Controls
![alt tag](https://raw.githubusercontent.com/balintkissdev/raycaster-engine/master/demo2.png)

## Features

- Up/Down to move and Left/Right to turn around
- WASD to move and strafe
Expand Down
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ target_sources(${PROJECT_NAME}
Texture.h
Vector2.h
Vector2.inl
WallTypes.h
)
19 changes: 14 additions & 5 deletions src/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "Camera.h"

#include "Matrix2.h"
#include "WallTypes.h"

Camera::Camera(const Vector2<float>& position, const Vector2<float>& direction, const float fieldOfView, Map& map)
: map_(map)
Expand All @@ -17,13 +16,13 @@ void Camera::move(const float moveDirection)
{
if (map_.position(
static_cast<int>(position_.x + moveDirection * (direction_.x * movementSpeed_)),
static_cast<int>(position_.y)) == EMPTY_SPACE)
static_cast<int>(position_.y)) == Map::EMPTY_SQUARE_INDEX)
{
position_.x += moveDirection * (direction_.x * movementSpeed_);
}
if (map_.position(
static_cast<int>(position_.x),
static_cast<int>(position_.y + moveDirection * (direction_.y * movementSpeed_))) == EMPTY_SPACE)
static_cast<int>(position_.y + moveDirection * (direction_.y * movementSpeed_))) == Map::EMPTY_SQUARE_INDEX)
{
position_.y += moveDirection * (direction_.y * movementSpeed_);
}
Expand All @@ -39,13 +38,13 @@ void Camera::strafe(const float strafeDirection)
{
if (map_.position(
static_cast<int>(position_.x - strafeDirection * (plane_.x * movementSpeed_)),
static_cast<int>(position_.y)) == EMPTY_SPACE)
static_cast<int>(position_.y)) == Map::EMPTY_SQUARE_INDEX)
{
position_.x -= strafeDirection * (plane_.x * movementSpeed_);
}
if (map_.position(
static_cast<int>(position_.x),
static_cast<int>(position_.y - strafeDirection * (plane_.y * movementSpeed_))) == EMPTY_SPACE)
static_cast<int>(position_.y - strafeDirection * (plane_.y * movementSpeed_))) == Map::EMPTY_SQUARE_INDEX)
{
position_.y -= strafeDirection * (plane_.y * movementSpeed_);
}
Expand Down Expand Up @@ -77,3 +76,13 @@ Camera& Camera::rotationSpeed(const float rotationSpeed)
rotationSpeed_ = rotationSpeed;
return *this;
}

Vector2<float> Camera::planeLeftEdgeDirection() const
{
return direction_ - plane_;
}

Vector2<float> Camera::planeRightEdgeDirection() const
{
return direction_ + plane_;
}
3 changes: 3 additions & 0 deletions src/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Camera
Camera& movementSpeed(const float movementSpeed);
Camera& rotationSpeed(const float rotationSpeed);

[[nodiscard]] Vector2<float> planeLeftEdgeDirection() const;
[[nodiscard]] Vector2<float> planeRightEdgeDirection() const;

private:
Map& map_;
Vector2<float> position_;
Expand Down
91 changes: 1 addition & 90 deletions src/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "Game.h"

#include "SDLRenderer.h"
#include "WallTypes.h"

#include <SDL2/SDL.h>

Expand Down Expand Up @@ -167,7 +166,7 @@ void Game::event()
running_ = false;
break;
case SDLK_m:
overviewMapOn_ = !overviewMapOn_;
raycaster_.toggleMapDraw();
break;
}
}
Expand All @@ -182,98 +181,10 @@ void Game::update()
void Game::render()
{
renderer_->clearScreen();

raycaster_.drawEverything(*renderer_);

if (overviewMapOn_)
{
drawMap();
}

renderer_->refreshScreen();
}

// TODO: this is a very basic map
// TODO: Move this into RayCaster
void Game::drawMap()
{
static const WallColor grey = {160, 160, 160};
static const WallColor red = {255, 0, 0};
static const WallColor green = {0, 255, 0};
static const WallColor blue = {0, 0, 255};
static const WallColor yellow = {255, 255, 0};

struct Rectangle
{
size_t x;
size_t y;
size_t width;
size_t height;
};

Rectangle rect;

// Draw blocks
const size_t squareSize = 32;
for (size_t row = 0; row < map_.rowCount(); ++row)
{
for (size_t column = 0; column < map_.columnCount(); ++column)
{
WallColor wallColor = grey;
switch (map_.position(row, column))
{
case RED_WALL:
wallColor = red;
break;
case GREEN_WALL:
wallColor = green;
break;
case BLUE_WALL:
wallColor = blue;
break;
case YELLOW_WALL:
wallColor = yellow;
break;
}
renderer_->setDrawColor(wallColor.red, wallColor.green, wallColor.blue);

// Watch out: row/column is not the same as x/y. This was a source of a nasty bug.
rect = {0 + squareSize * column, 0 + squareSize * row, squareSize, squareSize};
renderer_->fillRectangle(
static_cast<int>(rect.x),
static_cast<int>(rect.y),
static_cast<int>(rect.width),
static_cast<int>(rect.height));
renderer_->setDrawColor(0, 0, 0);
renderer_->drawRectangle(
static_cast<int>(rect.x),
static_cast<int>(rect.y),
static_cast<int>(rect.width),
static_cast<int>(rect.height));
}
}

// Draw player
renderer_->setDrawColor(255, 255, 255);
// HACK: need to change internal representation of the map instead switching x/y here
rect = {
squareSize * static_cast<int>(camera_.position().y) + squareSize / 4,
squareSize * static_cast<int>(camera_.position().x) + squareSize / 4,
squareSize / 2,
squareSize / 2};
renderer_->fillRectangle(
static_cast<int>(rect.x),
static_cast<int>(rect.y),
static_cast<int>(rect.width),
static_cast<int>(rect.height));
renderer_->setDrawColor(0, 0, 0, 255);
renderer_->drawRectangle(
static_cast<int>(rect.x),
static_cast<int>(rect.y),
static_cast<int>(rect.width),
static_cast<int>(rect.height));
}

int main(int /*argc*/, char** /*argv*/)
{
Game game;
Expand Down
3 changes: 0 additions & 3 deletions src/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,10 @@ class Game
std::unique_ptr<IRenderer> renderer_;
float movementSpeed_;
bool running_{false};
bool overviewMapOn_{false};

void event();
void update();
void render();

void drawMap();
};

#endif
4 changes: 3 additions & 1 deletion src/Map.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MAP_H
#define MAP_H

#include <Vector2.h>
#include "Vector2.h"

#include <optional>
#include <string>
Expand All @@ -10,6 +10,8 @@
class Map
{
public:
static constexpr size_t EMPTY_SQUARE_INDEX = 0;

static std::optional<Map> create(const std::string& mapFilePath);

Map();
Expand Down
Loading

0 comments on commit 92b311d

Please sign in to comment.