Problem with Allegro sprites

This is a discussion on Problem with Allegro sprites within the C++ Programming forums, part of the General Programming Boards category; I have this problew that when I move my sprite (using arrowkeys) it leaves behind a nasty trail and I ...

  1. #1
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29

    Exclamation Problem with Allegro sprites

    I have this problew that when I move my sprite (using arrowkeys) it leaves behind a nasty trail and I couldn't find a solution to this...I think the problem is in this piece of code (if you need more just ask).

    Code:
    void moveChar()
    {
         while( !key[KEY_ESC])
         {
                if(key [KEY_LEFT]) --x;
                if(key [KEY_RIGHT]) ++x;
                if(key [KEY_DOWN]) ++y;
                if(key [KEY_UP]) --y;
        
                draw_sprite(buffer, character, x, y);
                blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
                
        }
        destroy_bitmap(buffer);
        destroy_bitmap(character);
        release_screen();
        rest(100);
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well you need some way to save what was "under" the sprite before you drew the sprite, so that you can restore it (to delete the old sprite) before drawing it again in a new position.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It would be simplest to redraw everything each frame. Draw the background (fill with solid colour?) to buffer, then the character, then blit everything to screen.

    Salem is probably describing what is known as dirty rectangles (redrawing only those parts of the screen where something has changed).
    Last edited by anon; 01-18-2011 at 01:29 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    The background is made of two multidimensional arrays. One contains the grass texture and the water texture. The other contains objects which are a tree and a rock. Is there a way to save everything except the sprite?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Like I said, if your background scene is complicated, then save + write + restore.
    It's only a couple of lines of code, and a separate buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    Could you write me an example code please? I can't quite get what you mean

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by godly 20 View Post
    Could you write me an example code please? I can't quite get what you mean
    1) Save the rectangle under your sprite...
    2) draw it...
    3) restore the rectangle
    4) move the sprite
    5) goto step 1

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    What you should do..is use double buffering.

    So create a buffer
    Code:
          BITMAP *backbuffer ;
          BITMAP *img1=load_bitmap(..), img2 = load_bitmap(...) 
         
          while(...)
          {
    
              draw_sprite(backbuffer) 
    
                .....
    
                         draw_sprite(img1)
                .....
    
              draw_sprite(img2)
                
           clear_bitmap(backbuffer) ;
          }
    the "nasty trail" should be gone... I dunno..try it. I suck at allegro

    EDIT:
    didn't notice your code
    Code:
    void moveChar()
    {
         while( !key[KEY_ESC])
         {
                if(key [KEY_LEFT]) --x;
                if(key [KEY_RIGHT]) ++x;
                if(key [KEY_DOWN]) ++y;
                if(key [KEY_UP]) --y;
        
                draw_sprite(buffer, character, x, y);
                blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
               clear_bitmap(buffer) //if buffer has a background picture it will be cleared to black...so you should follow the code above if you like
                
        }
        destroy_bitmap(buffer);
        destroy_bitmap(character);
        release_screen();
        rest(100);
    
    }
    Last edited by Eman; 01-18-2011 at 02:08 PM.
    You ended that sentence with a preposition...Bastard!

  9. #9
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    Ok, so this is the full code:
    Code:
    #include <allegro.h>
    
    BITMAP* ground;
    BITMAP* water;
    BITMAP* tree;
    BITMAP* rock;
    BITMAP* inventory;
    BITMAP* character;
    BITMAP* buffer;
    
    //These will hold the player's position
    //instead of using pixels, we will use the position within the 2d array
    int x = 15;
    int y = 11;
    
    int tempX = 15;
    int tempY = 11;
    
    int grounds, waters, trees, rocks, inventorys;
    
    //This will be our background, 1 = grass, 3 = water.
    int map[24][32] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
    
    //This will contain all the objects, 100 = player, 101 = tree, 102 = rock.
    int objMap[24][32] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,102,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,103,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
    
    void moveChar()
    {
         while( !key[KEY_ESC])
         {
                if(key [KEY_LEFT]) --x;
                if(key [KEY_RIGHT]) ++x;
                if(key [KEY_DOWN]) ++y;
                if(key [KEY_UP]) --y;
                
                draw_sprite(buffer, character, x, y);
                blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
                
        }
        destroy_bitmap(buffer);
        destroy_bitmap(character);
        release_screen();
        rest(100);
    
    } 
    
    void setupGame()
    {
        buffer = create_bitmap( 640, 480);
        ground = load_bitmap( "ground.bmp", NULL);
        water = load_bitmap( "water(2).bmp", NULL);
        tree = load_bitmap( "sprite-0001.bmp", NULL);
        rock = load_bitmap( "sprite-0002.bmp", NULL);
        character = load_bitmap( "character.bmp", NULL);
        inventory = load_bitmap( "inventory.bmp", NULL);
        grounds = 1;
        waters = 3;
        trees = 101;
        rocks = 102;
        inventorys = 103;
        
        for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( map[i][t] == 1) draw_sprite( buffer, ground, t * 20, i * 20);
                else if( map[i][t] == 3) draw_sprite( buffer, water, t * 20, i * 20);
                
               }       
            
        }  
        
        draw_sprite( screen, buffer, 0, 0);
        
            for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( objMap[i][t] == 101) draw_sprite( buffer, tree, t * 20, i * 20);
                else if( objMap[i][t] == 102) draw_sprite( buffer, rock, t * 20, i * 20);
                else if( objMap[i][t] == 103) draw_sprite( buffer, inventory, t * 20, i * 20);
                
               }       
            
        }
    
        draw_sprite( screen, buffer, 0, 0);
              
    }  
    
    int main()
    {    
        allegro_init();
        install_keyboard();
        set_color_depth(32);
        set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
        
        BITMAP* bmp = create_bitmap(640, 480);
        blit(bmp, screen, 0, 0, 0, 0, 0, 0);
        setupGame();    
        moveChar();
        
        destroy_bitmap(bmp);
        return 0;
    }
    END_OF_MAIN();
    Don't get scared! All it does is lets the "character.bmp" image move around using arrow keys. Where should I insert the above code? I couldn't find a way to make it fit in.

  10. #10
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I should be wise and run away..it looks too advanced.. but I will suggest
    Code:
      for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( map[i][t] == 1) draw_sprite( buffer, ground, t * 20, i * 20);
                else if( map[i][t] == 3) draw_sprite( buffer, water, t * 20, i * 20);
                
               }       
            
        }  
        
        draw_sprite( screen, buffer, 0, 0);
        
            for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( objMap[i][t] == 101) draw_sprite( buffer, tree, t * 20, i * 20);
                else if( objMap[i][t] == 102) draw_sprite( buffer, rock, t * 20, i * 20);
                else if( objMap[i][t] == 103) draw_sprite( buffer, inventory, t * 20, i * 20);
                
               }       
            
        }
    
        draw_sprite( screen, buffer, 0, 0);
    Don't draw it to the screen.

    create a background buffer.

    and in your while loop.. in the moveChar() function

    do
    Code:
         while()
        { 
            blit backbuffer to screen//I think is the first step
            draw_sprite(backbuffer, rock,...) 
            draw_sprite(backbuffer,tree..) 
            ....
    
             clear_bitmap(backbuffer); 
        }
    try that...
    I should have been smart and run away.
    You ended that sentence with a preposition...Bastard!

  11. #11
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    Doesn't work...it blackscreens and crashes

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    can you attach the images..so I can try?

    And what is the error?
    You ended that sentence with a preposition...Bastard!

  13. #13
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    I have included everything you need into a zip file. You can download it here: Allegro_game.zip

    By the way, i use Dev-C++ v.4.9.9.2

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Code:
    										#include <allegro.h>
    
    BITMAP* ground;
    BITMAP* water;
    BITMAP* tree;
    BITMAP* rock;
    BITMAP* inventory;
    BITMAP* character;
    BITMAP* buffer;
    BITMAP *backbuffer; 
    
    //These will hold the player's position
    //instead of using pixels, we will use the position within the 2d array
    int x = 15;
    int y = 11;
    
    int tempX = 15;
    int tempY = 11;
    
    int grounds, waters, trees, rocks, inventorys;
    
    //This will be our background, 1 = grass, 3 = water.
    int map[24][32] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
    
    //This will contain all the objects, 100 = player, 101 = tree, 102 = rock.
    int objMap[24][32] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,101,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,102,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,103,0,0,0},
                       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
    
    void moveChar()
    {
         while( !key[KEY_ESC])
         {
    		  blit(backbuffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
    
    		     
        		draw_sprite(backbuffer, buffer, 0,0) ;
    
    			draw_sprite(backbuffer, character, x,y) ;
    
                if(key [KEY_LEFT]) --x;
                if(key [KEY_RIGHT]) ++x;
                if(key [KEY_DOWN]) ++y;
                if(key [KEY_UP]) --y;
                
               
                	
                
        }
        destroy_bitmap(buffer);
        destroy_bitmap(backbuffer);
        destroy_bitmap(character);
        release_screen();
        rest(100);
    
    } 
    
    void setupGame()
    {
        buffer = create_bitmap( 640, 480);
    	backbuffer = create_bitmap( 640, 480);
    	
        ground = load_bitmap( "ground.bmp", NULL);
        water = load_bitmap( "water(2).bmp", NULL);
        tree = load_bitmap( "sprite-0001.bmp", NULL);
        rock = load_bitmap( "sprite-0002.bmp", NULL);
        character = load_bitmap( "character.bmp", NULL);
        grounds = 1;
        waters = 3;
        trees = 101;
        rocks = 102;
        inventorys = 103;
    
      //  draw_sprite( screen, buffer, 0, 0);
              
    
    	for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( map[i][t] == 1) draw_sprite( buffer, ground, t * 20, i * 20);
                else if( map[i][t] == 3) draw_sprite( buffer, water, t * 20, i * 20);
                
               }       
            
        }  
        
        //draw_sprite( screen, buffer, 0, 0);
        
            for (int i = 0; i <= 24; i++){
         
            for( int t = 0; t <= 32; t++){
                
                if( objMap[i][t] == 101) draw_sprite( buffer, tree, t * 20, i * 20);
                else if( objMap[i][t] == 102) draw_sprite( buffer, rock, t * 20, i * 20);
                
               }       
            
        }
    	
    }  
    
    int main()
    {    
        allegro_init();
        install_keyboard();
        set_color_depth(32);
        set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
        
        BITMAP* bmp = create_bitmap(640, 480);
        blit(bmp, screen, 0, 0, 0, 0, 0, 0);
        setupGame();    
        moveChar();
        
        destroy_bitmap(bmp);
        return 0;
    }
    END_OF_MAIN();
    try that..
    but instead of running that for loop every time..is there not a way to copy the tiles in separate buffers. in the setup function
    and then simply draw it like i did in the main loop?
    Could have tried it now..but I have exams tomorrow.. Can you tell us how it goes?
    Last edited by Eman; 01-18-2011 at 03:16 PM.
    You ended that sentence with a preposition...Bastard!

  15. #15
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I changed the code..
    It was slower because of the copy loop in the while..

    the code i posted up ^^ is what I meant...someone can tell you how to do it better. And it runs fast..

    Anyways I am off to study. Good luck.

    PS
    And good sprites.. I wish I knew how to make one
    You ended that sentence with a preposition...Bastard!

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

Similar Threads

  1. Problem Loading Bitmaps in Allegro
    By LiNeAr in forum Game Programming
    Replies: 1
    Last Post: 08-15-2005, 04:12 PM
  2. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  3. Animating Multiple Sprites
    By Tommaso in forum Game Programming
    Replies: 2
    Last Post: 10-11-2002, 12:06 AM
  4. problem in allegro with sprite leaving trail
    By Leeman_s in forum C++ Programming
    Replies: 18
    Last Post: 09-13-2002, 03:04 PM
  5. Problem declaring struct in allegro
    By bobish in forum Game Programming
    Replies: 7
    Last Post: 03-08-2002, 08:26 AM

Tags for this Thread


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