07_C++精灵库之functools.h头文件源代码(2026年1月15日版)

#ifndef FUNCTOOLS_H
#define FUNCTOOLS_H

#include <string>
#include <vector>
#include <SDL2/SDL.h> 
#include "polygon_offset.h"
#include "coloradd.h"
#include <map>
#include <shlobj.h>
#include <fstream>  // 添加这一行以使用 ofstream


// 函数声明
float deg_to_rad(float deg);

//由于RtlGetVersion不是标准的SDK部分,需要手动定义其原型
typedef LONG (NTAPI *RTLGETVERSION)(PRTL_OSVERSIONINFOW);
std::string getWindowsVersion();

std::string ConvertToUtf8(const std::string& ansiPath);

std::string getWindowsDirectory() ;

std::string rgb2hex(Uint8 r, Uint8 g, Uint8 b);
bool hexColorToRGB(const std::string& hexStr, Uint8& r, Uint8& g, Uint8& b);

SDL_Color color_string2_SDL_Color(std::string penyanse);

// 可以添加其他常用函数的声明
bool isPointInPolygon(float x, float y, const std::vector<Point>& points);
void getBoundingBox(const std::vector<Point>& points,int& minX, int& minY, int& maxX, int& maxY);
bool intersectEdge(float y, const Point& v0, const Point& v1, float& x) ;

void drawFilledPolygon(SDL_Renderer* renderer,const std::vector<Point>& points,
                       Uint8 r, Uint8 g, Uint8 b, Uint8 a) ;
void drawFilledPolygon_slow(SDL_Renderer* renderer,
                       const std::vector<Point>& points,
                       Uint8 r, Uint8 g, Uint8 b, Uint8 a) ;    
                       
bool isPointInPolygon_Winding(float x, float y, const std::vector<Point>& points);
float isLeft(float x0, float y0, float x1, float y1, float x, float y);
SDL_Color decomposeRGBA(Uint32 color);

void floodfill(int x,int y,Color color); //在x,y进行洪水填充
void floodfill(int x,int y,SDL_Color fc); 
void floodfill(int x,int y,std::string newColor); //在x,y进行洪水填充 
void _floodfill(Point cur, SDL_Color newCol);
std::string replaceBackslashes(const std::string& path);

Point rotatePoint(const Point& point, const Point& center, float angle);
SDL_Rect rotatedRect(const SDL_Rect& rect, float angle);

    
// 工具函数实现,模版函数要放在头文件里 
template<typename T>
T clamppx(T value, T min, T max)  {
    return (value < min) ? min : (value > max) ? max : value;
}

template<typename T>
T max3(T a, T b, T c)  {
    return std::max(std::max(a, b), c);
}

template<typename T>
T min3(T a, T b, T c)  {
    return std::min(std::min(a, b), c);
}
        
char* GbkToUtf8(const char* gbkStr);
char* Utf8ToGbk(const char* utf8Str); 
// 文本转图片函数声明
SDL_Surface* text2surface(std::string txt, unsigned int size, SDL_Color color);  //基本作废 
bool surface_save_png(SDL_Surface* sur, std::string savepath);

bool fileExists(const std::string& filename); 

void to_world_xy(double &x,double &y);            //到世界坐标,中间为原点 
void to_world_xy(float &x,float &y);            //到世界坐标,中间为原点 
void to_world_xy(int &x,int &y);            //到世界坐标,中间为原点 

void to_screen_xy(double &x,double &y);        //到屏幕坐标,左上为原点 
void to_screen_xy(float &x,float &y);        //到屏幕坐标,左上为原点 
void to_screen_xy(int &x,int &y);        //到屏幕坐标,左上为原点 

int randint(int a,int b);
// 生成 [a, b] 区间的浮点数,均匀分布
float random(float a, float b);

template<typename T>
T oneof(T a, T b) {
    int x = randint(0, 1);
    if (x == 0) return a;
    return b;
}

// 将宽字符转换为窄字符(UTF-8)
std::string wideToNarrow(const wchar_t* wideStr);
std::string get_system_font_folder();
// 获取字体名称到路径的映射表
std::map<std::string, std::string> getFontNameToPathMap() ;

std::vector<unsigned char> base64_decode(const std::string& encoded);
void base64_to_png(const std::string& base64_data, const std::string& output_filename);

void ApplyFlipToPoints(SDL_Point* points, int w, int h, SDL_RendererFlip flip);
SDL_Rect CalculateRenderCopyExBoundingRect(const SDL_Rect* dstRect, double angle, const SDL_Point* center,SDL_RendererFlip flip);

bool svg2png(const char* svgPath, const char* pngPath, float scale=1.0f);

// 简单直接的版本,把svg里的fill填充颜色换成其它颜色 
std::string replace_fill_color_simple(const std::string& src_svg_content, const std::string& color);
void release_svg_content();   //释放在functools.cpp里存储的svg文件到res文件夹 
std::string inputbox(std::string title, std::string prompt);
int messagebox(std::string title,std::string prompt,unsigned flags=SDL_MESSAGEBOX_INFORMATION);
// 计算角ABC的大小(单位:度)
float get_angle(Point A, Point B, Point C);

#endif // FUNCTOOLS_H 

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在C++分类目录。将固定链接加入收藏夹。

发表回复