The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
video.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef VIDEO_HPP_INCLUDED
15 #define VIDEO_HPP_INCLUDED
16 
17 #include "events.hpp"
18 #include "exceptions.hpp"
20 
21 #include <boost/utility.hpp>
22 #include <boost/scoped_ptr.hpp>
23 
24 #include "sdl/window.hpp"
25 
26 struct surface;
27 #ifdef SDL_GPU
28 #include "sdl/shader.hpp"
29 #include "sdl/utils.hpp"
30 
31 namespace sdl
32 {
33 class timage;
34 }
35 #endif
36 
37 //possible flags when setting video modes
38 #define SDL_APPMOUSEFOCUS 0x01 /**< The app has mouse coverage */
39 #define SDL_APPINPUTFOCUS 0x02 /**< The app has input focus */
40 #define SDL_APPACTIVE 0x04 /**< The application is active */
41 
42 #ifdef SDL_GPU
43 struct GPU_Target;
44 GPU_Target *get_render_target();
45 #endif
46 
48 
49 
50 SDL_Rect screen_area();
51 
52 
53 
54 //which areas of the screen will be updated when the buffer is flipped?
55 void update_rect(size_t x, size_t y, size_t w, size_t h);
56 void update_rect(const SDL_Rect& rect);
57 
58 class CVideo : private boost::noncopyable {
59 public:
60  enum FAKE_TYPES {
62  , FAKE
64  };
65 
66  enum MODE_EVENT {
71  };
72 
74  ~CVideo();
75  static CVideo& get_singleton() { return *singleton_; }
76 
77  bool non_interactive();
78 
79  const static int DefaultBpp = 32;
80 
81 
82  /**
83  * Initializes a new window, taking into account any preiously saved states.
84  */
85  bool init_window();
86 
87  void setMode( int x, int y, const MODE_EVENT mode );
88 
89 
90  void set_fullscreen(bool ison);
91 
92  /**
93  * Set the resolution.
94  *
95  * @param width The new width.
96  * @param height The new height.
97  *
98  * @returns The status true if width and height are the
99  * size of the framebuffer, false otherwise.
100  */
101  void set_resolution(const std::pair<int,int>& res);
102  void set_resolution(const unsigned width, const unsigned height);
103 
104  std::pair<int,int> current_resolution();
105 
106  //did the mode change, since the last call to the modeChanged() method?
107  bool modeChanged();
108 
109  //functions to get the dimensions of the current video-mode
110  int getx() const;
111  int gety() const;
112 
113  //blits a surface with black as alpha
114  void blit_surface(int x, int y, surface surf, SDL_Rect* srcrect=nullptr, SDL_Rect* clip_rect=nullptr);
115 #ifdef SDL_GPU
116  GPU_Target *render_target() const;
117 
118  void draw_texture(sdl::timage &texture, int x, int y);
119  void set_texture_color_modulation(int r, int g, int b, int a);
120  void set_texture_submerge(float f);
121  void set_texture_effects(int effects);
122 
123  void blit_to_overlay(surface surf, int x, int y);
124  void clear_overlay_area(SDL_Rect area);
125  void clear_overlay();
126 #endif
127  void flip();
128  static void delay(unsigned int milliseconds);
129 
130  surface& getSurface();
131 
132  bool isFullScreen() const;
133 
134  struct error : public game::error
135  {
136  error() : game::error("Video initialization failed") {}
137  };
138 
139  class quit
140  : public tlua_jailbreak_exception
141  {
142  public:
143 
146  {
147  }
148 
149  private:
150 
152  };
153 
154  //functions to allow changing video modes when 16BPP is emulated
155 
156  void make_fake();
157  /**
158  * Creates a fake frame buffer for the unit tests.
159  *
160  * @param width The width of the buffer.
161  * @param height The height of the buffer.
162  * @param bpp The bpp of the buffer.
163  */
164  void make_test_fake(const unsigned width = 1024,
165  const unsigned height = 768, const unsigned bpp = DefaultBpp);
166  bool faked() const { return fake_screen_; }
167 
168  //functions to set and clear 'help strings'. A 'help string' is like a tooltip, but it appears
169  //at the bottom of the screen, so as to not be intrusive. Setting a help string sets what
170  //is currently displayed there.
171  int set_help_string(const std::string& str);
172  void clear_help_string(int handle);
173  void clear_all_help_strings();
174 
175  //function to stop the screen being redrawn. Anything that happens while
176  //the update is locked will be hidden from the user's view.
177  //note that this function is re-entrant, meaning that if lock_updates(true)
178  //is called twice, lock_updates(false) must be called twice to unlock
179  //updates.
180  void lock_updates(bool value);
181  bool update_locked() const;
182 
183  //this needs to be invoked immediately after a resize event or the game will crash.
184  void update_framebuffer();
185 
186  /**
187  * Wrapper for SDL_GetAppState.
188  */
189  Uint8 window_state();
190 
191  /**
192  * Sets the title of the main window.
193  *
194  * @param title The new title for the window.
195  */
196  void set_window_title(const std::string& title);
197 
198  /**
199  * Sets the icon of the main window.
200  *
201  * @param icon The new icon for the window.
202  */
203  void set_window_icon(surface& icon);
204 
206 
207  /**
208  * Returns the list of available screen resolutions.
209  */
210  std::vector<std::pair<int, int> > get_available_resolutions(const bool include_current = false);
211 
212 private:
214 
215  boost::scoped_ptr<sdl::twindow> window;
217  public:
218  virtual void handle_event(const SDL_Event &) {}
219 
220  virtual void handle_window_event(const SDL_Event &event);
221 
223  };
224 
225  void initSDL();
226 #ifdef SDL_GPU
227  void update_overlay(SDL_Rect *rect = nullptr);
228 
229  sdl::shader_program shader_;
230  surface overlay_;
231 #endif
232 
234 
235 
236  //if there is no display at all, but we 'fake' it for clients
238 
240 
241  //variables for help strings
243 
245 };
246 
247 //an object which will lock the display for the duration of its lifetime.
249 {
250  update_locker(CVideo& v, bool lock=true) : video(v), unlock(lock) {
251  if(lock) {
252  video.lock_updates(true);
253  }
254  }
255 
257  unlock_update();
258  }
259 
260  void unlock_update() {
261  if(unlock) {
262  video.lock_updates(false);
263  unlock = false;
264  }
265  }
266 
267 private:
269  bool unlock;
270 };
271 
272 namespace video2 {
274 protected:
275  draw_layering(const bool auto_join=true);
276  virtual ~draw_layering();
277 };
278 void trigger_full_redraw();
279 }
280 #endif
void set_window_icon(surface &icon)
Sets the icon of the main window.
Definition: video.cpp:556
draw_layering(const bool auto_join=true)
Definition: video.cpp:75
std::pair< int, int > current_resolution()
Definition: video.cpp:615
void set_resolution(const std::pair< int, int > &res)
Set the resolution.
Definition: video.cpp:696
FAKE_TYPES
Definition: video.hpp:60
#define IMPLEMENT_LUA_JAILBREAK_EXCEPTION(type)
Helper macro for classes deriving from tlua_jailbreak_exception.
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
int help_string_
Definition: video.hpp:242
Definition: video.hpp:58
void flip()
Definition: video.cpp:496
void lock_updates(bool value)
Definition: video.cpp:509
static CVideo * singleton_
Definition: video.hpp:213
std::vector< std::pair< int, int > > get_available_resolutions(const bool include_current=false)
Returns the list of available screen resolutions.
Definition: video.cpp:576
GLenum GLenum GLuint texture
Definition: glew.h:3453
MODE_EVENT
Definition: video.hpp:66
static CVideo & get_singleton()
Definition: video.hpp:75
GLboolean GLboolean g
Definition: glew.h:7319
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
void blit_surface(int x, int y, surface surf, SDL_Rect *srcrect=nullptr, SDL_Rect *clip_rect=nullptr)
Definition: video.cpp:290
sdl::twindow * get_window()
Definition: video.cpp:562
GLenum mode
Definition: glew.h:2390
~CVideo()
Definition: video.cpp:281
surface & getSurface()
Definition: dummy_video.cpp:22
virtual ~draw_layering()
Definition: video.cpp:81
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
void setMode(int x, int y, const MODE_EVENT mode)
Definition: video.cpp:431
void set_window_title(const std::string &title)
Sets the title of the main window.
Definition: video.cpp:550
surface & get_video_surface()
Definition: dummy_video.cpp:36
void make_fake()
Definition: video.cpp:355
boost::scoped_ptr< sdl::twindow > window
Definition: video.hpp:215
void initSDL()
Definition: video.cpp:241
The wrapper class for the SDL_Window class.
Definition: window.hpp:52
Uint8 window_state()
Wrapper for SDL_GetAppState.
Definition: video.cpp:522
int gety() const
Definition: video.cpp:481
CVideo & video
Definition: video.hpp:268
bool isFullScreen() const
Definition: video.cpp:620
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
const GLdouble * v
Definition: glew.h:1359
GLsizei const GLfloat * value
Definition: glew.h:1817
bool init_window()
Initializes a new window, taking into account any preiously saved states.
Definition: video.cpp:390
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
void unlock_update()
Definition: video.hpp:260
int updatesLocked_
Definition: video.hpp:244
surf
Definition: filter.cpp:143
bool non_interactive()
Definition: video.cpp:115
bool unlock
Definition: video.hpp:269
Definition: video.cpp:71
GLuint res
Definition: glew.h:9258
virtual void handle_window_event(const SDL_Event &event)
Definition: video.cpp:186
void update_rect(size_t x, size_t y, size_t w, size_t h)
Definition: video.cpp:140
Base class for exceptions that want to be thrown 'through' lua.
int getx() const
Definition: video.cpp:472
bool update_locked() const
Definition: video.cpp:517
static const int DefaultBpp
Definition: video.hpp:79
bool mode_changed_
Definition: video.hpp:233
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
SDL_Rect screen_area()
Definition: video.cpp:135
void set_fullscreen(bool ison)
Definition: video.cpp:670
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
CVideo(FAKE_TYPES type=NO_FAKE)
Definition: video.cpp:215
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
int set_help_string(const std::string &str)
Definition: video.cpp:625
~update_locker()
Definition: video.hpp:256
sdl_handler(const bool auto_join=true)
Definition: events.cpp:151
bool faked() const
Definition: video.hpp:166
cl_event event
Definition: glew.h:3070
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
void trigger_full_redraw()
Definition: video.cpp:88
video_event_handler event_handler_
Definition: video.hpp:239
void make_test_fake(const unsigned width=1024, const unsigned height=768, const unsigned bpp=DefaultBpp)
Creates a fake frame buffer for the unit tests.
Definition: video.cpp:362
void clear_all_help_strings()
Definition: video.cpp:664
static void delay(unsigned int milliseconds)
Definition: video.cpp:490
Contains a wrapper class for the SDL_Window class.
virtual void handle_event(const SDL_Event &)
Definition: video.hpp:218
bool fake_screen_
Definition: video.hpp:237
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
void update_framebuffer()
Definition: video.cpp:375
update_locker(CVideo &v, bool lock=true)
Definition: video.hpp:250
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void clear_help_string(int handle)
Definition: video.cpp:656
boost::shared_ptr< halo_record > handle
Definition: halo.hpp:34
bool modeChanged()
Definition: video.cpp:465
GLclampf f
Definition: glew.h:3024