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 
152  ConfigurationValues.get(), "Width", width, 1280, Logger::Get(), "Create window: ");
154  ConfigurationValues.get(), "Height", height, 720, Logger::Get(), "Create window: ");
155  ObjectFileProcessor::LoadValueFromNamedVars<std::string>(ConfigurationValues.get(),
156  "FullScreen", fullscreen, "no", Logger::Get(), "Create window: ");
157  ObjectFileProcessor::LoadValueFromNamedVars<int>(
158  ConfigurationValues.get(), "WindowMultiSampleCount", FSAA, 1);
159  ObjectFileProcessor::LoadValueFromNamedVars<int>(ConfigurationValues.get(),
160  "DisplayNumber", displayNumber, 0, Logger::Get(), "Create window: ");
161  ObjectFileProcessor::LoadValueFromNamedVars<bool>(
162  *ConfigurationValues, "Vsync", vsync, false, Logger::Get(), "Create window: ");
163 
164 #ifdef _WIN32
165  this->SetWindowDetails(WindowDataDetails(title, width, height, fullscreen, vsync,
166  displayNumber, FSAA, windowborder, icon, appvirtualptr));
167 #else
168  this->SetWindowDetails(WindowDataDetails(title, width, height, fullscreen, vsync,
169  displayNumber, FSAA, windowborder, appvirtualptr));
170 #endif
171 }
172 
174  const std::string& userreadable, const std::string& gamename,
175  const std::string& gameversion)
176 {
177  UserReadableGame = userreadable;
178  Game = gamename;
179  GameVersion = gameversion;
180  LeviathanVersion = LEVIATHAN_VERSION_ANSIS;
181 
182  return *this;
183 }
184 
186  std::string& userreadable, std::string& gamename, std::string& gameversion)
187 {
188  userreadable = UserReadableGame;
189  gamename = Game;
190  gameversion = GameVersion;
191 }
192 // ------------------------------------ //
194 {
195  bool changed = false;
196 
197  if(variables.ShouldAddValueIfNotFoundOrWrongType<bool>("Vsync")) {
198  changed = true;
199  variables.Add(std::make_shared<NamedVariableList>("Vsync", false));
200  }
201 
202  if(variables.ShouldAddValueIfNotFoundOrWrongType<std::string>("FullScreen")) {
203  changed = true;
204  variables.Add(
205  std::make_shared<NamedVariableList>("FullScreen", new StringBlock("no")));
206  }
207 
208  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("DisplayNumber")) {
209  changed = true;
210  variables.Add(std::make_shared<NamedVariableList>("DisplayNumber", 0));
211  }
212 
213  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("Width")) {
214  changed = true;
215  variables.Add(std::make_shared<NamedVariableList>("Width", 1280));
216  }
217 
218  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("Height")) {
219  changed = true;
220  variables.Add(std::make_shared<NamedVariableList>("Height", 720));
221  }
222 
223  if(variables.ShouldAddValueIfNotFoundOrWrongType<int>("MaxFPS")) {
224  changed = true;
225  variables.Add(std::make_shared<NamedVariableList>("MaxFPS", 144));
226  }
227 
228  if(variables.ShouldAddValueIfNotFoundOrWrongType<std::string>("AudioDevice")) {
229 
230  changed = true;
231  variables.Add(
232  std::make_shared<NamedVariableList>("AudioDevice", new StringBlock("default")));
233  }
234 
235  return changed;
236 }
237 
238 // ------------------ WindowDataDetails ------------------ //
239 #ifdef _WIN32
240 Leviathan::WindowDataDetails::WindowDataDetails(const std::string& title, const int& width,
241  const int& height, const std::string& fullscreen, bool vsync, int displaynumber, int fsaa,
242  const bool& windowborder, HICON icon, LeviathanApplication* appvirtualptr) :
243  Title(title),
244  Width(width), Height(height), FullScreen(fullscreen), VSync(vsync),
245  DisplayNumber(displaynumber), FSAA(fsaa), Icon(icon)
246 {}
247 #else
248 Leviathan::WindowDataDetails::WindowDataDetails(const std::string& title, const int& width,
249  const int& height, const std::string& fullscreen, bool vsync, int displaynumber, int fsaa,
250  const bool& windowborder, LeviathanApplication* appvirtualptr) :
251  Title(title),
252  Height(height), Width(width), FullScreen(fullscreen), VSync(vsync),
253  DisplayNumber(displaynumber), FSAA(fsaa)
254 {}
255 #endif
256 
258 
259 #ifdef _WIN32
260 void Leviathan::WindowDataDetails::ApplyIconToHandle(HWND hwnd) const
261 {
262 
263  // send set icon message //
264  SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)Icon);
265 }
266 #else
267 
268 #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:94
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't found to fill the options with default values.
Definition: AppDefine.cpp:193
#define LEVIATHAN_VERSION_ANSIS
Definition: Include.h:56
DLLEXPORT AppDef & SetApplicationIdentification(const std::string &userreadable, const std::string &gamename, const std::string &gameversion)
Definition: AppDefine.cpp:173
DLLEXPORT void GetGameIdentificationData(std::string &userreadable, std::string &gamename, std::string &gameversion)
Definition: AppDefine.cpp:185
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:143
static DLLEXPORT Logger * Get()
Definition: Logger.cpp:106
#define DLLEXPORT
Definition: Include.h:84
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:456
DLLEXPORT AppDef(const bool &isdef=false)
Definition: AppDefine.cpp:15
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18
DLLEXPORT bool Init(std::function< void(Lock &guard, KeyConfiguration *checkfrom)> functocheck)
Loads the defined keys from a file.