GAME PROGRAMMING IN LINUX FOR WINDOWS PROGRAMMERS - PART 1

Perfect for those who are new to Linux and familliar with Windows.

Introduction

Aaa! So you've decided to totally lose your mind and write games that work in Linux! Welcome to Linux, chances are you decided to get Ubuntu since you have absolutely no idea how to use Linux. It seems like you probably have made games for windows before in c or something, and have probably never heard of GCC, SDL, or OpenGL. No! With you it probably has always been DirectX to the death! I might be overreacting and you might have knowledge of any of the above, but if that's the case then you're going to be much better off~ If you meet the above minimum requirements, then this tutorial is for you. To make sure that this tutorial works like it should and so that you don't have to open the terminal so much, I highly recommend you get Ubuntu.

After you've played with the system a bit and got your video card working... You didn't? Well just click on the Hardware Drivers manager as shown in the picture:


You'll see a list of drivers, try and enable your video card if it isn't already enabled. You may notice a significant increase in the performance of the GUI now. Alright... Now what!? I think it's best to start at the bare minimum and compile a simple hello world program. Compiler? What compiler? There may not be a Microsoft Visual Studio in Linux, but it came with GCC with c and c++ versions. Let's stick with c to keep it simple. Anyways, go ahead and make a new folder called hello:


Hello World

Alright, let's take GCC for a spin! Open the folder, right click, and create a new file and name it hello.c as shown in the picture:


Double click on it to open the edit and put the following hello world program inside:

/*
hello.c
A Hello World Example for Linux
written by WolfCoder (2008)
*/

/* Includes */
#include <stdio.h>

/* Main */
int main()
{
/* Welcome the user */
printf("Hello, world!\n");
/* Exit */
return 0;
}


Alright! Er... Now we need to get GCC to compile and run this thing. Linux supports some weird batch files called Makefiles, which are sort of smart .bat files as you are used to in Windows. There's much you can do with a Makefile, but it basically follows this syntax:


<target>: <list of files to depend on>
<command>
<command>
...


The target is a name, for exmaple "compile". After the colon you need to press TAB and enter file names, sperated by a space. If these files are modified from the last time the command was run, the commands below execute. The commands are simple command-line terminal commands. Simple? Well, create a new text file called "Makefile" and don't add .txt or anything, it's just called Makefile. Put the following code in it:

compile: hello.c
gcc -c hello.c
gcc hello.o -o hello
./hello


Don't forget to press enter at the end. Basically, this file means that if "make compile" is typed in the command line, then if hello.c is modified from the last time, it should run the commands below. gcc -c <list of .c files> compiles the files, gcc <list of .o> -o <name of program> puts them together and ./hello executes the program to test it. But... How do you test it? There's a nifty shortcut built into the text editor you are in right now (GEdit to be sure), just Go to Edit, and the Preferences, click the Plugins tab, and then check External Tools if it isn't checked already. The following should be available to you as shown in the picture:


So click it and... What!? stdio.h is not found? How did you manage that!? We need to actually add... *sigh* the standard c library manually. Click on the following shown in the picture to get to the Synaptic Package Manager:


Click search and type "libc6-dev" or "libc" or "libc-dev" until you see a libc-dev package to install. Click the box, mark it for installation and apply changes. It should look something like this (but not exactly):


Hello SDL

That's how you add libraries and things directly. Now go back to the makefile and click the tools->build again. Yay! It tells you "Hello, world!" finally. It's been a bumpy ride, but I'm going to finish off with an application that opens a blank SDL window. SDL is a system for simple 2D games, and it's perfect for getting used to Linux game programming. Go ahead and install the following package like you did before, here's a picture so it did happen:


Alright, make a new folder called "sdltest" and create a new file called sdltest.c with the following code (notice how GEdit highlights c syntax^^):

/*
sdltest.c
Tests SDL under Linux
written by WolfCoder (2008)
*/

/* Includes */
#include </usr/include/SDL/SDL.h>

/* Globals */
SDL_Surface *screen;
SDL_Event event;

/* Main */
int main()
{
/* Initialize SDL */
SDL_Init(SDL_INIT_VIDEO);
/* Set video mode */
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
/* Enter a loop */
while(1)
{
/* Check for user quit */
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
/* Stop the test */
SDL_Quit();
return 0;
}
}
/* Fill with black */
SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0));
/* Update */
SDL_Flip(screen);
}
}


Alright, put a Makefile in there with the following code (the file should ALWAYS BE "Makefile"):

compile: sdltest.c
gcc -c sdltest.c
gcc -lSDL sdltest.o -o sdltest
./sdltest


Notice the lSDL? That has to go BEFORE the other stuff because GCC has (at least for me) placed them all in order. Alright, just to make sure, my folder looks like this (I have already tried out the program as you can see from the extra files... you should tools->build too...):


And finally, we've come to a landing! The final result is a black window of SDL:


Conclusion

From here, now all you have to do is learn SDL. There's nothing else Linux-related you have to worry about anymore for the time being, but keep an eye out for any more Linux Tutorials for such things as using the tools and libraries to make games, and how to build a Windows version of your game (it's actually sort of the same way, only with a Windows port of GCC).

Before I let you loose, let me give you a list of tools you can use to help you make games. You can search for these under Add/Remove in Applications. Installing them is easy.

- GIMP (probably already installed) for making images, it's no Photoshop but it does the job.
- Audacity for editing sound files.
- Blender for making 3D models.
...and so much more, browse their software catalog under Add/Remove.

Also, take a look for what they've got for IDEs, I'll use GEdit's build shortcut and makefiles for the sake of simplicity for any demos I make, but using an IDE allows you to organize better your game projects. I hope you enjoyed yourself, and have fun with Linux! Try playing a few of the games you can find under Add/Remove, hehe ^_^