SDL Breakout Engine  0.1.0
An implementation of a Breakout game using sdl, implemented with efficient resource management.
SpriteManager.hpp
Go to the documentation of this file.
1 #ifndef SPRITE_MANAGER_HPP
2 #define SPRITE_MANAGER_HPP
3 
4 #include <SDL2/SDL.h>
5 #include <ResourceManager.hpp>
6 
10 class SpriteManager : public ResourceManager<SpriteManager, Sprite>
11 {
12 public:
17  {
18  static SpriteManager *instance = new SpriteManager();
19  return *instance;
20  }
21 
27  int startUp(SDL_Renderer *in)
28  {
29  ren = in;
30  fileMap.emplace("sprite", "./Assets/sprites/sprite.bmp"); // can add more sprites here
31  return 1;
32  }
33 
39  std::shared_ptr<Sprite> Load(const std::string &path)
40  {
41 
42  std::shared_ptr<SDL_Surface> surface = std::shared_ptr<SDL_Surface>(SDL_LoadBMP(path.data()), SDL_FreeSurface);
43 
44  if (surface == NULL)
45  {
46  SDL_Log("Failed to allocate surface");
47  return NULL;
48  }
49  else
50  {
51  SDL_Log("Allocated a bunch of memory to create identical game character");
52  // Create a texture from our surface
53  // Textures run faster and take advantage of hardware acceleration
54  std::shared_ptr<Sprite> s = std::make_shared<Sprite>();
55  s->spriteSheet = surface;
56  s->texture = std::shared_ptr<SDL_Texture>(SDL_CreateTextureFromSurface(ren, surface.get()), SDL_DestroyTexture);
57  return s;
58  }
59  }
60 
61 private:
62  SDL_Renderer *ren;
63 };
64 
65 #endif
std::shared_ptr< Sprite > Load(const std::string &path)
Definition: SpriteManager.hpp:39
std::map< std::string, std::string > fileMap
stores location of resources
Definition: ResourceManager.hpp:156
int startUp(SDL_Renderer *in)
Definition: SpriteManager.hpp:27
static SpriteManager & instance()
Definition: SpriteManager.hpp:16
Definition: ResourceManager.hpp:62
Definition: SpriteManager.hpp:10