Simple DirectMedia Layer 2.0
Fit
To fit the image properly, you need to tell the SDL_RenderCopy()
function the size of the texture, as well as the position of the upper left corner of the image in the window. SDL_RenderCopy()
expects this information as an SDL_Rect
.
This data structure simply encapsulates the dimensions of a rectangle. In other words, SDL_RECT pos;
first draws a rectangle. SDL counts the pixels from the left upper corner of the window, starting at 0
. In this example, I want the upper-left corner of the texture to be 150 pixels from the left edge of the window and 100 pixels from the top edge of the window. These two numbers are stored in the variables x
and y
:
pos.x = 150; pos.y = 100;
To define the width and height of the texture window, you use the pos.w
and pos.h
variables. To avoid texture distortion by the renderer, you thus assign the width and height of the texture to it. You can either painstakingly use a paint program to discover these two values, or you can use the SDL_QueryTexture()
function:
SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
This returns the pixel format (RGB, YUV, or similar), a pointer to the actual bitmap in memory, and the width and height of the texture. This information is no longer of interest, which is why SDL_QueryTexture()
keeps it to itself, thanks to the two NULL
s.
At this point, pos
contains the position and size that the renderer will use to paint the texture in the window. You only need to pass a pointer to pos
to SDL_RenderCopy()
:
SDL_RenderCopy(renderer, texture, NULL,&pos);
The results are shown in Figure 4. Using the same principle, you can then draw more textures in the window. The renderer draws the first textures copied first. In the following example,
SDL_RenderCopy(renderer, rear, NULL,NULL); SDL_RenderCopy(renderer, front, NULL, NULL);
the renderer would first paint the rear
texture in the window and then glue the front
texture on top. At the end of the code, you need to tell the renderer to display the updated window contents:
SDL_RenderPresent(renderer);
This last step only seems superfluous at first glance: If the renderer directly output every drawing action in the window, you would be able to see how the image was built up from individual textures. The result would be ugly, flickering animations.
Bus Stop
If you were to put together the code snippets as a program and launch it, you would briefly see a window appear and immediately disappear. To prevent that, you could stop the program just for two seconds:
SDL_Delay(2000);
However, a more elegant approach is to allow the user to quit by pressing a key, clicking on the window, or pressing the close button in the title bar. From this point on, SDL 1.2 users will be back on familiar ground: Keystrokes, mouse clicks, and other inputs are events in SDL.
Please Wait
Each event first ends up in a queue. The next event in the queue is retrieved by the SDL_WaitEvent()
function. If the queue is empty, SDL_WaitEvent()
waits until an event occurs. Then, you only need to query what event is occurring. The resulting switch
/case
monster is shown in Listing 1.
Listing 1
Event Handling in SDL
The helper variable quit
only indicates whether the end of the program has been reached. SDL_WaitEvent()
picks up the next event from the queue and puts it into a data structure of the SDL_Event
type. The structure stores the event type in a variable named type
. If the user presses a key, type
has a value of SDL_KEYDOWN
. The myevent.key.keysym.scancode
event variables reveal what key this was. The key number is translated by SDL_GetScancodeName()
into the corresponding letter or the label.
Because the keyboard on any given system will tend to differ, SDL assigns a symbolic name to each key, which is stored in the myevent.key.keysym.sym
variable. The SDL_GetKeyName()
function converts this to a readable result (Figure 5). An SDL_MOUSEBUTTONDOWN
type event is created if the user clicks on the window. The mouse pointer coordinates are stored in the variables myevent.motion.x
and myevent.motion.y
. A game would evaluate these coordinates and check which game object lies below them.
The user triggers the SDL_QUIT
event on quitting the program. In addition to the three cases presented, SDL supports many more events [4]. For example, if the user releases a key, this triggers an SDL_KEYUP
event.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.