Refactored out the tilemap since it was mostly doing nothing useful.

master
Zed A. Shaw 6 days ago
parent ea9f6bf383
commit 3a745d492a
  1. 14
      assets/tiles.json
  2. 1
      gui/map_view.hpp
  3. 1
      gui/mini_map.hpp
  4. 10
      map.cpp
  5. 7
      map.hpp
  6. 2
      meson.build
  7. 5
      raycaster.cpp
  8. 10
      systems.cpp
  9. 6
      tests/textures.cpp
  10. 22
      tests/tilemap.cpp
  11. 25
      textures.cpp
  12. 2
      textures.hpp
  13. 78
      tilemap.cpp
  14. 38
      tilemap.hpp
  15. 2
      worldbuilder.cpp

@ -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",

@ -2,7 +2,6 @@
#include "levelmanager.hpp"
#include "textures.hpp"
#include <guecs/ui.hpp>
#include "tilemap.hpp"
#include <string>
namespace gui {

@ -2,7 +2,6 @@
#include "levelmanager.hpp"
#include "textures.hpp"
#include <guecs/ui.hpp>
#include "tilemap.hpp"
namespace gui {
class MiniMapUI {

@ -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() {

@ -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<Room> $rooms;
std::vector<Point> $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();
};

@ -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,

@ -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<components::Sprite>([&](const auto ent, auto& sprite) {
// player doesn't need a sprite

@ -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<Position>(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<wchar_t>(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';
}

@ -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));
}

@ -1,22 +0,0 @@
#include <catch2/catch_test_macros.hpp>
#include <fmt/core.h>
#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());
}

@ -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();
}

@ -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();

@ -1,78 +0,0 @@
#include "tilemap.hpp"
#include "dbc.hpp"
#include "constants.hpp"
#include <iostream>
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>(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<std::string> TileMap::tile_names(bool collision) {
const auto &json = $config.json();
std::vector<std::string> 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;
}

@ -1,38 +0,0 @@
#pragma once
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <fmt/core.h>
#include "point.hpp"
#include "matrix.hpp"
#include "config.hpp"
#include "components.hpp"
typedef std::vector<components::Tile> TileRow;
typedef std::vector<TileRow> 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<std::string> 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();
};

@ -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) {

Loading…
Cancel
Save