/** ****************************************************************************** @file /common/rm/rmrast.cpp @brief RM pristupujici k rasterum @author Vta @version 1.0 ******************************************************************************/ #include "common/rm/rmrast.h" #include "common/utils.h" #ifndef _K_EDITOR_ using namespace gui; #endif // ifndef namespace rm { /// Pomocny buffer static char bu_file[K8_MAX_FILE_NAME_LEN]; /// Pomocny buffer static char bu_path[K8_MAX_PATH_LEN]; #define bu_typesize 10 /// Pomocny buffer static char bu_type[bu_typesize]; void TRM_raster::createRecord(int id) { this->lock(); if ((*(this->rasters))[id]==NULL) { (*(this->rasters))[id]=(struct TRaster*)KMemAlloc(sizeof (struct TRaster)); (*(this->rasters))[id]->szFilename[0]=0; // nactu typ - musim nejdriv, abych kdyztak mohl nacist soubor if (this->index->getData(bu_type,bu_typesize,"raster",id,"type",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMraster can not load the raster having id %i",id); THROW(E_8K_RM,"RMraster can not load a raster"); } // priradim typ if (strcmp(bu_type,RASTER_BMP_NAME)==0) { (*(this->rasters))[id]->type=RASTER_BMP_CODE; } else { if (strcmp(bu_type,RASTER_TGA_NAME)==0) { (*(this->rasters))[id]->type=RASTER_TGA_CODE; } } // nactu jmeno souboru if (this->index->getData(bu_file,K8_MAX_FILE_NAME_LEN,"raster",id,"file",0,NULL)==XML_ERROR_NO_SUCH_A_PATH) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMraster can not load the raster having id %i",id); THROW(E_8K_RM,"RMraster can not load a raster"); } switch ((*(this->rasters))[id]->type) // sestavim file, ktery se ma nacitat { case RASTER_BMP_CODE: { strpartpath(bu_path,RESOURCES_DIR,this->rmconfi->getPack(),BMP_DIR,bu_file,K8_MAX_PATH_LEN,""); // pripona je v xml file } break; case RASTER_TGA_CODE: { strpartpath(bu_path,RESOURCES_DIR,this->rmconfi->getPack(),TGA_DIR,bu_file,K8_MAX_PATH_LEN,""); // pripona je v xml file } break; } strcopy((*this->rasters)[id]->szFilename,bu_path,K8_MAX_FILE_NAME_LEN); // this->index->getData(info,infosize,"raster",id,"width",0,NULL); // int i=_wtoi(info); // (*(this->rasters))[id]->x=i; // this->index->getData(info,infosize,"raster",id,"heigh",0,NULL); // i=_wtoi(info); // (*(this->rasters))[id]->y=i; // nastavim zbytek // (*(this->rasters))[id]->group=this->group; (*(this->rasters))[id]->surface=NULL; } this->unlock(); } int TRM_raster::getRasterId(const char *name) // nacte raster { this->lock(); int id=this->index->getIdOfTagWhoseChildContains((char*)name,0,0,0); this->unlock(); return id; /* DA structure; // zastarale this->index->getListOfStructs(&structure,fwch,"file",0); int ids[NUMBER_OF_TYPES+1]; int j=structure.getNext(-1); char se[4]; int id; while (j!=-1) { this->index->getData(se,4,"raster",j,"type",0,0); if (strcmp(se,ext)==0) // nasel tu spravnou priponu { id=j; } else { KMemFree(structure[j]); j=structure.getNext(j); } } this->loadraster(1,priority); return 1;*/ } void TRM_raster::loadData(int id) { this->loadRaster(id); } void TRM_raster::loadRaster(int id) { this->lock(); // if ((*(this->rasters))[id]==NULL) // pokud TRaster jeste nebyl vytvoren { this->createRecord(id); if ((*(this->rasters))[id]->surface==NULL) { switch ((*(this->rasters))[id]->type) { case RASTER_BMP_CODE: { // char * str = (*(this->rasters))[id]->szFilename; (*(this->rasters))[id]->surface=SDL_LoadBMP((*(this->rasters))[id]->szFilename); if ((*(this->rasters))[id]->surface==NULL) { GLOBALLOGID(PRIORITY_FATAL, "RMraster can not load the raster from the file %s",(*(this->rasters))[id]->szFilename); THROW(E_8K_RM,"RMraster can not load a raster"); } } break; case RASTER_TGA_CODE: { #ifndef _K_EDITOR_ try { (*(this->rasters))[id]->surface=LoadTGA((*(this->rasters))[id]->szFilename); } catch (E_8K_TGA_READER) { GLOBALLOGID(PRIORITY_FATAL, "RMraster can not load the raster from the file %s",(*(this->rasters))[id]->szFilename); THROW(E_8K_RM,"RMraster can not load a raster"); } if ((*(this->rasters))[id]->surface==NULL) { GLOBALLOGID(PRIORITY_FATAL, "RMraster can not load the raster from the file %s",(*(this->rasters))[id]->szFilename); THROW(E_8K_RM,"RMraster can not load a raster"); } #endif } break; default: { GLOBALLOGID(PRIORITY_FATAL, "RMraster can not load an unknown type of raster %i in the record id %i",(*(this->rasters))[id]->type,id); THROW(E_8K_RM,"RMraster can not load an unknown type of raster"); } break; } // (*(this->rasters))[id]->group=this->group; // pridam (mozna) do nove vyssi skupiny } this->writeGroup(id); } this->unlock(); } void TRM_raster::delRaster(int id) { this->lock(); this->index->delData(0,"raster",id,NULL); if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getRasterFile())!=XML_ERROR_OK) // zapisu index do souboru { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMraster can not write index file"); THROW(E_8K_RM,"RMraster can not write index file"); } if ((*(this->rasters))[id]!=NULL) // odstanim i z pameti { if ((*(this->rasters))[id]->surface!=NULL) SDL_FreeSurface((*(this->rasters))[id]->surface); KMemFree((*(this->rasters))[id]); } this->unlock(); } int TRM_raster::addRaster(SDL_Surface *raster,int newid,char *filename,int type) { this->lock(); int maxindex; if (newid==-1) { // vezmu prvni volny - tj. max (id_ze_souboru,id_v_pameti) pro pripad, ze uz je neco ulozeno maxindex=this->getMaxId(); maxindex++; int i; for (i=1;iindex->getData(NULL,0,"raster",i,"type",0,NULL)==XML_ERROR_NO_SUCH_A_PATH ) && ((*(this->rasters))[i]==NULL) )// aby nebylo nic v souboru ani nacteno - nic se neprepsalo { break; } } maxindex=i; // vezmu prvni volnou hodnotu } else { maxindex=newid; } if (maxindex<1) { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMraster can not add a raster having id %i < 1",maxindex); THROW(E_8K_RM,"RMraster can not add a raster having id less than 1"); } if ((*(this->rasters))[maxindex]==NULL) { (*(this->rasters))[maxindex]=(struct TRaster*)KMemAlloc(sizeof (struct TRaster)); (*(this->rasters))[maxindex]->szFilename[0]=0; (*(this->rasters))[maxindex]->surface=NULL; } if (filename!=NULL) // pridavam neco natrvalo { strcopy((*(this->rasters))[maxindex]->szFilename,filename,K8_MAX_FILE_NAME_LEN); this->index->setData(filename,"raster",maxindex,"file",0,NULL); switch (type) { case RASTER_BMP_CODE: { this->index->setData(RASTER_BMP_NAME,"raster",maxindex,"type",0,NULL); } break; case RASTER_TGA_CODE: { this->index->setData(RASTER_TGA_NAME,"raster",maxindex,"type",0,NULL); } break; default: { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMraster can not add a raster with unknown type %i",maxindex); THROW(E_8K_RM,"RMraster can not add a raster with unknown type"); } break; } } if (this->saveToFile(this->rmconfi->getPack(),this->rmconfi->getRasterFile())!=XML_ERROR_OK) // zapisu index do souboru { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMraster can not write index file"); THROW(E_8K_RM,"RMraster can not write index file"); } // (*(this->rasters))[maxindex]->group=this->group; (*(this->rasters))[maxindex]->surface=raster; this->unlock(); return maxindex; } TRaster * TRM_raster::getRaster(int id) { this->lock(); if ((*(this->rasters))[id]==NULL) { this->createRecord(id); } this->unlock(); return ((*(this->rasters))[id]); } int TRM_raster::getMaxId() { this->lock(); int maxindex; if (this->rasters->getMaxId()>this->index->getMaxId()) maxindex=this->rasters->getMaxId(); else maxindex=this->index->getMaxId(); this->unlock(); return maxindex; } void TRM_raster::freeData(int id) { this->freeRaster(id,1); } void TRM_raster::freeRaster(int id,int total) { this->lock(); if ((*(this->rasters))[id]!=NULL) { SDL_FreeSurface((*(this->rasters))[id]->surface); if (total==1) { KMemFree((*(this->rasters))[id]); (*(this->rasters))[id]=NULL; } } else { this->unlock(); GLOBALLOGID(PRIORITY_FATAL, "RMraster can not release the raster %i which has not been loaded yet",id); THROW(E_8K_RM,"RMraster can not release the raster, which has not been loaded yet"); } this->unlock(); } int TRM_raster::getRasters(DA ** rsts) { this->lock(); *rsts=this->rasters; this->unlock(); return this->rasters->getMaxId(); } int TRM_raster::getRastersCount() { this->lock(); int num=this->index->getMaxId(); this->unlock(); return num; } int TRM_raster::getRastersLoaded() { this->lock(); int num=0; int j; for (j=this->rasters->getNext(-1);j!=-1;j=this->rasters->getNext(j)) { num++; } this->unlock(); return num; } TRM_raster::TRM_raster(TRM_conf_i * rmc) // konstruktor nema smyl zamykat... { this->rmconfi=rmc; this->group=0; this->rasters=new DA; int retvalue; try { // lze pouzit bu_path v konstruktoru strpartpath(bu_path,RESOURCES_DIR,this->rmconfi->getPack(),XML_DIR,this->rmconfi->getRasterFile(),K8_MAX_PATH_LEN,".xml"); } catch (E_8K_RM) { GLOBALLOGID(PRIORITY_FATAL, "files.xml is corrupted"); THROW(E_8K_RM,"files.xml is corrupted"); } retvalue=this->readInfoFile(bu_path); // do RMtxt nactu informace kde ma hledat vsechny raster if (retvalue!=XML_ERROR_OK) // chyba pri cteni fajlu o jazycich { GLOBALLOGID(PRIORITY_MISSING_FILE,"RMraster the file %s is corrupted",bu_path); THROW(E_8K_RM,"RMraster the index file is corrupted"); } GLOBALLOGID(PRIORITY_CONFIRM ,"RMraster was created"); } TRM_raster::~TRM_raster() { delete this->rmconfi; int j; for (j=this->rasters->getNext(-1);j!=-1;j=this->rasters->getNext(j)) { if ((*(this->rasters))[j]->surface!=NULL) SDL_FreeSurface((*(this->rasters))[j]->surface); KMemFree((*(this->rasters))[j]); } delete this->rasters; GLOBALLOGID(PRIORITY_CONFIRM ,"RMraster was destroyed"); } }