Tiles now record their textures and this is loaded from the map then converted to an indexed integer on the fly.

master
Zed A. Shaw 1 month ago
parent 9e3e347e4a
commit a67d25ee10
  1. 4
      animator.cpp
  2. 2
      animator.hpp
  3. 9
      assets/config.json
  4. 8
      assets/tiles.json
  5. 55
      main.cpp
  6. 7
      raycaster.cpp
  7. 4
      raycaster.hpp
  8. 33
      texture.cpp
  9. 7
      texture.hpp
  10. 1
      worldbuilder.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();
}

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

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

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

@ -5,7 +5,10 @@
#include <functional>
#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<Player>();
auto &player_position = level.world->get<Position>(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;

@ -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<RGBA[]>($width * $height);
$textures.load_textures();
$textures.load_sprites();
$textures.position_sprite(4.0, 3.55, "evil_eye");
$view_texture.setSmooth(false);
}

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

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

@ -6,6 +6,7 @@
#include <SFML/Graphics.hpp>
#include <unordered_map>
#include <memory>
#include "matrix.hpp"
struct SpriteTexture {
std::shared_ptr<sf::Sprite> sprite = nullptr;
@ -22,15 +23,19 @@ struct TexturePack {
std::vector<sf::Image> surfaces;
std::vector<Sprite> sprites;
std::unordered_map<std::string, SpriteTexture> sprite_textures;
std::unordered_map<wchar_t, int> 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);
};

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

Loading…
Cancel
Save