HoverRace  2.0
TypeCase.h
Go to the documentation of this file.
1 
2 // TypeCase.h
3 //
4 // Copyright (c) 2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <SDL2/SDL.h>
25 
26 #include "Color.h"
27 
28 #include "UiFont.h"
29 
30 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
31 # ifdef MR_ENGINE
32 # define MR_DllDeclare __declspec( dllexport )
33 # else
34 # define MR_DllDeclare __declspec( dllimport )
35 # endif
36 #else
37 # define MR_DllDeclare
38 #endif
39 
40 namespace HoverRace {
41 namespace Display {
42 
43 class TypeCase;
44 class TypeLine;
45 
51 {
52 public:
53  GlyphEntry() : cp(0), advance(0), page(0) { }
54 
55 public:
56  bool IsInitialized() const { return cp > 0; }
57 
58 public:
60  int advance;
61  unsigned int page;
62  SDL_Rect srcRect;
63 };
64 
70 {
71 public:
78  TypeCase(const UiFont &font, int width, int height) :
79  font(font), width(width), height(height) { }
80  TypeCase(const TypeCase&) = delete;
81 
82  virtual ~TypeCase() { }
83 
84  TypeCase &operator=(const TypeCase&) = delete;
85 
86 public:
91  int GetWidth() const { return width; }
92 
97  int GetHeight() const { return height; }
98 
103  virtual MR_UInt32 CountTextures() const = 0;
104 
105 public:
115  virtual void Prepare(const std::string &s, TypeLine *rects = nullptr) = 0;
116 
129  virtual void Render(const TypeLine &s, const Color color, int x, int y,
130  boost::optional<size_t> caret = {}) = 0;
131 
139  virtual void RenderTexture(MR_UInt32 idx, int x, int y,
140  double scale = 1.0) = 0;
141 
142 protected:
143  const UiFont font;
144  const int width;
145  const int height;
146 };
147 
157 {
158 public:
159  TypeLine() : width(0), height(0) { }
160 
161 public:
166  bool IsPrepared() const {
167  return static_cast<bool>(typeCase);
168  }
169 
177  void Render(const Color color, int x, int y,
178  boost::optional<size_t> caret = {})
179  {
180  if (typeCase) {
181  typeCase->Render(*this, color, x, y, caret);
182  }
183  }
184 
185 public:
186  std::shared_ptr<TypeCase> typeCase; // Owning TypeCase, for sanity checking.
187  std::vector<std::pair<GlyphEntry*, SDL_Rect>> glyphs;
188  int width;
189  int height;
190 };
191 
192 } // namespace Display
193 } // namespace HoverRace
194 
195 #undef MR_DllDeclare
Definition: Color.h:40
GlyphEntry()
Definition: TypeCase.h:53
MR_UInt32 cp
The Unicode character point represented by this glyph.
Definition: TypeCase.h:59
const int height
Definition: TypeCase.h:145
Describes a font used for the UI.
Definition: UiFont.h:44
Character-by-character text renderer.
Definition: TypeCase.h:69
const UiFont font
Definition: TypeCase.h:143
TypeLine()
Definition: TypeCase.h:159
void Render(const Color color, int x, int y, boost::optional< size_t > caret={})
Render this line using the owning TypeCase.
Definition: TypeCase.h:177
unsigned int page
Index of the texture that contains the character.
Definition: TypeCase.h:61
std::vector< std::pair< GlyphEntry *, SDL_Rect > > glyphs
Definition: TypeCase.h:187
int GetWidth() const
Retrieve the width of each of the backing textures.
Definition: TypeCase.h:91
bool IsPrepared() const
Check if this instance has been prepared.
Definition: TypeCase.h:166
int advance
The width of the glyph when placed next to other glyphs.
Definition: TypeCase.h:60
SDL_Rect srcRect
The bounds of the glyph in the texture.
Definition: TypeCase.h:62
int GetHeight() const
Retrieve the height of each of the backing textures.
Definition: TypeCase.h:97
int width
Definition: TypeCase.h:188
virtual ~TypeCase()
Definition: TypeCase.h:82
A line of text, prepared by a TypeCase.
Definition: TypeCase.h:156
bool IsInitialized() const
Definition: TypeCase.h:56
int idx
Definition: SdlDisplay.cpp:254
int height
Definition: TypeCase.h:189
std::shared_ptr< TypeCase > typeCase
Definition: TypeCase.h:186
Definition: Announcement.h:24
uint32_t MR_UInt32
Definition: MR_Types.h:44
#define MR_DllDeclare
Definition: TypeCase.h:37
TypeCase(const UiFont &font, int width, int height)
Constructor.
Definition: TypeCase.h:78
A single glyph in the backing texture.
Definition: TypeCase.h:50
const int width
Definition: TypeCase.h:144