Creating a Window

Adjusting Window Size Dynamically in SDL

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

Abstract art representing computer programming

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.RenderFrame();
  }

  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.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 62 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved