Leviathan  0.8.0.0
Leviathan game engine
BindStandardFunctions.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "BindDefinitions.h"
3 
4 #include "Define.h"
5 
6 #include "utf8.h"
7 
8 #include <algorithm>
9 #include <cmath>
10 #include <inttypes.h>
11 
12 using namespace Leviathan;
13 // ------------------------------------ //
14 
15 // Proxies etc.
16 // ------------------------------------ //
17 int RoundProxy(double value)
18 {
19  return static_cast<int>(std::round(value));
20 }
21 
22 int RoundProxy2(float value)
23 {
24  return static_cast<int>(std::round(value));
25 }
26 
27 float SignProxy(float value)
28 {
29  return value < 0.f ? -1.f : 1.f;
30 }
31 
32 std::string CharacterToStringProxy(int32_t character)
33 {
34  try {
35 
36  std::string str;
37 
38  utf8::append(character, std::back_inserter(str));
39 
40  return str;
41 
42  } catch(const std::exception& e) {
43 
44  auto ctx = asGetActiveContext();
45  if(ctx) {
46 
47  ctx->SetException(("character code ' " + std::to_string(character) +
48  "' failed to be represented as utf8 string, error: " + e.what())
49  .c_str());
50  // The return value cannot be read by the script so it
51  // isn't any use to return an error here
52  return "";
53  } else {
54  // Not called from script
55  throw;
56  }
57  }
58 }
59 
60 // ------------------------------------ //
61 // Start of the actual bind
62 namespace Leviathan {
63 
64 bool BindMathOperations(asIScriptEngine* engine)
65 {
66 
67  // ------------------------------------ //
68  // min
69  if(engine->RegisterGlobalFunction(
70  "const float & min(const float &in a, const float &in b)",
71  asFUNCTIONPR(std::min<float>, (const float&, const float&), const float&),
72  asCALL_CDECL) < 0) {
73 
75  }
76 
77  if(engine->RegisterGlobalFunction(
78  "const int64 & min(const int64 &in a, const int64 &in b)",
79  asFUNCTIONPR(std::min<int64_t>, (const int64_t&, const int64_t&), const int64_t&),
80  asCALL_CDECL) < 0) {
81 
83  }
84 
85  // ------------------------------------ //
86  // max
87  if(engine->RegisterGlobalFunction(
88  "const float & max(const float &in a, const float &in b)",
89  asFUNCTIONPR(std::max<float>, (const float&, const float&), const float&),
90  asCALL_CDECL) < 0) {
91 
93  }
94 
95  if(engine->RegisterGlobalFunction(
96  "const int64 & max(const int64 &in a, const int64 &in b)",
97  asFUNCTIONPR(std::max<int64_t>, (const int64_t&, const int64_t&), const int64_t&),
98  asCALL_CDECL) < 0) {
99 
101  }
102 
103  // ------------------------------------ //
104  // round
105  if(engine->RegisterGlobalFunction(
106  "int round(double value)", asFUNCTION(RoundProxy), asCALL_CDECL) < 0) {
107 
109  }
110  if(engine->RegisterGlobalFunction(
111  "int round(float value)", asFUNCTION(RoundProxy2), asCALL_CDECL) < 0) {
112 
114  }
115 
116  // ------------------------------------ //
117  // sign
118  if(engine->RegisterGlobalFunction(
119  "float sign(float value)", asFUNCTION(SignProxy), asCALL_CDECL) < 0) {
120 
122  }
123 
124  // ------------------------------------ //
125  // clamp
126  if(engine->RegisterGlobalFunction("const float & clamp(const float &in value, const float "
127  "&in lower, const float &in upper)",
128  asFUNCTIONPR(
129  std::clamp<float>, (const float&, const float&, const float&), const float&),
130  asCALL_CDECL) < 0) {
131 
133  }
134 
135  // ------------------------------------ //
136  // Extra string helpers
137  if(engine->RegisterGlobalFunction("string CharacterToString(int32 character)",
138  asFUNCTION(CharacterToStringProxy), asCALL_CDECL) < 0) {
139 
141  }
142 
143 
144  return true;
145 }
146 // ------------------------------------ //
147 
148 } // namespace Leviathan
149 
150 // ------------------------------------ //
151 // Main bind function
152 bool Leviathan::BindStandardFunctions(asIScriptEngine* engine)
153 {
154  if(!BindMathOperations(engine))
155  return false;
156 
157  return true;
158 }
float SignProxy(float value)
std::string CharacterToStringProxy(int32_t character)
int RoundProxy(double value)
bool BindStandardFunctions(asIScriptEngine *engine)
Binds standard and other utilities like: std::min, std::max etc.
bool BindMathOperations(asIScriptEngine *engine)
int RoundProxy2(float value)
#define ANGELSCRIPT_REGISTERFAIL
Definition: BindHelpers.h:12
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
octet_iterator append(uint32_t cp, octet_iterator result)
The library API - functions intended to be called by the users.
Definition: checked.h:73