Adjusting Window Size Dynamically in SDL

How can I adjust the size of an SDL window dynamically during runtime?

SDL allows you to adjust the size of a window dynamically using the SDL_SetWindowSize() function. Here's how you can integrate this into your application to change the window size based on user input or other conditions:

#include <SDL.h>
#include "Window.h"

int main(int argc, char** argv) {
  Window GameWindow;
  SDL_Event event;
  bool running = true;

  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      } else if (event.type == SDL_KEYDOWN) {
        switch (event.key.keysym.sym) {
          case SDLK_UP:
            SDL_SetWindowSize(
              GameWindow.SDLWindow, 800, 600);  
            break;
          case SDLK_DOWN:
            SDL_SetWindowSize(
              GameWindow.SDLWindow, 640, 480);  
            break;
        }
      }
    }
    GameWindow.Update();
  }

  SDL_Quit();
  return 0;
}

In this example, pressing the UP key increases the window size to 800x600, while pressing the DOWN key reduces it to 640x480. This dynamic resizing can be triggered by any event or condition in your program.

Creating a Window

Learn how to create and customize windows, covering initialization, window management, and rendering

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Purpose of SDL_Quit() in SDL2
Why do we need to call SDL_Quit()? What happens if we forget?
Understanding Screen Coordinates and Pixels in SDL2
What exactly is a "screen coordinate"? Is it always the same as a pixel?
How Window Position is Determined when using SDL_WINDOWPOS_UNDEFINED
How does does SDL decide where to actually place the window when using SDL_WINDOWPOS_UNDEFINED?
Understanding SDL_PumpEvents() and the Event Loop
What does SDL_PumpEvents() actually do? Why is it in an infinite loop?
Handling the Window Close Event in SDL2
How do I make the window close properly when I click the 'X' button?
The Rule of Three/Five and SDL Resource Management
What is the "Rule of Three" and why did deleting the copy operations satisfy it here? What about the Rule of Five?
Using Raw vs. Smart Pointers for SDL Resources
Is SDL_Window* a raw pointer? Should I be using smart pointers like std::unique_ptr?
Purpose of argc and argv in SDL Applications
What are argc/argv in main for? Do I need them here?
How to Handle Window Close Events in SDL
How can I make my SDL window respond to close events, such as clicking the close button?
Using Multiple Windows in SDL
Can I create and manage multiple windows in SDL, and if so, how?
Improving SDL Window Performance
What are some tips to improve the performance of an SDL window?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant