Page 1 of 1

Allegro help needed.

Posted: Thu Jul 22, 2010 8:38 am
by fryz
Hi. I am new in Allegro. I know basics of C++. Took a few tutorials of that.

As for allegro, i cannot find any 'advanced' tutorials. Only tutorials for beginners, working on how to detect collision, how draw a picture, insert a sound or an animation.

Is there any websites, which are not so popular on Google, i haven't found, with little advanced tutorials? I am creating my first Allegro game which is Ping Pong. I got the idea, as someone has given an advice to someone else at other forums, to create ping pong as a first game. I currently would like to create a specific speed for a ball and the paddles. Also i am failing a detecting the collision on the paddles. The ball is not reacting to the paddles and just keep bouncing on the screen's walls. I have used this tutorial to make the ball moving by the way:

Code: Select all

#include 
#include 

int x = 100;
int y = 100;

int tempX = 100;
int tempY = 100;

int dir = 1; //This will keep track of the circles direction
            //1= up and left, 2 = down and left, 3 = up and right, 4 = down and right

BITMAP *buffer; //This will be our temporary bitmap for double buffering

void moveCircle(){

    tempX = x;
    tempY = y;

    if (dir == 1 && x != 20 && y != 20){
     
        --x;
        --y;
              
    } else if (dir == 2 && x != 20 && y != 460){

        --x;
        ++y;

    } else if (dir == 3 && x != 620 && y != 20){

        ++x;
        --y;

    } else if (dir == 4 && x != 620 && y != 460){

        ++x;
        ++y;

    } else { 

        dir = rand() % 4 + 1;

    }    
    
    acquire_screen();
    circlefill ( buffer, tempX, tempY, 20, makecol( 0, 0, 0));
    circlefill ( buffer, x, y, 20, makecol( 128, 255, 0));
    draw_sprite( screen, buffer, 0, 0);
    release_screen();
    
    rest(10);

}    

int main(){

    allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
    
    buffer = create_bitmap( 640, 480); 
    
    while( !key[KEY_ESC]){
     
        moveCircle();
   
    }    
    
    return 0;

}
END_OF_MAIN();


Any help is greatly appreciated. I would love to learn these things. But please explain what you write, so i can understand everything clearly.

P.S. generally, for the tutorials, i am looking into creating just a few simple basic games, not a Mario like game. To keep things simple and learn efficiently. Later i would like to move to those Mario style games. But that would be after i would work on a few such style games, to get the methods of creating basic and average stuff good.

Thank you, if you will help me learn Allegro. :)

Re: Allegro help needed.

Posted: Thu Jul 22, 2010 9:34 am
by GroundUpEngine
Welcome to the forums ;)
Taken from my 'Useful C++ Info' Topic wrote: Well Known Allegro Tutorials - http://www.loomsoft.net/resources/alltu ... _index.htm

Re: Allegro help needed.

Posted: Thu Jul 22, 2010 10:17 am
by fryz
Thanks, it seems, that you have a nice community over here, so i decided to register and join it. :) Although, i cannot help anyone much yet. :/
Anyway, that looks like a nice tutorial. It seems, that it can explain me a lot. Long and nice explanations near almost every function written over there. Especially those "Datafiles" for bigger projects. I will get to it right away!

Although, i would like something more advanced, since i went trough about 70 percent of the stuff covered in this tutorial. But it wasn't explained perfectly, so i didn't understood a few functions, so i will go trough these explanations anyway.

Re: Allegro help needed.

Posted: Thu Jul 22, 2010 11:36 am
by Bludklok
The way I learned Allegro was by using that tutorial (http://www.loomsoft.net/resources/alltu ... _index.htm). After that I would read through the API manual (http://alleg.sourceforge.net/stabledocs/en/allegro.html) and learn from there. This would probably be your best bet after knowing everything in the tutorial. ;)

EDIT: Oh and if you don't already know about http://www.allegro.cc you should definitely go there if you need help with any Allegro related issues, their forums are very active.

Re: Allegro help needed.

Posted: Fri Jul 23, 2010 6:56 am
by fryz
That was a very nice tutorial! Explained a lot of things other tutorials did not explain. Although, i have noted a few notes, when doing the exercises, which were not explained over here too. here is a copy:

int main(int argc, char *argv[]) <--- why is it supposed to look like this? Why not just simply int main()?

blit(my_pic, screen, 50,50,100,100,150,150); // Draw from 50,50 to 150,150 on the screen at (100,100) <--- was explained in no place, now i get the additional numbers, instead of just "it should be like that". A little bit tricky though.

Also with timers. Logically it is bad to try to update more than 60 times a second, but then the "ball" (my current image, from the tutorial) was moving so slow... So my question about this part of the tutorial - is it alright to do the BPS_TO_TIMER more than 60?

Okay, now as for the advanced stuff i needed to do with my Ping Pong game. Here is what i think would work. Haven't tried yet, as i have worked on one of my websites a lot, so was doing the tutorial in breaks between the website. I am going to move to another forum system instead of my current one... Anyway, i talk too much as always, here's how i think my Ping Pong game's ball would work:

Code: Select all

install_int_ex(increment_speed_counter, BPS_TO_TIMER(velocity));

int velocity = 60; // still waiting on the answer about this though.
int col1 = FALSE; // paddle of player 1.
int col2 = FALSE; // paddle of player 2.
int dir = rand() % 4 1;

if(dir == 1)
{
	ball_x ++;
	ball_y ++;
}
else if(dir == 2)
{
	ball_x ++;
	ball_y --;
}
else if(dir == 3)
{
	ball_x --;
	ball_y --;
}
else if(dir == 4)
{
	ball_x --;
	ball_y ++;
}

if(col1 == TRUE)
{
	velocity = 0;
	dir = rand() % 2 1;
}
else if(col2 == TRUE)
{
	velocity = 0;
	dir = rand() $ 4 3;
}

if(ball_x < col1_x) // ball is farther than paddle 1.
	col1 = TRUE;
else if(ball_x > col2_x) // ball is farther than padd 2.
	col2 = TRUE;
else if(ball_x > 640) // if ball is off screen, then player 2 loses.
	lose2 = TRUE
else if(ball_x < 0) // if ball is off screen for another side, player 1 loses.
	lose1 = TRUE;

if(col1_y > 480 || col1_y < 0)
	velocity = 0;
else if(col2_y > 480 || col2_y < 0)
	velocity = 0;
Although, i am VERY unsure about the speed timer value as velocity, because the paddle would be in the same position and theorically the screen would freeze and stay that way until the program is turned off. Please comment. Thank you.
And i will try this code, if no comments are made in the evening, sorry, that i just posted it, but i'm really i a hurry. Thank you for understanding.

Re: Allegro help needed.

Posted: Sun Jul 25, 2010 1:16 am
by wearymemory
fryz wrote: int main(int argc, char *argv[]) <--- why is it supposed to look like this? Why not just simply int main()?
The user can specify command-line arguments which the program can use through certain parameters. In this case, argc specifies the argument count, where argv is a vector of user-specified arguments. Look here for more information.
fryz wrote: blit(my_pic, screen, 50,50,100,100,150,150); // Draw from 50,50 to 150,150 on the screen at (100,100) <--- was explained in no place, now i get the additional numbers, instead of just "it should be like that". A little bit tricky though.
That's correct. Consult the API linked to by Bludklok for more information about function parameters:
void blit(BITMAP *source, BITMAP *dest, int source_x, int source_y, int dest_x, int dest_y, int width, int height);
fryz wrote: Also with timers. Logically it is bad to try to update more than 60 times a second...
Not necessarily, but many games function perfectly fine at around 30-40 FPS.
fryz wrote: ...but then the "ball" (my current image, from the tutorial) was moving so slow... So my question about this part of the tutorial - is it alright to do the BPS_TO_TIMER more than 60?
Some part of movement should be based on time (time-based movement), but never in this way. Use separate deltas for the x and y values of your game objects instead of relying on the speed at which you update your game. If you wish to increase the speed of your ball or paddles, then increase the value of your deltas. For example:

Code: Select all

int x;
int y;
int deltaX;
int deltaY;
int radius;
...
if (x < 0) {
	deltaX = abs(deltaX);
}
if (x + radius > SCREEN_W) {
	deltaX = -abs(deltaX);
}
if (y < 0) {
	deltaY = abs(deltaY);
}
if (y + radius > SCREEN_H) {
	deltaY = -abs(deltaY);
}

x += deltaX;
y += deltaY;
If your ball's delta is greater than the width or height of your paddles, then you may need to determine ball-to-paddle collision based on the paddle's bounds intersecting a line segment from the ball's last x and y coordinates to its newest location. If an intersection occurs, then the ball's location and deltas can be altered.
fryz wrote: Although, i am VERY unsure about the speed timer value as velocity, because the paddle would be in the same position and theorically the screen would freeze and stay that way until the program is turned off. Please comment. Thank you.
For this situation, velocity is a poorly chosen variable identifier, and I'm not sure that modifying it would alter the timer handler. If you wish to remove a timer handler, there is a method for that. Otherwise, please explain more about what you're trying to do with this when you're not in a hurry.

Re: Allegro help needed.

Posted: Sun Aug 01, 2010 12:48 pm
by fryz
Hi. Sorry, i was busy for the past few days, as i wrote in the last post, now i can get back to programming, i think. So anyway, let me make sure, that i got it right, the code you gave me:

Code: Select all

    int x; //ball's x.
    int y; // ball's y.
    int deltaX; // pong's x.
    int deltaY; // pong's y.
    int radius; // not sure... ball's size?
    ...
    if (x < 0) { // logically if ball is off the screen, but not logical in this kind of code...
       deltaX = abs(deltaX); //change it's course...?
    }
    if (x + radius > SCREEN_W) { // if ball goes off the screen...?
       deltaX = -abs(deltaX); // ball changes it's course?
    }
    if (y < 0) {
       deltaY = abs(deltaY);
    }
    if (y + radius > SCREEN_H) {
       deltaY = -abs(deltaY);
    }

    x += deltaX;
    y += deltaY;
Well, i kinda don't understand the code, especially the part where it says "delta". Delta is speed? Or something like that? If that's physics or mathematics class, i'm learning it in a different language and different terms... In Latin i know, delta is d, it's used in military organizations... But i don't know anything else about it, tried to do a little googling, but have found only other unrelated stuff, like restourants, hotels, etc.
Your velocity question to my question:
Well, at the time i understood velocity as install_int_ex(increment_speed_counter, BPS_TO_TIMER(velocity)). Now i understand it as if(key[KEY_UP]){ball_y +5;} or another number. Though it goes off the screen with some numbers... Collision is if(ball_y < 0), but it still goes off the screen and doesn't go back.

Re: Allegro help needed.

Posted: Sun Aug 01, 2010 1:27 pm
by dandymcgee
fryz wrote: Well, i kinda don't understand the code, especially the part where it says "delta". Delta is speed? Or something like that? If that's physics or mathematics class, i'm learning it in a different language and different terms... In Latin i know, delta is d, it's used in military organizations... .
Delta is the fourth letter of the Greek alphabet and is used in physics and math to represent change.
The uppercase symbol for delta looks very much like a triangle. Δx ("delta x", or "the change in x") in this context is simply the displacement of x from the previous frame.

http://en.wikipedia.org/wiki/Delta_(letter)

Re: Allegro help needed.

Posted: Sun Aug 01, 2010 2:06 pm
by fryz
Aha, thanks for the explanation. So now as i see the code, it is like DeltaX and DeltaY is the number, by which the ball advances, speed of the ball. But i still don't get the code at this point: how to use that advancement, speed? I currently figured the collision out and made the speed this way: ball_x +3 (higher number fails, as it goes under the screen). It is velocity, not time based i guess. So anyway, i still don't quite get how to use that speed change with the time... :/

Re: Allegro help needed.

Posted: Sun Aug 01, 2010 8:46 pm
by dandymcgee
fryz wrote:Aha, thanks for the explanation. So now as i see the code, it is like DeltaX and DeltaY is the number, by which the ball advances, speed of the ball. But i still don't get the code at this point: how to use that advancement, speed?
It's being used quite clearly at the bottom of wearymemory's code:

Code: Select all

x += deltaX;
y += deltaY;
He is in fact using 'deltaX' and 'deltaY' to represent the ball's X velocity and Y velocity, respectively. I prefer 'xVel' and 'yVel' personally, but it makes no difference what you name the variable.
fryz wrote:So anyway, i still don't quite get how to use that speed change with the time... :/
When talking Physics "speed" is a bad term to use. Velocity and acceleration are more accurate and well-defined. Velocity is how far something moves with respect to time (15m/s "meters per second") whereas acceleration is the amount that the velocity is changing with respect to time (5m/s/s "meters per second per second").

One unit of time can be defined as anything: 1 second, 1 minute, 1 frame.
Unites of space can also be defined arbitrarily: meters, feet, pixels.

An example progression over time:
  • Initial velocity: 0 [unites of space] per [units of time]
    Initial acceleration: 4 [unites of space] per [units of time]
    Initial position: 700 [unites of space]
    Resolution: 800x600

Code: Select all

[Time passed][Position][Velocity][Acceleration]
     0          700       0           4
	  1          704       4           4
	  2          712       8           4
	  3          724      12           4
	  4          740      16           4
	  5          760      20           4
	  6          784      24           4
	  7          812      28           4
	  
	COLLISION WITH RIGHT SIDE OF SCREEN
  Position = 800; Velocity = Velocity * -1
  
	  8          800       0          -4
	  9          796      -4          -4
	 10          788      -8          -4
	 11          776     -12          -4
Hope that helps.

Re: Allegro help needed.

Posted: Mon Aug 02, 2010 1:00 pm
by fryz
Okay, thanks for explanation, but anyway, how i understand this, is like i did before with the ball. Except i did it now in more efficient and controllable way, because this explanation has gave me a lot of ideas, about the gameplay and game features too. The ball is faster now. Working perfectly. And now it detects the collision alright. And i understood what means abs(variable) now. It means to + abs to the variable. At least if we would use my code for an example explanation. :)

However it is not perfect sometimes:

Code: Select all

			if(key[KEY_W])
			{
				if(pong1_col_up == FALSE)
				{
				if(velocity_pong1 < 15)
					velocity_pong1 ++;
				pong1_y = pong1_y - velocity_pong1;
				}
			}
The paddle plusses it's pixels happilly under the screen. I have also tried something like this, but wasn't successeful try:

Code: Select all

				if(pong1_y < 5)
					velocity_pong1 --;
As i udnerstand it, it's not so smooth, the movement like i did it. For ball it's alright, but for paddle... Nop. But yea, i have used for loops for the ball, as it moves automatically, so it plusses differently. My knowledge is not good enough, at least at the moment, i cannot think of something. Well, except a very primitive way - replacing the paddle's place, size, screen size and stuff like that. But no cofortable way to make it work. Any ideas?