瀏覽代碼

String : Added ::replace(char, char, bool), ::ToLower(), ::ToUpper()

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
Benjamin ‘Touky’ Huet Sam Hocevar <sam@hocevar.net> 11 年之前
父節點
當前提交
d8114e6cfb
共有 7 個文件被更改,包括 122 次插入8 次删除
  1. +1
    -1
      src/Makefile.am
  2. +1
    -0
      src/core.h
  3. +34
    -0
      src/lol/base/string.h
  4. +1
    -0
      src/lolcore.vcxproj
  5. +3
    -0
      src/lolcore.vcxproj.filters
  6. +0
    -7
      src/numeric.h
  7. +82
    -0
      src/utils.h

+ 1
- 1
src/Makefile.am 查看文件

@@ -11,7 +11,7 @@ liblolcore_a_SOURCES = \
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 \
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 \
platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \
light.cpp light.h \


+ 1
- 0
src/core.h 查看文件

@@ -117,6 +117,7 @@ static inline int isnan(float f)
#include <lol/image/all.h>
#include <lol/debug/all.h>

#include "utils.h"
#include "numeric.h"

// Static classes


+ 34
- 0
src/lol/base/string.h 查看文件

@@ -147,6 +147,40 @@ public:
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
{
using namespace std;


+ 1
- 0
src/lolcore.vcxproj 查看文件

@@ -274,6 +274,7 @@
<ClInclude Include="ticker.h" />
<ClInclude Include="tiler.h" />
<ClInclude Include="tileset.h" />
<ClInclude Include="utils.h" />
<ClInclude Include="video.h" />
<ClInclude Include="world.h" />
<ClInclude Include="worldentity.h" />


+ 3
- 0
src/lolcore.vcxproj.filters 查看文件

@@ -608,6 +608,9 @@
<ClInclude Include="input\input_internal.h">
<Filter>input</Filter>
</ClInclude>
<ClInclude Include="utils.h">
<Filter>...</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<LolFxCompile Include="gpu\emptymaterial.lolfx">


+ 0
- 7
src/numeric.h 查看文件

@@ -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));
}

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.
static inline float SmoothClamp(float &x, float a, float b, float sd)
{


+ 82
- 0
src/utils.h 查看文件

@@ -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__ */


Loading…
取消
儲存