Fixed bug 3605 - Software renderer no longer renders after Android screen orientation change
authorSam Lantinga <slouken@libsdl.org>
Sun, 13 Aug 2017 21:12:14 -0700
changeset 11274daea00fb0938
parent 11273 1a13003276fc
child 11275 6dd70238edb7
Fixed bug 3605 - Software renderer no longer renders after Android screen orientation change

Sylvain

This still happens with the current trunk version. (software renderer of testdrawchessboard.c)

When there is a rotation, the window size changed and the internal surface is marked as "surface_valid == SDL_FALSE".
And all further call fails.

SDL_video.c :

2478 void
2479 SDL_OnWindowResized(SDL_Window * window)
2480 {
2481 window->surface_valid = SDL_FALSE;
2482 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SIZE_CHANGED, window->w, window->h);
2483 }

some error set to :
2233 return SDL_SetError("Window surface is invalid, please call SDL_GetWindowSurface() to get a new surface");


So, this seems to be the behavior of the API ...


In the loop() function of testdrawchessboard.c, we can recreate the surface/renderer :

65 if (e.type == SDL_WINDOWEVENT)
66 {
67 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
68 {
69 surface = SDL_GetWindowSurface(window);
70 renderer = SDL_CreateSoftwareRenderer(surface);
71 }
72 /* Clear the rendering surface with the specified color */
73 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
74 SDL_RenderClear(renderer);
75 }

And it displays correctly.
test/testdrawchessboard.c
     1.1 --- a/test/testdrawchessboard.c	Sun Aug 13 21:09:00 2017 -0700
     1.2 +++ b/test/testdrawchessboard.c	Sun Aug 13 21:12:14 2017 -0700
     1.3 @@ -25,6 +25,7 @@
     1.4  
     1.5  SDL_Window *window;
     1.6  SDL_Renderer *renderer;
     1.7 +SDL_Surface *surface;
     1.8  int done;
     1.9  
    1.10  void
    1.11 @@ -59,7 +60,20 @@
    1.12  {
    1.13      SDL_Event e;
    1.14      while (SDL_PollEvent(&e)) {
    1.15 -        if (e.type == SDL_QUIT) {
    1.16 +       
    1.17 +       /* Re-create when window has been resized */
    1.18 +       if ((e.type == SDL_WINDOWEVENT) && (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)) {
    1.19 +
    1.20 +          SDL_DestroyRenderer(renderer);
    1.21 +
    1.22 +          surface = SDL_GetWindowSurface(window);
    1.23 +          renderer = SDL_CreateSoftwareRenderer(surface);
    1.24 +          /* Clear the rendering surface with the specified color */
    1.25 +          SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
    1.26 +          SDL_RenderClear(renderer);
    1.27 +       }
    1.28 +
    1.29 +       if (e.type == SDL_QUIT) {
    1.30              done = 1;
    1.31  #ifdef __EMSCRIPTEN__
    1.32              emscripten_cancel_main_loop();
    1.33 @@ -86,8 +100,6 @@
    1.34  int
    1.35  main(int argc, char *argv[])
    1.36  {
    1.37 -    SDL_Surface *surface;
    1.38 -
    1.39      /* Enable standard application logging */
    1.40      SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    1.41  
    1.42 @@ -100,7 +112,7 @@
    1.43  
    1.44  
    1.45      /* Create window and renderer for given surface */
    1.46 -    window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    1.47 +    window = SDL_CreateWindow("Chess Board", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE);
    1.48      if(!window)
    1.49      {
    1.50          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n",SDL_GetError());