Mercurial > sdl-ios-xcode
comparison src/video/SDL_renderer_gl.c @ 5152:be02be2ea897
Making the API simpler, removed support for palettized video modes and textures.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 01 Feb 2011 21:23:43 -0800 |
parents | 5429daf5e3f9 |
children | 1435f8a6425c |
comparison
equal
deleted
inserted
replaced
5151:5429daf5e3f9 | 5152:be02be2ea897 |
---|---|
70 const SDL_WindowEvent *event); | 70 const SDL_WindowEvent *event); |
71 static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); | 71 static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); |
72 static int GL_QueryTexturePixels(SDL_Renderer * renderer, | 72 static int GL_QueryTexturePixels(SDL_Renderer * renderer, |
73 SDL_Texture * texture, void **pixels, | 73 SDL_Texture * texture, void **pixels, |
74 int *pitch); | 74 int *pitch); |
75 static int GL_SetTexturePalette(SDL_Renderer * renderer, | |
76 SDL_Texture * texture, | |
77 const SDL_Color * colors, int firstcolor, | |
78 int ncolors); | |
79 static int GL_GetTexturePalette(SDL_Renderer * renderer, | |
80 SDL_Texture * texture, SDL_Color * colors, | |
81 int firstcolor, int ncolors); | |
82 static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, | 75 static int GL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
83 const SDL_Rect * rect, const void *pixels, | 76 const SDL_Rect * rect, const void *pixels, |
84 int pitch); | 77 int pitch); |
85 static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, | 78 static int GL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, |
86 const SDL_Rect * rect, int markDirty, void **pixels, | 79 const SDL_Rect * rect, int markDirty, void **pixels, |
109 SDL_RenderDriver GL_RenderDriver = { | 102 SDL_RenderDriver GL_RenderDriver = { |
110 GL_CreateRenderer, | 103 GL_CreateRenderer, |
111 { | 104 { |
112 "opengl", | 105 "opengl", |
113 (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED), | 106 (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED), |
114 15, | 107 13, |
115 { | 108 { |
116 SDL_PIXELFORMAT_INDEX1LSB, | |
117 SDL_PIXELFORMAT_INDEX1MSB, | |
118 SDL_PIXELFORMAT_INDEX8, | |
119 SDL_PIXELFORMAT_RGB332, | 109 SDL_PIXELFORMAT_RGB332, |
120 SDL_PIXELFORMAT_RGB444, | 110 SDL_PIXELFORMAT_RGB444, |
121 SDL_PIXELFORMAT_RGB555, | 111 SDL_PIXELFORMAT_RGB555, |
122 SDL_PIXELFORMAT_ARGB4444, | 112 SDL_PIXELFORMAT_ARGB4444, |
123 SDL_PIXELFORMAT_ARGB1555, | 113 SDL_PIXELFORMAT_ARGB1555, |
147 /* OpenGL functions */ | 137 /* OpenGL functions */ |
148 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params; | 138 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params; |
149 #include "SDL_glfuncs.h" | 139 #include "SDL_glfuncs.h" |
150 #undef SDL_PROC | 140 #undef SDL_PROC |
151 | 141 |
152 PFNGLCOLORTABLEEXTPROC glColorTableEXT; | |
153 void (*glTextureRangeAPPLE) (GLenum target, GLsizei length, | 142 void (*glTextureRangeAPPLE) (GLenum target, GLsizei length, |
154 const GLvoid * pointer); | 143 const GLvoid * pointer); |
155 | 144 |
156 PFNGLGETPROGRAMIVARBPROC glGetProgramivARB; | 145 PFNGLGETPROGRAMIVARBPROC glGetProgramivARB; |
157 PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB; | 146 PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB; |
276 } | 265 } |
277 | 266 |
278 renderer->WindowEvent = GL_WindowEvent; | 267 renderer->WindowEvent = GL_WindowEvent; |
279 renderer->CreateTexture = GL_CreateTexture; | 268 renderer->CreateTexture = GL_CreateTexture; |
280 renderer->QueryTexturePixels = GL_QueryTexturePixels; | 269 renderer->QueryTexturePixels = GL_QueryTexturePixels; |
281 renderer->SetTexturePalette = GL_SetTexturePalette; | |
282 renderer->GetTexturePalette = GL_GetTexturePalette; | |
283 renderer->UpdateTexture = GL_UpdateTexture; | 270 renderer->UpdateTexture = GL_UpdateTexture; |
284 renderer->LockTexture = GL_LockTexture; | 271 renderer->LockTexture = GL_LockTexture; |
285 renderer->UnlockTexture = GL_UnlockTexture; | 272 renderer->UnlockTexture = GL_UnlockTexture; |
286 renderer->DirtyTexture = GL_DirtyTexture; | 273 renderer->DirtyTexture = GL_DirtyTexture; |
287 renderer->RenderClear = GL_RenderClear; | 274 renderer->RenderClear = GL_RenderClear; |
336 renderer->info.max_texture_height = value; | 323 renderer->info.max_texture_height = value; |
337 | 324 |
338 if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle") | 325 if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle") |
339 || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) { | 326 || SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) { |
340 data->GL_ARB_texture_rectangle_supported = SDL_TRUE; | 327 data->GL_ARB_texture_rectangle_supported = SDL_TRUE; |
341 } | |
342 if (SDL_GL_ExtensionSupported("GL_EXT_paletted_texture")) { | |
343 data->GL_EXT_paletted_texture_supported = SDL_TRUE; | |
344 data->glColorTableEXT = | |
345 (PFNGLCOLORTABLEEXTPROC) SDL_GL_GetProcAddress("glColorTableEXT"); | |
346 } else { | |
347 /* Don't advertise support for 8-bit indexed texture format */ | |
348 Uint32 i, j; | |
349 SDL_RendererInfo *info = &renderer->info; | |
350 for (i = 0, j = 0; i < info->num_texture_formats; ++i) { | |
351 if (info->texture_formats[i] != SDL_PIXELFORMAT_INDEX8) { | |
352 info->texture_formats[j++] = info->texture_formats[i]; | |
353 } | |
354 } | |
355 --info->num_texture_formats; | |
356 } | 328 } |
357 if (SDL_GL_ExtensionSupported("GL_APPLE_ycbcr_422")) { | 329 if (SDL_GL_ExtensionSupported("GL_APPLE_ycbcr_422")) { |
358 data->GL_APPLE_ycbcr_422_supported = SDL_TRUE; | 330 data->GL_APPLE_ycbcr_422_supported = SDL_TRUE; |
359 } | 331 } |
360 if (SDL_GL_ExtensionSupported("GL_MESA_ycbcr_texture")) { | 332 if (SDL_GL_ExtensionSupported("GL_MESA_ycbcr_texture")) { |
570 static __inline__ SDL_bool | 542 static __inline__ SDL_bool |
571 convert_format(GL_RenderData *renderdata, Uint32 pixel_format, | 543 convert_format(GL_RenderData *renderdata, Uint32 pixel_format, |
572 GLint* internalFormat, GLenum* format, GLenum* type) | 544 GLint* internalFormat, GLenum* format, GLenum* type) |
573 { | 545 { |
574 switch (pixel_format) { | 546 switch (pixel_format) { |
575 case SDL_PIXELFORMAT_INDEX1LSB: | |
576 case SDL_PIXELFORMAT_INDEX1MSB: | |
577 *internalFormat = GL_RGB; | |
578 *format = GL_COLOR_INDEX; | |
579 *type = GL_BITMAP; | |
580 break; | |
581 case SDL_PIXELFORMAT_INDEX8: | |
582 if (!renderdata->GL_EXT_paletted_texture_supported) { | |
583 return SDL_FALSE; | |
584 } | |
585 *internalFormat = GL_COLOR_INDEX8_EXT; | |
586 *format = GL_COLOR_INDEX; | |
587 *type = GL_UNSIGNED_BYTE; | |
588 break; | |
589 case SDL_PIXELFORMAT_RGB332: | 547 case SDL_PIXELFORMAT_RGB332: |
590 *internalFormat = GL_R3_G3_B2; | 548 *internalFormat = GL_R3_G3_B2; |
591 *format = GL_RGB; | 549 *format = GL_RGB; |
592 *type = GL_UNSIGNED_BYTE_3_3_2; | 550 *type = GL_UNSIGNED_BYTE_3_3_2; |
593 break; | 551 break; |
749 SDL_OutOfMemory(); | 707 SDL_OutOfMemory(); |
750 return -1; | 708 return -1; |
751 } | 709 } |
752 | 710 |
753 data->shader = shader; | 711 data->shader = shader; |
754 | |
755 if (texture->format == SDL_PIXELFORMAT_INDEX8) { | |
756 data->palette = (Uint8 *) SDL_malloc(3 * 256 * sizeof(Uint8)); | |
757 if (!data->palette) { | |
758 SDL_OutOfMemory(); | |
759 SDL_free(data); | |
760 return -1; | |
761 } | |
762 SDL_memset(data->palette, 0xFF, 3 * 256 * sizeof(Uint8)); | |
763 } | |
764 | 712 |
765 if (texture->access == SDL_TEXTUREACCESS_STREAMING) { | 713 if (texture->access == SDL_TEXTUREACCESS_STREAMING) { |
766 data->pitch = texture->w * bytes_per_pixel(texture->format); | 714 data->pitch = texture->w * bytes_per_pixel(texture->format); |
767 data->pixels = SDL_malloc(texture->h * data->pitch); | 715 data->pixels = SDL_malloc(texture->h * data->pitch); |
768 if (!data->pixels) { | 716 if (!data->pixels) { |
869 *pixels = data->pixels; | 817 *pixels = data->pixels; |
870 *pitch = data->pitch; | 818 *pitch = data->pitch; |
871 return 0; | 819 return 0; |
872 } | 820 } |
873 | 821 |
874 static int | |
875 GL_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, | |
876 const SDL_Color * colors, int firstcolor, int ncolors) | |
877 { | |
878 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; | |
879 GL_TextureData *data = (GL_TextureData *) texture->driverdata; | |
880 Uint8 *palette; | |
881 | |
882 GL_ActivateRenderer(renderer); | |
883 | |
884 if (!data->palette) { | |
885 SDL_SetError("Texture doesn't have a palette"); | |
886 return -1; | |
887 } | |
888 palette = data->palette + firstcolor * 3; | |
889 while (ncolors--) { | |
890 *palette++ = colors->r; | |
891 *palette++ = colors->g; | |
892 *palette++ = colors->b; | |
893 ++colors; | |
894 } | |
895 renderdata->glEnable(data->type); | |
896 renderdata->glBindTexture(data->type, data->texture); | |
897 renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB, | |
898 GL_UNSIGNED_BYTE, data->palette); | |
899 return 0; | |
900 } | |
901 | |
902 static int | |
903 GL_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, | |
904 SDL_Color * colors, int firstcolor, int ncolors) | |
905 { | |
906 GL_TextureData *data = (GL_TextureData *) texture->driverdata; | |
907 Uint8 *palette; | |
908 | |
909 if (!data->palette) { | |
910 SDL_SetError("Texture doesn't have a palette"); | |
911 return -1; | |
912 } | |
913 palette = data->palette + firstcolor * 3; | |
914 while (ncolors--) { | |
915 colors->r = *palette++; | |
916 colors->g = *palette++; | |
917 colors->b = *palette++; | |
918 colors->unused = SDL_ALPHA_OPAQUE; | |
919 ++colors; | |
920 } | |
921 return 0; | |
922 } | |
923 | |
924 static void | 822 static void |
925 SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture, | 823 SetupTextureUpdate(GL_RenderData * renderdata, SDL_Texture * texture, |
926 int pitch) | 824 int pitch) |
927 { | 825 { |
928 if (texture->format == SDL_PIXELFORMAT_INDEX1LSB) { | |
929 renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 1); | |
930 } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) { | |
931 renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0); | |
932 } | |
933 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | 826 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
934 renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, | 827 renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, |
935 (pitch / bytes_per_pixel(texture->format)) / | 828 (pitch / bytes_per_pixel(texture->format)) / |
936 ((GL_TextureData *) texture->driverdata)-> | 829 ((GL_TextureData *) texture->driverdata)-> |
937 HACK_RYAN_FIXME); | 830 HACK_RYAN_FIXME); |
1263 /* FIXME: Do a temp copy to a format that is supported */ | 1156 /* FIXME: Do a temp copy to a format that is supported */ |
1264 SDL_SetError("Unsupported pixel format"); | 1157 SDL_SetError("Unsupported pixel format"); |
1265 return -1; | 1158 return -1; |
1266 } | 1159 } |
1267 | 1160 |
1268 if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) { | |
1269 data->glPixelStorei(GL_PACK_LSB_FIRST, 1); | |
1270 } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) { | |
1271 data->glPixelStorei(GL_PACK_LSB_FIRST, 0); | |
1272 } | |
1273 data->glPixelStorei(GL_PACK_ALIGNMENT, 1); | 1161 data->glPixelStorei(GL_PACK_ALIGNMENT, 1); |
1274 data->glPixelStorei(GL_PACK_ROW_LENGTH, | 1162 data->glPixelStorei(GL_PACK_ROW_LENGTH, |
1275 (pitch / bytes_per_pixel(pixel_format))); | 1163 (pitch / bytes_per_pixel(pixel_format))); |
1276 | 1164 |
1277 data->glReadPixels(rect->x, (window->h-rect->y)-rect->h, rect->w, rect->h, | 1165 data->glReadPixels(rect->x, (window->h-rect->y)-rect->h, rect->w, rect->h, |
1312 /* FIXME: Do a temp copy to a format that is supported */ | 1200 /* FIXME: Do a temp copy to a format that is supported */ |
1313 SDL_SetError("Unsupported pixel format"); | 1201 SDL_SetError("Unsupported pixel format"); |
1314 return -1; | 1202 return -1; |
1315 } | 1203 } |
1316 | 1204 |
1317 if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) { | |
1318 data->glPixelStorei(GL_UNPACK_LSB_FIRST, 1); | |
1319 } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) { | |
1320 data->glPixelStorei(GL_UNPACK_LSB_FIRST, 0); | |
1321 } | |
1322 data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | 1205 data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
1323 data->glPixelStorei(GL_UNPACK_ROW_LENGTH, | 1206 data->glPixelStorei(GL_UNPACK_ROW_LENGTH, |
1324 (pitch / bytes_per_pixel(pixel_format))); | 1207 (pitch / bytes_per_pixel(pixel_format))); |
1325 | 1208 |
1326 /* Flip the rows to be bottom-up */ | 1209 /* Flip the rows to be bottom-up */ |