-
Печенин Данила Михайлович authored43dd0474
#include "Assets.h"
#include <array>
#include <random>
#include <Configuration.h>
sf::Font& Assets::GetFont() {
static sf::Font font;
if (!font.getInfo().family.empty()) return font;
if (!font.loadFromFile(config::FONT_FILE))
throw std::runtime_error("Failed to load font");
return font;
}
sf::Texture& Assets::GetPacmanTexture() {
static sf::Texture texture;
if (texture.getSize() != sf::Vector2u{0, 0}) return texture;
if (!texture.loadFromFile(config::PACMAN_FILE))
throw std::runtime_error("Failed to load texture: pacman");
return texture;
}
sf::Texture& Assets::GetEnemyTexture() {
static std::array<sf::Texture, config::ENEMY_TEXTURES_NUMBER> textures;
if (textures[0].getSize() == sf::Vector2u{0, 0}) {
if (!textures[0].loadFromFile(config::ENEMY_RED_FILE))
throw std::runtime_error("Failed to load texture: enemy red");
if (!textures[1].loadFromFile(config::ENEMY_BLUE_FILE))
throw std::runtime_error("Failed to load texture: enemy blue");
if (!textures[2].loadFromFile(config::ENEMY_YELLOW_FILE))
throw std::runtime_error("Failed to load texture: enemy yellow");
if (!textures[3].loadFromFile(config::ENEMY_PINK_FILE))
throw std::runtime_error("Failed to load texture: enemy pink");
}
static std::mt19937 gen{std::random_device{}()};
static std::uniform_int_distribution<size_t> dist{0, config::ENEMY_TEXTURES_NUMBER-1};
return textures[dist(gen)];
}
sf::Texture& Assets::GetFoodTexture() {
static std::array<sf::Texture, config::FOOD_TEXTURES_NUMBER> textures;
if (textures[0].getSize() == sf::Vector2u{0, 0}) {
if (!textures[0].loadFromFile(config::FOOD_1))
throw std::runtime_error("Failed to load texture: food 1");
if (!textures[1].loadFromFile(config::FOOD_2))
throw std::runtime_error("Failed to load texture: food 2");
if (!textures[2].loadFromFile(config::FOOD_3))
throw std::runtime_error("Failed to load texture: food 3");
if (!textures[3].loadFromFile(config::FOOD_4))
throw std::runtime_error("Failed to load texture: food 4");
if (!textures[4].loadFromFile(config::FOOD_5))
throw std::runtime_error("Failed to load texture: food 5");
if (!textures[5].loadFromFile(config::FOOD_6))
throw std::runtime_error("Failed to load texture: food 6");
if (!textures[6].loadFromFile(config::FOOD_7))
throw std::runtime_error("Failed to load texture: food 7");
}
static std::mt19937 gen{std::random_device{}()};
static std::uniform_int_distribution<size_t> dist{0, config::FOOD_TEXTURES_NUMBER-1};
return textures[dist(gen)];
}