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

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