eclasslib.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 1999-2006 Id Software, Inc. and contributors.
00003  For a list of contributors, see the accompanying CONTRIBUTORS file.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined (INCLUDED_ECLASSLIB_H)
00023 #define INCLUDED_ECLASSLIB_H
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <list>
00028 #include <map>
00029 #include <vector>
00030 
00031 #include "ieclass.h"
00032 #include "irender.h"
00033 
00034 #include "string/string.h"
00035 
00036 typedef Vector3 Colour3;
00037 
00038 class ListAttributeType
00039 {
00040         typedef std::pair<std::string, std::string> ListItem;
00041         typedef std::vector<ListItem> ListItems;
00042         ListItems m_items;
00043     public:
00044 
00045         typedef ListItems::const_iterator const_iterator;
00046         const_iterator begin () const
00047         {
00048             return m_items.begin();
00049         }
00050         const_iterator end () const
00051         {
00052             return m_items.end();
00053         }
00054 
00055         const ListItem& operator[] (std::size_t i) const
00056         {
00057             return m_items[i];
00058         }
00059         const_iterator findValue (const std::string& value) const
00060         {
00061             for (ListItems::const_iterator i = m_items.begin(); i != m_items.end(); ++i) {
00062                 if (value == i->second)
00063                     return i;
00064             }
00065             return m_items.end();
00066         }
00067 
00068         void push_back (const std::string& name, const std::string& value)
00069         {
00070             m_items.push_back(ListItems::value_type(name, value));
00071         }
00072 };
00073 
00074 class EntityClassAttribute
00075 {
00076     public:
00077         std::string m_type; 
00078         std::string m_name; 
00079         std::string m_value; 
00080         std::string m_description; 
00081         bool m_mandatory; 
00082         EntityClassAttribute ()
00083         {
00084         }
00085         EntityClassAttribute (const char* type, const char* name, bool mandatory = false, const char* value = "",
00086                 const char* description = "") :
00087             m_type(type), m_name(name), m_value(value), m_description(description), m_mandatory(mandatory)
00088         {
00089         }
00090 };
00091 
00092 typedef std::pair<std::string, EntityClassAttribute> EntityClassAttributePair;
00093 typedef std::list<EntityClassAttributePair> EntityClassAttributes;
00094 typedef std::list<std::string> StringList;
00095 
00096 inline const char* EntityClassAttributePair_getName (const EntityClassAttributePair& attributePair)
00097 {
00098     if (!attributePair.second.m_name.empty()) {
00099         return attributePair.second.m_name.c_str();
00100     }
00101     return attributePair.first.c_str();
00102 }
00103 
00104 inline const char* EntityClassAttributePair_getDescription (const EntityClassAttributePair& attributePair)
00105 {
00106     if (!attributePair.second.m_description.empty()) {
00107         return attributePair.second.m_description.c_str();
00108     }
00109     return EntityClassAttributePair_getName(attributePair);
00110 }
00111 
00112 class EntityClass
00113 {
00114     public:
00115         std::string m_name;
00116         StringList m_parent;
00117         bool fixedsize;
00118         Vector3 mins;
00119         Vector3 maxs;
00120 
00121         Colour3 color;
00122         Shader* m_state_fill;
00123         Shader* m_state_wire;
00124         Shader* m_state_blend;
00125 
00126         std::string m_comments;
00127         char flagnames[MAX_FLAGS][32];
00128 
00129         std::string m_modelpath; 
00130         std::string m_skin;
00131 
00132         void (*free) (EntityClass*);
00133 
00134         EntityClassAttributes m_attributes;
00135 
00136         const char* name () const
00137         {
00138             return m_name.c_str();
00139         }
00140         const char* comments () const
00141         {
00142             return m_comments.c_str();
00143         }
00144         const std::string& modelpath () const
00145         {
00146             return m_modelpath;
00147         }
00148         const char* skin () const
00149         {
00150             return m_skin.c_str();
00151         }
00152 
00158         EntityClassAttribute *getAttribute (const std::string& attributeName) const
00159         {
00160             for (EntityClassAttributes::const_iterator i = m_attributes.begin(); i != m_attributes.end(); ++i) {
00161                 if (attributeName == i->first) {
00162                     return const_cast<EntityClassAttribute*> (&(*i).second);
00163                 }
00164             }
00165             return NULL;
00166         }
00167 
00173         const std::string getDefaultForAttribute (const std::string& attributeName) const
00174         {
00175             EntityClassAttribute *attrib = getAttribute(attributeName);
00176             //use value if it is set to something
00177             if (attrib && attrib->m_value.length() > 0)
00178                 return attrib->m_value;
00179             // TODO retrieve some default value from entity definition instead of that empty value
00180             return "";
00181         }
00182 };
00183 
00184 inline const char* EntityClass_valueForKey (const EntityClass& entityClass, const std::string& key)
00185 {
00186     for (EntityClassAttributes::const_iterator i = entityClass.m_attributes.begin(); i
00187             != entityClass.m_attributes.end(); ++i) {
00188         if (key == (*i).first) {
00189             return (*i).second.m_value.c_str();
00190         }
00191     }
00192     return "";
00193 }
00194 
00195 inline EntityClassAttributePair& EntityClass_insertAttribute (EntityClass& entityClass, const char* key,
00196         const EntityClassAttribute& attribute = EntityClassAttribute())
00197 {
00198     entityClass.m_attributes.push_back(EntityClassAttributePair(key, attribute));
00199     return entityClass.m_attributes.back();
00200 }
00201 
00202 inline void buffer_write_colour_fill (char buffer[128], const Colour3& colour)
00203 {
00204     sprintf(buffer, "(%g %g %g)", colour[0], colour[1], colour[2]);
00205 }
00206 
00207 inline void buffer_write_colour_wire (char buffer[128], const Colour3& colour)
00208 {
00209     sprintf(buffer, "<%g %g %g>", colour[0], colour[1], colour[2]);
00210 }
00211 
00212 inline void buffer_write_colour_blend (char buffer[128], const Colour3& colour)
00213 {
00214     sprintf(buffer, "[%g %g %g]", colour[0], colour[1], colour[2]);
00215 }
00216 
00217 inline Shader* colour_capture_state_fill (const Colour3& colour)
00218 {
00219     char buffer[128];
00220     buffer_write_colour_fill(buffer, colour);
00221     return GlobalShaderCache().capture(buffer);
00222 }
00223 
00224 inline void colour_release_state_fill (const Colour3& colour)
00225 {
00226     char buffer[128];
00227     buffer_write_colour_fill(buffer, colour);
00228     GlobalShaderCache().release(buffer);
00229 }
00230 
00231 inline Shader* colour_capture_state_wire (const Colour3& colour)
00232 {
00233     char buffer[128];
00234     buffer_write_colour_wire(buffer, colour);
00235     return GlobalShaderCache().capture(buffer);
00236 }
00237 
00238 inline void colour_release_state_wire (const Colour3& colour)
00239 {
00240     char buffer[128];
00241     buffer_write_colour_wire(buffer, colour);
00242     GlobalShaderCache().release(buffer);
00243 }
00244 
00245 inline Shader* colour_capture_state_blend (const Colour3& colour)
00246 {
00247     char buffer[128];
00248     buffer_write_colour_blend(buffer, colour);
00249     return GlobalShaderCache().capture(buffer);
00250 }
00251 
00252 inline void colour_release_state_blend (const Colour3& colour)
00253 {
00254     char buffer[128];
00255     buffer_write_colour_blend(buffer, colour);
00256     GlobalShaderCache().release(buffer);
00257 }
00258 
00259 inline void eclass_capture_state (EntityClass* eclass)
00260 {
00261     eclass->m_state_fill = colour_capture_state_fill(eclass->color);
00262     eclass->m_state_wire = colour_capture_state_wire(eclass->color);
00263     eclass->m_state_blend = colour_capture_state_blend(eclass->color);
00264 }
00265 
00266 inline void eclass_release_state (EntityClass* eclass)
00267 {
00268     colour_release_state_fill(eclass->color);
00269     colour_release_state_wire(eclass->color);
00270     colour_release_state_blend(eclass->color);
00271 }
00272 
00273 // eclass constructor
00274 inline EntityClass* Eclass_Alloc ()
00275 {
00276     EntityClass* e = new EntityClass;
00277 
00278     e->fixedsize = false;
00279     memset(e->flagnames, 0, MAX_FLAGS * 32);
00280 
00281     e->maxs = Vector3(-1, -1, -1);
00282     e->mins = Vector3(1, 1, 1);
00283     e->color = Vector3(1, 1, 1);
00284 
00285     e->free = 0;
00286 
00287     return e;
00288 }
00289 
00290 // eclass destructor
00291 inline void Eclass_Free (EntityClass* e)
00292 {
00293     eclass_release_state(e);
00294 
00295     delete e;
00296 }
00297 
00298 inline bool classname_equal (const std::string& classname, const std::string& other)
00299 {
00300     return string_equal(classname, other);
00301 }
00302 
00303 inline EntityClass* EClass_Create (const std::string& name, const Vector3& colour, const std::string& comments)
00304 {
00305     EntityClass *e = Eclass_Alloc();
00306     e->free = &Eclass_Free;
00307 
00308     e->m_name = name;
00309 
00310     e->color = colour;
00311     eclass_capture_state(e);
00312 
00313     e->m_comments = comments;
00314 
00315     return e;
00316 }
00317 
00318 inline EntityClass* EClass_Create_FixedSize (const std::string& name, const Vector3& colour, const Vector3& mins,
00319         const Vector3& maxs, const std::string& comments)
00320 {
00321     EntityClass *e = Eclass_Alloc();
00322     e->free = &Eclass_Free;
00323 
00324     e->m_name = name;
00325 
00326     e->color = colour;
00327     eclass_capture_state(e);
00328 
00329     e->fixedsize = true;
00330 
00331     e->mins = mins;
00332     e->maxs = maxs;
00333 
00334     e->m_comments = comments;
00335 
00336     return e;
00337 }
00338 
00339 const Vector3 smallbox[2] = { Vector3(-8, -8, -8), Vector3(8, 8, 8), };
00340 
00341 inline EntityClass *EntityClass_Create_Default (const std::string& name, bool has_brushes)
00342 {
00343     // create a new class for it
00344     if (has_brushes) {
00345         return EClass_Create(name, Vector3(0.0f, 0.5f, 0.0f), "Not found in source.");
00346     } else {
00347         return EClass_Create_FixedSize(name, Vector3(0.0f, 0.5f, 0.0f), smallbox[0], smallbox[1],
00348                 "Not found in source.");
00349     }
00350 }
00351 
00352 #endif

Generated by  doxygen 1.6.2