Back to index

supertuxkart  0.5+dfsg1
collectable_manager.cpp
Go to the documentation of this file.
00001 //  $Id: collectable_manager.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program 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 this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <iostream>
00021 #include <stdexcept>
00022 #include "collectable_manager.hpp"
00023 #include "file_manager.hpp"
00024 #include "material_manager.hpp"
00025 #include "material.hpp"
00026 #include "translation.hpp"
00027 #include "spark.hpp"
00028 #include "missile.hpp"
00029 #include "homing.hpp"
00030 #include "loader.hpp"
00031 
00032 #if defined(WIN32) && !defined(__CYGWIN__)
00033 #  define snprintf _snprintf
00034 #endif
00035 
00036 typedef struct
00037 {
00038     CollectableType collectable;
00039     const char*const dataFile;
00040 }
00041 initCollectableType;
00042 
00043 initCollectableType ict[]=
00044 {
00045     {COLLECT_ZIPPER,    "zipper.collectable"       },
00046     {COLLECT_SPARK,     "spark.projectile"         },
00047     {COLLECT_MISSILE,   "missile.projectile"       },
00048     {COLLECT_HOMING,    "homingmissile.projectile" },
00049     {COLLECT_ANVIL,     "anvil.collectable"        },
00050     {COLLECT_PARACHUTE, "parachute.collectable"    },
00051     {COLLECT_MAX,       ""                         },
00052 };
00053 
00054 CollectableManager* collectable_manager=0;
00055 
00056 //-----------------------------------------------------------------------------
00057 CollectableManager::CollectableManager()
00058 {
00059     for(int i=0; i<COLLECT_MAX; i++)
00060     {
00061         m_all_models[i] = (ssgEntity*)NULL;
00062         m_all_icons[i]  = (Material*)NULL;
00063     }
00064 }   // CollectableManager
00065 
00066 //-----------------------------------------------------------------------------
00067 void CollectableManager::removeTextures()
00068 {
00069     for(int i=0; i<COLLECT_MAX; i++)
00070     {
00071         if(m_all_icons [i]) ssgDeRefDelete(m_all_icons [i]->getState());
00072         if(m_all_models[i]) ssgDeRefDelete(m_all_models[i]            );
00073     }   // for
00074     callback_manager->clear(CB_COLLECTABLE);
00075 
00076 }   // removeTextures
00077 
00078 //-----------------------------------------------------------------------------
00079 void CollectableManager::loadCollectables()
00080 {
00081     for(int i=0; ict[i].collectable != COLLECT_MAX; i++)
00082     {
00083         Load(ict[i].collectable, ict[i].dataFile);
00084     }
00085 }  // loadCollectables
00086 
00087 //-----------------------------------------------------------------------------
00088 void CollectableManager::Load(int collectType, const char* filename)
00089 {
00090     const lisp::Lisp* ROOT = 0;
00091 
00092     lisp::Parser parser;
00093     std::string tmp= "data/" + (std::string)filename;
00094     ROOT = parser.parse(file_manager->getConfigFile(filename));
00095         
00096     const lisp::Lisp* lisp = ROOT->getLisp("tuxkart-collectable");
00097     if(!lisp)
00098     {
00099         char msg[MAX_ERROR_MESSAGE_LENGTH];
00100         snprintf(msg, sizeof(msg), 
00101                  "No 'tuxkart-collectable' node found while parsing '%s'.",
00102                  filename);
00103         throw std::runtime_error(msg);
00104     }
00105     LoadNode(lisp, collectType);
00106 
00107     delete ROOT;
00108 
00109 }   // Load
00110 
00111 //-----------------------------------------------------------------------------
00112 void CollectableManager::LoadNode(const lisp::Lisp* lisp, int collectType )
00113 {
00114     std::string sName, sModel, sIconFile; 
00115     lisp->get("name",            sName                              );
00116     lisp->get("model",           sModel                             );
00117     lisp->get("icon",            sIconFile                          );
00118  
00119     // load material
00120     m_all_icons[collectType] = material_manager->getMaterial(sIconFile);
00121     m_all_icons[collectType]->getState()->ref();
00122 
00123     //FIXME: something probably forgets to disable GL_CULL_FACE after enabling it,
00124     //this is just a quick fix.
00125     if(collectType == COLLECT_SPARK) m_all_icons[COLLECT_SPARK]->getState()->disable ( GL_CULL_FACE ) ;
00126 
00127     if(sModel!="")
00128     {
00129         ssgEntity* e = loader->load(sModel, CB_COLLECTABLE);
00130         m_all_models[collectType] = e;
00131         e->ref();
00132         e->clrTraversalMaskBits(SSGTRAV_ISECT|SSGTRAV_HOT);
00133     }
00134     else
00135     {
00136         m_all_models[collectType] = 0;
00137         m_all_extends[collectType] = btVector3(0.0f,0.0f,0.0f);
00138     }
00139 
00140     // Load special attributes for certain collectables
00141     switch (collectType) {
00142         case COLLECT_SPARK:          
00143              Spark::init  (lisp, m_all_models[collectType]); break;
00144         case COLLECT_MISSILE:        
00145              Missile::init(lisp, m_all_models[collectType]); break;
00146         case COLLECT_HOMING: 
00147              Homing::init (lisp, m_all_models[collectType]); break;
00148         default:;
00149     }   // switch
00150 
00151 }   // LoadNode
00152