/* * $Id: sprite.cc,v 1.2 2002/09/18 21:46:22 stefan Exp $ * Copyright (c) 2002, Dominik Schnitzer * * JFK - JFK Fucking Killerz, a massive multiplayer 2d shoot'em-up game * http://relax.ath.cx/jfk/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "sprite.h" #include "output.h" #include "debug.h" using namespace std; using namespace JFK::client; sprite::sprite(output* output, const string& section, const string& file) { int i = 0; image img; out = output; while ((img = out->load_image(section, file, i, false)) != NULL) { images.push_back(img); i++; } if (i > 0) current = images.front(); else { current = NULL; /* TODO: throw exception */ LOG(("Error loading: %s", file.c_str())); } } sprite::~sprite() { while (current) { out->free_image(images.back()); images.resize(images.size() - 1); if (images.size() > 0) current = images.back(); else current = NULL; } } void sprite::draw_centered(int x, int y) { /* TODO: throw exception if current is NULL */ /* Drawing Origin of a sprite is it's CENTER (!). This is a bit weird * I hope this changes sometime. */ out->draw_image(current, x - out->get_image_width(current)/2, y - out->get_image_height(current)/2); } void sprite::draw(int x, int y) { /* TODO: throw exception if current is NULL */ out->draw_image(current, x, y); } void sprite::update(double dx, double dy, int status) { if (images.size() <= 0) { current = NULL; return; } /* Calculate the correct image number out of the objects looking direction * and available images */ double angle = atan2(dy, dx); int imgnr = (int) floor(angle/((PI*2)/images.size())); if (imgnr < 0) imgnr = images.size() + imgnr; current = images[imgnr]; } void sprite::set(int angle, int anim, int status) { /* TODO: Throw an exception if out of bounds * if ((angle < 0) || (angle >= images.size())) */ current = images[angle]; }