summary refs log tree commit diff stats
path: root/src/renderer.h
blob: 82d34a50daf25ccb464cf0016eab0d75948d5a1f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef RENDERER_H_6A58EC30
#define RENDERER_H_6A58EC30

#include <SDL.h>
#include <SDL_image.h>
#include <stdexcept>
#include <memory>
#include <map>
#include <string>
#include <vector>

class Game;
class Map;
class Sprite;
class Menu;

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 | SDL_INIT_AUDIO) != 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<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 Renderer {
public:

  Renderer();

  void render(Game& game);

  int loadImageFromFile(std::string filename);

private:

  // Important wrappers
  sdl_wrapper sdl_;
  img_wrapper img_;
  window_ptr win_;
  renderer_ptr ren_;

  // Textures loaded from files
  std::vector<texture_ptr> textures_;
  std::map<std::string, int> filenameToTexId_;

  // Menu rendering
  void renderMenu(Game& game, const Menu& menu);

  texture_ptr menuTex_;

  // Gameplay rendering
  void renderGame(Game& game);

  texture_ptr cameraTex_;

  // Sprite rendering
  void renderSprite(const Sprite& sprite);

  // Map rendering
  texture_ptr renderMapLayer(const Map& map, bool above);

  std::string cachedMapName_;
  std::string cachedTilesetName_;
  texture_ptr tilesetTex_;
  texture_ptr renLowerLayer_;
  texture_ptr renUpperLayer_;
  texture_ptr mapSwapTex_;

  // Text rendering
  struct MessageCache {
    texture_ptr renderedTex;
    std::vector<int> charIndexToWidth;
    std::string line;
    std::string overflow;
  };

  void renderMessageLine(MessageCache& line, const std::string& text, Game& game);

  MessageCache messageLines_[2];
  int speakerHeaderTex_ = -1;
  MessageCache speakerHeaderLine_;
  int advMsgArrowTex_ = -1;
  int choiceArrowTex_ = -1;

  // Debug console
  texture_ptr debugConsoleTex_;
  std::string cachedDebugText_;
  SDL_Rect debugDestRect_;
};

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