Fix issue in Rocket.lua script.
[Cafu-Engine.git] / Common / WorldMan.cpp
blobe460a185d384c1f65f3f26164df4ef6aa9e39692
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
7 #include "WorldMan.hpp"
8 #include "ConsoleCommands/ConVar.hpp"
11 const WorldT* WorldManT::LoadWorld(const char* FileName, ModelManagerT& ModelMan, cf::GuiSys::GuiResourcesT& GuiRes, bool InitForGraphics, WorldT::ProgressFunctionT ProgressFunction)
13 for (unsigned long WorldNr=0; WorldNr<Worlds.Size(); WorldNr++)
15 WorldInfoT& WI=Worlds[WorldNr];
17 if (WI.FileName==FileName)
19 assert(WI.RefCount>0);
20 WI.RefCount++;
21 if (InitForGraphics && !WI.Init4Gfx) InitWorldForGfx(WI);
22 return WI.WorldPtr;
27 // FileName not found in cache, create a new instance.
28 WorldT* NewWorld=new WorldT(FileName, ModelMan, GuiRes, ProgressFunction); // Must do this here in case WorldT::WorldT() throws.
30 Worlds.PushBackEmpty();
31 WorldInfoT& WI=Worlds[Worlds.Size()-1];
33 WI.FileName=FileName;
34 WI.RefCount=1;
35 WI.Init4Gfx=false;
36 WI.WorldPtr=NewWorld;
38 if (InitForGraphics) InitWorldForGfx(WI);
40 return WI.WorldPtr;
44 void WorldManT::FreeWorld(const WorldT* World)
46 for (unsigned long WorldNr=0; WorldNr<Worlds.Size(); WorldNr++)
48 WorldInfoT& WI=Worlds[WorldNr];
50 if (WI.WorldPtr==World)
52 assert(WI.RefCount>0);
53 WI.RefCount--;
55 if (WI.RefCount==0)
57 delete WI.WorldPtr;
58 Worlds.RemoveAt(WorldNr);
61 return;
65 assert(false);
69 WorldManT::~WorldManT()
71 // Did we really pair each call to LoadWorld() with a call to FreeWorld()?
72 assert(Worlds.Size()==0);
76 void WorldManT::InitWorldForGfx(WorldInfoT& WI)
78 // Assert that this is done only once for any world instance.
79 assert(!WI.Init4Gfx);
81 #if SHL_ENABLED
82 WI.WorldPtr->SHLMapMan.InitTextures();
83 #endif
85 static ConVarT r_gamma_lm("cl_gamma_lm", 1.0, ConVarT::FLAG_MAIN_EXE, "The gamma correction value that is applied to lightmaps on the next map load.");
87 static ConVarT r_lm_ambient_r("cl_lm_ambient_r", 0, ConVarT::FLAG_MAIN_EXE, "The red component of the lightmaps ambient (==minimum) color.");
88 static ConVarT r_lm_ambient_g("cl_lm_ambient_g", 0, ConVarT::FLAG_MAIN_EXE, "The green component of the lightmaps ambient (==minimum) color.");
89 static ConVarT r_lm_ambient_b("cl_lm_ambient_b", 0, ConVarT::FLAG_MAIN_EXE, "The blue component of the lightmaps ambient (==minimum) color.");
91 assert(r_gamma_lm.GetType()==ConVarT::Double);
92 assert(r_lm_ambient_r.GetType()==ConVarT::Integer);
94 WI.WorldPtr->LightMapMan.InitTextures(float(r_gamma_lm.GetValueDouble()), r_lm_ambient_r.GetValueInt(), r_lm_ambient_g.GetValueInt(), r_lm_ambient_b.GetValueInt());
96 // Note that this world has been inited for graphics already.
97 WI.Init4Gfx=true;