changed copyright years in source files
[fegdk.git] / core / code / system / entclasses.cpp
blobd97ed4c437a482dcfe286cb077178c4f5b63fd3c
1 #include "pch.h"
2 #include "entclasses.h"
3 #include "f_engine.h"
4 #include "entitymanager.h"
5 #include "f_resourcemgr.h"
6 #include "f_material.h"
8 namespace fe
11 /**
12 * editorname: worldspawn
15 entWorld::~entWorld (void)
19 void entWorld::init (void)
23 struct vertex_t
25 float x, y, z;
26 float u, v;
29 struct polygon_t
31 int mtl;
32 std::vector <vertex_t> verts;
33 vector3 normal;
36 struct cmpPolygons : public std::binary_function<polygon_t, polygon_t, bool>
38 bool operator() (const polygon_t &a, const polygon_t &b)
40 return (a.mtl < b.mtl);
44 void entWorld::load (const char *fname)
46 std::vector <cStr> mtlNameList;
47 std::vector <materialPtr> mtlList;
48 std::vector <polygon_t> polygons;
50 try
52 charParser p (g_engine->getFileSystem (), fname);
53 for (;;)
55 p.getToken ();
56 if (!p.cmptoken ("polygons"))
58 p.matchToken ("{");
59 for (;;)
61 p.getToken ();
62 if (!p.cmptoken ("}"))
63 break;
64 else if (!p.cmptoken ("{"))
66 // load polygon
67 polygon_t poly;
68 p.getToken ();
70 std::vector <cStr>::iterator m = std::find (mtlNameList.begin (), mtlNameList.end (), p.token ());
71 if (m == mtlNameList.end ())
73 mtlNameList.push_back (p.token ());
74 poly.mtl = mtlNameList.size ();
76 else
78 poly.mtl = m - mtlNameList.begin ();
81 p.getToken ();
82 int nverts = atoi (p.token ());
84 for (int v = 0; v < nverts; v++)
86 vertex_t vx;
87 p.getToken ();
88 vx.x = atof (p.token ());
89 p.getToken ();
90 vx.y = atof (p.token ());
91 p.getToken ();
92 vx.z = atof (p.token ());
93 p.getToken ();
94 vx.u = atof (p.token ());
95 p.getToken ();
96 vx.v = atof (p.token ());
97 poly.verts.push_back (vx);
100 p.matchToken ("}");
101 // calc normal
102 vector3 a = (vector3&)(poly.verts[0].x) - (vector3&)(poly.verts[1].x);
103 vector3 b = (vector3&)(poly.verts[2].x) - (vector3&)(poly.verts[1].x);
104 poly.normal = a.cross (b);
105 poly.normal.normalize ();
106 polygons.push_back (poly);
108 else
109 p.generalSyntaxError ();
112 else if (!p.cmptoken ("entity"))
114 p.matchToken ("{");
115 for (;;)
117 std::map <cStr, cStr> parms;
118 p.getToken ();
119 if (!p.cmptoken ("}"))
121 std::map <cStr, cStr>::iterator it = parms.find ("classname");
122 if (it == parms.end ())
124 fprintf (stderr, "WARNING: entity without classname in mapfile\n");
125 break;
127 uint32 id = g_engine->getEntManager ()->getEClassForClassName ((*it).second);
128 entityPtr ent = g_engine->getEntManager ()->createEntity (id);
129 // ent->setParms (parms);
130 mEnts.push_back (ent);
131 break;
133 else
135 p.getToken ();
136 cStr key = p.token ();
137 p.getToken ();
138 cStr value = p.token ();
139 parms[key] = value;
143 else
144 p.generalSyntaxError ();
147 catch (parserException e)
151 // sort polygons
152 sort (polygons.begin (), polygons.end (), cmpPolygons());
154 // load materials
155 size_t sz = mtlNameList.size ();
156 for (size_t m = 0; m < sz; m++)
158 mtlList.push_back (g_engine->getResourceMgr ()->createMaterial (mtlNameList[m]));
161 // create vertex&index buffers, and create model
162 std::vector <drawVertex_t> verts;
163 std::vector <ushort> inds;
164 std::vector <modelSubset_t> subs;
166 sz = polygons.size ();
167 int mtl = -1;
168 modelSubset_t *sub = NULL;
169 for (size_t pp = 0; pp < sz; pp++)
171 polygon_t &p = polygons[pp];
172 if (p.mtl != mtl)
174 // create new subset
175 mtl = p.mtl;
176 subs.resize (subs.size () + 1);
177 sub = &subs.back ();
178 sub->mtl = mtlList[mtl];
179 sub->firstVertex = verts.size ();
180 sub->firstFace = inds.size ()/3;
181 sub->numVerts = 0;
182 sub->numFaces = 0;
183 sub->tris = NULL;
185 // add polygon as a set of verts and indexes
186 size_t nverts = p.verts.size ();
187 for (size_t v = 0; v < nverts; v++)
189 verts.resize (verts.size () + 1);
190 drawVertex_t &vx = verts.back ();
191 vx.pos.x = p.verts[v].x;
192 vx.pos.y = p.verts[v].y;
193 vx.pos.z = p.verts[v].z;
194 vx.norm = p.normal;
195 vx.uv.x = p.verts[v].u;
196 vx.uv.y = p.verts[v].v;
197 vx.uvLightmap.x = p.verts[v].u;
198 vx.uvLightmap.y = p.verts[v].v;
199 vx.color[0] = 0xff;
200 vx.color[1] = 0xff;
201 vx.color[2] = 0xff;
202 vx.color[3] = 0xff;
204 // poly is a triangle fan, convert it to indexed trilist
205 size_t nfaces = nverts-2;
206 for (size_t f = 1; f < nfaces-1; f++)
208 inds.push_back (0+sub->numVerts);
209 inds.push_back (f+sub->numVerts);
210 inds.push_back (f+1+sub->numVerts);
212 sub->numVerts += nverts;
213 sub->numFaces += nfaces;
215 // now create model
216 matrix4 mtx (true);
217 // mpModel = new model (fname, NULL, mtx, verts, inds, subs, mtlList);
218 mpModel = NULL;
221 smartPtr <model> entWorld::getModel (void) const
223 return mpModel;
227 * editorname: info_playerstart
230 void entSpawnSpot::init (void)
235 * editorname: info_physx
237 void entPhysXObj::init (void)