- Computers & Software»
- Computer Science & Programming
2D Game Programming In C++ under Windows
In This tutorial Series:
I: 2D Game Programming In C++ under Windows.
II: 2D Game Programming in C++ under Windows: Allegro Basics (For developing Simple Games)
How Do They Do it?
Games aren't new, they have been around us almost from the beginning. And as soon as Computers came, Computer Games followed. I play Games, you play Games, we all play Games. Games like Sim-City, Age Of Empire Series, Skyrim, NFS Series, GTA, and every other of the genre-buster of this decade, as well as the ol' & good Mario, Dave (my fav), Tetris, and the most recent Global phenomenon The Angry Birds. The Influence of Games in our individual as well as social life is Just Undeniable! The recent success of Zynga Games. Yes, the giant behind the social games like Farmville, CityVille etc. proves the point. Gaming Industry is a living, growing & thriving Empire, in its own rights. Games are interactive, Games are addictive & Games make us feel Good!
But at one or other point in our time, we hit, "Whoa! Wish I could do that as well! Wish I could make a game." Sometimes we complain in sheer disgust, "Damn, this is so boring, anyone could make a better Game." Once you're into it, making games, things change, you begin appreciating and seeing games, from the eyes of 'The Creator'.
Again, How Do They Do It!?
If you just say Games, I've written Tic-tac-toe, Soduku Games that run in back-white console. Its only logic and ASCII characters!
But then, when we say Games, we're referring to those glossy, graphical 2D/3D games around us. Mind there are 2.5D 's as well!
Believe me, to start with, its just about loading some images, move them, manipulate them, display some text, and think logic for all of this! So its damn simple, for anyone with even slight programming background!
But All I have done as of yet, is to have my C++ compiler carry out the Algorithm, and output Text To The Console!
Nothing Graphical, nothing catchy! End of the Story? No! You're basically writing programs targeting the DOS or DOS-like environments, i.e. the Consoles. For GUI (Graphical User Environment) Applications and Computer Games, you have to interact with the underlying OS, use APIs (Application-Programming Interface). Graphics Libraries & Game Programming Libraries make this task easier, by abstracting out the Low Level Details, we usually won't like to bother about. Again, C/C++ provides no such capabilities in the language itself, for designing GUIs or fancy Graphics. But using the language, APIs can be utilized for achieving the purpose.
What was graphics.h bdw?
Still obsessed with Turbo C++ 3.0?It was part of Borland's Compiler's Graphics Library, and it targeted the DOS environment. Its available for gcc compiler, and you can perform low level routines, like drawing and rotating shapes. And we use something similar here, but much more powerful, and updated, and can give you descent GUIs for Games, because focus is on how to implement Game Logic flexibly, so we can't spend all the time in plotting a line.
You'll see, soon enough.
To a computer programmer, with some experience, things are clearer, but not everyone bothers to explore. To others, its just another World, the 'Third Level' ! Well whether a programmer or not, we all have The Curious Person inside of us. And Question of The Day is : "How To Program a Computer Game?"
Wait! Before I take you to the World of Game Programming (believe me, its too deep, and I'm gonna show you the way in), there's something I'd like to mention.
This Tutorial Series shows (Including the tutorials following this one) how to create a 2-D Game. The Programming language is C/C++ (And I'm bound to this, at least for this tutorial, since the library used, is for C/C++) . So, you should be familiar to C/C++, because this tutorial is meant to introduce you to Game Programming, not the programming Language itself. We'll use ALLEGRO, a Game Programming Library for C/C++. Its free, pretty Low level, flexible, has almost every feature you'll need for the 2D game, and Its Easy To Get Hold Of!
Another thing is that, the tutorial focuses on compilation on Windows Platform, Windows XP or Windows 7, will do . But if you have Mac/Linux Platform,this tutorial might give you mere pointers, as sole focus of the tutorial is "How to achieve this, this way.." (Sorry..)
No I repeat! Its not the only language you can program a Game in! In fact, its just one among the many. Recently there has been an surge of free Game Engines, which require you to only implement the Game Logic, through some easy Graphical Interface Eg. SCRATCH, Blender,Game Maker etc. But they are abstracting out the Details, and as such have low learning curve as pros but cons? Lots of inflexibility associated with them + Its like a magic box, and over-simplified. With some workable Lower Level API's, you can make descent Game Engines of your own!
What are Game Engines?
Basically Game Engines are bunch (huge one) of codes, which enable you to use the established template, to create similar Games. For eg. 'The Age Of Empire' engine, upon which the games in the series were built. Haven't you heard about about Quake Game Engine? There are Game Engines with Graphical Interface as well. And once you know how to design and implement GUI's, its just a matter of time, you've made a Game Engine of your own.
But again, you could mug your hands in Windows APIs (Which I've not courage'd much as of yet), low level Graphics, use DirectX/OpenGl, and do something awesome .
So Game programming can be done in every language that provides provisions to draw Graphics on screen! In a varied number of ways.
To our purpose..
As mentioned, our sole focus in 2D Game Programming in C++ under Windows. We'll use Allegro: A Game Programming Library, and its pretty low level. Let's see what we'll need.
Tools you'll need
First, You'll need a Compiler & preferably an IDE. We'll use DevC++ IDE, old, but simple, free, n open source and works good with the game programming library we'll use here (ALLEGRO). For Windows Users, you can download it from here
Install It, without hesitations :D
Second, you need the Programming Library itself, ALLEGRO.
ALLEGRO stands for Allegro Low LEvel Game ROutines. Yes, its another Recursive Acronym!
Why Do we need Allegro?
Because, C/C++, unlike JAVA or other languages, don't come with high level Graphics or Advanced Audio/Video or I/O required for Computer Games. It was written by Shawn Hargreaves in the early 1990s, as Wikipedia has it!
Now, it's a Giftware, and comes with lots of examples & good documentation.
It stands for Allegro Low LEvel Game ROutines. It doesn't come preinstalled. DevC++ IDE will minimize your effort in the installation part.
Start DevC++. Go to Tools->Check for Updates/Packages. Select DevPak Server- devpaks.org Community Devpaks. Check For Updates.
After sometime, you'll see a list of DevPaks. From Groups, Select Allegro. Now mark the following:
- Allegro 4.2.2
- Allegro supplement
- AllegroFont (Because, By default Allegro supports PCX Fonts, but you would use TTF Fonts most of the time.)
These will get installed automatically.
You'll need a few more libraries, to make things easier for you.
1. http://devpaks.org/details.php?devpak=56 (AlGif library)
2. http://devpaks.org/details.php?devpak=134 (AlPng Library)
3. http://devpaks.org/details.php?devpak=70 (JPGalleg Library)
The last 3 are required to work with Gif, png, and JPEG Images. ALLEGRO, by default supports BItmap Images.
Now install these. There shouldn't be any problem in this part.
Third , you need some Graphics Editing/ Picture Manipulation/Creation Suite. GIMP, is free & Opensource. If you've got Adobe Photoshop, that's great as well. Why?
Because, its why you play games so deep! Art! Game Characters, Scenarios, Textures, you love them, don't you? But right now, I'll suggest you, to focus less on this part, and more on the programming instead. Well I had no option, I'm bad at sketching/painting. So for me, I discovered that, there are large collections of Art in Public Domain, i.e Royalty Free & come without any copyrights.
These are a few websites:
you'll find lots of free resources on the Internet, which are good to give you a Head-Start!
Also, you might need a MIDI music editor, for composing some in-game music. Mp3 tracks can also be used. Just with some more downloads. We'll talk more on that later.
The Action Plan
By now, you've got your system ready for some Game Programming, and you'll draw out some Imagination soon. But before that, Have we yet planned it Yet?
A Game is just another expression of art, in expression, in interaction, which makes it a unique form of art.
What do we need?
1. Story or Plot. Keep it Simple, and you might learn a lot, without getting dry & frustrated!
2. Game Logic- The Heart Of the Game.
3. Allegro Basics
And finally, A Cup of Coffee (You, not Me)...
...and Lots of Patience!
So Where Shall we Start?
Simple, didn't we determine? Here we'll do some very simple things. Why? Ok you said 2D but Why Not a Mario Clone or Zelda Style Game?
Hold On, we're just beginning..we must start light! You can't expect to learn a new thing and doin' miracles with it at the same time. We must start very simple. Later you'll be creating better & more advanced games on yourself.
After we've learned Allegro basics, we'll make, say A Basketball bounce on your flicks. We'll start out simple, very simple, so that you can learn How to use the Graphics, and implement Game Logic, and later you can expand the Game, steadily. Here the journey begins, I'll show you "How to do this & that and create something good"...
Believe it or Not
Games are easy to make, once you've levelled up, and are comfortable with the Graphics & the Logic.
For instance, right now, I can create 2D Games, quite pretty and playable games. Its quite clear to me, using the ways I work. But I won't be making 3D games. Simply, because I've never attempted one. So once you learn simpler things, you'll be working at more difficult aspects, comfortably.
But one thing I'll assure, Game Logic, is what'll remain unchanged. In programming, this is the most vital step, developing 'Logic' for solving a particular problem. Second thing is playablility and presentation. The small Game like thing we'll create subsequently, in this tutorial series, won't be neither, because its simply there to share space with a Game programming tutorial. Its not a demo, its just a tutorial.
Where To Go from here?
That was just the Introduction, and we'll visit the Game at last! Before that, we must learn 'Allegro Basics'. In the Next Tutorial, we'll see how to use Allegro Game Library to create the Game.
Next Tutorial: Allegro Basics