#include "sprite_factory.hpp" #include "../texture_config.h" #include "texture_manager.hpp" std::shared_ptr SpriteFactory::createSingleSprite(const std::string &name, sf::Vector2f size) { // Get sprite config auto sprite_config = all_sprites.find(name); if (sprite_config == all_sprites.end()) { LOG(ERROR) << "Sprite " << name << " not found. Could not create single sprite."; return nullptr; } // Construct sprite auto config = sprite_config->second; // Construct simply from texture if (!config.isFromSheet) { auto texture = TextureManager::getInstance()->getTexture(config.resourceName); if (texture == nullptr) { LOG(ERROR) << "Texture " << config.resourceName << " not found. Could not create single sprite."; return nullptr; } LOG(INFO) << "Creating single sprite from texture " << config.resourceName; return std::make_shared(texture, size); } // Construct from sheet auto sheet = createSheet(config.resourceName); if (sheet == nullptr) { LOG(ERROR) << "Sheet " << config.resourceName << " not found. Could not create single sprite."; return nullptr; } LOG(INFO) << "Creating single sprite from sheet " << config.resourceName; auto sprite = sheet->getSprite(config.sheetIndex); sprite->setSize(size); return sprite; } std::shared_ptr SpriteFactory::createAnimatedSprite(const std::string &name, sf::Vector2f size) { // Get animation config auto animation_config = all_animations.find(name); if (animation_config == all_animations.end()) { LOG(ERROR) << "Animation " << name << " not found. Could not create animated sprite."; return nullptr; } // Construct animation auto config = animation_config->second; auto sheet = createSheet(config.sheetName); if (sheet == nullptr) { LOG(ERROR) << "Sheet " << config.sheetName << " not found. Could not create animated sprite."; return nullptr; } LOG(INFO) << "Creating animated sprite from sheet " << config.sheetName; auto animation = sheet->getAnimation(config.startingSheetIndex, config.frameCount); animation->frameDuration = config.frameDuration; animation->setSize(size); return animation; } std::shared_ptr SpriteFactory::createSheet(const std::string &name) { // Get config auto sheet_config = all_sheets.find(name); if (sheet_config == all_sheets.end()) { LOG(ERROR) << "Sheet " << name << " not found. Could not create sheet."; return nullptr; } // Construct sheet auto config = sheet_config->second; auto texture = TextureManager::getInstance()->getTexture(config.textureName); if (texture == nullptr) { LOG(ERROR) << "Texture " << config.textureName << " not found. Could not create sheet."; return nullptr; } LOG(INFO) << "Creating sheet " << name; return std::make_shared(texture, config.columns, config.rows); } std::shared_ptr SpriteFactory::createTileSet(const std::string &name) { // Get config auto tileset_config = all_tilesets.find(name); if (tileset_config == all_tilesets.end()) { LOG(ERROR) << "Tileset " << name << " not found. Could not create tileset."; return nullptr; } // Construct tileset auto config = tileset_config->second; auto sheet = createSheet(config.sheetName); if (sheet == nullptr) { LOG(ERROR) << "Sheet " << config.sheetName << " not found. Could not create tileset."; return nullptr; } LOG(INFO) << "Creating tileset " << name; return std::make_shared(sheet, config.tileIndices); } std::shared_ptr SpriteFactory::copySingleSprite(const std::shared_ptr& sprite) { return std::make_shared(sprite->getSprite(), sprite->getSize()); } std::shared_ptr SpriteFactory::createTileMap(TileMapConfig config) { auto tileSet = createTileSet(config.tileSet); if (tileSet == nullptr) { LOG(ERROR) << "Tileset " << config.tileSet << " not found. Could not create tilemap."; return nullptr; } return std::make_shared(tileSet, config.tiles); }