Window Sizing

Learn how to resize, constrain, and manage SDL3 windows

Ryan McCombe
Updated

This lesson explores the tools SDL3 offers for controlling window sizes and responding to changes. We'll cover:

  • Allowing users to resize windows.
  • Retrieving and setting window dimensions programmatically.
  • Using resize events for dynamic layouts.

Starting Point

A minimal project containing a Window class and a main function with a basic application loop is provided below if you want to follow along.

We've removed the specific event handling logic from main.cpp and the positioning methods from Window.h to keep the code focused on this lesson's topic.

Files

src
Select a file to view its content

Setting the Window Size

As we've seen, we can define the initial size when creating our window using the 2nd and 3rd arguments to SDL_CreateWindow():

SDL_CreateWindow(
  "My Program", // Title
  600, // Width
  300, // Height
  0 // Configuration
);

Resizable Windows

In most desktop environments, users can resize windows by clicking and dragging their borders. We can allow our window to be resized by passing the SDL_WINDOW_RESIZABLE window flag:

SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
);

We cover window flags in detail in our lesson on .

Using SDL_SetWindowResizable()

We can specify whether an existing window can be resized by players using the SDL_SetWindowResizable() function. We pass the SDL_Window pointer as the first argument. The second argument is a bool - true if we want the window to be resizable, or false otherwise:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  // Not setting the resizable flag
  0 
)};

// Update the window to be resizable
SDL_SetWindowResizable(Window, true);

// Disable resizing
SDL_SetWindowResizable(Window, false);

Checking if a Window is Resizable

If we need to check if an SDL_Window is currently resizable by players, we can retrieve its window flags and use the & operator to isolate the SDL_WINDOW_RESIZABLE bit:

bool isResizable(SDL_Window* Window) {
  SDL_WindowFlags Flags{SDL_GetWindowFlags(Window)};
  return Flags & SDL_WINDOW_RESIZABLE;
}

The window flags associated with a window are kept up to date as we use other functions, like SDL_SetWindowResizable(), to update the configuration of our window:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

if (isResizable(Window)) {
  std::cout << "Window is resizable\n";
}

SDL_SetWindowResizable(Window, false);

if (!isResizable(Window)) {
  std::cout << "Not any more!";
}
Window is resizable
Not any more!

Minimum Size

If we want to let players resize our window, we will often want to set some constraints on how large or how small they can make it.

To set the minimum size, we use the SDL_SetWindowMinimumSize() function, passing three arguments:

  1. The pointer to the SDL_Window we want to constrain

  2. The minimum horizontal size

  3. The minimum vertical size

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

SDL_SetWindowMinimumSize(Window, 200, 200);

We can retrieve the currently configured minimum size using SDL_GetWindowMinimumSize(). We pass the SDL_Window pointer and two integer pointers. The integers are updated with the current minimum width and height, respectively:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

SDL_SetWindowMinimumSize(Window, 200, 200);

int minW, minH;
SDL_GetWindowMinimumSize(Window, &minW, &minH);

std::cout << "Minimum Width: " << minW
  << "\nMinimum Height: " << minH;
Minimum Width: 200
Minimum Height: 200

Either of the integer pointers can be a nullptr if we don't care about the minimum size in that dimension:

int minW, minH;

// We only care about the minimum width
SDL_GetWindowMinimumSize(Window, &minW, nullptr);

// We only care about the minimum height
SDL_GetWindowMinimumSize(Window, nullptr, &minH);

Maximum Size

For the maximum window size, we have the SDL_SetWindowMaximumSize() and SDL_GetWindowMaximumSize() functions. These work in the same way as their minimum-size counterparts:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

SDL_SetWindowMaximumSize(Window, 1200, 1200);

int maxW, maxH;
SDL_GetWindowMaximumSize(Window, &maxW, &maxH);

std::cout << "Maximum Width: " << maxW
  << "\nMaximum Height: " << maxH;
Maximum Width: 1200
Maximum Height: 1200

Getting the Current Window Size

We can request the current window size at any time using the SDL_GetWindowSize() function. We pass the window pointer and two pointers to integers. The integers will be updated with the width and height of the window respectively:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

int w, h;
SDL_GetWindowSize(Window, &w, &h);
std::cout << "Size: " << w << "x" << h;
Size: 600x300

If we only care about the size in a single dimension, we can pass a nullptr as the other argument:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

int w;
SDL_GetWindowSize(Window, &w, nullptr);
std::cout << "Width: " << w;

int h;
SDL_GetWindowSize(Window, nullptr, &h);
std::cout << "\nHeight: " << h;
Width: 600
Height: 300

Programmatic Resizing

In addition to letting users resize our window, we can programmatically resize our window at any time. We do this using the SDL_SetWindowSize() function, passing our SDL_Window pointer, and two integers representing the desired width and height respectively:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

int w, h;
SDL_GetWindowSize(Window, &w, &h);
std::cout << "\nSize: " << w << "x" << h;

