]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/eclasslib.h
various: add explicit default contructors
[xonotic/netradiant.git] / libs / eclasslib.h
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #if !defined ( INCLUDED_ECLASSLIB_H )
23 #define INCLUDED_ECLASSLIB_H
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <list>
28 #include <map>
29 #include <vector>
30
31 #include "ieclass.h"
32 #include "irender.h"
33
34 #include "math/vector.h"
35 #include "string/string.h"
36
37 typedef Vector3 Colour3;
38
39 class ListAttributeType
40 {
41         using ListItem = std::pair<CopiedString, CopiedString>;
42         using ListItems = std::vector<ListItem>;
43         ListItems m_items;
44 public:
45
46         typedef ListItems::const_iterator const_iterator;
47         const_iterator begin() const {
48                 return m_items.begin();
49         }
50         const_iterator end() const {
51                 return m_items.end();
52         }
53
54         const ListItem& operator[]( std::size_t i ) const {
55                 return m_items[i];
56         }
57         const_iterator findValue( const char* value ) const {
58                 for ( ListItems::const_iterator i = m_items.begin(); i != m_items.end(); ++i )
59                 {
60                         if ( string_equal( value, ( *i ).second.c_str() ) ) {
61                                 return i;
62                         }
63                 }
64                 return m_items.end();
65         }
66
67         void push_back( const char* name, const char* value ){
68                 m_items.push_back( ListItems::value_type( name, value ) );
69         }
70 };
71
72 class EntityClassAttribute
73 {
74         public:
75                 CopiedString m_type;
76                 CopiedString m_name;
77                 CopiedString m_value;
78                 CopiedString m_description;
79                 EntityClassAttribute(){
80                 }
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 ){
83                 }
84                 EntityClassAttribute& operator=(const EntityClassAttribute& attr) = default;
85 };
86
87 typedef std::pair<CopiedString, EntityClassAttribute> EntityClassAttributePair;
88 typedef std::list<EntityClassAttributePair> EntityClassAttributes;
89 typedef std::list<CopiedString> StringList;
90
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();
94         }
95         return attributePair.first.c_str();
96 }
97
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();
101         }
102         return EntityClassAttributePair_getName( attributePair );
103 }
104
105 class EntityClass
106 {
107 public:
108         CopiedString m_name;
109         StringList m_parent;
110         bool fixedsize;
111         bool unknown;               // wasn't found in source
112         Vector3 mins;
113         Vector3 maxs;
114
115         Colour3 color;
116         Shader* m_state_fill;
117         Shader* m_state_wire;
118         Shader* m_state_blend;
119
120         CopiedString m_comments;
121         char flagnames[MAX_FLAGS][32];
122
123         CopiedString m_modelpath;
124         CopiedString m_skin;
125
126         void ( *free )( EntityClass* );
127
128         EntityClassAttributes m_attributes;
129
130         bool inheritanceResolved;
131         bool sizeSpecified;
132         bool colorSpecified;
133
134         const char* name() const {
135                 return m_name.c_str();
136         }
137         const char* comments() const {
138                 return m_comments.c_str();
139         }
140         const char* modelpath() const {
141                 return m_modelpath.c_str();
142         }
143         const char* skin() const {
144                 return m_skin.c_str();
145         }
146 };
147
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 )
150         {
151                 if ( string_equal( key, ( *i ).first.c_str() ) ) {
152                         return ( *i ).second.m_value.c_str();
153                 }
154         }
155         return "";
156 }
157
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();
161 }
162
163
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] );
166 }
167
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] );
170 }
171
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] );
174 }
175
176 inline Shader* colour_capture_state_fill( const Colour3& colour ){
177         char buffer[128];
178         buffer_write_colour_fill( buffer, colour );
179         return GlobalShaderCache().capture( buffer );
180 }
181
182 inline void colour_release_state_fill( const Colour3& colour ){
183         char buffer[128];
184         buffer_write_colour_fill( buffer, colour );
185         GlobalShaderCache().release( buffer );
186 }
187
188 inline Shader* colour_capture_state_wire( const Colour3& colour ){
189         char buffer[128];
190         buffer_write_colour_wire( buffer, colour );
191         return GlobalShaderCache().capture( buffer );
192 }
193
194 inline void colour_release_state_wire( const Colour3& colour ){
195         char buffer[128];
196         buffer_write_colour_wire( buffer, colour );
197         GlobalShaderCache().release( buffer );
198 }
199
200 inline Shader* colour_capture_state_blend( const Colour3& colour ){
201         char buffer[128];
202         buffer_write_colour_blend( buffer, colour );
203         return GlobalShaderCache().capture( buffer );
204 }
205
206 inline void colour_release_state_blend( const Colour3& colour ){
207         char buffer[128];
208         buffer_write_colour_blend( buffer, colour );
209         GlobalShaderCache().release( buffer );
210 }
211
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 );
216 }
217
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 );
222 }
223
224 // eclass constructor
225 inline EntityClass* Eclass_Alloc(){
226         EntityClass* e = new EntityClass;
227
228         e->fixedsize = false;
229         e->unknown = false;
230         memset( e->flagnames, 0, MAX_FLAGS * 32 );
231
232         e->maxs = Vector3( -1,-1,-1 );
233         e->mins = Vector3( 1, 1, 1 );
234
235         e->free = 0;
236
237         e->inheritanceResolved = true;
238         e->sizeSpecified = false;
239         e->colorSpecified = false;
240
241         return e;
242 }
243
244 // eclass destructor
245 inline void Eclass_Free( EntityClass* e ){
246         eclass_release_state( e );
247
248         delete e;
249 }
250
251 inline bool classname_equal( const char* classname, const char* other ){
252         return string_equal( classname, other );
253 }
254
255 inline EntityClass* EClass_Create( const char* name, const Vector3& colour, const char* comments ){
256         EntityClass *e = Eclass_Alloc();
257         e->free = &Eclass_Free;
258
259         e->m_name = name;
260
261         e->color = colour;
262         eclass_capture_state( e );
263
264         if ( comments ) {
265                 e->m_comments = comments;
266         }
267
268         return e;
269 }
270
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;
274
275         e->m_name = name;
276
277         e->color = colour;
278         eclass_capture_state( e );
279
280         e->fixedsize = true;
281
282         e->mins = mins;
283         e->maxs = maxs;
284
285         if ( comments ) {
286                 e->m_comments = comments;
287         }
288
289         return e;
290 }
291
292 const Vector3 smallbox[2] = {
293         Vector3( -8,-8,-8 ),
294         Vector3( 8, 8, 8 ),
295 };
296
297 inline EntityClass *EntityClass_Create_Default( const char *name, bool has_brushes ){
298         // create a new class for it
299         if ( has_brushes ) {
300                 return EClass_Create( name, Vector3( 0.0f, 0.5f, 0.0f ), "Not found in source." );
301         }
302         else
303         {
304                 return EClass_Create_FixedSize( name, Vector3( 0.0f, 0.5f, 0.0f ), smallbox[0], smallbox[1], "Not found in source." );
305         }
306 }
307
308 #endif