Sunday, October 30, 2011

2D C++ Game: Part 3 - Getting Started

0 comments
So, by now everything must be in place and working. Let's start, with a very basic program to check if Allegro is working properly.

Objective: Initialize a window and fill it with colour.
#include <allegro5/allegro.h>

#define SCREEN_W 640
#define SCREEN_H 480
int main()
{
    al_init();
    ALLEGRO_DISPLAY *screen = NULL;
    screen=al_create_display(SCREEN_W, SCREEN_H);
    al_clear_to_color(al_map_rgb(0,0,255));
    al_flip_display();

    al_rest(5.0);
    al_destroy_display(screen);
    return 0;
}
Explanation:
#include <allegro5/allegro.h>
This is the core library of Allegro, which is required to use the Allegro functions. Just the way we include library files like iostream.h or stdio.h inorder to use cout or printf function.

▪ #define SCREEN_W 640
Then we define a constant screen width and assign value 640.
▪ #define SCREEN_H 480
Then we define a constant screen height and assign value 480, both are later used in the program.

al_init();
This is to initialize the Allegro system and has be to initialized before using any other Allegro function.

ALLEGRO_DISPLAY *screen = NULL;
Here we declare a pointer 'screen' which will be our window/display and then initialize it to NULL.

display = al_create_display(SCREEN_W, SCREEN H);
Then we initialize screen with specified dimensions which we had defined in the very beginning of our program i.e 640, 480. For this, we use al_create_display(int width, int height), which returns NULL if an error has occured.

al_clear_to_color(al_map_rgb(0,0,255));
Then we clear the display to a specific colour using al_clear_to_color(ALLEGRO_COLOR color), An ALLEGRO_COLOR structure describes a color in a device. So, we need ALLEGRO_COLOR structure as parameter, for which we use al_map_rgb(unsigned char r, unsigned char g, unsigned char b) which takes three arguments, the value for the red, green, and blue components of the color ranging from 0-255 respectively, and will return a ALLEGRO_COLOR structure.

al_flip_display();
In Allegro, there are two image buffers, one is the front buffer and another is back buffer. Front buffer is the one displayed currently on the screen and back buffer is the one being drawn in the code. Once we are done drawing on the back buffer, we have to call al_flip_display() which updates the back buffer to the front buffer. This is done inorder to prevent the flickering effect that is caused due to drawing directly on to the front buffer, this technique in computer graphics terminology is called as double buffering.

al_rest(5.0);
Then we used al_rest(double seconds) to pause the system for 5 seconds, orelse the program would execute the next line of the code and window will close before you can even see it.

al_destroy_display(screen);
This should be always called at the end of the program to destroy our window and to free up the memory.

return 0;
Finally, we return 0 to the main function signifying the successful execution of the program.

Output:

Thursday, October 27, 2011

2D C++ Game: Part 2 - Prerequisites

0 comments
Note: If you are not interested to read any of this and want to get straight to the prerequisites then please scroll down the post.

» Application Programming Interface ( API ):

▪ What is API ?
In simple words, API is a interface used by programmers to communicate with the hardware. In game programming, we will be needing some way to of draw graphics on to the monitor via graphics card and play music/sound to our speaker via sound card, that is where API will come into the action.

▪ What APIs are there ?
DirectX and OpenGL are two APIs widely used when it comes to game programming.

DirectX is developed by Microsoft and made to run only on Windows platforms, whereas on the contrary OpenGL(Open Graphics Library) is developed by Khronos Group and is cross platform i.e it can run on various platforms. DirectX takes care of both graphics and audio where as OpenGL is solely for graphics and lacks audio, but not to worry there are other APIs which can be used alongside, like OpenAL (Open Audio Library) made by Creative Technology which can be used along with OpenGL. Anyways, I don't want to run into details about each API as its a very vast topic. For detailed explanation there is always Wikipedia. I have enclosed a link, in case you would want a comparison between both DirectX vs OpenGL.

While browsing through various forums, I found out that for years there have been heated arguments when its comes to topics like DirectX vs OpenGL, both have their own fanbase. Deciding which one to use becomes a big headache for the people who are new into game programming. Anyways, I am not going to use any API directly instead I will be using a multimedia library/game programming library.

» Multimedia Library/Game Programming Library:

▪ What is Library ?
Library is a collection of resources which may include various pre-written subroutines or classes, values etc to carry out specific tasks.

In simpler words, imagine it as a multivitamin capsule. Consider each vitamin packed inside it as subroutine or class which carries out specific task. I know it sounds lame.

▪ Which Game Programming Library to use ?
As by now you must be able to guess what Multimedia Library means. Its a library solely made to handle operations related to multimedia such a graphics, audio, input etc. Library acts a wrapper for various APIs. Using APIs directly can be a very tedious task, which can create complications for the people who are new to game programming.

Using library simplifies the task and prevents a lot of  headache, for instance if you want to create a window and draw a line, all you do is call a function to initialize a window and call xyz function to draw line and pass xy parameters and you are done.

