summary refs log tree commit diff stats
path: root/src/renderer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderer.h')
-rw-r--r--src/renderer.h132
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
9class Game;
10
11class sdl_error : public std::logic_error {
12public:
13
14 sdl_error() : std::logic_error(SDL_GetError())
15 {
16 }
17};
18
19class img_error : public std::logic_error {
20public:
21
22 img_error() : std::logic_error(IMG_GetError())
23 {
24 }
25};
26
27class sdl_wrapper {
28public:
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
47class img_wrapper {
48public:
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
67class window_deleter {
68public:
69
70 void operator()(SDL_Window* ptr)
71 {
72 SDL_DestroyWindow(ptr);
73 }
74};
75
76using window_ptr = std::unique_ptr<SDL_Window, window_deleter>;
77
78class renderer_deleter {
79public:
80
81 void operator()(SDL_Renderer* ptr)
82 {
83 SDL_DestroyRenderer(ptr);
84 }
85};
86
87using renderer_ptr = std::unique_ptr<SDL_Renderer, renderer_deleter>;
88
89class surface_deleter {
90public:
91
92 void operator()(SDL_Surface* ptr)
93 {
94 SDL_FreeSurface(ptr);
95 }
96};
97
98using surface_ptr = std::unique_ptr<SDL_Surface, surface_deleter>;
99
100class texture_deleter {
101public:
102
103 void operator()(SDL_Texture* ptr)
104 {
105 SDL_DestroyTexture(ptr);
106 }
107};
108
109using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>;
110
111class Renderer {
112public:
113
114 Renderer();
115
116 void render(
117 const Game& game,
118 bool drawDark = true);
119
120private:
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 */