2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined ( INCLUDED_ECLASSLIB_H )
23 #define INCLUDED_ECLASSLIB_H
34 #include "math/vector.h"
35 #include "string/string.h"
37 typedef Vector3 Colour3;
39 class ListAttributeType
41 using ListItem = std::pair<CopiedString, CopiedString>;
42 using ListItems = std::vector<ListItem>;
46 typedef ListItems::const_iterator const_iterator;
47 const_iterator begin() const {
48 return m_items.begin();
50 const_iterator end() const {
54 const ListItem& operator[]( std::size_t i ) const {
57 const_iterator findValue( const char* value ) const {
58 for ( ListItems::const_iterator i = m_items.begin(); i != m_items.end(); ++i )
60 if ( string_equal( value, ( *i ).second.c_str() ) ) {
67 void push_back( const char* name, const char* value ){
68 m_items.push_back( ListItems::value_type( name, value ) );
72 class EntityClassAttribute
78 CopiedString m_description;
79 EntityClassAttribute(){
81 EntityClassAttribute(const EntityClassAttribute& attr) = default;
82 EntityClassAttribute( const char* type, const char* name, const char* value = "", const char* description = "" ) : m_type( type ), m_name( name ), m_value( value ), m_description( description ){
84 EntityClassAttribute& operator=(const EntityClassAttribute& attr) = default;
87 typedef std::pair<CopiedString, EntityClassAttribute> EntityClassAttributePair;
88 typedef std::list<EntityClassAttributePair> EntityClassAttributes;
89 typedef std::list<CopiedString> StringList;
91 inline const char* EntityClassAttributePair_getName( const EntityClassAttributePair& attributePair ){
92 if ( !string_empty( attributePair.second.m_name.c_str() ) ) {
93 return attributePair.second.m_name.c_str();
95 return attributePair.first.c_str();
98 inline const char* EntityClassAttributePair_getDescription( const EntityClassAttributePair& attributePair ){
99 if ( !string_empty( attributePair.second.m_description.c_str() ) ) {
100 return attributePair.second.m_description.c_str();
102 return EntityClassAttributePair_getName( attributePair );
111 bool unknown; // wasn't found in source
116 Shader* m_state_fill;
117 Shader* m_state_wire;
118 Shader* m_state_blend;
120 CopiedString m_comments;
121 char flagnames[MAX_FLAGS][32];
123 CopiedString m_modelpath;
126 void ( *free )( EntityClass* );
128 EntityClassAttributes m_attributes;
130 bool inheritanceResolved;
134 const char* name() const {
135 return m_name.c_str();
137 const char* comments() const {
138 return m_comments.c_str();
140 const char* modelpath() const {
141 return m_modelpath.c_str();
143 const char* skin() const {
144 return m_skin.c_str();
148 inline const char* EntityClass_valueForKey( const EntityClass& entityClass, const char* key ){
149 for ( EntityClassAttributes::const_iterator i = entityClass.m_attributes.begin(); i != entityClass.m_attributes.end(); ++i )
151 if ( string_equal( key, ( *i ).first.c_str() ) ) {
152 return ( *i ).second.m_value.c_str();
158 inline EntityClassAttributePair& EntityClass_insertAttribute( EntityClass& entityClass, const char* key, const EntityClassAttribute& attribute = EntityClassAttribute() ){
159 entityClass.m_attributes.push_back( EntityClassAttributePair( key, attribute ) );
160 return entityClass.m_attributes.back();
164 inline void buffer_write_colour_fill( char buffer[128], const Colour3& colour ){
165 sprintf( buffer, "(%g %g %g)", colour[0], colour[1], colour[2] );
168 inline void buffer_write_colour_wire( char buffer[128], const Colour3& colour ){
169 sprintf( buffer, "<%g %g %g>", colour[0], colour[1], colour[2] );
172 inline void buffer_write_colour_blend( char buffer[128], const Colour3& colour ){
173 sprintf( buffer, "[%g %g %g]", colour[0], colour[1], colour[2] );
176 inline Shader* colour_capture_state_fill( const Colour3& colour ){
178 buffer_write_colour_fill( buffer, colour );
179 return GlobalShaderCache().capture( buffer );
182 inline void colour_release_state_fill( const Colour3& colour ){
184 buffer_write_colour_fill( buffer, colour );
185 GlobalShaderCache().release( buffer );
188 inline Shader* colour_capture_state_wire( const Colour3& colour ){
190 buffer_write_colour_wire( buffer, colour );
191 return GlobalShaderCache().capture( buffer );
194 inline void colour_release_state_wire( const Colour3& colour ){
196 buffer_write_colour_wire( buffer, colour );
197 GlobalShaderCache().release( buffer );
200 inline Shader* colour_capture_state_blend( const Colour3& colour ){
202 buffer_write_colour_blend( buffer, colour );
203 return GlobalShaderCache().capture( buffer );
206 inline void colour_release_state_blend( const Colour3& colour ){
208 buffer_write_colour_blend( buffer, colour );
209 GlobalShaderCache().release( buffer );
212 inline void eclass_capture_state( EntityClass* eclass ){
213 eclass->m_state_fill = colour_capture_state_fill( eclass->color );
214 eclass->m_state_wire = colour_capture_state_wire( eclass->color );
215 eclass->m_state_blend = colour_capture_state_blend( eclass->color );
218 inline void eclass_release_state( EntityClass* eclass ){
219 colour_release_state_fill( eclass->color );
220 colour_release_state_wire( eclass->color );
221 colour_release_state_blend( eclass->color );
224 // eclass constructor
225 inline EntityClass* Eclass_Alloc(){
226 EntityClass* e = new EntityClass;
228 e->fixedsize = false;
230 memset( e->flagnames, 0, MAX_FLAGS * 32 );
232 e->maxs = Vector3( -1,-1,-1 );
233 e->mins = Vector3( 1, 1, 1 );
237 e->inheritanceResolved = true;
238 e->sizeSpecified = false;
239 e->colorSpecified = false;
245 inline void Eclass_Free( EntityClass* e ){
246 eclass_release_state( e );
251 inline bool classname_equal( const char* classname, const char* other ){
252 return string_equal( classname, other );
255 inline EntityClass* EClass_Create( const char* name, const Vector3& colour, const char* comments ){
256 EntityClass *e = Eclass_Alloc();
257 e->free = &Eclass_Free;
262 eclass_capture_state( e );
265 e->m_comments = comments;
271 inline EntityClass* EClass_Create_FixedSize( const char* name, const Vector3& colour, const Vector3& mins, const Vector3& maxs, const char* comments ){
272 EntityClass *e = Eclass_Alloc();
273 e->free = &Eclass_Free;
278 eclass_capture_state( e );
286 e->m_comments = comments;
292 const Vector3 smallbox[2] = {
297 inline EntityClass *EntityClass_Create_Default( const char *name, bool has_brushes ){
298 // create a new class for it
300 return EClass_Create( name, Vector3( 0.0f, 0.5f, 0.0f ), "Not found in source." );
304 return EClass_Create_FixedSize( name, Vector3( 0.0f, 0.5f, 0.0f ), smallbox[0], smallbox[1], "Not found in source." );