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

Latest commit

 

History

History
651 lines (580 loc) · 19.1 KB

SDL_renderer_gl.c

File metadata and controls

651 lines (580 loc) · 19.1 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
24
/*
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"
#if SDL_VIDEO_OPENGL
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
35
/* OpenGL renderer implementation */
static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
Jul 22, 2006
Jul 22, 2006
36
static int GL_ActivateRenderer(SDL_Renderer * renderer);
Jul 19, 2006
Jul 19, 2006
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
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);
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,
const SDL_Rect * rect, int markDirty,
void **pixels, int *pitch);
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);
static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 color);
static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect,
int blendMode, int scaleMode);
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",
(SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync |
SDL_Renderer_Accelerated),
(SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
SDL_TextureBlendMode_Blend | SDL_TextureBlendMode_Add |
SDL_TextureBlendMode_Mod),
(SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
Jul 22, 2006
Jul 22, 2006
74
SDL_TextureScaleMode_Slow),
Jul 22, 2006
Jul 22, 2006
75
16,
Jul 19, 2006
Jul 19, 2006
76
{
Jul 22, 2006
Jul 22, 2006
77
78
SDL_PixelFormat_Index1LSB,
SDL_PixelFormat_Index1MSB,
Jul 19, 2006
Jul 19, 2006
79
80
81
82
83
84
85
SDL_PixelFormat_Index8,
SDL_PixelFormat_RGB332,
SDL_PixelFormat_RGB444,
SDL_PixelFormat_RGB555,
SDL_PixelFormat_ARGB4444,
SDL_PixelFormat_ARGB1555,
SDL_PixelFormat_RGB565,
Jul 22, 2006
Jul 22, 2006
86
87
SDL_PixelFormat_RGB24,
SDL_PixelFormat_BGR24,
Jul 19, 2006
Jul 19, 2006
88
SDL_PixelFormat_RGB888,
Jul 22, 2006
Jul 22, 2006
89
SDL_PixelFormat_BGR888,
Jul 19, 2006
Jul 19, 2006
90
SDL_PixelFormat_ARGB8888,
Jul 22, 2006
Jul 22, 2006
91
SDL_PixelFormat_ABGR8888,
Jul 22, 2006
Jul 22, 2006
92
SDL_PixelFormat_ARGB2101010},
Jul 19, 2006
Jul 19, 2006
93
94
95
96
97
98
99
100
101
102
103
104
0,
0}
};
typedef struct
{
SDL_GLContext context;
} GL_RenderData;
typedef struct
{
GLuint texture;
Jul 22, 2006
Jul 22, 2006
105
GLenum type;
Jul 19, 2006
Jul 19, 2006
106
107
GLfloat texw;
GLfloat texh;
Jul 22, 2006
Jul 22, 2006
108
109
GLenum format;
GLenum formattype;
Jul 19, 2006
Jul 19, 2006
110
111
void *pixels;
int pitch;
Jul 22, 2006
Jul 22, 2006
112
SDL_DirtyRectList dirty;
Jul 19, 2006
Jul 19, 2006
113
114
115
} GL_TextureData;
Jul 22, 2006
Jul 22, 2006
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
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 19, 2006
Jul 19, 2006
153
154
155
void
GL_AddRenderDriver(_THIS)
{
Jul 22, 2006
Jul 22, 2006
156
if (_this->GL_CreateContext) {
Jul 19, 2006
Jul 19, 2006
157
158
159
160
161
162
163
164
165
166
167
SDL_AddRenderDriver(0, &GL_RenderDriver);
}
}
SDL_Renderer *
GL_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer;
GL_RenderData *data;
if (!(window->flags & SDL_WINDOW_OPENGL)) {
Jul 22, 2006
Jul 22, 2006
168
169
170
171
window->flags |= SDL_WINDOW_OPENGL;
if (SDL_RecreateWindow(window) < 0) {
return NULL;
}
Jul 19, 2006
Jul 19, 2006
172
173
}
Jul 22, 2006
Jul 22, 2006
174
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
Jul 19, 2006
Jul 19, 2006
175
176
177
178
179
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Jul 22, 2006
Jul 22, 2006
180
data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
181
182
183
184
185
186
if (!data) {
GL_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
Jul 22, 2006
Jul 22, 2006
187
renderer->ActivateRenderer = GL_ActivateRenderer;
Jul 19, 2006
Jul 19, 2006
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
renderer->CreateTexture = GL_CreateTexture;
renderer->SetTexturePalette = GL_SetTexturePalette;
renderer->GetTexturePalette = GL_GetTexturePalette;
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 =
(SDL_Renderer_PresentDiscard | SDL_Renderer_Accelerated);
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;
}
if (flags & SDL_Renderer_PresentVSync) {
SDL_GL_SetSwapInterval(1);
} else {
SDL_GL_SetSwapInterval(0);
}
if (SDL_GL_GetSwapInterval() > 0) {
renderer->info.flags |= SDL_Renderer_PresentVSync;
}
Jul 22, 2006
Jul 22, 2006
226
227
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &renderer->info.max_texture_width);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &renderer->info.max_texture_height);
Jul 22, 2006
Jul 22, 2006
228
229
230
/* FIXME: Check for GL_ARB_texture_rectangle and GL_EXT_texture_rectangle */
Jul 19, 2006
Jul 19, 2006
231
232
233
/* Set up parameters for rendering */
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
Jul 22, 2006
Jul 22, 2006
234
#ifdef USE_GL_TEXTURE_RECTANGLE
Jul 22, 2006
Jul 22, 2006
235
glEnable(GL_TEXTURE_RECTANGLE_ARB);
Jul 22, 2006
Jul 22, 2006
236
237
238
#else
glEnable(GL_TEXTURE_2D);
#endif
Jul 19, 2006
Jul 19, 2006
239
240
241
242
243
244
245
246
247
248
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, window->w, window->h);
glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0, 0.0, 1.0);
return renderer;
}
Jul 22, 2006
Jul 22, 2006
249
250
251
252
253
254
255
256
257
static int
GL_ActivateRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
return SDL_GL_MakeCurrent(window->id, data->context);
}
Jul 22, 2006
Jul 22, 2006
258
259
260
261
262
263
264
265
266
267
268
static __inline__ int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
Jul 19, 2006
Jul 19, 2006
269
270
271
272
273
274
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
275
276
GLint internalFormat;
GLenum format, type;
Jul 22, 2006
Jul 22, 2006
277
int texture_w, texture_h;
Jul 22, 2006
Jul 22, 2006
278
GLenum result;
Jul 22, 2006
Jul 22, 2006
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
326
327
328
switch (texture->format) {
case SDL_PixelFormat_Index1LSB:
case SDL_PixelFormat_Index1MSB:
internalFormat = GL_RGB;
format = GL_COLOR_INDEX;
type = GL_BITMAP;
break;
case SDL_PixelFormat_Index8:
internalFormat = GL_RGB;
format = GL_COLOR_INDEX;
type = GL_UNSIGNED_BYTE;
break;
case SDL_PixelFormat_RGB332:
internalFormat = GL_R3_G3_B2;
format = GL_RGB;
type = GL_UNSIGNED_BYTE_3_3_2;
break;
case SDL_PixelFormat_RGB444:
internalFormat = GL_RGB4;
format = GL_RGB;
type = GL_UNSIGNED_SHORT_4_4_4_4;
break;
case SDL_PixelFormat_RGB555:
internalFormat = GL_RGB5;
format = GL_RGB;
type = GL_UNSIGNED_SHORT_5_5_5_1;
break;
case SDL_PixelFormat_ARGB4444:
internalFormat = GL_RGBA4;
format = GL_BGRA;
type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
break;
case SDL_PixelFormat_ARGB1555:
internalFormat = GL_RGB5_A1;
format = GL_BGRA;
type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
break;
case SDL_PixelFormat_RGB565:
internalFormat = GL_RGB8;
format = GL_RGB;
type = GL_UNSIGNED_SHORT_5_6_5;
break;
case SDL_PixelFormat_RGB24:
internalFormat = GL_RGB8;
format = GL_RGB;
type = GL_UNSIGNED_BYTE;
break;
case SDL_PixelFormat_RGB888:
internalFormat = GL_RGB8;
Jul 22, 2006
Jul 22, 2006
329
330
format = GL_BGRA;
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
331
332
333
334
335
336
337
338
break;
case SDL_PixelFormat_BGR24:
internalFormat = GL_RGB8;
format = GL_BGR;
type = GL_UNSIGNED_BYTE;
break;
case SDL_PixelFormat_BGR888:
internalFormat = GL_RGB8;
Jul 22, 2006
Jul 22, 2006
339
340
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
341
342
343
344
break;
case SDL_PixelFormat_ARGB8888:
internalFormat = GL_RGBA8;
format = GL_BGRA;
Jul 22, 2006
Jul 22, 2006
345
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
346
347
348
349
break;
case SDL_PixelFormat_ABGR8888:
internalFormat = GL_RGBA8;
format = GL_RGBA;
Jul 22, 2006
Jul 22, 2006
350
type = GL_UNSIGNED_BYTE;
Jul 22, 2006
Jul 22, 2006
351
352
353
354
355
356
357
358
359
360
break;
case SDL_PixelFormat_ARGB2101010:
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
361
Jul 22, 2006
Jul 22, 2006
362
data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
363
364
365
366
367
368
369
if (!data) {
SDL_OutOfMemory();
return -1;
}
texture->driverdata = data;
Jul 22, 2006
Jul 22, 2006
370
glGetError();
Jul 22, 2006
Jul 22, 2006
371
glGenTextures(1, &data->texture);
Jul 22, 2006
Jul 22, 2006
372
#ifdef USE_GL_TEXTURE_RECTANGLE
Jul 22, 2006
Jul 22, 2006
373
data->type = GL_TEXTURE_RECTANGLE_ARB;
Jul 22, 2006
Jul 22, 2006
374
375
texture_w = texture->w;
texture_h = texture->h;
Jul 22, 2006
Jul 22, 2006
376
377
data->texw = (GLfloat) texture->w;
data->texh = (GLfloat) texture->h;
Jul 22, 2006
Jul 22, 2006
378
379
380
381
382
383
384
#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;
#endif
Jul 22, 2006
Jul 22, 2006
385
386
387
data->format = format;
data->formattype = type;
glBindTexture(data->type, data->texture);
Jul 22, 2006
Jul 22, 2006
388
glTexImage2D(data->type, 0, internalFormat, texture_w, texture_h, 0,
Jul 22, 2006
Jul 22, 2006
389
format, type, NULL);
Jul 22, 2006
Jul 22, 2006
390
391
392
393
394
result = glGetError();
if (result != GL_NO_ERROR) {
GL_SetError("glTexImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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;
return 0;
}
static int
GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
SDL_Color * colors, int firstcolor, int ncolors)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
return 0;
}
Jul 22, 2006
Jul 22, 2006
417
418
419
420
421
422
423
424
425
426
427
428
429
static void
SetupTextureUpdate(SDL_Texture * texture, int pitch)
{
if (texture->format == SDL_PixelFormat_Index1LSB) {
glPixelStorei(GL_UNPACK_LSB_FIRST, 1);
} else if (texture->format == SDL_PixelFormat_Index1MSB) {
glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH,
pitch / SDL_BYTESPERPIXEL(texture->format));
}
Jul 19, 2006
Jul 19, 2006
430
431
432
433
434
static int
GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
Jul 22, 2006
Jul 22, 2006
435
GLenum result;
Jul 19, 2006
Jul 19, 2006
436
Jul 22, 2006
Jul 22, 2006
437
438
glGetError();
SetupTextureUpdate(texture, pitch);
Jul 22, 2006
Jul 22, 2006
439
440
441
glBindTexture(data->type, data->texture);
glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, rect->h,
data->format, data->formattype, pixels);
Jul 22, 2006
Jul 22, 2006
442
443
444
445
446
result = glGetError();
if (result != GL_NO_ERROR) {
GL_SetError("glTexSubImage2D()", result);
return -1;
}
Jul 19, 2006
Jul 19, 2006
447
448
449
450
451
452
453
454
455
456
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
457
458
459
460
461
462
463
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
464
465
}
Jul 22, 2006
Jul 22, 2006
466
467
if (markDirty) {
SDL_AddDirtyRect(&data->dirty, rect);
Jul 19, 2006
Jul 19, 2006
468
}
Jul 22, 2006
Jul 22, 2006
469
470
471
472
473
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
Jul 19, 2006
Jul 19, 2006
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
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
490
SDL_AddDirtyRect(&data->dirty, &rects[i]);
Jul 19, 2006
Jul 19, 2006
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
}
}
static int
GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
GLclampf r, g, b, a;
a = ((GLclampf) ((color >> 24) & 0xFF)) / 255.0f;
r = ((GLclampf) ((color >> 16) & 0xFF)) / 255.0f;
g = ((GLclampf) ((color >> 8) & 0xFF)) / 255.0f;
b = ((GLclampf) (color & 0xFF)) / 255.0f;
glClearColor(r, g, b, a);
glViewport(rect->x, window->h - rect->y, rect->w, rect->h);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, window->w, window->h);
return 0;
}
static int
GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_Rect * dstrect,
int blendMode, int scaleMode)
{
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
523
524
525
526
527
528
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
529
SetupTextureUpdate(texture, pitch);
Jul 22, 2006
Jul 22, 2006
530
531
532
533
534
535
536
537
538
539
540
541
542
glBindTexture(texturedata->type, texturedata->texture);
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);
glTexSubImage2D(texturedata->type, 0, rect->x, rect->y, rect->w,
rect->h, texturedata->format,
texturedata->formattype, pixels);
}
SDL_ClearDirtyRects(&texturedata->dirty);
}
Jul 19, 2006
Jul 19, 2006
543
544
545
546
547
548
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
549
minu *= texturedata->texw;
Jul 19, 2006
Jul 19, 2006
550
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
Jul 22, 2006
Jul 22, 2006
551
maxu *= texturedata->texw;
Jul 19, 2006
Jul 19, 2006
552
minv = (GLfloat) srcrect->y / texture->h;
Jul 22, 2006
Jul 22, 2006
553
minv *= texturedata->texh;
Jul 19, 2006
Jul 19, 2006
554
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
Jul 22, 2006
Jul 22, 2006
555
maxv *= texturedata->texh;
Jul 19, 2006
Jul 19, 2006
556
Jul 22, 2006
Jul 22, 2006
557
glBindTexture(texturedata->type, texturedata->texture);
Jul 19, 2006
Jul 19, 2006
558
559
560
switch (blendMode) {
case SDL_TextureBlendMode_None:
Jul 22, 2006
Jul 22, 2006
561
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
Jul 19, 2006
Jul 19, 2006
562
563
564
565
glDisable(GL_BLEND);
break;
case SDL_TextureBlendMode_Mask:
case SDL_TextureBlendMode_Blend:
Jul 22, 2006
Jul 22, 2006
566
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Jul 19, 2006
Jul 19, 2006
567
568
569
570
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
break;
case SDL_TextureBlendMode_Add:
Jul 22, 2006
Jul 22, 2006
571
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Jul 19, 2006
Jul 19, 2006
572
573
574
575
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
break;
case SDL_TextureBlendMode_Mod:
Jul 22, 2006
Jul 22, 2006
576
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Jul 19, 2006
Jul 19, 2006
577
578
579
580
581
582
583
584
glEnable(GL_BLEND);
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
break;
}
switch (scaleMode) {
case SDL_TextureScaleMode_None:
case SDL_TextureScaleMode_Fast:
Jul 22, 2006
Jul 22, 2006
585
586
glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Jul 19, 2006
Jul 19, 2006
587
588
break;
case SDL_TextureScaleMode_Slow:
Jul 22, 2006
Jul 22, 2006
589
case SDL_TextureScaleMode_Best:
Jul 22, 2006
Jul 22, 2006
590
591
glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Jul 19, 2006
Jul 19, 2006
592
593
594
595
596
597
598
599
600
break;
}
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(minu, minv);
glVertex2i(minx, miny);
glTexCoord2f(maxu, minv);
glVertex2i(maxx, miny);
glTexCoord2f(minu, maxv);
Jul 22, 2006
Jul 22, 2006
601
glVertex2i(minx, maxy);
Jul 19, 2006
Jul 19, 2006
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
glTexCoord2f(maxu, maxv);
glVertex2i(maxx, maxy);
glEnd();
return 0;
}
static void
GL_RenderPresent(SDL_Renderer * renderer)
{
SDL_GL_SwapWindow(renderer->window);
}
static void
GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GL_TextureData *data = (GL_TextureData *) texture->driverdata;
if (!data) {
return;
}
if (data->texture) {
Jul 22, 2006
Jul 22, 2006
624
625
626
627
glDeleteTextures(1, &data->texture);
}
if (data->pixels) {
SDL_free(data->pixels);
Jul 19, 2006
Jul 19, 2006
628
}
Jul 22, 2006
Jul 22, 2006
629
SDL_FreeDirtyRects(&data->dirty);
Jul 19, 2006
Jul 19, 2006
630
631
632
633
634
635
636
637
638
639
SDL_free(data);
texture->driverdata = NULL;
}
void
GL_DestroyRenderer(SDL_Renderer * renderer)
{
GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
if (data) {
Jul 22, 2006
Jul 22, 2006
640
641
642
if (data->context) {
SDL_GL_MakeCurrent(0, NULL);
SDL_GL_DeleteContext(data->context);
Jul 19, 2006
Jul 19, 2006
643
644
645
646
647
648
649
650
651
}
SDL_free(data);
}
SDL_free(renderer);
}
#endif /* SDL_VIDEO_OPENGL */
/* vi: set ts=4 sw=4 expandtab: */