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