Search code examples
c++csdlsdl-2

SDL Window Disappears on Movement?


I am writing a program with the SDL_WINDOW_BORDERLESS window flag. Now I want to move the window freely, but I as you know can't as you don't have a window bar in BORDERLESS mode. I came up with the idea of moving the window with the arrow keys. At first this worked wonderfully, but when it came time to display an image. BMP to be exact; the window, the second one of the arrow keys were pressed, the window disappears.

I have tried to change the code all I can but I can't seem to figure out what causes the window to disappear? Nothing seems to work. Does anyone know what causes with and how to fix it?

int right;
int down;

bool isquit = false;
SDL_Event event;
while (!isquit) {
   if (SDL_PollEvent( & event)) {
       switch(event.key.keysym.sym) {
              case SDL_QUIT:
                   isquit = true;
                   break;
              case SDLK_RIGHT:
                   right = right + 10;

                   SDL_SetWindowPosition(window, right, down);
                   break;
              case SDLK_DOWN:
                   down = down + 10;

                   SDL_SetWindowPosition(window, right, down);
                   break;
              case SDLK_LEFT:
                   right = right - 10;

                   SDL_SetWindowPosition(window, right, down);
                   break;
              case SDLK_UP:
                   down = down - 10;

                   SDL_SetWindowPosition(window, right, down);
                   break;
              case SDLK_ESCAPE:
                   isquit = true;
                   break;
           }
     }
}

Does anyone know anyways of moving a border less window with the arrow keys?


Solution

  • Your right and down variables are automatic locals. They are NOT initialized. So when you set the window position, it moves (probably a great distance) off the screen.

    You should have your compiler warning level set to notify you of the use of unassigned local variables.