34 lines
654 B
C++
34 lines
654 B
C++
#include <SDL2/SDL_image.h>
|
|
|
|
#include "ImageProvider.hh"
|
|
|
|
template<>
|
|
std::unique_ptr<ImageProvider> Singleton<ImageProvider>::instance{nullptr};
|
|
|
|
template<>
|
|
std::once_flag Singleton<ImageProvider>::instance_flag{};
|
|
|
|
ImageProvider::ImageProvider()
|
|
{
|
|
}
|
|
|
|
SDL_Surface *ImageProvider::getImage(std::string const& name)
|
|
{
|
|
auto it = imageCache_.find(name);
|
|
|
|
if(it == imageCache_.end()) {
|
|
SDLSurfaceUPtr image{IMG_Load(name.c_str())};
|
|
if (!image)
|
|
throw SDLException{};
|
|
tie(it, std::ignore) = imageCache_.insert(make_pair(name, std::move(image)));
|
|
}
|
|
|
|
return it->second.get();
|
|
}
|
|
|
|
|
|
void ImageProvider::cleanup()
|
|
{
|
|
imageCache_.clear();
|
|
}
|