Debian Codegraph @

engine/include/video.h

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.h - The video headerfile. */
11 //
12 // (c) Copyright 1999-2007 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 #ifndef __VIDEO_H__
29 #define __VIDEO_H__
30
31 //@{
32
33 #include "SDL.h"
34 #define __gl_glext_h_
35 #include "SDL_opengl.h"
36 #include "guichan/image.h"
37
38 class CFontFamily;
39
40 extern bool UseOpenGL;
41
42
43 class CGraphic : public gcn::Image {
44 protected:
45 CGraphic() : Surface(NULL), SurfaceFlip(NULL),
46 Width(0), Height(0), NumFrames(1), GraphicWidth(0), GraphicHeight(0),
47 Refs(1), Resized(false),
48 TextureWidth(0.f), TextureHeight(0.f), Textures(NULL), NumTextures(0)
49 {
50 }
51 ~CGraphic() {}
52
53 public:
54 // Draw
55 void DrawClip(int x, int y) const;
56 void DrawSub(int gx, int gy, int w, int h, int x, int y) const;
57 void DrawSubClip(int gx, int gy, int w, int h, int x, int y) const;
58 void DrawSubTrans(int gx, int gy, int w, int h, int x, int y,
59 unsigned char alpha) const;
60 void DrawSubClipTrans(int gx, int gy, int w, int h, int x, int y,
61 unsigned char alpha) const;
62
63 // Draw frame
64 void DrawFrame(unsigned frame, int x, int y) const;
65 void DoDrawFrameClip(GLuint *textures, unsigned frame, int x, int y) const;
66 void DrawFrameClip(unsigned frame, int x, int y) const;
67 void DrawFrameTrans(unsigned frame, int x, int y, int alpha) const;
68 void DrawFrameClipTrans(unsigned frame, int x, int y, int alpha) const;
69
70 // Draw frame flipped horizontally
71 void DrawFrameX(unsigned frame, int x, int y) const;
72 void DoDrawFrameClipX(GLuint *textures, unsigned frame, int x, int y) const;
73 void DrawFrameClipX(unsigned frame, int x, int y) const;
74 void DrawFrameTransX(unsigned frame, int x, int y, int alpha) const;
75 void DrawFrameClipTransX(unsigned frame, int x, int y, int alpha) const;
76
77
78 static CGraphic *New(const std::string &file, int w = 0, int h = 0);
79 static CGraphic *ForceNew(const std::string &file, int w = 0, int h = 0);
80
81 CGraphic *Clone() const;
82
83 static void Free(CGraphic *g);
84
85 bool LoadGraphicSize(int *w, int *h);
86 void Load();
87 void Flip();
88 void UseDisplayFormat();
89 void Resize(int w, int h);
90 bool TransparentPixel(int x, int y);
91 void MakeShadow();
92
93 inline bool IsLoaded() const { return Surface != NULL; }
94
95 //guichan
96 virtual void *_getData() const { return Surface; }
97 virtual int getWidth() const { return Width; }
98 virtual int getHeight() const { return Height; }
99
100 std::string File; /// Filename
101 std::string HashFile; /// Filename used in hash
102 SDL_Surface *Surface; /// Surface
103 SDL_Surface *SurfaceFlip; /// Flipped surface
104 int Width; /// Width of a frame
105 int Height; /// Height of a frame
106 int NumFrames; /// Number of frames
107 int GraphicWidth; /// Original graphic width
108 int GraphicHeight; /// Original graphic height
109 int Refs; /// Uses of this graphic
110 bool Resized; /// Image has been resized
111 GLfloat TextureWidth; /// Width of the texture
112 GLfloat TextureHeight; /// Height of the texture
113 GLuint *Textures; /// Texture names
114 int NumTextures; /// Number of textures
115
116 friend class CFontFamily;
117 };
118
119 class CPlayerColorGraphic : public CGraphic
120 {
121 protected:
122 CPlayerColorGraphic() {
123 memset(PlayerColorTextures, 0, sizeof(PlayerColorTextures));
124 }
125
126 public:
127 void DrawPlayerColorFrameClipX(int player, unsigned frame, int x, int y);
128 void DrawPlayerColorFrameClip(int player, unsigned frame, int x, int y);
129
130 static CPlayerColorGraphic *New(const std::string &file, int w = 0, int h = 0);
131 static CPlayerColorGraphic *ForceNew(const std::string &file, int w = 0, int h = 0);
132
133 GLuint *PlayerColorTextures[PlayerMax];/// Textures with player colors
134 };
135
136 /// A platform independent color
137 class CColor {
138 public:
139 CColor(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0,
140 unsigned char a = 0) : R(r), G(g), B(b), A(a) {}
141
142 /// Cast to a SDL_Color
143 operator SDL_Color() const {
144 SDL_Color c = { R, G, B, A };
145 return c;
146 };
147
148 unsigned char R; /// Red
149 unsigned char G; /// Green
150 unsigned char B; /// Blue
151 unsigned char A; /// Alpha
152 };
153
154 class CUnitColors {
155 public:
156 CUnitColors() : Colors(NULL) {}
157
158 SDL_Color *Colors;
159 };
160
161 /**
162 ** Event call back.
163 **
164 ** This is placed in the video part, because it depends on the video
165 ** hardware driver.
166 */
167 typedef struct _event_callback_ {
168
169 /// Callback for mouse button press
170 void (*ButtonPressed)(unsigned buttons);
171 /// Callback for mouse button release
172 void (*ButtonReleased)(unsigned buttons);
173 /// Callback for mouse move
174 void (*MouseMoved)(int x, int y);
175 /// Callback for mouse exit of game window
176 void (*MouseExit)(void);
177
178 /// Callback for key press
179 void (*KeyPressed)(unsigned keycode, unsigned keychar);
180 /// Callback for key release
181 void (*KeyReleased)(unsigned keycode, unsigned keychar);
182 /// Callback for key repeated
183 void (*KeyRepeated)(unsigned keycode, unsigned keychar);
184
185 /// Callback for network event
186 void (*NetworkEvent)(void);
187
188 } EventCallback;
189
190 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
191 #define RSHIFT 0
192 #define GSHIFT 8
193 #define BSHIFT 16
194 #define ASHIFT 24
195 #define RMASK 0x000000ff
196 #define GMASK 0x0000ff00
197 #define BMASK 0x00ff0000
198 #define AMASK 0xff000000
199 #else
200 #define RSHIFT 24
201 #define GSHIFT 16
202 #define BSHIFT 8
203 #define ASHIFT 0
204 #define RMASK 0xff000000
205 #define GMASK 0x00ff0000
206 #define BMASK 0x0000ff00
207 #define AMASK 0x000000ff
208 #endif
209
210
211 class CVideo
212 {
213 public:
214 CVideo() : Width(0), Height(0), Depth(0), FullScreen(false) {}
215
216 void LockScreen();
217 void UnlockScreen();
218
219 void ClearScreen();
220 bool ResizeScreen(int x, int y);
221
222 void DrawPixelClip(Uint32 color, int x, int y);
223 void DrawTransPixelClip(Uint32 color, int x, int y, unsigned char alpha);
224
225 void DrawVLine(Uint32 color, int x, int y, int height);
226 void DrawTransVLine(Uint32 color, int x, int y, int height, unsigned char alpha);
227 void DrawVLineClip(Uint32 color, int x, int y, int height);
228 void DrawTransVLineClip(Uint32 color, int x, int y, int height, unsigned char alpha);
229
230 void DrawHLine(Uint32 color, int x, int y, int width);
231 void DrawTransHLine(Uint32 color, int x, int y, int width, unsigned char alpha);
232 void DrawHLineClip(Uint32 color, int x, int y, int width);
233 void DrawTransHLineClip(Uint32 color, int x, int y, int width, unsigned char alpha);
234
235 void DrawLine(Uint32 color, int sx, int sy, int dx, int dy);
236 void DrawTransLine(Uint32 color, int sx, int sy, int dx, int dy, unsigned char alpha);
237 void DrawLineClip(Uint32 color, int sx, int sy, int dx, int dy);
238 void DrawTransLineClip(Uint32 color, int sx, int sy, int dx, int dy, unsigned char alpha);
239
240 void DrawRectangle(Uint32 color, int x, int y, int w, int h);
241 void DrawTransRectangle(Uint32 color, int x, int y, int w, int h, unsigned char alpha);
242 void DrawRectangleClip(Uint32 color, int x, int y, int w, int h);
243 void DrawTransRectangleClip(Uint32 color, int x, int y, int w, int h, unsigned char alpha);
244
245 void FillRectangle(Uint32 color, int x, int y, int w, int h);
246 void FillTransRectangle(Uint32 color, int x, int y, int w, int h, unsigned char alpha);
247 void FillRectangleClip(Uint32 color, int x, int y, int w, int h);
248 void FillTransRectangleClip(Uint32 color, int x, int y, int w, int h, unsigned char alpha);
249
250 void DrawCircle(Uint32 color, int x, int y, int r);
251 void DrawTransCircle(Uint32 color, int x, int y, int r, unsigned char alpha);
252 void DrawCircleClip(Uint32 color, int x, int y, int r);
253 void DrawTransCircleClip(Uint32 color, int x, int y, int r, unsigned char alpha);
254
255 void FillCircle(Uint32 color, int x, int y, int radius);
256 void FillTransCircle(Uint32 color, int x, int y, int radius, unsigned char alpha);
257 void FillCircleClip(Uint32 color, int x, int y, int radius);
258 void FillTransCircleClip(Uint32 color, int x, int y, int radius, unsigned char alpha);
259
260 inline Uint32 MapRGB(SDL_PixelFormat *f, Uint8 r, Uint8 g, Uint8 b) {
261 if (!UseOpenGL) {
262 return SDL_MapRGB(f, r, g, b);
263 } else {
264 return MapRGBA(f, r, g, b, 0xFF);
265 }
266 }
267 inline Uint32 MapRGBA(SDL_PixelFormat *f, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
268 if (!UseOpenGL) {
269 return SDL_MapRGBA(f, r, g, b, a);
270 } else {
271 return ((r << RSHIFT) | (g << GSHIFT) | (b << BSHIFT) | (a << ASHIFT));
272 }
273 }
274 inline void GetRGB(Uint32 c, SDL_PixelFormat *f, Uint8 *r, Uint8 *g, Uint8 *b) {
275 if (!UseOpenGL) {
276 SDL_GetRGB(c, f, r, g, b);
277 } else {
278 *r = (c >> RSHIFT) & 0xff;
279 *g = (c >> GSHIFT) & 0xff;
280 *b = (c >> BSHIFT) & 0xff;
281 }
282 }
283 inline void GetRGBA(Uint32 c, SDL_PixelFormat *f, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a) {
284 if (!UseOpenGL) {
285 SDL_GetRGBA(c, f, r, g, b, a);
286 } else {
287 *r = (c >> RSHIFT) & 0xff;
288 *g = (c >> GSHIFT) & 0xff;
289 *b = (c >> BSHIFT) & 0xff;
290 *a = (c >> ASHIFT) & 0xff;
291 }
292 }
293
294 int Width;
295 int Height;
296 int Depth;
297 bool FullScreen;
298 };
299
300 extern CVideo Video;
301
302 /**
303 ** Video synchronization speed. Synchronization time in percent.
304 ** If =0, video framerate is not synchronized. 100 is exact
305 ** CYCLES_PER_SECOND (30). Game will try to redraw screen within
306 ** intervals of VideoSyncSpeed, not more, not less.
307 ** @see CYCLES_PER_SECOND
308 */
309 extern int VideoSyncSpeed;
310
311 extern int SkipFrames;
312
313 /// Fullscreen or windowed set from commandline.
314 extern char VideoForceFullScreen;
315
316 /// Next frame ticks
317 extern unsigned long NextFrameTicks;
318
319 /// Counts frames
320 extern unsigned long FrameCounter;
321
322 /// Counts quantity of slow frames
323 extern int SlowFrameCounter;
324
325 /// Initialize Pixels[] for all players.
326 /// (bring Players[] in sync with Pixels[])
327 extern void SetPlayersPalette(void);
328
329 /// The SDL screen
330 extern SDL_Surface *TheScreen;
331
332 /// Max texture size supported on the video card
333 extern GLint GLMaxTextureSize;
334 /// User-specified limit for ::GLMaxTextureSize
335 extern GLint GLMaxTextureSizeOverride;
336 /// Is OpenGL texture compression supported
337 extern bool GLTextureCompressionSupported;
338 /// Use OpenGL texture compression
339 extern bool UseGLTextureCompression;
340
341 /// initialize the video part
342 extern void InitVideo(void);
343
344 /// Check if a resolution is valid
345 extern int VideoValidResolution(int w, int h);
346
347 /// Load graphic from PNG file
348 extern int LoadGraphicPNG(CGraphic *g, bool headerOnly);
349
350 /// Make an OpenGL texture
351 extern void MakeTexture(CGraphic *graphic);
352 /// Make an OpenGL texture of the player color pixels only.
353 extern void MakePlayerColorTexture(CPlayerColorGraphic *graphic, int player);
354
355 /// Free OpenGL graphics
356 extern void FreeOpenGLGraphics();
357 /// Reload OpenGL graphics
358 extern void ReloadGraphics();
359 /// Reload OpenGL
360 extern void ReloadOpenGL();
361
362 /// Initializes video synchronization.
363 extern void SetVideoSync(void);
364
365 /// Init line draw
366 extern void InitLineDraw(void);
367
368 /// Simply invalidates whole window or screen.
369 extern void Invalidate(void);
370
371 /// Invalidates selected area on window or screen. Use for accurate
372 /// redrawing. in so
373 extern void InvalidateArea(int x, int y, int w, int h);
374
375 /// Set clipping for nearly all vector primitives. Functions which support
376 /// clipping will be marked Clip. Set the system-wide clipping rectangle.
377 extern void SetClipping(int left, int top, int right, int bottom);
378
379 /// Realize video memory.
380 extern void RealizeVideoMemory(void);
381
382 /// Save a screenshot to a PNG file
383 extern void SaveScreenshotPNG(const std::string &name);
384
385 /// Set the current callbacks
386 extern void SetCallbacks(const EventCallback *callbacks);
387 /// Get the current callbacks
388 extern const EventCallback *GetCallbacks();
389
390 /// Process all system events. Returns if the time for a frame is over
391 extern void WaitEventsOneFrame();
392
393 /// Toggle full screen mode
394 extern void ToggleFullScreen(void);
395
396 /// Push current clipping.
397 extern void PushClipping(void);
398
399 /// Pop current clipping.
400 extern void PopClipping(void);
401
402 /// Returns the ticks in ms since start
403 extern unsigned long GetTicks(void);
404
405 /// Convert a SDLKey to a string
406 extern const char *SdlKey2Str(int key);
407
408 /// Check if the mouse is grabbed
409 extern bool SdlGetGrabMouse(void);
410 /// Toggle mouse grab mode
411 extern void ToggleGrabMouse(int mode);
412
413 extern EventCallback GameCallbacks; /// Game callbacks
414 extern EventCallback EditorCallbacks; /// Editor callbacks
415
416 extern Uint32 ColorBlack;
417 extern Uint32 ColorDarkGreen;
418 extern Uint32 ColorDarkBlue;
419 extern Uint32 ColorBlue;
420 extern Uint32 ColorCyan;
421 extern Uint32 ColorOrange;
422 extern Uint32 ColorWhite;
423 extern Uint32 ColorGray;
424 extern Uint32 ColorRed;
425 extern Uint32 ColorGreen;
426 extern Uint32 ColorYellow;
427
428 void DrawTexture(const CGraphic *g, GLuint *textures,
429 int gx_beg, int gy_beg, int gx_end, int gy_end,
430 int sx_beg, int sy_beg, int flip);
431
432 extern void FreeGraphics();
433
434
435 // ARB_texture_compression
436 extern PFNGLCOMPRESSEDTEXIMAGE3DARBPROC glCompressedTexImage3DARB;
437 extern PFNGLCOMPRESSEDTEXIMAGE2DARBPROC glCompressedTexImage2DARB;
438 extern PFNGLCOMPRESSEDTEXIMAGE1DARBPROC glCompressedTexImage1DARB;
439 extern PFNGLCOMPRESSEDTEXSUBIMAGE3DARBPROC glCompressedTexSubImage3DARB;
440 extern PFNGLCOMPRESSEDTEXSUBIMAGE2DARBPROC glCompressedTexSubImage2DARB;
441 extern PFNGLCOMPRESSEDTEXSUBIMAGE1DARBPROC glCompressedTexSubImage1DARB;
442 extern PFNGLGETCOMPRESSEDTEXIMAGEARBPROC glGetCompressedTexImageARB;
443
444 //@}
445
446 #endif // !__VIDEO_H__