SDL Breakout Engine  0.1.0
An implementation of a Breakout game using sdl, implemented with efficient resource management.
ImageManager.hpp
Go to the documentation of this file.
1 #ifndef IMAGE_MANAGER_HPP
2 #define IMAGE_MANAGER_HPP
3 
4 #include <SDL2/SDL.h>
5 #include <ResourceManager.hpp>
6 
10 class ImageManager : public ResourceManager<ImageManager, TextureWrapper>
11 {
12 public:
17  {
18  static ImageManager *instance = new ImageManager();
19  return *instance;
20  }
21 
25  int shutDown()
26  {
28  IMG_Quit();
29  return 1;
30  }
31 
37  int startUp(SDL_Renderer *in)
38  {
39  int imgFlags = IMG_INIT_PNG;
40  if (!(IMG_Init(imgFlags) & imgFlags))
41  {
42  SDL_LogError(SDL_LOG_CATEGORY_RENDER, "Couldn't start image manager");
43  return 0;
44  }
45  ren = in;
46  fileMap.emplace("ball", "./Assets/images/ballGrey.png");
47  fileMap.emplace("blue_rect", "./Assets/images/element_blue_rectangle.png");
48  fileMap.emplace("green_rect", "./Assets/images/element_green_rectangle.png");
49  fileMap.emplace("grey_rect", "./Assets/images/element_grey_rectangle.png");
50  fileMap.emplace("purple_rect", "./Assets/images/element_purple_rectangle.png");
51  fileMap.emplace("red_rect", "./Assets/images/element_red_rectangle.png");
52  fileMap.emplace("yellow_rect", "./Assets/images/element_yellow_rectangle.png");
53  fileMap.emplace("paddle", "./Assets/images/paddleRed.png");
54  return 1;
55  }
56 
62  std::shared_ptr<TextureWrapper> Load(const std::string &path)
63  {
64  SDL_Texture* img = IMG_LoadTexture(ren, path.c_str());
65 
66  if (img == NULL)
67  {
68  printf("%s\n", IMG_GetError());
69  return NULL;
70  }
71  else
72  {
73  SDL_Log("Allocated image memory for %s", path.c_str());
74  std::shared_ptr<SDL_Texture> surface = std::shared_ptr<SDL_Texture>(img, SDL_DestroyTexture);
75  return std::make_shared<TextureWrapper>(surface);
76  }
77  }
78 
79  ImageManager(ImageManager const &) = delete;
80  void operator=(ImageManager const &) = delete;
81 private:
82  SDL_Renderer *ren;
83  ImageManager() {
84  printf("Constructing image manager\n");
85  };
86 };
87 
88 #endif
void operator=(ImageManager const &) = delete
don't allow assignment
std::map< std::string, std::string > fileMap
stores location of resources
Definition: ResourceManager.hpp:156
int startUp(SDL_Renderer *in)
Definition: ImageManager.hpp:37
std::shared_ptr< TextureWrapper > Load(const std::string &path)
Definition: ImageManager.hpp:62
int shutDown()
Definition: ResourceManager.hpp:69
int shutDown()
Definition: ImageManager.hpp:25
static ImageManager & instance()
Definition: ImageManager.hpp:16
Definition: ResourceManager.hpp:62
Definition: ImageManager.hpp:10