RTYPE TECHNICAL DOCUMENTATION
  • Introduction
  • Classes
    • utils::InputMap
    • utils::Window
    • utils::constant::ButtonValue
    • network::Client
    • network::LockedQueue
    • network::Server
    • ecs::EnemyFactory
    • ecs::Engine
    • ecs::Entity
    • ecs::Event
    • ecs::Registry
    • ecs::SparseArray
    • ecs::World
    • ecs::WorldManager
    • ecs::component::Activable
    • ecs::component::Animated
    • ecs::component::Animated::AnimFrame
    • ecs::component::AttackAI
    • ecs::component::AttackAI::Action
    • ecs::component::AttackAI::AI
    • ecs::component::AttackAI::AI::Pattern
    • ecs::component::Controllable
    • ecs::component::Direction
    • ecs::component::Drawable
    • ecs::component::EntityType
    • ecs::component::Faction
    • ecs::component::FollowEntity
    • ecs::component::Health
    • ecs::component::Hitbox
    • ecs::component::MovementAI
    • ecs::component::MovementAI::AI
    • ecs::component::NetworkId
    • ecs::component::Parallax
    • ecs::component::Position
    • ecs::component::Projectile
    • ecs::component::Score
    • ecs::component::Shootable
    • ecs::component::Size
    • ecs::component::Text
    • ecs::component::textColor
    • ecs::component::Velocity
    • ecs::component::Weapon
    • audio::AudioManager
    • asset::AssetLoader
    • anim::Animation
  • Namespaces
    • utils
    • utils::constant
    • network
    • ecs
    • ecs::component
    • ecs::systems
    • audio
    • asset
    • anim
  • Modules
    • Input
  • Files
    • src
    • src/client
    • Animation.cpp
    • Animation.hpp
    • AssetLoader.cpp
    • AssetLoader.hpp
    • AudioManager.cpp
    • AudioManager.hpp
    • entrypoint.cpp
    • GetWorld.cpp
    • GetWorld.hpp
    • NetworkClient.cpp
    • NetworkClient.hpp
    • src/ecs
    • src/ecs/components
    • src/ecs/components/client
    • Activable.hpp
    • Animated.hpp
    • Controllable.hpp
    • Drawable.hpp
    • Hitbox.hpp
    • Parallax.hpp
    • Shootable.hpp
    • Text.hpp
    • src/ecs/components/server
    • AttackAI.cpp
    • AttackAI.hpp
    • FollowEntity.hpp
    • Projectile.hpp
    • Direction.hpp
    • EntityType.hpp
    • Faction.hpp
    • Health.hpp
    • MovementAI.cpp
    • MovementAI.hpp
    • NetworkId.hpp
    • Position.hpp
    • Score.hpp
    • Size.hpp
    • Velocity.hpp
    • Weapon.hpp
    • src/ecs/systems
    • src/ecs/systems/client
    • Animate.hpp
    • Draw.hpp
    • ExecuteOnce.hpp
    • HandleIncomingMessages.hpp
    • HandleParallaxBounds.hpp
    • HandleSFMLEvents.hpp
    • HandleSFMLKeys.hpp
    • HealthBar.hpp
    • MenuSelect.hpp
    • ScoreUpdate.hpp
    • SendDirection.hpp
    • src/ecs/systems/server
    • DeathUpdate.hpp
    • FollowEntitySystem.hpp
    • HandleIncomingMessage.hpp
    • PlayerHealthUpdate.hpp
    • PositionUpdate.hpp
    • ProjectileCollision.hpp
    • RunAttackAI.hpp
    • Waves.hpp
    • ManageClientEvents.hpp
    • Movement.hpp
    • PositionLogger.hpp
    • RunMovementAI.hpp
    • EnemyFactory.cpp
    • EnemyFactory.hpp
    • Engine.hpp
    • Entity.hpp
    • Event.hpp
    • LockedQueue.hpp
    • Registry.hpp
    • SparseArray.hpp
    • World.hpp
    • WorldManager.cpp
    • WorldManager.hpp
    • src/server
    • entrypoint.cpp
    • Server.cpp
    • Server.hpp
    • src/utils
    • Constant.hpp
    • InputMap.cpp
    • InputMap.hpp
    • Window.cpp
    • README.md
  • Pages
    • deprecated
  • GitHub
Powered by GitBook
On this page
  • Namespaces
  • Classes
  • Source code
  1. Files

AssetLoader.hpp

Namespaces

Name

Classes

Name

class

Source code

/*
** EPITECH PROJECT, 2022
** R-TYPEMIRROR
** File description:
** AssetLoader
*/

#pragma once

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <filesystem>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <unordered_map>
#include <utility>
#include "InputMap.hpp"

namespace asset
{
    class AssetLoader {
      public:
        ~AssetLoader() = default;

        template <class... Args>
        static void LoadTexture(
            const std::string &key, const std::filesystem::path &path, std::string_view next, Args... args)
        {
            sf::Texture texture;

            std::filesystem::path smart = smartPath(path, next, args...);

            if (!texture.loadFromFile(smart.generic_string()))
                return;
            getInstance()._textureMap[key] = texture;
        }

        static void LoadTexture(const std::string &key, std::vector<std::string> paths)
        {
            sf::Texture texture;

            std::filesystem::path smart = smartPath(std::move(paths));

            if (!texture.loadFromFile(smart.generic_string()))
                return;
            getInstance()._textureMap[key] = texture;
        }

