Skip to content

Commit

Permalink
chore: A refactor round
Browse files Browse the repository at this point in the history
- CMakeLists.txt tidy
- Move README.md images to `doc/img/` folder
- Rename `resources` to `assets` folder
- Rename `map.dat` to `map.txt`
- Move `main()` to `main.cpp`
- Change include guards to `#pragma once`
  • Loading branch information
balintkissdev committed Feb 27, 2024
1 parent 5b9e815 commit 917e777
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 72 deletions.
33 changes: 24 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,24 @@ if(NOT WEBBUILD_CI)
FetchContent_MakeAvailable(SetupHunter)
endif()

# Set default build type to Release
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' default.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()

set(PROJECT_NAME Raycaster)
set(PROJECT_AUTHOR "Balint Kiss")
set(PROJECT_DESCRIPTION "Pseudo-3D raycasting engine in C++17.")
set(PROJECT_URL "https://github.com/balintkissdev/raycaster-engine")

# TODO: Get version from git tag
set(PROJECT_VERSION_MAJOR 0)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 5)
set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})

project(${PROJECT_NAME} LANGUAGES CXX)
project(${PROJECT_NAME} VERSION 0.0.5) # TODO: Get version from git tag

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")

Expand Down Expand Up @@ -57,13 +72,13 @@ target_include_directories(${PROJECT_NAME} PRIVATE src)

add_subdirectory(src)

# Copy resource files
# Copy asset files
add_custom_command(
TARGET ${PROJECT_NAME}
PRE_LINK # Needed for Emscripten preload
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/resources"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/resources"
"${PROJECT_SOURCE_DIR}/assets"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/assets"
)

# TODO: Remove exceptions
Expand All @@ -80,7 +95,7 @@ if(EMSCRIPTEN)
target_link_options(${PROJECT_NAME} PRIVATE
"SHELL: -s USE_SDL=2"
"SHELL: -s INITIAL_MEMORY=33554432"
--preload-file "resources"
--preload-file "assets"
--shell-file "${PROJECT_SOURCE_DIR}/src/shell_minimal.html"
)
else()
Expand All @@ -102,18 +117,18 @@ if(WIN32)

install(FILES "${CPACK_RESOURCE_FILE_LICENSE}" "${CPACK_RESOURCE_FILE_README}" DESTINATION ".")
install(TARGETS ${PROJECT_NAME} RUNTIME CONFIGURATIONS Release DESTINATION ".")
install(DIRECTORY "${PROJECT_SOURCE_DIR}/resources/" DESTINATION "resources")
install(DIRECTORY "${PROJECT_SOURCE_DIR}/assets/" DESTINATION "assets")
set(CPACK_GENERATOR ZIP)
include(CPack)
elseif(EMSCRIPTEN)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.data" DESTINATION ".")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.js" DESTINATION ".")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.wasm" DESTINATION ".")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.html" RENAME "index.html" DESTINATION ".")
install(DIRECTORY "${PROJECT_SOURCE_DIR}/resources/" DESTINATION "resources")
install(DIRECTORY "${PROJECT_SOURCE_DIR}/assets/" DESTINATION "assets")
# Linux releases are distributed as AppImage files instead for compatibility
else()
# Keep "resources" and "share" folder separate to avoid packaging icon as asset for the app
# Keep "assets" and "share" folder separate to avoid packaging icon as asset for the app
set(ICON "${PROJECT_SOURCE_DIR}/share/icons/appimage.svg")
install(CODE
"include(${PROJECT_SOURCE_DIR}/cmake/modules/appimage.cmake)
Expand All @@ -123,7 +138,7 @@ else()
ICON \"${ICON}\"
DIR_ICON \"${ICON}\"
OUTPUT_NAME \"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-${PROJECT_VERSION}-linux-${CMAKE_SYSTEM_PROCESSOR}.AppImage\"
ASSETS \"${CMAKE_CURRENT_BINARY_DIR}/resources\"
ASSETS \"${CMAKE_CURRENT_BINARY_DIR}/assets\"
)
"
COMPONENT Runtime
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Raycaster engine

