Jump to content
  • Advertisement
Sign in to follow this  
horizon981

Allegro: Weird Sprite Handler

This topic is 4471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wanted to make a program to load sprites from a sprite strip and a simple sprite engine. The proble is that I get a black screen only. The application doesn't crash, just that you get to see nothing. If anything from the code is not clear to you, please don't hesitate to ask, it's really important. The code:
/*Advanced Sprite Animation with RLE Sprites*/
#include <allegro.h>

struct Sprites
{
 int x, y;
 int xSpeed, ySpeed;
 int xDir, yDir;
 bool isAlive;
 int delay;
 int curFrame, maxFrame;
}monster;


void setup_sprites(Sprites *Sprite)
{
 Sprite->x = rand()%100;
 Sprite->y = rand()%100;
 Sprite->xSpeed = 2; //--Move 2 pixels per keystroke
 Sprite->ySpeed = 0; //--Vertical movement
 Sprite->xDir = 1; //Default = +ve
 Sprite->yDir = 0; //No movement in Y direction
 Sprite->isAlive = true;
 Sprite->delay = 20;
 Sprite->curFrame = 0;
 Sprite->maxFrame = 3;
}
 
void update_sprite(Sprites *Sprite)
{
 if(Sprite->xDir == 1)    
  Sprite->x += Sprite->xSpeed;
 else if(Sprite->xDir == -1)
  Sprite->x -= Sprite->xSpeed;
 else
  Sprite->x += 0; 
 
 if(Sprite->yDir == 1)    
  Sprite->y += Sprite->ySpeed;
 else if(Sprite->yDir == -1)
  Sprite->y -= Sprite->ySpeed;
 else
  Sprite->y += 0;
  
  Sprite->curFrame++;
  
  if(Sprite->curFrame>3)
   Sprite->curFrame = 0;
}

    
int main()
{
 allegro_init();
 set_color_depth(desktop_color_depth());
 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
 
 install_keyboard();
 
 BITMAP *buffer  = create_bitmap(640, 480);
 BITMAP *image = load_bitmap("monster.bmp", NULL);
 
 if(!buffer)
 {
  allegro_message("Double Buffering Failed, outta here...");
  allegro_exit();
  return -1;
 }
 if(!image)
 {
  allegro_message("Sprite loading failed... outta here");
  allegro_exit();
  return -1;          
 }
 
 Sprites *Sprite = &monster;  
 //--Lock Screen
 acquire_screen();
 
 //--Set up initial behaviour of sprites
 setup_sprites(Sprite);
 
 while(!key[KEY_ESC])
 {
  int x = image->w/(Sprite->maxFrame+1)*Sprite->curFrame;
  int y = 0;
                       
  update_sprite(Sprite);
  rest(Sprite->delay);
  blit(image, buffer, x, y, Sprite->x, Sprite->y, 
       (image->w/(Sprite->maxFrame+1)), image->h);
  
  blit(buffer, screen, 0, 0, 0, 0, 640, 480); 
 }
 release_screen();
 
 destroy_bitmap(image);
 destroy_bitmap(buffer);
 allegro_exit();
 return 0;
}
END_OF_MAIN();

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
First off, get rid of those acquire/release screen calls. They are dangerous the way you have it set up. They are only useful when doing more than one consecutive drawing operation to a video bitmap. Never do anything but draw between acquire/release calls.

Share this post


Link to post
Share on other sites
Well.. do what konForce said, and get rid of the acquire and release calls.

Next...

I think there might be a problem with, inside of your while() loop, the value of the "x" integer and/or the first blit() call.

Try changing the first blit call to this and see if anything shows up (if so, we'll know it's a problem with the arguments being passed to blit(), if not, we'll know it's something else). Hard-code the values of WIDTH and HEIGHT to the width and height of the full image (not just of a single frame).

blit(image, buffer, 0, 0, 0, 0, 
WIDTH, HEIGHT);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!