summary refs log tree commit diff stats
path: root/sdl.h
blob: 46d1fa486cf9f9fba1acfa0a3bc35b88f3f5b1d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef SDL_H_A2226476
#define SDL_H_A2226476

#include <SDL.h>
#include <SDL_net.h>
#include <SDL_ttf.h>

#include <memory>

class sdl_error : public std::logic_error {
 public:
  sdl_error() : std::logic_error(SDL_GetError()) {}
};

class ttf_error : public std::logic_error {
 public:
  ttf_error() : std::logic_error(TTF_GetError()) {}
};

class net_error : public std::logic_error {
 public:
  net_error() : std::logic_error(SDLNet_GetError()) {}
};

class sdl_wrapper {
 public:
  sdl_wrapper() {
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
      sdl_error ex;
      SDL_Quit();

      throw ex;
    }
  }

  ~sdl_wrapper() { SDL_Quit(); }
};

class ttf_wrapper {
 public:
  ttf_wrapper() {
    if (TTF_Init() == -1) {
      ttf_error ex;
      TTF_Quit();

      throw ex;
    }
  }

  ~ttf_wrapper() { TTF_Quit(); }
};

class net_wrapper {
 public:
  net_wrapper() {
    if (SDLNet_Init() == -1) {
      net_error ex;
      SDLNet_Quit();

      throw ex;
    }
  }

  ~net_wrapper() { SDLNet_Quit(); }
};

class window_deleter {
 public:
  void operator()(SDL_Window* ptr) { SDL_DestroyWindow(ptr); }
};

using window_ptr = std::unique_ptr<SDL_Window, window_deleter>;

class renderer_deleter {
 public:
  void operator()(SDL_Renderer* ptr) { SDL_DestroyRenderer(ptr); }
};

using renderer_ptr = std::unique_ptr<SDL_Renderer, renderer_deleter>;

class surface_deleter {
 public:
  void operator()(SDL_Surface* ptr) { SDL_FreeSurface(ptr); }
};

using surface_ptr = std::unique_ptr<SDL_Surface, surface_deleter>;

class texture_deleter {
 public:
  void operator()(SDL_Texture* ptr) { SDL_DestroyTexture(ptr); }
};

using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>;

class font_deleter {
 public:
  void operator()(TTF_Font* ptr) { TTF_CloseFont(ptr); }
};

using font_ptr = std::unique_ptr<TTF_Font, font_deleter>;

#endif /* end of include guard: SDL_H_A2226476 */