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.
8 #include "ConsoleCommands/Console.hpp"
9 #include "ConsoleCommands/ConVar.hpp"
10 #include "ConsoleCommands/ConFunc.hpp"
11 #include "MaterialSystem/MaterialManager.hpp"
12 #include "MaterialSystem/Renderer.hpp"
13 #include "SoundSystem/Sound.hpp"
14 #include "SoundSystem/SoundSys.hpp"
15 #include "SoundSystem/SoundShaderManager.hpp"
16 #include "TextParser/TextParser.hpp"
29 static const int CLIENT_RUNMODE
=1;
30 static const int SERVER_RUNMODE
=2;
31 static const int DefaultRunMode
=CLIENT_RUNMODE
| SERVER_RUNMODE
;
33 /*static*/ ConVarT
Options_RunMode ("dlg_RunMode", DefaultRunMode
, ConVarT::FLAG_MAIN_EXE
, "1 is client-only, 2 is server-only, 3 is both.", 1, 3);
34 /*static*/ ConVarT
Options_PlayerName ("dlg_dmPlayerName", "Player", ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "Player name.");
35 /*static*/ ConVarT
Options_PlayerModelName ("dlg_dmModelName", "James", ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "Name of the player model.");
36 /*static*/ ConVarT
Options_ClientFullScreen ("dlg_clFullScreen", true, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "When true, the video mode is changed on startup and fullscreen display is used. Otherwise, Cafu runs in an application window on the desktop.");
37 /*static*/ ConVarT
Options_ClientWindowSizeX ("dlg_clWindowSizeX", 1024, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "The size of the client window in X direction.");
38 /*static*/ ConVarT
Options_ClientWindowSizeY ("dlg_clWindowSizeY", 768, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "The size of the client window in Y direction.");
39 /*static*/ ConVarT
Options_ClientDesiredRenderer ("dlg_clDesiredRenderer", "", ConVarT::FLAG_MAIN_EXE
, "If set, overrides the auto-selection of the renderer and determines which renderer is to be used instead.");
40 /*static*/ ConVarT
Options_ClientDesiredSoundSystem("dlg_clDesiredSoundSystem", "", ConVarT::FLAG_MAIN_EXE
, "If set, overrides the auto-selection of the sound system and determines which sound system is to be used instead.");
41 /*static*/ ConVarT
Options_ClientPortNr ("dlg_clPortNr", 33000, ConVarT::FLAG_MAIN_EXE
, "The client port number.", 0, 0xFFFF);
42 /*static*/ ConVarT
Options_ClientRemoteName ("dlg_clRemoteName", "192.168.1.1", ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "Name or IP of the server the client connects to.");
43 /*static*/ ConVarT
Options_ClientRemotePortNr ("dlg_clRemotePort", 30000, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "Port number of the remote server.", 0, 0xFFFF);
44 /*static*/ ConVarT
Options_ClientDisplayBPP ("dlg_clDisplayBPP", 32, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "The display depth in bits-per-pixel. Normally use 32, or 0 for system default.", 0, 64);
45 /*static*/ ConVarT
Options_ClientDisplayRefresh ("dlg_clDisplayRefresh", 0, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "The display refresh rate. Use 0 for system default, check with your monitor specs for any other value.");
46 /*static*/ ConVarT
Options_ClientTextureDetail ("dlg_clTextureDetail", 0, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_PERSISTENT
, "0 high detail, 1 medium detail, 2 low detail", 0, 2);
47 /*static*/ ConVarT
Options_ServerWorldName ("dlg_svWorldName", "TechDemo", ConVarT::FLAG_MAIN_EXE
, "Name of the world to load.");
48 /*static*/ ConVarT
Options_ServerPortNr ("dlg_svPortNr", 30000, ConVarT::FLAG_MAIN_EXE
, "Server port number.", 0, 0xFFFF);
51 // Notes about the GlobalTime convar:
52 // 1) It's declared for use with "extern ConVarT GlobalTime;" elsewhere, so we don't use "static" here.
53 // 2) It's intentionally not defined very precisely (except for that it is in seconds and ever increases),
54 // because I especially don't want to define where "0" is (at program start, at start of first frame, at new map start, ...?).
55 ConVarT
GlobalTime("time", 0.0, ConVarT::FLAG_MAIN_EXE
| ConVarT::FLAG_READ_ONLY
, "The ever proceeding time, in seconds.");
58 static int ConFunc_CleanupPersistentConfig_Callback(lua_State
* LuaState
)
60 const char* CfgFileName
=luaL_checkstring(LuaState
, 1);
61 TextParserT
TP(CfgFileName
, "=");
62 std::map
<std::string
, std::string
> Pairs
;
66 // This function must not cause a Lua error, or otherwise the calling config.lua script is aborted early!
67 Console
->Warning(cf::va("Unable to open file %s.\n", CfgFileName
));
75 std::string Key
=TP
.GetNextToken(); TP
.AssertAndSkipToken("=");
76 std::string Value
=TP
.GetNextToken();
78 if (TP
.WasLastTokenQuoted()) Value
="\""+Value
+"\"";
83 catch (const TextParserT::ParseError
&)
85 // This function must not cause a Lua error, or otherwise the calling config.lua script is aborted early!
86 Console
->Warning(cf::va("Problem parsing the config file near byte %lu (%.3f%%).", TP
.GetReadPosByte(), TP
.GetReadPosPercent()*100.0));
91 // Ok, all the right keys and values are now in Pairs, now rewrite the config file.
92 std::ofstream
CfgFile(CfgFileName
, std::ios::out
);
96 for (std::map
<std::string
, std::string
>::const_iterator It
=Pairs
.begin(); It
!=Pairs
.end(); ++It
)
98 CfgFile
<< It
->first
<< " = " << It
->second
<< "\n";
107 static ConFuncT
ConFunc_CleanupPersistentConfig("CleanupPersistentConfig", ConFunc_CleanupPersistentConfig_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");
110 static int ConFunc_VideoInfo_Callback(lua_State
* LuaState
)
112 // Console->Print(GetVideoModes());
113 Console
->Print(cf::va("Renderer Info: %s\n", MatSys::Renderer
? MatSys::Renderer
->GetDescription() : "[No renderer active.]"));
117 static ConFuncT
ConFunc_VideoInfo("VideoInfo", ConFunc_VideoInfo_Callback
, ConFuncT::FLAG_MAIN_EXE
, "Prints some information about the OpenGL window and renderer.");
120 static int ConFunc_forceRM_Callback(lua_State
* LuaState
)
122 static MatSys::RenderMaterialT
* ForceRM
=NULL
;
124 // First check if the MatSys::Renderer is available, we need it below in all usage-cases of this function.
125 if (MatSys::Renderer
==NULL
)
127 ForceRM
=NULL
; // The ForceRM was either never assigned, or automatically freed when the MatSys::Renderer last went down.
128 return luaL_error(LuaState
, "MatSys::Renderer is not available.");
131 // If a ForceRM was set in a previous call to this function, free it.
134 if (MatSys::Renderer
->GetCurrentMaterial()==ForceRM
)
136 MatSys::Renderer
->FreeMaterial(ForceRM
);
140 // Getting here is highly unusual, because either there is code somewhere else that improperly broke our material lock,
141 // or the MatSys::Renderer was shutdown and re-instantiated since the previous call to this function.
142 // In the latter case, we must not try to free the ForceRM, so the above if-test is a nice protection.
143 Console
->Warning("The previously forced material has been unforced elsewhere!\n");
149 // If no parameters were given, remove any lock to restore normal operation.
150 if (lua_gettop(LuaState
)==0)
152 MatSys::Renderer
->LockCurrentMaterial(false);
156 // Establish the lock for the given material name.
157 if (MaterialManager
==NULL
) return luaL_error(LuaState
, "The MaterialManager is not available.");
159 const char* MatName
=luaL_checkstring(LuaState
, 1);
160 MaterialT
* Mat
=MaterialManager
->GetMaterial(MatName
);
162 if (Mat
==NULL
) return luaL_error(LuaState
, "Unknown material \"%s\".", MatName
);
164 ForceRM
=MatSys::Renderer
->RegisterMaterial(Mat
);
166 if (ForceRM
==NULL
) return luaL_error(LuaState
, "Could not register material \"%s\".", MatName
);
168 MatSys::Renderer
->SetCurrentMaterial(ForceRM
);
169 MatSys::Renderer
->LockCurrentMaterial(true);
174 static ConFuncT
ConFunc_forceRM("forceRM", ConFunc_forceRM_Callback
, ConFuncT::FLAG_MAIN_EXE
,
175 "Enforces the use of the given material for all rendering. Restores normal rendering when no parameter is given.");
178 static SoundI
* ConMusic
=NULL
; // For playing background music from the console.
180 static int ConFunc_MusicLoad_Callback(lua_State
* LuaState
)
182 if (SoundSystem
==NULL
)
184 return luaL_error(LuaState
, "SoundSystem is not available.");
189 SoundSystem
->DeleteSound(ConMusic
);
193 ConMusic
=SoundSystem
->CreateSound2D(SoundShaderManager
->GetSoundShader(luaL_checkstring(LuaState
, 1)));
197 static ConFuncT
ConFunc_MusicLoad("MusicLoad", ConFunc_MusicLoad_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");
200 static int ConFunc_MusicPlay_Callback(lua_State
* LuaState
)
202 if (SoundSystem
==NULL
) return luaL_error(LuaState
, "SoundSystem is not available.");
203 if (ConMusic
==NULL
) return luaL_error(LuaState
, "No sound shader loaded.");
209 static ConFuncT
ConFunc_MusicPlay("MusicPlay", ConFunc_MusicPlay_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");
212 static int ConFunc_MusicIsPlaying_Callback(lua_State
* LuaState
)
214 if (SoundSystem
==NULL
) return luaL_error(LuaState
, "SoundSystem is not available.");
216 lua_pushboolean(LuaState
, ConMusic
!=NULL
&& ConMusic
->IsPlaying());
220 static ConFuncT
ConFunc_MusicIsPlaying("MusicIsPlaying", ConFunc_MusicIsPlaying_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");
223 static int ConFunc_MusicStop_Callback(lua_State
* LuaState
)
225 if (SoundSystem
==NULL
) return luaL_error(LuaState
, "SoundSystem is not available.");
226 if (ConMusic
==NULL
) return luaL_error(LuaState
, "No sound shader loaded.");
232 static ConFuncT
ConFunc_MusicStop("MusicStop", ConFunc_MusicStop_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");
235 static int ConFunc_MusicSetVol_Callback(lua_State
* LuaState
)
237 if (SoundSystem
==NULL
) return luaL_error(LuaState
, "SoundSystem is not available.");
238 if (ConMusic
==NULL
) return luaL_error(LuaState
, "No sound shader loaded.");
240 ConMusic
->SetInnerVolume(float(lua_tonumber(LuaState
, 1)));
244 static ConFuncT
ConFunc_MusicSetVol("MusicSetVolume", ConFunc_MusicSetVol_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");
247 static int ConFunc_GetMasterVolume_Callback(lua_State
* LuaState
)
249 if (SoundSystem
==NULL
) return luaL_error(LuaState
, "SoundSystem is not available.");
251 lua_pushnumber(LuaState
, SoundSystem
->GetMasterVolume());
255 static ConFuncT
ConFunc_GetMasterVolume("GetMasterVolume", ConFunc_GetMasterVolume_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");
258 static int ConFunc_SetMasterVolume_Callback(lua_State
* LuaState
)
260 if (SoundSystem
==NULL
) return luaL_error(LuaState
, "SoundSystem is not available.");
262 SoundSystem
->SetMasterVolume(float(lua_tonumber(LuaState
, 1)));
266 static ConFuncT
ConFunc_SetMasterVolume("SetMasterVolume", ConFunc_SetMasterVolume_Callback
, ConFuncT::FLAG_MAIN_EXE
, "");