about summary refs log tree commit diff stats
path: root/data/maps/daedalus/rooms/Post Orange Smiley Three Way.txtpb
diff options
context:
space:
mode:
Diffstat (limited to 'data/maps/daedalus/rooms/Post Orange Smiley Three Way.txtpb')
0 files changed, 0 insertions, 0 deletions
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#ifndef RENDERER_H_6A58EC30
#define RENDERER_H_6A58EC30

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdexcept>
#include <memory>
#include <array>
#include <string_view>
#include <vector>
#include <string>
#include "consts.h"

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 ttf_error : public std::logic_error {
public:

  ttf_error() : std::logic_error(TTF_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 ttf_wrapper {
public:

  ttf_wrapper()
  {
    if (TTF_Init() != 0)
    {
      ttf_error ex;
      TTF_Quit();

    	throw ex;
    }
  }

  ~ttf_wrapper()
  {
    TTF_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>;

class Renderer {
public:

  Renderer();

  void renderGame(
    const Game& game,
    bool drawDark = true);

  void renderTitle(int num, double fade);

  TTF_Font* getFont() { return font_.get(); }

private:

  void loadTextureFromFile(std::string_view path, texture_ptr& texture);

  sdl_wrapper sdl_;
  img_wrapper img_;
  ttf_wrapper ttf_;
  window_ptr win_;
  renderer_ptr ren_;
  font_ptr font_;

  texture_ptr playerFade_;
  texture_ptr lampFade_;
  texture_ptr dustFade_;
  texture_ptr playerSheet_;
  texture_ptr tileset_;
  texture_ptr lamp_;
  texture_ptr readInstruction_;

  std::array<texture_ptr, NUM_TITLES> titles_;
  std::array<int, NUM_TITLES> titleWidths_;
  std::array<int, NUM_TITLES> titleHeights_;

  // 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, const Game& game);

  MessageCache messageLines_[2];
};

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