SDL_SetWindowSize(Window, 500, 700);

SDL_GetWindowSize(Window, &w, &h);
std::cout << "\nSize: " << w << "x" << h;
Size: 600x300
Size: 500x700

Maximising and Restoring Windows

Desktop platforms typically support the idea of having a window maximized within a screen. Maximized windows are sized and positioned to fill almost the entire visible area of the screen, only leaving space for some platform-specific elements like the taskbar on Windows.

If our SDL_Window is resizable, users will typically also be able to maximize it by, for example, double-clicking the title bar in Windows or macOS:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  // If a window is resizable, it will also
  // also be maximizable in supported platforms
  SDL_WINDOW_RESIZABLE
)};

We can also create our window in the maximized state using the SDL_WINDOW_MAXIMIZED flag. This does not require the window to be resizable but, in most circumstances, we'll want a maximized window to also be resizable:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED
)};

Maximising Windows Using SDL_MaximizeWindow()

We can maximize an existing window by passing its SDL_Window pointer to SDL_MaximizeWindow():

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

SDL_MaximizeWindow(Window);

Restoring Windows Using SDL_RestoreWindow()

When a window is maximized, the platform remembers its previous position and size. We can restore a window to this state by passing its SDL_Window pointer to SDL_RestoreWindow():

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE
)};

SDL_MaximizeWindow(Window);
SDL_RestoreWindow(Window);

Checking if a Window is Maximized

We can check if a window is currently maximized by retrieving its SDL_WindowFlags and isolating the SDL_WINDOW_MAXIMIZED bit:

SDL_Window* Window{SDL_CreateWindow(
  "My Program",
  600, 300,
  SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED
)};

if (SDL_GetWindowFlags(Window) &
    SDL_WINDOW_MAXIMIZED) {
  std::cout << "Window is maximized";
}
Window is maximized

Window Resize Events

When a window's size changes for any reason, SDL dispatches a top-level event with the type SDL_EVENT_WINDOW_RESIZED. This happens whether the change was caused by the user dragging the window border or by a programmatic call to a function like SDL_SetWindowSize().

The new horizontal and vertical dimensions of the window are available in the data1 and data2 members of the SDL_WindowEvent struct, which can be accessed via Event.window:

src/main.cpp

#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <iostream>
#include "Window.h"

void HandleWindowEvent(const SDL_WindowEvent& E) {
  if (E.type == SDL_EVENT_WINDOW_RESIZED) {
    std::cout << "Window Resized to: "
      << E.data1 << "x" << E.data2 << '\n';
  }
}

int main(int, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  Window GameWindow;
  SDL_Event E;

  bool IsRunning = true;
  while (IsRunning) {
    while (SDL_PollEvent(&E)) {
      if (E.type >= SDL_EVENT_WINDOW_FIRST &&
          E.type <= SDL_EVENT_WINDOW_LAST) {
        HandleWindowEvent(E.window);
      } else if (E.type == SDL_EVENT_QUIT) {
        IsRunning = false;
      }
    }
    GameWindow.Render();
    GameWindow.Update();
  }

  SDL_Quit();
  return 0;
}
Window Resized to: 831x497

Window Maximize and Minimize Events

Window resize events will also be triggered if the act of maximizing or restoring a window causes the size to change. However, if we need to react to maximizing and restoring actions explicitly, additional top-level events are also reported to help us achieve that: SDL_EVENT_WINDOW_MAXIMIZED and SDL_EVENT_WINDOW_RESTORED.

src/main.cpp

#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <iostream>
#include "Window.h"

void HandleWindowEvent(const SDL_WindowEvent& E) {
  if (E.type == SDL_EVENT_WINDOW_RESIZED) {
    std::cout << "Window Resized to "
      << E.data1 << 'x' << E.data2 << '\n';
  } else if (E.type == SDL_EVENT_WINDOW_MAXIMIZED) {
    std::cout << "Maximized: ";
  } else if (E.type == SDL_EVENT_WINDOW_RESTORED) {
    std::cout << "Restored: ";
  }
}

int main(int, char**) {/*...*/}
Maximized: Window Resized to 2560x1369
Restored: Window Resized to 600x300

Summary

This lesson explored the ways in which we manage our window sizes. Key points to remember include:

  • Use the SDL_WINDOW_RESIZABLE flag to create a resizable window, or to determine if an existing window is resizable.
  • The SDL_SetWindowResizable() function lets us configure whether an existing window is resizable.
  • Use SDL_SetWindowMinimumSize() and SDL_SetWindowMaximumSize() to limit the extent to which a window is resizable.
  • We can programmatically resize a window using SDL_SetWindowSize().
  • SDL_GetWindowSize() lets us determine the current size of a window.
  • Windows can be maximized and restored using SDL_MaximizeWindow() and SDL_RestoreWindow().
  • If we need to react to users resizing, maximizing or restoring our windows, we can monitor the event loop for the corresponding top-level events.
Have a question about this lesson?
Answers are generated by AI models and may not be accurate