Mercurial > sdl-ios-xcode
comparison src/video/SDL_renderer_gl.c @ 3435:9f62f47d989b
You can specify the format for pixel data in SDL_RenderReadPixels() and SDL_RenderWritePixels()
This code still doesn't quite work yet. :)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 16 Nov 2009 07:13:07 +0000 |
parents | ad845d9835aa |
children | e9502d56ae94 |
comparison
equal
deleted
inserted
replaced
3434:147d6ef5be03 | 3435:9f62f47d989b |
---|---|
101 int y2); | 101 int y2); |
102 static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect); | 102 static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect); |
103 static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, | 103 static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, |
104 const SDL_Rect * srcrect, const SDL_Rect * dstrect); | 104 const SDL_Rect * srcrect, const SDL_Rect * dstrect); |
105 static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, | 105 static int GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, |
106 void * pixels, int pitch); | 106 Uint32 pixel_format, void * pixels, int pitch); |
107 static int GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect, | 107 static int GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect, |
108 const void * pixels, int pitch); | 108 Uint32 pixel_format, const void * pixels, int pitch); |
109 static void GL_RenderPresent(SDL_Renderer * renderer); | 109 static void GL_RenderPresent(SDL_Renderer * renderer); |
110 static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture); | 110 static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture); |
111 static void GL_DestroyRenderer(SDL_Renderer * renderer); | 111 static void GL_DestroyRenderer(SDL_Renderer * renderer); |
112 | 112 |
113 | 113 |
1252 return 0; | 1252 return 0; |
1253 } | 1253 } |
1254 | 1254 |
1255 static int | 1255 static int |
1256 GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, | 1256 GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, |
1257 void * pixels, int pitch) | 1257 Uint32 pixel_format, void * pixels, int pitch) |
1258 { | 1258 { |
1259 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 1259 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
1260 SDL_Window *window = SDL_GetWindowFromID(renderer->window); | |
1261 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); | |
1262 Uint32 pixel_format = display->current_mode.format; | |
1263 GLint internalFormat; | 1260 GLint internalFormat; |
1264 GLenum format, type; | 1261 GLenum format, type; |
1262 Uint8 *src, *dst, *tmp; | |
1263 int length, rows; | |
1265 | 1264 |
1266 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { | 1265 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { |
1266 /* FIXME: Do a temp copy to a format that is supported */ | |
1267 SDL_SetError("Unsupported pixel format"); | 1267 SDL_SetError("Unsupported pixel format"); |
1268 return -1; | 1268 return -1; |
1269 } | 1269 } |
1270 | 1270 |
1271 if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) { | 1271 if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) { |
1273 } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) { | 1273 } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) { |
1274 data->glPixelStorei(GL_PACK_LSB_FIRST, 0); | 1274 data->glPixelStorei(GL_PACK_LSB_FIRST, 0); |
1275 } | 1275 } |
1276 data->glPixelStorei(GL_PACK_ALIGNMENT, 1); | 1276 data->glPixelStorei(GL_PACK_ALIGNMENT, 1); |
1277 data->glPixelStorei(GL_PACK_ROW_LENGTH, | 1277 data->glPixelStorei(GL_PACK_ROW_LENGTH, |
1278 -2 * (pitch / bytes_per_pixel(pixel_format))); | 1278 (pitch / bytes_per_pixel(pixel_format))); |
1279 | 1279 |
1280 data->glReadPixels(rect->x, rect->y+rect->h-1, rect->w, rect->h, | 1280 data->glReadPixels(rect->x, rect->y+rect->h-1, rect->w, rect->h, |
1281 format, type, pixels + (rect->h-1)*pitch); | 1281 format, type, pixels); |
1282 | |
1283 /* Flip the rows to be top-down */ | |
1284 length = rect->w * bytes_per_pixel(pixel_format); | |
1285 src = (Uint8*)pixels + (rect->h-1)*pitch; | |
1286 dst = (Uint8*)pixels; | |
1287 tmp = SDL_stack_alloc(Uint8, length); | |
1288 rows = rect->h / 2; | |
1289 while (rows--) { | |
1290 SDL_memcpy(tmp, dst, length); | |
1291 SDL_memcpy(dst, src, length); | |
1292 SDL_memcpy(src, tmp, length); | |
1293 } | |
1294 SDL_stack_free(tmp); | |
1282 } | 1295 } |
1283 | 1296 |
1284 static int | 1297 static int |
1285 GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect, | 1298 GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect, |
1286 const void * pixels, int pitch) | 1299 Uint32 pixel_format, const void * pixels, int pitch) |
1287 { | 1300 { |
1301 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | |
1302 GLint internalFormat; | |
1303 GLenum format, type; | |
1304 | |
1305 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { | |
1306 /* FIXME: Do a temp copy to a format that is supported */ | |
1307 SDL_SetError("Unsupported pixel format"); | |
1308 return -1; | |
1309 } | |
1310 | |
1311 /* FIXME: We need to copy the data and flip it */ | |
1312 | |
1313 if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) { | |
1314 data->glPixelStorei(GL_UNPACK_LSB_FIRST, 1); | |
1315 } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) { | |
1316 data->glPixelStorei(GL_UNPACK_LSB_FIRST, 0); | |
1317 } | |
1318 data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
1319 data->glPixelStorei(GL_UNPACK_ROW_LENGTH, | |
1320 (pitch / bytes_per_pixel(pixel_format))); | |
1321 | |
1322 data->glReadPixels(rect->x, rect->y+rect->h-1, rect->w, rect->h, | |
1323 format, type, pixels); | |
1288 } | 1324 } |
1289 | 1325 |
1290 static void | 1326 static void |
1291 GL_RenderPresent(SDL_Renderer * renderer) | 1327 GL_RenderPresent(SDL_Renderer * renderer) |
1292 { | 1328 { |