        template <class... Args>
        static void LoadBGM(
            const std::string &key, const std::filesystem::path &path, std::string_view next, Args... args)
        {
            std::filesystem::path smart = smartPath(path, next, args...);

            if (!getInstance()._bgmMap[key].openFromFile(smart.generic_string()))
                return;
        }

        static void LoadBGM(const std::string &key, std::vector<std::string> paths)
        {
            std::filesystem::path smart = smartPath(std::move(paths));

            if (!getInstance()._bgmMap[key].openFromFile(smart.generic_string()))
                return;
        }

        template <class... Args>
        static void LoadSFX(
            const std::string &key, const std::filesystem::path &path, std::string_view next, Args... args)
        {
            std::filesystem::path smart = smartPath(path, next, args...);

            if (!getInstance()._sfxBufferMap[key].loadFromFile(smart.generic_string()))
                return;
            getInstance()._sfxMap[key].setBuffer(getInstance()._sfxBufferMap[key]);
        }

        static void LoadSFX(const std::string &key, std::vector<std::string> paths)
        {
            std::filesystem::path smart = smartPath(std::move(paths));

            if (!getInstance()._sfxBufferMap[key].loadFromFile(smart.generic_string()))
                return;
            getInstance()._sfxMap[key].setBuffer(getInstance()._sfxBufferMap[key]);
        }

        template <class... Args>
        static void LoadFont(
            const std::string &key, const std::filesystem::path &path, std::string_view next, Args... args)
        {
            sf::Font font;

            std::filesystem::path smart = smartPath(path, next, args...);

            if (!font.loadFromFile(smart.generic_string()))
                return;
            getInstance()._fontMap[key] = font;
        }

        static void LoadFont(const std::string &key, std::vector<std::string> paths)
        {
            sf::Font font;

            std::filesystem::path smart = smartPath(std::move(paths));

            if (!font.loadFromFile(smart.generic_string()))
                return;
            getInstance()._fontMap[key] = font;
        }

        static void loadKeybind(const std::string &action, const std::string &key)
        {
            getInstance()._keyMap[action] = utils::toKey(key);
        }

        static std::filesystem::path smartPath(std::vector<std::string> paths)
        {
            std::filesystem::path smart = paths[0];

            for (int i = 1; i < paths.size(); i++) {
                smart.append(paths[i]);
            }
            return smart;
        }

        static sf::Texture &GetTexture(const std::string &key) { return getInstance()._textureMap[key]; }

        static sf::Music &GetBGM(const std::string &key) { return getInstance()._bgmMap[key]; }

        static sf::Sound &GetSFX(const std::string &key) { return getInstance()._sfxMap[key]; }

        static sf::Font &GetFont(const std::string &key) { return getInstance()._fontMap[key]; }

        static sf::Keyboard::Key &GetKeybind(const std::string &key) { return getInstance()._keyMap[key]; }

        static void LoadIniFile(const std::filesystem::path &path)
        {
            boost::property_tree::ptree pt;
            try {
                boost::property_tree::ini_parser::read_ini(path.generic_string(), pt);
            } catch (std::exception &e) {
                std::cout << e.what() << std::endl;
            }

            // loop through the sections
            for (auto &section : pt) {
                for (auto &value : section.second) {
                    std::vector<std::string> paths;
                    // parse the section.second with slash and put it in the vector
                    while (value.second.data().find('/') != std::string::npos) {
                        paths.push_back(value.second.data().substr(0, value.second.data().find('/')));
                        value.second.data().erase(0, value.second.data().find('/') + 1);
                    }
                    if (section.first == "keybind") {
                        loadKeybind(value.first, value.second.data());
                        continue;
                    }
                    paths.push_back(value.second.data());

                    if (section.first == "texture")
                        LoadTexture(value.first, paths);
                    if (section.first == "bgm")
                        LoadBGM(value.first, paths);
                    if (section.first == "sfx")
                        LoadSFX(value.first, paths);
                    if (section.first == "font")
                        LoadFont(value.first, paths);
                }
            }
        }

        static AssetLoader &getInstance();

        static inline std::filesystem::path smartPath(std::filesystem::path path) { return path; }

        template <class... Args>
        static std::filesystem::path smartPath(std::filesystem::path path, std::string_view next, Args... args)
        {
            return smartPath(path.append(next), args...);
        }

        static void display_key_from_map()
        {
            std::cout << "len of map: " << getInstance()._textureMap.size() << std::endl;
            for (auto &key : getInstance()._textureMap) {
                std::cout << key.first << std::endl;
            }
        }

      private:
        std::unordered_map<std::string, sf::Texture> _textureMap;

        std::unordered_map<std::string, sf::Music> _bgmMap;

        std::unordered_map<std::string, sf::SoundBuffer> _sfxBufferMap;

        std::unordered_map<std::string, sf::Sound> _sfxMap;

        std::unordered_map<std::string, sf::Font> _fontMap;

        std::unordered_map<std::string, sf::Keyboard::Key> _keyMap;
    };
} // namespace asset

Updated on 2022-11-13 at 17:21:37 +0100

PreviousAssetLoader.cppNextAudioManager.cpp

Last updated 2 years ago

asset
asset::AssetLoader