-
Notifications
You must be signed in to change notification settings - Fork 0
/
glSDL.h
325 lines (276 loc) · 10.1 KB
/
glSDL.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*(LGPL)
------------------------------------------------------------
glSDL 0.6 - SDL 2D API on top of OpenGL
------------------------------------------------------------
* (c) David Olofson, 2001-2003
* This code is released under the terms of the GNU LGPL.
*/
#ifndef _GLSDL_H_
#define _GLSDL_H_
/*
* If you don't use GNU autotools or similar, uncomment this to
* compile with OpenGL enabled:
#define HAVE_OPENGL
*/
/* We're still using SDL datatypes here - we just add some stuff. */
#include <SDL.h>
#include <SDL_image.h>
#ifndef HAVE_OPENGL
/* Fakes to make glSDL code compile with SDL. */
#define SDL_GLSDL 0
#define LOGIC_W(s) ( (s)->w )
#define LOGIC_H(s) ( (s)->h )
#define GLSDL_FIX_SURFACE(s)
#else /* HAVE_OPENGL */
#include "begin_code.h"
#ifdef __cplusplus
extern "C" {
#endif
#if (SDL_MAJOR_VERSION <= 1) && (SDL_MINOR_VERSION <= 2) && \
(SDL_PATCHLEVEL < 5)
#warning glSDL: Using SDL version 1.2.5 or later is strongly recommended!
#endif
/*----------------------------------------------------------
SDL style API
----------------------------------------------------------*/
typedef enum glSDL_TileModes
{
GLSDL_TM_SINGLE,
GLSDL_TM_HORIZONTAL,
GLSDL_TM_VERTICAL,
GLSDL_TM_HUGE
} glSDL_TileModes;
typedef struct glSDL_TexInfo
{
/* Size of surface in logic screen pixels */
int lw, lh;
int textures;
int *texture;
int texsize; /* width/height of OpenGL texture */
glSDL_TileModes tilemode;
int tilew, tileh; /* At least one must equal texsize! */
int tilespertex;
SDL_Rect virt; /* Total size of assembled surface */
/* Area of surface to download when/after unlocking */
SDL_Rect invalid_area;
} glSDL_TexInfo;
#define GLSDL_FIX_SURFACE(s) (s)->unused1 = 0;
#define IS_GLSDL_SURFACE(s) ((s) && glSDL_GetTexInfo(s))
#define LOGIC_W(s) ( IS_GLSDL_SURFACE(s) ? TEXINFO(s)->lw : (s)->w )
#define LOGIC_H(s) ( IS_GLSDL_SURFACE(s) ? TEXINFO(s)->lh : (s)->h )
#define SDL_GLSDL 0x00100000 /* Create an OpenGL 2D rendering context */
/*
* Wraps SDL_SetVideoMode(), and adds support for the SDL_GLSDL flag.
*
* If 'flags' contains SDL_GLSDL, glSDL_SetVideoMode() sets up a "pure"
* OpenGL rendering context for use with the glSDL_ calls.
*
* SDL can be closed as usual (using SDL_ calls), but you should call
* glSDL_Quit() (kludge) to allow glSDL to clean up it's internal stuff.
*/
SDL_Surface *glSDL_SetVideoMode(int width, int height, int bpp, Uint32 flags);
void glSDL_Quit(void);
void glSDL_QuitSubSystem(Uint32 flags);
/* Replaces SDL_Quit() entirely, when using the override defines */
void _glSDL_FullQuit(void);
SDL_Surface *glSDL_GetVideoSurface(void);
void glSDL_UpdateRects(SDL_Surface *screen, int numrects, SDL_Rect *rects);
void glSDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);
/*
* Works like SDL_Flip(), but may also perform enqueued blits.
* (That is, it's possible that the implementation renders
* *nothing* until glSDL_Flip() is called.)
*/
int glSDL_Flip(SDL_Surface *screen);
void glSDL_FreeSurface(SDL_Surface *surface);
int glSDL_LockSurface(SDL_Surface *surface);
void glSDL_UnlockSurface(SDL_Surface *surface);
/*
* Like the respective SDL functions, although they ignore
* SDL_RLEACCEL, as it makes no sense in this context.
*/
int glSDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key);
int glSDL_SetAlpha(SDL_Surface *surface, Uint32 flag, Uint8 alpha);
/*
* Sets up clipping for the screen, or a SDL_Surface.
*
* Note that this function takes both SDL_Surfaces and
* glSDL_Surfaces.
*/
SDL_bool glSDL_SetClipRect(SDL_Surface *surface, SDL_Rect *rect);
int glSDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);
int glSDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
/*
* Convert the given surface into a SDL_Surface (if it isn't
* one already), and makes sure that the underlying SDL_Surface
* is of a pixel format suitable for fast texture downloading.
*
* Note that you *only* have to use this function if you want
* fast pixel access to surfaces (ie "procedural textures").
* Any surfaces that aren't converted will be downloaded
* automatically upon the first call to glSDL_BlitSurface(),
* but if conversion is required, it will be required for
* every glSDL_UnlockSurface() call.
*
* IMPORTANT:
* You *can* pass an SDL_Surface directly to this function,
* and it will try to deal with it nicely. However, this
* requires that a temporary SDL_Surface is created, and
* this surface is cached only until the texture memory is
* needed for new surfaces.
*/
SDL_Surface *glSDL_DisplayFormat(SDL_Surface *surface);
SDL_Surface *glSDL_DisplayFormatAlpha(SDL_Surface *surface);
SDL_Surface *glSDL_ConvertSurface
(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags);
SDL_Surface *glSDL_CreateRGBSurface
(Uint32 flags, int width, int height, int depth,
Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
SDL_Surface *glSDL_CreateRGBSurfaceFrom(void *pixels,
int width, int height, int depth, int pitch,
Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
SDL_Surface *glSDL_LoadBMP(const char *file);
int glSDL_SaveBMP(SDL_Surface *surface, const char *file);
/*----------------------------------------------------------
glSDL specific API extensions
----------------------------------------------------------*/
/*
* Invalidate part of a texture.
*
* This function can be used either between calls to
* glSDL_LockSurface() and glSDL_UnlockSurface(), or before
* calling glSDL_DownloadSurface().
*
* In either case, it causes only the specified area to be
* downloaded when unlocking the surface, or calling
* glSDL_UnlockSurface(), respectively.
*
* Note that if this function is not used, glSDL assumes that
* the entire surface has to be updated. (That is, it's safe
* to ignore this function - it's "just a performance hack.")
*
* Passing a rectangle with zero height or width cancels the
* downloading when/after unlocking the surface. Use if you
* just want to read the texture, but feel like being nice and
* obeying SDL_MUSTLOCK() - which is a good idea, as things
* may change...
*
* Passing NULL for the 'area' argument results in the entire
* surface being invalidated.
*
* NOTE: This function does NOT perform clipping! Weird or
* even Bad Things may happen if you specify areas
* that protrude outside the edges of the actual
* surface.
*/
void glSDL_Invalidate(SDL_Surface *surface, SDL_Rect *area);
/*
* Set logic width and height of a surface.
*
* When used on the screen surface:
* Changes the OpenGL view coordinate system without
* changing the screen resolution. This is used to
* decouple the rendering coordinate system from the
* physical screen resolution, which is needed to
* achieve sub-pixel resolution without switching to
* float coords.
*
* When used on a SDL_Surface:
* Normally, surfaces are scaled so that one pixel on
* the surface corresponds to one pixel in the current
* logic screen resolution. This function makes it
* possible to change that relation by specifying the
* desired size of the rendered texture in the current
* logic screen resolution.
*
* BUG? WRT blitting *to* surfaces, this call affects
* the screen surface and other surfaces differently!
* As of now, coordinates are *always* treated as real
* pixels when blitting to normal surfaces, whereas
* they are scaled when blitting to the screen.
*
* Of course, this function can also be "abused" just to
* make rendering code independent of screen resolution,
* although that can potentially *lower* accuracy, if the
* real resolution is higher than the logic resolution.
*/
void glSDL_SetLogicSize(SDL_Surface *surface, int w, int h);
/*
* Make sure that the texture of the specified surface is up
* to date in OpenGL texture memory.
*
* This can be used together with glSDL_UnloadSurface() to
* implement custom texture caching schemes.
*
* Returns 0 on success, or a negative value if something
* went wrong.
*/
int glSDL_UploadSurface(SDL_Surface *surface);
/*
* Free the texture space used by the specified surface.
*
* Normally, glSDL should download textures when needed, and
* unload the oldest (in terms of use) surfaces, if it runs out
* of texture space.
*/
void glSDL_UnloadSurface(SDL_Surface *surface);
/*
* Get the clSDL_TexInfo struct associated with a surface.
*/
glSDL_TexInfo *glSDL_GetTexInfo(SDL_Surface *surface);
#ifdef __cplusplus
}
#endif
#include "close_code.h"
/* Some ugly "overriding"... */
#ifndef _GLSDL_NO_REDEFINES_
/*
* You *always* need to lock and unlock a glSDL surface in
* order to get glSDL to update the OpenGL texture!
*/
#undef SDL_MUSTLOCK
#define SDL_MUSTLOCK(surface) \
(surface->offset || \
((surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_RLEACCEL)) != 0) || \
IS_GLSDL_SURFACE(surface))
#define SDL_SetVideoMode glSDL_SetVideoMode
#define SDL_GetVideoSurface glSDL_GetVideoSurface
#define SDL_Quit _glSDL_FullQuit
#define SDL_QuitSubSystem glSDL_QuitSubSystem
#define SDL_UpdateRects glSDL_UpdateRects
#define SDL_UpdateRect glSDL_UpdateRect
#define SDL_Flip glSDL_Flip
#define SDL_FreeSurface glSDL_FreeSurface
#define SDL_LockSurface glSDL_LockSurface
#define SDL_UnlockSurface glSDL_UnlockSurface
#define SDL_SetColorKey glSDL_SetColorKey
#define SDL_SetAlpha glSDL_SetAlpha
#define SDL_SetClipRect glSDL_SetClipRect
#undef SDL_BlitSurface
#define SDL_BlitSurface glSDL_BlitSurface
#define SDL_FillRect glSDL_FillRect
#define SDL_DisplayFormat glSDL_DisplayFormat
#define SDL_DisplayFormatAlpha glSDL_DisplayFormatAlpha
#define SDL_ConvertSurface glSDL_ConvertSurface
#define SDL_CreateRGBSurface glSDL_CreateRGBSurface
#define SDL_CreateRGBSurfaceFrom glSDL_CreateRGBSurfaceFrom
#undef SDL_AllocSurface
#define SDL_AllocSurface glSDL_CreateRGBSurface
#undef SDL_LoadBMP
#define SDL_LoadBMP glSDL_LoadBMP
#undef SDL_SaveBMP
#define SDL_SaveBMP glSDL_SaveBMP
#define IMG_Load(x) glSDL_IMG_Load(x)
#endif
/* Some extra overloading for common external lib calls... */
#include <SDL_image.h>
#ifdef __cplusplus
extern "C" {
#endif
SDL_Surface *glSDL_IMG_Load(const char *file);
#ifdef __cplusplus
}
#endif
#endif /* HAVE_OPENGL */
#endif