Lecture 4 | The wonderful use of low-level drawing interfaces

The previous section introduced the concept of game engines and their role in game development. At the same time, the working methods behind the game engine are also mentioned. At the code level, the game engine is a set of encapsulated implementations of underlying graphics, audio, and operating system interfaces.

On this basis, an example of drawing lines on the game screen is also given. In this example, the line drawing interface function decomposes, combines, and calculates behind the scenes, and hands the drawing work to the underlying drawing interface. This drawing interface is what we are going to talk about today.

Several common drawing interfaces

I have already said before that I will explain the process and details of game development for 2D games, so here I will first introduce several 2DDrawing Interfaces (i.e. API, full name Application Programming Interface). We have selected 5 of the most popular drawing interfaces under Windows and explained them respectively.

1.OpenGL

OpenGL is an old graphics interface. GL is the abbreviation of Graphics Library. So, as the name suggests, OpenGL means open graphics interface. Like DirectX to be discussed next, OpenGL can also create and render 2D and 3D graphics. However, unlike DirectX, it can run on multiple platforms, such as Windows, Linux, macOS and some UNIX, while DirectX can only run in the Windows ecosystem.

OpenGL itself only provides a graphics rendering interface. If you need other functions, such as audio, mouse, keyboard operations, or even creating a form, you need the support of other extension libraries.

2.DirectX

Speaking of DirectX, the name is already familiar to everyone. The original intention of the development of DirectX was to enable game developers to operate various hardware devices as efficiently and quickly on the then-new Windows 95 platform as when writing games on the DOS platform.

In fact, before DirectX was released, Microsoft had included OpenGL in Windows systems. Over time, OpenGL gradually became the industry standard, and DirectX naturally competed with it.

Here, we mainly introduce the two core components in DirectX. The functions of these two core components are closely related to 2D game programming, and you must understand them.

The first is DirectDraw. It was the component responsible for the 2D part of early DirectX. DirectDraw is similar to the GDI I will talk about later. It supports video memory bitmaps instead of only storing bitmaps in memory, so DirectDraw is closer to the hardware. But after the DirectX 7 version, DirectDraw was merged into the Direct Graphics component. Although many people are still using the old version of DirectDraw development kit, DirectDraw has been gradually phased out by Microsoft.

The second is Direct2D. It is the latest 2D component launched by Microsoft, and it appears to replace GDI, GDI + and DirectDraw under Windows. Direct2D can draw 2D graphics through hardware acceleration, and also supports high-quality 2D graphics rendering, such as supporting ClearType rendering, de-aliasing, geometric bitmap drawing and filling, etc.

3.SDL

The full name of SDL is Simple DirectMedia Layer, which literally translates to Simple Direct Media Layer. Strictly speaking, SDL is not an “independent” graphics rendering interface, because it encapsulates the graphics and image rendering interfaces of various operating systems and packages them into unified functions for easy calling. For example, under Windows, it encapsulates DirectX and GDI+; under Linux, it encapsulates Xlib and so on. At the same time, it also provides OpenGL calling functions.

SDL can not only encapsulate existing graphics interfaces, but also provide programming interfaces officially released by SDL. For example, SDL_image, image interface, SDL_net, network interface, etc. Pygame, which I will introduce later, is written using SDL.

Pygame is a game library packaged in Python. You can easily use Pygame to write 2D games. Behind it, the SDL interface is called. So we will use Pygame to do a complete review of the 2D game development process. Although there are many codes and teaching materials about Pygame on the Internet, what we want to talk about is not only how to write Pygame code, but also to analyze the writing logic and programming ideas of 2D games from the Pygame code. In this process, Pygame is just a carrier.

4.GDI

GDI, full name Graphics Device Interface, is also a graphics device interface under Windows. What it does is process the graphical output of Windows programs and is responsible for exchanging information between the Windows system and the drawing program. Fewer and fewer people use GDI. Judging from the convenience of programming and functions such as hardware acceleration, it is obvious that GDI has been replaced by GDI+.

5.GDI +

Under Windows, most programmers who have been exposed to graphics programming will have used GDI +. GDI+ is actually an advanced version of GDI.

GDI + has hardware acceleration function, but GDI does not; GDI is provided in the form of C language interface, while GDI + is provided in the form of C + + and managed classes; from the interface code On a level basis, GDI+ is more programmer-friendly and easier to use.

GDI+ also provides Image processing interfaces, such as Image, Bitmap and other classes, which can be used to read, save, display, and operate various types of images, such as BMP, JPG, GIF, etc. .

There are also differences between the drawing operations of GDI and GDI+. There is a location in GDI called the “current coordinate” (MoveTo). The existence of “current coordinates” is to improve the efficiency of painting.

I also take the process of drawing lines as an example. There is a new line drawn connected to an old line. If you have the “current coordinates” setting, you can logically avoid giving the coordinates of two points (start and end) every time you draw a line; if you draw a line every time, Taking the “current coordinates” as the starting point, after the line drawing is completed, the end position of the line becomes the “current coordinates”.

In fact, this approach exists for historical reasons. There is a saying that comes from the very early Logo language. This language provides educational and entertaining programming education for children. Its drawing logic is that if there is the concept of “current coordinates”, only one recursion is needed to draw lines continuously and finally form a figure. Therefore, many painting interfaces in the later period followed this method. But actually around 2000, people found this method inconvenient, so GDI+ canceled this “current coordinates”.

One reason is inconvenience; another reason is that if the “current coordinates” cannot be determined, errors will occur in the drawing. When using GDI + to draw a line, you can directly specify the coordinate positions of the starting point and end point in the DrawLine function.

How to directly use the drawing interface for game development?

Through the above introduction, do you have a general understanding of the popular drawing interfaces under Windows? Next, you may ask, after I understand the programming interfaces of these graphical interfaces, can I directly use these interfaces to develop games?

The answer is of course yes. Due to the development convenience and versatility of SDL, I take the SDL programming interface as an example to explain how to directly develop games through the graphical interface.

Starting from the most basic, we first need to download the latest version of SDL from the SDL website. The download URL is: http://www.libsdl.org/download-2.0.php

On the downloaded website page, we can see the Source Code column, which is the source code of SDL. Students with a certain programming foundation can download the source code and compile it directly using compilers such as VC++ and MinGW. The compiled header files and library files can be used directly.

If you are not familiar with compilation, you can choose to download Development Libraries, which is the development package after compilation. The website has divided the development environment under Windows into VC++ 32-bit version and 64-bit version, MinGW 32-bit version and 64-bit version. For the sake of teaching and uniformity, as well as taking care of users on various platforms, I recommend using the 32-bit version of MinGW. Because 64-bit Windows is compatible with 32-bit applications. As for the download and installation details of the MinGW compiler and IDE, they will be introduced in a subsequent column.

After the download is completed, decompress the compressed package to any directory. Use the “include” and “lib” in the decompressed “i686-w64-mingw32” directory for the header files and library files.

Next, we set the include path and lib path in the IDE. When linking the program, we need to set the include library files libsdl.a and libsdlmain.a in the IDE, and then we can start writing code in the IDE.

When starting development, first use SDL_Init to initialize. Use this method to pass in an unsigned int type parameter. The parameter list looks like this:

The “Initialize all systems” option, in addition to “Ignore any errors”, includes all the above different initialization systems. Generally, SDL_INIT_EVERYTHING can be used.

Next, we’ll use SDL_CreateWindows to create a form. SDL_CreateWindows supports six parameters, namely: form name, x coordinate displayed on the Windows screen, y coordinate displayed on the Windows screen, width, length, and display mode.

An SDL renderer (SDL_Renderer) will then be created using SDL_CreateRenderer. The renderer parameters are:

You can then use SDL_RenderClear to clear the SDL renderer and use the SDL_RenderPresent method to display the rendering results. Then we need to build a large loop. Within this loop, you can fill in the graphics and image functions or other logic codes supported by SDL to complete the program content of the game. The specific operations will be introduced in detail in a later article.

Within this large loop, we need to use the SDL_Event event system. Capture user events within the loop. For example, to exit the loop, you must click the X close button in the upper right corner. If you click the

When finally exiting the program, use SDL_Quit to clear resources and exit the program.

Let’s take a look at the code that connects these contents in conjunction with this flow chart:

#include <SDL.h>
int main(int argc,char *args[])
{
    SDL_Window* window;
    SDL_Renderer* render;
    SDL_Event e;
    bool q = 0;
    int sdl=SDL_Init(SDL_INIT_EVERYTHING);

After the initialization is completed, we need to create the form and write the subsequent steps:

if(0 <= sdl ){
        // After SDL initialization is completed, create a window titled "SDL Window", the window alignment is center-aligned, and the resolution is 640x480.
        g_pWindow=SDL_CreateWindow("SDL Window",
            SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,
            640,480,SDL_WINDOW_SHOWN);
        if(0 != window)
            render=SDL_CreateRenderer(window,-1,0);
    }
    SDL_SetRenderDrawColor(render,0,255,255,255);
    SDL_RenderClear(render);
    SDL_RenderPresent(render);

Next is the content of the game’s main loop:

while( 0 == q )
    {
       while( 0 != SDL_PollEvent( & amp;e ) )
                {
                    //Detected that the user needs to exit
                    if(e.type == SDL_QUIT)
                        q = true;
                }
    }
    SDL_Quit();
    return 0;
}

This simple example illustrates how to write a game directly using the SDL interface. Directly using other graphical interfaces to write games basically follows the same steps.

Summary

To summarize today’s content, just remember these contents:

Drawing interface The drawing interface actually uses C/C++ language or assembly language to call drawing devices such as graphics cards and memory through the bottom layer of the operating system, and finally makes an interface;

SDL has a uniformly encapsulated drawing interface that you can seamlessly compile and use on various platforms.