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

Latest commit

 

History

History
792 lines (669 loc) · 21.9 KB

SDL_render_gles.c

File metadata and controls

792 lines (669 loc) · 21.9 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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"
Feb 8, 2011
Feb 8, 2011
24
#if SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED
Mar 13, 2011
Mar 13, 2011
26
#include "SDL_hints.h"
27
#include "SDL_opengles.h"
Feb 2, 2011
Feb 2, 2011
28
#include "../SDL_sysrender.h"
Feb 2, 2011
Feb 2, 2011
30
#if defined(SDL_VIDEO_DRIVER_PANDORA)
Jun 5, 2009
Jun 5, 2009
31
32
33
/* Empty function stub to get OpenGL ES 1.x support without */
/* OpenGL ES extension GL_OES_draw_texture supported */
May 31, 2009
May 31, 2009
34
35
36
37
38
GL_API void GL_APIENTRY
glDrawTexiOES(GLint x, GLint y, GLint z, GLint width, GLint height)
{
return;
}
Jun 5, 2009
Jun 5, 2009
39
Feb 2, 2011
Feb 2, 2011
40
#endif /* PANDORA */
May 31, 2009
May 31, 2009
41
42
43
44
45
46
/* OpenGL ES 1.1 renderer implementation, based on the OpenGL renderer */
static const float inv255f = 1.0f / 255.0f;
static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags);
Feb 2, 2011
Feb 2, 2011
47
48
static void GLES_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
49
50
static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Sep 15, 2008
Sep 15, 2008
51
52
const SDL_Rect * rect, const void *pixels,
int pitch);
53
static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Feb 3, 2011
Feb 3, 2011
54
const SDL_Rect * rect, void **pixels, int *pitch);
Sep 15, 2008
Sep 15, 2008
55
56
static void GLES_UnlockTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
Feb 15, 2011
Feb 15, 2011
57
static int GLES_UpdateViewport(SDL_Renderer * renderer);
Feb 17, 2011
Feb 17, 2011
58
static int GLES_RenderClear(SDL_Renderer * renderer);
Jan 13, 2010
Jan 13, 2010
59
60
61
62
63
static int GLES_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int GLES_RenderDrawLines(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int GLES_RenderFillRects(SDL_Renderer * renderer,
Feb 15, 2011
Feb 15, 2011
64
const SDL_Rect * rects, int count);
65
static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Sep 15, 2008
Sep 15, 2008
66
67
const SDL_Rect * srcrect,
const SDL_Rect * dstrect);
68
static void GLES_RenderPresent(SDL_Renderer * renderer);
Sep 15, 2008
Sep 15, 2008
69
70
static void GLES_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
71
72
73
static void GLES_DestroyRenderer(SDL_Renderer * renderer);
Feb 6, 2011
Feb 6, 2011
74
SDL_RenderDriver GLES_RenderDriver = {
75
76
GLES_CreateRenderer,
{
Feb 6, 2011
Feb 6, 2011
77
78
"opengles",
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
Feb 3, 2011
Feb 3, 2011
79
80
1,
{SDL_PIXELFORMAT_ABGR8888},
Sep 15, 2008
Sep 15, 2008
81
0,
82
83
84
85
86
87
0}
};
typedef struct
{
SDL_GLContext context;
Feb 20, 2011
Feb 20, 2011
88
89
90
91
92
struct {
Uint32 color;
int blendMode;
SDL_bool tex_coords;
} current;
Sep 15, 2008
Sep 15, 2008
93
94
95
SDL_bool useDrawTexture;
SDL_bool GL_OES_draw_texture_supported;
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
} GLES_RenderData;
typedef struct
{
GLuint texture;
GLenum type;
GLfloat texw;
GLfloat texh;
GLenum format;
GLenum formattype;
void *pixels;
int pitch;
} GLES_TextureData;
static void
GLES_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;
default:
error = "UNKNOWN";
break;
}
SDL_SetError("%s: %s", prefix, error);
}
Feb 15, 2011
Feb 15, 2011
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
static SDL_GLContext SDL_CurrentContext = NULL;
static int
GLES_ActivateRenderer(SDL_Renderer * renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
if (SDL_CurrentContext != data->context) {
if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
return -1;
}
SDL_CurrentContext = data->context;
GLES_UpdateViewport(renderer);
}
return 0;
}
Feb 20, 2011
Feb 20, 2011
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
/* This is called if we need to invalidate all of the SDL OpenGL state */
static void
GLES_ResetState(SDL_Renderer *renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
if (SDL_CurrentContext == data->context) {
GLES_UpdateViewport(renderer);
} else {
GLES_ActivateRenderer(renderer);
}
data->current.color = 0;
data->current.blendMode = -1;
data->current.tex_coords = SDL_FALSE;
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
188
189
190
SDL_Renderer *
GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
{
Sep 15, 2008
Sep 15, 2008
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
SDL_Renderer *renderer;
GLES_RenderData *data;
GLint value;
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) {
GLES_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
Feb 2, 2011
Feb 2, 2011
209
renderer->WindowEvent = GLES_WindowEvent;
210
211
212
213
renderer->CreateTexture = GLES_CreateTexture;
renderer->UpdateTexture = GLES_UpdateTexture;
renderer->LockTexture = GLES_LockTexture;
renderer->UnlockTexture = GLES_UnlockTexture;
Feb 15, 2011
Feb 15, 2011
214
renderer->UpdateViewport = GLES_UpdateViewport;
Feb 17, 2011
Feb 17, 2011
215
renderer->RenderClear = GLES_RenderClear;
Jan 13, 2010
Jan 13, 2010
216
217
218
renderer->RenderDrawPoints = GLES_RenderDrawPoints;
renderer->RenderDrawLines = GLES_RenderDrawLines;
renderer->RenderFillRects = GLES_RenderFillRects;
219
220
221
222
renderer->RenderCopy = GLES_RenderCopy;
renderer->RenderPresent = GLES_RenderPresent;
renderer->DestroyTexture = GLES_DestroyTexture;
renderer->DestroyRenderer = GLES_DestroyRenderer;
Feb 6, 2011
Feb 6, 2011
223
renderer->info = GLES_RenderDriver.info;
Feb 1, 2011
Feb 1, 2011
224
renderer->info.flags = SDL_RENDERER_ACCELERATED;
Feb 15, 2011
Feb 15, 2011
225
renderer->driverdata = data;
Feb 6, 2011
Feb 6, 2011
227
228
229
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
Jan 21, 2010
Jan 21, 2010
230
data->context = SDL_GL_CreateContext(window);
231
232
233
234
if (!data->context) {
GLES_DestroyRenderer(renderer);
return NULL;
}
Jan 21, 2010
Jan 21, 2010
235
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
236
237
238
239
240
241
242
243
244
245
246
247
248
GLES_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;
}
May 31, 2009
May 31, 2009
249
250
251
252
#if SDL_VIDEO_DRIVER_PANDORA
data->GL_OES_draw_texture_supported = SDL_FALSE;
data->useDrawTexture = SDL_FALSE;
#else
Sep 15, 2008
Sep 15, 2008
253
254
255
256
257
258
259
if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) {
data->GL_OES_draw_texture_supported = SDL_TRUE;
data->useDrawTexture = SDL_TRUE;
} else {
data->GL_OES_draw_texture_supported = SDL_FALSE;
data->useDrawTexture = SDL_FALSE;
}
May 31, 2009
May 31, 2009
260
#endif
Feb 6, 2011
Feb 6, 2011
262
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
263
renderer->info.max_texture_width = value;
Feb 6, 2011
Feb 6, 2011
264
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
265
266
267
renderer->info.max_texture_height = value;
/* Set up parameters for rendering */
Feb 20, 2011
Feb 20, 2011
268
GLES_ResetState(renderer);
269
270
271
272
return renderer;
}
Feb 2, 2011
Feb 2, 2011
273
274
static void
GLES_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
Feb 13, 2011
Feb 13, 2011
276
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
Feb 2, 2011
Feb 2, 2011
277
278
279
/* Rebind the context to the window area and update matrices */
SDL_CurrentContext = NULL;
}
280
281
282
283
284
285
286
287
288
289
290
291
292
}
static __inline__ int
power_of_2(int input)
{
int value = 1;
while (value < input) {
value <<= 1;
}
return value;
}
Mar 13, 2011
Mar 13, 2011
293
294
295
296
297
298
299
300
301
302
303
304
static GLenum
GetScaleQuality(void)
{
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
return GL_NEAREST;
} else {
return GL_LINEAR;
}
}
May 23, 2009
May 23, 2009
306
GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
307
308
309
310
311
{
GLES_TextureData *data;
GLint internalFormat;
GLenum format, type;
int texture_w, texture_h;
Mar 22, 2011
Mar 22, 2011
312
GLenum scaleMode;
Mar 24, 2009
Mar 24, 2009
314
Feb 2, 2011
Feb 2, 2011
315
316
GLES_ActivateRenderer(renderer);
Sep 15, 2008
Sep 15, 2008
317
318
switch (texture->format) {
case SDL_PIXELFORMAT_ABGR8888:
May 23, 2009
May 23, 2009
319
320
321
322
internalFormat = GL_RGBA;
format = GL_RGBA;
type = GL_UNSIGNED_BYTE;
break;
Sep 15, 2008
Sep 15, 2008
323
default:
Feb 6, 2011
Feb 6, 2011
324
SDL_SetError("Texture format not supported");
Sep 15, 2008
Sep 15, 2008
325
326
327
328
return -1;
}
data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data));
329
330
331
332
333
334
335
if (!data) {
SDL_OutOfMemory();
return -1;
}
if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
Feb 27, 2011
Feb 27, 2011
336
data->pixels = SDL_calloc(1, texture->h * data->pitch);
337
338
339
340
341
342
343
344
345
if (!data->pixels) {
SDL_OutOfMemory();
SDL_free(data);
return -1;
}
}
texture->driverdata = data;
Feb 6, 2011
Feb 6, 2011
346
347
348
glGetError();
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &data->texture);
Sep 15, 2008
Sep 15, 2008
349
350
351
352
353
354
355
356
data->type = GL_TEXTURE_2D;
/* no NPOV textures allowed in OpenGL ES (yet) */
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;
357
358
data->format = format;
data->formattype = type;
Mar 22, 2011
Mar 22, 2011
359
scaleMode = GetScaleQuality();
Feb 6, 2011
Feb 6, 2011
360
glBindTexture(data->type, data->texture);
Mar 22, 2011
Mar 22, 2011
361
362
glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, scaleMode);
glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, scaleMode);
Feb 20, 2011
Feb 20, 2011
363
364
glTexParameteri(data->type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(data->type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Sep 15, 2008
Sep 15, 2008
365
Feb 6, 2011
Feb 6, 2011
366
glTexImage2D(data->type, 0, internalFormat, texture_w,
Sep 15, 2008
Sep 15, 2008
367
texture_h, 0, format, type, NULL);
Feb 6, 2011
Feb 6, 2011
368
glDisable(GL_TEXTURE_2D);
Feb 6, 2011
Feb 6, 2011
370
result = glGetError();
371
372
373
374
375
376
377
378
if (result != GL_NO_ERROR) {
GLES_SetError("glTexImage2D()", result);
return -1;
}
return 0;
}
static int
May 23, 2009
May 23, 2009
379
380
GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
381
382
{
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
Feb 8, 2011
Feb 8, 2011
383
384
385
386
Uint8 *blob = NULL;
Uint8 *src;
int srcPitch;
int y;
Feb 2, 2011
Feb 2, 2011
388
389
GLES_ActivateRenderer(renderer);
Feb 8, 2011
Feb 8, 2011
390
391
392
/* Bail out if we're supposed to update an empty rectangle */
if (rect->w <= 0 || rect->h <= 0)
return 0;
Jan 20, 2011
Jan 20, 2011
393
Feb 8, 2011
Feb 8, 2011
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* Reformat the texture data into a tightly packed array */
srcPitch = rect->w * SDL_BYTESPERPIXEL(texture->format);
src = (Uint8 *)pixels;
if (pitch != srcPitch)
{
blob = (Uint8 *)SDL_malloc(srcPitch * rect->h);
if (!blob)
{
SDL_OutOfMemory();
return -1;
}
src = blob;
for (y = 0; y < rect->h; ++y)
{
SDL_memcpy(src, pixels, srcPitch);
src += srcPitch;
pixels = (Uint8 *)pixels + pitch;
}
src = blob;
Jan 20, 2011
Jan 20, 2011
413
414
}
Feb 8, 2011
Feb 8, 2011
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* Create a texture subimage with the supplied data */
glGetError();
glEnable(data->type);
glBindTexture(data->type, data->texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(data->type,
0,
rect->x,
rect->y,
rect->w,
rect->h,
data->format,
data->formattype,
src);
if (blob) {
SDL_free(blob);
Jan 20, 2011
Jan 20, 2011
431
432
}
Feb 8, 2011
Feb 8, 2011
433
434
435
if (glGetError() != GL_NO_ERROR)
{
SDL_SetError("Failed to update texture");
436
437
438
439
440
441
442
return -1;
}
return 0;
}
static int
GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Feb 3, 2011
Feb 3, 2011
443
const SDL_Rect * rect, void **pixels, int *pitch)
444
445
446
447
448
449
450
451
452
453
454
455
456
457
{
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
return 0;
}
static void
GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
Feb 8, 2011
Feb 8, 2011
458
459
460
461
462
463
464
465
SDL_Rect rect;
/* We do whole texture updates, at least for now */
rect.x = 0;
rect.y = 0;
rect.w = texture->w;
rect.h = texture->h;
GLES_UpdateTexture(renderer, texture, &rect, data->pixels, data->pitch);
Feb 15, 2011
Feb 15, 2011
468
469
static int
GLES_UpdateViewport(SDL_Renderer * renderer)
Feb 8, 2011
Feb 8, 2011
470
{
Feb 15, 2011
Feb 15, 2011
471
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Feb 8, 2011
Feb 8, 2011
472
Feb 15, 2011
Feb 15, 2011
473
474
475
if (SDL_CurrentContext != data->context) {
/* We'll update the viewport after we rebind the context */
return 0;
Feb 8, 2011
Feb 8, 2011
476
}
Feb 15, 2011
Feb 15, 2011
477
478
479
480
481
482
483
484
485
486
487
glViewport(renderer->viewport.x, renderer->viewport.y,
renderer->viewport.w, renderer->viewport.h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof((GLfloat) 0,
(GLfloat) renderer->viewport.w,
(GLfloat) renderer->viewport.h,
(GLfloat) 0, 0.0, 1.0);
return 0;
Feb 8, 2011
Feb 8, 2011
488
489
}
Feb 20, 2011
Feb 20, 2011
490
491
static void
GLES_SetColor(GLES_RenderData * data, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Feb 17, 2011
Feb 17, 2011
492
{
Feb 20, 2011
Feb 20, 2011
493
494
495
496
497
498
499
500
501
Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
if (color != data->current.color) {
glColor4f((GLfloat) r * inv255f,
(GLfloat) g * inv255f,
(GLfloat) b * inv255f,
(GLfloat) a * inv255f);
data->current.color = color;
}
Feb 17, 2011
Feb 17, 2011
502
503
}
Dec 31, 2008
Dec 31, 2008
504
static void
Feb 1, 2011
Feb 1, 2011
505
GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
Dec 31, 2008
Dec 31, 2008
506
{
Feb 20, 2011
Feb 20, 2011
507
if (blendMode != data->current.blendMode) {
Dec 31, 2008
Dec 31, 2008
508
509
switch (blendMode) {
case SDL_BLENDMODE_NONE:
Feb 6, 2011
Feb 6, 2011
510
511
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glDisable(GL_BLEND);
Dec 31, 2008
Dec 31, 2008
512
513
break;
case SDL_BLENDMODE_BLEND:
Feb 6, 2011
Feb 6, 2011
514
515
516
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Dec 31, 2008
Dec 31, 2008
517
518
break;
case SDL_BLENDMODE_ADD:
Feb 6, 2011
Feb 6, 2011
519
520
521
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
Dec 31, 2008
Dec 31, 2008
522
break;
Feb 5, 2011
Feb 5, 2011
523
case SDL_BLENDMODE_MOD:
Feb 6, 2011
Feb 6, 2011
524
525
526
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
Feb 5, 2011
Feb 5, 2011
527
break;
Dec 31, 2008
Dec 31, 2008
528
}
Feb 20, 2011
Feb 20, 2011
529
530
531
532
533
534
535
536
537
538
539
540
541
542
data->current.blendMode = blendMode;
}
}
static void
GLES_SetTexCoords(GLES_RenderData * data, SDL_bool enabled)
{
if (enabled != data->current.tex_coords) {
if (enabled) {
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
} else {
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
data->current.tex_coords = enabled;
Dec 31, 2008
Dec 31, 2008
543
544
545
}
}
Feb 20, 2011
Feb 20, 2011
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
static void
GLES_SetDrawingState(SDL_Renderer * renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_ActivateRenderer(renderer);
GLES_SetColor(data, (GLfloat) renderer->r,
(GLfloat) renderer->g,
(GLfloat) renderer->b,
(GLfloat) renderer->a);
GLES_SetBlendMode(data, renderer->blendMode);
GLES_SetTexCoords(data, SDL_FALSE);
}
static int
GLES_RenderClear(SDL_Renderer * renderer)
{
GLES_ActivateRenderer(renderer);
glClearColor((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
glClear(GL_COLOR_BUFFER_BIT);
return 0;
}
Jan 13, 2010
Jan 13, 2010
579
580
GLES_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 31, 2008
Dec 31, 2008
582
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Dec 12, 2009
Dec 12, 2009
583
584
int i;
GLshort *vertices;
Dec 31, 2008
Dec 31, 2008
585
Feb 20, 2011
Feb 20, 2011
586
GLES_SetDrawingState(renderer);
Dec 31, 2008
Dec 31, 2008
587
Dec 12, 2009
Dec 12, 2009
588
589
590
591
592
vertices = SDL_stack_alloc(GLshort, count*2);
for (i = 0; i < count; ++i) {
vertices[2*i+0] = (GLshort)points[i].x;
vertices[2*i+1] = (GLshort)points[i].y;
}
Feb 6, 2011
Feb 6, 2011
593
594
glVertexPointer(2, GL_SHORT, 0, vertices);
glDrawArrays(GL_POINTS, 0, count);
Dec 12, 2009
Dec 12, 2009
595
SDL_stack_free(vertices);
Jan 2, 2009
Jan 2, 2009
596
Dec 31, 2008
Dec 31, 2008
597
598
return 0;
}
Sep 15, 2008
Sep 15, 2008
599
Dec 31, 2008
Dec 31, 2008
600
static int
Jan 13, 2010
Jan 13, 2010
601
602
GLES_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 31, 2008
Dec 31, 2008
603
{
Sep 15, 2008
Sep 15, 2008
604
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Dec 12, 2009
Dec 12, 2009
605
606
int i;
GLshort *vertices;
Sep 15, 2008
Sep 15, 2008
607
Feb 20, 2011
Feb 20, 2011
608
GLES_SetDrawingState(renderer);
Dec 31, 2008
Dec 31, 2008
609
Dec 12, 2009
Dec 12, 2009
610
611
612
613
614
vertices = SDL_stack_alloc(GLshort, count*2);
for (i = 0; i < count; ++i) {
vertices[2*i+0] = (GLshort)points[i].x;
vertices[2*i+1] = (GLshort)points[i].y;
}
Feb 6, 2011
Feb 6, 2011
615
glVertexPointer(2, GL_SHORT, 0, vertices);
Dec 9, 2009
Dec 9, 2009
616
617
618
619
if (count > 2 &&
points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
/* GL_LINE_LOOP takes care of the final segment */
--count;
Feb 6, 2011
Feb 6, 2011
620
glDrawArrays(GL_LINE_LOOP, 0, count);
Dec 9, 2009
Dec 9, 2009
621
} else {
Feb 6, 2011
Feb 6, 2011
622
glDrawArrays(GL_LINE_STRIP, 0, count);
Dec 9, 2009
Dec 9, 2009
623
}
Dec 12, 2009
Dec 12, 2009
624
SDL_stack_free(vertices);
Jan 1, 2009
Jan 1, 2009
625
Dec 31, 2008
Dec 31, 2008
626
627
return 0;
}
Sep 15, 2008
Sep 15, 2008
628
Jan 13, 2010
Jan 13, 2010
629
static int
Feb 15, 2011
Feb 15, 2011
630
GLES_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects,
Jan 13, 2010
Jan 13, 2010
631
int count)
Dec 31, 2008
Dec 31, 2008
632
633
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
Dec 9, 2009
Dec 9, 2009
634
int i;
Sep 15, 2008
Sep 15, 2008
635
Feb 20, 2011
Feb 20, 2011
636
GLES_SetDrawingState(renderer);
Sep 15, 2008
Sep 15, 2008
637
Dec 9, 2009
Dec 9, 2009
638
for (i = 0; i < count; ++i) {
Feb 15, 2011
Feb 15, 2011
639
const SDL_Rect *rect = &rects[i];
Dec 9, 2009
Dec 9, 2009
640
641
642
643
644
645
646
647
648
649
650
651
652
653
GLshort minx = rect->x;
GLshort maxx = rect->x + rect->w;
GLshort miny = rect->y;
GLshort maxy = rect->y + rect->h;
GLshort vertices[8];
vertices[0] = minx;
vertices[1] = miny;
vertices[2] = maxx;
vertices[3] = miny;
vertices[4] = minx;
vertices[5] = maxy;
vertices[6] = maxx;
vertices[7] = maxy;
Feb 6, 2011
Feb 6, 2011
654
655
glVertexPointer(2, GL_SHORT, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Dec 9, 2009
Dec 9, 2009
656
}
Jan 1, 2009
Jan 1, 2009
657
Sep 15, 2008
Sep 15, 2008
658
return 0;
659
660
661
662
}
static int
GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Sep 15, 2008
Sep 15, 2008
663
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Sep 15, 2008
Sep 15, 2008
665
666
667
668
669
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata;
int minx, miny, maxx, maxy;
GLfloat minu, maxu, minv, maxv;
Sep 15, 2008
Sep 15, 2008
670
Feb 2, 2011
Feb 2, 2011
671
672
GLES_ActivateRenderer(renderer);
Feb 6, 2011
Feb 6, 2011
673
glEnable(GL_TEXTURE_2D);
May 14, 2009
May 14, 2009
674
Feb 6, 2011
Feb 6, 2011
675
glBindTexture(texturedata->type, texturedata->texture);
Sep 15, 2008
Sep 15, 2008
676
677
if (texture->modMode) {
Feb 20, 2011
Feb 20, 2011
678
GLES_SetColor(data, texture->r, texture->g, texture->b, texture->a);
Feb 20, 2011
Feb 20, 2011
680
GLES_SetColor(data, 255, 255, 255, 255);
Feb 1, 2011
Feb 1, 2011
683
GLES_SetBlendMode(data, texture->blendMode);
Feb 20, 2011
Feb 20, 2011
685
686
GLES_SetTexCoords(data, SDL_TRUE);
Sep 15, 2008
Sep 15, 2008
687
688
689
if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
/* this code is a little funny because the viewport is upside down vs SDL's coordinate system */
GLint cropRect[4];
Feb 2, 2011
Feb 2, 2011
690
691
692
693
int w, h;
SDL_Window *window = renderer->window;
SDL_GetWindowSize(window, &w, &h);
Sep 15, 2008
Sep 15, 2008
694
695
696
697
cropRect[0] = srcrect->x;
cropRect[1] = srcrect->y + srcrect->h;
cropRect[2] = srcrect->w;
cropRect[3] = -srcrect->h;
Feb 6, 2011
Feb 6, 2011
698
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
Sep 15, 2008
Sep 15, 2008
699
cropRect);
Feb 6, 2011
Feb 6, 2011
700
glDrawTexiOES(dstrect->x, h - dstrect->y - dstrect->h, 0,
Feb 2, 2011
Feb 2, 2011
701
dstrect->w, dstrect->h);
Sep 15, 2008
Sep 15, 2008
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
} else {
minx = dstrect->x;
miny = dstrect->y;
maxx = dstrect->x + dstrect->w;
maxy = dstrect->y + dstrect->h;
minu = (GLfloat) srcrect->x / texture->w;
minu *= texturedata->texw;
maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w;
maxu *= texturedata->texw;
minv = (GLfloat) srcrect->y / texture->h;
minv *= texturedata->texh;
maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
maxv *= texturedata->texh;
GLshort vertices[8];
GLfloat texCoords[8];
vertices[0] = minx;
vertices[1] = miny;
vertices[2] = maxx;
vertices[3] = miny;
vertices[4] = minx;
vertices[5] = maxy;
vertices[6] = maxx;
vertices[7] = maxy;
texCoords[0] = minu;
texCoords[1] = minv;
texCoords[2] = maxu;
texCoords[3] = minv;
texCoords[4] = minu;
texCoords[5] = maxv;
texCoords[6] = maxu;
texCoords[7] = maxv;
Feb 6, 2011
Feb 6, 2011
739
740
741
glVertexPointer(2, GL_SHORT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
Sep 15, 2008
Sep 15, 2008
742
}
Feb 6, 2011
Feb 6, 2011
743
glDisable(GL_TEXTURE_2D);
May 14, 2009
May 14, 2009
744
745
746
747
748
return 0;
}
static void
May 23, 2009
May 23, 2009
749
GLES_RenderPresent(SDL_Renderer * renderer)
Feb 2, 2011
Feb 2, 2011
751
752
GLES_ActivateRenderer(renderer);
753
754
755
756
757
758
759
760
SDL_GL_SwapWindow(renderer->window);
}
static void
GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
Feb 2, 2011
Feb 2, 2011
761
762
GLES_ActivateRenderer(renderer);
763
764
765
766
if (!data) {
return;
}
if (data->texture) {
Sep 15, 2008
Sep 15, 2008
767
glDeleteTextures(1, &data->texture);
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
}
if (data->pixels) {
SDL_free(data->pixels);
}
SDL_free(data);
texture->driverdata = NULL;
}
static void
GLES_DestroyRenderer(SDL_Renderer * renderer)
{
GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
if (data) {
if (data->context) {
SDL_GL_DeleteContext(data->context);
}
SDL_free(data);
}
SDL_free(renderer);
}
Feb 8, 2011
Feb 8, 2011
790
#endif /* SDL_VIDEO_RENDER_OGL_ES && !SDL_RENDER_DISABLED */
791
792
/* vi: set ts=4 sw=4 expandtab: */