mirror of
https://github.com/vale981/mazetd
synced 2025-03-04 09:21:40 -05:00
restructure, not compiling
This commit is contained in:
parent
45e61cc5c4
commit
759d7dce40
22 changed files with 428 additions and 262 deletions
0
.ccls-root
Normal file
0
.ccls-root
Normal file
6
.clang-format
Normal file
6
.clang-format
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 2
|
||||
---
|
||||
Language: Cpp
|
||||
...
|
146
.clang-tidy
Normal file
146
.clang-tidy
Normal file
|
@ -0,0 +1,146 @@
|
|||
---
|
||||
Checks: 'clang-diagnostic-*,clang-analyser-*,-clang-diagnostic-unused-command-line-argument,google-*,bugprone-*,modernize-*,performance-*,portability-*,readability-*,cppcoreguidelines-*'
|
||||
WarningsAsErrors: 'clang-diagnostic-*,clang-analyser-*,-clang-diagnostic-unused-command-line-argument,google-*,bugprone-*,modernize-*,performance-*,portability-*,readability-*,cppcoreguidelines-*'
|
||||
HeaderFilterRegex: '.*'
|
||||
AnalyzeTemporaryDtors: false
|
||||
FormatStyle: file
|
||||
User: hiro
|
||||
CheckOptions:
|
||||
- key: bugprone-argument-comment.StrictMode
|
||||
value: '0'
|
||||
- key: bugprone-assert-side-effect.AssertMacros
|
||||
value: assert
|
||||
- key: bugprone-assert-side-effect.CheckFunctionCalls
|
||||
value: '0'
|
||||
- key: bugprone-dangling-handle.HandleClasses
|
||||
value: 'std::basic_string_view;std::experimental::basic_string_view'
|
||||
- key: bugprone-string-constructor.LargeLengthThreshold
|
||||
value: '8388608'
|
||||
- key: bugprone-string-constructor.WarnOnLargeLength
|
||||
value: '1'
|
||||
- key: google-build-namespaces.HeaderFileExtensions
|
||||
value: ',h,hh,hpp,hxx'
|
||||
- key: google-global-names-in-headers.HeaderFileExtensions
|
||||
value: ',h,hh,hpp,hxx'
|
||||
- key: google-readability-braces-around-statements.ShortStatementLines
|
||||
value: '1'
|
||||
- key: google-readability-function-size.BranchThreshold
|
||||
value: '4294967295'
|
||||
- key: google-readability-function-size.LineThreshold
|
||||
value: '4294967295'
|
||||
- key: google-readability-function-size.NestingThreshold
|
||||
value: '4294967295'
|
||||
- key: google-readability-function-size.ParameterThreshold
|
||||
value: '4294967295'
|
||||
- key: google-readability-function-size.StatementThreshold
|
||||
value: '800'
|
||||
- key: google-readability-namespace-comments.ShortNamespaceLines
|
||||
value: '10'
|
||||
- key: google-readability-namespace-comments.SpacesBeforeComments
|
||||
value: '2'
|
||||
- key: google-runtime-int.SignedTypePrefix
|
||||
value: int
|
||||
- key: google-runtime-int.TypeSuffix
|
||||
value: ''
|
||||
- key: google-runtime-int.UnsignedTypePrefix
|
||||
value: uint
|
||||
- key: google-runtime-references.WhiteListTypes
|
||||
value: ''
|
||||
- key: modernize-loop-convert.MaxCopySize
|
||||
value: '16'
|
||||
- key: modernize-loop-convert.MinConfidence
|
||||
value: reasonable
|
||||
- key: modernize-loop-convert.NamingStyle
|
||||
value: CamelCase
|
||||
- key: modernize-make-shared.IgnoreMacros
|
||||
value: '1'
|
||||
- key: modernize-make-shared.IncludeStyle
|
||||
value: '0'
|
||||
- key: modernize-make-shared.MakeSmartPtrFunction
|
||||
value: 'std::make_shared'
|
||||
- key: modernize-make-shared.MakeSmartPtrFunctionHeader
|
||||
value: memory
|
||||
- key: modernize-make-unique.IgnoreMacros
|
||||
value: '1'
|
||||
- key: modernize-make-unique.IncludeStyle
|
||||
value: '0'
|
||||
- key: modernize-make-unique.MakeSmartPtrFunction
|
||||
value: 'std::make_unique'
|
||||
- key: modernize-make-unique.MakeSmartPtrFunctionHeader
|
||||
value: memory
|
||||
- key: modernize-pass-by-value.IncludeStyle
|
||||
value: llvm
|
||||
- key: modernize-pass-by-value.ValuesOnly
|
||||
value: '0'
|
||||
- key: modernize-raw-string-literal.ReplaceShorterLiterals
|
||||
value: '0'
|
||||
- key: modernize-replace-auto-ptr.IncludeStyle
|
||||
value: llvm
|
||||
- key: modernize-replace-random-shuffle.IncludeStyle
|
||||
value: llvm
|
||||
- key: modernize-use-auto.RemoveStars
|
||||
value: '0'
|
||||
- key: modernize-use-default-member-init.IgnoreMacros
|
||||
value: '1'
|
||||
- key: modernize-use-default-member-init.UseAssignment
|
||||
value: '0'
|
||||
- key: modernize-use-emplace.ContainersWithPushBack
|
||||
value: '::std::vector;::std::list;::std::deque'
|
||||
- key: modernize-use-emplace.SmartPointers
|
||||
value: '::std::shared_ptr;::std::unique_ptr;::std::auto_ptr;::std::weak_ptr'
|
||||
- key: modernize-use-emplace.TupleMakeFunctions
|
||||
value: '::std::make_pair;::std::make_tuple'
|
||||
- key: modernize-use-emplace.TupleTypes
|
||||
value: '::std::pair;::std::tuple'
|
||||
- key: modernize-use-equals-default.IgnoreMacros
|
||||
value: '1'
|
||||
- key: modernize-use-noexcept.ReplacementString
|
||||
value: ''
|
||||
- key: modernize-use-noexcept.UseNoexceptFalse
|
||||
value: '1'
|
||||
- key: modernize-use-nullptr.NullMacros
|
||||
value: 'NULL'
|
||||
- key: modernize-use-transparent-functors.SafeMode
|
||||
value: '0'
|
||||
- key: modernize-use-using.IgnoreMacros
|
||||
value: '1'
|
||||
- key: performance-faster-string-find.StringLikeClasses
|
||||
value: 'std::basic_string'
|
||||
- key: performance-for-range-copy.WarnOnAllAutoCopies
|
||||
value: '0'
|
||||
- key: performance-inefficient-string-concatenation.StrictMode
|
||||
value: '0'
|
||||
- key: performance-inefficient-vector-operation.VectorLikeClasses
|
||||
value: '::std::vector'
|
||||
- key: performance-move-const-arg.CheckTriviallyCopyableMove
|
||||
value: '1'
|
||||
- key: performance-move-constructor-init.IncludeStyle
|
||||
value: llvm
|
||||
- key: performance-type-promotion-in-math-fn.IncludeStyle
|
||||
value: llvm
|
||||
- key: performance-unnecessary-value-param.IncludeStyle
|
||||
value: llvm
|
||||
- key: readability-braces-around-statements.ShortStatementLines
|
||||
value: '0'
|
||||
- key: readability-function-size.BranchThreshold
|
||||
value: '4294967295'
|
||||
- key: readability-function-size.LineThreshold
|
||||
value: '4294967295'
|
||||
- key: readability-function-size.NestingThreshold
|
||||
value: '4294967295'
|
||||
- key: readability-function-size.ParameterThreshold
|
||||
value: '4294967295'
|
||||
- key: readability-function-size.StatementThreshold
|
||||
value: '800'
|
||||
- key: readability-identifier-naming.IgnoreFailedSplit
|
||||
value: '0'
|
||||
- key: readability-implicit-bool-conversion.AllowIntegerConditions
|
||||
value: '0'
|
||||
- key: readability-implicit-bool-conversion.AllowPointerConditions
|
||||
value: '0'
|
||||
- key: readability-simplify-boolean-expr.ChainedConditionalAssignment
|
||||
value: '0'
|
||||
- key: readability-simplify-boolean-expr.ChainedConditionalReturn
|
||||
value: '0'
|
||||
- key: readability-static-accessed-through-instance.NameSpecifierNestingThreshold
|
||||
value: '3'
|
1
compile_commands.json
Symbolic link
1
compile_commands.json
Symbolic link
|
@ -0,0 +1 @@
|
|||
build/compile_commands.json
|
17
factory.hpp
17
factory.hpp
|
@ -1,17 +0,0 @@
|
|||
#ifndef FACTORY_H
|
||||
#define FACTORY_H 1
|
||||
#include <entt/entt.hpp>
|
||||
#include "definitions.h"
|
||||
#include "components/components.hpp"
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
entt::entity create_bg_tile(entt::registry ®, char index, const sf::Vector2i &pos) {
|
||||
auto e = reg.create();
|
||||
|
||||
reg.assign<Tile>(e, pos, index);
|
||||
reg.assign<Background>(e);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -1 +0,0 @@
|
|||
#include "maze_generator.hpp"
|
|
@ -1,149 +0,0 @@
|
|||
#ifndef MAZE_GENERATOR_H
|
||||
#define MAZE_GENERATOR_H 1
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <entt/entt.hpp>
|
||||
#include <random>
|
||||
#include <stack>
|
||||
#include "../factory.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void dump_maze(std::vector<bool> &maze, int width, int height) {
|
||||
for (int y {0}; y < height; y++) {
|
||||
for (int x {0}; x < width; x++) {
|
||||
std::cout << (maze.at(x + y*width) ? " " : "█");
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... MazeComponentTypes>
|
||||
std::vector<entt::entity>
|
||||
generate_maze(entt::registry ®,
|
||||
int width, int height, unsigned long seed) {
|
||||
|
||||
// TODO: check width, height
|
||||
using intpair = std::pair<int, int>;
|
||||
std::uniform_int_distribution<int> uniform_dist{0, 4}; // Note: think of a better way
|
||||
std::default_random_engine engine{seed};
|
||||
|
||||
std::vector<bool> maze(width*height, false);
|
||||
std::stack<int> stack;
|
||||
|
||||
maze.at(0) = true;
|
||||
stack.push(0);
|
||||
|
||||
auto wrap_x =
|
||||
[=](int x) {
|
||||
return x < 0 ? (width + x) : (x % width);
|
||||
};
|
||||
|
||||
auto wrap_y =
|
||||
[=](int y) {
|
||||
return y < 0 ? (height + y) : (y % height);
|
||||
};
|
||||
|
||||
auto wrap_cell =
|
||||
[=](std::pair<int, int> in) {
|
||||
return std::make_pair(wrap_x(in.first), wrap_y(in.second));
|
||||
};
|
||||
|
||||
auto get_cell =
|
||||
[=](std::pair<int, int> in) {
|
||||
return in.first + width*in.second;
|
||||
};
|
||||
|
||||
auto get_coords =
|
||||
[=] (int current) {
|
||||
auto x = (current % width);
|
||||
return std::make_pair(x, (current - x) / width);
|
||||
};
|
||||
|
||||
while (!stack.empty()) {
|
||||
auto current = stack.top();
|
||||
auto [x, y] = get_coords(current);
|
||||
stack.pop();
|
||||
|
||||
/*
|
||||
1
|
||||
2 x 3 -- cell layout
|
||||
4
|
||||
|
||||
We use toroidal boundary conditions.
|
||||
*/
|
||||
|
||||
std::array<intpair, 4> neightbours
|
||||
{{ {x, y - 2}, {x, y + 2}, {x - 2, y}, {x + 2, y} }};
|
||||
std::transform(neightbours.begin(), neightbours.end(),
|
||||
neightbours.begin(), wrap_cell);
|
||||
|
||||
std::array<intpair, 4> walls
|
||||
{{ {x, y - 1}, {x, y + 1}, {x - 1, y}, {x + 1, y} }};
|
||||
std::transform(walls.begin(), walls.end(),
|
||||
walls.begin(), wrap_cell);
|
||||
|
||||
std::vector<char> unvisited;
|
||||
unvisited.reserve(4);
|
||||
|
||||
for (char i {0}; i < 4; i++) {
|
||||
if(!maze.at(get_cell(neightbours.at(i)))) {
|
||||
unvisited.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (unvisited.empty())
|
||||
continue;
|
||||
|
||||
stack.push(current);
|
||||
int chosen = unvisited.at(uniform_dist(engine) % unvisited.size());
|
||||
|
||||
auto neighbour = get_cell(neightbours[chosen]);
|
||||
maze.at(get_cell(walls[chosen])) = true;
|
||||
maze.at(neighbour) = true;
|
||||
stack.push(neighbour);
|
||||
|
||||
|
||||
spd::debug("x={} y={} chosen=[ x={} y={} ] stack[{}]={}",
|
||||
x, y, neightbours.at(chosen).first, neightbours.at(chosen).second,
|
||||
stack.size() - 1, stack.top());
|
||||
dump_maze(maze, width, height);
|
||||
}
|
||||
|
||||
std::vector<entt::entity> maze_entities;
|
||||
maze_entities.reserve(width*height);
|
||||
|
||||
for (int y {0}; y < height; y++) {
|
||||
for (int x {0}; x < width; x++) {
|
||||
auto e = create_bg_tile(reg, maze[x + y*width] ? 0 : 1, {x, y});
|
||||
maze_entities.push_back(e);
|
||||
}
|
||||
}
|
||||
|
||||
return maze_entities;
|
||||
}
|
||||
|
||||
|
||||
std::vector<entt::entity>
|
||||
generate_maze(entt::registry ®, int width, int height) {
|
||||
std::random_device rd;
|
||||
return generate_maze(reg, width, height, rd());
|
||||
}
|
||||
|
||||
// class MazeGenerator {
|
||||
// public:
|
||||
// MazeGenerator(const uint width, const uint height)
|
||||
// : m_width{width}, m_height{height} {};
|
||||
|
||||
// void generate(int);
|
||||
|
||||
// private:
|
||||
// bool **m_maze;
|
||||
|
||||
// const uint m_width;
|
||||
// const uint m_height;
|
||||
|
||||
// const std::random_device m_rand;
|
||||
// };
|
||||
|
||||
#endif
|
47
mazetd.cpp
47
mazetd.cpp
|
@ -1,47 +0,0 @@
|
|||
#include "mazetd.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "maze_generator/maze_generator.hpp"
|
||||
|
||||
Game::Game()
|
||||
: m_reg{}, m_game_state{Game::running},
|
||||
m_background_system{config.bg_tilemap_location, config.width,
|
||||
config.height, sf::Vector2u{64, 64}, 2,
|
||||
m_reg}
|
||||
{
|
||||
m_reg.set<config_t>(config);
|
||||
|
||||
// test changing
|
||||
auto maze = generate_maze(m_reg, config.width, config.height, 1);
|
||||
m_background_system.update();
|
||||
|
||||
// create the window
|
||||
sf::RenderWindow window(sf::VideoMode(512, 512), "Tilemap");
|
||||
window.setFramerateLimit(config.FPS);
|
||||
while (window.isOpen())
|
||||
{
|
||||
// handle events
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event))
|
||||
{
|
||||
if(event.type == sf::Event::Closed)
|
||||
window.close();
|
||||
}
|
||||
|
||||
// draw the map
|
||||
window.clear();
|
||||
window.draw(m_background_system);
|
||||
window.display();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void set_up_logging() {
|
||||
spd::set_level(config.log_level);
|
||||
}
|
||||
|
||||
int main() {
|
||||
set_up_logging();
|
||||
Game game{};
|
||||
|
||||
return 0;
|
||||
}
|
30
meson.build
30
meson.build
|
@ -3,6 +3,15 @@ project('mazetd', 'cpp',
|
|||
default_options : ['warning_level=3',
|
||||
'cpp_std=c++17'])
|
||||
|
||||
|
||||
project_sources = []
|
||||
project_header_files = []
|
||||
|
||||
inc = [include_directories('subprojects/entt/single_include/'),
|
||||
include_directories('src')]
|
||||
|
||||
subdir('src')
|
||||
|
||||
sfml_deps = []
|
||||
sfml_modules = ['window', 'graphics', 'system']
|
||||
|
||||
|
@ -13,9 +22,26 @@ endforeach
|
|||
spdlog_proj = subproject('spdlog')
|
||||
spdlog_dep = spdlog_proj.get_variable('spdlog_dep')
|
||||
|
||||
# This adds the clang format file to the build directory
|
||||
configure_file(input : '.clang-format',
|
||||
output : '.clang-format',
|
||||
copy: true)
|
||||
|
||||
exe = executable('mazetd', ['mazetd.cpp'],
|
||||
include_directories: include_directories('subprojects/entt/single_include/'),
|
||||
exe = executable('mazetd', project_sources,
|
||||
include_directories: inc,
|
||||
dependencies : sfml_deps + [spdlog_dep])
|
||||
|
||||
run_target('format',
|
||||
command : ['clang-format','-i','-style=file', project_sources, project_header_files])
|
||||
|
||||
test('basic', exe)
|
||||
|
||||
regex = '^((?!(third_party|tests|benchmarks|gtest|subprojects)).)*$'
|
||||
|
||||
# This adds clang tidy support
|
||||
configure_file(input : '.clang-tidy',
|
||||
output : '.clang-tidy',
|
||||
copy : true)
|
||||
|
||||
run_target('tidy',
|
||||
command : ['run-clang-tidy.py','-fix', '-j' , '8', 'files', regex ,'-format', '-p='+ meson.build_root()])
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
#ifndef COMPONENTS_H
|
||||
#define COMPONENTS_H 1
|
||||
#include <string>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include "../tilemap/tilemap.hpp"
|
||||
#include "spdlog/fmt/ostr.h"
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <string>
|
||||
|
||||
struct Background {};
|
||||
|
||||
|
||||
/**
|
||||
Tile component for discrete blocks.
|
||||
**/
|
1
src/components/meson.build
Normal file
1
src/components/meson.build
Normal file
|
@ -0,0 +1 @@
|
|||
project_header_files += files('components.hpp')
|
|
@ -7,5 +7,4 @@
|
|||
#include <spdlog/spdlog.h>
|
||||
namespace spd = spdlog;
|
||||
|
||||
|
||||
#endif
|
36
src/factory.hpp
Normal file
36
src/factory.hpp
Normal file
|
@ -0,0 +1,36 @@
|
|||
#ifndef FACTORY_H
|
||||
#define FACTORY_H 1
|
||||
#include "./maze_generator/maze_generator.hpp"
|
||||
#include "components/components.hpp"
|
||||
#include "definitions.h"
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
namespace factory {
|
||||
entt::entity bg_tile(entt::registry ®, char index, const sf::Vector2i &pos) {
|
||||
auto e = reg.create();
|
||||
|
||||
reg.assign<Tile>(e, pos, index);
|
||||
reg.assign<Background>(e);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
std::vector<entt::entity> background_maze(entt::registry ®, int width,
|
||||
int height) {
|
||||
auto maze = maze_generator::generate_maze(width, height);
|
||||
std::vector<entt::entity> maze_entities;
|
||||
maze_entities.reserve(width * height);
|
||||
|
||||
for (int y{0}; y < height; y++) {
|
||||
for (int x{0}; x < width; x++) {
|
||||
auto e = bg_tile(reg, maze[x + y * width] ? 0 : 1, {x, y});
|
||||
maze_entities.push_back(e);
|
||||
}
|
||||
}
|
||||
|
||||
return maze_entities;
|
||||
}
|
||||
} // namespace factory
|
||||
|
||||
#endif
|
81
src/maze_generator/maze_generator.cpp
Normal file
81
src/maze_generator/maze_generator.cpp
Normal file
|
@ -0,0 +1,81 @@
|
|||
#include "maze_generator/maze_generator.hpp"
|
||||
#include <cassert>
|
||||
#include <array>
|
||||
#include <stack>
|
||||
|
||||
using namespace maze_generator;
|
||||
|
||||
std::vector<bool> generate_maze(const int width, const int height, const unsigned long seed) {
|
||||
assert(width > 0 && height > 0 && "Width and height have to be positive.");
|
||||
|
||||
using intpair = std::pair<int, int>;
|
||||
std::uniform_int_distribution<int> uniform_dist{
|
||||
0, 4}; // Note: think of a better way
|
||||
std::default_random_engine engine{seed};
|
||||
|
||||
std::vector<bool> maze(width * height, false);
|
||||
std::stack<int> stack;
|
||||
|
||||
maze.at(0) = true;
|
||||
stack.push(0);
|
||||
|
||||
auto get_cell = [=](std::pair<int, int> in) {
|
||||
return in.first + width * in.second;
|
||||
};
|
||||
|
||||
auto get_coords = [=](int current) {
|
||||
auto x = (current % width);
|
||||
return std::make_pair(x, (current - x) / width);
|
||||
};
|
||||
|
||||
while (!stack.empty()) {
|
||||
auto current = stack.top();
|
||||
auto [x, y] = get_coords(current);
|
||||
stack.pop();
|
||||
|
||||
/*
|
||||
1
|
||||
2 x 3 -- cell layout
|
||||
4
|
||||
|
||||
We use toroidal boundary conditions.
|
||||
*/
|
||||
|
||||
std::array<intpair, 4> neighbours{
|
||||
{{x, y - 2}, {x, y + 2}, {x - 2, y}, {x + 2, y}}};
|
||||
|
||||
std::array<intpair, 4> walls // FIXME: i know... kinda to much
|
||||
{{{x, y - 1}, {x, y + 1}, {x - 1, y}, {x + 1, y}}};
|
||||
|
||||
std::vector<char> unvisited;
|
||||
unvisited.reserve(4);
|
||||
|
||||
for (char i{0}; i < 4; i++) {
|
||||
auto neighbour{neighbours.at(i)};
|
||||
if (neighbour.first >= 0 && neighbour.second >= 0 &&
|
||||
neighbour.first < height && neighbour.second < width &&
|
||||
!maze.at(get_cell(neighbour))) {
|
||||
unvisited.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (unvisited.empty())
|
||||
continue;
|
||||
|
||||
stack.push(current);
|
||||
int chosen = unvisited.at(uniform_dist(engine) % unvisited.size());
|
||||
|
||||
auto neighbour = get_cell(neighbours[chosen]);
|
||||
maze.at(get_cell(walls[chosen])) = true;
|
||||
maze.at(neighbour) = true;
|
||||
stack.push(neighbour);
|
||||
|
||||
// spd::debug("x={} y={} chosen=[ x={} y={} ] stack[{}]={}",
|
||||
// x, y, neighbours.at(chosen).first,
|
||||
// neighbours.at(chosen).second, stack.size() - 1,
|
||||
// stack.top());
|
||||
// dump_maze(maze, width, height);
|
||||
}
|
||||
|
||||
return maze;
|
||||
}
|
28
src/maze_generator/maze_generator.hpp
Normal file
28
src/maze_generator/maze_generator.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef MAZE_GENERATOR_H
|
||||
#define MAZE_GENERATOR_H 1
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
namespace maze_generator {
|
||||
void dump_maze(const std::vector<bool> &maze, const int width, const int height) {
|
||||
for (int y{0}; y < height; y++) {
|
||||
for (int x{0}; x < width; x++) {
|
||||
std::cout << (maze.at(x + y * width) ? " " : "█");
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<bool> generate_maze(const int width, const int height);
|
||||
std::vector<bool> generate_maze(const int width, const int height, const unsigned long seed);
|
||||
|
||||
std::vector<bool> generate_maze(const int width, const int height) {
|
||||
std::random_device rd;
|
||||
return generate_maze(width, height, rd());
|
||||
}
|
||||
|
||||
} // namespace maze_generator
|
||||
|
||||
#endif
|
2
src/maze_generator/meson.build
Normal file
2
src/maze_generator/meson.build
Normal file
|
@ -0,0 +1,2 @@
|
|||
project_header_files += files('maze_generator.hpp')
|
||||
project_sources += files('maze_generator.cpp')
|
41
src/mazetd.cpp
Normal file
41
src/mazetd.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include "mazetd.hpp"
|
||||
|
||||
Game::Game()
|
||||
: m_reg{}, m_game_state{Game::running}, m_background_system{
|
||||
config.bg_tilemap_location,
|
||||
config.width,
|
||||
config.height,
|
||||
sf::Vector2u{64, 64},
|
||||
2,
|
||||
m_reg} {
|
||||
m_reg.set<config_t>(config);
|
||||
|
||||
// create the window
|
||||
sf::RenderWindow window(sf::VideoMode(512, 512), "Tilemap");
|
||||
window.setFramerateLimit(config.FPS);
|
||||
while (window.isOpen()) {
|
||||
// handle events
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed)
|
||||
window.close();
|
||||
}
|
||||
|
||||
auto maze = factory::background_maze(m_reg, config.width, config.height);
|
||||
m_background_system.update();
|
||||
|
||||
// draw the map
|
||||
window.clear();
|
||||
window.draw(m_background_system);
|
||||
window.display();
|
||||
}
|
||||
}
|
||||
|
||||
void set_up_logging() { spd::set_level(config.log_level); }
|
||||
|
||||
int main() {
|
||||
set_up_logging();
|
||||
Game game{};
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -2,18 +2,20 @@
|
|||
#define MAZETD_H 1
|
||||
|
||||
#include "definitions.h"
|
||||
#include <SFML/Graphics/VertexArray.hpp>
|
||||
#include <string>
|
||||
#include <entt/entt.hpp>
|
||||
#include "tilemap/tilemap.hpp"
|
||||
#include "factory.hpp"
|
||||
#include "tilemap/tilemap.hpp"
|
||||
//#include "maze_generator/maze_generator.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Graphics/VertexArray.hpp>
|
||||
#include <entt/entt.hpp>
|
||||
#include <string>
|
||||
|
||||
const struct config_t {
|
||||
const unsigned int height{14};
|
||||
const unsigned int width{14};
|
||||
const unsigned int height{15};
|
||||
const unsigned int width{15};
|
||||
const spd::level::level_enum log_level{spd::level::debug};
|
||||
const std::string bg_tilemap_location{"./assets/bg_tilemap.tga"};
|
||||
const unsigned int FPS{60};
|
||||
const unsigned int FPS{1};
|
||||
} config;
|
||||
|
||||
class Game {
|
||||
|
@ -28,5 +30,4 @@ private:
|
|||
TileMapSystem<Background> m_background_system;
|
||||
};
|
||||
|
||||
|
||||
#endif // MAZETD_H
|
6
src/meson.build
Normal file
6
src/meson.build
Normal file
|
@ -0,0 +1,6 @@
|
|||
project_sources += files('mazetd.cpp')
|
||||
project_header_files += files(['definitions.h', 'mazetd.hpp', 'factory.hpp'])
|
||||
|
||||
subdir('components')
|
||||
subdir('maze_generator')
|
||||
subdir('tilemap')
|
2
src/tilemap/meson.build
Normal file
2
src/tilemap/meson.build
Normal file
|
@ -0,0 +1,2 @@
|
|||
project_sources += files('tilemap.cpp')
|
||||
project_header_files += files('tilemap.hpp')
|
|
@ -1,49 +1,49 @@
|
|||
#ifndef TILEMAP_H
|
||||
#define TILEMAP_H 1
|
||||
|
||||
#include <SFML/Graphics/VertexArray.hpp>
|
||||
#include <SFML/Graphics/Transformable.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Graphics/RenderTarget.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
#include <SFML/Graphics/Transformable.hpp>
|
||||
#include <SFML/Graphics/VertexArray.hpp>
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
#include "../components/components.hpp"
|
||||
#include "../definitions.h"
|
||||
#include "components/components.hpp"
|
||||
#include "definitions.h"
|
||||
|
||||
template <typename ComponentType>
|
||||
class TileMapSystem : public sf::Drawable, public sf::Transformable {
|
||||
public:
|
||||
TileMapSystem(const std::string &tileset, const unsigned int width,
|
||||
const unsigned int height, const sf::Vector2u tile_size,
|
||||
const unsigned int tile_count, entt::registry ®) :
|
||||
m_width{width}, m_height{height}, m_tile_size{tile_size},
|
||||
m_tile_count{tile_count}, m_reg{reg},
|
||||
m_observer{m_reg, entt::collector.replace<Tile>().where<ComponentType>()}
|
||||
{
|
||||
const unsigned int tile_count, entt::registry ®)
|
||||
: m_width{width}, m_height{height}, m_tile_size{tile_size},
|
||||
m_tile_count{tile_count}, m_reg{reg},
|
||||
m_observer{m_reg,
|
||||
entt::collector.replace<Tile>().where<ComponentType>()} {
|
||||
spd::debug("Creating {}", *this);
|
||||
|
||||
if (!m_tileset.loadFromFile(tileset)) {
|
||||
throw std::filesystem::filesystem_error
|
||||
(("Could not load the tileset: " + tileset).c_str(),
|
||||
std::error_code());
|
||||
throw std::filesystem::filesystem_error(
|
||||
("Could not load the tileset: " + tileset).c_str(),
|
||||
std::error_code());
|
||||
}
|
||||
|
||||
m_vertices.setPrimitiveType(sf::Quads);
|
||||
m_vertices.resize(width * height * 4);
|
||||
|
||||
m_reg.on_construct<ComponentType>()
|
||||
.template connect<&TileMapSystem<ComponentType>::on_tile_create>(*this);
|
||||
.template connect<&TileMapSystem<ComponentType>::on_tile_create>(*this);
|
||||
}
|
||||
|
||||
~TileMapSystem() {
|
||||
m_reg.on_construct<ComponentType>()
|
||||
.template disconnect<&TileMapSystem<ComponentType>::on_tile_create>(*this);
|
||||
|
||||
.template disconnect<&TileMapSystem<ComponentType>::on_tile_create>(
|
||||
*this);
|
||||
}
|
||||
|
||||
void update() {
|
||||
|
@ -60,30 +60,38 @@ public:
|
|||
|
||||
int tu = tile.index % (m_tileset.getSize().x / m_tile_size.x);
|
||||
int tv = tile.index / (m_tileset.getSize().x / m_tile_size.x);
|
||||
sf::Vertex* quad = &m_vertices[(pos.x + pos.y * m_width) * 4];
|
||||
sf::Vertex *quad = &m_vertices[(pos.x + pos.y * m_width) * 4];
|
||||
|
||||
// define its 4 corners
|
||||
quad[0].position = sf::Vector2f(pos.x * m_tile_size.x, pos.y * m_tile_size.y);
|
||||
quad[1].position = sf::Vector2f((pos.x + 1) * m_tile_size.x, pos.y * m_tile_size.y);
|
||||
quad[2].position = sf::Vector2f((pos.x + 1) * m_tile_size.x, (pos.y + 1) * m_tile_size.y);
|
||||
quad[3].position = sf::Vector2f(pos.x * m_tile_size.x, (pos.y + 1) * m_tile_size.y);
|
||||
quad[0].position =
|
||||
sf::Vector2f(pos.x * m_tile_size.x, pos.y * m_tile_size.y);
|
||||
quad[1].position =
|
||||
sf::Vector2f((pos.x + 1) * m_tile_size.x, pos.y * m_tile_size.y);
|
||||
quad[2].position =
|
||||
sf::Vector2f((pos.x + 1) * m_tile_size.x, (pos.y + 1) * m_tile_size.y);
|
||||
quad[3].position =
|
||||
sf::Vector2f(pos.x * m_tile_size.x, (pos.y + 1) * m_tile_size.y);
|
||||
|
||||
// define its 4 texture coordinates
|
||||
quad[0].texCoords = sf::Vector2f(tu * m_tile_size.x, tv * m_tile_size.y);
|
||||
quad[1].texCoords = sf::Vector2f((tu + 1) * m_tile_size.x, tv * m_tile_size.y);
|
||||
quad[2].texCoords = sf::Vector2f((tu + 1) * m_tile_size.x, (tv + 1) * m_tile_size.y);
|
||||
quad[3].texCoords = sf::Vector2f(tu * m_tile_size.x, (tv + 1) * m_tile_size.y);
|
||||
quad[1].texCoords =
|
||||
sf::Vector2f((tu + 1) * m_tile_size.x, tv * m_tile_size.y);
|
||||
quad[2].texCoords =
|
||||
sf::Vector2f((tu + 1) * m_tile_size.x, (tv + 1) * m_tile_size.y);
|
||||
quad[3].texCoords =
|
||||
sf::Vector2f(tu * m_tile_size.x, (tv + 1) * m_tile_size.y);
|
||||
}
|
||||
|
||||
void on_tile_create(const entt::registry ®, const entt::entity &e) {
|
||||
const auto tile = reg.get<Tile>(e);
|
||||
update(tile);
|
||||
const auto tile = reg.get<Tile>(e);
|
||||
update(tile);
|
||||
}
|
||||
|
||||
template <typename OStream>
|
||||
friend OStream &operator<<(OStream &os, const TileMapSystem<ComponentType> &c) {
|
||||
return os << typeid(TileMapSystem<ComponentType>).name() << " Width=" << c.m_width
|
||||
<< " Height=" << c.m_height ;
|
||||
friend OStream &operator<<(OStream &os,
|
||||
const TileMapSystem<ComponentType> &c) {
|
||||
return os << typeid(TileMapSystem<ComponentType>).name()
|
||||
<< " Width=" << c.m_width << " Height=" << c.m_height;
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -98,7 +106,7 @@ private:
|
|||
entt::registry &m_reg;
|
||||
entt::observer m_observer;
|
||||
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const {
|
||||
// apply the transform
|
||||
states.transform *= getTransform();
|
||||
|
||||
|
@ -110,6 +118,4 @@ private:
|
|||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // TILEMAP_H
|
Loading…
Add table
Reference in a new issue