• (2 Pages)
  • +
  • 1
  • 2

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

#1 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 786
  • View blog
  • Posts: 7,618
  • Joined: 05-May 05

Posted 15 September 2008 - 10:11 PM

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

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:
Posted 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:
Posted 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):
Posted 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:
Posted 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^^):
/*
	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...):
Posted Image

And finally, we've come to a landing! The final result is a black window of SDL:
Posted 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 ^_^

Is This A Good Question/Topic? 2
  • +

Replies To: Game Programming in Linux for Windows Programmers - Part 1

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Posted 16 September 2008 - 01:18 PM

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?
Was This Post Helpful? 0
  • +
  • -

#3 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Posted 16 September 2008 - 02:33 PM

Nice job wolf - nice to see you on board the Linux bandwagon :P

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.
Was This Post Helpful? 0
  • +
  • -

#4 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 786
  • View blog
  • Posts: 7,618
  • Joined: 05-May 05

Posted 17 September 2008 - 11:47 AM

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 September 2008 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 lattyware  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 08-August 05

Posted 18 September 2008 - 08:36 AM

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.
Was This Post Helpful? 0
  • +
  • -

#6 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 786
  • View blog
  • Posts: 7,618
  • Joined: 05-May 05

Posted 18 September 2008 - 08:35 PM

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 September 2008 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 ChazZeromus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 02-August 08

Posted 26 September 2008 - 07:03 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 786
  • View blog
  • Posts: 7,618
  • Joined: 05-May 05

Posted 01 October 2008 - 11:28 AM

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".
Was This Post Helpful? 0
  • +
  • -

#9 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Posted 06 November 2008 - 02:53 AM

this is brilliant.....:)
Was This Post Helpful? 0
  • +
  • -

#10 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Posted 02 January 2009 - 05:59 PM

View PostWolfCoder, on 1 Oct, 2008 - 01:28 PM, said:

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)
Posted 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. :)

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/...sCompiling.html

This post has been edited by Tom9729: 02 January 2009 - 06:13 PM

Was This Post Helpful? 1
  • +
  • -

#11 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Posted 04 February 2009 - 10:56 AM

::
:: great tutorial,
:: when you showed me how to tell the compiler,
:: how to build your project by modifying "Makefile" ...
::
Was This Post Helpful? 0
  • +
  • -

#12 antshockey  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 16-October 09

Posted 02 November 2009 - 03:05 PM

Fantastic tutorial.
I have read/tried so many other ones using SDL and none of them have worked.
Thank you very much :)
Was This Post Helpful? 0
  • +
  • -

#13 thspca  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 07-December 09

Posted 07 December 2009 - 03:24 PM

@WolfCoder
First of all good job on the tutorial, Thanks.
I must disagree on one point, however. The "wall of trash". I LOVE the terminal. And I am NOT a linux guru. If you had a task, say, install "firefox", how much easier would it be to:
#1: Click "Terminal".
#2: Type [code]sudo apt-get install firefox[code]
#4: Press Enter.

(about 10 seconds or so for a slow typist)

OR


Click Add/Remove Programs.
Click in the Search pane.
Type the name of your program.
Select the right one from the list.
Hilight for installation.
Click Install.
Agree to let it perform "the following operations".
(about 20/30 seconds for a slow user)

(The terminal is SIGNIFICANTLY faster than add/remove just for actual visual load as well)


Another example (I use Kubuntu so I am doing this all from memory)

I want to update my computer.
Click Terminal.
Type "sudo apt-get upgrade".
Press Enter.


OR

find the update utility somewhere in the system menu
select all the updates
click "apply all updates"


Yet another example:

I want to run firefox.


Click Terminal. (I leave a copy always open)
type "firefox"
press enter.
(about 5 sec)

OR

Click All Programs
Click Internet
Click Firefox Web Browser.

(about the same time as terminal)



Also it is fun to use the terminal. I have fun scribbling out random commands as relatives stare in amazement and then say "Wow! a real GEEK!" (in a good way :D ).

So that's how I feel about terminal; once again, I like the tutorial, just thought I'd voice my opinion on the terminal.



P.S. What would you do if you had scrambled your graphics in the D.E. and rebooted to find a black screen with a command shell?


You would try to edit your Xorg.conf file or just reconfigure the display manager, right?

But what if you had never learned to use a shell and had to stare in utter hopelessness while contemplating all the lost data involved in reinstalling?

That's when a good knowledge of the command line comes in handy...




Thanks for bearing with me,



-NinjaNumberNine



A transparent terminal:


Posted Image
Was This Post Helpful? 0
  • +
  • -

#14 wheelchairman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-December 09

Posted 14 December 2009 - 07:27 PM

I get following error. How to solve this. Thanks.

Using Makefile from ~/hello
make: Entering directory `/home/jayakumar/hello'
Makefile:3: *** missing separator. Stop.
make: Leaving directory `/home/jayakumar/hello'
Was This Post Helpful? 0
  • +
  • -

#15 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 786
  • View blog
  • Posts: 7,618
  • Joined: 05-May 05

Posted 03 January 2010 - 09:23 PM

You didn't write your Makefile correctly. I've never gotten that error before, but make sure your lines end with ; and you use equal tabs except for the build targets.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2