Exploring raycasters and possibly make a little "doom like" game based on it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
raycaster/texture.cpp

55 lines
1.4 KiB

#include "texture.hpp"
#include <SFML/Graphics/Image.hpp>
#include "dbc.hpp"
#include <fmt/core.h>
#include "config.hpp"
#include "constants.hpp"
using std::shared_ptr, std::make_shared;
sf::Image TexturePack::load_image(std::string filename) {
sf::Image texture;
bool good = texture.loadFromFile(filename);
dbc::check(good, fmt::format("failed to load {}", filename));
return texture;
}
void TexturePack::load_sprites() {
Config assets("assets/config.json");
for(auto& el : assets["sprites"].items()) {
string path = el.value();
auto texture = make_shared<sf::Texture>(path);
texture->setSmooth(false);
auto sprite = make_shared<sf::Sprite>(*texture);
string name = el.key();
sprite_textures[name] = {sprite, texture};
}
sword = sprite_textures["sword"];
}
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");
for(string tile_path : assets["textures"]) {
surfaces.emplace_back(load_image(tile_path));
}
floor = load_image(assets["sprites"]["floor"]);
ceiling = load_image(assets["sprites"]["ceiling"]);
}
const uint32_t* TexturePack::get_surface(size_t num) {
return (const uint32_t *)surfaces[num].getPixelsPtr();
}
Sprite &TexturePack::get_sprite(size_t sprite_num) {
return sprites[sprite_num];
}