Blamite Game Engine - blam!  00285.12.18.20.1411.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  unsigned char r = 255;
24  unsigned char g = 255;
25  unsigned char b = 255;
26  unsigned char 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 
111  {
112  int color_int = 0xFFFFFFFF;
113 
114  char* color_int_ptr = (char*)&color_int;
115 
116  memcpy(color_int_ptr + 3, &r, 1);
117  memcpy(color_int_ptr + 2, &g, 1);
118  memcpy(color_int_ptr + 1, &b, 1);
119  memcpy(color_int_ptr, &a, 1);
120 
121  return color_int;
122  }
123 
140  std::string ToString(std::string format)
141  {
142  std::string new_string = format;
143 
144  new_string = BlamStrings::Utils::String::Replace(new_string, "{r}", std::to_string(r));
145  new_string = BlamStrings::Utils::String::Replace(new_string, "{g}", std::to_string(g));
146  new_string = BlamStrings::Utils::String::Replace(new_string, "{b}", std::to_string(b));
147  new_string = BlamStrings::Utils::String::Replace(new_string, "{a}", std::to_string(a));
148 
149  double r_float = (double)r / 255.0f;
150  double g_float = (double)g / 255.0f;
151  double b_float = (double)b / 255.0f;
152  double a_float = (double)a / 255.0f;
153 
154  new_string = BlamStrings::Utils::String::Replace(new_string, "{r>f}", std::to_string(r_float));
155  new_string = BlamStrings::Utils::String::Replace(new_string, "{g>f}", std::to_string(g_float));
156  new_string = BlamStrings::Utils::String::Replace(new_string, "{b>f}", std::to_string(b_float));
157  new_string = BlamStrings::Utils::String::Replace(new_string, "{a>f}", std::to_string(a_float));
158 
159  return new_string;
160  }
161 
167  std::string ToStringForCSS()
168  {
169  return ToString("rgba({r},{g},{b},{a>f})");
170  }
171 
181  bool FromString(std::string string)
182  {
183  std::vector<std::string> color_values = BlamStrings::Utils::String::Split(string, ",");
184 
185  if (color_values.size() == 3)
186  {
187  r = atoi(color_values.at(0).c_str());
188  g = atoi(color_values.at(1).c_str());
189  b = atoi(color_values.at(2).c_str());
190  a = 255;
191 
192  return true;
193  }
194  else if (color_values.size() == 4)
195  {
196  r = atoi(color_values.at(0).c_str());
197  g = atoi(color_values.at(1).c_str());
198  b = atoi(color_values.at(2).c_str());
199  a = atoi(color_values.at(3).c_str());
200 
201  return true;
202  }
203  else
204  {
205  return false;
206  }
207  }
208 };
209 
210 namespace Blam
211 {
215  namespace Globals
216  {
221  {
224  Ok,
228  };
229 
233  enum GvarType
234  {
241  Int,
244  };
245 
257  {
259  std::string name = "";
260  std::string info = "";
261  std::string value_raw = "";
262  bool read_only = false;
263 
265  // Additional value storage types, these are used in place of value_raw based on type. //
267 
268  bool boolean_value = false;
269  short short_value = 0;
270  long long_value = 0;
271  int int_value = 0;
272  float float_value = 0.0f;
274  };
275 
284  BLAM std::map<std::string, EngineGlobal>* GetGlobalsList();
285 
291  BLAM bool LoadGlobalsFromFile();
292 
300  BLAM std::string GetGvarTypeLabel(GvarType type);
301 
309  BLAM bool GlobalExists(std::string id);
310 
316  BLAM void RegisterGvar(EngineGlobal var);
317 
325  BLAM void RegisterGvar(std::string name, std::string value_raw, GvarType type);
326 
335  BLAM void RegisterGvar(std::string name, std::string value_raw, GvarType type, std::string info);
336 
344  BLAM EngineGlobal* GetGlobal(std::string name);
345 
354  BLAM GvarUpdateResult UpdateGlobalWrap(std::string name, std::string new_value);
355 
364  BLAM GvarUpdateResult UpdateGlobal(std::string name, std::string new_value);
365 
374  BLAM GvarUpdateResult UpdateGlobal(std::string name, bool new_value);
375 
384  BLAM GvarUpdateResult UpdateGlobal(std::string name, int new_value);
385 
394  BLAM GvarUpdateResult UpdateGlobal(std::string name, short new_value);
395 
404  BLAM GvarUpdateResult UpdateGlobal(std::string name, long new_value);
405 
414  BLAM GvarUpdateResult UpdateGlobal(std::string name, float new_value);
415 
424  BLAM GvarUpdateResult UpdateGlobal(std::string name, BlamColor new_value);
425 
433  BLAM bool* GetGlobalAsBoolean(std::string name);
434 
442  BLAM std::string* GetGlobalAsString(std::string name);
443 
451  BLAM short* GetGlobalAsShort(std::string name);
452 
460  BLAM long* GetGlobalAsLong(std::string name);
461 
469  BLAM int* GetGlobalAsInteger(std::string name);
470 
478  BLAM float* GetGlobalAsFloat(std::string name);
479 
487  BLAM BlamColor* GetGlobalAsColor(std::string name);
488  }
489 }
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:272
Blam::Globals::EngineGlobal::info
std::string info
An optional description of the global.
Definition: globals.h:260
Blam::Globals::Int
@ Int
Represents an int.
Definition: globals.h:241
Blam::Globals::EngineGlobal::color_value
BlamColor color_value
The color value of the global.
Definition: globals.h:273
BlamColor::GetColorAsInt
int GetColorAsInt()
Gets the color's value as an integer.
Definition: globals.h:110
Blam::Globals::UnknownGlobal
@ UnknownGlobal
The specified global does not exist.
Definition: globals.h:223
Blam::Globals::Long
@ Long
Represents a long.
Definition: globals.h:238
Blam::Globals::GlobalIsProtected
@ GlobalIsProtected
The specified global is protected and cannot be modified during runtime.
Definition: globals.h:227
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:400
Blam::Globals::InvalidArgs
@ InvalidArgs
The provided arguments were invalid.
Definition: globals.h:225
Blam::Globals::Real
@ Real
Unknown. Referenced within the hs_doc from Halo 2 Sapien.
Definition: globals.h:236
Blam::Globals::UpdateGlobalWrap
BLAM GvarUpdateResult UpdateGlobalWrap(std::string name, std::string new_value)
Updates a global's raw value.
Definition: globals.cpp:428
BlamColor::a
unsigned char a
The Alpha value of the color.
Definition: globals.h:26
Blam::Globals::EngineGlobal::read_only
bool read_only
Whether or not the global is protected from modification.
Definition: globals.h:262
Blam::Globals::EngineGlobal::name
std::string name
The name of the global.
Definition: globals.h:259
Blam::Globals::GvarType
GvarType
Enumerator for the type of global variable.
Definition: globals.h:233
Blam::Globals::EngineGlobal::short_value
short short_value
The short value of the global.
Definition: globals.h:269
Blam::Globals::GetGlobalAsColor
BLAM BlamColor * GetGlobalAsColor(std::string name)
Retrieves a global's value as a BlamColor.
Definition: globals.cpp:412
Blam::Globals::String
@ String
Represents a std::string.
Definition: globals.h:240
BLAM
#define BLAM
Definition: globals.h:12
Blam::Globals::EngineGlobal
Structure containing data for a game engine global.
Definition: globals.h:256
Blam::Globals::Float
@ Float
Represents a float.
Definition: globals.h:242
Blam::Globals::EngineGlobal::long_value
long long_value
The long value of the global.
Definition: globals.h:270
Blam::Globals::GvarUpdateResult
GvarUpdateResult
Enumerator for the result of a global update attempt.
Definition: globals.h:220
Blam::Globals::GetGlobalAsShort
BLAM short * GetGlobalAsShort(std::string name)
Retrieves a global's value as a short.
Definition: globals.cpp:364
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:222
BlamColor::g
unsigned char g
The Green value of the color.
Definition: globals.h:24
Blam::Globals::Object
@ Object
Unknown. Referenced within the hs_doc from Halo 2 Sapien.
Definition: globals.h:239
BlamColor::b
unsigned char b
The Blue value of the color.
Definition: globals.h:25
Blam::Globals::Short
@ Short
Represents a short.
Definition: globals.h:237
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:271
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:226
Blam::Globals::GetGlobalAsLong
BLAM long * GetGlobalAsLong(std::string name)
Retrieves a global's value as a long.
Definition: globals.cpp:376
Blam::Globals::Color
@ Color
Represents a BlamColor. See BlamColor for details.
Definition: globals.h:243
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:235
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:352
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:261
BlamColor::ToStringForCSS
std::string ToStringForCSS()
Converts the color to a string that can be used with CSS.
Definition: globals.h:167
BlamColor::FromString
bool FromString(std::string string)
Attempts to set color information from a string.
Definition: globals.h:181
BlamColor::ToString
std::string ToString(std::string format)
Converts the color value to a string in the specified format.
Definition: globals.h:140
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:258
BlamColor::r
unsigned char r
The Red value of the color.
Definition: globals.h:23
Blam::Globals::Ok
@ Ok
The global was updated successfully.
Definition: globals.h:224
Blam::Globals::GetGlobalAsBoolean
BLAM bool * GetGlobalAsBoolean(std::string name)
Retrieves a global's value as a boolean.
Definition: globals.cpp:340
Blam::Globals::EngineGlobal::boolean_value
bool boolean_value
The boolean value of the global.
Definition: globals.h:268
Blam::Globals::UpdateGlobal
BLAM GvarUpdateResult UpdateGlobal(std::string name, std::string new_value)
Updates the value of a String global.
Definition: globals.cpp:571
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:388