|
Content: |
|
|
What is SDL? |
The origins of SDL |
Future of SDL |
References |
About the author |
|
Sam lantinga is the author of the Simple DirectMedia Layer (SDL) Library and head developer of Loki entertainment. He will introduce you to an excellent tool for porting games to Linux. SDL is an ideal tool for porting cross-platform code. It supports many platforms, such as Linux, Solaris, Irix, FreeBSD, and MACOs, this is a major improvement for Linux developers who think they can develop commercial software on Linux. He asked one of the Community's predecessors how SDL made Linux users enjoy the best games on any platform, and how SDL helped developers keep up with the requirements of the next generation of computer games fans. Since Linux was first developed by Linus, Linux has become the dream of all hackers all over the world. One of the most important elements of Linux development is the quality and availability of games on the OS. Games are used for entertainment and leisure. They can increase creativity and expand ideas. The game can also be used to measure the performance of the operating system. As games become increasingly complex, they force each subsystem to approach the limit. Whenever I assemble a system, the first thing I need to do is to load a game and try it out to "test" the performance of each item. The game on Linux has existed for a long time. From the early nettrek to the highly praisedDoom!AndQuake)People can play games on Linux. But the problem is that there are not enough games. No major company creates a sensation-making game for Linux. However, this situation is starting to improve as the operating system becomes increasingly popular. The oldest game on Linux uses the X11 protocol. However, X11 is too slow for games because it is a protocol designed for menu-based applications that run transparently on the network. Games that use it usually have no beautiful pictures and run quite slowly.Doom!It is an exception worth noting. Although X11 is used, it uses the MIT shared memory extension to make the animation smoother and provide realistic 3D effects. Some games also use the svga graphics library svgalib. One of my favorite old games is the gravity war (gravity wars), which makes significant changes to its simulated old Amiga game gravity force. However, the svgalib program can only be used for a few supported video cards.
Early X11 games,Craft). |
Myth 2 (Myth 2)Images produced by Loki |
|
|
Today, game developers have more options. You can still use the X toolbox or full-screen API, such as svgalib or fbcon, to write a game, but they still have many game libraries available. The Simple DirectMedia Layer library is one of the best low-layer game development APIs on Linux. What is SDL? Simple DirectMedia Layer Library (SDL) is one of the few free software libraries used by commercial game developers. It provides cross-platform 2D frame buffer graphics and audio services. It supports Linux, Win32, and BEOs. It also supports other platforms to varying degrees, including Solaris, Irix, FreeBSD, and MACOs. In addition to a large number of services, including threads, macros and CD audio independent of the byte storage order, SDL also provides a simple API that allows you to access your local hardware as close as possible. Using SDL has three advantages: stability, simplicity, and flexibility.
- Stability. If SDL does not provide reliable support to APIs, those enthusiasts and business companies will not be able to use it. Because SDL is used, error correction is added and performance is enhanced, so the API robustness is enhanced. Just like kernel development is carried out step by step, SDL development is also carried out step by step. One part is a reliable and stable API, and the other part is a sandbox with new functions and ideas.
- Simple. SDL is designed as a simple API to implement your ideas with minimal code. For example, I recently transplanted some demo programs from the Linux demonstration group optimum. I replaced their X11 code with the SDL code (see the list below ). As you can see, SDL code is very easy to write and understand.
X11 code
int init_x (int X, int Y, int W, int H, int bpp, const char *Name) { XPixmapFormatValues *formatList; int formatCount; int i; int formatOk; int scanlineLength; XGCValues gcVal; unsigned long gcMask; dis = XOpenDisplay ( NULL ); if ( dis == NULL) { fprintf ( stderr , "Error :/n" ); fprintf ( stderr , " Cannot connect to Display./n"); exit (1); } screen = DefaultScreen ( dis ); depth = DefaultDepth ( dis , screen ); width = DisplayWidth ( dis , screen ); height = DisplayHeight ( dis , screen ); winRoot = DefaultRootWindow ( dis ); winAttr.border_pixel = BlackPixel ( dis , screen ); winAttr.background_pixel = BlackPixel ( dis , screen ); winMask = CWBackPixel | CWBorderPixel; formatList = XListPixmapFormats( dis, &formatCount); if (formatList == NULL){ fprintf ( stderr , " Cannot get pixmap list/n"); exit (1); } formatOk=-1; for (i=0; ibytes_per_line*xim->height,IPC_CREAT|0777); xim->data = SHMInfo.shmaddr = (char *)shmat(SHMInfo.shmid, 0, 0); SHMInfo.readOnly = False; XShmAttach(dis, &SHMInfo); XSync(dis, False); buffer=(unsigned char *)xim->data;#else buffer = (unsigned char *)calloc(W*H, pixmapFormat.bits_per_pixel/8); xim = XCreateImage ( dis , CopyFromParent , depth , ZPixmap , 0 , (char *) buffer , W , H , pixmapFormat.scanline_pad, scanlineLength); if (xim == NULL){ fprintf(stderr, " Couldnt create Ximage../n"); exit(-1); }#endif gcVal.foreground = 0; gcVal.background = 0; gcMask = GCForeground | GCBackground; gc = XCreateGC ( dis , win , gcMask , &gcVal ); if (depth==24) depth = pixmapFormat.bits_per_pixel; return (depth);}
|
SDL code
int init_x (int X, int Y, int W, int H, int bpp, const char *Name) { int i; if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf ( stderr , "Erreur :/n" ); fprintf ( stderr , " Impossible de se connecter au Display/n"); exit (1); } screen = SDL_SetVideoMode(W, H, bpp, SDL_SWSURFACE|SDL_HWPALETTE); if ( screen == NULL ) { fprintf ( stderr , "Erreur :/n" ); fprintf ( stderr , " Impossible de se connecter au Display/n"); exit (1); } SDL_WM_SetCaption ( Name, Name ); for ( i=SDL_NOEVENT; iformat->BitsPerPixel; width = screen->w; height = screen->h; buffer = (unsigned char *)screen->pixels; return (depth);}
|
- Flexible. Return to the optimum DEMO code example above. As long as you port to SDL and confirm some data assumptions, you do not need to modify the code. The demo can run on Win32, BEOs, and Linux console. Flexibility is also reflected in that although the code is completely cross-platform, it will not separate you from the underlying implementation. SDL provides the function sdl_getwminfo (), which allows you to access the dedicated window information of the underlying driver. Loki entertainment software is widely used to interact with their intelligent game Window managers.
Optimum demo edition screen snapshot |
|
|
|
This rock-solid combination of stability, simplicity, and power has brought some extremely fascinating games to Linux, includingMaelstrom,Hopkins F. B. I.,Civilization: Call to power),2 (descent 2),Myth 2: soulblighter (mythii: soulblighter),Iron road tycoon 2 (railroad tycoon II)And so on. The fact that programmers and business companies use this library shows that it is increasingly improving its functionality and stability. This meets the actual needs of the game.
Civilization: Call to power)Image |
2 (descent 2)Image |
|
|
The origins of SDL SDL came out two years ago, when I was working on porting from the Macintosh simulator executor to Win32. I noticed that the same function is basically implemented in the same way on several platforms. All targets require access to the screen, ing mouse and keyboard input, and playing sound. I think: Why don't I write a cross-platform library to provide the basic services that many people will use? Application developers only need to write an API, which greatly simplifies their work and allows them to quickly run their code on multiple platforms to attract as many viewers as possible. I started. More than a year later, the first stable SDL release was born. The first step in creating SDL is to identify the required features. Based on the analysis of executor and previous porting experience, I have a good idea. The next step is to build a valid prototype that runs on Win32 and Linux. I know that the biggest test of the API is to work in the actual application, so the first thing I do is to use it for porting from Executor to Win32. During this period, I constantly add the API subject and port it to BEOs. Then, use the latestDoom!The source code allows it to run smoothly on all three supported platforms in just three days. A year later, I used it to port some of the world's best games to the world's most popular operating system Linux. Although I work for a commercial company, the Library itself is free and reflects the power of open source. Related Libraries SDL is an available large tool set. As a video API, SDL provides a simple frame buffer for customizing bitmap routines or special effects. Using SDL, you can complete a complete archive file with special demo effects and visual enjoyment, but you can also try the GGI and PTC libraries, which are also very popular in the gaming and demonstration fields. As a sound API, SDL supports automatic audio conversion and transparent esound. There is even a complete sample mixing implementation in the sample file, but you can also try the ALSA and GSI libraries. For 3D image generation, it is absolutely good to use Mesa. As a window toolbox, GTK is very popular. Freedom in Linux, part of which is freedom of choice and contribution. It proves the ideal and freedom of Linux.Contribution to SDL SDL is provided by GNU lesser General Public License, which means that free software and commercial programs can use it. After the source code changes are added to the public source code, everyone can share the changes. You are welcome to contribute to SDL and send the patch to me to add it to the SDL development version. A mailing list is provided for developers to subscribe to it by sending a message containing the word "subscribe SDL" to the majordomo@lokigames.com. The email list image is a newsgroup on news: // news.lokigames.com/loki.open-source.sdl. Add this email list to get the prompt about using SDL, the latest announcement and new features of error correction, and the preview version of the project using SDL.Future of SDL Some exciting development directions of SDL in the next year are as follows:
- Game control bar support. A great game controller API is built in the latest Linux kernel. I plan to integrate this support into the SDL event model, which allows any event filtering, callback, and asynchronous event dispatch. Garret banuk has compiled the initial implementation, which will be added to the latest SDL Development Branch.
- Linux frame buffer console support. The latest Linux kernel contains console support for local graphics in the form of a frame buffer driver. Its creator is Geert uytterhoeven. SDL has an initial driver with a local console keyboard and PS/2 mouse driver, which can execute this support.
- SciTech MGL support. SciTech has a great graphics library for Win32, DOS, Linux, OS/2, and QNX. But it is a commercial library, and the VESA console driver is free for Linux. Therefore, the VESA console driver in SDL is supported.
- Mesa is integrated into a Glx Layer. OpenGL is definitely a way to develop 3D games on Linux in the future. However, the Glx API is not as flexible as SDL in event processing. Someone asked to integrate Glx-like extensions into SDL to use it as an input interface for GL-based games.
- Redesign the architecture to allow multi-display and multi-input. The current architecture compiles the SDL function with the stub module and dynamically loads the low-level driver at runtime. This cross-platform execution method will lead to a difficult building process and produce a complex header file. For me, the most important thing is to redesign the build process so that all drivers can be compiled into a library and then dynamically selected at runtime. The advantage of doing so is that multiple activities can be displayed and input at the same time. Imagine the game is running on both the console and remote X11 display!
- More games!The improvement of SDL will make more and more people use this library, and more games will appear on Linux. In http://www.devolution.com /~ On slouken/SDL/intro/toc.html, you can find a good introduction to SDL APIs and a list of projects using SDL APIs.
I am very excited to get involved in Linux games. I hope to continue to work to bring joy to the Linux world, because it is so nice to be able to get involved in Linux games. As they said, penguins also like to be happy. I will soon write a subsequent article about How to Use SDL to write games. References An Introduction to the SDL API The SciTech graphics library Download some of the best games on Linux:
- Maelstrom
- Hopkins F. B. I.
- Descent 2
- Civilization: Call to power
- Mythii: soulblighter
- Railroad tycoon II
Game Developer's newsgroup A reviewMythii: soulblighter, InLinuxWorldMagazine "Games users play", inLinuxWorldMagazine "A whole new civilization built on Linux", inLinuxWorldMagazine About the author Sam lantinga, author of The Simple DirectMedia Layer Library, is now the chief programmer of Loki entertainment software, a company dedicated to producing the best-selling Linux games. He has been dealing with Linux and games since 1995 and has been engaged in various types of doom! Tool porting and Macintosh gamesMaelstromPort to Linux. He is portingIron road tycoon 2 (railroad tycoon II)This is a game that uses a lot of SDL technology and will soon be produced by Loki entertainment software. |