From 3fa9af58bc907048a8ccadc2bef7736ec554a09d Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 5 Jun 2018 21:02:51 -0400 Subject: refactored so that the renderer is its own class --- src/renderer.h | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/renderer.h (limited to 'src/renderer.h') 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 @@ +#ifndef RENDERER_H_6A58EC30 +#define RENDERER_H_6A58EC30 + +#include +#include +#include +#include + +class Game; + +class sdl_error : public std::logic_error { +public: + + sdl_error() : std::logic_error(SDL_GetError()) + { + } +}; + +class img_error : public std::logic_error { +public: + + img_error() : std::logic_error(IMG_GetError()) + { + } +}; + +class sdl_wrapper { +public: + + sdl_wrapper() + { + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + sdl_error ex; + SDL_Quit(); + + throw ex; + } + } + + ~sdl_wrapper() + { + SDL_Quit(); + } +}; + +class img_wrapper { +public: + + img_wrapper() + { + if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) + { + img_error ex; + IMG_Quit(); + + throw ex; + } + } + + ~img_wrapper() + { + IMG_Quit(); + } +}; + +class window_deleter { +public: + + void operator()(SDL_Window* ptr) + { + SDL_DestroyWindow(ptr); + } +}; + +using window_ptr = std::unique_ptr; + +class renderer_deleter { +public: + + void operator()(SDL_Renderer* ptr) + { + SDL_DestroyRenderer(ptr); + } +}; + +using renderer_ptr = std::unique_ptr; + +class surface_deleter { +public: + + void operator()(SDL_Surface* ptr) + { + SDL_FreeSurface(ptr); + } +}; + +using surface_ptr = std::unique_ptr; + +class texture_deleter { +public: + + void operator()(SDL_Texture* ptr) + { + SDL_DestroyTexture(ptr); + } +}; + +using texture_ptr = std::unique_ptr; + +class Renderer { +public: + + Renderer(); + + void render( + const Game& game, + bool drawDark = true); + +private: + + sdl_wrapper sdl_; + img_wrapper img_; + window_ptr win_; + renderer_ptr ren_; + + texture_ptr playerFade_; + texture_ptr lampFade_; + texture_ptr dustFade_; +}; + +#endif /* end of include guard: RENDERER_H_6A58EC30 */ -- cgit 1.4.1