This cleans up how I'm rendering the map but there's no way I can render a large map like this. It'd be way too big.
This commit is contained in:
parent
3b06105813
commit
d9219a8c64
3 changed files with 74 additions and 42 deletions
|
@ -80,71 +80,71 @@ TEST_CASE("dijkstra algo test", "[map]") {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Sprite render_sprite(std::unordered_map<wchar_t, sf::Vector2i>& sprite_coord, sf::Vector2i size, wchar_t display, sf::Texture& map_sprites) {
|
sf::Sprite render_map(GameLevel& level, sf::RenderTexture& render) {
|
||||||
auto coords = sprite_coord.at(display);
|
|
||||||
sf::IntRect square{coords, {size.x, size.y}};
|
|
||||||
sf::Sprite sprite{map_sprites, square};
|
|
||||||
return sprite;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("map image test", "[map-sprite]") {
|
|
||||||
components::init();
|
|
||||||
textures::init();
|
|
||||||
LevelManager levels;
|
|
||||||
GameLevel level = levels.current();
|
|
||||||
auto &walls = level.map->tiles();
|
auto &walls = level.map->tiles();
|
||||||
|
|
||||||
auto &tile_set = textures::get_map_tile_set();
|
auto &tile_set = textures::get_map_tile_set();
|
||||||
|
|
||||||
sf::Vector2i size{MAP_TILE_DIM,MAP_TILE_DIM};
|
sf::Vector2i size{MAP_TILE_DIM,MAP_TILE_DIM};
|
||||||
matrix::dump("TILES?", walls);
|
|
||||||
|
|
||||||
std::unordered_map<wchar_t, sf::Vector2i> sprite_coord;
|
|
||||||
|
|
||||||
Config config("./assets/map_tiles.json");
|
|
||||||
json& tiles = config.json();
|
|
||||||
|
|
||||||
for(auto tile : tiles) {
|
|
||||||
sf::Vector2i coords{tile["x"], tile["y"]};
|
|
||||||
REQUIRE(coords.x % size.x == 0);
|
|
||||||
REQUIRE(coords.y % size.y == 0);
|
|
||||||
sprite_coord.insert_or_assign(tile["display"], coords);
|
|
||||||
}
|
|
||||||
|
|
||||||
sf::Vector2u dim{
|
sf::Vector2u dim{
|
||||||
(unsigned int)matrix::width(walls) * size.x,
|
(unsigned int)matrix::width(walls) * size.x,
|
||||||
(unsigned int)matrix::height(walls) * size.y};
|
(unsigned int)matrix::height(walls) * size.y};
|
||||||
|
|
||||||
sf::RenderTexture render{dim};
|
bool worked = render.resize(dim);
|
||||||
render.clear({0,0,0,0});
|
dbc::check(worked, "Failed to resize map render target.");
|
||||||
|
|
||||||
sf::Texture map_sprites{"./assets/map_tiles.png"};
|
render.clear({0,0,0,0});
|
||||||
sf::Texture paper{"./assets/ui/full_screen_paper.png"};
|
|
||||||
sf::Sprite paper_sprite{paper};
|
|
||||||
paper_sprite.scale({1.5f, 1.5f});
|
|
||||||
paper_sprite.setPosition({-30.0f, -30.0f});
|
|
||||||
render.draw(paper_sprite);
|
|
||||||
|
|
||||||
for(matrix::each_row it{walls}; it.next();) {
|
for(matrix::each_row it{walls}; it.next();) {
|
||||||
size_t tid = walls[it.y][it.x];
|
size_t tid = walls[it.y][it.x];
|
||||||
wchar_t display = tile_set[tid];
|
wchar_t display = tile_set[tid];
|
||||||
REQUIRE(sprite_coord.contains(display));
|
|
||||||
|
|
||||||
auto sprite = render_sprite(sprite_coord, size, display, map_sprites);
|
auto& sprite = textures::get_map_sprite(display);
|
||||||
sprite.setPosition({float(it.x * size.x), float(it.y * size.y)});
|
sprite.setPosition({float(it.x * size.x), float(it.y * size.y)});
|
||||||
render.draw(sprite);
|
render.draw(sprite);
|
||||||
}
|
}
|
||||||
|
|
||||||
level.world->query<components::Position, components::Tile>([&](auto, auto &pos, auto &entity_glyph) {
|
render.display();
|
||||||
REQUIRE(sprite_coord.contains(entity_glyph.display));
|
return sf::Sprite{render.getTexture()};
|
||||||
|
}
|
||||||
|
|
||||||
auto sprite = render_sprite(sprite_coord, size, entity_glyph.display, map_sprites);
|
sf::Sprite render_entities(GameLevel& level, sf::Sprite& map_sprite, sf::RenderTexture& render) {
|
||||||
sprite.setPosition({float(pos.location.x * size.x), float(pos.location.y * size.y)});
|
render.clear({0,0,0,0});
|
||||||
|
render.draw(map_sprite);
|
||||||
|
|
||||||
|
level.world->query<components::Position, components::Tile>([&](auto, auto &pos, auto &entity_glyph) {
|
||||||
|
auto sprite = textures::get_map_sprite(entity_glyph.display);
|
||||||
|
sprite.setPosition({float(pos.location.x * MAP_TILE_DIM), float(pos.location.y * MAP_TILE_DIM)});
|
||||||
render.draw(sprite);
|
render.draw(sprite);
|
||||||
});
|
});
|
||||||
|
|
||||||
render.display();
|
render.display();
|
||||||
sf::Image out_img = render.getTexture().copyToImage();
|
return sf::Sprite{render.getTexture()};
|
||||||
bool worked = out_img.saveToFile("map_test.png");
|
}
|
||||||
|
|
||||||
|
TEST_CASE("map image test", "[map-sprite]") {
|
||||||
|
components::init();
|
||||||
|
textures::init();
|
||||||
|
|
||||||
|
LevelManager levels;
|
||||||
|
GameLevel level = levels.current();
|
||||||
|
// on level start make one render texture with the base map
|
||||||
|
auto render = std::make_shared<sf::RenderTexture>();
|
||||||
|
auto map_sprite = render_map(level, *render);
|
||||||
|
|
||||||
|
// every turn render the map to the entities texture, then render
|
||||||
|
// entities on this image, that way I just have to render them
|
||||||
|
auto entities = std::make_shared<sf::RenderTexture>(render->getSize());
|
||||||
|
render_entities(level, map_sprite, *entities);
|
||||||
|
|
||||||
|
// confirm we get two different maps
|
||||||
|
auto out_img = render->getTexture().copyToImage();
|
||||||
|
bool worked = out_img.saveToFile("map_render.png");
|
||||||
|
REQUIRE(worked);
|
||||||
|
|
||||||
|
// this has entities on it
|
||||||
|
out_img = entities->getTexture().copyToImage();
|
||||||
|
worked = out_img.saveToFile("map_entities.png");
|
||||||
REQUIRE(worked);
|
REQUIRE(worked);
|
||||||
}
|
}
|
||||||
|
|
28
textures.cpp
28
textures.cpp
|
@ -78,10 +78,31 @@ namespace textures {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void load_map_tiles() {
|
||||||
|
Config config("./assets/map_tiles.json");
|
||||||
|
nlohmann::json& tiles = config.json();
|
||||||
|
|
||||||
|
for(auto tile : tiles) {
|
||||||
|
sf::Vector2i coords{tile["x"], tile["y"]};
|
||||||
|
dbc::check(coords.x % ICONGEN_MAP_TILE_DIM == 0, "x coordinates wrong in map");
|
||||||
|
dbc::check(coords.y % ICONGEN_MAP_TILE_DIM == 0, "y coordinates wrong in map");
|
||||||
|
|
||||||
|
sf::IntRect square{coords, {ICONGEN_MAP_TILE_DIM, ICONGEN_MAP_TILE_DIM}};
|
||||||
|
sf::Sprite sprite{TMGR.map_sprite_sheet, square};
|
||||||
|
wchar_t display = tile["display"];
|
||||||
|
|
||||||
|
dbc::check(!TMGR.map_sprites.contains(display),
|
||||||
|
fmt::format("duplicate tile display {} in map_tiles.json", int(display)));
|
||||||
|
|
||||||
|
TMGR.map_sprites.try_emplace(display, sprite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
if(!initialized) {
|
if(!initialized) {
|
||||||
load_tiles();
|
load_tiles();
|
||||||
load_sprites();
|
load_sprites();
|
||||||
|
load_map_tiles();
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,4 +155,11 @@ namespace textures {
|
||||||
fmt::format("there is no texture named {} in tiles.json", name));
|
fmt::format("there is no texture named {} in tiles.json", name));
|
||||||
return TMGR.name_to_id.at(name);
|
return TMGR.name_to_id.at(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sf::Sprite& get_map_sprite(wchar_t display) {
|
||||||
|
dbc::check(TMGR.map_sprites.contains(display),
|
||||||
|
fmt::format("map_sprites.json doesn't have {} sprite", int(display)));
|
||||||
|
|
||||||
|
return TMGR.map_sprites.at(display);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -22,6 +22,8 @@ namespace textures {
|
||||||
std::vector<int> ambient_light;
|
std::vector<int> ambient_light;
|
||||||
std::unordered_map<std::string, SpriteTexture> sprite_textures;
|
std::unordered_map<std::string, SpriteTexture> sprite_textures;
|
||||||
std::unordered_map<std::string, size_t> name_to_id;
|
std::unordered_map<std::string, size_t> name_to_id;
|
||||||
|
std::unordered_map<wchar_t, sf::Sprite> map_sprites;
|
||||||
|
sf::Texture map_sprite_sheet{"./assets/map_tiles.png"};
|
||||||
};
|
};
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
@ -40,5 +42,7 @@ namespace textures {
|
||||||
|
|
||||||
const uint32_t* get_ceiling(size_t num);
|
const uint32_t* get_ceiling(size_t num);
|
||||||
|
|
||||||
|
sf::Sprite& get_map_sprite(wchar_t display);
|
||||||
|
|
||||||
size_t get_id(const std::string& name);
|
size_t get_id(const std::string& name);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue