Simple DirectMedia Layer 2.0
Great Artist
Next, the developer needs a renderer, that is, the tool that draws the window. How you do this is up to you. You could use DirectX in Windows, whereas you would normally use OpenGL on Linux. A renderer is generated by the SDL_CreateRenderer()
function:
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,↩ SDL_RENDERER_ACCELERATED |SDL_RENDERER_PRESENTVSYNC);
As its first parameter, this command expects a pointer to the window into which the renderer will later dump all the output. The -1
flag tells SDL to select a renderer with specific properties. The following flags specify what they are. In the example, I want the renderer to use the graphics card's hardware acceleration (SDL_RENDERER_ACCELERATED
) and synchronize the screen buildup with the screen frequency (SDL_RENDERER_PRESENTVSYNC
). SDL_CreateRenderer()
finally outputs a pointer to an appropriate renderer. If this pointer is NULL
, an error occurred, and the reason is displayed by SDL_GetError()
.
You now can tell the renderer to empty the contents of the window:
SDL_SetRenderDrawColor(renderer, 235, 235, 235, 255); SDL_RenderClear(renderer);
The first function sets drawing color to a light gray with an RGB value of 235, 235, 235
. The last parameter of SDL_SetRenderDrawColor()
sets the transparency (more precisely, the value of the alpha channel). In this case, I want the renderer to make light gray opaque. SDL_RenderClear()
then paints the window with the selected color.
Invitation
To display a picture in the window, you first need to load the picture. SDL itself only offers the SDL_LoadBMP()
function, which loads an uncompressed BMP format image into memory. You only need to pass a file name to the function:
SDL_Surface *fig = SDL_LoadBMP("logo.bmp");
As in the legacy SDL 1.2, the result is a pointer to a surface. This data structure simply encapsulates the bitmap in RAM. To load other formats, you need to either load them yourself and fire them into an SDL_Surface
data structure or use a helper library, such as the popular SDL_Image (see the "Accomplices" box).
Accomplices
SDL covers only the game programmer's basic requirements with its function set. For example, it only loads images in BMP format. Because of this, some developers have written additional helper libraries: SDL_net
simplifies access to the network, for example; SDL_mixer
allows mixing of audio material; SDL_ttf
embellishes text using TrueType fonts, and SDL_Image
loads images in popular file formats.
These and several other libraries have even received the status of "official" extensions and found a home on the SDL website. Since the redesign of the home page, they are, however, no longer linked directly [3].
Together with SDL, the four above-mentioned libraries have made the jump to version 2. Essentially, they support the new features in SDL. For example, SDL_Image
can return the image as a texture. The following statement loads a TIFF photo, logo.tiff
, from disk:
SDL_Texture *logo = IMG_LoadTexture(renderer, "logo.tiff");
Because the libraries only work with SDL 2, they are now called SDL2_Image
, SDL2_ttf
, SDL2_mixer
, and SDL2_net
.
Converted
At this point, a small problem arises: The renderer can paint only textures in the window and not surfaces. This means you need to convert the surface into a texture:
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, fig);
Because the surface is now redundant, the developer frees the memory allocated to it:
SDL_FreeSurface(fig);
Finally, you just need to tell the renderer to copy the texture into the window:
SDL_RenderCopy(renderer, texture, NULL,NULL);
The NULL
in the penultimate parameter instructs the renderer to draw the entire texture in the window and not just a section. If the last parameter is NULL
, the renderer fits the texture into the window. If the texture (i.e., the loaded image) has different dimensions from the window, it leads to distortion, as shown in Figure 3.
« 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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.