In this lesson, we’ll explore how objects are copied in more depth. There are two scenarios where our objects get copied. The first is when a new object is created by passing an existing object of the same type to the constructor:
struct Weapon{/*...*/};
int main() {
Weapon SwordA;
// Create SwordB by copying SwordA
Weapon SwordB{SwordA};
}
This copying process also happens when we pass an argument by value to a function. The function parameter is created by copying the object provided as the corresponding argument:
struct Weapon{/*...*/};
void SomeFunction(Weapon W) {/*...*/}
int main() {
Weapon Sword;
// Create the W parameter by copying Sword
SomeFunction(Sword);
}
The second scenario is when an existing object is provided as the right operand to the =
operator. In the following example, we’re expecting an existing PlayerTwo
to be updated by copying values from PlayerOne
:
struct Weapon{/*...*/};
int main() {
Weapon SwordA;
Weapon SwordB;
// Update SwordA by copying values from SwordB
SwordA = SwordB;
}
As we’ve likely noticed, C++ supports these behaviors by default, even when the objects we’re copying use our custom types. In this lesson, we’ll explore what that default behavior does, and learn how to override it when our classes and structs have more complex requirements.
Modern games and applications often require precise control over display management. In this lesson, you'll learn to retrieve monitor counts, display names, and manage window placement across different displays.
In the context of games, the concepts we cover in this lesson are primarily useful for letting players choose which monitor they want our game to run on:
As we likely noticed, platforms typically add additional decorations to our windows, such as a title bar. These decorations include things like title bars and borders. But what if you need a clean, borderless look?
In this lesson, we’ll cover everything you need to know about customizing SDL2 window styles, from removing decorations to dynamically adding them back.
The following screenshot is from Windows 11. It shows a decorated window on the left and an equivalent window without those decorations on the right:
In this lesson, we'll explore how to control window titles in SDL2 dynamically. We’ll cover how to set, update, and retrieve titles to provide a polished user experience.
As we’ve seen previously, the first argument to SDL_CreateWindow
is a string, representing what we want the title to be. Below, we create a window whose title is "Sample Window":
SDL_CreateWindow(
"Sample Window",
100, 100, 700, 300, 0
);
On some platforms, we can set the opacity of our window. In this lesson, we explore how to adjust the transparency of SDL2 windows using SDL_SetWindowOpacity()
and SDL_GetWindowOpacity()
. We’ll cover how to handle errors, retrieve the current opacity, and understand the practical uses of window transparency.
Opacity is represented by a floating-point number ranging from 0.0
to 1.0
. The default value is 1.0
, which represents a completely opaque window. A window with an opacity of 0.0
is fully transparent.
Intermediate values between 0.0
and 1.0
are used to make our windows semi-transparent. The following window has an opacity of 0.5
:
In this lesson, we’ll explore how to manage multiple windows using SDL2. We’ll also introduce one of the primary use cases for these techniques, which is creating menus and tooltips.
As we might expect, our program can manage multiple windows by performing multiple invocations to SDL_CreateWindow()
:
#include <SDL.h>
#include "Window.h"
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
Window GameWindow1;
Window GameWindow2;
SDL_Event E;
while (true) {
while (SDL_PollEvent(&E)) {
if (E.type == SDL_QUIT) {
SDL_Quit();
return 0;
}
}
GameWindow1.Update();
GameWindow2.Update();
GameWindow1.Render();
GameWindow2.Render();
}
}
Our windows do not need to be created at the same time. We can open additional windows in response to user events. Below, our program opens a second window when the user presses their space bar, and closes it when they press escape:
In this lesson, we'll explore SDL's window management system, learning how to create, configure, and respond to window events.
We'll cover essential concepts like window creation, error handling, and event processing, providing the foundation needed to build robust windowed applications.
Previously, we’ve seen how we can retrieve aspects of our window’s configuration by checking its window flags. However, we don’t need to continuously monitor our SDL_Window
to understand its state.
For most use cases, monitoring the event loop makes more sense. SDL dispatches a wide variety of events reporting actions performed on our window. In this lesson, we’ll explore this in a bit more detail.
In this lesson, we'll explore SDL's window positioning capabilities, from setting initial window locations to handling dynamic repositioning. We’ll cover
This lesson explores the tools SDL2 offers for controlling window sizes and responding to changes. We’ll cover: