goldenFox
goldenFox

Reputation: 67

Explanation of SDL2 windows/surfaces?

I made a short program to test out SDL2, though there are some things I don't understand how they work.

So I have created a window and a surface:

SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;

Now I have this (the part I don't get):

window = SDL_CreateWindow("Window name", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
windowSurface = SDL_GetWindowSurface(window);

So the first line: I use the SDL_createWindow() function to create a window called window I assume. The second line, I got no idea whats going on - explanation?

Finally I have this:

SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
SDL_UpdateWindowSurface(window);

followed by some clean up code to set the pointers back to nullptr and exit the program/destroy windows etc.

Upvotes: 3

Views: 5903

Answers (1)

Alexander Mladenov
Alexander Mladenov

Reputation: 91

The code you have pasted does the following things: Creates a SDL window called "Window name", sets its horizontal and vertical positions to center, sets the window size to 640 x 480 and marks it as shown. The second line acquires the SDL surface bind to this window.

What this means is: Create Window , actually sets up and openGL window and a GPU texture (the Surface, althou SDL2 has seperate class for Textures), to which it is going to draw. Modifying the surface acquired with GetWindowSurface will modify the pixel on the window you have just created.

Bliting is applying a array of pixel to a target texture, in the meaning : hey i got this image/prerendered frame etc.. and I want to apply it to this surface so i can show it. Blit it.

I hope this is helpful : >

You can find more information for SDL here

Official SDL wiki

LazyFoo

LazyFoo provides a full tutorial and explanations of everything for the old SDL, but a lot of the things are the same in SDL2

Upvotes: 4

Related Questions