Mercurial > sdl-ios-xcode
comparison src/video/SDL_renderer_gl.c @ 5150:ad50b3db78bd
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 01 Feb 2011 19:19:43 -0800 |
parents | c8e049de174c |
children | 5429daf5e3f9 |
comparison
equal
deleted
inserted
replaced
5149:3052772b59db | 5150:ad50b3db78bd |
---|---|
64 | 64 |
65 | 65 |
66 static const float inv255f = 1.0f / 255.0f; | 66 static const float inv255f = 1.0f / 255.0f; |
67 | 67 |
68 static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags); | 68 static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags); |
69 static int GL_ActivateRenderer(SDL_Renderer * renderer); | 69 static void GL_WindowEvent(SDL_Renderer * renderer, |
70 static int GL_DisplayModeChanged(SDL_Renderer * renderer); | 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, | 75 static int GL_SetTexturePalette(SDL_Renderer * renderer, |
275 GL_DestroyRenderer(renderer); | 275 GL_DestroyRenderer(renderer); |
276 SDL_OutOfMemory(); | 276 SDL_OutOfMemory(); |
277 return NULL; | 277 return NULL; |
278 } | 278 } |
279 | 279 |
280 renderer->ActivateRenderer = GL_ActivateRenderer; | 280 renderer->WindowEvent = GL_WindowEvent; |
281 renderer->DisplayModeChanged = GL_DisplayModeChanged; | |
282 renderer->CreateTexture = GL_CreateTexture; | 281 renderer->CreateTexture = GL_CreateTexture; |
283 renderer->QueryTexturePixels = GL_QueryTexturePixels; | 282 renderer->QueryTexturePixels = GL_QueryTexturePixels; |
284 renderer->SetTexturePalette = GL_SetTexturePalette; | 283 renderer->SetTexturePalette = GL_SetTexturePalette; |
285 renderer->GetTexturePalette = GL_GetTexturePalette; | 284 renderer->GetTexturePalette = GL_GetTexturePalette; |
286 renderer->UpdateTexture = GL_UpdateTexture; | 285 renderer->UpdateTexture = GL_UpdateTexture; |
406 data->updateSize = SDL_TRUE; | 405 data->updateSize = SDL_TRUE; |
407 | 406 |
408 return renderer; | 407 return renderer; |
409 } | 408 } |
410 | 409 |
410 static SDL_GLContext SDL_CurrentContext = NULL; | |
411 | |
411 static int | 412 static int |
412 GL_ActivateRenderer(SDL_Renderer * renderer) | 413 GL_ActivateRenderer(SDL_Renderer * renderer) |
413 { | 414 { |
414 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 415 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
415 SDL_Window *window = renderer->window; | 416 SDL_Window *window = renderer->window; |
416 | 417 |
417 if (SDL_GL_MakeCurrent(window, data->context) < 0) { | 418 if (SDL_CurrentContext != data->context) { |
418 return -1; | 419 if (SDL_GL_MakeCurrent(window, data->context) < 0) { |
420 return -1; | |
421 } | |
422 SDL_CurrentContext = data->context; | |
419 } | 423 } |
420 if (data->updateSize) { | 424 if (data->updateSize) { |
421 data->glMatrixMode(GL_PROJECTION); | 425 data->glMatrixMode(GL_PROJECTION); |
422 data->glLoadIdentity(); | 426 data->glLoadIdentity(); |
423 data->glMatrixMode(GL_MODELVIEW); | 427 data->glMatrixMode(GL_MODELVIEW); |
428 data->updateSize = SDL_FALSE; | 432 data->updateSize = SDL_FALSE; |
429 } | 433 } |
430 return 0; | 434 return 0; |
431 } | 435 } |
432 | 436 |
433 static int | 437 static void |
434 GL_DisplayModeChanged(SDL_Renderer * renderer) | 438 GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event) |
435 { | 439 { |
436 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 440 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
437 | 441 |
438 /* Rebind the context to the window area and update matrices */ | 442 if (event->event == SDL_WINDOWEVENT_RESIZED) { |
439 data->updateSize = SDL_TRUE; | 443 /* Rebind the context to the window area and update matrices */ |
440 return GL_ActivateRenderer(renderer); | 444 SDL_CurrentContext = NULL; |
445 data->updateSize = SDL_TRUE; | |
446 } | |
441 } | 447 } |
442 | 448 |
443 static __inline__ int | 449 static __inline__ int |
444 power_of_2(int input) | 450 power_of_2(int input) |
445 { | 451 { |
715 GLenum format, type; | 721 GLenum format, type; |
716 int texture_w, texture_h; | 722 int texture_w, texture_h; |
717 GLuint shader = 0; | 723 GLuint shader = 0; |
718 GLenum result; | 724 GLenum result; |
719 | 725 |
726 GL_ActivateRenderer(renderer); | |
727 | |
720 if (!convert_format(renderdata, texture->format, &internalFormat, | 728 if (!convert_format(renderdata, texture->format, &internalFormat, |
721 &format, &type)) { | 729 &format, &type)) { |
722 SDL_SetError("Texture format %s not supported by OpenGL", | 730 SDL_SetError("Texture format %s not supported by OpenGL", |
723 SDL_GetPixelFormatName(texture->format)); | 731 SDL_GetPixelFormatName(texture->format)); |
724 return -1; | 732 return -1; |
872 { | 880 { |
873 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; | 881 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; |
874 GL_TextureData *data = (GL_TextureData *) texture->driverdata; | 882 GL_TextureData *data = (GL_TextureData *) texture->driverdata; |
875 Uint8 *palette; | 883 Uint8 *palette; |
876 | 884 |
885 GL_ActivateRenderer(renderer); | |
886 | |
877 if (!data->palette) { | 887 if (!data->palette) { |
878 SDL_SetError("Texture doesn't have a palette"); | 888 SDL_SetError("Texture doesn't have a palette"); |
879 return -1; | 889 return -1; |
880 } | 890 } |
881 palette = data->palette + firstcolor * 3; | 891 palette = data->palette + firstcolor * 3; |
935 const SDL_Rect * rect, const void *pixels, int pitch) | 945 const SDL_Rect * rect, const void *pixels, int pitch) |
936 { | 946 { |
937 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; | 947 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; |
938 GL_TextureData *data = (GL_TextureData *) texture->driverdata; | 948 GL_TextureData *data = (GL_TextureData *) texture->driverdata; |
939 GLenum result; | 949 GLenum result; |
950 | |
951 GL_ActivateRenderer(renderer); | |
940 | 952 |
941 renderdata->glGetError(); | 953 renderdata->glGetError(); |
942 SetupTextureUpdate(renderdata, texture, pitch); | 954 SetupTextureUpdate(renderdata, texture, pitch); |
943 renderdata->glEnable(data->type); | 955 renderdata->glEnable(data->type); |
944 renderdata->glBindTexture(data->type, data->texture); | 956 renderdata->glBindTexture(data->type, data->texture); |
1016 static int | 1028 static int |
1017 GL_RenderClear(SDL_Renderer * renderer) | 1029 GL_RenderClear(SDL_Renderer * renderer) |
1018 { | 1030 { |
1019 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 1031 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
1020 | 1032 |
1033 GL_ActivateRenderer(renderer); | |
1034 | |
1021 data->glClearColor((GLfloat) renderer->r * inv255f, | 1035 data->glClearColor((GLfloat) renderer->r * inv255f, |
1022 (GLfloat) renderer->g * inv255f, | 1036 (GLfloat) renderer->g * inv255f, |
1023 (GLfloat) renderer->b * inv255f, | 1037 (GLfloat) renderer->b * inv255f, |
1024 (GLfloat) renderer->a * inv255f); | 1038 (GLfloat) renderer->a * inv255f); |
1025 | 1039 |
1033 int count) | 1047 int count) |
1034 { | 1048 { |
1035 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 1049 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
1036 int i; | 1050 int i; |
1037 | 1051 |
1052 GL_ActivateRenderer(renderer); | |
1053 | |
1038 GL_SetBlendMode(data, renderer->blendMode); | 1054 GL_SetBlendMode(data, renderer->blendMode); |
1039 | 1055 |
1040 data->glColor4f((GLfloat) renderer->r * inv255f, | 1056 data->glColor4f((GLfloat) renderer->r * inv255f, |
1041 (GLfloat) renderer->g * inv255f, | 1057 (GLfloat) renderer->g * inv255f, |
1042 (GLfloat) renderer->b * inv255f, | 1058 (GLfloat) renderer->b * inv255f, |
1055 GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points, | 1071 GL_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points, |
1056 int count) | 1072 int count) |
1057 { | 1073 { |
1058 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 1074 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
1059 int i; | 1075 int i; |
1076 | |
1077 GL_ActivateRenderer(renderer); | |
1060 | 1078 |
1061 GL_SetBlendMode(data, renderer->blendMode); | 1079 GL_SetBlendMode(data, renderer->blendMode); |
1062 | 1080 |
1063 data->glColor4f((GLfloat) renderer->r * inv255f, | 1081 data->glColor4f((GLfloat) renderer->r * inv255f, |
1064 (GLfloat) renderer->g * inv255f, | 1082 (GLfloat) renderer->g * inv255f, |
1124 GL_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count) | 1142 GL_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count) |
1125 { | 1143 { |
1126 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 1144 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
1127 int i, x, y; | 1145 int i, x, y; |
1128 | 1146 |
1147 GL_ActivateRenderer(renderer); | |
1148 | |
1129 GL_SetBlendMode(data, renderer->blendMode); | 1149 GL_SetBlendMode(data, renderer->blendMode); |
1130 | 1150 |
1131 data->glColor4f((GLfloat) renderer->r * inv255f, | 1151 data->glColor4f((GLfloat) renderer->r * inv255f, |
1132 (GLfloat) renderer->g * inv255f, | 1152 (GLfloat) renderer->g * inv255f, |
1133 (GLfloat) renderer->b * inv255f, | 1153 (GLfloat) renderer->b * inv255f, |
1162 GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count) | 1182 GL_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects, int count) |
1163 { | 1183 { |
1164 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 1184 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
1165 int i; | 1185 int i; |
1166 | 1186 |
1187 GL_ActivateRenderer(renderer); | |
1188 | |
1167 GL_SetBlendMode(data, renderer->blendMode); | 1189 GL_SetBlendMode(data, renderer->blendMode); |
1168 | 1190 |
1169 data->glColor4f((GLfloat) renderer->r * inv255f, | 1191 data->glColor4f((GLfloat) renderer->r * inv255f, |
1170 (GLfloat) renderer->g * inv255f, | 1192 (GLfloat) renderer->g * inv255f, |
1171 (GLfloat) renderer->b * inv255f, | 1193 (GLfloat) renderer->b * inv255f, |
1186 { | 1208 { |
1187 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; | 1209 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; |
1188 GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata; | 1210 GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata; |
1189 int minx, miny, maxx, maxy; | 1211 int minx, miny, maxx, maxy; |
1190 GLfloat minu, maxu, minv, maxv; | 1212 GLfloat minu, maxu, minv, maxv; |
1213 | |
1214 GL_ActivateRenderer(renderer); | |
1191 | 1215 |
1192 if (texturedata->dirty.list) { | 1216 if (texturedata->dirty.list) { |
1193 SDL_DirtyRect *dirty; | 1217 SDL_DirtyRect *dirty; |
1194 void *pixels; | 1218 void *pixels; |
1195 int bpp = bytes_per_pixel(texture->format); | 1219 int bpp = bytes_per_pixel(texture->format); |
1274 GLint internalFormat; | 1298 GLint internalFormat; |
1275 GLenum format, type; | 1299 GLenum format, type; |
1276 Uint8 *src, *dst, *tmp; | 1300 Uint8 *src, *dst, *tmp; |
1277 int length, rows; | 1301 int length, rows; |
1278 | 1302 |
1303 GL_ActivateRenderer(renderer); | |
1304 | |
1279 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { | 1305 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { |
1280 /* FIXME: Do a temp copy to a format that is supported */ | 1306 /* FIXME: Do a temp copy to a format that is supported */ |
1281 SDL_SetError("Unsupported pixel format"); | 1307 SDL_SetError("Unsupported pixel format"); |
1282 return -1; | 1308 return -1; |
1283 } | 1309 } |
1321 GLint internalFormat; | 1347 GLint internalFormat; |
1322 GLenum format, type; | 1348 GLenum format, type; |
1323 Uint8 *src, *dst, *tmp; | 1349 Uint8 *src, *dst, *tmp; |
1324 int length, rows; | 1350 int length, rows; |
1325 | 1351 |
1352 GL_ActivateRenderer(renderer); | |
1353 | |
1326 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { | 1354 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) { |
1327 /* FIXME: Do a temp copy to a format that is supported */ | 1355 /* FIXME: Do a temp copy to a format that is supported */ |
1328 SDL_SetError("Unsupported pixel format"); | 1356 SDL_SetError("Unsupported pixel format"); |
1329 return -1; | 1357 return -1; |
1330 } | 1358 } |
1358 } | 1386 } |
1359 | 1387 |
1360 static void | 1388 static void |
1361 GL_RenderPresent(SDL_Renderer * renderer) | 1389 GL_RenderPresent(SDL_Renderer * renderer) |
1362 { | 1390 { |
1391 GL_ActivateRenderer(renderer); | |
1392 | |
1363 SDL_GL_SwapWindow(renderer->window); | 1393 SDL_GL_SwapWindow(renderer->window); |
1364 } | 1394 } |
1365 | 1395 |
1366 static void | 1396 static void |
1367 GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture) | 1397 GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture) |
1368 { | 1398 { |
1369 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; | 1399 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; |
1370 GL_TextureData *data = (GL_TextureData *) texture->driverdata; | 1400 GL_TextureData *data = (GL_TextureData *) texture->driverdata; |
1401 | |
1402 GL_ActivateRenderer(renderer); | |
1371 | 1403 |
1372 if (!data) { | 1404 if (!data) { |
1373 return; | 1405 return; |
1374 } | 1406 } |
1375 if (data->texture) { | 1407 if (data->texture) { |