Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ sudo apt install \
### Build

```shell
./scripts/build.sh
./scripts/build.sh --release
```
If you want to build without tests:
```shell
./scripts/build.sh --release --without-tests
```

### Play

```shell
Expand Down
7 changes: 5 additions & 2 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
file(GLOB SOURCES_CXX *.cpp)
file(GLOB_RECURSE SOURCES_CXX *.cpp)
list(FILTER SOURCES_CXX EXCLUDE REGEX ".*/tests/.*")

add_library(engine ${SOURCES_CXX})
target_link_libraries(engine PUBLIC
Expand All @@ -9,4 +10,6 @@ target_link_libraries(engine PUBLIC
cereal)
target_include_directories(engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(tests)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()
2 changes: 1 addition & 1 deletion engine/camera.cpp → engine/core/camera.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "camera.h"
#include "core/camera.h"

#include <cmath>
#include <limits>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion engine/engine.cpp → engine/core/engine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "engine.h"
#include "core/engine.h"

#include <SFML/System.hpp>
#include <SFML/Window/Event.hpp>
Expand Down
10 changes: 5 additions & 5 deletions engine/engine.h → engine/core/engine.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once

#include "camera.h"
#include "image_manager.h"
#include "input.h"
#include "loop.h"
#include "render.h"
#include "core/camera.h"
#include "core/input.h"
#include "core/loop.h"
#include "core/render.h"
#include "resources/image_manager.h"

namespace engine {

Expand Down
4 changes: 2 additions & 2 deletions engine/input.cpp → engine/core/input.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "input.h"
#include "core/input.h"

#include "render.h"
#include "core/render.h"

namespace engine {

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion engine/loop.h → engine/core/loop.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "input.h"
#include "core/input.h"
#include <memory>

namespace engine {
Expand Down
8 changes: 4 additions & 4 deletions engine/render.cpp → engine/core/render.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "render.h"
#include "core/render.h"

#include "camera.h"
#include "loop.h"
#include "tile.h"
#include "core/camera.h"
#include "core/loop.h"
#include "ecs/tile.h"
#include <cmath>

namespace engine {
Expand Down
2 changes: 1 addition & 1 deletion engine/render.h → engine/core/render.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "render_frame.h"
#include "core/render_frame.h"
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/VideoMode.hpp>
#include <mutex>
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 8 additions & 7 deletions engine/systems.cpp → engine/ecs/systems.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "systems.h"
#include "camera.h"
#include "components.h"
#include "image_manager.h"
#include "input.h"
#include "render_frame.h"
#include "utils.h"
#include "ecs/systems.h"

#include "core/camera.h"
#include "core/input.h"
#include "core/render_frame.h"
#include "ecs/components.h"
#include "ecs/utils.h"
#include "resources/image_manager.h"
#include <cmath>
#include <random>

Expand Down
4 changes: 2 additions & 2 deletions engine/systems.h → engine/ecs/systems.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "components.h"
#include "tile.h"
#include "ecs/components.h"
#include "ecs/tile.h"
#include <entt/entt.hpp>

namespace engine {
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions engine/utils.h → engine/ecs/utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#pragma once

#include "ecs/tile.h"
#include "resources/image_manager.h"
#include "resources/serializable_world.h"
#include <SFML/Graphics.hpp>
#include <algorithm>
#include <unordered_map>

namespace engine {

Expand Down Expand Up @@ -55,4 +60,26 @@ inline sf::IntRect calculateContentRect(const sf::Image &image,

return {{minX, minY}, {(maxX - minX) + 1, (maxY - minY) + 1}};
}

/**
* @brief Generates a mapping from tile IDs to their corresponding visual data.
* @param textures A map of tile IDs to their texture metadata (`TileTexture`).
* @param imgMgr Reference to the `ImageManager` used to load or retrieve textures.
* @return An unordered_map where each key is a tile ID and the value is a `TileData`
* struct containing a pointer to the image and the tile's height.
*/
inline std::unordered_map<int, TileData>
makeTileData(const std::unordered_map<int, TileTexture> &textures,
ImageManager &imgMgr) {
std::unordered_map<int, TileData> tileImages;

for (auto keyvalue : textures) {
int key = keyvalue.first;
auto tex = keyvalue.second;
tileImages[key] = {&imgMgr.getImage(tex.texture_src), tex.height};
}

return tileImages;
}

} // namespace engine
29 changes: 29 additions & 0 deletions engine/ecs/world_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "ecs/world_loader.h"

namespace engine {

void WorldLoader::loadWorldFromJson(
const std::string &filename, int &width, int &height,
std::unordered_map<int, TileTexture> &tileTextures, std::vector<Tile> &tiles) {
SerializableWorld world = of_json(filename);
height = world.world_height;
width = world.world_width;
tiles.resize(width * height);
tileTextures = world.textures;

auto getIndex = [&](int x, int y) {
return (y + height / 2) * width + (x + width / 2);
};

for (int i = 0; i < world.areas.size(); i++) {
auto a = world.areas[i];

for (int x = a.posX; x < a.posX + a.sizeX; ++x) {
for (int y = a.posY; y < a.posY + a.sizeY; ++y) {
tiles[getIndex(x, y)] = a.tile;
}
}
}
}

} // namespace engine
34 changes: 34 additions & 0 deletions engine/ecs/world_loader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "ecs/tile.h"
#include "resources/serializable_world.h"
#include <string>
#include <unordered_map>
#include <vector>

namespace engine {

/**
* @brief Loads a tile-based game world from a JSON file.
*
* Responsible for parsing a serialized world description, initializing the
* tile layout and tile texture data. Converts JSON representation of world
* areas into a flat tile vector for use by the engine.
*/
class WorldLoader {
public:
/**
* @brief Loads world data from a JSON file into provided containers.
* @param filename Path to the JSON file describing the world.
* @param width Output parameter for the width of the world in tiles.
* @param height Output parameter for the height of the world in tiles.
* @param tileTextures Output map of tile ID to `TileTexture` metadata.
* @param tiles Output vector of tiles representing the world layout.
*/
static void loadWorldFromJson(const std::string &filename, int &width,
int &height,
std::unordered_map<int, TileTexture> &tileTextures,
std::vector<Tile> &tiles);
};

} // namespace engine
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#include "serializable_world.h"

#include <cereal/archives/json.hpp>
#include <cereal/types/unordered_map.hpp>
#include <cereal/types/vector.hpp>
#include <fstream>
#include <string>
#include <tile.h>
#include <unordered_map>

namespace engine {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <cereal/archives/json.hpp>
#include <ecs/tile.h>
#include <string>
#include <tile.h>
#include <unordered_map>

namespace engine {
Expand Down
2 changes: 1 addition & 1 deletion engine/tests/camera_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "camera.h"
#include "core/camera.h"
#include "gtest/gtest.h"

const float TOLERANCE = 0.0001f;
Expand Down
4 changes: 2 additions & 2 deletions engine/tests/system_animation_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "components.h"
#include "systems.h"
#include "ecs/components.h"
#include "ecs/systems.h"
#include "gtest/gtest.h"
#include <entt/entt.hpp>

Expand Down
6 changes: 3 additions & 3 deletions engine/tests/system_create_entities_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "components.h"
#include "engine.h"
#include "systems.h"
#include "core/engine.h"
#include "ecs/components.h"
#include "ecs/systems.h"
#include "gtest/gtest.h"
#include <entt/entt.hpp>
#include <unordered_map>
Expand Down
6 changes: 3 additions & 3 deletions engine/tests/system_input_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "components.h"
#include "input.h"
#include "systems.h"
#include "core/input.h"
#include "ecs/components.h"
#include "ecs/systems.h"
#include "gtest/gtest.h"
#include <cmath>
#include <entt/entt.hpp>
Expand Down
6 changes: 3 additions & 3 deletions engine/tests/system_movement_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "components.h"
#include "input.h"
#include "systems.h"
#include "core/input.h"
#include "ecs/components.h"
#include "ecs/systems.h"
#include "gtest/gtest.h"
#include <entt/entt.hpp>

Expand Down
4 changes: 2 additions & 2 deletions engine/tests/system_npc_movement_logic_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "components.h"
#include "systems.h"
#include "ecs/components.h"
#include "ecs/systems.h"
#include "gtest/gtest.h"
#include <cmath>
#include <cstdlib>
Expand Down
Loading