diff --git a/assets/tiles.json b/assets/tiles.json index 3b6c226..8ca54de 100644 --- a/assets/tiles.json +++ b/assets/tiles.json @@ -7,21 +7,21 @@ "display": 10398, "id": 0 }, - "WALL_MOSS": { - "texture": "assets/glowing_moss_wall-256.png", + "WALL_PLAIN": { + "texture": "assets/wall_texture_test-256.png", "foreground": [230, 20, 30], "background": [230, 20, 120], "collision": true, - "display": 8820, + "display": 9608, "id": 1 }, - "WALL_PLAIN": { - "texture": "assets/wall_texture_test-256.png", + "WALL_MOSS": { + "texture": "assets/glowing_moss_wall-256.png", "foreground": [230, 20, 30], "background": [230, 20, 120], "collision": true, - "display": 9608, - "id": 2 + "display": 8820, + "id": 3 }, "WALL_VINES": { "texture": "assets/wall_with_vines-256.png", diff --git a/gui/map_view.hpp b/gui/map_view.hpp index 9a34819..ed00a22 100644 --- a/gui/map_view.hpp +++ b/gui/map_view.hpp @@ -2,7 +2,6 @@ #include "levelmanager.hpp" #include "textures.hpp" #include -#include "tilemap.hpp" #include namespace gui { diff --git a/gui/mini_map.hpp b/gui/mini_map.hpp index bf159d5..ed2727e 100644 --- a/gui/mini_map.hpp +++ b/gui/mini_map.hpp @@ -2,7 +2,6 @@ #include "levelmanager.hpp" #include "textures.hpp" #include -#include "tilemap.hpp" namespace gui { class MiniMapUI { diff --git a/map.cpp b/map.cpp index e2dbfc5..52e3e9a 100644 --- a/map.cpp +++ b/map.cpp @@ -13,13 +13,11 @@ using namespace fmt; Map::Map(size_t width, size_t height) : $width(width), $height(height), - $tiles(width, height), $walls(height, matrix::Row(width, SPACE_VALUE)), $paths(width, height) {} Map::Map(Matrix &walls, Pathing &paths) : - $tiles(matrix::width(walls), matrix::height(walls)), $walls(walls), $paths(paths) { @@ -144,8 +142,12 @@ bool Map::INVARIANT() { return true; } -void Map::load_tiles() { - $tiles.load($walls); +void Map::init_tiles() { + $tiles = $walls; +} + +Matrix& Map::tiles() { + return $walls; } void Map::enclose() { diff --git a/map.hpp b/map.hpp index 80f2f2d..fd00998 100644 --- a/map.hpp +++ b/map.hpp @@ -10,7 +10,6 @@ #include "pathing.hpp" #include "matrix.hpp" #include "constants.hpp" -#include "tilemap.hpp" using lighting::LightSource; @@ -25,8 +24,8 @@ class Map { public: size_t $width; size_t $height; - TileMap $tiles; Matrix $walls; + Matrix $tiles; Pathing $paths; std::vector $rooms; std::vector $dead_ends; @@ -36,7 +35,6 @@ public: Map(Matrix &walls, Pathing &paths); Matrix& paths() { return $paths.paths(); } - TileMap& tiles() { return $tiles; } Matrix& input_map() { return $paths.input(); } Matrix& walls() { return $walls; } size_t width() { return $width; } @@ -65,7 +63,8 @@ public: void dump(int show_x=-1, int show_y=-1); bool INVARIANT(); - void load_tiles(); + void init_tiles(); + Matrix& tiles(); void add_room(Room &room); void invert_space(); }; diff --git a/meson.build b/meson.build index e8fd589..772a1fe 100644 --- a/meson.build +++ b/meson.build @@ -122,7 +122,6 @@ sources = [ 'stats.cpp', 'systems.cpp', 'textures.cpp', - 'tilemap.cpp', 'worldbuilder.cpp', 'maze.cpp' ] @@ -150,7 +149,6 @@ executable('runtests', sources + [ 'tests/spatialmap.cpp', 'tests/stats.cpp', 'tests/textures.cpp', - 'tests/tilemap.cpp', 'tests/mazes.cpp', ], cpp_args: cpp_args, diff --git a/raycaster.cpp b/raycaster.cpp index 055f4f6..4fe1898 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -417,9 +417,8 @@ void Raycaster::update_level(GameLevel level) { $sprites.clear(); $level = level; - // BUG: this is way too complex, please make it easier, the issue is that I need to convert the maps to visible tiles and that involves wstring convert, but this is many steps done probably over and over - auto& tiles = $level.map->tiles(); - $map = textures::convert_char_to_texture(tiles.$tile_ids); + + $map = $level.map->tiles(); $level.world->query([&](const auto ent, auto& sprite) { // player doesn't need a sprite diff --git a/systems.cpp b/systems.cpp index 08e9575..12f527f 100644 --- a/systems.cpp +++ b/systems.cpp @@ -390,6 +390,7 @@ std::wstring System::draw_map(GameLevel level, size_t view_x, size_t view_y, int auto player_pos = world.get(level.player).location; Point cam_orig = map.center_camera(player_pos, view_x, view_y); auto &tiles = map.tiles(); + (void)tiles; // make a grid of chars to work with auto grid = shiterator::make(view_x+1, view_y+1); @@ -398,12 +399,11 @@ std::wstring System::draw_map(GameLevel level, size_t view_x, size_t view_y, int for(shiterator::each_cell_t it{grid}; it.next();) { size_t tile_y = size_t(it.y) + cam_orig.y; size_t tile_x = size_t(it.x) + cam_orig.x; + (void)tile_y; + (void)tile_x; - if(tile_x < tiles.$width && tile_y < tiles.$height) { - grid[it.y][it.x] = tiles.at(tile_x, tile_y).display; - } else { - grid[it.y][it.x] = ' '; - } + // FIX ME + grid[it.y][it.x] = 'F'; } diff --git a/tests/textures.cpp b/tests/textures.cpp index e705c3c..55c03c8 100644 --- a/tests/textures.cpp +++ b/tests/textures.cpp @@ -26,7 +26,7 @@ TEST_CASE("test texture management", "[textures]") { LevelManager levels; GameLevel level = levels.current(); auto& tiles = level.map->tiles(); - auto map = textures::convert_char_to_texture(tiles.$tile_ids); - REQUIRE(matrix::width(map) == matrix::width(tiles.$tile_ids)); - REQUIRE(matrix::height(map) == matrix::height(tiles.$tile_ids)); + auto& walls = level.map->walls(); + REQUIRE(matrix::width(tiles) == matrix::width(walls)); + REQUIRE(matrix::height(tiles) == matrix::height(walls)); } diff --git a/tests/tilemap.cpp b/tests/tilemap.cpp deleted file mode 100644 index 4a82518..0000000 --- a/tests/tilemap.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include "map.hpp" -#include "levelmanager.hpp" -#include "tilemap.hpp" -#include "config.hpp" -#include "rand.hpp" - -using namespace fmt; -using std::string; - -TEST_CASE("tilemap can load tiles and make a map", "[tilemap]") { - LevelManager levels; - GameLevel level = levels.current(); - auto &map = *level.map; - - TileMap tiles(map.width(), map.height()); - auto& walls = map.walls(); - tiles.load(walls); - tiles.dump(); - REQUIRE(tiles.INVARIANT()); -} diff --git a/textures.cpp b/textures.cpp index c2fa8e1..ff12199 100644 --- a/textures.cpp +++ b/textures.cpp @@ -35,12 +35,21 @@ namespace textures { void load_tiles() { Config assets("assets/tiles.json"); auto &tiles = assets.json(); + TMGR.surfaces.resize(tiles.size()); for(auto &el : tiles.items()) { auto &config = el.value(); - TMGR.surfaces.emplace_back(load_image(config["texture"])); + const std::string& texture_fname = config["texture"]; + size_t surface_i = config["id"]; + + if(surface_i >= tiles.size()) { + TMGR.surfaces.resize(surface_i + 1); + } + + TMGR.surfaces[surface_i] = load_image(texture_fname); + wchar_t tid = config["display"]; - int surface_i = TMGR.surfaces.size() - 1; + fmt::println("texture {} has surface_i={}", texture_fname, surface_i); TMGR.char_to_texture[tid] = surface_i; } } @@ -72,7 +81,6 @@ namespace textures { sf::Image texture; bool good = texture.loadFromFile(filename); dbc::check(good, fmt::format("failed to load {}", filename)); - fmt::println("texture size={}", sizeof(texture)); return texture; } @@ -80,17 +88,6 @@ namespace textures { return (const uint32_t *)TMGR.surfaces[num].getPixelsPtr(); } - matrix::Matrix convert_char_to_texture(matrix::Matrix &tile_ids) { - auto result = matrix::make(matrix::width(tile_ids), matrix::height(tile_ids)); - - for(matrix::each_cell it(tile_ids); it.next();) { - wchar_t tid = tile_ids[it.y][it.x]; - result[it.y][it.x] = TMGR.char_to_texture.at(tid); - } - - return result; - } - const uint32_t* get_floor() { return (const uint32_t *)TMGR.floor.getPixelsPtr(); } diff --git a/textures.hpp b/textures.hpp index 050cd17..efca76e 100644 --- a/textures.hpp +++ b/textures.hpp @@ -30,8 +30,6 @@ namespace textures { const uint32_t* get_surface(size_t num); - matrix::Matrix convert_char_to_texture(matrix::Matrix &from); - const uint32_t* get_floor(); const uint32_t* get_ceiling(); diff --git a/tilemap.cpp b/tilemap.cpp deleted file mode 100644 index 80b0879..0000000 --- a/tilemap.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include "tilemap.hpp" -#include "dbc.hpp" -#include "constants.hpp" -#include - -using nlohmann::json; -using components::Tile; -using std::string; - -TileMap::TileMap(size_t width, size_t height) : - $config("./assets/tiles.json"), - $width(width), - $height(height), - $tile_ids(height, matrix::Row(width, SPACE_VALUE)), - $display(height, TileRow(width, {L'#', {0,0,0}, {0,0,0}})) -{ -} - -std::string TileMap::to_string(int show_x, int show_y) { - std::string result; - - for(matrix::each_row it{$tile_ids}; it.next();) { - const Tile &cell = $display[it.y][it.x]; - - if(int(it.x) == show_x && int(it.y) == show_y) { - result += "@"; - } else { - result += cell.display; - } - - if(it.row) result += "\n"; - } - - return result; -} - -void TileMap::dump(int show_x, int show_y) { - std::cout << to_string(show_x, show_y) << std::endl; -} - -void TileMap::set_tile(size_t x, size_t y, const string& tile_name) { - json& tile_conf = $config[tile_name]; - - auto tile = components::convert(tile_conf); - $tile_ids[y][x] = tile.display; - $display[y][x] = tile; -} - -void TileMap::load(matrix::Matrix &walls) { - for(matrix::each_cell it{walls}; it.next();) { - string tile_name = walls[it.y][it.x] == SPACE_VALUE ? "FLOOR_TILE" : "WALL_PLAIN"; - set_tile(it.x, it.y, tile_name); - } -} - -const Tile &TileMap::at(size_t x, size_t y) { - return $display[y][x]; -} - -std::vector TileMap::tile_names(bool collision) { - const auto &json = $config.json(); - std::vector keys; - - for(const auto& el : json.items()) { - const auto &val = el.value(); - if(val["collision"] == collision) { - keys.push_back(el.key()); - } - } - - return keys; -} - -bool TileMap::INVARIANT() { - dbc::check(matrix::height($tile_ids) == $height, "$tile_ids has wrong height"); - dbc::check(matrix::width($tile_ids) == $width, "$tile_ids has wrong width"); - return true; -} diff --git a/tilemap.hpp b/tilemap.hpp deleted file mode 100644 index b450da4..0000000 --- a/tilemap.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include "point.hpp" -#include "matrix.hpp" -#include "config.hpp" -#include "components.hpp" - -typedef std::vector TileRow; -typedef std::vector TileGrid; - -class TileMap { -public: - Config $config; - size_t $width; - size_t $height; - matrix::Matrix $tile_ids; - TileGrid $display; - - TileMap(size_t width, size_t height); - - // disable copying - TileMap(TileMap &map) = delete; - - size_t width() { return $width; } - size_t height() { return $height; } - void load(matrix::Matrix &walls); - const components::Tile& at(size_t x, size_t y); - void set_tile(size_t x, size_t y, const std::string& tile_name); - std::vector tile_names(bool collision); - - std::string to_string(int show_x, int show_y); - void dump(int show_x=-1, int show_y=-1); - bool INVARIANT(); -}; diff --git a/worldbuilder.cpp b/worldbuilder.cpp index fbc0845..dab3d29 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -36,7 +36,7 @@ void WorldBuilder::generate_map() { maze.hunt_and_kill(); $map.enclose(); - $map.load_tiles(); + $map.init_tiles(); } bool WorldBuilder::find_open_spot(Point& pos_out) {