Commit a7a361a9 authored by louiz’'s avatar louiz’

Add an asset manager, to simplify the handling of assets filenames

parent d76afe3d
......@@ -161,6 +161,19 @@ target_link_libraries(
${Boost_LIBRARIES}
)
#
## Assets
#
file(GLOB source_assets
src/assets/*.[hc]pp
)
add_library(assets STATIC ${source_assets})
target_link_libraries(assets
${Boost_LIBRARIES}
)
#
## Database
#
......@@ -191,6 +204,7 @@ if(BUILD_CLIENT)
config
world
utils
assets
${SFML_LIBRARIES}
${Boost_LIBRARIES}
)
......@@ -210,6 +224,7 @@ file(GLOB source_world
add_library(world STATIC ${source_world})
target_Link_libraries(world
assets
logging
serialization
fixmath
......
#include <assets/assets_manager.hpp>
#include <boost/filesystem.hpp>
#include <logging/logging.hpp>
boost::filesystem::path AssetsManager::data_path;
void AssetsManager::set_asset_directory(const boost::filesystem::path& path)
{
AssetsManager::data_path = boost::filesystem::canonical(path);
}
std::string AssetsManager::full_name(const boost::filesystem::path& path)
{
auto full_path = AssetsManager::data_path / path;
return full_path.native();
}
#ifndef ASSETS_MANAGER_HPP_INCLUDED
#define ASSETS_MANAGER_HPP_INCLUDED
#include <boost/filesystem/path.hpp>
#include <string>
class AssetsManager
{
public:
static void set_asset_directory(const boost::filesystem::path& path);
static std::string full_name(const boost::filesystem::path& path);
private:
static boost::filesystem::path data_path;
AssetsManager() = delete;
~AssetsManager() = delete;
AssetsManager(const AssetsManager&) = delete;
AssetsManager(AssetsManager&&) = delete;
AssetsManager& operator=(const AssetsManager&) = delete;
AssetsManager& operator=(AssetsManager&&) = delete;
};
#endif /* ASSETS_MANAGER_HPP_INCLUDED */
#include <game/sounds_handler.hpp>
#include <assets/assets_manager.hpp>
#include <stdexcept>
#include <string>
......@@ -8,48 +9,50 @@
using namespace std::string_literals;
using namespace std::chrono_literals;
using AM = AssetsManager;
SoundsHandler::SoundsHandler():
rd(),
random_engine(rd())
{
this->load("data/sounds/gyro/Gyro_move_01.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_02.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_03.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_05.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_06.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_07.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_08.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_10.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_11.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_12.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_13.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_16.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_19.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_25.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_27.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_30.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_31.wav", SoundType::DefaultOk);
this->load("data/sounds/gyro/Gyro_move_32.wav", SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_01.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_02.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_03.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_05.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_06.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_07.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_08.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_10.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_11.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_12.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_13.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_16.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_19.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_25.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_27.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_30.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_31.wav"), SoundType::DefaultOk);
this->load(AM::full_name("sounds/gyro/Gyro_move_32.wav"), SoundType::DefaultOk);
this->load("data/sounds/drow/Drow_Ranger_projectile_impact1.wav", SoundType::ProjectileImpact);
this->load("data/sounds/drow/Drow_Ranger_projectile_impact2.wav", SoundType::ProjectileImpact);
this->load("data/sounds/drow/Drow_Ranger_projectile_impact3.wav", SoundType::ProjectileImpact);
this->load(AM::full_name("sounds/drow/Drow_Ranger_projectile_impact1.wav"), SoundType::ProjectileImpact);
this->load(AM::full_name("sounds/drow/Drow_Ranger_projectile_impact2.wav"), SoundType::ProjectileImpact);
this->load(AM::full_name("sounds/drow/Drow_Ranger_projectile_impact3.wav"), SoundType::ProjectileImpact);
this->load("data/sounds/drow/Drow_Ranger_projectile_launch1.wav", SoundType::ProjectileLaunch);
this->load("data/sounds/drow/Drow_Ranger_projectile_launch2.wav", SoundType::ProjectileLaunch);
this->load("data/sounds/drow/Drow_Ranger_projectile_launch3.wav", SoundType::ProjectileLaunch);
this->load("data/sounds/drow/Drow_Ranger_projectile_launch4.wav", SoundType::ProjectileLaunch);
this->load(AM::full_name("sounds/drow/Drow_Ranger_projectile_launch1.wav"), SoundType::ProjectileLaunch);
this->load(AM::full_name("sounds/drow/Drow_Ranger_projectile_launch2.wav"), SoundType::ProjectileLaunch);
this->load(AM::full_name("sounds/drow/Drow_Ranger_projectile_launch3.wav"), SoundType::ProjectileLaunch);
this->load(AM::full_name("sounds/drow/Drow_Ranger_projectile_launch4.wav"), SoundType::ProjectileLaunch);
this->load("data/sounds/items/Blink_start_(AM).wav", SoundType::BlinkStart);
this->load(AM::full_name("sounds/items/Blink_start_(AM).wav"), SoundType::BlinkStart);
this->load("data/sounds/invoker/EMP_target.wav", SoundType::EmpStart);
this->load("data/sounds/invoker/EMP_effect.wav", SoundType::EmpExplode);
this->load(AM::full_name("sounds/invoker/EMP_target.wav"), SoundType::EmpStart);
this->load(AM::full_name("sounds/invoker/EMP_effect.wav"), SoundType::EmpExplode);
this->load("data/sounds/tusk/Walrus_Punch_cast.wav", SoundType::Concentrate);
this->load("data/sounds/sand_king/Burrowstrike.wav", SoundType::Dash);
this->load(AM::full_name("sounds/invoker/Alacrity.wav"), SoundType::Concentrate);
this->load(AM::full_name("sounds/sand_king/Burrowstrike.wav"), SoundType::Dash);
}
void SoundsHandler::load(const char* filename, const SoundType& type)
void SoundsHandler::load(const std::string& filename, const SoundType& type)
{
this->buffers[type].push_back({});
sf::SoundBuffer& buffer = this->buffers[type].back();
......
......@@ -39,7 +39,7 @@ public:
void play(const SoundType& type, const bool limit=false, const float volume=100.f);
private:
void load(const char* filename, const SoundType& type);
void load(const std::string& filename, const SoundType& type);
std::map<SoundType, std::vector<sf::SoundBuffer>> buffers;
/**
......
#include <logging/logging.hpp>
#include <assets/assets_manager.hpp>
#include <gui/camera/camera.hpp>
#include <gui/screen/screen.hpp>
#include <gui/sprites/pic_sprite.hpp>
......
#include <boost/property_tree/xml_parser.hpp>
#include <assets/assets_manager.hpp>
#include <gui/camera/tileset.hpp>
#include <logging/logging.hpp>
#include <utils/string.hpp>
#include <boost/utility.hpp>
using namespace std::string_literals;
Tileset::Tileset()
{
// A value of 0 in a cell means no tile, so the TileGraph
......@@ -18,7 +22,7 @@ Tileset::~Tileset()
bool Tileset::load_from_file(const std::string& map_name)
{
std::string filename = MAPS_DIRECTORY + map_name;
const auto filename = AssetsManager::full_name("maps/"s + map_name);
boost::property_tree::ptree tree;
try
......@@ -50,7 +54,7 @@ bool Tileset::read_tileset(const boost::property_tree::ptree& tileset_tree)
return false;
}
const boost::property_tree::ptree image_tree = tileset_tree.get_child("image");
const std::string source = MAPS_DIRECTORY + image_tree.get<std::string>("<xmlattr>.source", "");
const std::string source = AssetsManager::full_name("maps/"s + image_tree.get<std::string>("<xmlattr>.source", ""));
const uint32_t image_height = image_tree.get<uint32_t>("<xmlattr>.height", 0);
const uint32_t image_width = image_tree.get<uint32_t>("<xmlattr>.width", 0);
const uint32_t tile_height = tileset_tree.get<uint32_t>("<xmlattr>.tileheight", 0);
......
......@@ -6,8 +6,7 @@
#include <vector>
#include <string>
static const std::string MINIMAPS_DIRECTORY("./data/minimaps/");
static const std::string TILESETS_DIRECTORY("./data/tilesets/");
static const std::string TILESETS_DIRECTORY("tilesets/");
#include <gui/camera/tile.hpp>
#include <world/map.hpp>
......
......@@ -5,6 +5,8 @@
#include <gui/hud/hud.hpp>
#include <world/entity.hpp>
#include <assets/assets_manager.hpp>
#include <world/abilities.hpp>
#include <world/abilities/blink.hpp>
......@@ -26,7 +28,7 @@ Hud::Hud(GameClient* game, Screen* screen):
// whenever the selection is modified.
// world->add_selection_change_callback(std::bind(&ActionPanel::reset_all_tables, &this->action_panel));
this->hud_texture.loadFromFile("./data/images/hud.png");
this->hud_texture.loadFromFile(AssetsManager::full_name("images/hud.png"));
this->hud_sprite.setTexture(this->hud_texture);
this->hud_sprite.setPosition(0, win_size.y - HUD_HEIGHT);
if (!this->font.loadFromFile("/usr/share/fonts/dejavu/DejaVuSans.ttf"))
......
......@@ -2,6 +2,7 @@
#include <gui/hud/selection_panel.hpp>
#include <game/game_client.hpp>
#include <gui/screen/screen.hpp>
#include <assets/assets_manager.hpp>
SelectionPanel::SelectionPanel(GameClient* game, Screen* screen, const Selection* selection):
ScreenElement(screen),
......@@ -26,7 +27,7 @@ void SelectionPanel::draw()
this->current_tab = 0;
auto entities = this->selection->get_entities();
sf::Texture texture;
texture.loadFromFile("data/images/unit50.png");
texture.loadFromFile(AssetsManager::full_name("images/unit50.png"));
sf::Sprite sprite;
sprite.setTexture(texture);
uint x = PANEL_X;
......
#include <logging/logging.hpp>
#include <gui/screen/screen.hpp>
#include <gui/common.hpp>
#include <assets/assets_manager.hpp>
using namespace std::string_literals;
LeftClick LeftClick::null = {nullptr, nullptr};
......@@ -8,10 +11,9 @@ Screen::Screen():
win{sf::VideoMode(1920, 1080), "Bata", sf::Style::Default, sf::ContextSettings{}},
current_cursor_type(cursor::Normal)
{
const std::string data_path("./data/images/");
for (int i = 0; i < cursor::size; ++i)
{
const std::string filename = data_path + cursor::filenames[i];
const std::string filename = AssetsManager::full_name("images/"s + cursor::filenames[i]);
if (!this->cursor_textures[i].loadFromFile(filename.c_str()))
throw GraphInitError();
}
......
......@@ -3,6 +3,8 @@
#include <world/mobility.hpp>
#include <world/entity.hpp>
#include <gui/effects/smoke_trail.hpp>
bool BulletSprite::init = false;
sf::Texture BulletSprite::texture;
......@@ -11,7 +13,7 @@ BulletSprite::BulletSprite(const Entity* const entity):
{
if (this->init == false)
{
if (!this->texture.loadFromFile("data/images/bullet.png"))
if (!this->texture.loadFromFile(AssetsManager::full_name("images/bullet.png")))
exit(1);
this->texture.setSmooth(true);
this->init = true;
......
......@@ -18,7 +18,7 @@ EntitySprite::EntitySprite(const Entity* const entity):
{
if (EntitySprite::init == false)
{
if (!EntitySprite::shadow_texture.loadFromFile("data/images/units/shadow.png"))
if (!EntitySprite::shadow_texture.loadFromFile(AssetsManager::full_name("images/units/shadow.png")))
exit(1);
EntitySprite::init = true;
}
......
......@@ -15,9 +15,9 @@ PicpicSprite::PicpicSprite(const Entity* const entity):
{
if (PicpicSprite::init == false)
{
if (!PicpicSprite::body_texture.loadFromFile("data/images/units/picpic.svg.png"))
if (!PicpicSprite::body_texture.loadFromFile(AssetsManager::full_name("images/units/picpic.svg.png")))
exit(1);
if (!PicpicSprite::eye_texture.loadFromFile("data/images/units/picpic_eye.svg.png"))
if (!PicpicSprite::eye_texture.loadFromFile(AssetsManager::full_name("images/units/picpic_eye.svg.png")))
exit(1);
PicpicSprite::body_texture.setSmooth(true);
PicpicSprite::eye_texture.setSmooth(true);
......
......@@ -12,7 +12,7 @@ TourbillonSprite::TourbillonSprite(const Entity* const entity):
{
if (TourbillonSprite::init == false)
{
if (!TourbillonSprite::body_texture.loadFromFile("data/images/units/tourbillon.png"))
if (!TourbillonSprite::body_texture.loadFromFile(AssetsManager::full_name("images/units/tourbillon.png")))
exit(1);
TourbillonSprite::body_texture.setSmooth(true);
TourbillonSprite::init = true;
......
......@@ -5,6 +5,7 @@
#include <world/position.hpp>
#include <gui/sprites/energy_bar.hpp>
#include <assets/assets_manager.hpp>
class WorldSprite
{
......
#include <config/config.hpp>
#include <logging/logging.hpp>
#include <game/game_client.hpp>
#include <assets/assets_manager.hpp>
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
int main()
#include <boost/program_options.hpp>
int main(int ac, char** av)
{
std::ios_base::sync_with_stdio(false);
srandom(getpid());
std::string data_directory;
boost::program_options::variables_map vm;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help,h",
"Display this help message")
("data-dir,d",
boost::program_options::value<std::string>(&data_directory)->default_value("../data"),
"The (full or relative) path to the directory containing the various assets");
AssetsManager::set_asset_directory("../data/");
try
{
boost::program_options::store(boost::program_options::command_line_parser(ac, av).options(desc).run(),
vm);
boost::program_options::notify(vm);
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
if (vm.count("help"))
{
std::cout << desc;
return 0;
}
AssetsManager::set_asset_directory(data_directory);
auto screen = std::make_shared<Screen>();
GameClient client(screen);
......
......@@ -3,6 +3,7 @@
#include <network/server.hpp>
#include <game/remote_game_client.hpp>
#include <game/game_server.hpp>
#include <assets/assets_manager.hpp>
#include <utils/time.hpp>
#include <iostream>
......@@ -57,6 +58,8 @@ int main(int ac, char** av)
if (!Config::read_conf("./batajelo.conf"))
return 1;
AssetsManager::set_asset_directory("../data");
log_debug("Starting game server, id: " << game_id << ". Port: " << port << " and config file: " << conf_filename
<< " and ipc path [" << ipc_path << "]");
......
......@@ -5,12 +5,15 @@
#include <boost/utility.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/trim_all.hpp>
#include <assets/assets_manager.hpp>
#include <logging/logging.hpp>
#include <world/map.hpp>
#include <utils/base64.hpp>
#include <utils/zlib.hpp>
using namespace std::string_literals;
Map::Map():
width(0),
height(0),
......@@ -33,8 +36,7 @@ Map::~Map()
bool Map::load_from_file(const std::string& map_name)
{
std::string filename(MAPS_DIRECTORY);
filename += map_name;
const std::string filename = AssetsManager::full_name("maps/"s + map_name);
log_debug("Loading map: " << filename);
boost::property_tree::ptree tree;
......
......@@ -25,8 +25,6 @@ static constexpr unsigned int TILE_TOP_OFFSET = 56;
#define TILESET_WIDTH 2
#define TILESET_HEIGHT 6
#define MAPS_DIRECTORY "./data/maps/"
/**
* Defines the heights of the four corners of various tiles based on where
* they are positionned in the tile set. For example a tile positionned on
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment