From a67d25ee106835017ce20b8c80a162df1e999724 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 31 Jan 2025 13:37:01 -0500 Subject: [PATCH] Tiles now record their textures and this is loaded from the map then converted to an indexed integer on the fly. --- animator.cpp | 4 ++-- animator.hpp | 2 +- assets/config.json | 9 -------- assets/tiles.json | 8 +++---- main.cpp | 55 ++++++++++++++++++---------------------------- raycaster.cpp | 7 ++---- raycaster.hpp | 4 ++-- texture.cpp | 33 ++++++++++++++++++++++------ texture.hpp | 7 +++++- worldbuilder.cpp | 1 - 10 files changed, 64 insertions(+), 66 deletions(-) diff --git a/animator.cpp b/animator.cpp index 6c46503..4b3d40f 100644 --- a/animator.cpp +++ b/animator.cpp @@ -15,7 +15,7 @@ void Animator::step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int {rect_w, rect_h}})); } -void Animator::play() { +void Animator::play(bool sound_too) { playing = true; - sound.play(); + if(sound_too) sound.play(); } diff --git a/animator.hpp b/animator.hpp index 2c58430..abc1e4a 100644 --- a/animator.hpp +++ b/animator.hpp @@ -22,5 +22,5 @@ struct Animator { void step(sf::Sprite& sprite, int rect_x, int rect_y, int rect_w, int rect_h); - void play(); + void play(bool sound_too=true); }; diff --git a/assets/config.json b/assets/config.json index dc3cb6f..6615929 100644 --- a/assets/config.json +++ b/assets/config.json @@ -1,13 +1,4 @@ { - "textures": [ - "assets/wall_simple-256.png", - "assets/wall_with_vines-256.png", - "assets/wall_with_pillars-256.png", - "assets/wall_texture_test-256.png", - "assets/floor_tile_test-256.png", - "assets/ceiling_test-256.png", - "assets/wood_wall-256.png" - ], "sprites": { "armored_knight": "assets/armored_knight_1-256.png", "sword": "assets/cinqueda_1-512.png", diff --git a/assets/tiles.json b/assets/tiles.json index a97ea62..ae3ea79 100644 --- a/assets/tiles.json +++ b/assets/tiles.json @@ -1,27 +1,27 @@ { "FLOOR_TILE": { - "id": 0, + "texture": "assets/floor_tile_test-256.png", "foreground": [40, 15, 125], "background": [200, 15, 75], "collision": false, "display":"\u289e" }, "WALL_PLAIN": { - "id": 1, + "texture": "assets/wall_simple-256.png", "foreground": [230, 20, 30], "background": [230, 20, 120], "collision": true, "display": "\ua5b8" }, "WALL_VINES": { - "id": 2, + "texture": "assets/wall_with_vines-256.png", "foreground": [40, 15, 125], "background": [200, 29, 75], "collision": false, "display":"\u19f0" }, "WALL_PILLAR": { - "id": 3, + "texture": "assets/wall_with_pillars-256.png", "foreground": [40, 15, 125], "background": [200, 29, 75], "collision": false, diff --git a/main.cpp b/main.cpp index 417bae5..88f5503 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,10 @@ #include #include "constants.hpp" #include "stats.hpp" -#include "worldbuilder.hpp" +#include "levelmanager.hpp" +#include "components.hpp" + +using namespace components; void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) { sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT}); @@ -20,36 +23,13 @@ void draw_gui(sf::RenderWindow &window, sf::Text &text, Stats &stats) { window.draw(text); } -Matrix generate_map(Map& map, Point &player) { - // generate the world and make the map - WorldBuilder builder(map); - builder.generate_map(); - - bool can_place = map.place_entity(1, player); - dbc::check(can_place, "couldn't place the player"); - - auto &tiles = map.tiles(); - tiles.dump(player.x, player.y); - - auto bad_map = matrix::make(tiles.width(), tiles.height()); - - for(matrix::each_cell it(tiles.$tile_ids); it.next();) { - switch(tiles.$tile_ids[it.y][it.x]) { - case 0x289e: - bad_map[it.y][it.x] = 0; break; - case 0xa5b8: - bad_map[it.y][it.x] = 1; break; - case 0x19f0: - bad_map[it.y][it.x] = 2; break; - case 0x16de: - bad_map[it.y][it.x] = 3; break; - default: - bad_map[it.y][it.x] = 0; - } - } +Matrix generate_map(TexturePack &textures, GameLevel &level, Point &player_out) { + auto &tiles = level.map->tiles(); + auto &player = level.world->get_the(); + auto &player_position = level.world->get(player.entity); + player_out = player_position.location; - matrix::dump("CONVERTED MAP", bad_map); - return bad_map; + return textures.convert_char_to_texture(tiles.$tile_ids); } void draw_weapon(sf::RenderWindow &window, sf::Sprite &weapon, float rotation) { @@ -66,11 +46,18 @@ int main() { text.setFillColor({255,255,255}); text.setPosition({10,10}); - Map map(30, 30); Point player{0, 0}; - auto MAP = generate_map(map, player); + LevelManager levels; + GameLevel &cur_level = levels.current(); + + TexturePack textures; + textures.load_tiles(); + textures.load_sprites(); + textures.position_sprite(4.0, 3.55, "evil_eye"); + + auto map = generate_map(textures, cur_level, player); - Raycaster rayview(window, MAP, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT); + Raycaster rayview(window, textures, map, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT); rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y); rayview.position_camera(player.x, player.y); rayview.init_shaders(); @@ -126,7 +113,7 @@ int main() { } if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { - rayview.$anim.play(); + rayview.$anim.play(false); rotation = -30.0f; } else { rotation = -10.0f; diff --git a/raycaster.cpp b/raycaster.cpp index 37b7ea9..13a20b9 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -27,8 +27,8 @@ inline uint32_t dumb_lighting(uint32_t pixel, double distance) { return conv.as_int; } - -Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) : +Raycaster::Raycaster(sf::RenderWindow& window, TexturePack &textures, Matrix &map, int width, int height) : + $textures(textures), $view_texture({(unsigned int)width, (unsigned int)height}), $view_sprite($view_texture), $width(width), $height(height), @@ -41,9 +41,6 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh { $view_sprite.setPosition({0, 0}); $pixels = make_unique($width * $height); - $textures.load_textures(); - $textures.load_sprites(); - $textures.position_sprite(4.0, 3.55, "evil_eye"); $view_texture.setSmooth(false); } diff --git a/raycaster.hpp b/raycaster.hpp index 24cb043..bde4048 100644 --- a/raycaster.hpp +++ b/raycaster.hpp @@ -21,7 +21,7 @@ struct Raycaster { int $pitch=0; sf::Clock $clock; - TexturePack $textures; + TexturePack &$textures; double $posX = 0; double $posY = 0; @@ -48,7 +48,7 @@ struct Raycaster { sf::Shader $paused; sf::Shader* $active_shader = nullptr; - Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height); + Raycaster(sf::RenderWindow& window, TexturePack &textures, Matrix &map, int width, int height); void draw_pixel_buffer(); void clear(); diff --git a/texture.cpp b/texture.cpp index cb44063..d01a9a0 100644 --- a/texture.cpp +++ b/texture.cpp @@ -29,21 +29,29 @@ void TexturePack::load_sprites() { } sword = sprite_textures["sword"]; + + floor = load_image(assets["sprites"]["floor"]); + ceiling = load_image(assets["sprites"]["ceiling"]); } void TexturePack::position_sprite(double x, double y, string name) { sprites.emplace_back(x, y, sprite_textures[name]); } -void TexturePack::load_textures() { - Config assets("assets/config.json"); +void TexturePack::load_tiles() { + Config assets("assets/tiles.json"); + auto &tiles = assets.json(); - for(string tile_path : assets["textures"]) { - surfaces.emplace_back(load_image(tile_path)); - } + for(auto &el : tiles.items()) { + auto &config = el.value(); + surfaces.emplace_back(load_image(config["texture"])); - floor = load_image(assets["sprites"]["floor"]); - ceiling = load_image(assets["sprites"]["ceiling"]); + std::wstring display = assets.wstring(el.key(), "display"); + int surface_i = surfaces.size() - 1; + wchar_t tid = display[0]; + + char_to_texture[tid] = surface_i; + } } const uint32_t* TexturePack::get_surface(size_t num) { @@ -53,3 +61,14 @@ const uint32_t* TexturePack::get_surface(size_t num) { Sprite &TexturePack::get_sprite(size_t sprite_num) { return sprites[sprite_num]; } + +matrix::Matrix TexturePack::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] = char_to_texture.at(tid); + } + + return result; +} diff --git a/texture.hpp b/texture.hpp index 6a968a7..beed4cd 100644 --- a/texture.hpp +++ b/texture.hpp @@ -6,6 +6,7 @@ #include #include #include +#include "matrix.hpp" struct SpriteTexture { std::shared_ptr sprite = nullptr; @@ -22,15 +23,19 @@ struct TexturePack { std::vector surfaces; std::vector sprites; std::unordered_map sprite_textures; + std::unordered_map char_to_texture; sf::Image floor; sf::Image ceiling; SpriteTexture sword; - void load_textures(); + void load_tiles(); void load_sprites(); sf::Image load_image(std::string filename); Sprite& get_sprite(size_t sprite_num); const uint32_t* get_surface(size_t num); // this needs to go into a map place void position_sprite(double x, double y, std::string name); + + // ZED: this is ugly so maybe you should like rewrite it or something + matrix::Matrix convert_char_to_texture(matrix::Matrix &from); }; diff --git a/worldbuilder.cpp b/worldbuilder.cpp index a4af291..a7a23b0 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -110,7 +110,6 @@ void WorldBuilder::stylize_room(int room, string tile_name, float size) { bool placed = $map.place_entity(room, pos_out); dbc::check(placed, "failed to place style in room"); - dbc::log("FIX THE SYTLE ROOM TO VARY FLOOR OR WALL"); tile_name = tile_name == "FLOOR_TILE" ? "WALL_PLAIN" : tile_name; for(matrix::circle it{$map.$walls, pos_out, size}; it.next();) {