HoverRace  2.0
SdlDisplay.h
Go to the documentation of this file.
1 
2 // SdlDisplay.h
3 //
4 // Copyright (c) 2013-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 #include <SDL2/SDL_ttf.h>
26 
27 #include "../Display.h"
28 #include "../Res.h"
29 #include "../UiViewModel.h"
30 
31 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
32 # ifdef MR_ENGINE
33 # define MR_DllDeclare __declspec( dllexport )
34 # else
35 # define MR_DllDeclare __declspec( dllimport )
36 # endif
37 #else
38 # define MR_DllDeclare
39 #endif
40 
41 namespace HoverRace {
42  namespace Display {
43  namespace SDL {
44  class SdlTexture;
45  }
46  class Label;
47  class Texture;
48  struct UiFont;
49  }
50 }
51 
52 namespace HoverRace {
53 namespace Display {
54 namespace SDL {
55 
61 {
62  using SUPER = Display;
63 
64 public:
65  SdlDisplay(const std::string &windowTitle="");
66  virtual ~SdlDisplay();
67 
68 public:
69  // ViewAttacher
70  void AttachView(ActiveText &model) override;
71  void AttachView(BaseContainer &model) override;
72  void AttachView(Button &model) override;
73  void AttachView(ClickRegion &model) override;
74  void AttachView(FillBox &model) override;
75  void AttachView(KeycapIcon &model) override;
76  void AttachView(Label &model) override;
77  void AttachView(Picture &model) override;
78  void AttachView(RuleLine &model) override;
79  void AttachView(ScreenFade &model) override;
80  void AttachView(Slider &model) override;
81  void AttachView(SymbolIcon &model) override;
82  void AttachView(Wallpaper &model) override;
83 
84 protected:
85  std::shared_ptr<TypeCase> MakeTypeCase(const UiFont &font) override;
86 public:
87  std::shared_ptr<SdlTexture> LoadRes(std::shared_ptr<Res<Texture>> res);
88 
89 public:
90  // Display
91  VideoServices::VideoBuffer &GetLegacyDisplay() const override { return *legacyDisplay; }
92  void OnDesktopModeChanged(int width, int height) override;
93  void OnDisplayConfigChanged() override;
94  void Flip() override;
95  void Screenshot() override;
96 
97 private:
98  void ApplyVideoMode();
99 
100 public:
101  int GetScreenWidth() const { return width; }
102  int GetScreenHeight() const { return height; }
103 
104  SDL_Window *GetWindow() const { return window; }
105  SDL_Renderer *GetRenderer() const { return renderer; }
106 
107 public:
108  // Text-renderer-specific utilities.
109  TTF_Font *LoadTtfFont(const UiFont &font, bool uiScale = true);
110 
111 public:
112  // SDL-specific utilities.
113  void DrawUiTexture(SDL_Texture *texture, const Vec2 &relPos,
114  uiLayoutFlags_t layoutFlags = 0);
115  void DrawUiTexture(SDL_Texture *texture, const Vec2 &relPos,
116  const Vec2 &scale, uiLayoutFlags_t layoutFlags = 0);
117 
118 private:
119  std::string windowTitle;
120  SDL_Window *window;
121  SDL_Renderer *renderer;
122  int width, height;
124 
125  using loadedFontKey = std::pair<std::string, int>;
126  using loadedFonts_t = std::map<loadedFontKey, TTF_Font*>;
128 };
129 
130 } // namespace SDL
131 } // namespace Display
132 } // namespace HoverRace
133 
134 #undef MR_DllDeclare
Draws a single texture.
Definition: Picture.h:52
A horizontal or vertical line.
Definition: RuleLine.h:54
Paints a texture across the entire screen.
Definition: Wallpaper.h:53
Describes a font used for the UI.
Definition: UiFont.h:44
Base class for loadable textures.
Definition: Res.h:94
loadedFonts_t loadedFonts
Definition: SdlDisplay.h:127
Base class for widgets that contain other widgets.
Definition: BaseContainer.h:63
A static text label.
Definition: Label.h:51
Definition: Vec.h:38
MR_UInt32 uiLayoutFlags_t
Definition: UiLayoutFlags.h:53
int GetScreenHeight() const
Definition: SdlDisplay.h:102
#define MR_DllDeclare
Definition: SdlDisplay.h:38
A slider for selecting values from a range.
Definition: Slider.h:52
VideoServices::VideoBuffer * legacyDisplay
Definition: SdlDisplay.h:123
Paints a solid color over the whole screen with adjustable opacity.
Definition: ScreenFade.h:51
Base class for display managers.
Definition: Display.h:73
Icon for a keycap (keyboard key or gamepad button(.
Definition: KeycapIcon.h:43
std::pair< std::string, int > loadedFontKey
Definition: SdlDisplay.h:125
Video framebuffer for legacy (8-bit, palettized) rendering.
Definition: VideoBuffer.h:54
A standard button, with a text label.
Definition: Button.h:56
VideoServices::VideoBuffer & GetLegacyDisplay() const override
Retrieve the legacy (8-bit) framebuffer.
Definition: SdlDisplay.h:91
std::map< loadedFontKey, TTF_Font * > loadedFonts_t
Definition: SdlDisplay.h:126
int width
Definition: SdlDisplay.h:122
A colored rectangle, that&#39;s all.
Definition: FillBox.h:51
A text widget where the contents may change often (i.e., updated every few frames, or even every frame).
Definition: ActiveText.h:49
Base class for clickable areas.
Definition: ClickRegion.h:49
SDL_Window * window
Definition: SdlDisplay.h:120
SDL_Renderer * renderer
Definition: SdlDisplay.h:121
One of the predefined icons from the symbol font.
Definition: SymbolIcon.h:49
Definition: Announcement.h:24
std::string windowTitle
Definition: SdlDisplay.h:119
SDL_Renderer * GetRenderer() const
Definition: SdlDisplay.h:105
int GetScreenWidth() const
Definition: SdlDisplay.h:101
SDL-based software rendering.
Definition: SdlDisplay.h:60
SDL_Window * GetWindow() const
Definition: SdlDisplay.h:104