holesome/src/sprites/sprite_factory.cpp

139 lines
4.3 KiB
C++
Raw Normal View History

#include "sprite_factory.hpp"
#include "../texture_config.h"
#include "texture_manager.hpp"
2023-06-17 19:48:51 +02:00
std::shared_ptr<SingleSprite> 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<SingleSprite>(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;
}
2023-06-17 19:48:51 +02:00
std::shared_ptr<AnimatedSprite> 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;
}
2023-06-17 19:48:51 +02:00
std::shared_ptr<SpriteSheet> 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<SpriteSheet>(texture, config.columns, config.rows);
}
2023-06-11 13:24:27 +02:00
std::shared_ptr<TileSet> 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<TileSet>(sheet, config.tileIndices);
}
2023-06-17 19:48:51 +02:00
std::shared_ptr<SingleSprite> SpriteFactory::copySingleSprite(const std::shared_ptr<SingleSprite>& sprite)
{
return std::make_shared<SingleSprite>(sprite->getSprite(), sprite->getSize());
}
std::shared_ptr<TileMap> 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<TileMap>(tileSet, config.tiles);
}