The Battle for Wesnoth  1.15.2+dev
video.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "events.hpp"
18 #include "exceptions.hpp"
20 
21 #include <memory>
22 
23 class surface;
24 struct point;
25 
26 namespace sdl
27 {
28 class window;
29 }
30 
31 class CVideo
32 {
33 public:
34  CVideo(const CVideo&) = delete;
35  CVideo& operator=(const CVideo&) = delete;
36 
37  enum FAKE_TYPES { NO_FAKE, FAKE, FAKE_TEST };
38 
39  CVideo(FAKE_TYPES type = NO_FAKE);
40 
41  ~CVideo();
42 
44  {
45  return *singleton_;
46  }
47 
48  /***** ***** ***** ***** Unit test-related functions ***** ***** ****** *****/
49 
50  void make_fake();
51 
52  /**
53  * Creates a fake frame buffer for the unit tests.
54  *
55  * @param width The width of the buffer.
56  * @param height The height of the buffer.
57  */
58  void make_test_fake(const unsigned width = 1024, const unsigned height = 768);
59 
60  bool faked() const
61  {
62  return fake_screen_;
63  }
64 
65  bool non_interactive() const;
66 
67  /***** ***** ***** ***** Window-related functions ***** ***** ****** *****/
68 
69  /** Initializes a new SDL window instance, taking into account any preiously saved states. */
70  void init_window();
71 
72  /** Returns a pointer to the underlying SDL window. */
73  sdl::window* get_window();
74 
75 private:
76  enum MODE_EVENT { TO_RES, TO_FULLSCREEN, TO_WINDOWED, TO_MAXIMIZED_WINDOW };
77 
78  /**
79  * Sets the window's mode - ie, changing it to fullscreen, maximizing, etc.
80  *
81  * @param mode The action to perform.
82  * @param size The new window size. Utilized if @a mode is TO_RES.
83  */
84  void set_window_mode(const MODE_EVENT mode, const point& size);
85 
86 public:
87  void set_fullscreen(bool ison);
88 
89  void toggle_fullscreen();
90 
91  bool is_fullscreen() const;
92 
93  bool set_resolution(const unsigned width, const unsigned height);
94 
95  /**
96  * Set the window resolution.
97  *
98  * @param resolution The new width and height.
99  *
100  * @returns Whether the resolution was successfully changed.
101  */
102  bool set_resolution(const point& resolution);
103 
104  point current_resolution();
105 
106  /** Returns the list of available screen resolutions. */
107  std::vector<point> get_available_resolutions(const bool include_current = false);
108 
109  /**
110  * Returns the current window renderer area, either in pixels or screen coordinates.
111  *
112  * @param as_pixels Whether to return the area in pixels (default true) or
113  * DPI-independent (DIP) screen coordinates.
114  */
115  SDL_Rect screen_area(bool as_pixels = true) const;
116 
117  /** Returns the window renderer width in pixels or screen coordinates. */
118  int get_width(bool as_pixels = true) const;
119 
120  /** Returns the window renderer height in pixels or in screen coordinates. */
121  int get_height(bool as_pixels = true) const;
122 
123  /** The current scale factor on High-DPI screens. */
124  std::pair<float, float> get_dpi_scale_factor() const;
125 
126  /**
127  * Tests whether the given flags are currently set on the SDL window.
128  *
129  * @param flags The flags to test, OR'd together.
130  */
131  bool window_has_flags(uint32_t flags) const;
132 
133  /**
134  * Sets the title of the main window.
135  *
136  * @param title The new title for the window.
137  */
138  void set_window_title(const std::string& title);
139 
140  /**
141  * Sets the icon of the main window.
142  *
143  * @param icon The new icon for the window.
144  */
145  void set_window_icon(surface& icon);
146 
148  {
149  return refresh_rate_;
150  }
151 
152  /***** ***** ***** ***** Drawing functions ***** ***** ****** *****/
153 
154  /**
155  * Draws a surface directly onto the screen framebuffer.
156  *
157  * @param x The x coordinate at which to draw.
158  * @param y The y coordinate at which to draw.
159  * @param surf The surface to draw.
160  * @param srcrect The area of the surface to draw. This defaults to nullptr,
161  * which implies the entire thing.
162  * @param clip_rect The clippin rect. If not null, the surface will only be drawn
163  * within the bounds of the given rectangle.
164  */
165  void blit_surface(int x, int y, surface surf, SDL_Rect* srcrect = nullptr, SDL_Rect* clip_rect = nullptr);
166 
167  /** Renders the screen. Should normally not be called directly! */
168  void flip();
169 
170  /**
171  * Updates and ensures the framebuffer surface is valid.
172  * This needs to be invoked immediately after a resize event or the game will crash.
173  */
174  void update_framebuffer();
175 
176  /** Clear the screen contents */
177  void clear_screen();
178 
179  /** Returns a reference to the framebuffer. */
180  surface& getSurface();
181 
182  /**
183  * Stop the screen being redrawn. Anything that happens while the updates are locked will
184  * be hidden from the user's view.
185  *
186  * Note that this function is re-entrant, meaning that if lock_updates(true) is called twice,
187  * lock_updates(false) must be called twice to unlock updates.
188  */
189  void lock_updates(bool value);
190 
191  /** Whether the screen has been 'locked' or not. */
192  bool update_locked() const;
193 
194  void lock_flips(bool);
195 
196  /***** ***** ***** ***** Help string functions ***** ***** ****** *****/
197 
198  /**
199  * Displays a help string with the given text. A 'help string' is like a tooltip,
200  * but appears at the bottom of the screen so as to not be intrusive.
201  *
202  * @param str The text to display.
203  *
204  * @returns The handle id of the new help string.
205  */
206  int set_help_string(const std::string& str);
207 
208  /** Removes the help string with the given handle. */
209  void clear_help_string(int handle);
210 
211  /** Removes all help strings. */
212  void clear_all_help_strings();
213 
214  /***** ***** ***** ***** General utils ***** ***** ****** *****/
215 
216  /** Waits a given number of milliseconds before returning. */
217  static void delay(unsigned int milliseconds);
218 
219  struct error : public game::error
220  {
222  : game::error("Video initialization failed")
223  {
224  }
225  };
226 
227  /** Type that can be thrown as an exception to quit to desktop. */
229  {
230  public:
233  {
234  }
235 
236  private:
238  };
239 
240 private:
242 
243  /** The SDL window object. */
244  std::unique_ptr<sdl::window> window;
245 
246  /** Initializes the SDL video subsystem. */
247  void initSDL();
248 
249  // if there is no display at all, but we 'fake' it for clients
251 
252  /** Helper class to manage SDL events. */
254  {
255  public:
256  virtual void handle_event(const SDL_Event&)
257  {
258  }
259 
260  virtual void handle_window_event(const SDL_Event& event);
261 
263  : sdl_handler(false)
264  {
265  }
266  };
267 
269 
270  /** Curent ID of the help string. */
272 
276 };
277 
278 /** An object which will lock the display for the duration of its lifetime. */
280 {
281  update_locker(CVideo& v, bool lock = true)
282  : video(v)
283  , unlock(lock)
284  {
285  if(lock) {
286  video.lock_updates(true);
287  }
288  }
289 
291  {
292  unlock_update();
293  }
294 
296  {
297  if(unlock) {
298  video.lock_updates(false);
299  unlock = false;
300  }
301  }
302 
303 private:
305  bool unlock;
306 };
307 
309 {
310 public:
312  : video_(video)
313  {
314  video_.lock_flips(true);
315  }
316 
318  {
319  video_.lock_flips(false);
320  }
321 
322 private:
324 };
325 
326 namespace video2
327 {
329 {
330 protected:
331  draw_layering(const bool auto_join = true);
332  virtual ~draw_layering();
333 };
334 
335 void trigger_full_redraw();
336 }
FAKE_TYPES
Definition: video.hpp:37
#define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type)
Helper macro for classes deriving from lua_jailbreak_exception.
int help_string_
Curent ID of the help string.
Definition: video.hpp:271
Definition: video.hpp:31
static CVideo * singleton_
Definition: video.hpp:241
Type that can be thrown as an exception to quit to desktop.
Definition: video.hpp:228
MODE_EVENT
Definition: video.hpp:76
int refresh_rate_
Definition: video.hpp:275
static CVideo & get_singleton()
Definition: video.hpp:43
int flip_locked_
Definition: video.hpp:274
int updated_locked_
Definition: video.hpp:273
void blit_surface(const surface &surf, const SDL_Rect *srcrect, surface &dst, const SDL_Rect *dstrect)
Replacement for sdl_blit.
Definition: utils.cpp:2009
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
The wrapper class for the SDL_Window class.
Definition: window.hpp:44
CVideo & video
Definition: video.hpp:304
void unlock_update()
Definition: video.hpp:295
An object which will lock the display for the duration of its lifetime.
Definition: video.hpp:279
bool unlock
Definition: video.hpp:305
flip_locker(CVideo &video)
Definition: video.hpp:311
bool faked() const
Definition: video.hpp:60
Definition: video.cpp:45
int current_refresh_rate() const
Definition: video.hpp:147
CVideo & video_
Definition: video.hpp:323
Holds a 2D point.
Definition: point.hpp:23
~flip_locker()
Definition: video.hpp:317
~update_locker()
Definition: video.hpp:290
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
void trigger_full_redraw()
Definition: video.cpp:62
video_event_handler event_handler_
Definition: video.hpp:268
virtual void handle_event(const SDL_Event &)
Definition: video.hpp:256
bool fake_screen_
Definition: video.hpp:250
point resolution()
Definition: general.cpp:373
update_locker(CVideo &v, bool lock=true)
Definition: video.hpp:281
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
Base class for exceptions that want to be thrown &#39;through&#39; lua.
std::unique_ptr< sdl::window > window
The SDL window object.
Definition: video.hpp:244
Helper class to manage SDL events.
Definition: video.hpp:253