From 759d7dce407f5f9f99ce21d21e8931d506d277c7 Mon Sep 17 00:00:00 2001 From: Valentin Boettcher Date: Tue, 17 Mar 2020 16:16:02 +0100 Subject: [PATCH] restructure, not compiling --- .ccls-root | 0 .clang-format | 6 + .clang-tidy | 146 +++++++++++++++++ compile_commands.json | 1 + factory.hpp | 17 -- maze_generator/maze_generator.cpp | 1 - maze_generator/maze_generator.hpp | 149 ------------------ mazetd.cpp | 47 ------ meson.build | 30 +++- {components => src/components}/components.hpp | 6 +- src/components/meson.build | 1 + definitions.h => src/definitions.h | 1 - src/factory.hpp | 36 +++++ src/maze_generator/maze_generator.cpp | 81 ++++++++++ src/maze_generator/maze_generator.hpp | 28 ++++ src/maze_generator/meson.build | 2 + src/mazetd.cpp | 41 +++++ mazetd.hpp => src/mazetd.hpp | 17 +- src/meson.build | 6 + src/tilemap/meson.build | 2 + {tilemap => src/tilemap}/tilemap.cpp | 0 {tilemap => src/tilemap}/tilemap.hpp | 72 +++++---- 22 files changed, 428 insertions(+), 262 deletions(-) create mode 100644 .ccls-root create mode 100644 .clang-format create mode 100644 .clang-tidy create mode 120000 compile_commands.json delete mode 100644 factory.hpp delete mode 100644 maze_generator/maze_generator.cpp delete mode 100644 maze_generator/maze_generator.hpp delete mode 100644 mazetd.cpp rename {components => src/components}/components.hpp (94%) create mode 100644 src/components/meson.build rename definitions.h => src/definitions.h (99%) create mode 100644 src/factory.hpp create mode 100644 src/maze_generator/maze_generator.cpp create mode 100644 src/maze_generator/maze_generator.hpp create mode 100644 src/maze_generator/meson.build create mode 100644 src/mazetd.cpp rename mazetd.hpp => src/mazetd.hpp (76%) create mode 100644 src/meson.build create mode 100644 src/tilemap/meson.build rename {tilemap => src/tilemap}/tilemap.cpp (100%) rename {tilemap => src/tilemap}/tilemap.hpp (54%) diff --git a/.ccls-root b/.ccls-root new file mode 100644 index 0000000..e69de29 diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b6c4d62 --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +--- +BasedOnStyle: LLVM +IndentWidth: 2 +--- +Language: Cpp +... diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..69e32ba --- /dev/null +++ b/.clang-tidy @@ -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' diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..25eb4b2 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +build/compile_commands.json \ No newline at end of file diff --git a/factory.hpp b/factory.hpp deleted file mode 100644 index b941681..0000000 --- a/factory.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef FACTORY_H -#define FACTORY_H 1 -#include -#include "definitions.h" -#include "components/components.hpp" -#include - -entt::entity create_bg_tile(entt::registry ®, char index, const sf::Vector2i &pos) { - auto e = reg.create(); - - reg.assign(e, pos, index); - reg.assign(e); - - return e; -} - -#endif diff --git a/maze_generator/maze_generator.cpp b/maze_generator/maze_generator.cpp deleted file mode 100644 index d54e263..0000000 --- a/maze_generator/maze_generator.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "maze_generator.hpp" diff --git a/maze_generator/maze_generator.hpp b/maze_generator/maze_generator.hpp deleted file mode 100644 index d592d7e..0000000 --- a/maze_generator/maze_generator.hpp +++ /dev/null @@ -1,149 +0,0 @@ -#ifndef MAZE_GENERATOR_H -#define MAZE_GENERATOR_H 1 -#include -#include -#include -#include -#include -#include "../factory.hpp" -#include - -void dump_maze(std::vector &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 -std::vector -generate_maze(entt::registry ®, - int width, int height, unsigned long seed) { - - // TODO: check width, height - using intpair = std::pair; - std::uniform_int_distribution uniform_dist{0, 4}; // Note: think of a better way - std::default_random_engine engine{seed}; - - std::vector maze(width*height, false); - std::stack 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 in) { - return std::make_pair(wrap_x(in.first), wrap_y(in.second)); - }; - - auto get_cell = - [=](std::pair 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 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 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 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 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 -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 diff --git a/mazetd.cpp b/mazetd.cpp deleted file mode 100644 index 545a002..0000000 --- a/mazetd.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "mazetd.hpp" -#include -#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); - - // 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; -} diff --git a/meson.build b/meson.build index 7ef6dda..6e4b899 100644 --- a/meson.build +++ b/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()]) diff --git a/components/components.hpp b/src/components/components.hpp similarity index 94% rename from components/components.hpp rename to src/components/components.hpp index 5c4ffb4..48c965f 100644 --- a/components/components.hpp +++ b/src/components/components.hpp @@ -1,13 +1,11 @@ #ifndef COMPONENTS_H #define COMPONENTS_H 1 -#include -#include -#include "../tilemap/tilemap.hpp" #include "spdlog/fmt/ostr.h" +#include +#include struct Background {}; - /** Tile component for discrete blocks. **/ diff --git a/src/components/meson.build b/src/components/meson.build new file mode 100644 index 0000000..747953f --- /dev/null +++ b/src/components/meson.build @@ -0,0 +1 @@ +project_header_files += files('components.hpp') diff --git a/definitions.h b/src/definitions.h similarity index 99% rename from definitions.h rename to src/definitions.h index 9e2443c..32fc507 100644 --- a/definitions.h +++ b/src/definitions.h @@ -7,5 +7,4 @@ #include namespace spd = spdlog; - #endif diff --git a/src/factory.hpp b/src/factory.hpp new file mode 100644 index 0000000..f60262a --- /dev/null +++ b/src/factory.hpp @@ -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 +#include + +namespace factory { +entt::entity bg_tile(entt::registry ®, char index, const sf::Vector2i &pos) { + auto e = reg.create(); + + reg.assign(e, pos, index); + reg.assign(e); + + return e; +} + +std::vector background_maze(entt::registry ®, int width, + int height) { + auto maze = maze_generator::generate_maze(width, height); + std::vector 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 diff --git a/src/maze_generator/maze_generator.cpp b/src/maze_generator/maze_generator.cpp new file mode 100644 index 0000000..fc900a8 --- /dev/null +++ b/src/maze_generator/maze_generator.cpp @@ -0,0 +1,81 @@ +#include "maze_generator/maze_generator.hpp" +#include +#include +#include + +using namespace maze_generator; + +std::vector 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; + std::uniform_int_distribution uniform_dist{ + 0, 4}; // Note: think of a better way + std::default_random_engine engine{seed}; + + std::vector maze(width * height, false); + std::stack stack; + + maze.at(0) = true; + stack.push(0); + + auto get_cell = [=](std::pair 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 neighbours{ + {{x, y - 2}, {x, y + 2}, {x - 2, y}, {x + 2, y}}}; + + std::array walls // FIXME: i know... kinda to much + {{{x, y - 1}, {x, y + 1}, {x - 1, y}, {x + 1, y}}}; + + std::vector 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; +} diff --git a/src/maze_generator/maze_generator.hpp b/src/maze_generator/maze_generator.hpp new file mode 100644 index 0000000..67f376f --- /dev/null +++ b/src/maze_generator/maze_generator.hpp @@ -0,0 +1,28 @@ +#ifndef MAZE_GENERATOR_H +#define MAZE_GENERATOR_H 1 +#include +#include +#include + +namespace maze_generator { +void dump_maze(const std::vector &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 generate_maze(const int width, const int height); + std::vector generate_maze(const int width, const int height, const unsigned long seed); + + std::vector generate_maze(const int width, const int height) { + std::random_device rd; + return generate_maze(width, height, rd()); + } + +} // namespace maze_generator + +#endif diff --git a/src/maze_generator/meson.build b/src/maze_generator/meson.build new file mode 100644 index 0000000..a143f84 --- /dev/null +++ b/src/maze_generator/meson.build @@ -0,0 +1,2 @@ +project_header_files += files('maze_generator.hpp') +project_sources += files('maze_generator.cpp') diff --git a/src/mazetd.cpp b/src/mazetd.cpp new file mode 100644 index 0000000..71806b8 --- /dev/null +++ b/src/mazetd.cpp @@ -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); + + // 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; +} diff --git a/mazetd.hpp b/src/mazetd.hpp similarity index 76% rename from mazetd.hpp rename to src/mazetd.hpp index 3a7ddf3..cc74318 100644 --- a/mazetd.hpp +++ b/src/mazetd.hpp @@ -2,18 +2,20 @@ #define MAZETD_H 1 #include "definitions.h" -#include -#include -#include -#include "tilemap/tilemap.hpp" #include "factory.hpp" +#include "tilemap/tilemap.hpp" +//#include "maze_generator/maze_generator.hpp" +#include +#include +#include +#include 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 m_background_system; }; - #endif // MAZETD_H diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 0000000..a334e66 --- /dev/null +++ b/src/meson.build @@ -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') diff --git a/src/tilemap/meson.build b/src/tilemap/meson.build new file mode 100644 index 0000000..94fc606 --- /dev/null +++ b/src/tilemap/meson.build @@ -0,0 +1,2 @@ +project_sources += files('tilemap.cpp') +project_header_files += files('tilemap.hpp') diff --git a/tilemap/tilemap.cpp b/src/tilemap/tilemap.cpp similarity index 100% rename from tilemap/tilemap.cpp rename to src/tilemap/tilemap.cpp diff --git a/tilemap/tilemap.hpp b/src/tilemap/tilemap.hpp similarity index 54% rename from tilemap/tilemap.hpp rename to src/tilemap/tilemap.hpp index 7f6597c..16f3e16 100644 --- a/tilemap/tilemap.hpp +++ b/src/tilemap/tilemap.hpp @@ -1,49 +1,49 @@ #ifndef TILEMAP_H #define TILEMAP_H 1 -#include -#include -#include #include #include +#include +#include +#include #include -#include #include +#include -#include "../components/components.hpp" -#include "../definitions.h" +#include "components/components.hpp" +#include "definitions.h" template 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().where()} - { + 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().where()} { 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() - .template connect<&TileMapSystem::on_tile_create>(*this); + .template connect<&TileMapSystem::on_tile_create>(*this); } ~TileMapSystem() { m_reg.on_construct() - .template disconnect<&TileMapSystem::on_tile_create>(*this); - + .template disconnect<&TileMapSystem::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(e); - update(tile); + const auto tile = reg.get(e); + update(tile); } template - friend OStream &operator<<(OStream &os, const TileMapSystem &c) { - return os << typeid(TileMapSystem).name() << " Width=" << c.m_width - << " Height=" << c.m_height ; + friend OStream &operator<<(OStream &os, + const TileMapSystem &c) { + return os << typeid(TileMapSystem).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