From c784dea82dbeee0133077a1b673f5652c59c2f77 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sat, 25 Dec 2004 13:38:16 +0000 Subject: Made image.cpp into module and done some more attempts at making graphics.cpp readable. --- src/Makefile.am | 1 + src/graphic/graphic.cpp | 86 +++++++++++++++++++++++------------ src/graphic/image.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++++ src/graphic/image.h | 117 +++++++++++++++--------------------------------- 4 files changed, 211 insertions(+), 109 deletions(-) create mode 100644 src/graphic/image.cpp (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 1890a8d6..cf1b2550 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,6 +2,7 @@ bin_PROGRAMS = tmw tmw_SOURCES = sound/sound.cpp \ graphic/2xsai.cpp \ graphic/graphic.cpp \ + graphic/image.cpp \ gui/button.cpp \ gui/char_server.cpp \ gui/char_select.cpp \ diff --git a/src/graphic/graphic.cpp b/src/graphic/graphic.cpp index 0fe36ae5..1df06bd5 100644 --- a/src/graphic/graphic.cpp +++ b/src/graphic/graphic.cpp @@ -322,35 +322,57 @@ void do_graphic(void) { while (node) { unsigned short x = get_x(node->coordinates); unsigned short y = get_y(node->coordinates); - unsigned char dir = get_direction(node->coordinates); + unsigned char dir = get_direction(node->coordinates) / 2; int sx = x - camera_x; int sy = y - camera_y; if ((node->job >= 100) && (node->job <= 110)) { // Draw a NPC - new_npcset->spriteset[4 * (node->job - 100) + dir / 2]->draw(vbuffer, sx * 16 - 4 - offset_x, sy * 16 - 24 - offset_y); + new_npcset->spriteset[4 * (node->job - 100) + dir]->draw(vbuffer, + sx * 16 - 4 - offset_x, + sy * 16 - 24 - offset_y); } else if (node->job < 10) { // Draw a player node->text_x = sx * 16 - 34 + get_x_offset(node) - offset_x; node->text_y = sy * 16 - 36 + get_y_offset(node) - offset_y; if (node->action == SIT) node->frame = 0; if (node->action == ATTACK) { - new_playerset->spriteset[(dir / 2) + 4 * (node->frame + node->action + 4 * node->weapon)]->draw(vbuffer, node->text_x, node->text_y); - draw_rle_sprite(vbuffer, (RLE_SPRITE*)weaponset[16 * node->weapon + 4 * node->frame + dir / 2].dat, node->text_x, node->text_y); - masked_blit(hairset, vbuffer, 20 * (node->hair_color - 1), 20 * (dir / 2 + 4 * (node->hair_style - 1)), node->text_x + 31 + hairtable[node->action + node->frame + 4 * node->weapon][dir / 2][0], node->text_y + 15 + hairtable[node->action + node->frame + 4 * node->weapon][dir / 2][1], 20, 20); + int pf = node->frame + node->action + 4 * node->weapon; + int wf = 16 * node->weapon + 4 * node->frame; + new_playerset->spriteset[4 * pf + dir]->draw(vbuffer, + node->text_x, node->text_y); + draw_rle_sprite(vbuffer, (RLE_SPRITE*)weaponset[wf + dir].dat, + node->text_x, node->text_y); + masked_blit(hairset, vbuffer, + 20 * (node->hair_color - 1), + 20 * (dir + 4 * (node->hair_style - 1)), + node->text_x + 31 + hairtable[pf][dir][0], + node->text_y + 15 + hairtable[pf][dir][1], + 20, 20); } else { - new_playerset->spriteset[4 * (node->frame + node->action) + dir / 2]->draw(vbuffer, node->text_x, node->text_y); - masked_blit(hairset, vbuffer, 20 * (node->hair_color - 1), 20 * (dir / 2 + 4 * (node->hair_style - 1)), node->text_x + 31 + hairtable[node->action + node->frame][dir / 2][0], node->text_y + 15 + hairtable[node->action + node->frame][dir / 2][1], 20, 20); + int pf = node->frame + node->action; + new_playerset->spriteset[4 * pf + dir]->draw(vbuffer, + node->text_x, node->text_y); + masked_blit(hairset, vbuffer, + 20 * (node->hair_color - 1), + 20 * (dir + 4 * (node->hair_style - 1)), + node->text_x + 31 + hairtable[pf][dir][0], + node->text_y + 15 + hairtable[pf][dir][1], + 20, 20); } if (node->emotion != 0) { - new_emotionset->spriteset[node->emotion - 1]->draw(vbuffer, sx * 16 - 5 + get_x_offset(node) - offset_x, sy * 16 - 45 + get_y_offset(node) - offset_y); + new_emotionset->spriteset[node->emotion - 1]->draw(vbuffer, + sx * 16 - 5 + get_x_offset(node) - offset_x, + sy * 16 - 45 + get_y_offset(node) - offset_y); node->emotion_time--; if (node->emotion_time == 0) { node->emotion = 0; } } if (node->action != STAND && node->action != SIT) { - node->frame = (get_elapsed_time(node->tick_time) * 4) / (node->speed); + node->frame = + (get_elapsed_time(node->tick_time) * 4) / (node->speed); + if (node->frame >= 4) { node->frame = 0; node->action = STAND; @@ -360,13 +382,11 @@ void do_graphic(void) { } } - /* textprintf_centre_ex(vbuffer, font, node->text_x + 45, node->text_y + 55, node->speech_color, -1, - "(%d,%d)", x, y); - */ + "(%d,%d), %d", x, y, dir); } else if (node->job == 45) { // Draw a warp } else { // Draw a monster @@ -380,10 +400,10 @@ void do_graphic(void) { //int r_y = node->text_y-get_y_offset(node); if (node->action == MONSTER_DEAD) { - new_monsterset->spriteset[(dir / 2) + 4 * (node->job - 1002) + 8 * MONSTER_DEAD]->draw(vbuffer, node->text_x, node->text_y); + new_monsterset->spriteset[dir + 4 * (node->job - 1002) + 8 * MONSTER_DEAD]->draw(vbuffer, node->text_x, node->text_y); } else { - new_monsterset->spriteset[(dir / 2) + 4 * (node->job - 1002) + 8 * (node->frame + node->action)]->draw(vbuffer, node->text_x, node->text_y); + new_monsterset->spriteset[dir + 4 * (node->job - 1002) + 8 * (node->frame + node->action)]->draw(vbuffer, node->text_x, node->text_y); } if (node->action != STAND) { @@ -391,23 +411,31 @@ void do_graphic(void) { if (node->frame >= 4) { if (node->action != MONSTER_DEAD && node->path) { if (node->path->next) { - PATH_NODE *old = node->path; + int old_x, old_y, new_x, new_y; + old_x = node->path->x; + old_y = node->path->y; node->path = node->path->next; + new_x = node->path->x; + new_y = node->path->y; direction = 0; - //if(node->path->next) { - if (node->path->x>old->x && node->path->y>old->y)direction = SE; - else if (node->path->xx && node->path->y>old->y)direction = SW; - else if (node->path->x>old->x && node->path->yy)direction = NE; - else if (node->path->xx && node->path->yy)direction = NW; - else if (node->path->x>old->x)direction = EAST; - else if (node->path->xx)direction = WEST; - else if (node->path->y>old->y)direction = SOUTH; - else if (node->path->yy)direction = NORTH; - //} - - set_coordinates(node->coordinates, node->path->x, node->path->y, direction); - - //node->tick_time = tick_time; + + if (new_x > old_x) { + if (new_y > old_y) direction = SE; + else if (new_y < old_y) direction = NE; + else direction = EAST; + } + else if (new_x < old_x) { + if (new_y > old_y) direction = SW; + else if (new_y < old_y) direction = NW; + else direction = WEST; + } + else { + if (new_y > old_y) direction = SOUTH; + else if (new_y < old_y) direction = NORTH; + } + + set_coordinates(node->coordinates, + node->path->x, node->path->y, direction); } else { node->action = STAND; } diff --git a/src/graphic/image.cpp b/src/graphic/image.cpp new file mode 100644 index 00000000..da9bb1d5 --- /dev/null +++ b/src/graphic/image.cpp @@ -0,0 +1,116 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * By ElvenProgrammer aka Eugenio Favalli (umperio@users.sourceforge.net) + */ + +#include "image.h" + + +// Image + +Image::Image(int offset_x, int offset_y) { + this->offset_x = offset_x; + this->offset_y = offset_y; +} + + +// RleImage + +RleImage::RleImage(RLE_SPRITE *src, int offset_x, int offset_y): + Image(offset_x, offset_y) +{ + this->src = src; +} + +RleImage::~RleImage() { + destroy_rle_sprite(src); +} + +void RleImage::draw(BITMAP *dest, int x, int y) { + draw_rle_sprite(dest, src, x + offset_x, y + offset_y); +} + + +// VideoImage + +VideoImage::VideoImage(BITMAP *src, int offset_x, int offset_y): + Image(offset_x, offset_y) +{ + this->src = src; +} + +VideoImage::~VideoImage() { + destroy_bitmap(src); +} + +void VideoImage::draw(BITMAP *dest, int x, int y) { + masked_blit(src, dest, 0, 0, x + offset_x, y + offset_y, src->w, src->h); +} + + +// Spriteset + +Spriteset::Spriteset(std::string filename) +{ + DATAFILE *datafile = load_datafile(filename.c_str()); + if (!datafile)error("Unable to load graphic file: " + filename); + int i = 0; + while (datafile[i].type != DAT_END) { + Image *temp_image; + if (gfx_capabilities & GFX_HW_VRAM_BLIT) { + BITMAP *temp_video_bitmap = create_video_bitmap( + ((RLE_SPRITE *)datafile[i].dat)->w, + ((RLE_SPRITE *)datafile[i].dat)->h); + if (temp_video_bitmap) { + clear_to_color(temp_video_bitmap, makecol(255, 0, 255)); + draw_rle_sprite(temp_video_bitmap, + (RLE_SPRITE *)datafile[i].dat, 0, 0); + temp_image = new VideoImage(temp_video_bitmap, + getProperty(&datafile[i], DAT_ID('X','C','R','P')), + getProperty(&datafile[i], DAT_ID('Y','C','R','P'))); + } else { + warning("You ran out of video memory!"); + temp_image = new RleImage( + (RLE_SPRITE*)datafile[i].dat, + getProperty(&datafile[i], DAT_ID('X','C','R','P')), + getProperty(&datafile[i], DAT_ID('Y','C','R','P'))); + } + } else { + temp_image = new RleImage( + (RLE_SPRITE*)datafile[i].dat, + getProperty(&datafile[i], DAT_ID('X','C','R','P')), + getProperty(&datafile[i], DAT_ID('Y','C','R','P'))); + } + spriteset.push_back(temp_image); + i++; + } +} + +Spriteset::~Spriteset() +{ + for (int i = 0; i < spriteset.size(); i++) { + delete spriteset[i]; + } +} + +int Spriteset::getProperty(DATAFILE *datafile, int type) { + return atoi(get_datafile_property(datafile, type)); +} diff --git a/src/graphic/image.h b/src/graphic/image.h index b5944b3e..2491b5ad 100644 --- a/src/graphic/image.h +++ b/src/graphic/image.h @@ -37,19 +37,14 @@ * operated by the grabber utility. */ class Image { - protected: - // From where to start drawing - int offset_x, offset_y; public: /** * Creates an Image * @param offset_x is the x offset from where to start drawing * @param offset_y is the y offset from where to start drawing */ - Image(int offset_x, int offset_y) { - this->offset_x = offset_x; - this->offset_y = offset_y; - } + Image(int offset_x, int offset_y); + /** * Virtual function to draw a sprite * @param dest is the destination bitmap on which to draw the sprite @@ -57,15 +52,16 @@ class Image { * @param y is the vertical position */ virtual void draw(BITMAP *dest, int x, int y) = 0; + + protected: + // From where to start drawing + int offset_x, offset_y; }; /** * A RLE sprite */ class RleImage : public Image { - private: - // Refernce to RLE_SPRITE - RLE_SPRITE *src; public: /** * Creates a RleSprite @@ -73,21 +69,21 @@ class RleImage : public Image { * @param offset_x is the x offset from where to start drawing * @param offset_y is the y offset from where to start drawing */ - RleImage(RLE_SPRITE *src, int offset_x, int offset_y) : Image(offset_x, offset_y) { - this->src = src; - } + RleImage(RLE_SPRITE *src, int offset_x, int offset_y); + /** - * Destroy a RleSprite + * Destructor */ - virtual ~RleImage() { - destroy_rle_sprite(src); - } + virtual ~RleImage(); + /** - * Draw a sprite + * Draws a sprite */ - void draw(BITMAP *dest, int x, int y) { - draw_rle_sprite(dest, src, x+offset_x, y+offset_y); - } + void draw(BITMAP *dest, int x, int y); + + private: + // Reference to RLE_SPRITE + RLE_SPRITE *src; }; /** @@ -104,84 +100,45 @@ class VideoImage : public Image { * @param offset_x is the x offset from where to start drawing * @param offset_y is the y offset from where to start drawing */ - VideoImage(BITMAP *src, int offset_x, int offset_y) : Image(offset_x, offset_y) { - this->src = src; - } + VideoImage(BITMAP *src, int offset_x, int offset_y); + /** - * Destroy a VideoImage + * Destructor */ - virtual ~VideoImage() { - destroy_bitmap(src); - } + virtual ~VideoImage(); + /** - * Draw a sprite + * Draws a sprite */ - void draw(BITMAP *dest, int x, int y) { - masked_blit(src, dest, 0, 0, x+offset_x, y+offset_y, src->w, src->h); - } + void draw(BITMAP *dest, int x, int y); }; /** * Stores a complete set of sprites. */ class Spriteset { - private: - /** - * Helper function to get offset - * @param datafile is a reference to the whole spriteset - * @param type is the property of the datafile object - */ - int get_property(DATAFILE *datafile, int type) { - return atoi(get_datafile_property(datafile, type)); - } public: // Vector storing the whole spriteset. std::vector spriteset; + /** * Load a datafile containing the spriteset * @param filename is the path of the datafile */ - Spriteset(std::string filename) { - DATAFILE *datafile = load_datafile(filename.c_str()); - if(!datafile)error("Unable to load graphic file: " + filename); - int i = 0; - while(datafile[i].type!=DAT_END) { - Image *temp_image; - if(gfx_capabilities & GFX_HW_VRAM_BLIT) { - BITMAP *temp_video_bitmap = create_video_bitmap( - ((RLE_SPRITE *)datafile[i].dat)->w, - ((RLE_SPRITE *)datafile[i].dat)->h); - if(temp_video_bitmap) { - clear_to_color(temp_video_bitmap, makecol(255,0,255)); - draw_rle_sprite(temp_video_bitmap, - (RLE_SPRITE *)datafile[i].dat, 0, 0); - temp_image = new VideoImage(temp_video_bitmap, - get_property(&datafile[i], DAT_ID('X','C','R','P')), - get_property(&datafile[i], DAT_ID('Y','C','R','P'))); - } else { - warning("You ran out of video memory!"); - temp_image = new RleImage( - (RLE_SPRITE*)datafile[i].dat, - get_property(&datafile[i], DAT_ID('X','C','R','P')), - get_property(&datafile[i], DAT_ID('Y','C','R','P'))); - } - } else { - temp_image = new RleImage( - (RLE_SPRITE*)datafile[i].dat, - get_property(&datafile[i], DAT_ID('X','C','R','P')), - get_property(&datafile[i], DAT_ID('Y','C','R','P'))); - } - spriteset.push_back(temp_image); - i++; - } - } + Spriteset(std::string filename); + + /** + * Destructor + */ + ~Spriteset(); + + private: /** - * Destoy a spriteset + * Helper function to get offset + * @param datafile is a reference to the whole spriteset + * @param type is the property of the datafile object */ - ~Spriteset() { - for(int i=0;i