Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
876 lines (791 loc) · 26.7 KB

SDL_renderer_gl.c

File metadata and controls

876 lines (791 loc) · 26.7 KB
 
Jul 19, 2006
Jul 19, 2006
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
Jul 28, 2006
Jul 28, 2006
24
#if SDL_VIDEO_RENDER_OGL
Jul 22, 2006
Jul 22, 2006
25
26
27
28
29
30
31
#include "SDL_video.h"
#include "SDL_opengl.h"
#include "SDL_sysvideo.h"
#include "SDL_pixels_c.h"
#include "SDL_rect_c.h"
#include "SDL_yuv_sw_c.h"
Jul 19, 2006
Jul 19, 2006
32
33
34
/* OpenGL renderer implementation */
Aug 28, 2006
Aug 28, 2006
35
36
static const float inv255f = 1.0f / 255.0f;
Jul 19, 2006
Jul 19, 2006
37
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
Jul 22, 2006
Jul 22, 2006
38
static int GL_ActivateRenderer(SDL_Renderer * renderer);
Aug 6, 2006
Aug 6, 2006
39
static int GL_DisplayModeChanged(SDL_Renderer * renderer);
Jul 19, 2006
Jul 19, 2006
40
41
42
43
44
45
46
47
static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int GL_SetTexturePalette(SDL_Renderer * renderer,
SDL_Texture * texture,
const SDL_Color * colors, int firstcolor,
int ncolors);
static int GL_GetTexturePalette(SDL_Renderer * renderer,
SDL_Texture * texture, SDL_Color * colors,
int firstcolor, int ncolors);
Aug 28, 2006
Aug 28, 2006
48
49
50
51
52
53
54
55
static int GL_SetTextureColorMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int GL_SetTextureAlphaMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int GL_SetTextureBlendMode(SDL_Renderer * renderer,
SDL_Texture * texture);
static int GL_SetTextureScaleMode(SDL_Renderer * renderer,
SDL_Texture * texture);
Jul 19, 2006
Jul 19, 2006
56
57
58
59
static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
60
61
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch);
Jul 19, 2006
Jul 19, 2006
62
63
64
static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
int numrects, const SDL_Rect * rects);
Aug 28, 2006
Aug 28, 2006
65
66
static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
Uint8 a, const SDL_Rect * rect);
Jul 19, 2006
Jul 19, 2006
67
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
68
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Jul 19, 2006
Jul 19, 2006
69
70
71
72
73
74
75
76
77
static void GL_RenderPresent(SDL_Renderer * renderer);
static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void GL_DestroyRenderer(SDL_Renderer * renderer);
SDL_RenderDriver GL_RenderDriver = {
GL_CreateRenderer,
{
"opengl",
Aug 6, 2006
Aug 6, 2006
78
79
(SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
Aug 28, 2006
Aug 28, 2006
80
81
(SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
SDL_TEXTUREMODULATE_ALPHA),
Aug 5, 2006
Aug 5, 2006
82
83
84
85
86
(SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
SDL_TEXTUREBLENDMODE_MOD),
(SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
SDL_TEXTURESCALEMODE_SLOW),
Jul 22, 2006
Jul 22, 2006
87
16,
Jul 19, 2006
Jul 19, 2006
88
{
Aug 5, 2006
Aug 5, 2006
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
SDL_PIXELFORMAT_INDEX1LSB,
SDL_PIXELFORMAT_INDEX1MSB,
SDL_PIXELFORMAT_INDEX8,
SDL_PIXELFORMAT_RGB332,
SDL_PIXELFORMAT_RGB444,
SDL_PIXELFORMAT_RGB555,
SDL_PIXELFORMAT_ARGB4444,
SDL_PIXELFORMAT_ARGB1555,
SDL_PIXELFORMAT_RGB565,
SDL_PIXELFORMAT_RGB24,
SDL_PIXELFORMAT_BGR24,
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_BGR888,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_ABGR8888,
SDL_PIXELFORMAT_ARGB2101010},
Jul 19, 2006
Jul 19, 2006
105
106
107
108
109
110
111
0,
0}
};
typedef struct
{
SDL_GLContext context;
Aug 6, 2006
Aug 6, 2006
112
SDL_bool updateSize;
Aug 6, 2006
Aug 6, 2006
113
SDL_bool GL_EXT_paletted_texture_supported;
Jul 22, 2006
Jul 22, 2006
114
SDL_bool GL_ARB_texture_rectangle_supported;
Jul 22, 2006
Jul 22, 2006
115
116
117
118
119
120
121
int blendMode;
int scaleMode;
/* OpenGL functions */
#define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
#include "SDL_glfuncs.h"
#undef SDL_PROC
Aug 6, 2006
Aug 6, 2006
122
123
PFNGLCOLORTABLEEXTPROC glColorTableEXT;
Jul 19, 2006
Jul 19, 2006
124
125
126
127
128
} GL_RenderData;
typedef struct
{
GLuint texture;
Jul 22, 2006
Jul 22, 2006
129
GLenum type;
Jul 19, 2006
Jul 19, 2006
130
131
GLfloat texw;
GLfloat texh;
Jul 22, 2006
Jul 22, 2006
132
133
GLenum format;
GLenum formattype;
Aug 6, 2006
Aug 6, 2006
134
Uint8 *palette;
Jul 19, 2006
Jul 19, 2006
135
136
void *pixels;
int pitch;
Jul 22, 2006
Jul 22, 2006
137
SDL_DirtyRectList dirty;
Jul 19, 2006
Jul 19, 2006
138
139
140
} GL_TextureData;
Jul 22, 2006
Jul 22, 2006
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
static void
GL_SetError(const char *prefix, GLenum result)
{
const char *error;
switch (result) {
case GL_NO_ERROR:
error = "GL_NO_ERROR";
break;
case GL_INVALID_ENUM:
error = "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
error = "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
error = "GL_INVALID_OPERATION";
break;
case GL_STACK_OVERFLOW:
error = "GL_STACK_OVERFLOW";
break;
case GL_STACK_UNDERFLOW:
error = "GL_STACK_UNDERFLOW";
break;
case GL_OUT_OF_MEMORY:
error = "GL_OUT_OF_MEMORY";
break;
case GL_TABLE_TOO_LARGE:
error = "GL_TABLE_TOO_LARGE";
break;
default:
error = "UNKNOWN";
break;
}
SDL_SetError("%s: %s", prefix, error);
}
Jul 22, 2006
Jul 22, 2006
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
static int
GL_LoadFunctions(GL_RenderData * data)
{
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
#define __SDL_NOGETPROCADDR__
#elif defined(__MINT__)
#define __SDL_NOGETPROCADDR__
#endif
#ifdef __SDL_NOGETPROCADDR__
#define SDL_PROC(ret,func,params) data->func=func;
#else
#define SDL_PROC(ret,func,params) \
do { \
data->func = SDL_GL_GetProcAddress(#func); \
if ( ! data->func ) { \
SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
return -1; \
} \
} while ( 0 );
#endif /* __SDL_NOGETPROCADDR__ */
#include "SDL_glfuncs.h"
#undef SDL_PROC
return 0;
}
Jul 19, 2006
Jul 19, 2006
204
205
206
void
GL_AddRenderDriver(_THIS)
{
Jul 22, 2006
Jul 22, 2006
207
if (_this->GL_CreateContext) {
Jul 19, 2006
Jul 19, 2006
208
209
210
211
212
213
214
215
216
SDL_AddRenderDriver(0, &GL_RenderDriver);
}
}
SDL_Renderer *
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer;
GL_RenderData *data;
Jul 28, 2006
Jul 28, 2006
217
GLint value;
Aug 6, 2006
Aug 6, 2006
218
int doublebuffer;
Jul 19, 2006
Jul 19, 2006
219
Aug 6, 2006
Aug 6, 2006
220
221
222
223
224
225
/* Render directly to the window, unless we're compositing */
#ifndef __MACOSX__
if (flags & SDL_RENDERER_SINGLEBUFFER) {
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
}
#endif
Jul 19, 2006
Jul 19, 2006
226
if (!(window->flags & SDL_WINDOW_OPENGL)) {
Jul 23, 2006
Jul 23, 2006
227
if (SDL_RecreateWindow(window, window->flags | SDL_WINDOW_OPENGL) < 0) {
Jul 22, 2006
Jul 22, 2006
228
229
return NULL;
}
Jul 19, 2006
Jul 19, 2006
230
231
}
Jul 22, 2006
Jul 22, 2006
232
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
Jul 19, 2006
Jul 19, 2006
233
234
235
236
237
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Jul 22, 2006
Jul 22, 2006
238
data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
239
240
241
242
243
244
if (!data) {
GL_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
Jul 22, 2006
Jul 22, 2006
245
renderer->ActivateRenderer = GL_ActivateRenderer;
Aug 6, 2006
Aug 6, 2006
246
renderer->DisplayModeChanged = GL_DisplayModeChanged;
Jul 19, 2006
Jul 19, 2006
247
248
249
renderer->CreateTexture = GL_CreateTexture;
renderer->SetTexturePalette = GL_SetTexturePalette;
renderer->GetTexturePalette = GL_GetTexturePalette;
Aug 28, 2006
Aug 28, 2006
250
251
252
253
renderer->SetTextureColorMod = GL_SetTextureColorMod;
renderer->SetTextureAlphaMod = GL_SetTextureAlphaMod;
renderer->SetTextureBlendMode = GL_SetTextureBlendMode;
renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
Jul 19, 2006
Jul 19, 2006
254
255
256
257
258
259
260
261
262
263
264
265
266
267
renderer->UpdateTexture = GL_UpdateTexture;
renderer->LockTexture = GL_LockTexture;
renderer->UnlockTexture = GL_UnlockTexture;
renderer->DirtyTexture = GL_DirtyTexture;
renderer->RenderFill = GL_RenderFill;
renderer->RenderCopy = GL_RenderCopy;
renderer->RenderPresent = GL_RenderPresent;
renderer->DestroyTexture = GL_DestroyTexture;
renderer->DestroyRenderer = GL_DestroyRenderer;
renderer->info = GL_RenderDriver.info;
renderer->window = window->id;
renderer->driverdata = data;
renderer->info.flags =
Aug 5, 2006
Aug 5, 2006
268
(SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED);
Jul 19, 2006
Jul 19, 2006
269
Jul 22, 2006
Jul 22, 2006
270
271
272
273
274
if (GL_LoadFunctions(data) < 0) {
GL_DestroyRenderer(renderer);
return NULL;
}
Jul 19, 2006
Jul 19, 2006
275
276
277
278
279
280
281
282
283
284
data->context = SDL_GL_CreateContext(window->id);
if (!data->context) {
GL_DestroyRenderer(renderer);
return NULL;
}
if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
GL_DestroyRenderer(renderer);
return NULL;
}
Aug 5, 2006
Aug 5, 2006
285
if (flags & SDL_RENDERER_PRESENTVSYNC) {
Jul 19, 2006
Jul 19, 2006
286
287
288
289
290
SDL_GL_SetSwapInterval(1);
} else {
SDL_GL_SetSwapInterval(0);
}
if (SDL_GL_GetSwapInterval() > 0) {
Aug 5, 2006
Aug 5, 2006
291
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
Jul 19, 2006
Jul 19, 2006
292
293
}
Aug 6, 2006
Aug 6, 2006
294
295
296
297
298
299
if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) {
if (!doublebuffer) {
renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
}
}
Jul 28, 2006
Jul 28, 2006
300
301
302
303
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
renderer->info.max_texture_width = value;
data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
renderer->info.max_texture_height = value;
Jul 22, 2006
Jul 22, 2006
304
Jul 22, 2006
Jul 22, 2006
305
306
307
308
if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle")
|| SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
}
Aug 6, 2006
Aug 6, 2006
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) {
data->GL_EXT_paletted_texture_supported = SDL_TRUE;
data->glColorTableEXT =
(PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT");
} else {
/* Don't advertise support for 8-bit indexed texture format */
Uint32 i, j;
SDL_RendererInfo *info = &renderer->info;
for (i = 0, j = 0; i < info->num_texture_formats; ++i) {
if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) {
info->texture_formats[j++] = info->texture_formats[i];
}
}
--info->num_texture_formats;
}
Jul 22, 2006
Jul 22, 2006
324
Jul 19, 2006
Jul 19, 2006
325
/* Set up parameters for rendering */
Jul 22, 2006
Jul 22, 2006
326
327
328
329
data->blendMode = -1;
data->scaleMode = -1;
data->glDisable(GL_DEPTH_TEST);
data->glDisable(GL_CULL_FACE);
Jul 22, 2006
Jul 22, 2006
330
if (data->GL_ARB_texture_rectangle_supported) {
Jul 22, 2006
Jul 22, 2006
331
data->glEnable(GL_TEXTURE_RECTANGLE_ARB);
Jul 22, 2006
Jul 22, 2006
332
} else {
Jul 22, 2006
Jul 22, 2006
333
data->glEnable(GL_TEXTURE_2D);
Jul 22, 2006
Jul 22, 2006
334
}
Aug 6, 2006
Aug 6, 2006
335
data->updateSize = SDL_TRUE;
Jul 19, 2006
Jul 19, 2006
336
337
338
339
return renderer;
}
Jul 22, 2006
Jul 22, 2006
340
341
342
343
344
345
static int
GL_ActivateRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Aug 6, 2006
Aug 6, 2006
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
return -1;
}
if (data->updateSize) {
data->glMatrixMode(GL_PROJECTION);
data->glLoadIdentity();
data->glMatrixMode(GL_MODELVIEW);
data->glLoadIdentity();
data->glViewport(0, 0, window->w, window->h);
data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
0.0, 1.0);
data->updateSize = SDL_FALSE;
}
return 0;
}
static int
GL_DisplayModeChanged(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
data->updateSize = SDL_TRUE;
return 0;
Jul 22, 2006
Jul 22, 2006
369
370
}
Jul 22, 2006
Jul 22, 2006
371
372
373
374
375
376
377
378
379
380
381
static __inline__ int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
Jul 19, 2006
Jul 19, 2006
382
383
384
385
386
387
static int
GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
GL_TextureData *data;
Jul 22, 2006
Jul 22, 2006
388
389
GLint internalFormat;
GLenum format, type;
Jul 22, 2006
Jul 22, 2006
390
int texture_w, texture_h;
Jul 22, 2006
Jul 22, 2006
391
GLenum result;
Jul 22, 2006
Jul 22, 2006
392
393
switch (texture->format) {
Aug 5, 2006
Aug 5, 2006
394
395
case SDL_PIXELFORMAT_INDEX1LSB:
case SDL_PIXELFORMAT_INDEX1MSB:
Jul 22, 2006
Jul 22, 2006
396
397
398
399
internalFormat = GL_RGB;
format = GL_COLOR_INDEX;
type = GL_BITMAP;
break;
Aug 5, 2006
Aug 5, 2006
400
case SDL_PIXELFORMAT_INDEX8:
Aug 6, 2006
Aug 6, 2006
401
402
403
404
405
if (!renderdata->GL_EXT_paletted_texture_supported) {
SDL_SetError("Unsupported texture format");
return -1;
}
internalFormat = GL_COLOR_INDEX8_EXT;
Jul 22, 2006
Jul 22, 2006
406
407
408
format = GL_COLOR_INDEX;
type = GL_UNSIGNED_BYTE;
break;
Aug 5, 2006
Aug 5, 2006
409
case SDL_PIXELFORMAT_RGB332:
Jul 22, 2006
Jul 22, 2006
410
411
412
413
internalFormat = GL_R3_G3_B2;
format = GL_RGB;
type = GL_UNSIGNED_BYTE_3_3_2;
break;
Aug 5, 2006
Aug 5, 2006
414
case SDL_PIXELFORMAT_RGB444:
Jul 22, 2006
Jul 22, 2006
415
416
417
418
internalFormat = GL_RGB4;
format = GL_RGB;
type = GL_UNSIGNED_SHORT_4_4_4_4;
break;
Aug 5, 2006
Aug 5, 2006
419
case SDL_PIXELFORMAT_RGB555:
Jul 22, 2006
Jul 22, 2006
420
421
422
423
internalFormat = GL_RGB5;
format = GL_RGB;
type = GL_UNSIGNED_SHORT_5_5_5_1;
break;
Aug 5, 2006
Aug 5, 2006
424
case SDL_PIXELFORMAT_ARGB4444:
Jul 22, 2006
Jul 22, 2006
425
426
427
428
internalFormat = GL_RGBA4;
format = GL_BGRA;
type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
break;
Aug 5, 2006
Aug 5, 2006
429
case SDL_PIXELFORMAT_ARGB1555:
Jul 22, 2006
Jul 22, 2006
430
431
432
433
internalFormat = GL_RGB5_A1;
format = GL_BGRA;
type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
break;
Aug 5, 2006
Aug 5, 2006
434
case SDL_PIXELFORMAT_RGB565:
Jul 22, 2006
Jul 22, 2006
435
436
437
438
internalFormat = GL_RGB8;
format = GL_RGB;
type = GL_UNSIGNED_SHORT_5_6_5;
break;
Aug 5, 2006
Aug 5, 2006
439
case SDL_PIXELFORMAT_RGB24:
Jul 22, 2006
Jul 22, 2006
440
441
442
443
internalFormat = GL_RGB8;
format = GL_RGB;
type = GL_UNSIGNED_BYTE;
break;
Aug 5, 2006
Aug 5, 2006
444
case SDL_PIXELFORMAT_RGB888:
Jul 22, 2006
Jul 22, 2006
445
internalFormat = GL_RGB8;
Jul 22, 2006
Jul 22, 2006
446
447
format = GL_BGRA;
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
448
break;
Aug 5, 2006
Aug 5, 2006
449
case SDL_PIXELFORMAT_BGR24:
Jul 22, 2006
Jul 22, 2006
450
451
452
453
internalFormat = GL_RGB8;
format = GL_BGR;
type = GL_UNSIGNED_BYTE;
break;
Aug 5, 2006
Aug 5, 2006
454
case SDL_PIXELFORMAT_BGR888:
Jul 22, 2006
Jul 22, 2006
455
internalFormat = GL_RGB8;
Jul 22, 2006
Jul 22, 2006
456
457
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
458
break;
Aug 5, 2006
Aug 5, 2006
459
case SDL_PIXELFORMAT_ARGB8888:
Jul 22, 2006
Jul 22, 2006
460
461
internalFormat = GL_RGBA8;
format = GL_BGRA;
Jul 22, 2006
Jul 22, 2006
462
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
463
break;
Aug 5, 2006
Aug 5, 2006
464
case SDL_PIXELFORMAT_ABGR8888:
Jul 22, 2006
Jul 22, 2006
465
466
internalFormat = GL_RGBA8;
format = GL_RGBA;
Jul 22, 2006
Jul 22, 2006
467
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
468
break;
Aug 5, 2006
Aug 5, 2006
469
case SDL_PIXELFORMAT_ARGB2101010:
Jul 22, 2006
Jul 22, 2006
470
471
472
473
474
475
476
477
internalFormat = GL_RGB10_A2;
format = GL_BGRA;
type = GL_UNSIGNED_INT_2_10_10_10_REV;
break;
default:
SDL_SetError("Unsupported texture format");
return -1;
}
Jul 19, 2006
Jul 19, 2006
478
Jul 22, 2006
Jul 22, 2006
479
data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
480
481
482
483
484
if (!data) {
SDL_OutOfMemory();
return -1;
}
Aug 6, 2006
Aug 6, 2006
485
486
487
488
489
490
491
492
493
494
if (texture->format == SDL_PIXELFORMAT_INDEX8) {
data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8));
if (!data->palette) {
SDL_OutOfMemory();
SDL_free(data);
return -1;
}
SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8));
}
Jul 19, 2006
Jul 19, 2006
495
496
texture->driverdata = data;
Jul 22, 2006
Jul 22, 2006
497
498
renderdata->glGetError();
renderdata->glGenTextures(1, &data->texture);
Jul 22, 2006
Jul 22, 2006
499
500
501
502
503
504
505
506
507
508
509
510
511
if (renderdata->GL_ARB_texture_rectangle_supported) {
data->type = GL_TEXTURE_RECTANGLE_ARB;
texture_w = texture->w;
texture_h = texture->h;
data->texw = (GLfloat) texture->w;
data->texh = (GLfloat) texture->h;
} else {
data->type = GL_TEXTURE_2D;
texture_w = power_of_2(texture->w);
texture_h = power_of_2(texture->h);
data->texw = (GLfloat) texture->w / texture_w;
data->texh = (GLfloat) texture->h / texture_h;
}
Jul 22, 2006
Jul 22, 2006
512
513
data->format = format;
data->formattype = type;
Jul 22, 2006
Jul 22, 2006
514
515
516
517
renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w,
texture_h, 0, format, type, NULL);
result = renderdata->glGetError();
Jul 22, 2006
Jul 22, 2006
518
519
520
521
if (result != GL_NO_ERROR) {
GL_SetError("glTexImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
522
523
524
525
526
527
528
529
530
return 0;
}
static int
GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Color * colors, int firstcolor, int ncolors)
{
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Aug 6, 2006
Aug 6, 2006
531
Uint8 *palette;
Jul 19, 2006
Jul 19, 2006
532
Aug 6, 2006
Aug 6, 2006
533
534
535
536
537
538
539
540
541
542
543
544
545
546
if (!data->palette) {
SDL_SetError("Texture doesn't have a palette");
return -1;
}
palette = data->palette + firstcolor * 3;
while (ncolors--) {
*palette++ = colors->r;
*palette++ = colors->g;
*palette++ = colors->b;
++colors;
}
renderdata->glBindTexture(data->type, data->texture);
renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
GL_UNSIGNED_BYTE, data->palette);
Jul 19, 2006
Jul 19, 2006
547
548
549
550
551
552
553
return 0;
}
static int
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
SDL_Color * colors, int firstcolor, int ncolors)
{
Aug 6, 2006
Aug 6, 2006
554
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Jul 19, 2006
Jul 19, 2006
555
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Aug 6, 2006
Aug 6, 2006
556
Uint8 *palette;
Jul 19, 2006
Jul 19, 2006
557
Aug 6, 2006
Aug 6, 2006
558
559
560
561
562
563
564
565
566
567
568
569
if (!data->palette) {
SDL_SetError("Texture doesn't have a palette");
return -1;
}
palette = data->palette + firstcolor * 3;
while (ncolors--) {
colors->r = *palette++;
colors->g = *palette++;
colors->b = *palette++;
colors->unused = SDL_ALPHA_OPAQUE;
++colors;
}
Jul 19, 2006
Jul 19, 2006
570
571
572
return 0;
}
Jul 22, 2006
Jul 22, 2006
573
static void
Jul 22, 2006
Jul 22, 2006
574
575
SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture,
int pitch)
Jul 22, 2006
Jul 22, 2006
576
{
Aug 5, 2006
Aug 5, 2006
577
if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) {
Jul 22, 2006
Jul 22, 2006
578
renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
Aug 5, 2006
Aug 5, 2006
579
} else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
Jul 22, 2006
Jul 22, 2006
580
renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
Jul 22, 2006
Jul 22, 2006
581
}
Jul 22, 2006
Jul 22, 2006
582
583
584
renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
pitch / SDL_BYTESPERPIXEL(texture->format));
Jul 22, 2006
Jul 22, 2006
585
586
}
Aug 28, 2006
Aug 28, 2006
587
588
589
static int
GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 28, 2006
Aug 28, 2006
590
return 0;
Aug 28, 2006
Aug 28, 2006
591
592
593
594
595
}
static int
GL_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 28, 2006
Aug 28, 2006
596
return 0;
Aug 28, 2006
Aug 28, 2006
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
}
static int
GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
switch (texture->blendMode) {
case SDL_TEXTUREBLENDMODE_NONE:
case SDL_TEXTUREBLENDMODE_MASK:
case SDL_TEXTUREBLENDMODE_BLEND:
case SDL_TEXTUREBLENDMODE_ADD:
case SDL_TEXTUREBLENDMODE_MOD:
return 0;
default:
SDL_Unsupported();
texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
return -1;
}
}
static int
GL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
switch (texture->scaleMode) {
case SDL_TEXTURESCALEMODE_NONE:
case SDL_TEXTURESCALEMODE_FAST:
case SDL_TEXTURESCALEMODE_SLOW:
return 0;
case SDL_TEXTURESCALEMODE_BEST:
SDL_Unsupported();
texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW;
return -1;
default:
SDL_Unsupported();
texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
return -1;
}
}
Jul 19, 2006
Jul 19, 2006
635
636
637
638
static int
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
Jul 22, 2006
Jul 22, 2006
639
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Jul 19, 2006
Jul 19, 2006
640
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Jul 22, 2006
Jul 22, 2006
641
GLenum result;
Jul 19, 2006
Jul 19, 2006
642
Jul 22, 2006
Jul 22, 2006
643
644
645
646
647
648
649
renderdata->glGetError();
SetupTextureUpdate(renderdata, texture, pitch);
renderdata->glBindTexture(data->type, data->texture);
renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
rect->h, data->format, data->formattype,
pixels);
result = renderdata->glGetError();
Jul 22, 2006
Jul 22, 2006
650
651
652
653
if (result != GL_NO_ERROR) {
GL_SetError("glTexSubImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
654
655
656
657
658
659
660
661
662
663
return 0;
}
static int
GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Jul 22, 2006
Jul 22, 2006
664
665
666
667
668
669
670
if (!data->pixels) {
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
data->pixels = SDL_malloc(texture->h * data->pitch);
if (!data->pixels) {
SDL_OutOfMemory();
return -1;
}
Jul 19, 2006
Jul 19, 2006
671
672
}
Jul 22, 2006
Jul 22, 2006
673
674
if (markDirty) {
SDL_AddDirtyRect(&data->dirty, rect);
Jul 19, 2006
Jul 19, 2006
675
}
Jul 22, 2006
Jul 22, 2006
676
677
678
679
680
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
Jul 19, 2006
Jul 19, 2006
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
return 0;
}
static void
GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
}
static void
GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
const SDL_Rect * rects)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
int i;
for (i = 0; i < numrects; ++i) {
Jul 22, 2006
Jul 22, 2006
697
SDL_AddDirtyRect(&data->dirty, &rects[i]);
Jul 19, 2006
Jul 19, 2006
698
699
700
701
}
}
static int
Aug 28, 2006
Aug 28, 2006
702
703
GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
const SDL_Rect * rect)
Jul 19, 2006
Jul 19, 2006
704
705
706
707
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
Aug 28, 2006
Aug 28, 2006
708
709
data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f,
(GLclampf) b * inv255f, (GLclampf) a * inv255f);
Jul 22, 2006
Jul 22, 2006
710
711
712
data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
data->glClear(GL_COLOR_BUFFER_BIT);
data->glViewport(0, 0, window->w, window->h);
Jul 19, 2006
Jul 19, 2006
713
714
715
716
717
return 0;
}
static int
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
718
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Jul 19, 2006
Jul 19, 2006
719
720
721
722
723
724
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
int minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
Jul 22, 2006
Jul 22, 2006
725
726
727
728
729
730
if (texturedata->dirty.count > 0) {
SDL_DirtyRect *dirty;
void *pixels;
int bpp = SDL_BYTESPERPIXEL(texture->format);
int pitch = texturedata->pitch;
Jul 22, 2006
Jul 22, 2006
731
732
SetupTextureUpdate(data, texture, pitch);
data->glBindTexture(texturedata->type, texturedata->texture);
Jul 22, 2006
Jul 22, 2006
733
734
735
736
737
for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
SDL_Rect *rect = &dirty->rect;
pixels =
(void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
rect->x * bpp);
Jul 22, 2006
Jul 22, 2006
738
739
740
data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
rect->w, rect->h, texturedata->format,
texturedata->formattype, pixels);
Jul 22, 2006
Jul 22, 2006
741
742
743
744
}
SDL_ClearDirtyRects(&texturedata->dirty);
}
Jul 19, 2006
Jul 19, 2006
745
746
747
748
749
750
minx = dstrect->x;
miny = dstrect->y;
maxx = dstrect->x + dstrect->w;
maxy = dstrect->y + dstrect->h;
minu = (GLfloat) srcrect->x / texture->w;
Jul 22, 2006
Jul 22, 2006
751
minu *= texturedata->texw;
Jul 19, 2006
Jul 19, 2006
752
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
Jul 22, 2006
Jul 22, 2006
753
maxu *= texturedata->texw;
Jul 19, 2006
Jul 19, 2006
754
minv = (GLfloat) srcrect->y / texture->h;
Jul 22, 2006
Jul 22, 2006
755
minv *= texturedata->texh;
Jul 19, 2006
Jul 19, 2006
756
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
Jul 22, 2006
Jul 22, 2006
757
maxv *= texturedata->texh;
Jul 19, 2006
Jul 19, 2006
758
Jul 22, 2006
Jul 22, 2006
759
760
data->glBindTexture(texturedata->type, texturedata->texture);
Aug 28, 2006
Aug 28, 2006
761
762
763
764
765
766
767
768
769
770
771
if (texture->modMode) {
data->glColor4f((GLfloat) texture->r * inv255f,
(GLfloat) texture->g * inv255f,
(GLfloat) texture->b * inv255f,
(GLfloat) texture->a * inv255f);
} else {
data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
if (texture->blendMode != data->blendMode) {
switch (texture->blendMode) {
Aug 5, 2006
Aug 5, 2006
772
case SDL_TEXTUREBLENDMODE_NONE:
Jul 22, 2006
Jul 22, 2006
773
774
775
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
data->glDisable(GL_BLEND);
break;
Aug 5, 2006
Aug 5, 2006
776
777
case SDL_TEXTUREBLENDMODE_MASK:
case SDL_TEXTUREBLENDMODE_BLEND:
Jul 22, 2006
Jul 22, 2006
778
779
780
781
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
Aug 5, 2006
Aug 5, 2006
782
case SDL_TEXTUREBLENDMODE_ADD:
Jul 22, 2006
Jul 22, 2006
783
784
785
786
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
Aug 5, 2006
Aug 5, 2006
787
case SDL_TEXTUREBLENDMODE_MOD:
Jul 22, 2006
Jul 22, 2006
788
789
790
791
792
data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
data->glEnable(GL_BLEND);
data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
break;
}
Aug 28, 2006
Aug 28, 2006
793
data->blendMode = texture->blendMode;
Jul 19, 2006
Jul 19, 2006
794
795
}
Aug 28, 2006
Aug 28, 2006
796
797
if (texture->scaleMode != data->scaleMode) {
switch (texture->scaleMode) {
Aug 5, 2006
Aug 5, 2006
798
799
case SDL_TEXTURESCALEMODE_NONE:
case SDL_TEXTURESCALEMODE_FAST:
Jul 22, 2006
Jul 22, 2006
800
801
802
803
804
data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
break;
Aug 5, 2006
Aug 5, 2006
805
806
case SDL_TEXTURESCALEMODE_SLOW:
case SDL_TEXTURESCALEMODE_BEST:
Jul 22, 2006
Jul 22, 2006
807
808
809
810
811
812
data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
break;
}
Aug 28, 2006
Aug 28, 2006
813
data->scaleMode = texture->scaleMode;
Jul 19, 2006
Jul 19, 2006
814
815
}
Jul 22, 2006
Jul 22, 2006
816
817
818
819
820
821
822
823
824
825
data->glBegin(GL_TRIANGLE_STRIP);
data->glTexCoord2f(minu, minv);
data->glVertex2i(minx, miny);
data->glTexCoord2f(maxu, minv);
data->glVertex2i(maxx, miny);
data->glTexCoord2f(minu, maxv);
data->glVertex2i(minx, maxy);
data->glTexCoord2f(maxu, maxv);
data->glVertex2i(maxx, maxy);
data->glEnd();
Jul 19, 2006
Jul 19, 2006
826
827
828
829
830
831
832
833
834
835
836
837
838
return 0;
}
static void
GL_RenderPresent(SDL_Renderer * renderer)
{
SDL_GL_SwapWindow(renderer->window);
}
static void
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
Jul 22, 2006
Jul 22, 2006
839
GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
Jul 19, 2006
Jul 19, 2006
840
841
842
843
844
845
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
if (!data) {
return;
}
if (data->texture) {
Jul 22, 2006
Jul 22, 2006
846
renderdata->glDeleteTextures(1, &data->texture);
Jul 22, 2006
Jul 22, 2006
847
}
Aug 6, 2006
Aug 6, 2006
848
849
850
if (data->palette) {
SDL_free(data->palette);
}
Jul 22, 2006
Jul 22, 2006
851
852
if (data->pixels) {
SDL_free(data->pixels);
Jul 19, 2006
Jul 19, 2006
853
}
Jul 22, 2006
Jul 22, 2006
854
SDL_FreeDirtyRects(&data->dirty);
Jul 19, 2006
Jul 19, 2006
855
856
857
858
SDL_free(data);
texture->driverdata = NULL;
}
Aug 7, 2006
Aug 7, 2006
859
static void
Jul 19, 2006
Jul 19, 2006
860
861
862
863
864
GL_DestroyRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
if (data) {
Jul 22, 2006
Jul 22, 2006
865
866
867
if (data->context) {
SDL_GL_MakeCurrent(0, NULL);
SDL_GL_DeleteContext(data->context);
Jul 19, 2006
Jul 19, 2006
868
869
870
871
872
873
}
SDL_free(data);
}
SDL_free(renderer);
}
Jul 28, 2006
Jul 28, 2006
874
#endif /* SDL_VIDEO_RENDER_OGL */
Jul 19, 2006
Jul 19, 2006
875
876
/* vi: set ts=4 sw=4 expandtab: */