comparison src/video/SDL_video.c @ 3485:e77a69aae239

Mason Wheeler to sdl I updated SDL, and suddenly my SDL frames stopped working. They'd "initialize" full of gibberish, and I couldn't render anything to them. After a bit of digging, I found a problem: the renderer initialization routine in my SDL frame code wasn't getting called anymore. procedure TSdlFrame.Paint; begin if SDL_SelectRenderer(FWindowID) = -1 then CreateRenderer; SDL_RenderPresent; end; function TSdlFrame.CreateRenderer: boolean; const pf: tagPIXELFORMATDESCRIPTOR = (nSize: sizeof(pf); nVersion: 1; dwFlags: PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER; iPixelType: PFD_TYPE_RGBA; cColorBits: 24; cAlphaBits: 8; iLayerType: PFD_MAIN_PLANE); RENDERERS: array[TRendererType] of AnsiString = ('software', 'gdi', 'opengl', 'd3d'); var pFormat: integer; begin if (SDL_SelectRenderer(FWindowID) = 0) then begin result := true; Exit; end; if FRendererType = rtOpenGL then begin pFormat := ChoosePixelFormat(canvas.Handle, @pf); if not SetPixelFormat(canvas.Handle, pFormat, @pf) then outputDebugString(PChar(SysErrorMessage(GetLastError))); if wglCreateContext(canvas.Handle) = 0 then outputDebugString(PChar(SysErrorMessage(GetLastError))); end; if (SDL_CreateRenderer(FWindowID, SDL_RendererIndex(RENDERERS[FRendererType]), [sdlrPresentFlip3, sdlrAccelerated]) = 0) then begin SDL_ShowWindow(FWindowID); assert(SDL_SetRenderDrawColor(0, 0, 0, 255) = 0); FFlags := SDL_GetWindowFlags(FWindowID); if assigned(FOnAvailable) then FOnAvailable(self); end else outputDebugString(pChar(format('SDL_CreateRenderer failed: %s', [sdl_GetError]))); result := SDL_SelectRenderer(FWindowID) = 0; end; This is a critical issue. The Paint method gets called when the control receives a WM_PAINT message from Windows. I can't create the renderer before then, or it will fail and cause trouble. And when I do create it, it needs to be created with certain parameters. So imagine my surprise when I started debugging into the DLL and found that SDL_SelectRenderer was trying to be "helpful" by creating the renderer for me if it didn't already exist! Now not only does my initialization code not get called, I end up with the wrong renderer and so things don't render as expected when I try to use the window.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 24 Nov 2009 04:48:12 +0000
parents 173199bce642
children c87dbbde2bc2
comparison
equal deleted inserted replaced
3484:fed6d4c6c693 3485:e77a69aae239
964 } 964 }
965 return &_this->displays[window->display]; 965 return &_this->displays[window->display];
966 } 966 }
967 967
968 static __inline__ SDL_Renderer * 968 static __inline__ SDL_Renderer *
969 SDL_GetCurrentRenderer() 969 SDL_GetCurrentRenderer(SDL_bool create)
970 { 970 {
971 if (!_this) { 971 if (!_this) {
972 SDL_UninitializedVideo(); 972 SDL_UninitializedVideo();
973 return NULL; 973 return NULL;
974 } 974 }
975 if (!SDL_CurrentRenderer) { 975 if (!SDL_CurrentRenderer) {
976 if (!create) {
977 SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
978 return NULL;
979 }
976 if (SDL_CreateRenderer(0, -1, 0) < 0) { 980 if (SDL_CreateRenderer(0, -1, 0) < 0) {
977 return NULL; 981 return NULL;
978 } 982 }
979 } 983 }
980 return SDL_CurrentRenderer; 984 return SDL_CurrentRenderer;
1547 if (!window) { 1551 if (!window) {
1548 SDL_SetError("Invalid window ID"); 1552 SDL_SetError("Invalid window ID");
1549 return -1; 1553 return -1;
1550 } 1554 }
1551 renderer = window->renderer; 1555 renderer = window->renderer;
1552 if (renderer) { 1556 if (!renderer) {
1553 if (renderer->ActivateRenderer) { 1557 SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
1554 if (renderer->ActivateRenderer(renderer) < 0) { 1558 return -1;
1555 return -1; 1559 }
1556 } 1560 if (renderer->ActivateRenderer) {
1557 } 1561 if (renderer->ActivateRenderer(renderer) < 0) {
1558 SDL_CurrentDisplay.current_renderer = renderer;
1559 } else {
1560 if (SDL_CreateRenderer(windowID, -1, 0) < 0) {
1561 return -1; 1562 return -1;
1562 } 1563 }
1563 } 1564 }
1565 SDL_CurrentDisplay.current_renderer = renderer;
1564 return 0; 1566 return 0;
1565 } 1567 }
1566 1568
1567 int 1569 int
1568 SDL_GetRendererInfo(SDL_RendererInfo * info) 1570 SDL_GetRendererInfo(SDL_RendererInfo * info)
1569 { 1571 {
1570 SDL_Renderer *renderer = SDL_GetCurrentRenderer(); 1572 SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
1571 if (!renderer) { 1573 if (!renderer) {
1572 return -1; 1574 return -1;
1573 } 1575 }
1574 *info = renderer->info; 1576 *info = renderer->info;
1575 return 0; 1577 return 0;
1580 { 1582 {
1581 int hash; 1583 int hash;
1582 SDL_Renderer *renderer; 1584 SDL_Renderer *renderer;
1583 SDL_Texture *texture; 1585 SDL_Texture *texture;
1584 1586
1585 renderer = SDL_GetCurrentRenderer(); 1587 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
1586 if (!renderer) { 1588 if (!renderer) {
1587 return 0; 1589 return 0;
1588 } 1590 }
1589 if (!renderer->CreateTexture) { 1591 if (!renderer->CreateTexture) {
1590 SDL_Unsupported(); 1592 SDL_Unsupported();
1634 SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface"); 1636 SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
1635 return 0; 1637 return 0;
1636 } 1638 }
1637 fmt = surface->format; 1639 fmt = surface->format;
1638 1640
1639 renderer = SDL_GetCurrentRenderer(); 1641 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
1640 if (!renderer) { 1642 if (!renderer) {
1641 return 0; 1643 return 0;
1642 } 1644 }
1643 1645
1644 if (format) { 1646 if (format) {
2268 int 2270 int
2269 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) 2271 SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
2270 { 2272 {
2271 SDL_Renderer *renderer; 2273 SDL_Renderer *renderer;
2272 2274
2273 renderer = SDL_GetCurrentRenderer(); 2275 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2274 if (!renderer) { 2276 if (!renderer) {
2275 return -1; 2277 return -1;
2276 } 2278 }
2277 renderer->r = r; 2279 renderer->r = r;
2278 renderer->g = g; 2280 renderer->g = g;
2288 int 2290 int
2289 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a) 2291 SDL_GetRenderDrawColor(Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
2290 { 2292 {
2291 SDL_Renderer *renderer; 2293 SDL_Renderer *renderer;
2292 2294
2293 renderer = SDL_GetCurrentRenderer(); 2295 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2294 if (!renderer) { 2296 if (!renderer) {
2295 return -1; 2297 return -1;
2296 } 2298 }
2297 if (r) { 2299 if (r) {
2298 *r = renderer->r; 2300 *r = renderer->r;
2312 int 2314 int
2313 SDL_SetRenderDrawBlendMode(int blendMode) 2315 SDL_SetRenderDrawBlendMode(int blendMode)
2314 { 2316 {
2315 SDL_Renderer *renderer; 2317 SDL_Renderer *renderer;
2316 2318
2317 renderer = SDL_GetCurrentRenderer(); 2319 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2318 if (!renderer) { 2320 if (!renderer) {
2319 return -1; 2321 return -1;
2320 } 2322 }
2321 renderer->blendMode = blendMode; 2323 renderer->blendMode = blendMode;
2322 if (renderer->SetDrawBlendMode) { 2324 if (renderer->SetDrawBlendMode) {
2329 int 2331 int
2330 SDL_GetRenderDrawBlendMode(int *blendMode) 2332 SDL_GetRenderDrawBlendMode(int *blendMode)
2331 { 2333 {
2332 SDL_Renderer *renderer; 2334 SDL_Renderer *renderer;
2333 2335
2334 renderer = SDL_GetCurrentRenderer(); 2336 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2335 if (!renderer) { 2337 if (!renderer) {
2336 return -1; 2338 return -1;
2337 } 2339 }
2338 *blendMode = renderer->blendMode; 2340 *blendMode = renderer->blendMode;
2339 return 0; 2341 return 0;
2343 SDL_RenderPoint(int x, int y) 2345 SDL_RenderPoint(int x, int y)
2344 { 2346 {
2345 SDL_Renderer *renderer; 2347 SDL_Renderer *renderer;
2346 SDL_Window *window; 2348 SDL_Window *window;
2347 2349
2348 renderer = SDL_GetCurrentRenderer(); 2350 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2349 if (!renderer) { 2351 if (!renderer) {
2350 return -1; 2352 return -1;
2351 } 2353 }
2352 if (!renderer->RenderPoint) { 2354 if (!renderer->RenderPoint) {
2353 SDL_Unsupported(); 2355 SDL_Unsupported();
2369 2371
2370 if (x1 == x2 && y1 == y2) { 2372 if (x1 == x2 && y1 == y2) {
2371 return SDL_RenderPoint(x1, y1); 2373 return SDL_RenderPoint(x1, y1);
2372 } 2374 }
2373 2375
2374 renderer = SDL_GetCurrentRenderer(); 2376 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2375 if (!renderer) { 2377 if (!renderer) {
2376 return -1; 2378 return -1;
2377 } 2379 }
2378 if (!renderer->RenderLine) { 2380 if (!renderer->RenderLine) {
2379 SDL_Unsupported(); 2381 SDL_Unsupported();
2396 { 2398 {
2397 SDL_Renderer *renderer; 2399 SDL_Renderer *renderer;
2398 SDL_Window *window; 2400 SDL_Window *window;
2399 SDL_Rect real_rect; 2401 SDL_Rect real_rect;
2400 2402
2401 renderer = SDL_GetCurrentRenderer(); 2403 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2402 if (!renderer) { 2404 if (!renderer) {
2403 return -1; 2405 return -1;
2404 } 2406 }
2405 if (!renderer->RenderFill) { 2407 if (!renderer->RenderFill) {
2406 SDL_Unsupported(); 2408 SDL_Unsupported();
2428 SDL_Renderer *renderer; 2430 SDL_Renderer *renderer;
2429 SDL_Window *window; 2431 SDL_Window *window;
2430 SDL_Rect real_srcrect; 2432 SDL_Rect real_srcrect;
2431 SDL_Rect real_dstrect; 2433 SDL_Rect real_dstrect;
2432 2434
2433 renderer = SDL_GetCurrentRenderer(); 2435 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2434 if (!renderer) { 2436 if (!renderer) {
2435 return -1; 2437 return -1;
2436 } 2438 }
2437 if (!texture) { 2439 if (!texture) {
2438 SDL_SetError("Texture not found"); 2440 SDL_SetError("Texture not found");
2491 { 2493 {
2492 SDL_Renderer *renderer; 2494 SDL_Renderer *renderer;
2493 SDL_Window *window; 2495 SDL_Window *window;
2494 SDL_Rect real_rect; 2496 SDL_Rect real_rect;
2495 2497
2496 renderer = SDL_GetCurrentRenderer(); 2498 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2497 if (!renderer) { 2499 if (!renderer) {
2498 return -1; 2500 return -1;
2499 } 2501 }
2500 if (!renderer->RenderReadPixels) { 2502 if (!renderer->RenderReadPixels) {
2501 SDL_Unsupported(); 2503 SDL_Unsupported();
2535 { 2537 {
2536 SDL_Renderer *renderer; 2538 SDL_Renderer *renderer;
2537 SDL_Window *window; 2539 SDL_Window *window;
2538 SDL_Rect real_rect; 2540 SDL_Rect real_rect;
2539 2541
2540 renderer = SDL_GetCurrentRenderer(); 2542 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2541 if (!renderer) { 2543 if (!renderer) {
2542 return -1; 2544 return -1;
2543 } 2545 }
2544 if (!renderer->RenderWritePixels) { 2546 if (!renderer->RenderWritePixels) {
2545 SDL_Unsupported(); 2547 SDL_Unsupported();
2576 void 2578 void
2577 SDL_RenderPresent(void) 2579 SDL_RenderPresent(void)
2578 { 2580 {
2579 SDL_Renderer *renderer; 2581 SDL_Renderer *renderer;
2580 2582
2581 renderer = SDL_GetCurrentRenderer(); 2583 renderer = SDL_GetCurrentRenderer(SDL_TRUE);
2582 if (!renderer || !renderer->RenderPresent) { 2584 if (!renderer || !renderer->RenderPresent) {
2583 return; 2585 return;
2584 } 2586 }
2585 renderer->RenderPresent(renderer); 2587 renderer->RenderPresent(renderer);
2586 } 2588 }