diff options
Diffstat (limited to 'src/renderer.h')
| -rw-r--r-- | src/renderer.h | 132 |
1 files changed, 132 insertions, 0 deletions
| diff --git a/src/renderer.h b/src/renderer.h new file mode 100644 index 0000000..4aa27bb --- /dev/null +++ b/src/renderer.h | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | #ifndef RENDERER_H_6A58EC30 | ||
| 2 | #define RENDERER_H_6A58EC30 | ||
| 3 | |||
| 4 | #include <SDL.h> | ||
| 5 | #include <SDL_image.h> | ||
| 6 | #include <stdexcept> | ||
| 7 | #include <memory> | ||
| 8 | |||
| 9 | class Game; | ||
| 10 | |||
| 11 | class sdl_error : public std::logic_error { | ||
| 12 | public: | ||
| 13 | |||
| 14 | sdl_error() : std::logic_error(SDL_GetError()) | ||
| 15 | { | ||
| 16 | } | ||
| 17 | }; | ||
| 18 | |||
| 19 | class img_error : public std::logic_error { | ||
| 20 | public: | ||
| 21 | |||
| 22 | img_error() : std::logic_error(IMG_GetError()) | ||
| 23 | { | ||
| 24 | } | ||
| 25 | }; | ||
| 26 | |||
| 27 | class sdl_wrapper { | ||
| 28 | public: | ||
| 29 | |||
| 30 | sdl_wrapper() | ||
| 31 | { | ||
| 32 | if (SDL_Init(SDL_INIT_VIDEO) != 0) | ||
| 33 | { | ||
| 34 | sdl_error ex; | ||
| 35 | SDL_Quit(); | ||
| 36 | |||
| 37 | throw ex; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | ~sdl_wrapper() | ||
| 42 | { | ||
| 43 | SDL_Quit(); | ||
| 44 | } | ||
| 45 | }; | ||
| 46 | |||
| 47 | class img_wrapper { | ||
| 48 | public: | ||
| 49 | |||
| 50 | img_wrapper() | ||
| 51 | { | ||
| 52 | if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) | ||
| 53 | { | ||
| 54 | img_error ex; | ||
| 55 | IMG_Quit(); | ||
| 56 | |||
| 57 | throw ex; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | ~img_wrapper() | ||
| 62 | { | ||
| 63 | IMG_Quit(); | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | |||
| 67 | class window_deleter { | ||
| 68 | public: | ||
| 69 | |||
| 70 | void operator()(SDL_Window* ptr) | ||
| 71 | { | ||
| 72 | SDL_DestroyWindow(ptr); | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | |||
| 76 | using window_ptr = std::unique_ptr<SDL_Window, window_deleter>; | ||
| 77 | |||
| 78 | class renderer_deleter { | ||
| 79 | public: | ||
| 80 | |||
| 81 | void operator()(SDL_Renderer* ptr) | ||
| 82 | { | ||
| 83 | SDL_DestroyRenderer(ptr); | ||
| 84 | } | ||
| 85 | }; | ||
| 86 | |||
| 87 | using renderer_ptr = std::unique_ptr<SDL_Renderer, renderer_deleter>; | ||
| 88 | |||
| 89 | class surface_deleter { | ||
| 90 | public: | ||
| 91 | |||
| 92 | void operator()(SDL_Surface* ptr) | ||
| 93 | { | ||
| 94 | SDL_FreeSurface(ptr); | ||
| 95 | } | ||
| 96 | }; | ||
| 97 | |||
| 98 | using surface_ptr = std::unique_ptr<SDL_Surface, surface_deleter>; | ||
| 99 | |||
| 100 | class texture_deleter { | ||
| 101 | public: | ||
| 102 | |||
| 103 | void operator()(SDL_Texture* ptr) | ||
| 104 | { | ||
| 105 | SDL_DestroyTexture(ptr); | ||
| 106 | } | ||
| 107 | }; | ||
| 108 | |||
| 109 | using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>; | ||
| 110 | |||
| 111 | class Renderer { | ||
| 112 | public: | ||
| 113 | |||
| 114 | Renderer(); | ||
| 115 | |||
| 116 | void render( | ||
| 117 | const Game& game, | ||
| 118 | bool drawDark = true); | ||
| 119 | |||
| 120 | private: | ||
| 121 | |||
| 122 | sdl_wrapper sdl_; | ||
| 123 | img_wrapper img_; | ||
| 124 | window_ptr win_; | ||
| 125 | renderer_ptr ren_; | ||
| 126 | |||
| 127 | texture_ptr playerFade_; | ||
| 128 | texture_ptr lampFade_; | ||
| 129 | texture_ptr dustFade_; | ||
| 130 | }; | ||
| 131 | |||
| 132 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ | ||
