School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,145 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,723 people online right now. Registration is fast and FREE... Join Now!




Game Programming in Linux for Windows Programmers - Part 1

 
Reply to this topicStart new topic

> Game Programming in Linux for Windows Programmers - Part 1, Perfect for those who are new to Linux and familliar with Windows.

WolfCoder
Group Icon



post 15 Sep, 2008 - 09:11 PM
Post #1


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:
IPB Image

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:
IPB Image

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:
IPB Image

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

/*
    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:

CODE

<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:
CODE

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:
IPB Image

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:
IPB Image

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):
IPB Image

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:
IPB Image

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^^):
CODE

/*
    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"):
CODE

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...):
IPB Image

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

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 happy.gif

Next Tutorial

This post has been edited by WolfCoder: 19 Sep, 2008 - 07:24 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

grimpirate
Group Icon



post 16 Sep, 2008 - 12:18 PM
Post #2
Very cool WolfCoder, I look forward to more of these. One question for the n00b in me. Is the sdl library included with Ubuntu or is it something you had to download separately?
Go to the top of the page
+Quote Post

Nykc
Group Icon



post 16 Sep, 2008 - 01:33 PM
Post #3
Nice job wolf - nice to see you on board the Linux bandwagon tongue.gif

anyways allegro is also another good library which is easy to use as well.

I am still trying to figure out how to get gtk to work in Linux. I use Code::Blocks which is a decent IDE. I have yet to try to compile directly from the command line.
Go to the top of the page
+Quote Post

WolfCoder
Group Icon



post 17 Sep, 2008 - 10:47 AM
Post #4
I already submitted part II, and hopefully a moderator will accept it soon. In there you work with SDL to create a swarm of pixel dots. Anyways, to answer your question, SDL must be installed separately and I teach you how to install packs using the Synaptic Package Manager in this very tutorial because you also have to install libc-dev (libc6 / libc6-dev / ect).

I'm a Windows user so I'm writing as though you have only slightly less of an idea of how to use Linux than I do. Using the operating system isn't that important in game programming since once you've got your graphics open, that's all you need to deal with.

I've read other Linux game programming tutorials and they're all mostly awful unless you're a *nix junkie. For the last time, the terminal is not awesome! I might be able to go through the entire series of my tutorials without ever opening that stupid thing. The whole point of an operating system that has windows and graphics is to make everything easy to use. We're GUI users and we demand a GUI, so I'll stay away from that wall of text garbage.

What I cover in the next tutorial each time is unpredictable, I may cover anything from sound to tool programming. The only criteria for this series is that you've started with part 1 and went in order, then you should be fine.

This post has been edited by WolfCoder: 17 Sep, 2008 - 10:59 AM
Go to the top of the page
+Quote Post

lattyware
Group Icon



post 18 Sep, 2008 - 07:36 AM
Post #5
It's worth taking a look at ClanLib (the version in the Ubuntu repos is 0.6 or something, very old, grab from SVN) - it's a great library which is cross platform and powerful.
Go to the top of the page
+Quote Post

WolfCoder
Group Icon



post 18 Sep, 2008 - 07:35 PM
Post #6
I'll use SDL for the tutorials for it being easy, but of course I do encourage all you new Windows to Linux game programmers to give other libraries a try. Note that you should be dual-booting and you might actually develop the game in Windows, but compile in Linux to test so you have 2 versions of the same program. When part 3 of my tutorials are accepted, I teach you how to do that.

This post has been edited by WolfCoder: 18 Sep, 2008 - 07:35 PM
Go to the top of the page
+Quote Post

ChazZeromus
*



post 26 Sep, 2008 - 06:03 PM
Post #7
For me, one amazing thing about linux is, easy to use libraries and API but ultra complex configuration scripts to compile your program due to too many libraries. Good tutorial, I don't feel like going in my docs folder and reading library documentation.
Go to the top of the page
+Quote Post

WolfCoder
Group Icon



post 1 Oct, 2008 - 10:28 AM
Post #8
When giving your game to people, sure it's nice to give them the source codes but for the LOVE OF WHATEVER IT IS YOU BELIEVE IN please provide just the program we can click and run. I absolutely refuse to support any sort of Linux software that REQUIRES me to compile it using the terminal. If I have to open the terminal, no matter how good your program is, I will not support it.

So far Ubuntu and the software people provide for it is working well. When/If I release more of this series, the tutorials contain methods that follow the above.

Also probably in high demand, I'll see how to write an installer that installs the files in your Linux system using a GUI just like Windows since not everyone uses Ubuntu's version of Linux. I loathe make-install, we're going to evolve from apes (screw you intelligent design!) that throw stones to a program called "install-game" with some crate file such as "game.dat".
Go to the top of the page
+Quote Post

Rickster0
Group Icon



post 6 Nov, 2008 - 01:53 AM
Post #9
this is brilliant.....smile.gif
Go to the top of the page
+Quote Post

Tom9729
Group Icon



post 2 Jan, 2009 - 04:59 PM
Post #10
QUOTE(WolfCoder @ 1 Oct, 2008 - 01:28 PM) *

Also probably in high demand, I'll see how to write an installer that installs the files in your Linux system using a GUI just like Windows since not everyone uses Ubuntu's version of Linux. I loathe make-install, we're going to evolve from apes (screw you intelligent design!) that throw stones to a program called "install-game" with some crate file such as "game.dat".

Already been done.

http://icculus.org/loki_setup/

(for example)
IPB Image

-------------

By the way, making a .deb package isn't really that hard and it is preferable to having an installer for your program.

There is a program called "checkinstall" that builds .deb, .rpm, and .tgz packages from your program's Makefile. I wrote a tutorial on making .deb packages awhile back, but you can probably find a better description of the process somewhere on Google.

-------------

Also instead of installing libc6-dev, you should install the build-essential package. Otherwise a nice tutorial. smile.gif

Something I might suggest for future tutorials is to cover cross-compiling, or compiling Windows versions of the game from Linux.
http://www.talpa.dk/?id=23&url=Linux/crossCompiling.html

This post has been edited by Tom9729: 2 Jan, 2009 - 05:13 PM
Go to the top of the page
+Quote Post

Plus
Group Icon



post 4 Feb, 2009 - 09:56 AM
Post #11

::
:: great tutorial,
:: when you showed me how to tell the compiler,
:: how to build your project by modifying "Makefile" ...
::
Go to the top of the page
+Quote Post

antshockey
*



post 2 Nov, 2009 - 02:05 PM
Post #12
Fantastic tutorial.
I have read/tried so many other ones using SDL and none of them have worked.
Thank you very much smile.gif
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 03:43PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month