Blamite Game Engine - blam!  00272.10.26.20.0001.blamite
The core library for the Blamite Game Engine.
globals.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <map>
5 #include <vector>
6 
7 #include <Strings/components/utils/string/string.h>
8 
9 #define GVARS_FILE "./globals.xml"
10 
11 #ifndef BLAM
12 #define BLAM
13 #endif
14 
20 class BlamColor
21 {
22 public:
23  short r = 255;
24  short g = 255;
25  short b = 255;
26  short a = 255;
27 
29  {
30  r = 255;
31  g = 255;
32  b = 255;
33  a = 255;
34  }
35 
36  BlamColor(short _r, short _g, short _b)
37  {
38  r = _r;
39  g = _g;
40  b = _b;
41  a = 255;
42  }
43 
44  BlamColor(short _r, short _g, short _b, short _a)
45  {
46  r = _r;
47  g = _g;
48  b = _b;
49  a = _a;
50  }
51 
57  std::string ToString()
58  {
59  return std::string(std::to_string(r) + "," + std::to_string(g) + "," + std::to_string(b) + "," + std::to_string(a));
60  }
61 
67  float GetRedAsFloat()
68  {
69  return r / 255.0f;
70  }
71 
78  {
79  return g / 255.0f;
80  }
81 
88  {
89  return b / 255.0f;
90  }
91 
98  {
99  return a / 255.0f;
100  }
101 
118  std::string ToString(std::string format)
119  {
120  std::string new_string = format;
121 
122  new_string = BlamStrings::Utils::String::Replace(new_string, "{r}", std::to_string(r));
123  new_string = BlamStrings::Utils::String::Replace(new_string, "{g}", std::to_string(g));
124  new_string = BlamStrings::Utils::String::Replace(new_string, "{b}", std::to_string(b));
125  new_string = BlamStrings::Utils::String::Replace(new_string, "{a}", std::to_string(a));
126 
127  double r_float = (double)r / 255.0f;
128  double g_float = (double)g / 255.0f;
129  double b_float = (double)b / 255.0f;
130  double a_float = (double)a / 255.0f;
131 
132  new_string = BlamStrings::Utils::String::Replace(new_string, "{r>f}", std::to_string(r_float));
133  new_string = BlamStrings::Utils::String::Replace(new_string, "{g>f}", std::to_string(g_float));
134  new_string = BlamStrings::Utils::String::Replace(new_string, "{b>f}", std::to_string(b_float));
135  new_string = BlamStrings::Utils::String::Replace(new_string, "{a>f}", std::to_string(a_float));
136 
137  return new_string;
138  }
139 
145  std::string ToStringForCSS()
146  {
147  return ToString("rgba({r},{g},{b},{a>f})");
148  }
149 
159  bool FromString(std::string string)
160  {
161  std::vector<std::string> color_values = BlamStrings::Utils::String::Split(string, ",");
162 
163  if (color_values.size() == 3)
164  {
165  r = atoi(color_values.at(0).c_str());
166  g = atoi(color_values.at(1).c_str());
167  b = atoi(color_values.at(2).c_str());
168  a = 255;
169 
170  return true;
171  }
172  else if (color_values.size() == 4)
173  {
174  r = atoi(color_values.at(0).c_str());
175  g = atoi(color_values.at(1).c_str());
176  b = atoi(color_values.at(2).c_str());
177  a = atoi(color_values.at(3).c_str());
178 
179  return true;
180  }
181  else
182  {
183  return false;
184  }
185  }
186 };
187 
188 namespace Blam
189 {
193  namespace Globals
194  {
199  {
202  Ok,
206  };
207 
211  enum GvarType
212  {
219  Int,
222  };
223 
235  {
237  std::string name = "";
238  std::string info = "";
239  std::string value_raw = "";
240  bool read_only = false;
241 
243  // Additional value storage types, these are used in place of value_raw based on type. //
245 
246  bool boolean_value = false;
247  short short_value = 0;
248  long long_value = 0;
249  int int_value = 0;
250  float float_value = 0.0f;
252  };
253 
262  BLAM std::map<std::string, EngineGlobal>* GetGlobalsList();
263 
269  BLAM bool LoadGlobalsFromFile();
270 
278  BLAM std::string GetGvarTypeLabel(GvarType type);
279 
287  BLAM bool GlobalExists(std::string id);
288 
294  BLAM void RegisterGvar(EngineGlobal var);
295 
303  BLAM void RegisterGvar(std::string name, std::string value_raw, GvarType type);
304 
313  BLAM void RegisterGvar(std::string name, std::string value_raw, GvarType type, std::string info);
314 
322  BLAM EngineGlobal* GetGlobal(std::string name);
323 
332  BLAM GvarUpdateResult UpdateGlobalWrap(std::string name, std::string new_value);
333 
342  BLAM GvarUpdateResult UpdateGlobal(std::string name, std::string new_value);
343 
352  BLAM GvarUpdateResult UpdateGlobal(std::string name, bool new_value);
353 
362  BLAM GvarUpdateResult UpdateGlobal(std::string name, int new_value);
363 
372  BLAM GvarUpdateResult UpdateGlobal(std::string name, short new_value);
373 
382  BLAM GvarUpdateResult UpdateGlobal(std::string name, long new_value);
383 
392  BLAM GvarUpdateResult UpdateGlobal(std::string name, float new_value);
393 
402  BLAM GvarUpdateResult UpdateGlobal(std::string name, BlamColor new_value);
403 
411  BLAM bool* GetGlobalAsBoolean(std::string name);
412 
420  BLAM std::string* GetGlobalAsString(std::string name);
421 
429  BLAM short* GetGlobalAsShort(std::string name);
430 
438  BLAM long* GetGlobalAsLong(std::string name);
439 
447  BLAM int* GetGlobalAsInteger(std::string name);
448 
456  BLAM float* GetGlobalAsFloat(std::string name);
457 
465  BLAM BlamColor* GetGlobalAsColor(std::string name);
466  }
467 }
Blam
Namespace surrounding all major engine components.
Definition: blam_api.h:18
BlamColor::BlamColor
BlamColor()
Definition: globals.h:28
Blam::Globals::RegisterGvar
BLAM void RegisterGvar(EngineGlobal var)
Registers a new engine global.
Definition: globals.cpp:65
Blam::Globals::GetGlobal
BLAM EngineGlobal * GetGlobal(std::string name)
Retrieves a global with the specified ID.
Definition: globals.cpp:193
Blam::Globals::GlobalExists
BLAM bool GlobalExists(std::string id)
Determines whether or not a global exists.
Definition: globals.cpp:27
Blam::Globals::EngineGlobal::float_value
float float_value
The float value of the global.
Definition: globals.h:250
Blam::Globals::EngineGlobal::info
std::string info
An optional description of the global.
Definition: globals.h:238
Blam::Globals::Int
@ Int
Represents an int.
Definition: globals.h:219
Blam::Globals::EngineGlobal::color_value
BlamColor color_value
The color value of the global.
Definition: globals.h:251
BlamColor::g
short g
The Green value of the color.
Definition: globals.h:24
Blam::Globals::UnknownGlobal
@ UnknownGlobal
The specified global does not exist.
Definition: globals.h:201
Blam::Globals::Long
@ Long
Represents a long.
Definition: globals.h:216
Blam::Globals::GlobalIsProtected
@ GlobalIsProtected
The specified global is protected and cannot be modified during runtime.
Definition: globals.h:205
Blam::Globals::LoadGlobalsFromFile
BLAM bool LoadGlobalsFromFile()
Loads any globals from GVARS_FILE.
Definition: globals.cpp:206
Blam::Globals::GetGlobalAsFloat
BLAM float * GetGlobalAsFloat(std::string name)
Retrieves a global's value as a float.
Definition: globals.cpp:399
Blam::Globals::InvalidArgs
@ InvalidArgs
The provided arguments were invalid.
Definition: globals.h:203
Blam::Globals::Real
@ Real
Unknown. Referenced within the hs_doc from Halo 2 Sapien.
Definition: globals.h:214
Blam::Globals::UpdateGlobalWrap
BLAM GvarUpdateResult UpdateGlobalWrap(std::string name, std::string new_value)
Updates a global's raw value.
Definition: globals.cpp:427
Blam::Globals::EngineGlobal::read_only
bool read_only
Whether or not the global is protected from modification.
Definition: globals.h:240
Blam::Globals::EngineGlobal::name
std::string name
The name of the global.
Definition: globals.h:237
Blam::Globals::GvarType
GvarType
Enumerator for the type of global variable.
Definition: globals.h:211
Blam::Globals::EngineGlobal::short_value
short short_value
The short value of the global.
Definition: globals.h:247
Blam::Globals::GetGlobalAsColor
BLAM BlamColor * GetGlobalAsColor(std::string name)
Retrieves a global's value as a BlamColor.
Definition: globals.cpp:411
BlamColor::a
short a
The Alpha value of the color.
Definition: globals.h:26
Blam::Globals::String
@ String
Represents a std::string.
Definition: globals.h:218
BlamColor::r
short r
The Red value of the color.
Definition: globals.h:23
BLAM
#define BLAM
Definition: globals.h:12
Blam::Globals::EngineGlobal
Structure containing data for a game engine global.
Definition: globals.h:234
Blam::Globals::Float
@ Float
Represents a float.
Definition: globals.h:220
Blam::Globals::EngineGlobal::long_value
long long_value
The long value of the global.
Definition: globals.h:248
Blam::Globals::GvarUpdateResult
GvarUpdateResult
Enumerator for the result of a global update attempt.
Definition: globals.h:198
Blam::Globals::GetGlobalAsShort
BLAM short * GetGlobalAsShort(std::string name)
Retrieves a global's value as a short.
Definition: globals.cpp:363
BlamColor::BlamColor
BlamColor(short _r, short _g, short _b)
Definition: globals.h:36
Blam::Globals::InvalidType
@ InvalidType
The provided value was of an invalid type.
Definition: globals.h:200
BlamColor::b
short b
The Blue value of the color.
Definition: globals.h:25
Blam::Globals::Object
@ Object
Unknown. Referenced within the hs_doc from Halo 2 Sapien.
Definition: globals.h:217
Blam::Globals::Short
@ Short
Represents a short.
Definition: globals.h:215
BlamColor::GetBlueAsFloat
float GetBlueAsFloat()
Gets the color's red value as a float, between 0 and 1.
Definition: globals.h:87
Blam::Globals::EngineGlobal::int_value
int int_value
The int value of the global.
Definition: globals.h:249
Blam::Globals::GetGlobalsList
BLAM std::map< std::string, EngineGlobal > * GetGlobalsList()
Retrieves the list of loaded globals.
Definition: globals.cpp:22
Blam::Globals::OutOfBounds
@ OutOfBounds
The provided value was too small or too large for the globals' data type.
Definition: globals.h:204
Blam::Globals::GetGlobalAsLong
BLAM long * GetGlobalAsLong(std::string name)
Retrieves a global's value as a long.
Definition: globals.cpp:375
Blam::Globals::Color
@ Color
Represents a BlamColor. See BlamColor for details.
Definition: globals.h:221
BlamColor::GetRedAsFloat
float GetRedAsFloat()
Gets the color's red value as a float, between 0 and 1.
Definition: globals.h:67
Blam::Globals::Boolean
@ Boolean
Represents a boolean. Can be true or false.
Definition: globals.h:213
BlamColor::GetGreenAsFloat
float GetGreenAsFloat()
Gets the color's red value as a float, between 0 and 1.
Definition: globals.h:77
Blam::Globals::GetGlobalAsString
BLAM std::string * GetGlobalAsString(std::string name)
Retrieves a global's value as a string.
Definition: globals.cpp:351
BlamColor::ToString
std::string ToString()
Converts the color value to a string.
Definition: globals.h:57
Blam::Globals::EngineGlobal::value_raw
std::string value_raw
The raw value of the global as a string.
Definition: globals.h:239
BlamColor::ToStringForCSS
std::string ToStringForCSS()
Converts the color to a string that can be used with CSS.
Definition: globals.h:145
BlamColor::FromString
bool FromString(std::string string)
Attempts to set color information from a string.
Definition: globals.h:159
BlamColor::ToString
std::string ToString(std::string format)
Converts the color value to a string in the specified format.
Definition: globals.h:118
BlamColor::BlamColor
BlamColor(short _r, short _g, short _b, short _a)
Definition: globals.h:44
BlamColor::GetAlphaAsFloat
float GetAlphaAsFloat()
Gets the color's red value as a float, between 0 and 1.
Definition: globals.h:97
BlamColor
Structure representing a color.
Definition: globals.h:20
Blam::Globals::EngineGlobal::type
GvarType type
The type of the global.
Definition: globals.h:236
Blam::Globals::Ok
@ Ok
The global was updated successfully.
Definition: globals.h:202
Blam::Globals::GetGlobalAsBoolean
BLAM bool * GetGlobalAsBoolean(std::string name)
Retrieves a global's value as a boolean.
Definition: globals.cpp:339
Blam::Globals::EngineGlobal::boolean_value
bool boolean_value
The boolean value of the global.
Definition: globals.h:246
Blam::Globals::UpdateGlobal
BLAM GvarUpdateResult UpdateGlobal(std::string name, std::string new_value)
Updates the value of a String global.
Definition: globals.cpp:570
Blam::Globals::GetGvarTypeLabel
BLAM std::string GetGvarTypeLabel(GvarType type)
Retrieves a string representation of a global's type, for use in UI.
Definition: globals.cpp:40
Blam::Globals::GetGlobalAsInteger
BLAM int * GetGlobalAsInteger(std::string name)
Retrieves a global's value as an int.
Definition: globals.cpp:387