changed copyright years in source files
[fegdk.git] / core / code / system / f_resourcemgr.cpp
blob5045f4b172c6c0ab07ebd37e11814aa415b696af
1 /*
2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 #include "pch.h"
24 #include "config.h"
25 #include "f_texture.h"
26 #include "f_fontft.h"
27 #include "f_material.h"
28 #include "f_animation.h"
29 #include "f_model.h"
30 #if HAVE_LIBCG
31 #include "f_cgshader.h"
32 #endif
33 #include "f_ps14shader.h"
34 #include "f_effect.h"
35 #include "f_resourcemgr.h"
36 #include "f_error.h"
37 #include "f_engine.h"
39 namespace fe
42 resourceMgr::resourceMgr (void)
44 setName ("resource mgr");
47 resourceMgr::~resourceMgr (void)
51 resource <texture> *resourceMgr::getTextureMgr (void)
53 return &mTextureMgr;
56 resource <material> *resourceMgr::getMaterialMgr (void)
58 return &mMaterialMgr;
61 resource <fontFT> *resourceMgr::getFontFTMgr (void)
63 return &mFontMgr;
66 resource <sceneObject> *resourceMgr::getModelMgr (void)
68 return &mModelMgr;
71 resource <animation> *resourceMgr::getAnimationMgr (void)
73 return &mAnimationMgr;
76 resource <cgShader> *resourceMgr::getCGShaderMgr (void)
78 #if HAVE_LIBCG
79 return &mCGShaderMgr;
80 #else
81 return NULL;
82 #endif
85 resource <ps14Shader> *resourceMgr::getPS14ShaderMgr (void)
87 return &mPS14ShaderMgr;
90 resource <effect> *resourceMgr::getEffectMgr (void)
92 return &mEffectMgr;
95 texture* resourceMgr::createTexture (const char *name)
97 texture *t = mTextureMgr.create (name);
98 if (!t)
99 return mTextureMgr.create ("textures/common/notfound.png");
100 return t;
103 texture* resourceMgr::createTexture (charParser &parser)
105 texture *t = mTextureMgr.create (parser);
106 if (!t)
107 return mTextureMgr.create ("textures/common/notfound.png");
108 return t;
111 material* resourceMgr::createMaterial (const char *name)
113 return mMaterialMgr.create (name);
116 material* resourceMgr::createMaterial (charParser &parser)
118 return mMaterialMgr.create (parser);
121 fontFT* resourceMgr::createFontFT (const char *name)
123 return mFontMgr.create (name);
126 fontFT* resourceMgr::createFontFT (charParser &parser)
128 return mFontMgr.create (parser);
131 struct mtlBlock
133 std::vector <materialPtr> mMtlList;
135 void add (const materialPtr &mtl) { mMtlList.push_back (mtl); }
136 int numMtls (void) const { return (int)mMtlList.size (); }
137 materialPtr getMtl (int i) { return mMtlList[i]; }
138 void load (file *f)
140 uint32 sp;
141 f->readUINT32 (&sp, 1);
142 if (!sp)
143 return;
144 mMtlList.resize (sp);
145 for (uint32 i = 0; i < sp; i++)
147 uchar ln;
148 f->read (&ln, 1);
149 char *str = new char[ln+1];
150 f->read (str, ln);
151 str[ln]=0;
152 mMtlList[i] = g_engine->getResourceMgr ()->createMaterial (str);
153 delete[] str;
158 static void loadModelFromFile (file *f, int majorver, int minorver, sceneObject *node, mtlBlock *mtls)
160 if (minorver >= 2)
162 char nsz;
163 f->read (&nsz, sizeof (char));
164 if (nsz)
166 char nm[100];
167 f->read (nm, nsz);
168 nm[nsz] = 0;
169 node->setName (nm);
173 uint32 sp;
175 // matrix
176 matrix4 mtx;
177 f->readMatrix4 (&mtx, 1);
178 matrix4 worldMtx = node->getParent () ? mtx * node->getParent ()->getWorldMatrix() : mtx;
179 node->setRelativeMatrix (mtx);
180 node->setWorldMatrix (worldMtx);
182 model *mdl = NULL;
183 // ok, try mesh subsets
184 f->readUINT32 (&sp, 1);
185 if (sp)
187 mdl = new model ();
188 renderable *rend = new renderable (mdl);
189 node->attachRenderable (rend);
190 mdl->loadMesh (f, majorver, minorver, mtls->mMtlList, sp, rend);
192 else
194 // skip mesh flags
196 // tangents
197 uchar tangents;
198 f->read (&tangents, sizeof (uchar));
200 // lightmaps
201 uchar lightmaps = 0;
202 if (minorver >= 1)
203 f->read (&lightmaps, sizeof(uchar));
205 // numverts
206 f->readUINT32 (&sp, 1);
208 // numfaces
209 f->readUINT32 (&sp, 1);
211 // old version animation flag
212 if (minorver < 2)
214 uchar b;
215 f->read (&b, sizeof (b));
219 // load children
220 f->readUINT32 (&sp, 1);
221 int nChildren = sp;
223 for (uint32 i = 0; i < nChildren; i++)
225 sceneObject *c = new sceneObject (node);
226 loadModelFromFile (f, majorver, minorver, c, mtls);
227 node->addChild (c);
231 static sceneObject *loadModelFromFile (const char *fname)
233 // sceneobject must be created for each node
234 // model must be created only where mesh exists
235 file *f = g_engine->getFileSystem ()->openFile (fname, FS_MODE_READ);
237 int majorver = 1;
238 int minorver = 0;
239 sceneObject *node = new sceneObject (NULL);
240 // read magic
241 char mgc[10];
242 f->read( mgc, 9 );
243 mgc[9] = 0;
244 // for now i support only 1.1
245 if (!strcmp (mgc, "FEMDL0101"))
247 majorver = 1;
248 minorver = 1;
250 else if (!strcmp (mgc, "FEMDL0102"))
252 majorver = 1;
253 minorver = 2;
255 else
256 sys_error("Invalid model file signature (%s).\n", f->name());
258 // load materials
259 mtlBlock mtls;
260 mtls.load (f);
262 loadModelFromFile (f, majorver, minorver, node, &mtls);
263 f->close ();
265 return node;
268 sceneObject* resourceMgr::createModel (const char *name, animContainer *anim)
270 // model is created specially, cause we need instancing
271 sceneObject *obj = mModelMgr.getResource (name);
272 if (!obj)
274 // load template
275 obj = loadModelFromFile (name);
276 // model *mdl = new model ();
277 // obj = mdl->load (name);
278 mModelMgr.addResource (name, obj);
281 // create from template
282 sceneObject *newModel = obj->getNewInstance ();
283 if (anim)
284 newModel->initAnimData (anim);
285 return newModel;
288 sceneObject* resourceMgr::createModel (charParser &parser)
290 return NULL;
293 animation* resourceMgr::createAnimation (const char *name)
295 return mAnimationMgr.create (name);
298 animation* resourceMgr::createAnimation (charParser &parser)
300 return mAnimationMgr.create (parser);
303 #ifdef HAVE_LIBCG
304 cgShader* resourceMgr::createCGShader (const char *name)
306 return mCGShaderMgr.create (name);
309 cgShader* resourceMgr::createCGShader (charParser &parser)
311 return mCGShaderMgr.create (parser);
313 #endif
315 ps14Shader* resourceMgr::createPS14Shader (const char *name)
317 return mPS14ShaderMgr.create (name);
319 ps14Shader* resourceMgr::createPS14Shader (charParser &parser)
321 return mPS14ShaderMgr.create (parser);
324 effect* resourceMgr::createEffect (const char *name)
326 return mEffectMgr.create (name);
329 effect* resourceMgr::createEffect (charParser &parser)
331 return mEffectMgr.create (parser);