home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter10 / AngularMotion / sprite.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-14  |  2.6 KB  |  121 lines

  1. #include <allegro.h>
  2. #include "sprite.h"
  3.  
  4.  
  5. sprite::sprite() {
  6.     image = NULL;
  7.     alive = 1;
  8.     direction = 0;
  9.     animcolumns = 0;
  10.     animstartx = 0; 
  11.     animstarty = 0;
  12.     x = 0.0f; 
  13.     y = 0.0f;
  14.     width = 0; 
  15.     height = 0;
  16.     xdelay = 0; 
  17.     ydelay = 0;
  18.     xcount = 0; 
  19.     ycount = 0;
  20.     velx = 0.0; 
  21.     vely = 0.0;
  22.     speed = 0.0;
  23.     curframe = 0; 
  24.     totalframes = 1;
  25.     framecount = 0; 
  26.     framedelay = 10;
  27.     animdir = 1;
  28.     faceAngle = 0; 
  29.     moveAngle = 0;
  30. }
  31.  
  32. sprite::~sprite() {
  33.     //remove bitmap from memory
  34.     if (image != NULL)
  35.         destroy_bitmap(image);
  36. }
  37.  
  38. int sprite::load(char *filename) {
  39.     image = load_bitmap(filename, NULL);
  40.     if (image == NULL) return 0;
  41.     width = image->w;
  42.     height = image->h;
  43.     return 1;
  44. }
  45.  
  46. void sprite::draw(BITMAP *dest) 
  47. {
  48.     draw_sprite(dest, image, (int)x, (int)y);
  49. }
  50.  
  51. void sprite::drawframe(BITMAP *dest)
  52. {
  53.     int fx = animstartx + (curframe % animcolumns) * width;
  54.     int fy = animstarty + (curframe / animcolumns) * height;
  55.     masked_blit(image,dest,fx,fy,(int)x,(int)y,width,height);
  56. }
  57.  
  58. void sprite::updatePosition()
  59. {
  60.     //update x position
  61.     if (++xcount > xdelay)
  62.     {
  63.         xcount = 0;
  64.         x += velx;
  65.     }
  66.  
  67.     //update y position
  68.     if (++ycount > ydelay)
  69.     {
  70.         ycount = 0;
  71.         y += vely;
  72.     }
  73. }
  74.  
  75. void sprite::updateAnimation() 
  76. {
  77.     //update frame based on animdir
  78.     if (++framecount > framedelay)
  79.     {
  80.         framecount = 0;
  81.         curframe += animdir;
  82.  
  83.         if (curframe < 0) {
  84.             curframe = totalframes-1;
  85.         }
  86.         if (curframe > totalframes-1) {
  87.             curframe = 0;
  88.         }
  89.     }
  90. }
  91.  
  92. int sprite::inside(int x,int y,int left,int top,int right,int bottom)
  93. {
  94.     if (x > left && x < right && y > top && y < bottom)
  95.         return 1;
  96.     else
  97.         return 0;
  98. }
  99.  
  100. int sprite::pointInside(int px,int py)
  101. {
  102.     return inside(px, py, (int)x, (int)y, (int)x+width, (int)y+height);
  103. }
  104.  
  105. int sprite::collided(sprite *other, int shrink)
  106. {
  107.     int wa = (int)x + width;
  108.     int ha = (int)y + height;
  109.     int wb = (int)other->x + other->width;
  110.     int hb = (int)other->y + other->height;
  111.  
  112.     if (inside((int)x, (int)y, (int)other->x+shrink, (int)other->y+shrink, wb-shrink, hb-shrink) ||
  113.         inside((int)x, ha, (int)other->x+shrink, (int)other->y+shrink, wb-shrink, hb-shrink) ||
  114.         inside(wa, (int)y, (int)other->x+shrink, (int)other->y+shrink, wb-shrink, hb-shrink) ||
  115.         inside(wa, ha, (int)other->x+shrink, (int)other->y+shrink, wb-shrink, hb-shrink))
  116.         return 1;
  117.     else
  118.         return 0;
  119. }
  120.  
  121.