Added utils.h : GetRandom(Array<T>) : gets a random element in a template list with weight logic. T FindValue(char*) : Find the value of a safe enum from the given name (when GetName is implemented) swap(T&, T&) is now in here too.undefined
@@ -11,7 +11,7 @@ liblolcore_a_SOURCES = \ | |||||
map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ | map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ | ||||
tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h \ | tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h \ | ||||
world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \ | world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \ | ||||
profiler.cpp profiler.h text.cpp text.h emitter.cpp emitter.h numeric.h \ | |||||
profiler.cpp profiler.h text.cpp text.h emitter.cpp emitter.h numeric.h utils.h \ | |||||
worldentity.cpp worldentity.h gradient.cpp gradient.h gradient.lolfx \ | worldentity.cpp worldentity.h gradient.cpp gradient.h gradient.lolfx \ | ||||
platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \ | platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \ | ||||
light.cpp light.h \ | light.cpp light.h \ | ||||
@@ -117,6 +117,7 @@ static inline int isnan(float f) | |||||
#include <lol/image/all.h> | #include <lol/image/all.h> | ||||
#include <lol/debug/all.h> | #include <lol/debug/all.h> | ||||
#include "utils.h" | |||||
#include "numeric.h" | #include "numeric.h" | ||||
// Static classes | // Static classes | ||||
@@ -147,6 +147,40 @@ public: | |||||
return tmp ? (int)(intptr_t)(tmp - C()) : -1; | return tmp ? (int)(intptr_t)(tmp - C()) : -1; | ||||
} | } | ||||
int Replace(const char old_token, const char new_token, bool all_occurence=false) | |||||
{ | |||||
using namespace std; | |||||
int res = 0; | |||||
char *tmp = NULL; | |||||
while ((tmp = strrchr(C(), old_token))) | |||||
{ | |||||
*tmp = new_token; | |||||
res++; | |||||
if (!all_occurence) | |||||
break; | |||||
} | |||||
return res; | |||||
} | |||||
inline String& ToLower() | |||||
{ | |||||
char* p = C(); | |||||
for (int i = 0; i < Count(); ++i) | |||||
if ('A' <= p[i] && p[i] <= 'Z') | |||||
p[i] += 'a' - 'A'; | |||||
return *this; | |||||
} | |||||
inline String& ToUpper() | |||||
{ | |||||
char* p = C(); | |||||
for (int i = 0; i < Count(); ++i) | |||||
if ('a' <= p[i] && p[i] <= 'z') | |||||
p[i] += 'A' - 'a'; | |||||
return *this; | |||||
} | |||||
int LastIndexOf(char const* token) const | int LastIndexOf(char const* token) const | ||||
{ | { | ||||
using namespace std; | using namespace std; | ||||
@@ -274,6 +274,7 @@ | |||||
<ClInclude Include="ticker.h" /> | <ClInclude Include="ticker.h" /> | ||||
<ClInclude Include="tiler.h" /> | <ClInclude Include="tiler.h" /> | ||||
<ClInclude Include="tileset.h" /> | <ClInclude Include="tileset.h" /> | ||||
<ClInclude Include="utils.h" /> | |||||
<ClInclude Include="video.h" /> | <ClInclude Include="video.h" /> | ||||
<ClInclude Include="world.h" /> | <ClInclude Include="world.h" /> | ||||
<ClInclude Include="worldentity.h" /> | <ClInclude Include="worldentity.h" /> | ||||
@@ -608,6 +608,9 @@ | |||||
<ClInclude Include="input\input_internal.h"> | <ClInclude Include="input\input_internal.h"> | ||||
<Filter>input</Filter> | <Filter>input</Filter> | ||||
</ClInclude> | </ClInclude> | ||||
<ClInclude Include="utils.h"> | |||||
<Filter>...</Filter> | |||||
</ClInclude> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<LolFxCompile Include="gpu\emptymaterial.lolfx"> | <LolFxCompile Include="gpu\emptymaterial.lolfx"> | ||||
@@ -43,13 +43,6 @@ template <typename T1, typename T2, typename Tf> static inline T1 damp(const T1 | |||||
return lol::lerp(a, b, dt / (dt + x)); | return lol::lerp(a, b, dt / (dt + x)); | ||||
} | } | ||||
template< class T > inline void Swap( T& A, T& B ) | |||||
{ | |||||
const T Temp = A; | |||||
A = B; | |||||
B = Temp; | |||||
} | |||||
//SmoothClamp clamps x in [a - sd, b + sd] and returns a value in [a, b] that is smoothed at the borders. | //SmoothClamp clamps x in [a - sd, b + sd] and returns a value in [a, b] that is smoothed at the borders. | ||||
static inline float SmoothClamp(float &x, float a, float b, float sd) | static inline float SmoothClamp(float &x, float a, float b, float sd) | ||||
{ | { | ||||
@@ -0,0 +1,82 @@ | |||||
// | |||||
// Lol Engine | |||||
// | |||||
// Copyright: (c) 2012-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com> | |||||
// (c) 2012-2013 Sam Hocevar <sam@hocevar.net> | |||||
// This program is free software; you can redistribute it and/or | |||||
// modify it under the terms of the Do What The Fuck You Want To | |||||
// Public License, Version 2, as published by Sam Hocevar. See | |||||
// http://www.wtfpl.net/ for more details. | |||||
// | |||||
// | |||||
// Various basic convenience functions | |||||
// ------------------ | |||||
// | |||||
#if !defined __LOL_UTILS_H__ | |||||
#define __LOL_UTILS_H__ | |||||
#include "lol\base\string.h" | |||||
namespace lol | |||||
{ | |||||
//Swaps two given values. | |||||
template< class T > inline void Swap( T& A, T& B ) | |||||
{ | |||||
const T Temp = A; | |||||
A = B; | |||||
B = Temp; | |||||
} | |||||
//TODO: random struct | |||||
//Gets a random Element from the given array<T>, considering you have implemented what follows : | |||||
//NEEDS : float T::m_weight; //if m_weight is 0, it automatically assumes that this step is ignored. | |||||
template< class T > inline int GetRandom(Array<T> src) | |||||
{ | |||||
float r_total = 0.f; | |||||
float r_alpha = rand(1.f); | |||||
float r_value = 0.f; | |||||
int r_j = 0; | |||||
int i = 0; | |||||
for (; i < src.Count(); ++i) | |||||
{ | |||||
T& tmp = src[i]; | |||||
if (tmp.m_weight > .0f) | |||||
{ | |||||
r_total += tmp.m_weight; | |||||
float r_tmp = r_alpha * r_total; | |||||
while (r_tmp > r_value + src[r_j].m_weight && r_j < i) | |||||
{ | |||||
r_value += src[r_j].m_weight; | |||||
r_j++; | |||||
} | |||||
} | |||||
} | |||||
return (r_total > .0f)?(r_j):(-1); | |||||
} | |||||
//Gets the value for the given safe-enum, considering you have implemented what follows : | |||||
//NEEDS : Contructor(Value) | |||||
//NEEDS : static char const *GetName(Value v) | |||||
//NEEDS : T::Max | |||||
template< class T > inline T FindValue(const char* name) | |||||
{ | |||||
String n = name; | |||||
n.ToLower(); | |||||
for (int i = 0; i < T::Max; ++i) | |||||
{ | |||||
String s = T::GetName(T::Value(i)); | |||||
if (s.ToLower().IndexOf(n.C()) > -1) | |||||
return T(T::Value(i)); | |||||
} | |||||
return T::Max; | |||||
} | |||||
} /* namespace lol */ | |||||
#endif /* __LOL_UTILS_H__ */ | |||||