CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

engine.h

00001 /*
00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_ENGINE_H__
00020 #define __CS_ENGINE_H__
00021 
00022 #include "csutil/scf.h"
00023 #include "csutil/nobjvec.h"
00024 #include "csutil/csobject.h"
00025 #include "csutil/garray.h"
00026 #include "csutil/parray.h"
00027 #include "csutil/refarr.h"
00028 #include "csutil/hashmap.h"
00029 #include "iutil/eventh.h"
00030 #include "iutil/comp.h"
00031 #include "iutil/config.h"
00032 #include "csgeom/math3d.h"
00033 #include "csengine/rview.h"
00034 #include "csengine/meshobj.h"
00035 #include "csengine/region.h"
00036 #include "csengine/sharevar.h"
00037 #include "iengine/engine.h"
00038 #include "iengine/collectn.h"
00039 #include "iengine/campos.h"
00040 #include "iutil/dbghelp.h"
00041 #include "ivideo/graph3d.h"
00042 
00043 
00044 class csPoly2DPool;
00045 //class csRadiosity;
00046 class csRegion;
00047 class csSector;
00048 class csMeshWrapper;
00049 class csTextureList;
00050 class csMaterialList;
00051 class csPolygon3D;
00052 class csCamera;
00053 class csStatLight;
00054 class csDynLight;
00055 class csEngine;
00056 class csLight;
00057 class csLightPatchPool;
00058 class csLightHalo;
00059 class csRenderView;
00060 struct iVFS;
00061 struct iMaterialWrapper;
00062 struct iRegion;
00063 struct iLight;
00064 struct iImageIO;
00065 struct iClipper2D;
00066 struct iReporter;
00067 struct iProgressMeter;
00068 struct iObjectRegistry;
00069 struct iVirtualClock;
00070 struct iCacheManager;
00071 struct iString;
00072 struct iEngineSequenceManager;
00073 
00080 class csLightIt : public iLightIterator
00081 {
00082 private:
00083   // The engine for this iterator.
00084   csEngine* engine;
00085   // The region we are iterating in (optional).
00086   iRegion* region;
00087   // Current sector index.
00088   int sector_idx;
00089   // Current light index.
00090   int light_idx;
00091 
00092   // Go to next sector. Return false if finished.
00093   bool NextSector ();
00094 
00095 public:
00097   csLightIt (csEngine*, iRegion* region = NULL);
00098 
00099   virtual ~csLightIt () { }
00100 
00101   SCF_DECLARE_IBASE;
00102 
00104   virtual void Restart ();
00105 
00107   virtual iLight* Fetch ();
00108 
00110   virtual iSector* GetLastSector ();
00111 };
00112 
00116 class csCollectionList : public csRefArrayObject<iCollection>
00117 {
00118 public:
00119   SCF_DECLARE_IBASE;
00120 
00122   csCollectionList ();
00123   virtual ~csCollectionList () { }
00125   virtual iCollection* NewCollection (const char* name);
00126 
00128   class CollectionList : public iCollectionList
00129   {
00130   public:
00131     SCF_DECLARE_EMBEDDED_IBASE (csCollectionList);
00132     virtual iCollection* NewCollection (const char* name);
00133     virtual int GetCount () const;
00134     virtual iCollection *Get (int n) const;
00135     virtual int Add (iCollection *obj);
00136     virtual bool Remove (iCollection *obj);
00137     virtual bool Remove (int n);
00138     virtual void RemoveAll ();
00139     virtual int Find (iCollection *obj) const;
00140     virtual iCollection *FindByName (const char *Name) const;
00141   } scfiCollectionList;
00142 };
00143 
00148 class csCameraPositionList : public csRefArrayObject<iCameraPosition>
00149 {
00150 public:
00151   SCF_DECLARE_IBASE;
00152 
00154   csCameraPositionList ();
00155   virtual ~csCameraPositionList () { }
00157   virtual iCameraPosition* NewCameraPosition (const char* name);
00158 
00160   class CameraPositionList : public iCameraPositionList
00161   {
00162     SCF_DECLARE_EMBEDDED_IBASE (csCameraPositionList);
00163     virtual iCameraPosition* NewCameraPosition (const char* name);
00164     virtual int GetCount () const;
00165     virtual iCameraPosition *Get (int n) const;
00166     virtual int Add (iCameraPosition *obj);
00167     virtual bool Remove (iCameraPosition *obj);
00168     virtual bool Remove (int n);
00169     virtual void RemoveAll ();
00170     virtual int Find (iCameraPosition *obj) const;
00171     virtual iCameraPosition *FindByName (const char *Name) const;
00172   } scfiCameraPositionList;
00173 };
00174 
00178 class csEngineMeshList : public csMeshList
00179 {
00180 public:
00181   virtual ~csEngineMeshList () { RemoveAll (); }
00182   virtual void FreeItem (iMeshWrapper*);
00183 };
00184 
00196 struct csEngineConfig : public iConfig
00197 {
00198   SCF_DECLARE_EMBEDDED_IBASE (csEngine);
00199   virtual bool GetOptionDescription (int idx, csOptionDescription *option);
00200   virtual bool SetOption (int id, csVariant* value);
00201   virtual bool GetOption (int id, csVariant* value);
00202 };
00203 
00209 class csEngine : public iEngine
00210 {
00211 public:
00220   csRef<iVFS> VFS;
00221 
00225   csRef<iEngineSequenceManager> eseqmgr;
00226 
00231   csRef<iReporter> Reporter;
00232 
00244   csRefArray<iBase> cleanup;
00245 
00251   csSectorList sectors;
00252 
00257   csCollectionList collections;
00258 
00263   csMeshFactoryList mesh_factories;
00264 
00272   csEngineMeshList meshes;
00273 
00277   csCameraPositionList camera_positions;
00278 
00280   static int frame_width, frame_height;
00282   static iObjectRegistry* object_reg;
00284   static csEngine* current_engine;
00286   static iEngine* current_iengine;
00288   static bool use_new_radiosity;
00290   csRef<iGraphics3D> G3D;
00292   csRef<iGraphics2D> G2D;
00294   csRef<iImageIO> ImageLoader;
00300   csRef<iCacheManager> cache_mgr;
00301 #ifndef CS_USE_NEW_RENDERER
00302 
00303   G3D_FOGMETHOD fogmethod;
00304 #endif // CS_USE_NEW_RENDERER
00305 
00306   bool NeedPO2Maps;
00308   int MaxAspectRatio;
00310   iRegion* region;
00312   csRegionList regions;
00313 
00315   csVector render_priorities;
00317   csGrowingArray<int> render_priority_sortflags;
00319   csGrowingArray<bool> render_priority_cameraflags;
00330   long render_priority_sky;
00331   long render_priority_wall;
00332   long render_priority_object;
00333   long render_priority_alpha;
00334 
00336   static int lightcache_mode;
00338   static int lightmap_quality;
00340   static bool do_force_revis;
00342   static bool do_rad_debug;
00344   static int max_lightmap_w;
00345   static int max_lightmap_h;
00346 
00347 private:
00349   csTextureList* textures;
00351   csMaterialList* materials;
00353   csDynLight* first_dyn_lights;
00355   csSharedVariableList* shared_variables;
00357   csPDelArray<csLightHalo> halos;
00359   csRef<iMeshObjectType> thing_type;
00361   static int max_process_polygons;
00363   static int cur_process_polygons;
00364 
00366   iTextureHandle* render_context;
00367 
00369   csHashSet want_to_die;
00370 
00372   //csRadiosity* rad_debug;
00373 
00375   int default_fastmesh_thresshold;
00377   int fastmesh_thresshold;
00378 
00380   bool clear_zbuf;
00381 
00383   bool default_clear_zbuf;
00384 
00386   bool clear_screen;
00387 
00389   bool default_clear_screen;
00390 
00392   int default_max_lightmap_w, default_max_lightmap_h;
00393 
00395   int default_ambient_red, default_ambient_green, default_ambient_blue;
00396 
00405   csTicks nextframe_pending;
00406 
00408   csRef<iVirtualClock> virtual_clock;
00409 
00410 private:
00414   void StartDraw (iCamera* c, iClipper2D* view, csRenderView& rview);
00415 
00419   void ControlMeshes ();
00420 
00432   char* SplitRegionName (const char* name, iRegion*& region, bool& global);
00433 
00437   void GetNearbyObjectList (iSector* sector,
00438     const csVector3& pos, float radius, csPArray<iObject>& list,
00439     csPArray<iSector>& visited_sectors, bool crossPortals = true);
00440 
00444   iEngineSequenceManager* GetEngineSequenceManager ();
00445 
00446 public:
00450   iCamera* current_camera;
00451 
00455   iClipper2D* top_clipper;
00456 
00457 public:
00463   csEngine (iBase *iParent);
00464 
00471   virtual ~csEngine ();
00472 
00476   void Report (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00477 
00481   void Warn (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00482 
00486   void ReportBug (const char* description, ...) CS_GNUC_PRINTF (2, 3);
00487 
00495   bool CheckConsistency ();
00496 
00504   virtual void PrepareTextures ();
00505 
00511   virtual void PrepareMeshes ();
00512 
00519   virtual void ShineLights (iRegion* region = NULL,
00520         iProgressMeter* meter = NULL);
00521 
00523   virtual iObject *QueryObject();
00525   inline csObject *QueryCsObject () { return &scfiObject; }
00526 
00535   virtual bool Prepare (iProgressMeter* meter = NULL);
00536 
00541   static void SetMaxProcessPolygons (int m) { max_process_polygons = m; }
00542 
00546   static int GetMaxProcessPolygons () { return max_process_polygons; }
00547 
00552   static bool ProcessPolygon ()
00553   {
00554     if (cur_process_polygons > max_process_polygons) return false;
00555     cur_process_polygons++;
00556     return true;
00557   }
00558 
00562   static bool ProcessLastPolygon ()
00563   {
00564     return cur_process_polygons >= max_process_polygons;
00565   }
00566 
00571   //csRadiosity* GetRadiosity () const { return rad_debug; }
00572 
00578   void InvalidateLightmaps ();
00579 
00585   virtual int GetBeginDrawFlags () const
00586   {
00587     int flag = 0;
00588     if (clear_screen) flag |= CSDRAW_CLEARSCREEN;
00589     if (clear_zbuf)
00590       return flag | CSDRAW_CLEARZBUFFER;
00591     else
00592       return flag;
00593   }
00594 
00598   csTicks GetLastAnimationTime () const { return nextframe_pending; }
00599 
00601   virtual void SetLightingCacheMode (int mode) { lightcache_mode = mode; }
00603   virtual int GetLightingCacheMode () { return lightcache_mode; }
00604 
00606   virtual void SetFastMeshThresshold (int th) { fastmesh_thresshold = th; }
00608   virtual int GetFastMeshThresshold () const { return fastmesh_thresshold; }
00609 
00611   virtual void SetClearZBuf (bool yesno)
00612   {
00613     clear_zbuf = yesno;
00614   }
00615 
00617   virtual bool GetClearZBuf () const { return clear_zbuf; }
00618 
00620   virtual bool GetDefaultClearZBuf () const { return default_clear_zbuf; }
00621 
00623   virtual void SetClearScreen (bool yesno)
00624   {
00625     clear_screen = yesno;
00626   }
00627 
00629   virtual bool GetClearScreen () const { return clear_screen; }
00630 
00632   virtual bool GetDefaultClearScreen () const { return default_clear_screen; }
00633 
00638   virtual void SetMaxLightmapSize(int w, int h) 
00639   { max_lightmap_w = w; max_lightmap_h = h; }
00641   virtual void GetMaxLightmapSize(int& w, int& h)
00642   { w = max_lightmap_w; h = max_lightmap_h; }
00644   virtual void GetDefaultMaxLightmapSize(int& w, int& h)
00645   { w = default_max_lightmap_w; h = default_max_lightmap_h; }
00647   virtual void GetDefaultAmbientLight (csColor &c) const;
00648 
00649   virtual bool GetLightmapsRequirePO2 () const { return NeedPO2Maps; }
00650   virtual int GetMaxLightmapAspectRatio () const { return MaxAspectRatio; }
00651   
00652   virtual csPtr<iFrustumView> CreateFrustumView ();
00653   virtual csPtr<iObjectWatcher> CreateObjectWatcher ();
00654   virtual void WantToDie (iMeshWrapper* mesh);
00655 
00664   virtual void ResetWorldSpecificSettings();  
00665 
00671   void ReadConfig (iConfigFile*);
00672 
00678   void StartEngine ();
00679 
00683   csTextureList* GetTextures () const { return textures; }
00684 
00688   csMaterialList* GetMaterials () const { return materials; }
00689 
00693   csSharedVariableList* GetVariables () const { return shared_variables; }
00694 
00698   virtual csPtr<iMaterial> CreateBaseMaterial (iTextureWrapper* txt);
00699   virtual csPtr<iMaterial> CreateBaseMaterial (iTextureWrapper* txt,
00700         int num_layers, iTextureWrapper** wrappers, csTextureLayer* layers);
00701 
00702   virtual iSharedVariableList* GetVariableList () const;
00703   virtual iMaterialList* GetMaterialList () const;
00704   virtual iTextureList* GetTextureList () const;
00705   virtual iRegionList* GetRegions ();
00706 
00707   virtual csPtr<iMeshWrapper> CreateSectorWallsMesh (iSector* sector,
00708       const char* name);
00709   virtual csPtr<iMeshWrapper> CreateThingMesh (iSector* sector, const char* name);
00710 
00714   void AddDynLight (csDynLight* dyn);
00715 
00719   void RemoveDynLight (csDynLight* dyn);
00720 
00724   virtual iDynLight* GetFirstDynLight () const;
00725 
00727   virtual int GetNearbyLights (iSector* sector, const csVector3& pos,
00728         uint32 flags, iLight** lights, int max_num_lights);
00729 
00731   virtual int GetNearbyLights (iSector* sector, const csBox3& box,
00732         uint32 flags, iLight** lights, int max_num_lights);
00733 
00738   virtual csPtr<iSectorIterator> GetNearbySectors (iSector* sector,
00739         const csVector3& pos, float radius);
00740   virtual csPtr<iObjectIterator> GetNearbyObjects (iSector* sector,
00741     const csVector3& pos, float radius, bool crossPortals = true);
00742   virtual csPtr<iObjectIterator> GetVisibleObjects (iSector* sector,
00743     const csVector3& pos);
00744   virtual csPtr<iObjectIterator> GetVisibleObjects (iSector* sector,
00745     const csFrustum& frustum);
00746 
00747   virtual bool RemoveObject (iBase* object);
00748 
00752   void AddHalo (csLight* Light);
00753 
00757   void RemoveHalo (csLight* Light);
00758 
00766   virtual void Draw (iCamera* c, iClipper2D* clipper);
00767 
00771   virtual csPtr<iLightIterator> GetLightIterator (iRegion* region = NULL)
00772   {
00773     csLightIt* it;
00774     it = new csLightIt (this, region);
00775     return csPtr<iLightIterator> (it);
00776   }
00777 
00781   virtual void AddToCurrentRegion (iObject* obj);
00782 
00784   virtual void RegisterRenderPriority (const char* name, long priority,
00785         int rendsort = CS_RENDPRI_NONE, bool do_camera = false);
00787   virtual long GetRenderPriority (const char* name) const;
00789   virtual void SetRenderPriorityCamera (long priority, bool do_camera);
00791   virtual bool GetRenderPriorityCamera (const char* name) const;
00793   virtual bool GetRenderPriorityCamera (long priority) const;
00795   virtual int GetRenderPrioritySorting (const char* name) const;
00797   virtual int GetRenderPrioritySorting (long priority) const;
00799   virtual long GetSkyRenderPriority () const { return render_priority_sky; }
00801   virtual long GetWallRenderPriority () const { return render_priority_wall; }
00803   virtual long GetObjectRenderPriority () const { return render_priority_object; }
00805   virtual long GetAlphaRenderPriority () const { return render_priority_alpha; }
00807   virtual void ClearRenderPriorities ();
00809   virtual int GetRenderPriorityCount () const;
00811   virtual const char* GetRenderPriorityName (long priority) const;
00812 
00813   iMeshObjectType* GetThingType ();
00814 
00815   SCF_DECLARE_IBASE;
00816 
00817   //------------------ iComponent interface implementation --------------------
00818 
00824   virtual bool Initialize (iObjectRegistry* object_reg);
00825 
00827   virtual bool HandleEvent (iEvent &Event);
00828 
00830   struct eiComponent : public iComponent
00831   {
00832     SCF_DECLARE_EMBEDDED_IBASE(csEngine);
00833     virtual bool Initialize (iObjectRegistry* p)
00834     { return scfParent->Initialize(p); }
00835   } scfiComponent;
00836 
00838   struct EventHandler : public iEventHandler
00839   {
00840   private:
00841     csEngine* parent;
00842   public:
00843     SCF_DECLARE_IBASE;
00844     EventHandler (csEngine* parent)
00845     {
00846       SCF_CONSTRUCT_IBASE (NULL);
00847       EventHandler::parent = parent;
00848     }
00849     virtual bool HandleEvent (iEvent& e) { return parent->HandleEvent(e); }
00850   } * scfiEventHandler;
00851 
00852   //--------------------- iEngine interface implementation --------------------
00853 
00858   virtual int GetTextureFormat () const;
00859 
00864   virtual void SelectRegion (const char* iName);
00865 
00870   virtual void SelectRegion (iRegion* region);
00871 
00876   virtual iRegion* GetCurrentRegion () const;
00877 
00879   virtual void DeleteAll ();
00880 
00882   virtual iTextureWrapper* CreateTexture (const char *iName,
00883         const char *iFileName, csColor *iTransp, int iFlags);
00884   virtual iTextureWrapper* CreateBlackTexture (const char *name,
00885         int w, int h, csColor *iTransp, int iFlags);
00887   virtual iMaterialWrapper* CreateMaterial (const char *iName,
00888         iTextureWrapper* texture);
00889 
00891   virtual iSector *CreateSector (const char *iName);
00892 
00894   virtual iSectorList *GetSectors ()
00895     { return &sectors; }
00897   virtual iMeshFactoryList *GetMeshFactories ()
00898     { return &mesh_factories; }
00900   virtual iMeshList *GetMeshes ()
00901     { return &meshes; }
00903   virtual iCollectionList *GetCollections ()
00904     { return &collections.scfiCollectionList; }
00906   virtual iCameraPositionList *GetCameraPositions ()
00907     { return &camera_positions.scfiCameraPositionList; }
00908 
00910   virtual csPtr<iCamera> CreateCamera ();
00911 
00913   virtual csPtr<iStatLight> CreateLight (const char* name,
00914         const csVector3& pos, float radius,
00915         const csColor& color, bool pseudoDyn);
00917   virtual iStatLight* FindLightID (const char* light_id) const;
00919   virtual iStatLight* FindLight (const char *Name, bool RegionOnly = false)
00920     const;
00922   virtual csPtr<iDynLight> CreateDynLight (const csVector3& pos, float radius,
00923         const csColor& color);
00925   virtual void RemoveDynLight (iDynLight*);
00926 
00928   virtual csPtr<iMeshFactoryWrapper> CreateMeshFactory (const char* classId,
00929         const char* name);
00931   virtual csPtr<iMeshFactoryWrapper> CreateMeshFactory (iMeshObjectFactory *,
00932         const char* name);
00934   virtual csPtr<iMeshFactoryWrapper> CreateMeshFactory (const char* name);
00935   virtual csPtr<iLoaderContext> CreateLoaderContext (iRegion* region = NULL);
00937   virtual csPtr<iMeshFactoryWrapper> LoadMeshFactory (
00938         const char* name, const char* loaderClassId,
00939         iDataBuffer* input);
00940 
00942   virtual csPtr<iMeshWrapper> CreateMeshWrapper (iMeshFactoryWrapper* factory,
00943         const char* name, iSector* sector = NULL,
00944         const csVector3& pos = csVector3(0, 0, 0));
00946   virtual csPtr<iMeshWrapper> CreateMeshWrapper (iMeshObject*,
00947         const char* name, iSector* sector = NULL,
00948         const csVector3& pos = csVector3(0, 0, 0));
00950   virtual csPtr<iMeshWrapper> CreateMeshWrapper (const char* classid,
00951         const char* name, iSector* sector = NULL,
00952         const csVector3& pos = csVector3(0, 0, 0));
00954   virtual csPtr<iMeshWrapper> CreateMeshWrapper (const char* name);
00956   virtual csPtr<iMeshWrapper> LoadMeshWrapper (
00957         const char* name, const char* loaderClassId,
00958         iDataBuffer* input, iSector* sector, const csVector3& pos);
00959 
00960   virtual iClipper2D* GetTopLevelClipper () const;
00961 
00963   virtual void SetAmbientLight (const csColor &c);
00965   virtual void GetAmbientLight (csColor &c) const;
00966 
00967   virtual iMaterialWrapper* FindMaterial (const char* name,
00968         iRegion* region = NULL);
00969   virtual iTextureWrapper* FindTexture (const char* name,
00970         iRegion* region = NULL);
00971   virtual iSector* FindSector (const char* name,
00972         iRegion* region = NULL);
00973   virtual iMeshWrapper* FindMeshObject (const char* name,
00974         iRegion* region = NULL);
00975   virtual iMeshFactoryWrapper* FindMeshFactory (const char* name,
00976         iRegion* region = NULL);
00977   virtual iCameraPosition* FindCameraPosition (const char* name,
00978         iRegion* region = NULL);
00979   virtual iCollection* FindCollection (const char* name,
00980         iRegion* region = NULL);
00981 
00982   bool DebugCommand (const char* cmd);
00983 
00984   //----------------------- iCacheManager implementation ---------------------
00985 
00986   virtual void SetCacheManager (iCacheManager* cache_mgr);
00987   virtual iCacheManager* GetCacheManager ();
00988 
00989   //--------------------- iConfig interface implementation -------------------
00990 
00991   csEngineConfig scfiConfig;
00992 
00993   //----------------Begin-Multi-Context-Support-------------------------------
00994 
00996   virtual void SetContext (iTextureHandle* txt);
00998   virtual iTextureHandle *GetContext () const;
00999 
01000 private:
01002   void Resize ();
01004   bool resize;
01005 
01009   class iObjectInterface : public csObject
01010   {
01011     SCF_DECLARE_EMBEDDED_IBASE (csEngine);
01012   } scfiObject;
01013 
01014   struct DebugHelper : public iDebugHelper
01015   {
01016     SCF_DECLARE_EMBEDDED_IBASE (csEngine);
01017     virtual int GetSupportedTests () const
01018     {
01019       return 0;
01020     }
01021     virtual csPtr<iString> UnitTest () { return 0; }
01022     virtual csPtr<iString> StateTest () { return 0; }
01023     virtual csTicks Benchmark (int) { return 0; }
01024     virtual csPtr<iString> Dump () { return 0; }
01025     virtual void Dump (iGraphics3D*) { }
01026     virtual bool DebugCommand (const char* cmd)
01027     {
01028       return scfParent->DebugCommand (cmd);
01029     }
01030   } scfiDebugHelper;
01031 };
01032 
01033 #endif // __CS_ENGINE_H__

Generated for Crystal Space by doxygen 1.2.14