Mercurial > sdl-ios-xcode
comparison src/video/SDL_renderer_gles.c @ 2753:0969758c8809
indent
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 15 Sep 2008 04:32:36 +0000 |
parents | 68862734a5fd |
children | 99210400e8b9 |
comparison
equal
deleted
inserted
replaced
2752:edd2839b36f7 | 2753:0969758c8809 |
---|---|
37 static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags); | 37 static SDL_Renderer *GLES_CreateRenderer(SDL_Window * window, Uint32 flags); |
38 static int GLES_ActivateRenderer(SDL_Renderer * renderer); | 38 static int GLES_ActivateRenderer(SDL_Renderer * renderer); |
39 static int GLES_DisplayModeChanged(SDL_Renderer * renderer); | 39 static int GLES_DisplayModeChanged(SDL_Renderer * renderer); |
40 static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); | 40 static int GLES_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); |
41 static int GLES_QueryTexturePixels(SDL_Renderer * renderer, | 41 static int GLES_QueryTexturePixels(SDL_Renderer * renderer, |
42 SDL_Texture * texture, void **pixels, | 42 SDL_Texture * texture, void **pixels, |
43 int *pitch); | 43 int *pitch); |
44 static int GLES_SetTexturePalette(SDL_Renderer * renderer, | 44 static int GLES_SetTexturePalette(SDL_Renderer * renderer, |
45 SDL_Texture * texture, | 45 SDL_Texture * texture, |
46 const SDL_Color * colors, int firstcolor, | 46 const SDL_Color * colors, int firstcolor, |
47 int ncolors); | 47 int ncolors); |
48 static int GLES_GetTexturePalette(SDL_Renderer * renderer, | 48 static int GLES_GetTexturePalette(SDL_Renderer * renderer, |
49 SDL_Texture * texture, SDL_Color * colors, | 49 SDL_Texture * texture, SDL_Color * colors, |
50 int firstcolor, int ncolors); | 50 int firstcolor, int ncolors); |
51 static int GLES_SetTextureColorMod(SDL_Renderer * renderer, | 51 static int GLES_SetTextureColorMod(SDL_Renderer * renderer, |
52 SDL_Texture * texture); | 52 SDL_Texture * texture); |
53 static int GLES_SetTextureAlphaMod(SDL_Renderer * renderer, | 53 static int GLES_SetTextureAlphaMod(SDL_Renderer * renderer, |
54 SDL_Texture * texture); | 54 SDL_Texture * texture); |
55 static int GLES_SetTextureBlendMode(SDL_Renderer * renderer, | 55 static int GLES_SetTextureBlendMode(SDL_Renderer * renderer, |
56 SDL_Texture * texture); | 56 SDL_Texture * texture); |
57 static int GLES_SetTextureScaleMode(SDL_Renderer * renderer, | 57 static int GLES_SetTextureScaleMode(SDL_Renderer * renderer, |
58 SDL_Texture * texture); | 58 SDL_Texture * texture); |
59 static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, | 59 static int GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
60 const SDL_Rect * rect, const void *pixels, | 60 const SDL_Rect * rect, const void *pixels, |
61 int pitch); | 61 int pitch); |
62 static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, | 62 static int GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
63 const SDL_Rect * rect, int markDirty, void **pixels, | 63 const SDL_Rect * rect, int markDirty, |
64 int *pitch); | 64 void **pixels, int *pitch); |
65 static void GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture); | 65 static void GLES_UnlockTexture(SDL_Renderer * renderer, |
66 SDL_Texture * texture); | |
66 static void GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, | 67 static void GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
67 int numrects, const SDL_Rect * rects); | 68 int numrects, const SDL_Rect * rects); |
68 static int GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, | 69 static int GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, |
69 Uint8 a, const SDL_Rect * rect); | 70 Uint8 a, const SDL_Rect * rect); |
70 static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, | 71 static int GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, |
71 const SDL_Rect * srcrect, const SDL_Rect * dstrect); | 72 const SDL_Rect * srcrect, |
73 const SDL_Rect * dstrect); | |
72 static void GLES_RenderPresent(SDL_Renderer * renderer); | 74 static void GLES_RenderPresent(SDL_Renderer * renderer); |
73 static void GLES_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture); | 75 static void GLES_DestroyTexture(SDL_Renderer * renderer, |
76 SDL_Texture * texture); | |
74 static void GLES_DestroyRenderer(SDL_Renderer * renderer); | 77 static void GLES_DestroyRenderer(SDL_Renderer * renderer); |
75 | 78 |
76 | 79 |
77 SDL_RenderDriver GL_ES_RenderDriver = { | 80 SDL_RenderDriver GL_ES_RenderDriver = { |
78 GLES_CreateRenderer, | 81 GLES_CreateRenderer, |
85 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK | | 88 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK | |
86 SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD | | 89 SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD | |
87 SDL_TEXTUREBLENDMODE_MOD), | 90 SDL_TEXTUREBLENDMODE_MOD), |
88 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST | | 91 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST | |
89 SDL_TEXTURESCALEMODE_SLOW), 2, | 92 SDL_TEXTURESCALEMODE_SLOW), 2, |
90 { | 93 { |
91 SDL_PIXELFORMAT_RGB24, | 94 SDL_PIXELFORMAT_RGB24, |
92 SDL_PIXELFORMAT_ABGR8888, | 95 SDL_PIXELFORMAT_ABGR8888, |
93 }, | 96 }, |
94 0, | 97 0, |
95 0} | 98 0} |
96 }; | 99 }; |
97 | 100 |
98 typedef struct | 101 typedef struct |
99 { | 102 { |
100 SDL_GLContext context; | 103 SDL_GLContext context; |
101 SDL_bool updateSize; | 104 SDL_bool updateSize; |
102 int blendMode; | 105 int blendMode; |
103 | 106 |
104 #ifndef APIENTRY | 107 #ifndef APIENTRY |
105 #define APIENTRY | 108 #define APIENTRY |
106 #endif | 109 #endif |
107 | 110 |
108 SDL_bool useDrawTexture; | 111 SDL_bool useDrawTexture; |
109 SDL_bool GL_OES_draw_texture_supported; | 112 SDL_bool GL_OES_draw_texture_supported; |
110 | 113 |
111 /* OpenGL ES functions */ | 114 /* OpenGL ES functions */ |
112 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params; | 115 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params; |
113 #include "SDL_glesfuncs.h" | 116 #include "SDL_glesfuncs.h" |
114 #undef SDL_PROC | 117 #undef SDL_PROC |
115 | 118 |
116 } GLES_RenderData; | 119 } GLES_RenderData; |
117 | 120 |
118 typedef struct | 121 typedef struct |
119 { | 122 { |
163 } | 166 } |
164 | 167 |
165 static int | 168 static int |
166 GLES_LoadFunctions(GLES_RenderData * data) | 169 GLES_LoadFunctions(GLES_RenderData * data) |
167 { | 170 { |
168 | 171 |
169 #define SDL_PROC(ret,func,params) \ | 172 #define SDL_PROC(ret,func,params) \ |
170 data->func = func; | 173 data->func = func; |
171 #include "SDL_glesfuncs.h" | 174 #include "SDL_glesfuncs.h" |
172 #undef SDL_PROC | 175 #undef SDL_PROC |
173 | 176 |
174 return 0; | 177 return 0; |
175 } | 178 } |
176 | 179 |
177 void | 180 void |
178 GLES_AddRenderDriver(_THIS) | 181 GLES_AddRenderDriver(_THIS) |
183 } | 186 } |
184 | 187 |
185 SDL_Renderer * | 188 SDL_Renderer * |
186 GLES_CreateRenderer(SDL_Window * window, Uint32 flags) | 189 GLES_CreateRenderer(SDL_Window * window, Uint32 flags) |
187 { | 190 { |
188 | 191 |
189 SDL_Renderer *renderer; | 192 SDL_Renderer *renderer; |
190 GLES_RenderData *data; | 193 GLES_RenderData *data; |
191 GLint value; | 194 GLint value; |
192 int doublebuffer; | 195 int doublebuffer; |
193 | 196 |
230 renderer->DestroyTexture = GLES_DestroyTexture; | 233 renderer->DestroyTexture = GLES_DestroyTexture; |
231 renderer->DestroyRenderer = GLES_DestroyRenderer; | 234 renderer->DestroyRenderer = GLES_DestroyRenderer; |
232 renderer->info = GL_ES_RenderDriver.info; | 235 renderer->info = GL_ES_RenderDriver.info; |
233 renderer->window = window->id; | 236 renderer->window = window->id; |
234 renderer->driverdata = data; | 237 renderer->driverdata = data; |
235 | 238 |
236 | 239 |
237 renderer->info.flags = (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED); | 240 renderer->info.flags = |
241 (SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED); | |
238 | 242 |
239 if (GLES_LoadFunctions(data) < 0) { | 243 if (GLES_LoadFunctions(data) < 0) { |
240 GLES_DestroyRenderer(renderer); | 244 GLES_DestroyRenderer(renderer); |
241 return NULL; | 245 return NULL; |
242 } | 246 } |
263 if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) { | 267 if (SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer) == 0) { |
264 if (!doublebuffer) { | 268 if (!doublebuffer) { |
265 renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER; | 269 renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER; |
266 } | 270 } |
267 } | 271 } |
268 | 272 |
269 if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) { | 273 if (SDL_GL_ExtensionSupported("GL_OES_draw_texture")) { |
270 data->GL_OES_draw_texture_supported = SDL_TRUE; | 274 data->GL_OES_draw_texture_supported = SDL_TRUE; |
271 data->useDrawTexture = SDL_TRUE; | 275 data->useDrawTexture = SDL_TRUE; |
272 } | 276 } else { |
273 else { | 277 data->GL_OES_draw_texture_supported = SDL_FALSE; |
274 data->GL_OES_draw_texture_supported = SDL_FALSE; | 278 data->useDrawTexture = SDL_FALSE; |
275 data->useDrawTexture = SDL_FALSE; | 279 } |
276 } | |
277 | 280 |
278 data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); | 281 data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); |
279 renderer->info.max_texture_width = value; | 282 renderer->info.max_texture_width = value; |
280 data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); | 283 data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); |
281 renderer->info.max_texture_height = value; | 284 renderer->info.max_texture_height = value; |
282 | 285 |
283 /* Set up parameters for rendering */ | 286 /* Set up parameters for rendering */ |
284 data->blendMode = -1; | 287 data->blendMode = -1; |
285 data->glDisable(GL_DEPTH_TEST); | 288 data->glDisable(GL_DEPTH_TEST); |
286 data->glDisable(GL_CULL_FACE); | 289 data->glDisable(GL_CULL_FACE); |
287 data->glEnable(GL_TEXTURE_2D); | 290 data->glEnable(GL_TEXTURE_2D); |
288 data->updateSize = SDL_TRUE; | 291 data->updateSize = SDL_TRUE; |
289 | 292 |
290 return renderer; | 293 return renderer; |
291 } | 294 } |
292 | 295 |
293 static int GLES_ActivateRenderer(SDL_Renderer * renderer) | 296 static int |
294 { | 297 GLES_ActivateRenderer(SDL_Renderer * renderer) |
295 | 298 { |
296 GLES_RenderData *data = (GLES_RenderData *)renderer->driverdata; | 299 |
300 GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; | |
297 SDL_Window *window = SDL_GetWindowFromID(renderer->window); | 301 SDL_Window *window = SDL_GetWindowFromID(renderer->window); |
298 | 302 |
299 if (SDL_GL_MakeCurrent(window->id, data->context) < 0) { | 303 if (SDL_GL_MakeCurrent(window->id, data->context) < 0) { |
300 return -1; | 304 return -1; |
301 } | 305 } |
302 if (data->updateSize) { | 306 if (data->updateSize) { |
303 data->glMatrixMode(GL_PROJECTION); | 307 data->glMatrixMode(GL_PROJECTION); |
304 data->glLoadIdentity(); | 308 data->glLoadIdentity(); |
305 data->glMatrixMode(GL_MODELVIEW); | 309 data->glMatrixMode(GL_MODELVIEW); |
306 data->glLoadIdentity(); | 310 data->glLoadIdentity(); |
307 data->glViewport(0, 0, window->w, window->h); | 311 data->glViewport(0, 0, window->w, window->h); |
308 data->glOrthof(0.0, (GLfloat)window->w, (GLfloat)window->h, 0.0, | 312 data->glOrthof(0.0, (GLfloat) window->w, (GLfloat) window->h, 0.0, |
309 0.0, 1.0); | 313 0.0, 1.0); |
310 data->updateSize = SDL_FALSE; | 314 data->updateSize = SDL_FALSE; |
311 } | 315 } |
312 return 0; | 316 return 0; |
313 } | 317 } |
314 | 318 |
340 GLES_TextureData *data; | 344 GLES_TextureData *data; |
341 GLint internalFormat; | 345 GLint internalFormat; |
342 GLenum format, type; | 346 GLenum format, type; |
343 int texture_w, texture_h; | 347 int texture_w, texture_h; |
344 GLenum result; | 348 GLenum result; |
345 switch (texture->format) { | 349 switch (texture->format) { |
346 case SDL_PIXELFORMAT_INDEX1LSB: | 350 case SDL_PIXELFORMAT_INDEX1LSB: |
347 case SDL_PIXELFORMAT_INDEX1MSB: | 351 case SDL_PIXELFORMAT_INDEX1MSB: |
348 case SDL_PIXELFORMAT_INDEX8: | 352 case SDL_PIXELFORMAT_INDEX8: |
349 case SDL_PIXELFORMAT_RGB332: | 353 case SDL_PIXELFORMAT_RGB332: |
350 case SDL_PIXELFORMAT_RGB444: | 354 case SDL_PIXELFORMAT_RGB444: |
351 case SDL_PIXELFORMAT_RGB555: | 355 case SDL_PIXELFORMAT_RGB555: |
352 case SDL_PIXELFORMAT_ARGB4444: | 356 case SDL_PIXELFORMAT_ARGB4444: |
353 case SDL_PIXELFORMAT_ARGB1555: | 357 case SDL_PIXELFORMAT_ARGB1555: |
354 case SDL_PIXELFORMAT_BGR24: | 358 case SDL_PIXELFORMAT_BGR24: |
355 case SDL_PIXELFORMAT_BGR888: | 359 case SDL_PIXELFORMAT_BGR888: |
356 case SDL_PIXELFORMAT_RGB888: | 360 case SDL_PIXELFORMAT_RGB888: |
357 case SDL_PIXELFORMAT_RGBA8888: | 361 case SDL_PIXELFORMAT_RGBA8888: |
358 case SDL_PIXELFORMAT_ARGB2101010: | 362 case SDL_PIXELFORMAT_ARGB2101010: |
359 case SDL_PIXELFORMAT_ARGB8888: | 363 case SDL_PIXELFORMAT_ARGB8888: |
360 case SDL_PIXELFORMAT_RGB24: | 364 case SDL_PIXELFORMAT_RGB24: |
361 internalFormat = GL_RGB; | 365 internalFormat = GL_RGB; |
362 format = GL_RGB; | 366 format = GL_RGB; |
363 type = GL_UNSIGNED_BYTE; | 367 type = GL_UNSIGNED_BYTE; |
364 break; | 368 break; |
365 case SDL_PIXELFORMAT_ABGR8888: | 369 case SDL_PIXELFORMAT_ABGR8888: |
366 internalFormat = GL_RGBA; | 370 internalFormat = GL_RGBA; |
367 format = GL_RGBA; | 371 format = GL_RGBA; |
368 type = GL_UNSIGNED_BYTE; | 372 type = GL_UNSIGNED_BYTE; |
369 break; | 373 break; |
370 /* | 374 /* |
371 These formats would be supported if SDL had the necessary pixel formats | 375 These formats would be supported if SDL had the necessary pixel formats |
372 case SDL_PIXELFORMAT_BGR565: | 376 case SDL_PIXELFORMAT_BGR565: |
373 internalFormat = GL_RGB; | 377 internalFormat = GL_RGB; |
374 format = GL_RGB; | 378 format = GL_RGB; |
375 type = GL_UNSIGNED_SHORT_5_6_5; | 379 type = GL_UNSIGNED_SHORT_5_6_5; |
376 break; | 380 break; |
377 case SDL_PIXELFORMAT_ABGR5551: | 381 case SDL_PIXELFORMAT_ABGR5551: |
378 internalFormat = GL_RGBA; | 382 internalFormat = GL_RGBA; |
379 format = GL_RGBA; | 383 format = GL_RGBA; |
380 type = GL_UNSIGNED_SHORT_5_5_5_1; | 384 type = GL_UNSIGNED_SHORT_5_5_5_1; |
381 break; | 385 break; |
382 case SDL_PIXELFORMAT_ABGR4444: | 386 case SDL_PIXELFORMAT_ABGR4444: |
383 internalFormat = GL_RGBA; | 387 internalFormat = GL_RGBA; |
384 format = GL_RGBA; | 388 format = GL_RGBA; |
385 type = GL_UNSIGNED_SHORT_4_4_4_4; | 389 type = GL_UNSIGNED_SHORT_4_4_4_4; |
386 break; | 390 break; |
387 */ | 391 */ |
388 default: | 392 default: |
389 SDL_SetError("Unsupported texture format"); | 393 SDL_SetError("Unsupported texture format"); |
390 return -1; | 394 return -1; |
391 } | 395 } |
392 | 396 |
393 data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data)); | 397 data = (GLES_TextureData *) SDL_calloc(1, sizeof(*data)); |
394 if (!data) { | 398 if (!data) { |
395 SDL_OutOfMemory(); | 399 SDL_OutOfMemory(); |
396 return -1; | 400 return -1; |
397 } | 401 } |
398 | 402 |
408 | 412 |
409 texture->driverdata = data; | 413 texture->driverdata = data; |
410 | 414 |
411 renderdata->glGetError(); | 415 renderdata->glGetError(); |
412 renderdata->glGenTextures(1, &data->texture); | 416 renderdata->glGenTextures(1, &data->texture); |
413 | 417 |
414 data->type = GL_TEXTURE_2D; | 418 data->type = GL_TEXTURE_2D; |
415 /* no NPOV textures allowed in OpenGL ES (yet) */ | 419 /* no NPOV textures allowed in OpenGL ES (yet) */ |
416 texture_w = power_of_2(texture->w); | 420 texture_w = power_of_2(texture->w); |
417 texture_h = power_of_2(texture->h); | 421 texture_h = power_of_2(texture->h); |
418 data->texw = (GLfloat) texture->w / texture_w; | 422 data->texw = (GLfloat) texture->w / texture_w; |
419 data->texh = (GLfloat) texture->h / texture_h; | 423 data->texh = (GLfloat) texture->h / texture_h; |
420 | 424 |
421 data->format = format; | 425 data->format = format; |
422 data->formattype = type; | 426 data->formattype = type; |
423 renderdata->glBindTexture(data->type, data->texture); | 427 renderdata->glBindTexture(data->type, data->texture); |
424 renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, | 428 renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, |
425 GL_NEAREST); | 429 GL_NEAREST); |
427 GL_NEAREST); | 431 GL_NEAREST); |
428 renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S, | 432 renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S, |
429 GL_CLAMP_TO_EDGE); | 433 GL_CLAMP_TO_EDGE); |
430 renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T, | 434 renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T, |
431 GL_CLAMP_TO_EDGE); | 435 GL_CLAMP_TO_EDGE); |
432 | 436 |
433 renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w, | 437 renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w, |
434 texture_h, 0, format, type, NULL); | 438 texture_h, 0, format, type, NULL); |
435 | 439 |
436 result = renderdata->glGetError(); | 440 result = renderdata->glGetError(); |
437 if (result != GL_NO_ERROR) { | 441 if (result != GL_NO_ERROR) { |
438 GLES_SetError("glTexImage2D()", result); | 442 GLES_SetError("glTexImage2D()", result); |
439 return -1; | 443 return -1; |
441 return 0; | 445 return 0; |
442 } | 446 } |
443 | 447 |
444 static int | 448 static int |
445 GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture, | 449 GLES_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture, |
446 void **pixels, int *pitch) | 450 void **pixels, int *pitch) |
447 { | 451 { |
448 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; | 452 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; |
449 | 453 |
450 *pixels = data->pixels; | 454 *pixels = data->pixels; |
451 *pitch = data->pitch; | 455 *pitch = data->pitch; |
452 return 0; | 456 return 0; |
453 } | 457 } |
454 | 458 |
455 static int | 459 static int |
456 GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, | 460 GLES_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, |
457 const SDL_Color * colors, int firstcolor, int ncolors) | 461 const SDL_Color * colors, int firstcolor, int ncolors) |
458 { | 462 { |
459 SDL_SetError("OpenGL ES does not support paletted textures"); | 463 SDL_SetError("OpenGL ES does not support paletted textures"); |
460 return -1; | 464 return -1; |
461 } | 465 } |
462 | 466 |
463 static int | 467 static int |
464 GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, | 468 GLES_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, |
465 SDL_Color * colors, int firstcolor, int ncolors) | 469 SDL_Color * colors, int firstcolor, int ncolors) |
466 { | 470 { |
467 SDL_SetError("OpenGL ES does not support paletted textures"); | 471 SDL_SetError("OpenGL ES does not support paletted textures"); |
468 return -1; | 472 return -1; |
469 } | 473 } |
470 | 474 |
471 static void | 475 static void |
472 SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture, | 476 SetupTextureUpdate(GLES_RenderData * renderdata, SDL_Texture * texture, |
473 int pitch) | 477 int pitch) |
474 { | 478 { |
475 | 479 |
476 | 480 |
477 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; | 481 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; |
478 renderdata->glBindTexture(data->type, data->texture); | 482 renderdata->glBindTexture(data->type, data->texture); |
479 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | 483 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
480 } | 484 } |
481 | 485 |
482 static int | 486 static int |
483 GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture) | 487 GLES_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture) |
516 case SDL_TEXTURESCALEMODE_FAST: | 520 case SDL_TEXTURESCALEMODE_FAST: |
517 case SDL_TEXTURESCALEMODE_SLOW: | 521 case SDL_TEXTURESCALEMODE_SLOW: |
518 return 0; | 522 return 0; |
519 case SDL_TEXTURESCALEMODE_BEST: | 523 case SDL_TEXTURESCALEMODE_BEST: |
520 SDL_Unsupported(); | 524 SDL_Unsupported(); |
521 texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW; | 525 texture->scaleMode = SDL_TEXTURESCALEMODE_SLOW; |
522 return -1; | 526 return -1; |
523 default: | 527 default: |
524 SDL_Unsupported(); | 528 SDL_Unsupported(); |
525 texture->scaleMode = SDL_TEXTURESCALEMODE_NONE; | 529 texture->scaleMode = SDL_TEXTURESCALEMODE_NONE; |
526 return -1; | 530 return -1; |
527 } | 531 } |
528 } | 532 } |
529 | 533 |
530 static int | 534 static int |
531 GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, | 535 GLES_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
532 const SDL_Rect * rect, const void *pixels, int pitch) | 536 const SDL_Rect * rect, const void *pixels, int pitch) |
533 { | 537 { |
534 GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata; | 538 GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata; |
535 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; | 539 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; |
536 GLenum result; | 540 GLenum result; |
537 | 541 |
538 SetupTextureUpdate(renderdata, texture, pitch); | 542 SetupTextureUpdate(renderdata, texture, pitch); |
539 renderdata->glGetError(); | 543 renderdata->glGetError(); |
540 renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, | 544 renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, |
541 rect->h, data->format, data->formattype, | 545 rect->h, data->format, data->formattype, |
542 pixels); | 546 pixels); |
543 result = renderdata->glGetError(); | 547 result = renderdata->glGetError(); |
544 if (result != GL_NO_ERROR) { | 548 if (result != GL_NO_ERROR) { |
545 GLES_SetError("glTexSubImage2D()", result); | 549 GLES_SetError("glTexSubImage2D()", result); |
548 return 0; | 552 return 0; |
549 } | 553 } |
550 | 554 |
551 static int | 555 static int |
552 GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, | 556 GLES_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
553 const SDL_Rect * rect, int markDirty, void **pixels, | 557 const SDL_Rect * rect, int markDirty, void **pixels, |
554 int *pitch) | 558 int *pitch) |
555 { | 559 { |
556 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; | 560 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; |
557 | 561 |
558 if (markDirty) { | 562 if (markDirty) { |
559 SDL_AddDirtyRect(&data->dirty, rect); | 563 SDL_AddDirtyRect(&data->dirty, rect); |
570 GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture) | 574 GLES_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture) |
571 { | 575 { |
572 } | 576 } |
573 | 577 |
574 static void | 578 static void |
575 GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects, | 579 GLES_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
576 const SDL_Rect * rects) | 580 int numrects, const SDL_Rect * rects) |
577 { | 581 { |
578 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; | 582 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; |
579 int i; | 583 int i; |
580 | 584 |
581 for (i = 0; i < numrects; ++i) { | 585 for (i = 0; i < numrects; ++i) { |
583 } | 587 } |
584 } | 588 } |
585 | 589 |
586 static int | 590 static int |
587 GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a, | 591 GLES_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a, |
588 const SDL_Rect * rect) | 592 const SDL_Rect * rect) |
589 { | 593 { |
590 | 594 |
591 GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; | 595 GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; |
592 SDL_Window *window = SDL_GetWindowFromID(renderer->window); | 596 SDL_Window *window = SDL_GetWindowFromID(renderer->window); |
593 | 597 |
594 /* set proper drawing color */ | 598 /* set proper drawing color */ |
595 GLfloat oldClearColor[4]; | 599 GLfloat oldClearColor[4]; |
596 | 600 |
597 data->glGetFloatv(GL_COLOR_CLEAR_VALUE, oldClearColor); | 601 data->glGetFloatv(GL_COLOR_CLEAR_VALUE, oldClearColor); |
598 | 602 |
599 data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f, | 603 data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f, |
600 (GLclampf) b * inv255f, (GLclampf) a * inv255f); | 604 (GLclampf) b * inv255f, (GLclampf) a * inv255f); |
601 | 605 |
602 data->glScissor(rect->x, window->h - rect->y - rect->h, rect->w, rect->h); | 606 data->glScissor(rect->x, window->h - rect->y - rect->h, rect->w, rect->h); |
603 data->glEnable(GL_SCISSOR_TEST); | 607 data->glEnable(GL_SCISSOR_TEST); |
604 data->glClear(GL_COLOR_BUFFER_BIT); | 608 data->glClear(GL_COLOR_BUFFER_BIT); |
605 data->glDisable(GL_SCISSOR_TEST); | 609 data->glDisable(GL_SCISSOR_TEST); |
606 | 610 |
607 /* reset clear color */ | 611 /* reset clear color */ |
608 data->glClearColor(oldClearColor[0], oldClearColor[1], oldClearColor[2], oldClearColor[2]); | 612 data->glClearColor(oldClearColor[0], oldClearColor[1], oldClearColor[2], |
609 | 613 oldClearColor[2]); |
610 return 0; | 614 |
615 return 0; | |
611 } | 616 } |
612 | 617 |
613 static int | 618 static int |
614 GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, | 619 GLES_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, |
615 const SDL_Rect * srcrect, const SDL_Rect * dstrect) | 620 const SDL_Rect * srcrect, const SDL_Rect * dstrect) |
616 { | 621 { |
617 | 622 |
618 GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; | 623 GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata; |
619 GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata; | 624 GLES_TextureData *texturedata = (GLES_TextureData *) texture->driverdata; |
620 int minx, miny, maxx, maxy; | 625 int minx, miny, maxx, maxy; |
621 GLfloat minu, maxu, minv, maxv; | 626 GLfloat minu, maxu, minv, maxv; |
622 int i; | 627 int i; |
623 void *temp_buffer; /* used for reformatting dirty rect pixels */ | 628 void *temp_buffer; /* used for reformatting dirty rect pixels */ |
624 void *temp_ptr; | 629 void *temp_ptr; |
625 | 630 |
626 if (texturedata->dirty.list) { | 631 if (texturedata->dirty.list) { |
627 SDL_DirtyRect *dirty; | 632 SDL_DirtyRect *dirty; |
628 void *pixels; | 633 void *pixels; |
629 int bpp = SDL_BYTESPERPIXEL(texture->format); | 634 int bpp = SDL_BYTESPERPIXEL(texture->format); |
630 int pitch = texturedata->pitch; | 635 int pitch = texturedata->pitch; |
631 | 636 |
632 SetupTextureUpdate(data, texture, pitch); | 637 SetupTextureUpdate(data, texture, pitch); |
633 | 638 |
634 data->glBindTexture(texturedata->type, texturedata->texture); | 639 data->glBindTexture(texturedata->type, texturedata->texture); |
635 for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) { | 640 for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) { |
636 SDL_Rect *rect = &dirty->rect; | 641 SDL_Rect *rect = &dirty->rect; |
637 pixels = (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch + rect->x * bpp); | 642 pixels = |
638 /* There is no GL_UNPACK_ROW_LENGTH in OpenGLES | 643 (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch + |
639 we must do this reformatting ourselves(!) | 644 rect->x * bpp); |
640 | 645 /* There is no GL_UNPACK_ROW_LENGTH in OpenGLES |
641 maybe it'd be a good idea to keep a temp buffer around | 646 we must do this reformatting ourselves(!) |
642 for this purpose rather than allocating it each time | 647 |
643 */ | 648 maybe it'd be a good idea to keep a temp buffer around |
644 temp_buffer = SDL_malloc(rect->w * rect->h * bpp); | 649 for this purpose rather than allocating it each time |
645 temp_ptr = temp_buffer; | 650 */ |
646 for (i=0; i<rect->h; i++) { | 651 temp_buffer = SDL_malloc(rect->w * rect->h * bpp); |
647 SDL_memcpy(temp_ptr, pixels, rect->w * bpp); | 652 temp_ptr = temp_buffer; |
648 temp_ptr += rect->w * bpp; | 653 for (i = 0; i < rect->h; i++) { |
649 pixels += pitch; | 654 SDL_memcpy(temp_ptr, pixels, rect->w * bpp); |
650 } | 655 temp_ptr += rect->w * bpp; |
651 | 656 pixels += pitch; |
657 } | |
658 | |
652 data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y, | 659 data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y, |
653 rect->w, rect->h, texturedata->format, | 660 rect->w, rect->h, texturedata->format, |
654 texturedata->formattype, temp_buffer); | 661 texturedata->formattype, temp_buffer); |
655 | 662 |
656 SDL_free(temp_buffer); | 663 SDL_free(temp_buffer); |
657 | 664 |
658 } | 665 } |
659 SDL_ClearDirtyRects(&texturedata->dirty); | 666 SDL_ClearDirtyRects(&texturedata->dirty); |
660 } | 667 } |
661 | 668 |
662 data->glBindTexture(texturedata->type, texturedata->texture); | 669 data->glBindTexture(texturedata->type, texturedata->texture); |
663 data->glEnable(GL_TEXTURE_2D); | 670 data->glEnable(GL_TEXTURE_2D); |
664 | 671 |
665 if (texture->modMode) { | 672 if (texture->modMode) { |
666 data->glColor4f((GLfloat) texture->r * inv255f, | 673 data->glColor4f((GLfloat) texture->r * inv255f, |
667 (GLfloat) texture->g * inv255f, | 674 (GLfloat) texture->g * inv255f, |
668 (GLfloat) texture->b * inv255f, | 675 (GLfloat) texture->b * inv255f, |
669 (GLfloat) texture->a * inv255f); | 676 (GLfloat) texture->a * inv255f); |
695 break; | 702 break; |
696 } | 703 } |
697 data->blendMode = texture->blendMode; | 704 data->blendMode = texture->blendMode; |
698 } | 705 } |
699 | 706 |
700 switch (texture->scaleMode) { | 707 switch (texture->scaleMode) { |
701 case SDL_TEXTURESCALEMODE_NONE: | 708 case SDL_TEXTURESCALEMODE_NONE: |
702 case SDL_TEXTURESCALEMODE_FAST: | 709 case SDL_TEXTURESCALEMODE_FAST: |
703 data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, | 710 data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, |
704 GL_NEAREST); | 711 GL_NEAREST); |
705 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, | 712 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, |
706 GL_NEAREST); | 713 GL_NEAREST); |
707 break; | 714 break; |
708 case SDL_TEXTURESCALEMODE_SLOW: | 715 case SDL_TEXTURESCALEMODE_SLOW: |
709 case SDL_TEXTURESCALEMODE_BEST: | 716 case SDL_TEXTURESCALEMODE_BEST: |
710 data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, | 717 data->glTexParameteri(texturedata->type, GL_TEXTURE_MIN_FILTER, |
711 GL_LINEAR); | 718 GL_LINEAR); |
712 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, | 719 data->glTexParameteri(texturedata->type, GL_TEXTURE_MAG_FILTER, |
713 GL_LINEAR); | 720 GL_LINEAR); |
714 break; | 721 break; |
715 } | 722 } |
716 | 723 |
717 if (data->GL_OES_draw_texture_supported && data->useDrawTexture) { | 724 if (data->GL_OES_draw_texture_supported && data->useDrawTexture) { |
718 /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */ | 725 /* this code is a little funny because the viewport is upside down vs SDL's coordinate system */ |
719 SDL_Window *window = SDL_GetWindowFromID(renderer->window); | 726 SDL_Window *window = SDL_GetWindowFromID(renderer->window); |
720 GLint cropRect[4]; | 727 GLint cropRect[4]; |
721 cropRect[0] = srcrect->x; | 728 cropRect[0] = srcrect->x; |
722 cropRect[1] = srcrect->y + srcrect->h; | 729 cropRect[1] = srcrect->y + srcrect->h; |
723 cropRect[2] = srcrect->w; | 730 cropRect[2] = srcrect->w; |
724 cropRect[3] = -srcrect->h; | 731 cropRect[3] = -srcrect->h; |
725 data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect); | 732 data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, |
726 data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h, 0, dstrect->w, dstrect->h); | 733 cropRect); |
727 } | 734 data->glDrawTexiOES(dstrect->x, window->h - dstrect->y - dstrect->h, |
728 else { | 735 0, dstrect->w, dstrect->h); |
729 | 736 } else { |
730 minx = dstrect->x; | 737 |
731 miny = dstrect->y; | 738 minx = dstrect->x; |
732 maxx = dstrect->x + dstrect->w; | 739 miny = dstrect->y; |
733 maxy = dstrect->y + dstrect->h; | 740 maxx = dstrect->x + dstrect->w; |
734 | 741 maxy = dstrect->y + dstrect->h; |
735 minu = (GLfloat) srcrect->x / texture->w; | 742 |
736 minu *= texturedata->texw; | 743 minu = (GLfloat) srcrect->x / texture->w; |
737 maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w; | 744 minu *= texturedata->texw; |
738 maxu *= texturedata->texw; | 745 maxu = (GLfloat) (srcrect->x + srcrect->w) / texture->w; |
739 minv = (GLfloat) srcrect->y / texture->h; | 746 maxu *= texturedata->texw; |
740 minv *= texturedata->texh; | 747 minv = (GLfloat) srcrect->y / texture->h; |
741 maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h; | 748 minv *= texturedata->texh; |
742 maxv *= texturedata->texh; | 749 maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h; |
743 | 750 maxv *= texturedata->texh; |
744 GLshort vertices[8]; | 751 |
745 GLfloat texCoords[8]; | 752 GLshort vertices[8]; |
746 | 753 GLfloat texCoords[8]; |
747 vertices[0] = minx; vertices[1] = miny; | 754 |
748 vertices[2] = maxx; vertices[3] = miny; | 755 vertices[0] = minx; |
749 vertices[4] = minx; vertices[5] = maxy; | 756 vertices[1] = miny; |
750 vertices[6] = maxx; vertices[7] = maxy; | 757 vertices[2] = maxx; |
751 | 758 vertices[3] = miny; |
752 texCoords[0] = minu; texCoords[1] = minv; | 759 vertices[4] = minx; |
753 texCoords[2] = maxu; texCoords[3] = minv; | 760 vertices[5] = maxy; |
754 texCoords[4] = minu; texCoords[5] = maxv; | 761 vertices[6] = maxx; |
755 texCoords[6] = maxu; texCoords[7] = maxv; | 762 vertices[7] = maxy; |
756 | 763 |
757 data->glVertexPointer(2, GL_SHORT, 0, vertices); | 764 texCoords[0] = minu; |
758 data->glEnableClientState(GL_VERTEX_ARRAY); | 765 texCoords[1] = minv; |
759 data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords); | 766 texCoords[2] = maxu; |
760 data->glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 767 texCoords[3] = minv; |
761 data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | 768 texCoords[4] = minu; |
762 | 769 texCoords[5] = maxv; |
763 } | 770 texCoords[6] = maxu; |
764 | 771 texCoords[7] = maxv; |
772 | |
773 data->glVertexPointer(2, GL_SHORT, 0, vertices); | |
774 data->glEnableClientState(GL_VERTEX_ARRAY); | |
775 data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords); | |
776 data->glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
777 data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
778 | |
779 } | |
780 | |
765 return 0; | 781 return 0; |
766 } | 782 } |
767 | 783 |
768 static void | 784 static void |
769 GLES_RenderPresent(SDL_Renderer * renderer) | 785 GLES_RenderPresent(SDL_Renderer * renderer) |
779 | 795 |
780 if (!data) { | 796 if (!data) { |
781 return; | 797 return; |
782 } | 798 } |
783 if (data->texture) { | 799 if (data->texture) { |
784 glDeleteTextures(1, &data->texture); | 800 glDeleteTextures(1, &data->texture); |
785 } | 801 } |
786 if (data->pixels) { | 802 if (data->pixels) { |
787 SDL_free(data->pixels); | 803 SDL_free(data->pixels); |
788 } | 804 } |
789 SDL_FreeDirtyRects(&data->dirty); | 805 SDL_FreeDirtyRects(&data->dirty); |