problem in allegro with sprite leaving trail

This is a discussion on problem in allegro with sprite leaving trail within the C++ Programming forums, part of the General Programming Boards category; Code: void flip_page(BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP* active_page) { if(active_page == page1) active_page = page2; if(active_page == page2) ...

  1. #16
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    Code:
    void flip_page(BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP* active_page)
    {
    	if(active_page == page1)
            active_page = page2;
        if(active_page == page2)
            active_page = page3;
    	if(active_page == page3)
    		active_page = page1;
    }
    END_OF_FUNCTION(flip_page);
    I'm not an allegro programmer but usually with graphics programming setting something with a value, which is what this flip func looks like it is doing, doesn't relate to results on the screen. You must purposely and explicitly set these values to be shown on the screen. Maybe there's a BLIT function in Allegro or DRAW or SET functions or just any function that explicitly sets the new values to screen results. You can't just change the values.

  2. #17
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by OneStiffRod
    Code:
    void flip_page(BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP* active_page)
    {
    	if(active_page == page1)
            active_page = page2;
        if(active_page == page2)
            active_page = page3;
    	if(active_page == page3)
    		active_page = page1;
    }
    END_OF_FUNCTION(flip_page);
    I'm not an allegro programmer but usually with graphics programming setting something with a value, which is what this flip func looks like it is doing, doesn't relate to results on the screen. You must purposely and explicitly set these values to be shown on the screen. Maybe there's a BLIT function in Allegro or DRAW or SET functions or just any function that explicitly sets the new values to screen results. You can't just change the values.
    he does that with
    show_video_bitmap(active_page);

    and if his swap function worked, then it would be right.

    leeman: havent you ever seen this before?
    Code:
    void swapvalues (int a, int b)
    {
      int c = a;
      a = b;
      b = c;
    }
    but of course, it doesnt work, because a and b are passed by value not refrence. so while a and b are swapped inside of swapvalues(), their values in the calling function remain the same. one fix is to use pointers
    Code:
    void swapvalues (int *a, int *b)
    {
      int c = *a;
      *a = *b;
      *b = c;
    }
    This works because while the variables are passed by value, what's being passed is pointers to a and b, not a and b themselves, so when the pointers are derefrenced, one accesses and modifies the same a and b as in the function that called swapvalues().

    now this is of course only one way to solve your problem; there are others as well. see my post a few above this for some code pertaining to your situation.
    hello, internet!

  3. #18
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Now that I really think about it, moi you seem very correct. When I get home from school in about 45 minutes I'll test it out. I'll let you know what happens.

  4. #19
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Ok here is the latest code. There is still flicker with this. Now, what I need to fix is the if statements that decide which page to clear. See, I don't want to clear a page right before I display it or you get flickering. So now there is a trail left behind, which I can eliminate by clearing each page each time through the loop. But I should NOT have to clear each one. Look at when I call clean_page, and perhaps the clean_page function. I thought I would do the same fix to the clean_page function as I did to the flip_page, but I get compile errors. I'll post that up later....I have to cut grass now.
    Code:
    #include <allegro.h> // You must include the Allegro Header file
    
    void increase_speed();
    void init_all();
    void clean_all();
    void clean_bitmap(BITMAP *page);
    void flip_page(int *wPage, BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP** active_page);
    
    volatile long speed_counter = 0;//A long integer which will store the value of the speed counter.
    
    int main(int argc, char *argv[])
    {
    	allegro_init(); // Initialize Allegro
    	install_keyboard(); // Initialize keyboard routines
    	set_color_depth(16); // Set the color depth						
    	set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480
    
    	install_timer();
    	LOCK_VARIABLE(speed_counter);
    	LOCK_FUNCTION(increase_speed);
    	install_int_ex(increase_speed, BPS_TO_TIMER(150));//Set our BPS
    
    	BITMAP *ship, *page1, *page2, *page3, *active_page, *background, *bullet; 
    
    	page1      = create_video_bitmap(SCREEN_W, SCREEN_H);
        page2      = create_video_bitmap(SCREEN_W, SCREEN_H);
        page3      = create_video_bitmap(SCREEN_W, SCREEN_H);
    	background = load_bitmap("lee.bmp", NULL);
        ship       = load_bitmap("ship.bmp", NULL); // Load our picture
    
        clean_bitmap(page1);
        clean_bitmap(page2);
    	clean_bitmap(page3);
    
        active_page    = page1;
    
    	int x          = 0;
    	int y          = 0;
    	int wPage      = 1; //what page are we on?
    	bool validness = true;
    
    /*///////////////////////////////////////////////////////////////////////////////////*/
    /*////// Name: Main Loop ////////////////////////////////////////////////////////////*/
    /*///////////////////////////////////////////////////////////////////////////////////*/
    
    	while(!key[KEY_ESC])
    	{
    		while(speed_counter > 0)
    		{
    		    validness = true;
    
    			if(key[KEY_UP] && key[KEY_RIGHT])
    			{
    				y--;// Moving down, so up the Y coordinate by 1
    				x++;
    			    if(y < 0)
    			    {
    				    y++;
    			    }
    				if(x > 604)
    				{
    					x--;
    				}
    			}
    			else if(key[KEY_UP] && key[KEY_LEFT])
    			{
    				y--;// Moving down, so up the Y coordinate by 1
    				x--;
    			    if(y < 0)
    			    {
    				    y++;
    			    }
    				if(x < 0)
    				{
    					x++;
    				}
    			}
    			else if(key[KEY_DOWN] && key[KEY_LEFT])
    			{
    				y++;// Moving down, so up the Y coordinate by 1
    				x--;
    			    if(y > 440)
    			    {
    				    y--;
    			    }
    				if(x < 0)
    				{
    					x++;
    				}
    			}
    			else if(key[KEY_DOWN] && key[KEY_RIGHT])
    			{
    				y++;// Moving down, so up the Y coordinate by 1
    				x++;
    			    if(y > 440)
    			    {
    				    y--;
    			    }
    				if(x > 604)
    				{
    					x--;
    				}
    			}
    		    else if(key[KEY_RIGHT])
    		    {
    			    x++;// Moving right so up the X coordinate by 1
    			    if(x > 604)
    		    	{
    		    		validness = false;
    			    	x--;
    			    }
    		    }
    		    else if(key[KEY_LEFT])// Ditto' - only for left key
    		    {
    			    x--;// Moving left, so lower the X coordinate by 1
    			    if(x < 0)
    			    {
    				    validness = false;
    				    x++;
    			    }
    		    }
    		    else if(key[KEY_UP])// If the user hits the up key, change the picture's Y coordinate
    		    {
    			    y--;// Moving up, so lower the Y coordinate by 1
    			    if(y < 0)
    			    {
    				    validness = false;
    				    y++;
    			    }
    		    }
    		    else if(key[KEY_DOWN])// Ditto' - only for down
    		    {
    			    y++;// Moving down, so up the Y coordinate by 1
    			    if(y > 440)
    			    {
    				    validness = false;
    				    y--;
    			    }
    		    }
    
    			speed_counter--;
    		};
    	
    		if(validness == true)
    		{
    		    acquire_screen();// Get the screen
    
    		    masked_blit(ship, active_page, 0,0, x, y,640,480);
                show_video_bitmap(active_page);
    
    			flip_page(&wPage, page1, page2, page3, &active_page);
    
    			if(wPage == 2)
    			    clean_bitmap(page3);
    			if(wPage == 3)
                    clean_bitmap(page1);
    			if(wPage == 1)
    			    clean_bitmap(page2);
    
    		    release_screen();// Release it
    		}
    	};
    
    
    	destroy_bitmap(ship);//Release the bitmap data
    	destroy_bitmap(background);
    	destroy_bitmap(page1);
    	destroy_bitmap(page2);
    	destroy_bitmap(page3);
    	destroy_bitmap(bullet);
    
    	return(0);// Exit with no errors
    }
    END_OF_MAIN(); // This must be called right after the closing bracket of your MAIN function.
                   // It is Allegro specific.
    void increase_speed()
    {
    	speed_counter++;
    }
    END_OF_FUNCTION(increase_speed);
    
    void clean_bitmap(BITMAP *page)
    {
    	acquire_bitmap(page);
        clear_bitmap(page);
        release_bitmap(page);
    }
    END_OF_FUNCTION(clear_bitmap);
    
    void flip_page(int *wPage, BITMAP* page1, BITMAP* page2, BITMAP* page3, BITMAP** active_page)
    {
    	if(*active_page == page1)
    	{
            *active_page = page2;
    		*wPage       = 2;
    	}
        if(*active_page == page2)
    	{
            *active_page = page3;
    		*wPage       = 3;
    	}
    	if(*active_page == page3)
    	{
    		*active_page = page1;
    		*wPage       = 1;
    	}
    }
    END_OF_FUNCTION(flip_page);

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem declaring struct in allegro
    By bobish in forum Game Programming
    Replies: 7
    Last Post: 03-08-2002, 08:26 AM
  2. Allegro problem...
    By QuestionC in forum C Programming
    Replies: 1
    Last Post: 12-31-2001, 01:04 AM
  3. Problem with Allegro
    By CeeCee in forum Game Programming
    Replies: 3
    Last Post: 12-24-2001, 06:59 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
  5. Allegro Problem
    By Xterria in forum Game Programming
    Replies: 6
    Last Post: 08-30-2001, 02:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21