home *** CD-ROM | disk | FTP | other *** search
- #include <allegro.h>
- #include "sprite.h"
-
-
- sprite::sprite() {
- image = NULL;
- frame = NULL;
- alive = 1;
- direction = 0;
- animcolumns = 0;
- animstartx = 0;
- animstarty = 0;
- x = 0.0f;
- y = 0.0f;
- width = 0;
- height = 0;
- xdelay = 0;
- ydelay = 0;
- xcount = 0;
- ycount = 0;
- velx = 0.0;
- vely = 0.0;
- speed = 0.0;
- curframe = 0;
- totalframes = 1;
- framecount = 0;
- framedelay = 10;
- animdir = 1;
- faceAngle = 0;
- moveAngle = 0;
- }
-
- sprite::~sprite() {
- //remove bitmap from memory
- if (image != NULL)
- destroy_bitmap(image);
- if (frame != NULL)
- destroy_bitmap(frame);
-
- }
-
- int sprite::load(char *filename) {
- image = load_bitmap(filename, NULL);
- if (image == NULL) return 0;
- width = image->w;
- height = image->h;
- return 1;
- }
-
- void sprite::draw(BITMAP *dest)
- {
- draw_sprite(dest, image, x, y);
- }
-
- void sprite::drawframe(BITMAP *dest)
- {
- int fx = animstartx + (curframe % animcolumns) * width;
- int fy = animstarty + (curframe / animcolumns) * height;
- masked_blit(image,dest,fx,fy,x,y,width,height);
- }
-
- void sprite::rotateframe(BITMAP *dest, int angle, int dx, int dy)
- {
- if (dx == -1) dx = x;
- if (dy == -1) dy = y;
-
- //create scratch frame image if needed
- if (frame==NULL)
- frame = create_bitmap(width,height);
-
- //blank out the frame image
- clear_to_color(frame, makecol(255,0,255));
-
- //grab frame image out of sprite sheet
- int fx = animstartx + (curframe % animcolumns) * width;
- int fy = animstarty + (curframe / animcolumns) * height;
- masked_blit(image,frame,fx,fy,0,0,width-1,height-1);
-
- //rotate frame image
- rotate_sprite(dest, frame, dx, dy, itofix(angle));
- }
-
- void sprite::updatePosition()
- {
- //update x position
- if (++xcount > xdelay)
- {
- xcount = 0;
- x += velx;
- }
-
- //update y position
- if (++ycount > ydelay)
- {
- ycount = 0;
- y += vely;
- }
- }
-
- void sprite::updateAnimation()
- {
- //update frame based on animdir
- if (++framecount > framedelay)
- {
- framecount = 0;
- curframe += animdir;
-
- if (curframe < 0) {
- curframe = totalframes-1;
- }
- if (curframe > totalframes-1) {
- curframe = 0;
- }
- }
- }
-
- int sprite::inside(int x,int y,int left,int top,int right,int bottom)
- {
- if (x > left && x < right && y > top && y < bottom)
- return 1;
- else
- return 0;
- }
-
- int sprite::pointInside(int px,int py)
- {
- return inside(px, py, x, y, x+width, y+height);
- }
-
- int sprite::collided(sprite *other, int shrink)
- {
- int wa = x + width;
- int ha =y + height;
- int wb = other->x + other->width;
- int hb = other->y + other->height;
-
- if (inside(x, y, other->x+shrink, other->y+shrink, wb-shrink, hb-shrink) ||
- inside(x, ha, other->x+shrink, other->y+shrink, wb-shrink, hb-shrink) ||
- inside(wa, y, other->x+shrink, other->y+shrink, wb-shrink, hb-shrink) ||
- inside(wa, ha, other->x+shrink, other->y+shrink, wb-shrink, hb-shrink))
- return 1;
- else
- return 0;
- }
-
-