Hello World in Python SDL2
In this tutorial we'll examine window Hello World program in Python programming language using SDL 2 package.
SDL - is a Simple DirectMedia Layer software library that allows to create video games. There are many other software for creating video games. For example PyGame (it's in Python too and it uses SDL under the cover).
SDL is written on C/C++ programming language and it's cross-platform - it means that program written for Windows OS will work on OSX or Ubuntu. Moreover SDL will work on iOS and Android. We'll use SDL2 in this series of tutorials
To work with SDL in Python language we need to use pysdl2 module. In this tutorial we'll create a so-called Hello World window application using SDL2 and in addition we'll color pixels in the window with random colors.
Python programming language with SDL 2 - Hello World
First let's create just a window with header "Hello World". Here is the code:
Firstly, we import sdl2 module. For now we'll use ext namespace that contains additional features (and make things easier).
Before use any of sdl2 functions we need to call init() - it initializes library to work with operating system.
On next line we create window object with the sdl2.ext method Window(). First argument - title of window - it will be visible in the title bar. size argument defines dimensions of window as tuple: width and height. If you have small monitor you need to change these values to smaller ones.
window.show() statement renders window on screen.
The last two lines are needed to make the window stay on screen. If we try to run the program without them (without the last one more precisely) the window will appear for a moment and will be closed immediately. We'll discuss event processors in following tutorials.
If you run the program now, you'll see standard window for your operating system. You can drag it, close it, minimize. We get this features without any effort. There is some issue with the content of the window. You'll see that window contains everything what was beneath the window before it was started. It's happening because we didn't make any drawing.
SDL2 window with noise
In next example we'll fill the window with colored noise: just pixels with random color. Let's see the code:
Here we additionally imported randint from random module to generate integer random numbers for colors. Also I've put width and height of the window in separate variables as now dimensions of window are referenced in two different places and variables allow to change the size of screen in one place. Now let's examine new code:
We create object renderer. By passing window variable to the constructor we tell the SDL that we want to draw on that window. In next tutorials we'll learn how to work with surfaces in sdl2 and you'll understand what happens here internally.
Then we iterate all pixels of window line by line. With 1600*1000 pixels it took too much time so to make a screenshot I've use the window with size 400*400 pixels.
In these lines we generate random values for each color channel (red, green blue) of pixel we render at this moment. Each channel can have value from zero to 255.
This line draws the pixel. First argument is the list with coordinates of points. In our case we draw only one point at a time. Second argument is color of the point. Though, at this moment the dot is not visible on the screen.
present method of sdl2.ext.Renderer object is used to show the point on the user screen.
When you run this program, initially you'll see the window with content that was beneath the window. And the program will start to fill the pixels from the top-left corner to bottom-right corner with random colors line by line
In this tutorial we encountered the first windowed program. Different operating system have theirs own procedures to create windows, but for us it's all hidden under SDL2 window.show() method. That allows us to start work with graphics right away instead of learning different aspects of internals of operating system. And Python programming language due it's simplicity gives us quick start compared to C/C++ or Java.