tileset.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software 
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00010 /*!****************************************************************************
00011  * \file    tileset.cpp
00012  * \author  Philip Vorsilak, gorzuate@allacrost.org
00013  * \brief   Source file for editor's tileset, used for maintaining a visible
00014  *          "list" of tiles to select from for painting on the map.
00015  *****************************************************************************/
00016 
00017 #include "tileset.h"
00018 
00019 using namespace std;
00020 using namespace hoa_video;
00021 using namespace hoa_script;
00022 using namespace hoa_editor;
00023 
00024 Tileset::Tileset(QWidget* parent, const QString& name)
00025 { 
00026   // Create filename from name.
00027   tileset_name = name;
00028   QString img_filename = QString("img/tilesets/" + name + ".png");
00029   QString dat_filename = QString("dat/tilesets/" + name + ".lua");
00030 
00031   // Load the tileset image.
00032   tiles.resize(256);
00033   for (int i = 0; i < 256; i++)
00034     tiles[i].SetDimensions(1.0f, 1.0f);
00035   // NOTE: the following line currently causes a seg fault when the editor exits!
00036   if (VideoManager->LoadMultiImageFromNumberElements(tiles, std::string(img_filename.toAscii()), 16, 16) == false)
00037     qDebug("LoadMultiImage failed to load tileset " + img_filename);
00038 
00039   // Set up the table.
00040   table = new Q3Table(16, 16);
00041   table->setReadOnly(true);
00042   table->setShowGrid(false);
00043   table->setSelectionMode(Q3Table::Multi);
00044   table->setTopMargin(0);
00045   table->setLeftMargin(0);
00046   for (int i = 0; i < table->numRows(); i++)
00047     table->setRowHeight(i, TILE_HEIGHT);
00048   for (int i = 0; i < table->numCols(); i++)
00049     table->setColumnWidth(i, TILE_WIDTH);
00050 
00051   // Read in tiles and create table items.
00052   QRect rectangle;
00053   for (int row = 0; row < 16; row++)
00054   {
00055     for (int col = 0; col < 16; col++)
00056     {
00057       QImageReader reader(img_filename, "png");
00058       rectangle.setRect(col * TILE_WIDTH, row * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
00059       reader.setClipRect(rectangle);
00060       QImage tile_img = reader.read();
00061       QVariant variant = tile_img;
00062       if (!tile_img.isNull())
00063       {
00064         QPixmap tile_pixmap = variant.value<QPixmap>();
00065         table->setPixmap(row, col, tile_pixmap);
00066       } // image of the tile must not be null
00067       else
00068         qDebug(QString("%1").arg(reader.error()));
00069     } // iterate through the columns of the tileset
00070   } // iterate through the rows of the tileset
00071 
00072 
00073   // Read in walkability information.
00074   ReadScriptDescriptor read_data;
00075   vector<int32> vect;           // used to read in vectors from the data file
00076   
00077   if (!read_data.OpenFile(std::string(dat_filename.toAscii())))
00078     QMessageBox::warning(parent, "Loading File...", QString("ERROR: could not open %1 for reading!").arg(dat_filename));
00079 
00080   read_data.OpenTable("walkability");
00081   //uint32 table_size = read_data.ReadGetTableSize();
00082   for (int32 i = 0; i < 16; i++)
00083   {
00084     read_data.OpenTable(i);
00085     if (read_data.IsErrorDetected() == false)
00086     {
00087       for (int32 j = 0; j < 16; j++)
00088       {
00089         read_data.ReadIntVector(j, vect);
00090         if (read_data.IsErrorDetected() == false)
00091           walkability[i * 16 + j] = vect;
00092         vect.clear();
00093       } // iterate through all tiles in a row
00094       read_data.CloseTable();
00095     } // make sure a row exists
00096   } // iterate through all rows of the walkability table
00097   read_data.CloseTable();
00098 
00099   //Read animated tiles
00100   uint32 animated_table_size = read_data.GetTableSize("animated_tiles");
00101   read_data.OpenTable("animated_tiles");
00102   for (uint32 i=1; i<=animated_table_size; i++) 
00103   {
00104     _animated_tiles.push_back(std::vector<AnimatedTileData>());
00105     std::vector<AnimatedTileData>& tiles=_animated_tiles.back();
00106     // Tile count is table size / 2 (tile id + time)
00107     uint32 tile_count=read_data.GetTableSize(i) / 2;
00108     read_data.OpenTable(i);
00109     for(uint32 index=1; index<=tile_count; index++) 
00110     {
00111       tiles.push_back(AnimatedTileData());
00112       AnimatedTileData& tile_data=tiles.back();
00113       tile_data.tile_id=read_data.ReadUInt(index*2-1);
00114       tile_data.time=read_data.ReadUInt(index*2);
00115     }
00116     read_data.CloseTable();
00117   }
00118   read_data.CloseTable();
00119 } // Tileset constructor
00120 
00121 Tileset::~Tileset()
00122 {
00123   delete table;
00124 } // TilesetTable destructor
00125 
00126 void Tileset::Save() {
00127   std::string dat_filename = "dat/tilesets/" + std::string(tileset_name.toAscii()) + ".lua";
00128   std::string img_filename = "img/tilesets/" + std::string(tileset_name.toAscii()) + ".png";
00129   WriteScriptDescriptor write_data;
00130 
00131   // Write global infos
00132   write_data.OpenFile(dat_filename); // NOTE: return value of this function should be checked!!!
00133   write_data.InsertNewLine();
00134   write_data.WriteString("file_name",dat_filename);
00135   write_data.WriteString("image",dat_filename);
00136   write_data.WriteInt("num_tile_cols",table->numCols());
00137   write_data.WriteInt("num_tile_rows",table->numRows());
00138   write_data.InsertNewLine();
00139 
00140   // Write walkability data
00141   write_data.BeginTable("walkability");
00142   for(int row=0;row<table->numRows();row++) {
00143     write_data.BeginTable(row);
00144     for(int col=0;col<table->numCols();col++) {
00145       write_data.WriteIntVector(col,this->walkability[row*16+col]);
00146     }
00147     write_data.EndTable();
00148   }
00149   write_data.EndTable();
00150 
00151   // Write animated tiles
00152   write_data.BeginTable("animated_tiles");
00153   for(int i=0;i<_animated_tiles.size();i++) {
00154     std::vector<int32> data;
00155     for(int c=0;c<_animated_tiles[i].size();c++) {
00156       data.push_back(_animated_tiles[i][c].tile_id);
00157       data.push_back(_animated_tiles[i][c].time);
00158     }
00159     write_data.WriteIntVector(i+1,data);
00160   }
00161   write_data.EndTable();
00162 
00163   write_data.CloseFile();
00164 }

Generated on Fri Jul 6 23:11:25 2007 for Hero of Allacrost by  doxygen 1.5.1