工具

杂项工具

Other.h 控制台、随机、字符串与插值

Other.h · 命名空间 plugin

打开控制台

OpenConsole

分配控制台并 freopen stdin/out,便于调试。

void OpenConsole();

随机数

RandomNumberInRange / Random

基于 mt19937Random() 为 0..999。

template <typename T = int32_t>
T RandomNumberInRange(T min, T max);

template <typename T = int32_t>
T Random();

检测按键(无边沿)

KeyPressed

当前是否按下。需要边沿时用 KeyCheck

bool KeyPressed(unsigned int keyCode);

查同目录 ASI

bool IsPluginInstalled(const TCHAR* pluginName);

窄宽字符串互转

std::wstring AtoW(std::string const& str);
std::string WtoA(std::wstring const& str);
std::wstring ToWString(const std::string& str);
std::string ToString(const std::wstring& wstr);

从文件或内存解码图

解码到 Image*,失败返回 false。成功后需 Release。见 图片与精灵

bool CreateImageFromFile(std::string const& path, Image*& img);
bool CreateImageFromMemory(std::string const& name, void* data, unsigned long size, Image*& img);

扫目录文件

不要每帧调用。

std::vector<std::string> GetAllFilesInFolder(
    std::string const& path,
    std::string const& ext,
    bool includePath = false);

格式化字符串

FormatStatic 使用轮转静态缓冲,不要长期保存返回指针。

template <typename... ArgTypes>
char* FormatStatic(const std::string& format, ArgTypes... args);

template <typename... ArgTypes>
std::string Format(const std::string& format, ArgTypes... args);

大小写与路径裁剪

std::string ToUpper(std::string const& str, int32_t offset);
std::string ToLower(std::string const& str, int32_t offset);
std::string RemoveExtension(std::string const& str);
std::string RemovePath(std::string const& str);

角度弧度

template <typename T> T DegToRad(T x);
template <typename T> T RadToDeg(T x);
// 宏 DEGTORAD / RADTODEG

限幅与插值

f 一般在 0..1。

template <typename T, typename T2, typename T3>
T Clamp(T v, T2 low, T3 high);

float InterpF(float a, float b, float f);
CVector2D InterpV2D(CVector2D a, CVector2D b, float f);

帧步归一与浮点近似

CTimer::ms_fTimeStep 归一,适合帧率相关位移。

float GetTimeStepFix();
bool IsNearlyEqualF(float a, float b, float t);

文件存在与可用内存

bool FileExists(const char* name);
uint64_t GetAvailableMemory(); // 物理内存 MB

On this page