TankAvatar.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <tank/TankAvatar.h>
00022 #include <common/Defines.h>
00023 #include <image/ImagePng.h>
00024 #include <stdio.h>
00025 #include <zlib.h>
00026 
00027 #ifndef S3D_SERVER
00028 #include <GLEXT/GLTexture.h>
00029 
00030 GLTexture *TankAvatar::defaultTexture_ = 0;
00031 std::list<TankAvatar::AvatarStore> TankAvatar::storeEntries_;
00032 #endif
00033 
00034 TankAvatar::TankAvatar()
00035 {
00036 #ifndef S3D_SERVER
00037         texture_ = 0;
00038 #endif
00039         file_ = new NetBuffer();
00040 }
00041 
00042 TankAvatar::~TankAvatar()
00043 {
00044         delete file_;
00045 }
00046 
00047 bool TankAvatar::writeMessage(NetBuffer &buffer)
00048 {
00049         buffer.addToBuffer(name_);
00050         buffer.addToBuffer(file_->getBufferUsed());
00051         if (file_->getBufferUsed() > 0)
00052         {
00053                 buffer.addDataToBuffer(file_->getBuffer(),
00054                         file_->getBufferUsed());
00055         }
00056         return true;
00057 }
00058 
00059 void TankAvatar::clear()
00060 {
00061 #ifndef S3D_SERVER
00062         texture_ = 0;
00063 #endif
00064         name_ = "";
00065         file_->reset();
00066 }
00067 
00068 bool TankAvatar::readMessage(NetBufferReader &reader)
00069 {
00070         unsigned int used = 0;
00071         if (!reader.getFromBuffer(name_)) return false;
00072         if (!reader.getFromBuffer(used)) return false;
00073         if (used > 0)
00074         {
00075                 file_->allocate(used);
00076                 file_->reset();
00077                 file_->setBufferUsed(used);
00078                 reader.getDataFromBuffer(file_->getBuffer(),
00079                         used);
00080         }
00081         return true;
00082 }
00083 
00084 bool TankAvatar::loadFromFile(const std::string &fileName)
00085 {
00086         FILE *in = fopen(fileName.c_str(), "rb");
00087         if (in)
00088         {
00089                 name_ = fileName;
00090                 file_->reset();
00091                 unsigned char readBuf[512];
00092                 while (unsigned int size = fread(readBuf, sizeof(unsigned char), 512, in))
00093                 {
00094                         file_->addDataToBuffer(readBuf, size);
00095                 }
00096                 fclose(in);
00097                 return true;
00098         }
00099         return false;
00100 }
00101 
00102 unsigned int TankAvatar::getCrc()
00103 {
00104         unsigned int crc =  crc32(0L, Z_NULL, 0);
00105         crc = crc32(crc, (unsigned char *) 
00106                 file_->getBuffer(), file_->getBufferUsed());    
00107         return crc;
00108 }
00109 
00110 bool TankAvatar::setFromBuffer(const std::string &fileName,
00111         NetBuffer &buffer, bool createTexture)
00112 {
00113         ImagePng png;
00114         if (!png.loadFromBuffer(buffer)) return false;
00115         if (png.getWidth() != 32 || 
00116                 png.getHeight() != 32) return false;
00117 
00118         name_ = fileName;
00119         file_->reset();
00120         file_->addDataToBuffer(buffer.getBuffer(), 
00121                 buffer.getBufferUsed());
00122 
00123 #ifndef S3D_SERVER
00124         if (createTexture)
00125         {
00126                 texture_ = 0;
00127                 unsigned int crc = getCrc();
00128                 std::list<AvatarStore>::iterator itor;
00129                 for (itor = storeEntries_.begin();
00130                         itor != storeEntries_.end();
00131                         itor++)
00132                 {
00133                         AvatarStore &store = (*itor);
00134                         if (store.crc_ == crc &&
00135                                 0 == strcmp(store.name_.c_str(), name_.c_str()))
00136                         {
00137                                 texture_ = store.texture_;
00138                                 break;
00139                         }
00140                 }
00141                 if (!texture_)
00142                 {
00143                         texture_ = new GLTexture;
00144                         texture_->create(png);
00145                         AvatarStore store;
00146                         store.crc_ = crc;
00147                         store.name_ = name_;
00148                         store.texture_ = texture_;
00149                         storeEntries_.push_back(store);
00150                 }
00151         }
00152 #endif
00153 
00154         return true;
00155 }
00156 
00157 #ifndef S3D_SERVER
00158 GLTexture *TankAvatar::getTexture()
00159 {
00160         if (!texture_)
00161         {
00162                 if (!defaultTexture_)
00163                 {
00164                         defaultTexture_ = new GLTexture();
00165                         ImagePng png;
00166                         png.loadFromFile(
00167                                 S3D::getDataFile("data/avatars/player.png"));
00168                         defaultTexture_->create(png);
00169                 }
00170                 return defaultTexture_;
00171         }
00172 
00173         return texture_; 
00174 }
00175 #endif

Generated on Mon Feb 16 15:14:51 2009 for Scorched3D by  doxygen 1.5.3