Debian Codegraph @

engine/video/video.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name video.cpp - The universal video functions. */
11 //
12 // (c) Copyright 1999-2008 by Lutz Sammer, Nehal Mistry, and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /**
32 ** @page VideoModule Module - Video
33 **
34 ** There are lots of video functions available, therefore this
35 ** page tries to summarize these separately.
36 **
37 ** @note care must be taken what to use, how to use it and where
38 ** put new source-code. So please read the following sections
39 ** first.
40 **
41 **
42 ** @section VideoMain Video main initialization
43 **
44 ** The general setup of platform dependent video and basic video
45 ** functionalities is done with function @see InitVideo
46 **
47 ** We support (depending on the platform) resolutions:
48 ** 640x480, 800x600, 1024x768, 1600x1200
49 ** with colors 8,15,16,24,32 bit
50 **
51 ** @see video.h @see video.cpp
52 **
53 **
54 ** @section VideoModuleHigh High Level - video dependent functions
55 **
56 ** These are the video platforms that are supported, any platform
57 ** dependent settings/functionailty are located within each
58 ** separate files:
59 **
60 ** SDL : Simple Direct Media for Linux,
61 ** Win32 (Windows 95/98/2000), BeOs, MacOS
62 ** (visit http://www.libsdl.org)
63 **
64 ** @see sdl.cpp
65 **
66 **
67 ** @section VideoModuleLow Low Level - draw functions
68 **
69 ** All direct drawing functions
70 **
71 ** @note you might need to use Decorations (see above), to prevent
72 ** drawing directly to screen in conflict with the video update.
73 **
74 ** @see linedraw.cpp
75 ** @see sprite.cpp
76 */
77
78 /*----------------------------------------------------------------------------
79 -- Includes
80 ----------------------------------------------------------------------------*/
81
82 #include <stdio.h>
83 #include <string.h>
84 #include <stdlib.h>
85
86 #include "stratagus.h"
87
88 #include <vector>
89
90 #include "video.h"
91 #include "font.h"
92 #include "ui.h"
93 #include "cursor.h"
94 #include "iolib.h"
95
96 #include "intern_video.h"
97
98 #include "SDL.h"
99
100 /*----------------------------------------------------------------------------
101 -- Declarations
102 ----------------------------------------------------------------------------*/
103
104 /**
105 ** Structure of pushed clippings.
106 */
107 struct Clip {
108 int X1; /// pushed clipping top left
109 int Y1; /// pushed clipping top left
110 int X2; /// pushed clipping bottom right
111 int Y2; /// pushed clipping bottom right
112 };
113
114 /*----------------------------------------------------------------------------
115 -- Externals
116 ----------------------------------------------------------------------------*/
117
118 extern void InitVideoSdl(void); /// Init SDL video hardware driver
119
120 extern void SdlLockScreen(void); /// Do SDL hardware lock
121 extern void SdlUnlockScreen(void); /// Do SDL hardware unlock
122
123 /*----------------------------------------------------------------------------
124 -- Variables
125 ----------------------------------------------------------------------------*/
126
127 CVideo Video;
128
129 bool UseOpenGL; /// Use OpenGL
130
131 char VideoForceFullScreen; /// fullscreen set from commandline
132
133 unsigned long NextFrameTicks; /// Ticks of begin of the next frame
134 unsigned long FrameCounter; /// Current frame number
135 int SlowFrameCounter; /// Profile, frames out of sync
136
137 int ClipX1; /// current clipping top left
138 int ClipY1; /// current clipping top left
139 int ClipX2; /// current clipping bottom right
140 int ClipY2; /// current clipping bottom right
141
142 static std::vector<Clip> Clips;
143
144 int VideoSyncSpeed = -100; /// Default value will be set by lua. 0 disable interrupts
145 int SkipFrames; /// Skip this frames
146
147 Uint32 ColorBlack;
148 Uint32 ColorDarkGreen;
149 Uint32 ColorDarkBlue;
150 Uint32 ColorBlue;
151 Uint32 ColorCyan;
152 Uint32 ColorOrange;
153 Uint32 ColorWhite;
154 Uint32 ColorGray;
155 Uint32 ColorRed;
156 Uint32 ColorGreen;
157 Uint32 ColorYellow;
158
159
160 /*----------------------------------------------------------------------------
161 -- Functions
162 ----------------------------------------------------------------------------*/
163
164 /**
165 ** Set clipping for graphic routines.
166 **
167 ** @param left Left X screen coordinate.
168 ** @param top Top Y screen coordinate.
169 ** @param right Right X screen coordinate.
170 ** @param bottom Bottom Y screen coordinate.
171 */
172 void SetClipping(int left, int top, int right, int bottom)
173 {
174 Assert(left <= right && top <= bottom && left >= 0 && left < Video.Width &&
175 top >= 0 && top < Video.Height && right >= 0 &&
176 right < Video.Width && bottom >= 0 && bottom < Video.Height);
177
178 ClipX1 = left;
179 ClipY1 = top;
180 ClipX2 = right;
181 ClipY2 = bottom;
182 }
183
184 /**
185 ** Push current clipping.
186 */
187 void PushClipping(void)
188 {
189 Clip clip = {ClipX1, ClipY1, ClipX2, ClipY2};
190 Clips.push_back(clip);
191 }
192
193 /**
194 ** Pop current clipping.
195 */
196 void PopClipping(void)
197 {
198 Clip clip = Clips.back();
199 ClipX1 = clip.X1;
200 ClipY1 = clip.Y1;
201 ClipX2 = clip.X2;
202 ClipY2 = clip.Y2;
203 Clips.pop_back();
204 }
205
206 /*----------------------------------------------------------------------------
207 -- Functions
208 ----------------------------------------------------------------------------*/
209
210 /**
211 ** Lock the screen for write access.
212 */
213 void CVideo::LockScreen(void)
214 {
215 SdlLockScreen();
216 }
217
218 /**
219 ** Unlock the screen for write access.
220 */
221 void CVideo::UnlockScreen(void)
222 {
223 SdlUnlockScreen();
224 }
225
226 /**
227 ** Clear the video screen.
228 */
229 void CVideo::ClearScreen(void)
230 {
231 FillRectangle(ColorBlack, 0, 0, Video.Width, Video.Height);
232 }
233
234 /**
235 ** Resize the video screen.
236 **
237 ** @return True if the resolution changed, false otherwise
238 */
239 bool CVideo::ResizeScreen(int w, int h)
240 {
241 if (VideoValidResolution(w, h)) {
242 if (UseOpenGL) {
243 FreeOpenGLGraphics();
244 FreeOpenGLFonts();
245 UI.Minimap.FreeOpenGL();
246 }
247 Width = w;
248 Height = h;
249 TheScreen = SDL_SetVideoMode(w, h, TheScreen->format->BitsPerPixel,
250 TheScreen->flags);
251 SetClipping(0, 0, Video.Width - 1, Video.Height - 1);
252 if (UseOpenGL) {
253 ReloadOpenGL();
254 }
255 return true;
256 }
257 return false;
258 }
259
260 /**
261 ** Return ticks in ms since start.
262 */
263 unsigned long GetTicks(void)
264 {
265 return SDL_GetTicks();
266 }
267
268 /**
269 ** Video initialize.
270 */
271 void InitVideo(void)
272 {
273 InitVideoSdl();
274 InitLineDraw();
275 }
276
277 //@}