Thread: allegro problem : /

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    allegro problem : /

    hey im fairly new to c++ and this site, i wrote this code to move a sprite, i know that its full of holes, constructive critisism would help, constructive being the key word, anyways back on topic

    the problem is the sprite doesnt move.. i set up a struct because im new to them so i decided to utilize them, ya so theirs something inside the see of while and if statements that makes it not work..

    i know i need to buy a more official book but my resources are crappy at best...


    i was also messing around with a refreshText() function to try to refresh the bitmap, yet i ended up taking the bitmap out, but i decided to leave the text in...cuz i wanted to refresh the coordinates, i should probably replace the prototype....but thts not the problem im almost positive..

    Code:
    #include <allegro.h>
    
    void refreshText(); // prototype
    
    
    struct zombie
    {
           int x,y;
           int dir,speed; // optional at the moment.
    }zombieS; // not an array, only one sprite.
    
    
    
    int movement(int x, int y)
    {    
        while(key[KEY_UP]);
        {
            if(y>=480) // checks height
            return 0;                              
            else{                                 
                y++;
                return y;
            }
         }
         while(key[KEY_DOWN])
         {
            if(y<=0)
            return 0; //access denied
            else{
                y--;
                return y;
            }
         }
         while(key[KEY_RIGHT])
         {
             if(x>=640)
             return 0;
             else{
                  x++;
                  return x;
             }
         }
         while(key[KEY_LEFT])
         {
             if(x<=0)
             return 0;
             else{
                  x--;
                  return x;
             }
         }
    }
    //---------------------------------------------
    
    int main()
    {
        BITMAP *zombie;
        
        allegro_init();
        set_gfx_mode(GFX_SAFE, 640, 480, 0, 0);
        set_color_depth(16);
        install_keyboard();
    
        
        zombie = load_bitmap("zombie.bmp", NULL);
        
        zombieS.x = 320; // starter locations
        zombieS.y = 240;
        textprintf_ex(screen, font, 0, 20, 90,-1, "loading succesful!", 0);
        
       
        blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50); // NEED TO LOCK X Y COORDINATES, DOI!
        refreshText(); // starts out the refreshables
        
        if(key[KEY_UP]) //dont know how to define if any key is pressed :/
        movement(zombieS.x, zombieS.y);
        refreshText();
        
        if(key[KEY_DOWN]);
        movement(zombieS.x, zombieS.y);
        refreshText();
        
        if(key[KEY_RIGHT])
        movement(zombieS.x, zombieS.y);
        refreshText();
        
        if(key[KEY_LEFT])
        movement(zombieS.x, zombieS.y);
        refreshText();
        
         
        
    
    
        //----------------------------------------------------    
        while(!key[KEY_ESC]);
        destroy_bitmap(zombie);
        allegro_exit();
        return 0;
    }
    END_OF_MAIN()
    
    void refreshText()
    {    
        textprintf_ex(screen, font, 0, 0, 90,-1, "x coor: %i", zombieS.x);
        textprintf_ex(screen, font, 0, 10, 90,-1, "y coor: %i", zombieS.y);
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > while(key[KEY_UP]);
    Note the ; at the end

    > if(key[KEY_DOWN]);
    Ditto

    Also, your movement function is passed the position by value, so any change inside the function is NOT reflected back in the caller. There are lots of return statements, but nothing pays any attention to the results.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    thanx mon'. everytime i learn something new the firsts time i post on a forum i expect a ripping, but that was pretty helpful .

    i wrote this at midnight and was dead tired, so i expected a bunha syntax errors, good to see it was only my stupidity :0

    so should i set another blit, like this?
    Code:
       // starts off where the keys are called,
    
        blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50); // NEED TO LOCK X Y 
    
        refreshText(); // starts out the refreshables
        //------------------------------------------------
        if(key[KEY_UP]) //dont know how to define if any key is pressed :/
        movement(zombieS.x, zombieS.y);
    
        rectfill(screen, zombieS.x, zombieS.y, 50, 50, 0); //erase the sprite
        blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50); //recall the new location
        
        refreshText();
        //-------------------------------------------------
        if(key[KEY_DOWN])
        movement(zombieS.x, zombieS.y);
    
        rectfill(screen, zombieS.x, zombieS.y, 50, 50, 0); //erase the sprite
        blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50); //recall the new location
        
        refreshText();
        //-----------------------------------------------------
        if(key[KEY_RIGHT])
        movement(zombieS.x, zombieS.y);
    
        rectfill(screen, zombieS.x, zombieS.y, 50, 50, 0); //erase the sprite
        blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50); //recall the new location
        
        refreshText();
        //-------------------------------------------------------
        if(key[KEY_LEFT])
        movement(zombieS.x, zombieS.y);
    
        rectfill(screen, zombieS.x, zombieS.y, 50, 50, 0); //erase the sprite
        blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50); //recall the new location
        
        refreshText();

    i know its impracticle, but is it plausable?

    or i could write a function for the x and y variabels sepratly and do something like,

    Code:
    int y_dec(int y);
    int x_dec(int x)
    {
         if(key[KEY_LEFT])
         {
                if(x<=0)
                return x;
                else
                {
                        x++;
                        return x;
                 }
          }
           if(key[KEY_RIGHT])
           {
                 if(x>=480)
                 return x;
                 else
                 {
                       x++;
                       return x;
                  }
             }
    }
    
    // do the same for all other directions, then
    if(key[KEY_RIGHT])
    x_dec(zombieS.x)
    
    rectfill(screen, zombieS.x, zombieS.y, 50, 50, 0);
    blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50);
    
    refreshtext();
    
    if(key[KEY_LEFT])
    x_dec(zombieS.x)
    
    rectfill(screen, zombieS.x, zombieS.y, 50, 50, 0);
    blit(zombie, screen, 0, 0, zombieS.x, zombieS.y, 50, 50);
    
    refreshtext();
    
    //ect. ect. ect.
    which way is more likely to succeed, or be more successful?
    Last edited by bada; 03-29-2008 at 03:02 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    As in
    Code:
    int movement(struct zombie *z)
    {    
        if(key[KEY_UP]){
            if ( z->y>=480) { // checks height
                z->y = 0;                              
            } else {                                 
                z->y++;
            }
        }
    }
    Which you would call with
    movement( &zombieS );

    Then do the screen refresh.

    How does allegro read the keyboard? Things like key[KEY_UP] look like array references, and are not going to be reading each successive key press (or key state).

    Consider a simpler text program which just contains a loop and responds with "A pressed" and "A released" in response to you pressing say the 'A' key. When you know how that works, then you can expand it into your current project.
    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.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you are going to use this method for keys you will have to poll the keyboard each frame in order to refresh the keyboard array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  3. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 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, 09:26 AM