Thread: Learning Allegro with C++ :D :(

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Learning Allegro with C++ :D :(

    Hi, my first visit to the Games Programming thread, I guess it was inevitable.
    I have attached an assignment that I have been working on in order to learn OOP style programming, and something is screwing me up. I haven't found the source for 24hrs.
    My first question- which has no relation to the problem is, what does clear_bitmap(BITMAP) do?
    From what I read and how I understood it is that clear_bitmap() function
    clears whatever drawing is on a bitmap(). But my world was thrown into confusion when my teacher uploaded his code online.
    He blitted a bitmap(which i call player) onto another bitmap(which i call backbuffer) which was then blitted (well draw_sprite() was used) on to the screen- double buffering which i understand.

    Now the player can draw itself. It has its own bitmap, a triangle is drawn on the bitmap, and this player bitmap is blitted to the backbuffer which was blitted to the screen.
    Before I saw my lecturers code. I didn't use the clear_bitmap(playerbuff) function.
    And because of that it appeared as if 2 individual bitmaps (for 2 players) were on the backbuffer, i checked everywhere to see if I could make the player bitmaps transparent to no avail.

    When I looked at my lecturers code I found i only needed one line of code! guess?
    clear_bitmap()!
    so I did
    Code:
    playerbuff = create_bitmap(100,200) ;
    clear_bitmap(playerbuff);
    draw_sprite(....) ;
    And that worked! it was as if there were 2 actual triangles on the screen and not 2 individual bitmaps if you get what I mean.
    but that confused me.
    how is it possible that after creating a bitmap, then clearing that bitmap
    I was still able to draw on the cleared bitmap and draw it on the backbuffer?

    2.
    I will post the code, that has made want to do a Padme Amidala
    Code:
    #include <allegro.h>								  
    #include "Player.h"
    #include "Vector2.h"  
    #include "Obstacle.h"
    
    #include "GameObjects.h"
     
    
    		GameObjects bullets[8] ;
    		
    void respondToKeyBoard(Player &,Player &) ;
    void updateBullets(Player &, BITMAP *) ;
    int oldspace  ;
    int spacedown ;
    			int count ;
    			
    void fireBullets(BITMAP *);
    volatile int ticks ;
    void timer()
    {
    	ticks++ ;
    }
    
    int main() 
    {
    		int oldticks;
    		count = 0 ;
    	spacedown = 0 ;
    	int x=0, y =0; 
    		
    		
    	if(!allegro_init())
    	{	 
    	
    		install_keyboard() ; 
    		install_mouse() ; 
    		install_timer();
    		
    		
    
    	
    		 Player playerOne(40.0f,50.0f) ; 
    		
    		 Player playerTwo(140.0f,50.0f) ; 
    
    		Obstacle SightBlinder(400.0f,100.0f);
    
    		Obstacle DarthVader(40, 350.0f);
    		
    	
    		  
    		BITMAP *backbuffer = create_bitmap(640,480) ;
    			 
    		
    			install_int_ex(timer,BPS_TO_TIMER(60)) ;
    			 srand(time(0)); 
    		if(!set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640,480,0,0))
    		{  
    			
    			
    			while(!key[KEY_ESC])
    			{
    					clear_to_color(backbuffer,makecol(0,0,0)) ;
    				   	
    				   
    				
    					
    				while(ticks==0)
    				{
    					rest(1);
    				}
    				while(ticks>0)
    				{
    					 oldticks = ticks ;
    
    					 
    					 		
    				
    					ticks-- ; 
    
    				
    					if(oldticks<=ticks)
    					{
    						break ;
    					}  
    				}
    				respondToKeyBoard(playerOne, playerTwo)  ; 
    				
    				
    				  				fireBullets(backbuffer);
    								////textprintf(screen,font,320,0,makecol(255,0,0),"COunt: %d", count) ;
    				
    				playerOne.draw(backbuffer) ;
    				playerTwo.draw(backbuffer) ; 
    				
    				
    					SightBlinder.draw(backbuffer,40,30,100,110) ;
    				
    				 DarthVader.draw(backbuffer,40,30,100,110) ;
    			
    				
    				blit(backbuffer,screen,0,0,0,0,SCREEN_W,SCREEN_H) ; 
    				
    			}
    		}
    	}
    
    
    	return 0 ;
    }
    END_OF_MAIN() 
    
    
    void respondToKeyBoard(Player &playerOne, Player &playerTwo) 
    {
    
    	if(key[KEY_SPACE])
    				{
    					oldspace = 0 ; 
    					spacedown = 1 ;
    					if(!oldspace)
    					{
    				
    						for(int index =0;index<=7;index++)
    						{
    								
    								if(!bullets[index].isObjectAlive())
    								{
    									count++ ;
    									bullets[index].setMode(true) ;
    									bullets[index].pos.x = playerTwo.getPosX() ; 
    									bullets[index].pos.y = playerTwo.getPosY() ;
    									
    									
    									break ; 
    								}	
    								else
    								{
    									count =0 ;
    								}
    
    										
    										
    										
    										
    									
    								
    									
    						}
    					}		
    				}
    				else
    				{
    					spacedown = 0 ;
    					
    				}
    				
    
    				
    			
    				
    			    
    				if(key[KEY_A])
    				{
    					playerOne.strafe(-0.3f); 
    				}
    			
    				if(key[KEY_D])
    				{
    					playerOne.strafe(0.3f); 
    				}
    
    				if(key[KEY_W])
    				{
    					playerOne.walk(-0.3f); 
    				}
    			
    				if(key[KEY_S])
    				{
    					playerOne.walk(0.3f); 
    				}
    
    
    
    				
    				if(key[KEY_LEFT])
    				{
    					playerTwo.strafe(-0.3f); 
    				}
    			
    				if(key[KEY_RIGHT])
    				{
    					playerTwo.strafe(0.3f); 
    				}
    
    				if(key[KEY_UP])
    				{
    					playerTwo.walk(-0.3f); 
    				}
    			
    				if(key[KEY_DOWN])
    				{
    					playerTwo.walk(0.3f); 
    				}
    			
    				
    
    }
    
    void fireBullets(BITMAP *backbuffer)
    {
    	for(int i=0;i<8;i++)
    	{
    				 
    			
    							
    		if(bullets[i].isObjectAlive())
    		{
    					//	bullets[i].draw(backbuffer);
    					
    				
    						
    
    					circlefill(backbuffer,bullets[i].pos.x,bullets[i].pos.y,3,makecol(255,0,0)) ;
    
    						bullets[i].pos.y -= 1.0f;
    							  
    						 
    		}
    			if(bullets[i].getPosY()<0)
    			{
    				bullets[i].setMode(false) ; 
    			}
    				
    				
    					
    					 
    			
    						
    	}
    				
    }
    What I want to do is, if the user presses space draw a bullet on the screen.
    Another space input draw another bullet.
    But the thing is it appears to draw all 8 bullets at once. I tried using random velocities in an effort to separate them, no luck. I used random velocity plus timer
    and that doesn't work. Not only does it barely separates the bullets, it slows the 2 players down! which isn't fun .
    I have no idea what to do at this stage: I believe there is a bug in my code, so I hope one of you guys has experience with allegro and can help me with this please.

    I also want to inquire about the install_int (_ex)() function
    does it just call a function like every time seconds specified?
    like:
    Code:
    void timer()
    {
          ticks++ ;
    }
    install_int(timer, 60) ;
    what does that do? Does it call the function timer() every 60 seconds, thus incrementing ticks?

    wow, i'm a great story teller huh?
    Well thanks for any future suggestions, hints and answers.

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Attachments of the project.
    Last edited by Eman; 11-03-2010 at 07:00 AM.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Remaining files attached.
    No need to create sprites! I am using vector graphics.
    Thanks for your help!
    Last edited by Eman; 11-03-2010 at 07:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 10:51 AM
  2. Allegro, OpenGL.. or even DirectX?
    By Zeusbwr in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2004, 08:16 AM
  3. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  4. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  5. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM