Mason Wheeler to sdl
authorSam Lantinga <slouken@libsdl.org>
Tue, 24 Nov 2009 04:48:12 +0000
changeset 3485e77a69aae239
parent 3484 fed6d4c6c693
child 3486 c87dbbde2bc2
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.
src/video/SDL_video.c
     1.1 --- a/src/video/SDL_video.c	Sun Nov 22 20:53:53 2009 +0000
     1.2 +++ b/src/video/SDL_video.c	Tue Nov 24 04:48:12 2009 +0000
     1.3 @@ -966,13 +966,17 @@
     1.4  }
     1.5  
     1.6  static __inline__ SDL_Renderer *
     1.7 -SDL_GetCurrentRenderer()
     1.8 +SDL_GetCurrentRenderer(SDL_bool create)
     1.9  {
    1.10      if (!_this) {
    1.11          SDL_UninitializedVideo();
    1.12          return NULL;
    1.13      }
    1.14      if (!SDL_CurrentRenderer) {
    1.15 +        if (!create) {
    1.16 +            SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
    1.17 +            return NULL;
    1.18 +        }
    1.19          if (SDL_CreateRenderer(0, -1, 0) < 0) {
    1.20              return NULL;
    1.21          }
    1.22 @@ -1549,25 +1553,23 @@
    1.23          return -1;
    1.24      }
    1.25      renderer = window->renderer;
    1.26 -    if (renderer) {
    1.27 -        if (renderer->ActivateRenderer) {
    1.28 -            if (renderer->ActivateRenderer(renderer) < 0) {
    1.29 -                return -1;
    1.30 -            }
    1.31 -        }
    1.32 -        SDL_CurrentDisplay.current_renderer = renderer;
    1.33 -    } else {
    1.34 -        if (SDL_CreateRenderer(windowID, -1, 0) < 0) {
    1.35 +    if (!renderer) {
    1.36 +        SDL_SetError("Use SDL_CreateRenderer() to create a renderer");
    1.37 +        return -1;
    1.38 +    }
    1.39 +    if (renderer->ActivateRenderer) {
    1.40 +        if (renderer->ActivateRenderer(renderer) < 0) {
    1.41              return -1;
    1.42          }
    1.43      }
    1.44 +    SDL_CurrentDisplay.current_renderer = renderer;
    1.45      return 0;
    1.46  }
    1.47  
    1.48  int
    1.49  SDL_GetRendererInfo(SDL_RendererInfo * info)
    1.50  {
    1.51 -    SDL_Renderer *renderer = SDL_GetCurrentRenderer();
    1.52 +    SDL_Renderer *renderer = SDL_GetCurrentRenderer(SDL_FALSE);
    1.53      if (!renderer) {
    1.54          return -1;
    1.55      }
    1.56 @@ -1582,7 +1584,7 @@
    1.57      SDL_Renderer *renderer;
    1.58      SDL_Texture *texture;
    1.59  
    1.60 -    renderer = SDL_GetCurrentRenderer();
    1.61 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
    1.62      if (!renderer) {
    1.63          return 0;
    1.64      }
    1.65 @@ -1636,7 +1638,7 @@
    1.66      }
    1.67      fmt = surface->format;
    1.68  
    1.69 -    renderer = SDL_GetCurrentRenderer();
    1.70 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
    1.71      if (!renderer) {
    1.72          return 0;
    1.73      }
    1.74 @@ -2270,7 +2272,7 @@
    1.75  {
    1.76      SDL_Renderer *renderer;
    1.77  
    1.78 -    renderer = SDL_GetCurrentRenderer();
    1.79 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
    1.80      if (!renderer) {
    1.81          return -1;
    1.82      }
    1.83 @@ -2290,7 +2292,7 @@
    1.84  {
    1.85      SDL_Renderer *renderer;
    1.86  
    1.87 -    renderer = SDL_GetCurrentRenderer();
    1.88 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
    1.89      if (!renderer) {
    1.90          return -1;
    1.91      }
    1.92 @@ -2314,7 +2316,7 @@
    1.93  {
    1.94      SDL_Renderer *renderer;
    1.95  
    1.96 -    renderer = SDL_GetCurrentRenderer();
    1.97 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
    1.98      if (!renderer) {
    1.99          return -1;
   1.100      }
   1.101 @@ -2331,7 +2333,7 @@
   1.102  {
   1.103      SDL_Renderer *renderer;
   1.104  
   1.105 -    renderer = SDL_GetCurrentRenderer();
   1.106 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.107      if (!renderer) {
   1.108          return -1;
   1.109      }
   1.110 @@ -2345,7 +2347,7 @@
   1.111      SDL_Renderer *renderer;
   1.112      SDL_Window *window;
   1.113  
   1.114 -    renderer = SDL_GetCurrentRenderer();
   1.115 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.116      if (!renderer) {
   1.117          return -1;
   1.118      }
   1.119 @@ -2371,7 +2373,7 @@
   1.120          return SDL_RenderPoint(x1, y1);
   1.121      }
   1.122  
   1.123 -    renderer = SDL_GetCurrentRenderer();
   1.124 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.125      if (!renderer) {
   1.126          return -1;
   1.127      }
   1.128 @@ -2398,7 +2400,7 @@
   1.129      SDL_Window *window;
   1.130      SDL_Rect real_rect;
   1.131  
   1.132 -    renderer = SDL_GetCurrentRenderer();
   1.133 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.134      if (!renderer) {
   1.135          return -1;
   1.136      }
   1.137 @@ -2430,7 +2432,7 @@
   1.138      SDL_Rect real_srcrect;
   1.139      SDL_Rect real_dstrect;
   1.140  
   1.141 -    renderer = SDL_GetCurrentRenderer();
   1.142 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.143      if (!renderer) {
   1.144          return -1;
   1.145      }
   1.146 @@ -2493,7 +2495,7 @@
   1.147      SDL_Window *window;
   1.148      SDL_Rect real_rect;
   1.149  
   1.150 -    renderer = SDL_GetCurrentRenderer();
   1.151 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.152      if (!renderer) {
   1.153          return -1;
   1.154      }
   1.155 @@ -2537,7 +2539,7 @@
   1.156      SDL_Window *window;
   1.157      SDL_Rect real_rect;
   1.158  
   1.159 -    renderer = SDL_GetCurrentRenderer();
   1.160 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.161      if (!renderer) {
   1.162          return -1;
   1.163      }
   1.164 @@ -2578,7 +2580,7 @@
   1.165  {
   1.166      SDL_Renderer *renderer;
   1.167  
   1.168 -    renderer = SDL_GetCurrentRenderer();
   1.169 +    renderer = SDL_GetCurrentRenderer(SDL_TRUE);
   1.170      if (!renderer || !renderer->RenderPresent) {
   1.171          return;
   1.172      }