Thread: need help fixing up this code in allegro

  1. #1
    Bobish
    Guest

    need help fixing up this code in allegro

    I'm working on a game and as you can see my codes a mess, ive been trying to break it up into smaller functions but I cant find any good places to break it up. I have one function writen but it doesn't work. As you can see my program also has some problems and any advice on how to fix it would be appriciated. I'm using Msvc++ 6 and allegro 4.00.


    Code:
    //#define DEBUGMODE //include this if your debugging
    
    #include <stdio.h>
    #include <allegro.h>
    #include "test.h"
    
    #define left		 1
    #define right		 2
    #define up			 3
    #define down		 4
    #define UpandLeft	 5
    #define DownandLeft  6
    #define UpandRight   7		
    #define DownandRight 8
    #define space		 9
    #define ericw		 43  // width of eric.bmp
    #define erich		 61  // hight of eric.bmp	 
    
    struct bullet{ 
    int bulletx; 
    int bullety; 
    int direction; 
    }bullets[256]; // this stores information on all the bullets in the air
    
    void calculate_shot_movment(int direction, int shot_draw_number);
    
    PALETTE  Pal;
    BITMAP *ImageBuffer;  
    BITMAP *Ammobuffer;  // stores the the immage of the amunition
    
    int main(void)
    {
        BITMAP *dblbuffer;
    	
    	int x=100;
    	int y=100;
    	int keyvar=0;  //a keyboard buffer
    	signed short shot_number=-1;   //which shot are we dealing with now
    	int lastkeypressed=0; 
    	signed short shot_draw_number=0;//which shot to draw
    	int waskeydown=FALSE;  //used to tell if a keypressed was aready there
    
    	allegro_init();
        
    	install_keyboard();
        install_timer();        /* needed for `rest' function */
    
    	set_color_depth(16); 
    		if (set_gfx_mode(GFX_SAFE, 800, 600, 0, 0) < 0) 
    		{
    		printf("%s\n", allegro_error);
    		exit(1);
    		}
    
        clear(screen);
    	
        // Pay attention!!! We HAVE to allocate memory for our memory bitmap
        dblbuffer = create_bitmap(SCREEN_W, SCREEN_H);
    	clear(dblbuffer);
    	
    	ImageBuffer = load_bmp("Eric.bmp", Pal);
    	Ammobuffer = load_bmp("rock.bmp", Pal);
    
    
    		while(1)// main loop
    		{
    			/////////////////// input
    			if (key[KEY_LEFT])
    			{
    			x--;
    			lastkeypressed=left;
    			}
    
    			if (key[KEY_RIGHT])
    			{
    			x++;
    			lastkeypressed=right;
    			}
    			
    			if (key[KEY_UP])
    			{
    			y--;
    			lastkeypressed=up;
    			}
    
    			if (key[KEY_DOWN])
    			{
    			y++;
    			lastkeypressed=down;
    			}
    			
    			if (key[KEY_LEFT] && key[KEY_UP])
    			{
    			lastkeypressed=UpandLeft;
    			}
    			
    			if (key[KEY_LEFT] && key[KEY_DOWN])
    			{
    			lastkeypressed=DownandLeft;
    			}
    
    			if (key[KEY_RIGHT] && key[KEY_UP])
    			{
    			lastkeypressed=UpandRight;
    			}
    
    			if (key[KEY_RIGHT] && key[KEY_DOWN])
    			{
    			lastkeypressed=DownandRight;
    			}
    			/*
    			if(keypressed())
    				keyvar=readkey();
    			else keyvar=0;
    			*/	
    
    			if (key[KEY_SPACE]  && waskeydown==FALSE)
    			{	
    				waskeydown=FALSE;// to prevent mutiple shots from one press
    
    				shot_number++;
    				if (shot_number>255)
    				{shot_number = shot_number-255;}   // makes it so shot number has a max value of 255
    
    				bullets[shot_number].direction=lastkeypressed;
    				
    				if(lastkeypressed==up || lastkeypressed==UpandLeft  || // if there was an up direction
    					lastkeypressed==UpandRight)
    				{
    				bullets[shot_number].bullety=y-5;  // the y position of the bullet is five pixles above the player
    				}
    				
    				else if(lastkeypressed==down || lastkeypressed==DownandLeft   // if there was a down direction
    					||lastkeypressed==DownandRight)
    				{
    				bullets[shot_number].bullety=y+erich+5; // the y position of the bullet is five pixles below the player
    				}
    
    				else
    				{
    				bullets[shot_number].bullety=y+(erich/2); // the y position of the bullet is level with the player}
    				}
    				
    				
    				if(lastkeypressed==left || lastkeypressed==UpandLeft // if there was a left direction
    					|| lastkeypressed==DownandLeft)
    				{
    				bullets[shot_number].bulletx=x-5;  //// the x position of the bullet is five pixles to the left of
    				}
    				
    				else if(lastkeypressed==right || bullets[shot_number].direction==UpandRight // if there was a left direction
    					|| lastkeypressed==DownandRight)
    				{
    				bullets[shot_number].bulletx=x+ericw+5;  //// the x position of the bullet is five pixles to the right of the player
    				}
    				
    				else
    				{
    				bullets[shot_number].bulletx=x+(ericw/2);
    				}
    				
    			}
    
    			if (keypressed()!=TRUE)
    			{
    			waskeydown=FALSE;
    			}
    
    
    			//////////////////output		
    		 
    
    		if (lastkeypressed==right)
    		{
    			rectfill(dblbuffer, x - 1 , y      ,x + ericw - 1, y + erich, makecol(000,000,000));	/* erase from last place */
    		}
    		
    		if (lastkeypressed==left)
    		{
    			rectfill(dblbuffer, x + 1 , y	   ,x + ericw + 1, y + erich, makecol(000,000,000));	/* erase from last place */
    		}
    
    		if (lastkeypressed==up)
    		{
    			rectfill(dblbuffer, x		, y + 1,x + ericw , y + erich + 1, makecol(000,000,000));	/* erase from last place */
    		}
    	
    		if (lastkeypressed==down)
    		{
    			rectfill(dblbuffer, x		, y - 1,x + ericw ,y + erich - 1, makecol(000,000,000));	/* erase from last place */
    		}
    		
    		if (lastkeypressed==UpandLeft)
    		{
    			rectfill(dblbuffer, x + 1 ,y + 1,x + ericw + 1, y + erich + 1, makecol(000,000,000));	/* erase from last place */
    		}
    		
    		if (lastkeypressed==DownandLeft)
    		{
    			rectfill(dblbuffer, x + 1 ,y - 1,x + ericw + 1, y + erich - 1, makecol(000,000,000));	/* erase from last place */
    		}
    
    		if (lastkeypressed==UpandRight)
    		{
    			rectfill(dblbuffer, x - 1 ,y + 1,x + ericw - 1, y + erich + 1, makecol(000,000,000));	/* erase from last place */
    		}
    		
    		if (lastkeypressed==DownandRight)
    		{
    			rectfill(dblbuffer, x - 1 ,y - 1,x + ericw - 1, y + erich - 1, makecol(000,000,000));	/* erase from last place */
    		}
    		
    	//	if (lastkeypressed=space)
    	//	{
    	//		draw_sprite(dblbuffer, Ammobuffer, shotx, shoty);
    	//		}
    		
    		draw_sprite(dblbuffer, ImageBuffer, x, y);  //draw bitmap on dblbuffer
    		
    		while (shot_draw_number < shot_number)
    		{
    		
    			if (bullets[shot_draw_number].direction==DownandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			if (bullets[shot_draw_number].direction==UpandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety--;
    			}
    
    			if (bullets[shot_draw_number].direction==DownandLeft)
    			{
    			bullets[shot_draw_number].bulletx--;
    			bullets[shot_draw_number].bullety++;
    			}
    
    			if (bullets[shot_draw_number].direction==UpandRight)
    			{
    			bullets[shot_draw_number].bulletx++;
    			bullets[shot_draw_number].bullety--;
    			}
    			
    			if (bullets[shot_draw_number].direction==down)
    			{
    			bullets[shot_draw_number].bullety++;
    			}
    			
    			if (bullets[shot_draw_number].direction==up)
    			{
    			bullets[shot_draw_number].bullety--;
    			}
    
    			if (bullets[shot_draw_number].direction==left)
    			{
    			bullets[shot_draw_number].bulletx--;
    			}
    
    			if (bullets[shot_draw_number].direction==right)
    			{
    			bullets[shot_draw_number].bulletx++;
    			}
    		//calculate_shot_movment(bullets[shot_draw_number].direction, shot_draw_number);
    
    		draw_sprite(dblbuffer, Ammobuffer, bullets[shot_number].bulletx, bullets[shot_draw_number].bullety);  //draw the bullet
    		shot_draw_number++;
    		}
    		shot_draw_number=0;
    
    		vsync();
    		blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // copy from double buffer to screen
    		
    
    			if (key[KEY_ESC])//clean up and quit
    			{
    			// Now, remember to FREE the memory you previously allocated
    			destroy_bitmap(dblbuffer);
    			destroy_bitmap(ImageBuffer);
    			destroy_bitmap(Ammobuffer);
    			allegro_exit();
    			exit(1);
    			}
    		}
        return 0;
    }
    END_OF_MAIN();
    
    
    // a function i'm working on to calculate a shot movement
    void calculate_shot_movment(int direction, int shot_draw_number)
    {
    int* rbulletx = &bullets[shot_draw_number].bulletx;  // note to self change to references
    int* rbullety = &bullets[shot_draw_number].bullety;
    
    			if (direction==DownandRight)
    			{
    			*rbulletx++;
    			*rbullety++;
    			}
    
    			if (direction==UpandLeft)
    			{
    			*rbulletx--;
    			*rbullety--;
    			}
    
    			if (direction==DownandLeft)
    			{
    			*rbulletx--;
    			*rbullety++;
    			}
    
    			if (direction==UpandRight)
    			{
    			*rbulletx++;
    			*rbullety--;
    			}
    			
    			if (direction==down)
    			{
    			*rbullety++;
    			}
    			
    			if (direction==up)
    			{
    			*rbullety--;
    			}
    
    			if (direction==left)
    			{
    			*rbulletx--;
    			}
    
    			if (direction==right)
    			{
    			*rbulletx++;
    			}
    
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Thats a lot of code... You could try to break it up to get something like this:
    Code:
    bool Setup()
    {
      if(AllegroInit()==false) return false;
      if(InstallKeyboard()==false) return false;
      if(InstallTimer()==false) return false;
      if(InstallWhatever()==false) return false;
      return true;
    }
    
    int main()
    {
      if(Setup()==true)
      {
        while(GameIsOn==true)
        {
          KeyboardCheck();
          CalculateStuff();
          RenderScreen();
        }
        ShutDown();
      }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    bobish
    Guest
    okay i am running this function: in the place of those three install/init functions but when i do keyboard imput down't work, including set_color_depth didn't work ether. Am i going to have to use pointers to those functions?

    int gameinit()
    {
    if(allegro_init()==FALSE) return FALSE;
    if(install_keyboard()==FALSE) return FALSE;
    if(install_timer()==FALSE) return FALSE;
    return TRUE;
    }

  4. #4
    Registered User compjinx's Avatar
    Join Date
    Aug 2001
    Posts
    214
    I just write all my general stuff in classes and put them into external cpp files.

    did you include allegro.h into all the cpp/h files you have in your project?
    "The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
    Eric Porterfield.

  5. #5
    Bobish
    Guest
    you can just look at my code to see that I included allegro.h and I personaly try to avoid using classes.

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Bobish
    you can just look at my code to see that I included allegro.h and I personaly try to avoid using classes.
    Your code snippet shows code from only one file. The question is did you include "allegro.h" in all your cpp/h files.

    Why would you avoid classes? I know some people go over-board with "wrapper-itus" but if you use them properly classes make manipulating your objects SO much easier.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    man you've got to help me with a spaceshooter game

    i've done some code but one of my problems are how to make
    a shot that moves actually i have mannaged to do that also but
    i cant "shoot" many shots i a row

    please help.

  8. #8
    Bobish
    Guest
    well you could do what I did of having an array of structs called bullets, the size of the array determines the max number of bullets.

  9. #9
    Bobish
    Guest
    so any more advice on how to clean up my code?

  10. #10
    Registered User compjinx's Avatar
    Join Date
    Aug 2001
    Posts
    214
    Can't you change to a system to 360 directions instead of just 8?
    then you can do some hard calculation instead of a whole bunch of IF statments.
    like:

    fx=fx-cos(angle)/20;
    fy=fy-sin(angle)/20;
    x+=fx;
    y+=fy;

    where FX and FY are the forces acting on X and Y, ANGLE is the angle the bullet is pointing,ect
    At least I think thats what this code does, I didn't get much sleep last night so I am a little tired.
    "The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
    Eric Porterfield.

  11. #11
    Registered User compjinx's Avatar
    Join Date
    Aug 2001
    Posts
    214
    WHAT!!!! why am I labeled as a Senior Member? I havn't posted THAT much! im not even a good programmer!
    "The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
    Eric Porterfield.

  12. #12
    bobish
    Guest
    I think ill stick to if statements for now untill i clean this code up a bit, for one thing those math.h functions can be bit slow though it would be nice to change this at some point so then i would have an engine ready to be any type of game.
    Okay i have an init function working but i still can't get any functions using references or pointers to global variables and ones from main. How can i make function change variables outside of the function?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM