Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Want to start programming your game?! Allegro is here!

C++ Stuff belonging in global scope pretty much

Want to start programming your game?! Allegro is here!

Postby Ruddie on Wed Sep 23, 2009 12:52 pm

Well, here is a tutorial for Allegro!
The easiest program language using C++ for game Programming/making!


1.1 Introduction & Installing the Library

If you notice anything that isn't correct/isn't working for you. Please reply and I'll fix it/help you with your problem (as long as it's not to complicated.. as I'm not that far in allegro)

what do you need to know to complete this tutorial?
Some basics, like:

- Int, char, bool
- Loops

I recommend that you at least saw 1-20 from antiRTFM's tutorials.

If you want to continue doing even more Allegro, then please. View a little bit more (up to 30)

Well, lets get started!

Now, the thing about Allegro is that you got to include the Allegro library.
Unfortunately, this is not just done by typing #include <allegro.h>, but you got to download the library.

How this is done:
Dev-C++,

I got this compiler (just cuz it's INCREDIBLY easy with this one).
Go to 'tools>>check for updates/packages'.
A window will open, from here select 'devpaks.org' from the drop down menu.

Press check for updates, and their you go, all the library's that you can download from the site!

Now scroll down and look for Allegro (just pick the newest version).

All you got to do now is, after you created your project add the library to it.

How are you going to do this? Well, easy!
Go to 'Projects>>Project Options'.
Click on the Parameters tab.

Click on the add library or object button.
Go to the directory where you installed dev-c++, and find the file liballeg.a
This should be in 'Dev-c++/lib'


Visual Studios

Instead of typing a whole tutorial on Visual Studios edition there are a couple links here to use. Go here http://www.allegro.cc/files/ to get the Allegro files needed. To know what version of Visual Studios have and what service pack is installed go to help menu and click on about Microsoft Visual Studios. Then look for the version and service pack it is. This will tell you which file to download. After you download and extract put the folder somewhere convenient easy to find(DO NOT put it in the project your making)This needs to get accessed every time you make an Allegro project.
You now have two choices set up the include and library every single time you make a project OR do it once and be over with it. Since I'm such a nice guy I will explain both. These methods are how to install any kind of libraries if you decide Allegro is not for you this tut will help for the most part. We will do the one time option first.

1 time option:
In Visual Studios with you project open got to Tools/Options... and then open the tree Projects and Solutions and click on VC++ Directories. In that there should be a drop down list under Show directories for: it will say Executable Files set that to Include Files and click on the new folder button or scroll down to the empty line and double click there should be three (...) click on the that on go to the Allegro folder and click on include folder and then say select folder. Now do the same instead of Include Files set it to Library Files and do the same except choose the Lib folder and set. You are now set up in this part and don't have to do it anymore.

2 every time option:
Go to your Project Properties and open up your Configuration Properties tree go down and click on C/C++ and click on it or open the tree and click on general (same thing)Click on Additional Include Directories do the same thing as above find and set include folder. Then either click on Linker or open tree and click on general find Additional Library Directories and you guessed it find and set the Lib folder.

Ta-dah, you are set up and hopeful ready for the next part

Now go to this site http://www.allegro.cc/docs/windows-msvc7-use.html and it will tell how to set up a project in Visual Studios.( I believe win32 project and not win32 console makes a difference) I didn't see it mentioned on the tutorial I may have missed it but if it compiles but then gives an error when you attempt to run copy (not cut) the .dll's for the Allegro bin folder (should be three) and paste them in the same folder that the .exe. is in. You should be able to run it then. Here is your test program(it's also on the bottom of the website given)

Code: Select all
#include <allegro.h>
int main()
{
allegro_init();
allegro_message("Hello World");
return 0;
}
END_OF_MAIN();


Well hopefully all went well and now that we have just finished doing software surgery we can actually go and learn some graphic programming.(How embarrassing a antique like dev is 10 times easier to set up, well I guess we can feel like sophisticated geeks with our sophisticated software)
any problems with setting up just ask.
where noobs teaching noobs but having fun in the process.

More compilers will be added soon

Their! wooh now you're all set to start Programming in Allegro!!!
Last edited by Ruddie on Thu Sep 24, 2009 1:45 pm, edited 11 times in total.
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start you epic game? Allegro is here!

Postby Ruddie on Wed Sep 23, 2009 12:52 pm

1.2 Hello Allegro World

Okay, so lets get started!
Lets show you the 'hello world' program of Allegro!

Code: Select all
#include <allegro.h>//includes the allegro library.

int main()//you got to know this, right?
{
    allegro_init(); // this initiates allegro, without it, program won't work.
    set_color_depth(16); // sets the color depth, 16/32-bit should be used the most.
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); //sets the screen sizes to 640x480 and makes it windowed.
    //do something here
}
END_OF_MAIN();//you HAVE to do this, otherwise your program will not run properly.



Now, this program does.. Well ehh nothing at all!!! *woohhh* It just opens the window and .. well that's it =P
Well, now make it do something!

Code: Select all
#include <allegro.h>//includes the allegro library.

int main()//you got to know this, right?
{
    allegro_init(); // this initiates allegro, without it, program won't work.
    install_keyboard();//this instals the keyboard, so you can actually use it!! W00h
    set_color_depth(16); // sets the color depth, 16/32-bit should be used the most.
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); //sets the screen sizes to 640x480 and makes it windowed.
   
    while (!key[KEY_ESC])//makes a while loop, that says: "While the key is not escape, acquire the screen".
    { 
          acquire_screen();
    }   
    return 0;
}
END_OF_MAIN();//you HAVE to do this, otherwise your program will not run properly.


Next one is Picture drawing!
Last edited by Ruddie on Wed Sep 23, 2009 2:27 pm, edited 5 times in total.
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start you epic game? Allegro is here!

Postby Ruddie on Wed Sep 23, 2009 12:52 pm

1.3 Drawing a Picture

Okay, now that's kinda stupid huh?

now for the real ZOMFG IT'S DOING SOMETHING!!!@% part.
Yes here it comes.... >>>>DRAWING a picture!!<<<< W00T.

Code: Select all
#include <allegro.h>

BITMAP *MyPicture;
BITMAP *buffer;

void draw_picture()
{
    draw_sprite( buffer, MyPicture, 20, 20 );
}
     
int main()
{
    allegro_init(); // this initiates allegro, without it, program won't work.
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
   
    MyPicture = load_bitmap( "MyPicture.bmp", NULL);
   
    buffer = create_bitmap( 640, 480 );
   
    while (!key[KEY_ESC])
    { 
          draw_picture();
         
          blit( buffer, screen, 0,0,0,0, 640, 480);
         
          clear_bitmap(buffer);
    }   
     destroy_bitmap(MyPicture); // VERY IMPORTANT!!!!!!!!!!
     destroy_bitmap(buffer); // VERY IMPORTANT!!!!!!!!!!/
     return 0;
}
END_OF_MAIN();//you HAVE to do this, otherwise your program will not run properly.


Now lets explain what's going on please?
Okay, here we go!

BITMAP *MyPicture

Well, this makes a place where you can put in your picture. Of course, the name can be changed to almost anything!

destroy_bitmap(MyPicture);

After you declare a bitmap, you GOT TO HAVE THIS, otherwise you get memory leaks, and you don't want that.
This deletes the bitmap you made. Of course, it can't do any harm if it's just a few bitmaps, but if you got some more.. You're not going to like it.

MyPicture = load_bitmap( "MyPicture.bmp", NULL);

This actually puts the picture in the BITMAP, it's built up like this: NameOfYourBITMAP = load_bitmap( "PlaceWhereYourPictureIs", NULL);
Just use NULL now this stands for the colors you want to use, you can change that later if you only want a certain group of colors. But just leave that for now.

Also, you can not use ALL of the image files in Allegro. I'm only going to use .bmp in this tutorial.

draw_sprite( buffer, MyPicture, 20, 20 );

Now what's that? You can probably guess it already! This draws it to the screen, and it's built up like this:

draw_sprite( PlaceToDrawTo, BITMAP-name, x-coordinate/pixel, y-coordinate/pixel);


Okay, now about the buffer.. Using a buffer should be normal later, but you can also draw something to the 'screen'.

Now, what's a buffer? you can actually see the buffer as a place where we're going to put all the pictures on before your going to put it on the screen!

So, first make a BITMAP named buffer (you can also call it Cake or something, but just call it buffer, okay?).
BITMAP *buffer;


Then, tell it to put an empty 'image' into it, with the length of 640, and the height of 480.
buffer = create_bitmap( 640, 480 );

Then, some where in the while loop(most of the time at the end) type this:
blit( buffer, screen, 0,0,0,0, 640, 480);

This will actually 'draws' the buffer to the screen.
and it's built up like this:

blit( YourBufferName, WhereToDrawTo, 0,0,0,0, length, height)

I'm not going to cover the 0,0,0,0 part. just leave it 0 for now.

And finally, delete EVERYTHING in the buffer.
clear_bitmap(buffer);


Of course.. you got to have the picture in the same file as your main.cpp file is... Otherwise.. you can't make this work.
(You can also use something like C:/documents and settings/....../location of your picture).

Now for those amazing people who like to organize their files. You can use something like this:
(GFX/picture.bmp)

If you got a map named GFX, and inside it the picture.bmp

Well that's it for now! next one if going to be making it movee!!! 0.o
Last edited by Ruddie on Wed Sep 23, 2009 2:28 pm, edited 2 times in total.
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start you epic game? Allegro is here!

Postby Ruddie on Wed Sep 23, 2009 12:53 pm

1.4 Moving The Picture

Now, lets move that picture of yours, shall we?

Code: Select all
#include <allegro.h>

BITMAP *MyPicture;
BITMAP *buffer;

int x;
int y;

void move_picture()
{
     if(key[KEY_UP])
                    y--;
     else if(key[KEY_DOWN])
          y++;
     if(key[KEY_LEFT])
                      x--;
     else if(key[KEY_RIGHT])
          x++;     
}
     


void draw_picture()
{
    draw_sprite( buffer, MyPicture, x, y );
}
     
int main()
{
    allegro_init(); // this initiates allegro, without it, program won't work.
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
   
    MyPicture = load_bitmap( "MyPicture.bmp", NULL);
   
    buffer = create_bitmap( 640, 480 );
   
    while (!key[KEY_ESC])
    { 
          move_picture();
          draw_picture();
         
          blit( buffer, screen, 0,0,0,0, 640, 480);
         
          clear_bitmap(buffer);
    }   

    destroy_bitmap(buffer);
    destroy_bitmap(MyPicture);
}
END_OF_MAIN();//you HAVE to do this, otherwise your program will not run properly.



Yep that's it!! that's it 0.o

declare some variables..

int x for the x coordinate
int y for the y coordinate

You should understand that right?

Now lets look at those new commands

if(key[KEY_UP]) / if(key[KEY_DOWN]) / if(key[KEY_LEFT]) / if(key[KEY_RIGHT])

well, okay that can't be hard to understand? huh....
if you press (key[KEY_THISKEY]) then do ..this...

now, what are we actually doing with those integers?

Then, for example, if you press the button to the left, it will decrease x by 1.
right will increase it by 1...
up will mean decrease y by 1..
down will increase y by 1...

As the upper left corner is 0,0 and the lower left is 640, 480.

Now, we don't want that our player is able to press left and right at the same time. So we use the else if statement.

But, we're not doing:

if ...

else if...

else if...

else if...

because if we did that, our player won't be able to move his picture diagonally (of course, if you don't want him to, then use this).

Well, I suppose that's it again! next one will be about shooting stuff, and making your picture stay inside the bounds of the screen..

Try to figure out how to keep it between the bounds in the meantime, I think most of you are able to figure that one out without reading the next tutorial.


Enjoy your first GAME!!!
Last edited by Ruddie on Wed Sep 23, 2009 2:33 pm, edited 5 times in total.
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start you epic game? Allegro is here!

Postby Ruddie on Wed Sep 23, 2009 1:04 pm

Reserved for more info
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start you epic game? Allegro is here!

Postby Ruddie on Wed Sep 23, 2009 1:05 pm

Reserved for more info
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start you epic game? Allegro is here!

Postby Ruddie on Wed Sep 23, 2009 1:05 pm

Reserved for more info
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start programming your epic game?!

Postby ReiKo on Wed Sep 23, 2009 5:28 pm

Well Allegro is in fact C++ library so... you can't really be "sick" of C++ :)

I appreciate effort though, keep up the good work :)
User avatar
ReiKo
 
Posts: 115
Joined: Sat May 09, 2009 5:11 pm

Re: Sick of C++? Want to start programming your epic game?!

Postby Ruddie on Thu Sep 24, 2009 11:14 am

ReiKo wrote:Well Allegro is in fact C++ library so... you can't really be "sick" of C++ :)

I appreciate effort though, keep up the good work :)


Okay, your right, but well... I mean like: "You don't want to see text, but something that actually looks like a game, this is what you want to do!".
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Sick of C++? Want to start programming your epic game?!

Postby ReiKo on Thu Sep 24, 2009 1:39 pm

Then write it that way because people might get confused when they start working with Allegro library and see that there is lot of standard C++ in it :)

Keep up the good work with tutorials ;)
User avatar
ReiKo
 
Posts: 115
Joined: Sat May 09, 2009 5:11 pm

Re: Want to start programming your game?! Allegro is here!

Postby Ruddie on Thu Sep 24, 2009 1:43 pm

ReiKo wrote:Then write it that way because people might get confused when they start working with Allegro library and see that there is lot of standard C++ in it :)

Keep up the good work with tutorials ;)


Happy now, I changed the subject lol
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Want to start programming your game?! Allegro is here!

Postby DevGeek++ on Thu Sep 24, 2009 5:01 pm

I installed the library sucessfull. I will read the tutorials later :) Keep up the great work ,we want more :P
DevGeek++
 
Posts: 28
Joined: Tue Aug 04, 2009 5:54 am

Re: Want to start programming your game?! Allegro is here!

Postby Iseki on Thu Nov 19, 2009 9:02 am

Thank you this helped a lot
I did have one problem. I have Windows Vista
and I need to run this in 16 bits rather than 32 bits
Even if I changed to 32 bits in the program it did not work
Just mentioning this as it took me awhile and program crashing
before I realized this. Its working awesomely now though!
Thanks! Looking forward to some more tutorials
Iseki
 
Posts: 6
Joined: Sat Nov 14, 2009 1:58 pm

Re: Want to start programming your game?! Allegro is here!

Postby glinka57 on Sat Dec 05, 2009 3:50 pm

Allegro==primative stone tools :p
User avatar
glinka57
 
Posts: 195
Joined: Fri Feb 27, 2009 7:32 pm

Re: Want to start programming your game?! Allegro is here!

Postby Vevix on Sat Dec 05, 2009 3:54 pm

I must say their API is pretty disgusting, Has anyone tried experimenting with Ogre3D?

http://www.ogre3d.org/
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg. -- Bjarne Stroustrup
User avatar
Vevix
 
Posts: 142
Joined: Sat Oct 10, 2009 9:24 pm
Location: kernel32.dll


Return to General

Who is online

Users browsing this forum: No registered users and 0 guests