Adonthell  0.4
screen.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 1999/2000/2001/2004 Alexandre Courbot
3  Copyright (C) 2016 Kai Sterker
4  Part of the Adonthell Project <http://adonthell.nongnu.org>
5 
6  Adonthell is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  Adonthell is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /**
21  * @file screen.h
22  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
23  * @author Kai Sterker
24  *
25  * @brief Declares the screen class.
26  *
27  *
28  */
29 
30 #ifndef SCREEN_H_
31 #define SCREEN_H_
32 
33 #include "surface.h"
34 #include "prefs.h"
35 #include <string>
36 
37 
38 #ifndef SWIG
39 using namespace std;
40 #endif
41 
42 
43 /** Screen access is made through this class.
44  * This static class sets video modes, flush the frame buffer to the physical
45  * screen and make abstraction of the real screen depth to ease the graphic
46  * programmer's task.
47  */
48 class screen
49 {
50 public:
51 
52  static void cleanup();
53 
54  static SDL_Renderer *get_renderer()
55  {
56  return Renderer;
57  }
58 
59  static u_int32 format()
60  {
61  return Window != NULL ? SDL_GetWindowPixelFormat(Window) : SDL_PIXELFORMAT_UNKNOWN;
62  }
63 
64  /**
65  * The actual screen surface.
66  * It is publicly available so you can do fast operations on the screen.
67  * Manipulate it just as a classic surface.
68  *
69  */
70  static surface display;
71 
72  /** Initializes the video subsystem and creates the required resources.
73  * @param nl X screen resolution.
74  * @param nh Y screen resolution.
75  * @param depth desired screen depth.
76  * @param myconfig configuration data.
77  * @return true on success, false otherwise.
78  */
79  static bool init (u_int16 nl, u_int16 nh, u_int8 depth, const config & myconfig);
80 
81  /** Returns the length of the screen.
82  * @return length of the screen.
83  */
84  static u_int16 length ()
85  {
86  return display.length ();
87  }
88 
89  /** Returns the height of the screen.
90  * @return height of the screen.
91  */
92  static u_int16 height ()
93  {
94  return display.height ();
95  }
96 
97  /** Returns the screen depth, in bytes per pixel.
98  * @return screen depth, in bytes per pixel.
99  */
101  {
102  return bytes_per_pixel_;
103  }
104 
105  /** Returns the translucent color in %screen's depth format.
106  * For manipulation on images that will only be displayed, this is
107  * the right function to call for getting the translucent color.
108  * @return the translucent color in %screen's depth format.
109  */
110  static u_int32 trans_col ()
111  {
112  return trans;
113  }
114 
115  /**
116  * Scale factor of the screen.
117  */
118  static u_int8 scale ()
119  {
120  return scale_;
121  }
122 
123  /**
124  * X offset of the viewport
125  */
126  static u_int16 offset_x()
127  {
128  return clip_rect_.x;
129  }
130 
131  /**
132  * Y offset of the viewport
133  */
134  static u_int16 offset_y()
135  {
136  return clip_rect_.y;
137  }
138 
139  /**
140  * Totally clears the screen with black.
141  *
142  */
143  static void clear ()
144  {
145  SDL_SetRenderDrawColor(Renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
146  if (clip_rect_.x || clip_rect_.y)
147  {
148  // SDL renderers do not treat clip rect identical when clearing screen
149  SDL_RenderSetClipRect(Renderer, NULL);
150  SDL_RenderClear(Renderer);
151  SDL_RenderSetClipRect(Renderer, &clip_rect_);
152  }
153  else
154  {
155  SDL_RenderClear(Renderer);
156  }
157  }
158 
159  /** Ensure the framebuffer is copied to the physical screen.
160  */
161  static void show ()
162  {
163  SDL_RenderPresent(Renderer);
164  }
165 
166  /** Returns whether the current mode is fullscreen or windowed.
167  * @return
168  * - 0: windowed mode.
169  * - 1: letterbox mode
170  * - 2: fullscreen mode
171  */
172  static u_int8 mode ()
173  {
174  return mode_;
175  }
176 
177  /** Sets fullscreen/windowed mode.
178  * @param mode
179  * - 0: windowed mode.
180  * - 1: letterbox mode
181  * - 2: fullscreen mode
182  * @return
183  * @li true if the operation succeed.
184  * @li false if the mode is already set, or the system doesn't support
185  * this mode.
186  */
187  static bool set_fullscreen (const u_int8 & m);
188 
189  /**
190  * Returns information about the current screen settings,
191  * suitable for being displayed to the user.
192  *
193  *
194  * @return printable information about the current screen settings.
195  */
196  static string info ();
197 
198  /**
199  * Make a nice transition effect.
200  *
201  * @param i advancement of the transition (finished when i == screen::length () / 2)
202  */
203  static void transition (u_int16 i);
204 
205 private:
206 
207  static u_int8 get_scale_for_display(u_int8 screen, u_int16 nl, u_int16 nh);
208  static void update_scale();
209 
210  /// Bytes per pixel.
211  static u_int8 bytes_per_pixel_;
212 
213  /// Transparent color.
214  static u_int32 trans;
215 
216  /// Whether fullscreen is on or not.
217  static u_int8 mode_;
218 
219  /// The output window
220  static SDL_Window *Window;
221 
222  /// the render target
223  static SDL_Renderer *Renderer;
224 
225  /// scale factor of the screen
226  static u_int8 scale_;
227 
228  /// clipping rectangle for letterbox mode
229  static SDL_Rect clip_rect_;
230 };
231 
232 
233 #endif
surface.h
Declares the surface class.
surface
Class where drawables can actually be drawn to.
Definition: surface.h:85
drawable::height
u_int16 height() const
Returns the height of the drawable.
Definition: drawable.h:91
screen::mode
static u_int8 mode()
Returns whether the current mode is fullscreen or windowed.
Definition: screen.h:172
screen::clear
static void clear()
Totally clears the screen with black.
Definition: screen.h:143
screen::length
static u_int16 length()
Returns the length of the screen.
Definition: screen.h:84
u_int32
#define u_int32
32 bits long unsigned integer
Definition: types.h:41
prefs.h
Adonthell's configuration.
u_int8
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
drawable::length
u_int16 length() const
Returns the length of the drawable.
Definition: drawable.h:80
screen::scale
static u_int8 scale()
Scale factor of the screen.
Definition: screen.h:118
screen::show
static void show()
Ensure the framebuffer is copied to the physical screen.
Definition: screen.h:161
screen::display
static surface display
The actual screen surface.
Definition: screen.h:70
screen::trans_col
static u_int32 trans_col()
Returns the translucent color in screen's depth format.
Definition: screen.h:110
screen::offset_y
static u_int16 offset_y()
Y offset of the viewport.
Definition: screen.h:134
config
This class contains the engine's configuration read either from the config file or from the command l...
Definition: prefs.h:74
screen::offset_x
static u_int16 offset_x()
X offset of the viewport.
Definition: screen.h:126
u_int16
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
screen::bytes_per_pixel
static u_int8 bytes_per_pixel()
Returns the screen depth, in bytes per pixel.
Definition: screen.h:100
screen
Screen access is made through this class.
Definition: screen.h:48
screen::height
static u_int16 height()
Returns the height of the screen.
Definition: screen.h:92