Blamite Game Engine - blam!  00263.10.17.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 
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 
78  std::string ToString(std::string format)
79  {
80  std::string new_string = format;
81 
82  new_string = Blam::Utils::String::Replace(new_string, "{r}", std::to_string(r));
83  new_string = Blam::Utils::String::Replace(new_string, "{g}", std::to_string(g));
84  new_string = Blam::Utils::String::Replace(new_string, "{b}", std::to_string(b));
85  new_string = Blam::Utils::String::Replace(new_string, "{a}", std::to_string(a));
86 
87  double r_float = (double)r / 255.0f;
88  double g_float = (double)g / 255.0f;
89  double b_float = (double)b / 255.0f;
90  double a_float = (double)a / 255.0f;
91 
92  new_string = Blam::Utils::String::Replace(new_string, "{r>f}", std::to_string(r_float));
93  new_string = Blam::Utils::String::Replace(new_string, "{g>f}", std::to_string(g_float));
94  new_string = Blam::Utils::String::Replace(new_string, "{b>f}", std::to_string(b_float));
95  new_string = Blam::Utils::String::Replace(new_string, "{a>f}", std::to_string(a_float));
96 
97  return new_string;
98  }
99 
105  std::string ToStringForCSS()
106  {
107  return ToString("rgba({r},{g},{b},{a>f})");
108  }
109 
119  bool FromString(std::string string)
120  {
121  std::vector<std::string> color_values = Blam::Utils::String::Split(string, ",");
122 
123  if (color_values.size() == 3)
124  {
125  r = atoi(color_values.at(0).c_str());
126  g = atoi(color_values.at(1).c_str());
127  b = atoi(color_values.at(2).c_str());
128  a = 255;
129 
130  return true;
131  }
132  else if (color_values.size() == 4)
133  {
134  r = atoi(color_values.at(0).c_str());
135  g = atoi(color_values.at(1).c_str());
136  b = atoi(color_values.at(2).c_str());
137  a = atoi(color_values.at(3).c_str());
138 
139  return true;
140  }
141  else
142  {
143  return false;
144  }
145  }
146 };
147 
148 namespace Blam
149 {
153  namespace Globals
154  {
159  {
162  Ok,
166  };
167 
171  enum GvarType
172  {
179  Int,
182  };
183 
195  {
197  std::string name = "";
198  std::string info = "";
199  std::string value_raw = "";
200  bool read_only = false;
201 
203  // Additional value storage types, these are used in place of value_raw based on type. //
205 
206  bool boolean_value = false;
207  short short_value = 0;
208  long long_value = 0;
209  int int_value = 0;
210  float float_value = 0.0f;
212  };
213 
222  BLAM std::map<std::string, EngineGlobal>* GetGlobalsList();
223 
229  BLAM bool LoadGlobalsFromFile();
230 
238  BLAM std::string GetGvarTypeLabel(GvarType type);
239 
247  BLAM bool GlobalExists(std::string id);
248 
254  BLAM void RegisterGvar(EngineGlobal var);
255 
263  BLAM void RegisterGvar(std::string name, std::string value_raw, GvarType type);
264 
273  BLAM void RegisterGvar(std::string name, std::string value_raw, GvarType type, std::string info);
274 
282  BLAM EngineGlobal* GetGlobal(std::string name);
283 
292  BLAM GvarUpdateResult UpdateGlobalWrap(std::string name, std::string new_value);
293 
302  BLAM GvarUpdateResult UpdateGlobal(std::string name, std::string new_value);
303 
312  BLAM GvarUpdateResult UpdateGlobal(std::string name, bool new_value);
313 
322  BLAM GvarUpdateResult UpdateGlobal(std::string name, int new_value);
323 
332  BLAM GvarUpdateResult UpdateGlobal(std::string name, short new_value);
333 
342  BLAM GvarUpdateResult UpdateGlobal(std::string name, long new_value);
343 
352  BLAM GvarUpdateResult UpdateGlobal(std::string name, float new_value);
353 
362  BLAM GvarUpdateResult UpdateGlobal(std::string name, BlamColor new_value);
363 
371  BLAM bool* GetGlobalAsBoolean(std::string name);
372 
380  BLAM std::string* GetGlobalAsString(std::string name);
381 
389  BLAM short* GetGlobalAsShort(std::string name);
390 
398  BLAM long* GetGlobalAsLong(std::string name);
399 
407  BLAM int* GetGlobalAsInteger(std::string name);
408 
416  BLAM float* GetGlobalAsFloat(std::string name);
417 
425  BLAM BlamColor* GetGlobalAsColor(std::string name);
426  }
427 }
Blam
Namespace surrounding all major engine components.
Definition: blam_api.h:17
Blam::Utils::String::Replace
BLAM std::string Replace(std::string orig, std::string to_replace, std::string replace_with)
Replaces part of a string with another string.
Definition: string.cpp:67
BlamColor::BlamColor
BlamColor()
Definition: globals.h:28
Blam::Utils::String::Split
BLAM std::vector< std::string > Split(std::string string, std::string splitter)
Splits a string around any instance of a substring.
Definition: string.cpp:87
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:210
Blam::Globals::EngineGlobal::info
std::string info
An optional description of the global.
Definition: globals.h:198
Blam::Globals::Int
@ Int
Represents an int.
Definition: globals.h:179
Blam::Globals::EngineGlobal::color_value
BlamColor color_value
The color value of the global.
Definition: globals.h:211
BlamColor::g
short g
The Green value of the color.
Definition: globals.h:24
string.h
Blam::Globals::UnknownGlobal
@ UnknownGlobal
The specified global does not exist.
Definition: globals.h:161
Blam::Globals::Long
@ Long
Represents a long.
Definition: globals.h:176
Blam::Globals::GlobalIsProtected
@ GlobalIsProtected
The specified global is protected and cannot be modified during runtime.
Definition: globals.h:165
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:163
Blam::Globals::Real
@ Real
Unknown. Referenced within the hs_doc from Halo 2 Sapien.
Definition: globals.h:174
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:200
Blam::Globals::EngineGlobal::name
std::string name
The name of the global.
Definition: globals.h:197
Blam::Globals::GvarType
GvarType
Enumerator for the type of global variable.
Definition: globals.h:171
Blam::Globals::EngineGlobal::short_value
short short_value
The short value of the global.
Definition: globals.h:207
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:178
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:194
Blam::Globals::Float
@ Float
Represents a float.
Definition: globals.h:180
Blam::Globals::EngineGlobal::long_value
long long_value
The long value of the global.
Definition: globals.h:208
Blam::Globals::GvarUpdateResult
GvarUpdateResult
Enumerator for the result of a global update attempt.
Definition: globals.h:158
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:160
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:177
Blam::Globals::Short
@ Short
Represents a short.
Definition: globals.h:175
Blam::Globals::EngineGlobal::int_value
int int_value
The int value of the global.
Definition: globals.h:209
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:164
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:181
Blam::Globals::Boolean
@ Boolean
Represents a boolean. Can be true or false.
Definition: globals.h:173
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:199
BlamColor::ToStringForCSS
std::string ToStringForCSS()
Converts the color to a string that can be used with CSS.
Definition: globals.h:105
BlamColor::FromString
bool FromString(std::string string)
Attempts to set color information from a string.
Definition: globals.h:119
BlamColor::ToString
std::string ToString(std::string format)
Converts the color value to a string in the specified format.
Definition: globals.h:78
BlamColor::BlamColor
BlamColor(short _r, short _g, short _b, short _a)
Definition: globals.h:44
BlamColor
Structure representing a color.
Definition: globals.h:20
Blam::Globals::EngineGlobal::type
GvarType type
The type of the global.
Definition: globals.h:196
Blam::Globals::Ok
@ Ok
The global was updated successfully.
Definition: globals.h:162
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:206
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