Once again when its comes to choosing which game library to use, is debatable. Everyone will suggest with which they are comfortable. Anyways, there are loads of game libraries out there, some using DirectX or OpenGL and even both. I suggest you to choose a library which suits your project needs and is well documented and has a healthy active community which in long run, will help you in your learning process. There is nothing so called best gaming library. Everything has its pros and cons, ultimately it depends on your requirements and how well you utilize the resources.

▪ Damn, just tell me already what game library are you going to use ?
I am going to use Allegro (Atari Low LEvel Game ROutine) Library. Allegro is a cross-platform, Open source library mainly aimed at video game and multimedia programming. It is capable of  handling common, low-level tasks such as creating windows, accepting user input, loading data, drawing images, playing sounds, etc. and generally abstracting away the underlying platform. However, Allegro is not a game engine: you are free to design and structure your program as you like.

Allegro 5.0 supports the following platforms:
■ Unix/Linux
■ Windows
■ MacOS X
■ iPhone


▪ 
Why Allegro ?
It is just  my personal choice, as I said before choose a game library that has proper documentation and has a healthy community. You can google 'game library', there are loads of other game libraries as well.

▪ Why use Allegro game library, why not make our own ?
Allegro takes care of all the low level tasks like I mentioned before , making a library from scratch to handle these can become very tedious. The whole point is why not use something that has been refined since years. I chose to use game library because I want to put all my focus on building game mechanics.

» Which C++ compiler to use ?
This is completely based on your personal choice but I would recommend you to use Microsoft Visual Studio or Code::Blocks because Allegrowiki has step by step installation guide of these two IDE's, so better to get started right away, instead of wasting time figuring out how to install. I have used Visual Studio and Dev C++ in the past so, I will be trying Code::Blocks this time.
(NoteIDE stands for Integrated Development Environment)


▪ 
Installation Guide:
This tutorials shows you how to install different IDEs and compilers.

Windows
■ Windows, Visual Studio 2008 and Allegro 5
■ Windows, Visual Studio 2010 and Allegro 5
■ Windows Vista, Code::Blocks 10.05 and Allegro 5

Mac OS
■ OSX, Xcode 3, Framework
■ OSX, Xcode 4, Framework

Linux/SVN
■ Fedora and Allegro 5
■ Ubuntu and Allegro 5
■ Debian and Allegro 5

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

List of prerequisites:
Finally, its about time. Let's sum it up and get started.

» C++ : You need to have a working knowledge on basics of C++ and object oriented programming.
» C++ IDE : Code::Blocks
» C++ Compiler : MinGW
» Game library : Allegro
» Graphics editor : Microsoft Paint, GIMP

2D C++ Game: Part 1 - Introduction

0 comments
I have always been a video game enthusiast since my childhood. My first gaming console I owned was 'NES' (Nintendo Entertainment System) which came with a complementary cartridge 'Super Mario Bros' (which I could never complete), I was just simply hooked to it.

Even though technology and gaming industry has steeped up vastly, I still love those 8-bit console games which I used to play in my childhood days, not only because it makes me nostalgic, its the effort, the developers must have put to make these games, considering the fact that in old times technology was still in its juvenile stage. This fascinates me.

I still remember being hooked for hours to 2D DOS games such as Paratroopers, Dangerous Dave, Keen Commander, Crystal Cave, Bio Menace and so on. Over the years the word 'Gaming' has undergone a paradigm shift, now we see awesome sound effects, high definition graphics and so on.

Why game development ?
For 2 reasons, first its a platform where you can bring your thoughts into reality. As a video game lover, I always wanted to make my own games but lacked the proper knowledge (Yes, I am damn lazy). Second, it will help you to improvise your coding skills because game programming is pretty much code intensive and you will get to apply all aspects of programming that you have learned, at some point of time while working on your game.

Anyways, now as I have got a lot of spare time lying around and my brain is rusting, so I thought of putting my mind into doing something productive rather than being sluggish. I have decided to, attempt to make 2D game(s), not sure about the genre though it may be a Side scroller, Role playing game or anything because I have not planned anything yet. Once I am comfortable with the aspects of game programming and stuff, I will plan accordingly as the time progresses. For now I just want things to go with the flow in my learning process. That's all for now, Ciao.

2D C++ Game Development

0 comments

Wednesday, October 26, 2011

Rebirth of the dead blog

0 comments
Since a long time this blog has been dead, so I came up with an idea of reviving this blog into a personal development diary i.e I would jot down, the progress of any project i am working on. Doing so would be beneficial for me to keep track of my learning process and for future reference as well. I will try to maintain the posts in such a perspective that, it will aid in the understanding of others as well.

Today, I changed the name of the blog to 'sunitdevdiary' which was previously named as 'smcybercool' (Sounds much more sensible now), changed the template of the blog and did some tweaks to the code, cleared up the old posts, made a new logo as well, with my favourite childhood paint program 'Ms Paint'. I bet everyone must have scribbled in it, atleast once in their lifetime.