diff options
Diffstat (limited to 'src/renderer.h')
| -rw-r--r-- | src/renderer.h | 57 |
1 files changed, 57 insertions, 0 deletions
| diff --git a/src/renderer.h b/src/renderer.h index de1e125..ce2e7e1 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
| @@ -3,10 +3,13 @@ | |||
| 3 | 3 | ||
| 4 | #include <SDL.h> | 4 | #include <SDL.h> |
| 5 | #include <SDL_image.h> | 5 | #include <SDL_image.h> |
| 6 | #include <SDL_ttf.h> | ||
| 6 | #include <stdexcept> | 7 | #include <stdexcept> |
| 7 | #include <memory> | 8 | #include <memory> |
| 8 | #include <array> | 9 | #include <array> |
| 9 | #include <string_view> | 10 | #include <string_view> |
| 11 | #include <vector> | ||
| 12 | #include <string> | ||
| 10 | #include "consts.h" | 13 | #include "consts.h" |
| 11 | 14 | ||
| 12 | class Game; | 15 | class Game; |
| @@ -27,6 +30,14 @@ public: | |||
| 27 | } | 30 | } |
| 28 | }; | 31 | }; |
| 29 | 32 | ||
| 33 | class ttf_error : public std::logic_error { | ||
| 34 | public: | ||
| 35 | |||
| 36 | ttf_error() : std::logic_error(TTF_GetError()) | ||
| 37 | { | ||
| 38 | } | ||
| 39 | }; | ||
| 40 | |||
| 30 | class sdl_wrapper { | 41 | class sdl_wrapper { |
| 31 | public: | 42 | public: |
| 32 | 43 | ||
| @@ -67,6 +78,26 @@ public: | |||
| 67 | } | 78 | } |
| 68 | }; | 79 | }; |
| 69 | 80 | ||
| 81 | class ttf_wrapper { | ||
| 82 | public: | ||
| 83 | |||
| 84 | ttf_wrapper() | ||
| 85 | { | ||
| 86 | if (TTF_Init() != 0) | ||
| 87 | { | ||
| 88 | ttf_error ex; | ||
| 89 | TTF_Quit(); | ||
| 90 | |||
| 91 | throw ex; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | ~ttf_wrapper() | ||
| 96 | { | ||
| 97 | TTF_Quit(); | ||
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 70 | class window_deleter { | 101 | class window_deleter { |
| 71 | public: | 102 | public: |
| 72 | 103 | ||
| @@ -111,6 +142,16 @@ public: | |||
| 111 | 142 | ||
| 112 | using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>; | 143 | using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>; |
| 113 | 144 | ||
| 145 | class font_deleter { | ||
| 146 | public: | ||
| 147 | |||
| 148 | void operator()(TTF_Font* ptr) { | ||
| 149 | TTF_CloseFont(ptr); | ||
| 150 | } | ||
| 151 | }; | ||
| 152 | |||
| 153 | using font_ptr = std::unique_ptr<TTF_Font, font_deleter>; | ||
| 154 | |||
| 114 | class Renderer { | 155 | class Renderer { |
| 115 | public: | 156 | public: |
| 116 | 157 | ||
| @@ -122,14 +163,18 @@ public: | |||
| 122 | 163 | ||
| 123 | void renderTitle(int num, double fade); | 164 | void renderTitle(int num, double fade); |
| 124 | 165 | ||
| 166 | TTF_Font* getFont() { return font_.get(); } | ||
| 167 | |||
| 125 | private: | 168 | private: |
| 126 | 169 | ||
| 127 | void loadTextureFromFile(std::string_view path, texture_ptr& texture); | 170 | void loadTextureFromFile(std::string_view path, texture_ptr& texture); |
| 128 | 171 | ||
| 129 | sdl_wrapper sdl_; | 172 | sdl_wrapper sdl_; |
| 130 | img_wrapper img_; | 173 | img_wrapper img_; |
| 174 | ttf_wrapper ttf_; | ||
| 131 | window_ptr win_; | 175 | window_ptr win_; |
| 132 | renderer_ptr ren_; | 176 | renderer_ptr ren_; |
| 177 | font_ptr font_; | ||
| 133 | 178 | ||
| 134 | texture_ptr playerFade_; | 179 | texture_ptr playerFade_; |
| 135 | texture_ptr lampFade_; | 180 | texture_ptr lampFade_; |
| @@ -142,6 +187,18 @@ private: | |||
| 142 | std::array<texture_ptr, NUM_TITLES> titles_; | 187 | std::array<texture_ptr, NUM_TITLES> titles_; |
| 143 | std::array<int, NUM_TITLES> titleWidths_; | 188 | std::array<int, NUM_TITLES> titleWidths_; |
| 144 | std::array<int, NUM_TITLES> titleHeights_; | 189 | std::array<int, NUM_TITLES> titleHeights_; |
| 190 | |||
| 191 | // Text rendering | ||
| 192 | struct MessageCache { | ||
| 193 | texture_ptr renderedTex; | ||
| 194 | std::vector<int> charIndexToWidth; | ||
| 195 | std::string line; | ||
| 196 | std::string overflow; | ||
| 197 | }; | ||
| 198 | |||
| 199 | void renderMessageLine(MessageCache& line, const std::string& text, const Game& game); | ||
| 200 | |||
| 201 | MessageCache messageLines_[2]; | ||
| 145 | }; | 202 | }; |
| 146 | 203 | ||
| 147 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ | 204 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ |
