Thread: Allegro help-o

  1. #61
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    are you using dbl buffer, cause when i use it there is no blur at all. also if you want to see the source for the snake game i made w/ allgero just ask. it is pretty simple, so you should be able to understand it.

  2. #62
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I do not see player::draw()

  3. #63
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    player.draw(buffer);


    i dunno if thats right or not... this is the first time ive even attempted oop ( classes).

    any way here is where im at know
    Code:
    #include <allegro.h>
    
    int counter;
    int left = 0, right = 0;
    BITMAP *buffer = NULL;
    
    class player
    {
       public:
           int x, y;
           BITMAP* ship;
    
           player(): x(300), y(425) { ship = load_bitmap ( "ship.bmp", NULL ); }
          ~player() { destroy_bitmap(ship); }
    
           void  getmove()
           {
               if ( x < 600 && key[KEY_LEFT] )
               {
                  left = 1;
               }
    
               else
               {
                  left = 0;
               }
    
               if ( x > 0 && key[KEY_RIGHT] )
               {
                  right = 1;
               }
    
               else
               {
                  right = 0;
               }
            }
    
            void draw(BITMAP* b)
            {
                draw_sprite ( b, ship, x, 425 );
            }
    };
    
    void update_counter()
    {
         counter++;
    }END_OF_FUNCTION(update_counter);
    
    int main()
    {
       allegro_init();
       install_keyboard();
       set_color_depth(32);
       set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
       buffer = create_bitmap( 640, 480 );
    
       LOCK_VARIABLE(counter);
       LOCK_FUNCTION(update_counter);
       install_int_ex( update_counter, BPS_TO_TIMER(60));
    
       player player1;
    
       do{
           while ( counter > 0 )
           {
               player1.getmove();
    
               if ( left == 1 ) player1.x -= 10;
               if ( right == 1 ) player1.x += 10;
    
               counter--;
           }
    
           clear(buffer);
           player1.draw(buffer);
           blit( buffer, screen, 0, 0, 0, 0, 640, 480);
    
       }
       while ( !key[KEY_ESC] );
    
       allegro_exit();
       return 0;
    }
    END_OF_MAIN();
    it runs.. i see the ship.. i move the ship... when i close the app... it gives me the same error
    What is C++?

  4. #64
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I am not too good at OOP, but Maybe you should make a class called object, and it can draw itself, it has a bitmap loaded representing itself, then make a player class that is inherited from the object class, but adds the player specific stuff, and make an enemy class that is inherited from the object class. Then you do not have to recode as much stuff.

  5. #65
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    what the crap did you just say...
    What is C++?

  6. #66
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    n/m

    its all fixed now

    Flicker free... no errors

    Code:
    #include <allegro.h>  
      
    int counter;  
    BITMAP *buffer = NULL;  
      
    class player  
    {  
       public:  
           int x, y, left, right;  
           BITMAP* ship;  
      
           player(): x(300), y(425), left(0), right(0) { ship = load_bitmap ( "ship.bmp", NULL ); }  
          ~player() { destroy_bitmap(ship); }  
      
           void  getmove()  
           {  
               if ( x > 0 && key[KEY_LEFT] )  
               {  
                  left = 1;  
               }  
      
               else  
               {  
                  left = 0;  
               }  
      
               if ( x < 600 && key[KEY_RIGHT] )  
               {  
                  right = 1;  
               }  
      
               else  
               {  
                  right = 0;  
               }  
      
               if ( left == 1 ) x -= 10;  
               if ( right == 1 ) x += 10;  
            }  
      
            void draw(BITMAP* b)  
            {  
                draw_sprite ( b, ship, x, 425 );  
            }  
    };  
      
    void update_counter()  
    {  
         counter++;  
    }END_OF_FUNCTION(update_counter);  
      
    int main()  
    {  
       allegro_init();  
       install_keyboard();  
       set_color_depth(32);  
       set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);  
       buffer = create_bitmap( 640, 480 );  
      
       LOCK_VARIABLE(counter);  
       LOCK_FUNCTION(update_counter);  
       install_int_ex( update_counter, BPS_TO_TIMER(60));  
      
       player *player1 = new player;  
      
       do{  
           while ( counter > 0 )  
           {  
               player1->getmove();  
               counter--;  
           }  
      
           clear(buffer);  
           player1->draw(buffer);  
           blit( buffer, screen, 0, 0, 0, 0, 640, 480);  
      
       }  
       while ( !key[KEY_ESC] );  
     
       delete player1;  
       allegro_exit();  
       return 0;  
    }  
    END_OF_MAIN();
    What is C++?

  7. #67
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    are any of you having this problem? ---

    once in a while when i press the arrow keys my bitmap flies off of the screen. it seems to happen mainly at the start of the program. and it especially occurs the most when i press space at the beginning and then press an arrow key...does this happen to you guys?

    also, when you just press the arrow key once does your bitmap take a larger gap rather than when you are holding the arrow key? my bitmap flies far to the left when i press the left arrow key, but when i hold down the left arrow key it only moves slightly to the left. do any of you have this problem?

  8. #68
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I use to have that problem, post the code you use to get the input. Here is mine:

    Code:
    while(!key[KEY_ESC])
      {
        if(key[KEY_UP] && y > 5)        // Move up
          y = y - 5;
    
        else if(key[KEY_DOWN] && y < 400)    // Move down
          y = y + 5;
    
        else if(key[KEY_LEFT] && x > 5)      // Move left
          x = x - 5;
    
        else if(key[KEY_RIGHT] && x < 525)   // Move right
          x = x + 5;
    
        else if(key[KEY_SPACE]) // Shoot, if there are already 10 bullets, do nothing
        {
          // Find an empty place in the bullets struct
          for(int i = 0; i < MAX_BULLETS; i++)
          {
            if(bullets[i].exists == 0) // We found an empty slot
            {
              bullets[i].exists = 1; // Make it exist and set its coords
              bullets[i].x = x;
              bullets[i].y = y;
            }
          }
        }

  9. #69
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    josh have you got working bullets... if so can you post some source to show how you did it....

    and tech it sounds like a timer problem to me...

    btw... all 3 of us are making a shooter? wierd... and cool

    but not fair cause you guys are better than me
    What is C++?

  10. #70
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Josh, when I use your calculations they work much better. Thanks.:b:

    Vicious, I'm getting close to getting working bullets. I just can't seem to get it so old bullets can display once new bullets are being shot. Actually I've been stuck with this problem all day long, and I can't find a way to get it to work. Right now I think I'm close but whatever I'm doing isn't working exactly how I want it to.

    Yeah it is pretty cool that each of us are making the same game. I mainly got the idea from you Vicious. And I don't think I'm better than you.

  11. #71
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Ok, I've probably spent around 10 hours today trying to get multiple animations to go on at once. I was finally able to do that but it was flicking too much. I could probably go back (ctrl+z) and get to that point, and fix what I know was wrong with that. But I'd rather do it the way I'm doing it now...more time efficient in the drawing routine. But the problem is that it's not working. It compiles in all but it doesn't work the way it should (by should I mean the way I intended for it to work ).

    Here's the problem: Even when I press space (shoot) twice the drawing routine only shows one shooting animation, and that is the old one. I can't display a new shooting animation until the old animation ends. Unless I start pressing the arrow keys and press shoot and then when I do this it's not what I want it do. It destroys the old animation and creates a new animation, but all that is beside the point right now.

    here's the code...I don't see what's wrong with it???

    Code:
    void user_spacebar()
     {
            a[0] = y;   //setting 1st bullet's y to y
            b[0] = x;     //setting 1st bullet's x to x
            num[0] = a[0];   //setting 1st bullets num to a
             do
              { 
            a[0]-=3;    // moving the bullets upwards for the 1st
            num[0] = a[0];
            draw_sprite ( buffer, plane, x, y );   //drawing the plane because the buffer has been cleared
            if(num[0] > 119)                         
             {     
            draw_sprite ( buffer, bullets, b[0], a[0] );   //drawing the 1st bullet to the buffer
             }
            if(var[1] == 1 && num[1] > 119)  //if the 2nd bullet has been initialized to be drawn
             {                               //initialization can't take place until 1st bullet has been drawn at least once
               a[1]-=3;    //move the bullets upwards for the 2nd bullet
               num[1] = a[1];   //setting 2nd bullets num to a  
               draw_sprite ( buffer, bullets, b[1], a[1] );  //drawing the 2nd bullet to the buffer
             } 
            if(var[1] == 1 && num[1] < 119)   //if 2nd bullet drawing has ended
             {
             num[1] = 0;  //restoring values
             var[1] = 0;
             }
            blit( buffer, screen, 0, 0, 0, 0, 640, 480 );   //copying buffer to the screen
            show_video_bitmap(screen);   //displaying the screen
            clear_bitmap ( buffer );   //clearing the buffer *but not the screen*
                   
         if( keypressed() && (key[KEY_SPACE]) && (var[1] != 1))   //if keypressed once while 2nd bullet isn't initialized
             {
               var[1] = 1;     //setting values
               a[1] = y;
               b[1] = x;    
             }     
          if( keypressed() && !key[KEY_SPACE] )       //if any other key besides space has been pressed
             {        
               key_detection();   //going to a key detection function
             } 
              }while((num[0] > 119 || num[1] > 119) && !key[KEY_ESC]);   //while 1st or 2nd bullet is initialized or while esc hasn't been pressed
              var[1] = 0;   //i don't remember lol
    }
    Btw, I commented the hell out of it to make it as easy as possible for any of you to read.

    Thank you.
    Last edited by TechWins; 06-09-2002 at 04:01 AM.

  12. #72
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I did my bullets like this:

    Code:
    #define MAX_BULLETS 10
    
    struct bullet {
      int x, y;
      int exists;
    } bullets[MAX_BULLETS];
    Then every time right before I draw to the screen (well, the backbuffer) I call the function updatebullets() which goes through each bullet and subtracts from y, to make it move up the screen. I also have that function change exists to 0 if the bullet goes off the screen, later I will have the code for when a bullet hits something. I loop through it like this

    Code:
    for(int i = 0; i < MAX_BULLETS; i++)
    {
      if(bullets[i].exists == 1)
      {
        bullets[i].y -= 5;
      }
    }
    That code loops through the whole bullets array and for everyone that exists it subtract five from y, therfore all the bullets move up the screen.

    When I create a bullet I have to loop through the bullets array and find the first empty space. If I had an integer for how many bullets are on the screen, and add to it everytime I add a bullet, and delete from it everytime a bullet disapears, I would overwrite them. Here is an example of how that would not work (using the bullet struct from above)

    Code:
      int NumberOfBullets = 0;
    
      // Okay lets say the user presses space
      // We create a bullet
    
      bullets[NumberOfBullets].exists = 1;
      bullets[NumberOfBullets].x = 300;
      bullets[NumberOfBullets].y = 400;
    
        int NumberOfBullets++;
    
      // Now lets create another bullet
      // Note the first bullets location would be bullets[0] 
    
      bullets[NumberOfBullets].exists = 1;
      bullets[NumberOfBullets].x = 200;
      bullets[NumberOfBullets].y = 450;
    
      NumberOfBullets++;
    
      // Now lets say some time has passed in our game, the first
      // bullet is off screen now, but the second is still on screen.
      // Now our code would break
    
      // First since our first bullet went off screen we do this
      NumberOfBullets--;
    
      // Now what happends if we create a new bullet?
    
      bullets[NumberOfBullets].exists = 1;
      bullets[NumberOfBullets].x = 245;
      bullets[NumberOfBullets].y = 420;
    
      // Uh oh, we just over wrote our second bullet, they are both
      // bullet[1].
    I tried that method first, it would not work, obviously. So now everytime I create a bullet, I have it loop through the bullets array and find the first place where exists = 0; The code for this is not hard, I will show you.

    Code:
    for(int i = 0; i < MAX_BULLETS; i++)
    {
      if(bullets[i].exists == 0)
      {
        // We found a bullet in the array that does not exist yet
        // meaning we can use that place to hold a bullet
        // We want to give it coordinates, then set exists to 1 so
        // it gets drawn, and nothing overwrites it
        bullets[i].x = 200;
        bullets[i].y = 200;
        bullets[i].exists = 1;
    
        // A problem I had earlier was that I did not put a break
        // statement here, that made it create a bullet with the
        // same coordinates every place there was a empty bullet
        // which meant that I could only have one bullet at a time
        break;
      }
    }
    Well, I hope I explained everything, here is the code to my game so far, there are no enemies yet, but you can have up to 30 bullets on screen at once, there is no flicker in bullet movement or plane movement. I am still looking for a new plane graphic. I think I am gonna quit with this game, because it is starting to get lame. I need to design what I want the game to be more, before I code. I will work on another game shortly, probably either pong or a space attack game. Here is the full code to the airplane game:

    Code:
    #include <allegro.h>
    
    #define MAX_BULLETS 10
    
                        // Draws ship and bullets to buffer, then
      void redraw();    // blits buffer to screen
    
                        // Initializes Allegro, sets screen mode
      void setup();     // and loads bitmaps
    
      void shutdown();  // Destroys bitmaps and calls allegro_exit()
    
      void updatebullets(); // Updates bullet coordinates
    
      BITMAP *background = NULL;    // Clouds background
      BITMAP *bullet     = NULL;    // A bullet...
      BITMAP *plane      = NULL;    // Da plane! Da plan!
      BITMAP *buffer     = NULL;    // The buffer, reduces flicker
    
      // I found the easiest way to implement bullets is to create
      // an array of structures, so it is easy to keep track of
      // each bullets coordinates and state (exists or not)
      struct bullet
      {
        int x, y;
        int exists;
      } bullets[MAX_BULLETS];
    
      // The ships coordinates, maybe I should make a ship structure...
      int x = 300, y = 375;
    
    int main()
    {
      setup();
    
      while(!key[KEY_ESC])
      {
        if(key[KEY_UP] && y > 5)        // Move up
          y = y - 5;
    
        else if(key[KEY_DOWN] && y < 400)    // Move down
          y = y + 5;
    
        else if(key[KEY_LEFT] && x > 5)      // Move left
          x = x - 5;
    
        else if(key[KEY_RIGHT] && x < 525)   // Move right
          x = x + 5;
    
        else if(key[KEY_SPACE]) // Shoot, if there are already 10 bullets, do nothing
        {
          // Find an empty place in the bullets struct
          for(int i = 0; i < MAX_BULLETS; i++)
          {
            if(bullets[i].exists == 0) // We found an empty slot
            {
              bullets[i].exists = 1; // Make it exist and set its coords
              bullets[i].x = x;
              bullets[i].y = y;
              break;
            }
          }
        }
        updatebullets();
        redraw();
      }
    
      shutdown();
      return 0;
    }
    END_OF_MAIN();
    
    void redraw()
    {
      blit(background, buffer, 0, 0, 0, 0, 640, 480);
      draw_sprite(buffer, plane, x, y);
    
      for(int i = 0; i < MAX_BULLETS; i++)
      {
        if(bullets[i].exists == 1)
        {
          draw_sprite(buffer, bullet, bullets[i].x, bullets[i].y);
        }
      }
    
      blit(buffer, screen, 0, 0, 0, 0, 640, 480);
    }
    
    void setup()
    {
      allegro_init();
      install_keyboard();
    
      // Set colors depth and set graphics mode
      set_color_depth(32);
      set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);
    
      // Load bitmaps
      background = load_bitmap("background.bmp", NULL);
      bullet     = load_bitmap("bullet.bmp", NULL);
      plane      = load_bitmap("plane.bmp", NULL);
      buffer     = create_bitmap(640, 480);
    
      // Make sure all bullets do not exist
      for(int i = 0; i < MAX_BULLETS; i++)
      {
        bullets[i].exists = 0;
      }
    }
    
    void shutdown()
    {
      destroy_bitmap(background);
      destroy_bitmap(plane);
    
      allegro_exit();
    }
    
    void updatebullets()
    {
       // Delete bullets that go past screen
      for(int j = 0; j < MAX_BULLETS; j++)
      {
        if(bullets[j].exists == 1)
        {
          if(bullets[j].y <= 0)
            bullets[j].exists = 0;
        }
      }
    
      // Move bullets up the screen
      for(int i = 0; i < MAX_BULLETS; i++)
      {
        if(bullets[i].exists == 1)
        {
          bullets[i].y -= 5;
        }
      }
    }
    I tried to attach a zip with the graphics, but it was too big. I will upload it to my website and post the url here if anyone wants it. I can also include the source and exec.

  13. #73
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Sorry if my previous post did not make sense, it is 5:00 in the morning.

    http://www.ciusa.net/~jrgrant/planeall.zip
    That is the exec, source, and graphics

    http://www.ciusa.net/~jrgrant/planegraphics.zip
    That is just the graphics.

  14. #74
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    TechWins, I was looking through your code, and one of the things that makes it flicker is that you are drawing to the screen, and you do it in different places. The best way to do it, is to have code that parses the user input, it changes different variable, then you have one, and only one function that draws to the screen. Think about it, if you are moving the ship around, and redrawing it with code in the function moveship(), and you have bullets moving and drawing in the function user_space(), then you could not have the ship moving while the bullets are moving. Read that long ass example I just posted. I am learning how to animate sprites right now.

  15. #75
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    josh... i love you

    my ship has bullets now

    Code:
    #include <allegro.h>
    
    #define max_laser 15
      
    int counter;
    BITMAP *buffer = NULL;
    BITMAP *laser = NULL;
    
    void updatelasers();
    
    struct laser
    {
       int x, y;
       int exists;
    }lasers[max_laser];
      
    class player  
    {  
       public:  
           int x, y, left, right, fire;
           BITMAP* ship;  
      
           player(): x(300), y(425), left(0), right(0) { ship = load_bitmap ( "ship.bmp", NULL ); }  
          ~player() { destroy_bitmap(ship); }  
      
           void  getmove()  
           {  
               if ( x > 0 && key[KEY_LEFT] )  
               {  
                  left = 1;  
               }  
      
               else  
               {  
                  left = 0;  
               }  
      
               if ( x < 600 && key[KEY_RIGHT] )  
               {  
                  right = 1;  
               }  
      
               else  
               {  
                  right = 0;  
               }
    
               if ( key[KEY_SPACE] )
               {
                  for ( int i = 0; i < max_laser; i++ )
                  {
                      if ( lasers[i].exists == 0 )
                      {
                          lasers[i].exists = 1;
                          lasers[i].x = x;
                          lasers[i].y = y;
                          break;
                       }
                   }
               }
               updatelasers();
    
               if ( left == 1 ) x -= 10;  
               if ( right == 1 ) x += 10;  
            }  
      
            void draw(BITMAP* b)  
            {  
                draw_sprite ( b, ship, x, 425 );  
            }  
    };
      
    void update_counter()  
    {  
         counter++;  
    }END_OF_FUNCTION(update_counter);  
      
    int main()  
    {  
       allegro_init();  
       install_keyboard();  
       set_color_depth(32);  
       set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0);  
       buffer = create_bitmap( 640, 480 );
       laser = load_bitmap ( "laser.bmp", NULL );
      
       LOCK_VARIABLE(counter);  
       LOCK_FUNCTION(update_counter);  
       install_int_ex( update_counter, BPS_TO_TIMER(60));  
      
       player *player1 = new player;  
      
       do{  
           while ( counter > 0 )  
           {  
               player1->getmove();  
               counter--;  
           }  
      
           clear(buffer);  
           player1->draw(buffer);
           for(int i = 0; i < max_laser; i++)
           {
               if( lasers[i].exists == 1 )
               {
               draw_sprite(buffer, laser, lasers[i].x, lasers[i].y);
               }
           }
           blit( buffer, screen, 0, 0, 0, 0, 640, 480);  
      
       }  
       while ( !key[KEY_ESC] );  
     
       delete player1;
       allegro_exit();  
       return 0;  
    }  
    END_OF_MAIN();
    
    void updatelasers()
    {
    
      for(int j = 0; j < max_laser; j++)
      {
        if( lasers[j].exists == 1 )
        {
          if( lasers[j].y <= 0 )
            lasers[j].exists = 0;
        }
      }
    
      for(int i = 0; i < max_laser; i++)
      {
        if( lasers[i].exists == 1 )
        {
          lasers[i].y -= 10;
        }
      }
    }
    i need to tweak it so it will fire from the center of the ship though...

    and josh... in your game your ship will not move diagonally...

    i thought about making mine move up and down as well but im gonna leave it a simple first game...
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allegro in C for a newb
    By Ideius in forum C Programming
    Replies: 5
    Last Post: 12-29-2005, 04:36 PM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  3. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. Allegro programming in a window
    By Person Man in forum Windows Programming
    Replies: 0
    Last Post: 11-16-2001, 03:23 PM