![alt tag](https://raw.githubusercontent.com/balintkissdev/raycaster-engine/master/demo.gif)
![alt tag](doc/img/demo.gif)

[Live demo](https://balintkissdev.github.io/raycaster-engine) | [Windows 64-bit download](https://github.com/balintkissdev/raycaster-engine/releases/download/0.0.5/Raycaster-0.0.5-win64.zip) | [Linux 64-bit download](https://github.com/balintkissdev/raycaster-engine/releases/download/0.0.5/Raycaster-0.0.5-linux-x86_64.AppImage)

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.
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. Made first back in 2016 and adding code since to it.

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

![alt tag](https://raw.githubusercontent.com/balintkissdev/raycaster-engine/master/demo_night.png)
![alt tag](doc/img/demo_night.png)

## Features

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_sources(${PROJECT_NAME}
Game.h
Game.cpp
IRenderer.h
main.cpp
Matrix2.h
Matrix2.inl
Map.h
Expand Down
10 changes: 4 additions & 6 deletions src/Camera.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef CAMERA_H
#define CAMERA_H

#include <cstdint>
#include <vector>
#pragma once

#include "Map.h"
#include "Vector2.h"

#include <cstdint>
#include <vector>

class Camera
{
public:
Expand Down Expand Up @@ -47,4 +46,3 @@ class Camera
float movementSpeed_, rotationSpeed_;
};

#endif
14 changes: 1 addition & 13 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Game::Game()

Game::~Game()
{
renderer_.reset();
SDL_Quit();
}

Expand All @@ -46,9 +45,8 @@ bool Game::init()
{
return false;
}
atexit(SDL_Quit);

std::optional<Map> map = Map::create("resources/map/map.dat");
std::optional<Map> map = Map::create("assets/map/map.txt");
if (!map.has_value())
{
return false;
Expand Down Expand Up @@ -188,13 +186,3 @@ void Game::render()
renderer_->refreshScreen();
}

int main(int /*argc*/, char** /*argv*/)
{
Game game;
if (!game.init())
{
return EXIT_FAILURE;
}

return game.run();
}
4 changes: 1 addition & 3 deletions src/Game.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef GAME_H
#define GAME_H
#pragma once

#include <memory>
#include <string>
Expand Down Expand Up @@ -47,4 +46,3 @@ class Game
void render();
};

#endif
4 changes: 1 addition & 3 deletions src/IRenderer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef IRENDERER_H
#define IRENDERER_H
#pragma once

#include "Texture.h"

Expand Down Expand Up @@ -30,4 +29,3 @@ class IRenderer
virtual std::optional<Texture> createTexture(const std::string& textureFilePath) = 0;
};

#endif // IRENDERER_H
4 changes: 1 addition & 3 deletions src/Map.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef MAP_H
#define MAP_H
#pragma once

#include "Vector2.h"

Expand All @@ -25,4 +24,3 @@ class Map
size_t rowCount_{0}, columnCount_{0};
};

#endif
6 changes: 3 additions & 3 deletions src/Matrix2.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef MATRIX2_H
#define MATRIX2_H
#pragma once

#include "Vector2.h"

// TODO: Can be improved with SIMD

template <typename T>
class Matrix2
{
Expand Down Expand Up @@ -52,4 +53,3 @@ inline Vector2<T> operator*(const Vector2<T>& vector, Matrix2<T> matrix);

#include "Matrix2.inl"

#endif
14 changes: 7 additions & 7 deletions src/RayCaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ bool RayCaster::init(IRenderer& renderer)
screenHeight_ = renderer.screenHeight();
drawBuffer_.resize(screenWidth_ * screenHeight_);

topTexture_ = renderer.createTexture("resources/textures/dusk_sky_texture.bmp");
topTextureNight_ = renderer.createTexture("resources/textures/night_sky_texture.bmp");
bottomTexture_ = renderer.createTexture("resources/textures/floor.bmp");
wallTextures_[0] = renderer.createTexture("resources/textures/wall0.bmp");
wallTextures_[1] = renderer.createTexture("resources/textures/wall1.bmp");
wallTextures_[2] = renderer.createTexture("resources/textures/wall2.bmp");
wallTextures_[3] = renderer.createTexture("resources/textures/wall3.bmp");
topTexture_ = renderer.createTexture("assets/textures/dusk_sky_texture.bmp");
topTextureNight_ = renderer.createTexture("assets/textures/night_sky_texture.bmp");
bottomTexture_ = renderer.createTexture("assets/textures/floor.bmp");
wallTextures_[0] = renderer.createTexture("assets/textures/wall0.bmp");
wallTextures_[1] = renderer.createTexture("assets/textures/wall1.bmp");
wallTextures_[2] = renderer.createTexture("assets/textures/wall2.bmp");
wallTextures_[3] = renderer.createTexture("assets/textures/wall3.bmp");

return topTexture_.has_value() && topTextureNight_.has_value() && bottomTexture_.has_value() &&
wallTextures_[0].has_value() && wallTextures_[1].has_value() && wallTextures_[2].has_value() &&
Expand Down
4 changes: 1 addition & 3 deletions src/RayCaster.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef RAYCASTER_H
#define RAYCASTER_H
#pragma once

#include "Camera.h"
#include "Map.h"
Expand Down Expand Up @@ -96,4 +95,3 @@ class RayCaster
float planeRightDistance_;
};

#endif
16 changes: 8 additions & 8 deletions src/SDLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ bool SDLRenderer::init(const uint16_t screenWidth, const uint16_t screenHeight,
}
SDL_ShowCursor(SDL_DISABLE);

streamableTexture_.reset(SDL_CreateTexture(
screenTexture_.reset(SDL_CreateTexture(
renderer_.get(),
SDL_GetWindowPixelFormat(window_.get()),
SDL_TEXTUREACCESS_STREAMING,
screenWidth,
screenHeight));
if (!streamableTexture_)
if (!screenTexture_)
{
std::cerr << SDL_GetError();
return false;
Expand Down Expand Up @@ -106,13 +106,13 @@ void SDLRenderer::fillRectangle(const int x, const int y, const int width, const

void SDLRenderer::drawBuffer(uint32_t* drawBuffer)
{
void* streamableTexturePixels = nullptr;
int streamableTexturePitch;
SDL_LockTexture(streamableTexture_.get(), nullptr, &streamableTexturePixels, &streamableTexturePitch);
memcpy(streamableTexturePixels, drawBuffer, streamableTexturePitch * screenHeight_);
SDL_UnlockTexture(streamableTexture_.get());
void* screenTexturePixels = nullptr;
int screenTexturePitch;
SDL_LockTexture(screenTexture_.get(), nullptr, &screenTexturePixels, &screenTexturePitch);
memcpy(screenTexturePixels, drawBuffer, screenTexturePitch * screenHeight_);
SDL_UnlockTexture(screenTexture_.get());

SDL_RenderCopy(renderer_.get(), streamableTexture_.get(), nullptr, nullptr);
SDL_RenderCopy(renderer_.get(), screenTexture_.get(), nullptr, nullptr);
}

std::optional<Texture> SDLRenderer::createTexture(const std::string& textureFilePath)
Expand Down
6 changes: 2 additions & 4 deletions src/SDLRenderer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef SDLRENDERER_H
#define SDLRENDERER_H
#pragma once

#include "IRenderer.h"

Expand Down Expand Up @@ -49,7 +48,6 @@ class SDLRenderer : public IRenderer
SDLRendererPtr renderer_;
uint16_t screenWidth_;
uint16_t screenHeight_;
SDLTexturePtr streamableTexture_;
SDLTexturePtr screenTexture_;
};

#endif // SDLRENDERER_H
4 changes: 1 addition & 3 deletions src/Texture.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#pragma once

#include <cstddef>
#include <cstdint>
Expand All @@ -13,4 +12,3 @@ struct Texture
std::vector<uint32_t> texels;
};

#endif
4 changes: 1 addition & 3 deletions src/Vector2.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef VECTOR2_H
#define VECTOR2_H
#pragma once

template <typename T>
struct Vector2
Expand Down Expand Up @@ -44,4 +43,3 @@ inline Vector2<T> operator/(Vector2<T> vector, const T& scalar);

#include "Vector2.inl"

#endif
13 changes: 13 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Game.h"

int main(int /*argc*/, char** /*argv*/)
{
Game game;
if (!game.init())
{
return EXIT_FAILURE;
}

return game.run();
}

0 comments on commit 917e777

Please sign in to comment.