Leviathan  0.8.0.0
Leviathan game engine
AppDefine.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "AppDefine.h"
3 
5 #include "FileSystem.h"
6 #include "ForwardDeclarations.h"
7 #include "GameConfiguration.h"
8 #include "KeyConfiguration.h"
10 #include "Sound/SoundDevice.h"
11 
12 #include <iostream>
13 using namespace Leviathan;
14 // ------------------------------------ //
15 DLLEXPORT Leviathan::AppDef::AppDef(const bool& isdef /*= false*/) :
16  ConfigurationValues(new NamedVars()),
17 #ifdef _WIN32
18  HInstance(NULL)
19 #else
20  HInstance(0)
21 #endif //_WIN32
22 {
23  // If this is the default configuration set as the static access one //
24  if(isdef)
25  Defaultconf = this;
26 }
27 
29 {
30  // reset static access if this is it //
31  if(Defaultconf == this)
32  Defaultconf = nullptr;
33 
36 }
37 
39 // ------------------------------------ //
41 {
42  return ConfigurationValues.get();
43 }
44 
45 DLLEXPORT AppDef* Leviathan::AppDef::GenerateAppdefine(const std::string& engineconfigfile,
46  const std::string& gameconfig, const std::string& keyconfig,
47  std::function<void(Lock& guard, GameConfiguration* configobj)> configchecker,
48  std::function<void(Lock& guard, KeyConfiguration* keysobject)> keychecker)
49 {
50  auto tmpptr = std::make_unique<AppDef>(true);
51 
52  // We created a new one //
53  tmpptr->DeleteLog = true;
54 
55  if(!Logger::Get()) {
56 
57  std::cout << "Error: main log hasn't been created before AppDef: GenerateAppdefine"
58  << std::endl;
59  return nullptr;
60  }
61 
62  // load variables from configuration file //
63  // TODO: refactor this duplicated code
64  if(!tmpptr->ConfigurationValues->LoadVarsFromFile(engineconfigfile, Logger::Get())) {
65 
66  // Set default values //
67  tmpptr->FillDefaultEngineConf(*tmpptr->ConfigurationValues);
68 
69  // Write it to the file for next time //
71  tmpptr->ConfigurationValues->Serialize(), engineconfigfile)) {
72 
73  LOG_ERROR(
74  "AppDef: failed to write default engine config to file: " + engineconfigfile);
75  }
76  } else {
77  // Fill missing values //
78  if(tmpptr->FillDefaultEngineConf(*tmpptr->ConfigurationValues)) {
79 
80  // Save changes
82  tmpptr->ConfigurationValues->Serialize(), engineconfigfile)) {
83 
84  LOG_ERROR("AppDef: failed to write amended engine config to file: " +
85  engineconfigfile);
86  }
87  }
88  }
89 
90  // Load game configuration //
91  tmpptr->_GameConfiguration = new GameConfiguration(gameconfig);
92 
93  if(!tmpptr->_GameConfiguration->Init(configchecker)) {
94 
95  return nullptr;
96  }
97 
98  // Load key configuration //
99  tmpptr->_KeyConfiguration = new KeyConfiguration(keyconfig);
100 
101  if(!tmpptr->_KeyConfiguration->Init(keychecker)) {
102 
103  return nullptr;
104  }
105 
106  return tmpptr.release();
107 }
108 
110  std::function<void(Lock& guard, GameConfiguration* configobj)> configchecker /*= nullptr*/,
111  std::function<void(Lock& guard, KeyConfiguration* keysobject)> keychecker /*= nullptr*/)
112 {
114  delete _GameConfiguration;
115 
117 
118  if(!_GameConfiguration->Init(configchecker)) {
119 
120  LOG_ERROR("AppDef: failed to init new in-memory game configuration");
121  }
122 
123  // Load key configuration //
125 
126  if(!_KeyConfiguration->Init(keychecker)) {
127 
128  LOG_ERROR("AppDef: failed to init new in-memory key configuration");
129  }
130 }
131 
132 
133 #ifdef _WIN32
134 DLLEXPORT void Leviathan::AppDef::StoreWindowDetails(const std::string& title,
135  const bool& windowborder, HICON icon, LeviathanApplication* appvirtualptr)
136 {
137 
138 #else
140  const std::string& title, const bool& windowborder, LeviathanApplication* appvirtualptr)
141 {
142 #endif
143  // store the parameters to be used for window creation //
144  int width;
145  int height;
146  std::string fullscreen;
147  int FSAA;
148  int displayNumber;
149  bool vsync;
150  bool gamma;
151 
153  ConfigurationValues.get(), "Width", width, 1280, Logger::Get(), "Create window: ");
155  ConfigurationValues.get(), "Height", height, 720, Logger::Get(), "Create window: ");
156  ObjectFileProcessor::LoadValueFromNamedVars<std::string>(ConfigurationValues.get(),
157  "FullScreen", fullscreen, "no", Logger::Get(), "Create window: ");
158  ObjectFileProcessor::LoadValueFromNamedVars<int>(
159  ConfigurationValues.get(), "FSAA", FSAA, 4, Logger::Get(), "Create window: ");
160  ObjectFileProcessor::LoadValueFromNamedVars<int>(ConfigurationValues.get(),
161  "DisplayNumber", displayNumber, 0, Logger::Get(), "Create window: ");
162  ObjectFileProcessor::LoadValueFromNamedVars<bool>(
163  *ConfigurationValues, "Vsync", vsync, false, Logger::Get(), "Create window: ");
164  ObjectFileProcessor::LoadValueFromNamedVars<bool>(
165  *ConfigurationValues, "UseGamma", gamma, false, Logger::Get(), "Create window: ");
166 
167 #ifdef _WIN32
168  this->SetWindowDetails(WindowDataDetails(title, width, height, fullscreen, vsync, gamma,
169  displayNumber, FSAA, windowborder, icon, appvirtualptr));
170 #else
171  this->SetWindowDetails(WindowDataDetails(title, width, height, fullscreen, vsync, gamma,
172  displayNumber, FSAA, windowborder, appvirtualptr));
173 #endif
174 }
175 
177  const std::string& userreadable, const std::string& gamename,
178  const std::string& gameversion)
179 {
180  UserReadableGame = userreadable;
181  Game = gamename;
182  GameVersion = gameversion;
183  LeviathanVersion = LEVIATHAN_VERSION_ANSIS;
184 
185  return *this;
186 }
187 
189  std::string& userreadable, std::string& gamename, std::string& gameversion)
190 {
191  userreadable = UserReadableGame;
192  gamename = Game;
193  gameversion = GameVersion;
194 }
195 // ------------------------------------ //
197 {
198  bool changed = false;
199 
200  if(variables.ShouldAddValueIfNotFoundOrWrongType<bool>("Vsync")) {
201  changed = true;
202  variables.Add(std::make_shared<NamedVariableList>("Vsync", false));
203  }
204 
205  if(variables.ShouldAddValueIfNotFoundOrWrongType<bool>("UseGamma")) {
206  changed = true;
207  variables.Add(std::make_shared<NamedVariableList>("UseGamma", true));
208  }
209 
210  if(variables.ShouldAddValueIfNotFoundOrWrongType<bool>("OgreBoreMe")) {
211  changed = true;
212  variables.Add(std::make_shared<NamedVariableList>("OgreBoreMe", false));
213  }
214 
215  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("FSAA")) {
216  changed = true;
217  variables.Add(std::make_shared<NamedVariableList>("FSAA", 4));
218  }
219 
220  if(variables.ShouldAddValueIfNotFoundOrWrongType<std::string>("FullScreen")) {
221  changed = true;
222  variables.Add(
223  std::make_shared<NamedVariableList>("FullScreen", new StringBlock("no")));
224  }
225 
226  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("DisplayNumber")) {
227  changed = true;
228  variables.Add(std::make_shared<NamedVariableList>("DisplayNumber", 0));
229  }
230 
231  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("Width")) {
232  changed = true;
233  variables.Add(std::make_shared<NamedVariableList>("Width", 1280));
234  }
235 
236  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("Height")) {
237  changed = true;
238  variables.Add(std::make_shared<NamedVariableList>("Height", 720));
239  }
240 
241  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("MaxFPS")) {
242  changed = true;
243  variables.Add(std::make_shared<NamedVariableList>("MaxFPS", 120));
244  }
245 
246  if(variables.ShouldAddValueIfNotFoundOrWrongType<std::string>("AudioDevice")) {
247 
248  changed = true;
249  variables.Add(
250  std::make_shared<NamedVariableList>("AudioDevice", new StringBlock("default")));
251  }
252 
253  return changed;
254 }
255 
256 // ------------------ WindowDataDetails ------------------ //
257 #ifdef _WIN32
258 Leviathan::WindowDataDetails::WindowDataDetails(const std::string& title, const int& width,
259  const int& height, const std::string& fullscreen, bool vsync, bool gamma,
260  int displaynumber, int fsaa, const bool& windowborder, HICON icon,
261  LeviathanApplication* appvirtualptr) :
262  Title(title),
263  Width(width), Height(height), FullScreen(fullscreen), VSync(vsync), UseGamma(gamma),
264  DisplayNumber(displaynumber), FSAA(fsaa), Icon(icon)
265 {}
266 #else
267 Leviathan::WindowDataDetails::WindowDataDetails(const std::string& title, const int& width,
268  const int& height, const std::string& fullscreen, bool vsync, bool gamma,
269  int displaynumber, int fsaa, const bool& windowborder,
270  LeviathanApplication* appvirtualptr) :
271  Title(title),
272  Height(height), Width(width), FullScreen(fullscreen), VSync(vsync), UseGamma(gamma),
273  DisplayNumber(displaynumber), FSAA(fsaa)
274 {}
275 #endif
276 
278 
279 #ifdef _WIN32
280 void Leviathan::WindowDataDetails::ApplyIconToHandle(HWND hwnd) const
281 {
282 
283  // send set icon message //
284  SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)Icon);
285 }
286 #else
287 
288 #endif
KeyConfiguration * _KeyConfiguration
Definition: AppDefine.h:162
static bool LoadValueFromNamedVars(NamedVars *block, const std::string &varname, T &receiver, const T &defaultvalue, LErrorReporter *ReportError=nullptr, const std::string &errorprefix="")
#define LOG_ERROR(x)
Definition: Define.h:83
bool ShouldAddValueIfNotFoundOrWrongType(const std::string &name)
Definition: NamedVars.h:376
Holds key configuration for an application.
static DLLEXPORT AppDef * GenerateAppdefine(const std::string &engineconfigfile, const std::string &gameconfig, const std::string &keyconfig, std::function< void(Lock &guard, GameConfiguration *configobj)> configchecker, std::function< void(Lock &guard, KeyConfiguration *keysobject)> keychecker)
Definition: AppDefine.cpp:45
DLLEXPORT void StoreWindowDetails(const std::string &title, const bool &windowborder, LeviathanApplication *appvirtualptr)
Definition: AppDefine.cpp:139
virtual DLLEXPORT bool FillDefaultEngineConf(NamedVars &variables)
This is called then the engine config file isn&#39;t found to fill the options with default values...
Definition: AppDefine.cpp:196
#define LEVIATHAN_VERSION_ANSIS
Definition: Include.h:87
DLLEXPORT AppDef & SetApplicationIdentification(const std::string &userreadable, const std::string &gamename, const std::string &gameversion)
Definition: AppDefine.cpp:176
DLLEXPORT void GetGameIdentificationData(std::string &userreadable, std::string &gamename, std::string &gameversion)
Definition: AppDefine.cpp:188
DLLEXPORT void ReplaceGameAndKeyConfigInMemory(std::function< void(Lock &guard, GameConfiguration *configobj)> configchecker=nullptr, std::function< void(Lock &guard, KeyConfiguration *keysobject)> keychecker=nullptr)
Creates game and key configurations without file backing that are empty.
Definition: AppDefine.cpp:109
DLLEXPORT NamedVars * GetValues()
Definition: AppDefine.cpp:40
DLLEXPORT bool Add(std::shared_ptr< NamedVariableList > value)
Adds a value.
Definition: NamedVars.cpp:883
GameConfiguration * _GameConfiguration
Definition: AppDefine.h:161
#define SAFE_RELEASEDEL(x)
Definition: Define.h:115
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
#define DLLEXPORT
Definition: Include.h:115
DataBlock< std::string > StringBlock
Definition: DataBlock.h:386
static AppDef * Defaultconf
Definition: AppDefine.h:177
Base class for all leviathan programs.
Definition: Application.h:16
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
virtual DLLEXPORT ~AppDef()
Definition: AppDefine.cpp:28
DLLEXPORT bool Init(std::function< void(Lock &guard, GameConfiguration *configobj)> sanitycheckcallback)
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:461
DLLEXPORT AppDef(const bool &isdef=false)
Definition: AppDefine.cpp:15
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:16
DLLEXPORT bool Init(std::function< void(Lock &guard, KeyConfiguration *checkfrom)> functocheck)
Loads the defined keys from a file.