first commit

This commit is contained in:
hinomori_minori
2026-09-12 05:38:04 +09:00
commit 3e167c7e13
1844 changed files with 841995 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
#include "json.h"
static int directory_exists(const char *path) {
struct stat info;
if (stat(path, &info) != 0) {
return 0; // 不存在或无法访问
}
return S_ISDIR(info.st_mode);
}
struct json_object* open_json_file(const char* _path){
char *buf1 = malloc(PATH_MAX);
memset(buf1,0,PATH_MAX);
ssize_t len = readlink("/proc/self/exe", buf1, PATH_MAX - 1);
if (len == -1) {
fprintf(stderr, "[Error] %s:%d: readlink() returned non-negative value. %s\n",__FILE__, __LINE__, __func__);
return NULL;
}
char *buf2 = malloc(PATH_MAX);
memset(buf2,0,PATH_MAX);
*buf2 = '/';
strcat(buf2,_path);
dirname(buf1);
strcat(buf1,buf2);
memset(buf2,0,PATH_MAX);
strncpy(buf2, buf1, PATH_MAX);
dirname(buf2);
if(!directory_exists(buf2)){
if(mkdir(buf2,0774)!=0){
fprintf(stderr, "[Error] %s:%d: Make directory failed. %s\n",__FILE__, __LINE__, __func__);
return NULL;
}
}
FILE* json_file = fopen(buf1,"r+");
if(json_file == NULL){
fprintf(stdout,"[Info] %s:%d: Read config file failed. %s : %s",__FILE__, __LINE__, __func__,strerror(errno));
json_file = fopen(buf1,"w");
if(json_file == NULL){
fprintf(stdout,"[Error] %s:%d: Create config file failed. %s : %s",__FILE__, __LINE__, __func__,strerror(errno));
return NULL;
}
fprintf(json_file,"{}");
fclose(json_file);
json_file = fopen(buf1,"r+");
}
free(buf1);free(buf2);
int file_size=0,malloced = 0;
char* json_string = malloc(malloced);
while(!feof(json_file)){
if(file_size>=malloced-1){
char* tmp_string = realloc(json_string,malloced+2048);
if(tmp_string!=NULL){
json_string = tmp_string;
}else{
fprintf(stderr, "[Error] %s:%d: Realloc failed. May system has been Out of Memory. %s\n",__FILE__, __LINE__, __func__);
return NULL;
}
memset(json_string+malloced,0,2048);
malloced+=2048;
}
json_string[file_size] = fgetc(json_file);
file_size++;
}
file_size--;
json_string[file_size] = '\0';
fclose(json_file);
char* tmp_string = realloc(json_string,file_size);
if(tmp_string!=NULL){
json_string = tmp_string;
}else{
fprintf(stderr, "[Error] %s:%d: Realloc failed. May system has been Out of Memory. %s\n",__FILE__, __LINE__, __func__);
return NULL;
}
struct json_object *obj;
obj = json_tokener_parse(json_string);
if(obj==NULL){
enum json_tokener_error jerr;
obj = json_tokener_parse_verbose("...", &jerr);
return NULL;
}
return obj;
}
int write_json_file(const char* _path,struct json_object* _json){
char* buf1 = malloc(PATH_MAX);
memset(buf1,0,PATH_MAX);
ssize_t len = readlink("/proc/self/exe", buf1, PATH_MAX - 1);
if (len == -1) {
fprintf(stderr, "[Error] %s:%d: readlink() returned non-negative value. %s\n",__FILE__, __LINE__, __func__);
return 1;
}
char* buf2 = malloc(PATH_MAX);
memset(buf2,0,PATH_MAX);
*buf2 = '/';
strcat(buf2,_path);
dirname(buf1);
strcat(buf1,buf2);
memset(buf2,0,PATH_MAX);
strncpy(buf2, buf1, PATH_MAX);
dirname(buf2);
if(!directory_exists(buf2)){
if(mkdir(buf2,0774)!=0){
fprintf(stderr, "[Error] %s:%d: Make directory failed. %s\n",__FILE__, __LINE__, __func__);
return -1;
}
}
FILE* json_file = fopen(buf1,"w");
if(json_file == NULL){
fprintf(stderr, "[Error] %s:%d: readlink() returned non-negative value. %s\n",__FILE__, __LINE__, __func__);
return -1;
}
free(buf1);free(buf2);
fprintf(json_file, "%s", json_object_to_json_string_ext(_json, JSON_C_TO_STRING_PRETTY));
fclose(json_file);
return 0;
}
struct json_object *get_json_object_object(struct json_object *_json,const char *key,struct json_object *_default,const char *_path){
struct json_object *obj = json_object_object_get(_json, key);
if(obj==NULL){
json_object_object_add(_json,key,_default);
write_json_file(_path,_json);
obj = json_object_object_get(_json, key);
}
return obj;
}
+20
View File
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <linux/fs.h>
#include <json-c/json.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <errno.h>
#ifndef CONF_H
#define CONF_H
//将json文件读取为json-c的json_object格式
struct json_object* open_json_file(const char* _path);
//将json_object结构写回文件
int write_json_file(const char* _path,struct json_object* _json);
//json文件读取和一键修补函数
struct json_object *get_json_object_object(struct json_object *_json,const char *key,struct json_object *_default,const char *_path);
#endif
+650
View File
@@ -0,0 +1,650 @@
#include "main.h"
const char *texture_path_list[] = {
"assets/textures/button/button_4x1.png",
"assets/textures/button/button_4x1_pressed.png",
"assets/textures/button/button_42x5.png",
"assets/textures/button/button_42x5_pressed.png",
"assets/textures/button/button_1x1.png",
"assets/textures/button/button_1x1_pressed.png",
"assets/textures/input_box/left_5x20px.png",
"assets/textures/input_box/right_5x20px.png",
"assets/textures/input_box/center_1x20px.png"
};//需要加载的纹理列表
bool InitTextList(struct text_list *_i,int count){
_i->count=count;
_i->_p = malloc(_i->count*sizeof(char *));
if(_i->_p==NULL){
_i->count=0;
return false;
}
for(int i=0;i<_i->count;i++){
*(_i->_p+i) = malloc(sizeof(char)*TEXT_MAX_LEN);
memset(*(_i->_p+i),0,sizeof(char)*TEXT_MAX_LEN);
}
return true;
}
void UninTextList(struct text_list *_i){
for(int i=0;i<_i->count;i++){
free(*(_i->_p+i));
}
free(_i->_p);
_i->_p=NULL;
_i->count=0;
}
bool half_clock = false;
void change_bool(){
if(half_clock){
half_clock=false;
}else{
half_clock=true;
}
}
int main(){
struct timer half_clock_timer = init_timer(change_bool,NULL,2);
pthread_t hct_t;
start_timer(&half_clock_timer,NULL,&hct_t);
bindtextdomain("Industrial Genesis", "locale");
textdomain("Industrial Genesis");
//区域设置
#ifdef _RELEASE_
SetTraceLogLevel(LOG_ERROR);
#endif
//调试日志输出
window_config.root = open_json_file("config/window.json");
//获取窗口配置文件
window_config.VSync_config = get_json_object_object(window_config.root,"VSync",json_object_new_boolean(1),"config/window.json");
bool VSync_enabled = json_object_get_boolean(window_config.VSync_config);
if(VSync_enabled){
SetConfigFlags(FLAG_VSYNC_HINT);
}//垂直同步
window_config.window_size_config = json_object_new_object();
json_object_object_add(window_config.window_size_config,"x",json_object_new_int(800));
json_object_object_add(window_config.window_size_config,"y",json_object_new_int(600));
window_config.window_size_config = get_json_object_object(window_config.root,"window_size",window_config.window_size_config,"config/window.json");
//窗口大小,默认800x600
window_config.background_config = json_object_new_object();
json_object_object_add(window_config.background_config,"type",json_object_new_string("color"));
json_object_object_add(window_config.background_config,"image_path",json_object_new_string("assets/background/black_1x1px.png"));
window_config.background_color_config = json_object_new_object();
json_object_object_add(window_config.background_color_config,"r",json_object_new_int(background_color.r));
json_object_object_add(window_config.background_color_config,"g",json_object_new_int(background_color.g));
json_object_object_add(window_config.background_color_config,"b",json_object_new_int(background_color.b));
json_object_object_add(window_config.background_color_config,"a",json_object_new_int(background_color.a));
json_object_object_add(window_config.background_config,"color",window_config.background_color_config);
window_config.background_config = get_json_object_object(window_config.root,"background",window_config.background_config,"config/window.json");
window_config.background_color_config = get_json_object_object(window_config.background_config,"color",window_config.background_color_config,"config/window.json");
//主界面背景
window_config.text_color_config = json_object_new_object();
json_object_object_add(window_config.text_color_config,"r",json_object_new_int(text_color.r));
json_object_object_add(window_config.text_color_config,"g",json_object_new_int(text_color.g));
json_object_object_add(window_config.text_color_config,"b",json_object_new_int(text_color.b));
json_object_object_add(window_config.text_color_config,"a",json_object_new_int(text_color.a));
window_config.text_color_config = get_json_object_object(window_config.root,"text_color",window_config.text_color_config,"config/window.json");
window_config.scale_config = get_json_object_object(window_config.root,"scale",json_object_new_double(1.0f),"config/window.json");
screen_scale = json_object_get_double(window_config.scale_config);
//缩放
window_config.language_config = get_json_object_object(window_config.root,"language",json_object_new_string(setlocale(LC_MESSAGES,NULL)),"config/window.json");
char lang[64];
snprintf(lang,sizeof(lang),"%s",json_object_get_string(window_config.language_config));
if(strcmp(lang,"en_US")==0){
language = ENGLISH;
}else if(strcmp(lang,"zh_CN")==0){
language = SIMPLIFIED_CHINESE;
}else{
language = ENGLISH;
}
setenv("LANGUAGE",lang,1);
strncat(lang,".UTF-8",sizeof(lang)-1);
lang[63]='\0';
setlocale(LC_MESSAGES,lang);
InitWindow(json_object_get_int(json_object_object_get(window_config.window_size_config,"x")), json_object_get_int(json_object_object_get(window_config.window_size_config,"y")), "Industrial Genesis");
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetWindowTitle(_("game.name"));
//窗口初始化,执行到这里就有窗口了
load_texture(texture_path_list,9);
//纹理列表加载,实际加载的纹理列表在texture.h中定义。
BeginDrawing();
ClearBackground(background_color);
DrawText("Loading...",(GetScreenWidth()-MeasureText("Loading...",40))*0.5f,(GetScreenHeight()-40)*0.5f,40,text_color);
EndDrawing();
struct Texture background_image;//背景纹理是按需加载,这里单独列出来了
if(strcmp("color",json_object_get_string(json_object_object_get(window_config.background_config,"type")))==0){
background_color.r = json_object_get_int(json_object_object_get(window_config.background_color_config,"r"));
background_color.g = json_object_get_int(json_object_object_get(window_config.background_color_config,"g"));
background_color.b = json_object_get_int(json_object_object_get(window_config.background_color_config,"b"));
background_color.a = json_object_get_int(json_object_object_get(window_config.background_color_config,"a"));
background_type = COLOR;
}else if(strcmp("image",json_object_get_string(json_object_object_get(window_config.background_config,"type")))==0){
background_image = LoadTexture(json_object_get_string(json_object_object_get(window_config.background_config,"image_path")));
background_type = IMAGE;
}//加载背景
window_config.showFPS_config = get_json_object_object(window_config.root,"showFPS",json_object_new_boolean(0),"config/window.json");
bool showFPS = json_object_get_boolean(window_config.showFPS_config);
//获取是否显示FPS的设置
float background_scale = 1;
struct Vector2 background_pos = {0.0f,0.0f};
if(background_type==IMAGE){
background_scale = (float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?(float)GetScreenHeight()/background_image.height:(float)GetScreenWidth()/background_image.width;
background_pos = (struct Vector2){(float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?((float)GetScreenWidth()-background_scale*background_image.width)*0.5f:0,(float)GetScreenWidth()/GetScreenHeight()>=(float)background_image.width/background_image.height?((float)GetScreenHeight()-background_scale*background_image.height)*0.5f:0};
}
//背景图片位置定义
struct json_object *font_config = open_json_file("config/font.json");
int codepointcount = json_object_get_int(json_object_object_get(font_config,json_object_get_string(json_object_object_get(font_config,"default"))));
struct Font font_25px = LoadFontEx(json_object_get_string(json_object_object_get(font_config,"default")),25*screen_scale,NULL,codepointcount);
struct Font font_20px = LoadFontEx(json_object_get_string(json_object_object_get(font_config,"default")),20*screen_scale,NULL,codepointcount);
//字体文件加载,加载的字体为font.json中键为default的值的文件
struct Button_list button_list={NULL,0};
//初始化按钮列表结构体
struct Input_Box_list input_box_list={NULL,0};
struct text_list text_list={NULL,0};
struct Input_Box *focus = NULL;
struct Input_Box **focus_p = NULL;
float backspace_timer = 0.0f;
bool backspaceHeld = false;
bool firstFrame = false;
//首帧标记
struct Vector2 setting_tip_text_pos = {(GetScreenWidth()-MeasureTextEx(font_20px,_("settings.restart.notice"),20*screen_scale,1.6f*screen_scale).x)*0.5f,GetScreenHeight()-30-20*screen_scale};
int c = 0;
while (!WindowShouldClose()||IsKeyPressed(KEY_ESCAPE)) {//渲染主循环
BeginDrawing();
switch (screen){//分场景渲染
case START_SCREEN://主菜单
if(button_list._p==NULL){
InitButtonList(&button_list,4);
firstFrame = true;
}//首次进入则创建按钮列表
if(IsWindowResized()||firstFrame){
InitAndDrawButtonEx(*(button_list._p),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){1,1},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("new"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){1,2},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("load"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawButtonEx(*(button_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){1,3},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("settings"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawButtonEx(*(button_list._p+3),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){2,3},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("exit"),font_25px,25*screen_scale,2*screen_scale);
firstFrame = false;
}//重新计算按钮布局或初始化
switch(background_type){
case COLOR:
ClearBackground(background_color);
break;
case IMAGE:
ClearBackground(background_color);
DrawTextureEx(background_image,background_pos,0,background_scale,WHITE);
break;
default:
ClearBackground(background_color);
break;
}//渲染背景
ReDrawButtonList(&button_list);
if(isButtonPressed(*(button_list._p+2))){
screen = SETTING_SCREEN;
UninButtonList(&button_list);
break;
}//设置按钮按下响应,内存回收防止泄漏
if(isButtonPressed(*(button_list._p+3))){
UninButtonList(&button_list);
screen = EXIT_SCREEN;
break;
}//退出按钮按下响应
break;
case EXIT_SCREEN:
if(button_list._p==NULL){
InitButtonList(&button_list,6);
firstFrame = true;
}//首次进入则创建按钮列表
if(IsWindowResized()||firstFrame){
InitAndDrawButtonEx(*(button_list._p),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){1,1},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("new"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){1,2},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("load"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawButtonEx(*(button_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){1,3},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("settings"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawButtonEx(*(button_list._p+3),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,3},(struct Vector2){2,3},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("exit"),font_25px,25*screen_scale,2*screen_scale);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (struct Color){0,0,0,128});
DrawTextEx(font_25px,_("exit.msg"),Vector2_minus(Vector2_minus((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){0,70*0.5f*screen_scale}),MeasureTextEx(font_25px,_("exit.msg"),25*screen_scale,2*screen_scale)),25*screen_scale,2*screen_scale,text_color);
InitAndDrawButtonEx(*(button_list._p+4),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,2},(struct Vector2){1,2},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("cancle"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawButtonEx(*(button_list._p+5),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,2},(struct Vector2){2,2},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("exit"),font_25px,25*screen_scale,2*screen_scale);
firstFrame = false;
}
if(isButtonPressed(*(button_list._p+4))){
screen = START_SCREEN;
UninButtonList(&button_list);
break;
}
if(isButtonPressed(*(button_list._p+5))){
UninButtonList(&button_list);
goto jump_break;
}
for(int i=0;i<4;i++){
ReDrawButton(*(button_list._p+i));
}
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (struct Color){0,0,0,128});
DrawTextEx(font_25px,_("exit.msg"),Vector2_minus(Vector2_minus((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){0,70*0.5f*screen_scale}),Vector2_quarter(MeasureTextEx(font_25px,_("exit.msg"),25*screen_scale,2*screen_scale))),25*screen_scale,2*screen_scale,text_color);
for(int i=4;i<6;i++){
ReDrawButton(*(button_list._p+i));
}
break;
case SETTING_SCREEN://设置菜单
if(button_list._p==NULL){
InitButtonList(&button_list,5);
firstFrame = true;
}//首次进入则创建按钮列表
if(IsWindowResized()||firstFrame){
InitAndDrawButtonEx(*(button_list._p),(struct Vector2){20,20},zoom(square_button_size),texture_list._p[SQUARE_BUTTON],texture_list._p[PRESSED_SQUARE_BUTTON],"x",font_25px,25*screen_scale,2*screen_scale);
setting_tip_text_pos = (struct Vector2){(GetScreenWidth()-MeasureTextEx(font_20px,_("settings.restart.notice"),20*screen_scale,1.6f*screen_scale).x)*0.5f,GetScreenHeight()-30-20*screen_scale};
if(VSync_enabled){
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("vsync.true"),font_25px,25*screen_scale,2*screen_scale);
}else{
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("vsync.false"),font_25px,25*screen_scale,2*screen_scale);
}
if(showFPS){
InitAndDrawButtonEx(*(button_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){2,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("fps.true"),font_25px,25*screen_scale,2*screen_scale);
}else{
InitAndDrawButtonEx(*(button_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){2,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("fps.false"),font_25px,25*screen_scale,2*screen_scale);
}
InitAndDrawButtonEx(*(button_list._p+3),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,2},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("background.settings"),font_25px,25*screen_scale,2*screen_scale);
switch(language){
case ENGLISH:
InitAndDrawButtonEx(*(button_list._p+4),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,3},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("language.en.us"),font_25px,25*screen_scale,2*screen_scale);
break;
case SIMPLIFIED_CHINESE:
InitAndDrawButtonEx(*(button_list._p+4),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,3},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("language.en.us"),font_25px,25*screen_scale,2*screen_scale);
break;
default:
InitAndDrawButtonEx(*(button_list._p+4),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,3},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("language.en.us"),font_25px,25*screen_scale,2*screen_scale);
break;
}
firstFrame = false;
}//重新计算按钮布局或初始化
switch(background_type){
case COLOR:
ClearBackground(background_color);
break;
case IMAGE:
ClearBackground(background_color);
DrawTextureEx(background_image,background_pos,0,background_scale,WHITE);
break;
default:
ClearBackground(background_color);
break;
}//渲染背景
if(IsKeyPressed(KEY_ESCAPE)||isButtonPressed(*(button_list._p))){
screen = START_SCREEN;
UninButtonList(&button_list);
break;
}//退出返回主菜单
if(isButtonPressed(*(button_list._p+1))){
if(VSync_enabled){
VSync_enabled = false;
json_object_set_boolean(window_config.VSync_config,0);
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("vsync.false"),font_25px,25*screen_scale,2*screen_scale);
}else{
VSync_enabled = true;
json_object_set_boolean(window_config.VSync_config,1);
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("vsync.true"),font_25px,25*screen_scale,2*screen_scale);
}
setting_tip_type = NEED_RESTART;
}
if(isButtonPressed(*(button_list._p+2))){
if(showFPS){
showFPS = false;
json_object_set_boolean(window_config.showFPS_config,0);
InitAndDrawButtonEx(*(button_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){2,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("fps.false"),font_25px,25*screen_scale,2*screen_scale);
}else{
showFPS = true;
json_object_set_boolean(window_config.showFPS_config,1);
InitAndDrawButtonEx(*(button_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){2,1},normal_button_size),zoom(normal_button_size),texture_list._p[BUTTON],texture_list._p[PRESSED_BUTTON],_("fps.true"),font_25px,25*screen_scale,2*screen_scale);
}
}
if(isButtonPressed(*(button_list._p+3))){
UninButtonList(&button_list);
screen = SETTING_BACKGROUND_SCREEN;
break;
}
switch(setting_tip_type){
case NEED_RESTART:
DrawTextEx(font_20px,_("settings.restart.notice"),setting_tip_text_pos,20*screen_scale,1.6f*screen_scale,text_color);
break;
}
if(isButtonPressed(*(button_list._p+4))){
if(language!=SIMPLIFIED_CHINESE){
language++;
}else{
language = ENGLISH;
}
switch(language){
case ENGLISH:
setenv("LANGUAGE","en_US",1);
setlocale(LC_MESSAGES,"en_US.UTF-8");
InitAndDrawButtonEx(*(button_list._p+4),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,3},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("language.en.us"),font_25px,25*screen_scale,2*screen_scale);
SetWindowTitle(_("game.name"));
firstFrame = true;
break;
case SIMPLIFIED_CHINESE:
setenv("LANGUAGE","zh_CN",1);
setlocale(LC_MESSAGES,"zh_CN.UTF-8");
InitAndDrawButtonEx(*(button_list._p+4),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,3},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("language.en.us"),font_25px,25*screen_scale,2*screen_scale);
SetWindowTitle(_("game.name"));
firstFrame = true;
break;
default:
setenv("LANGUAGE","en_US",1);
setlocale(LC_MESSAGES,"en_US.UTF-8");
InitAndDrawButtonEx(*(button_list._p+4),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){2,5},(struct Vector2){1,3},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("language.en.us"),font_25px,25*screen_scale,2*screen_scale);
SetWindowTitle(_("game.name"));
firstFrame = true;
break;
}
}
ReDrawButtonList(&button_list);
break;
case SETTING_BACKGROUND_SCREEN:
if(button_list._p==NULL){
InitButtonList(&button_list,2);
switch(background_type){
case COLOR:
InitInputBoxList(&input_box_list,4);
InitTextList(&text_list,4);
sprintf(*(text_list._p),"%d",background_color.r);
sprintf(*(text_list._p+1),"%d",background_color.g);
sprintf(*(text_list._p+2),"%d",background_color.b);
sprintf(*(text_list._p+3),"%d",background_color.a);
break;
case IMAGE:
InitInputBoxList(&input_box_list,1);
InitTextList(&text_list,1);
sprintf(*(text_list._p),"%s",json_object_get_string(json_object_object_get(window_config.background_config,"image_path")));
break;
}
firstFrame = true;
}//首次进入则创建按钮列表
if(IsWindowResized()||firstFrame){
InitAndDrawButtonEx(*(button_list._p),(struct Vector2){20,20},zoom(square_button_size),texture_list._p[SQUARE_BUTTON],texture_list._p[PRESSED_SQUARE_BUTTON],"x",font_25px,25*screen_scale,2*screen_scale);
switch(background_type){
case COLOR:
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,1},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("background.type.color"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawInputBox(*(input_box_list._p),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,2},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p));
InitAndDrawInputBox(*(input_box_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,3},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p+1));
InitAndDrawInputBox(*(input_box_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,4},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p+2));
InitAndDrawInputBox(*(input_box_list._p+3),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,5},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p+3));
break;
case IMAGE:
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,1},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("background.type.image"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawInputBox(*(input_box_list._p),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,2},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p));
break;
}
firstFrame = false;
}//重新计算按钮布局或初始化
switch(background_type){
case COLOR:
ClearBackground(background_color);
break;
case IMAGE:
ClearBackground(background_color);
DrawTextureEx(background_image,background_pos,0,background_scale,WHITE);
break;
default:
ClearBackground(background_color);
break;
}//渲染背景
if((IsKeyPressed(KEY_ESCAPE)&&focus==NULL)||isButtonPressed(*(button_list._p))){
screen = SETTING_SCREEN;
UninButtonList(&button_list);
UninInputBoxList(&input_box_list);
switch(background_type){
case COLOR:
sscanf(*(text_list._p),"%u",&background_color.r);
sscanf(*(text_list._p+1),"%u",&background_color.g);
sscanf(*(text_list._p+2),"%u",&background_color.b);
sscanf(*(text_list._p+3),"%u",&background_color.a);
window_config.background_color_config = json_object_object_get(window_config.background_config,"color");
json_object_set_int(json_object_object_get(window_config.background_color_config,"r"),background_color.r);
json_object_set_int(json_object_object_get(window_config.background_color_config,"g"),background_color.g);
json_object_set_int(json_object_object_get(window_config.background_color_config,"b"),background_color.b);
json_object_set_int(json_object_object_get(window_config.background_color_config,"a"),background_color.a);
json_object_set_string(json_object_object_get(window_config.background_config,"type"),"color");
break;
case IMAGE:
char background_path[TEXT_MAX_LEN];
sscanf(*(text_list._p),"%s",background_path);
UnloadTexture(background_image);
background_image = LoadTexture(background_path);
background_scale = (float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?(float)GetScreenHeight()/background_image.height:(float)GetScreenWidth()/background_image.width;
background_pos = (struct Vector2){(float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?((float)GetScreenWidth()-background_scale*background_image.width)*0.5f:0,(float)GetScreenWidth()/GetScreenHeight()>=(float)background_image.width/background_image.height?((float)GetScreenHeight()-background_scale*background_image.height)*0.5f:0};
json_object_set_string(json_object_object_get(window_config.background_config,"image_path"),background_path);
json_object_set_string(json_object_object_get(window_config.background_config,"type"),"image");
break;
}
UninTextList(&text_list);
break;
}
if(IsKeyPressed(KEY_ESCAPE)&&focus!=NULL){
focus = NULL;
focus_p = NULL;
}
if(isButtonPressed(*(button_list._p+1))){
switch(background_type){
case COLOR:
background_type = IMAGE;
UninTextList(&text_list);
InitTextList(&text_list,1);
sprintf(*(text_list._p),"%s",json_object_get_string(json_object_object_get(window_config.background_config,"image_path")));
UninInputBoxList(&input_box_list);
InitInputBoxList(&input_box_list,1);
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,1},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("background.type.image"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawInputBox(*(input_box_list._p),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,2},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p));
background_image = LoadTexture(json_object_get_string(json_object_object_get(window_config.background_config,"image_path")));
background_scale = (float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?(float)GetScreenHeight()/background_image.height:(float)GetScreenWidth()/background_image.width;
background_pos = (struct Vector2){(float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?((float)GetScreenWidth()-background_scale*background_image.width)*0.5f:0,(float)GetScreenWidth()/GetScreenHeight()>=(float)background_image.width/background_image.height?((float)GetScreenHeight()-background_scale*background_image.height)*0.5f:0};
if(!IsTextureValid(background_image)){
background_image = LoadTexture("assets/background/black_1x1px.png");
}
break;
case IMAGE:
background_type = COLOR;
UnloadTexture(background_image);
UninTextList(&text_list);
InitTextList(&text_list,4);
sprintf(*(text_list._p),"%i",background_color.r);
sprintf(*(text_list._p+1),"%i",background_color.g);
sprintf(*(text_list._p+2),"%i",background_color.b);
sprintf(*(text_list._p+3),"%i",background_color.a);
UninInputBoxList(&input_box_list);
InitInputBoxList(&input_box_list,4);
InitAndDrawButtonEx(*(button_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,1},normal_button_size),zoom(wide_button_size),texture_list._p[WIDE_BUTTON],texture_list._p[PRESSED_WIDE_BUTTON],_("background.type.color"),font_25px,25*screen_scale,2*screen_scale);
InitAndDrawInputBox(*(input_box_list._p),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,2},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p));
InitAndDrawInputBox(*(input_box_list._p+1),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,3},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p+1));
InitAndDrawInputBox(*(input_box_list._p+2),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,4},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p+2));
InitAndDrawInputBox(*(input_box_list._p+3),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f,GetScreenHeight()*0.5f},(struct Vector2){3,5},(struct Vector2){2,5},normal_button_size),zoom(wide_button_size),texture_list._p[LEFT_INPUT_BOX],texture_list._p[CENTER_INPUT_BOX],texture_list._p[RIGHT_INPUT_BOX],font_25px,25*screen_scale,2*screen_scale,*(text_list._p+3));
break;
}
}
c = GetCharPressed();
if(IsKeyPressed(KEY_BACKSPACE)&&focus!=NULL){
char *_p = *(text_list._p+((unsigned long)(focus_p)-(unsigned long)(input_box_list._p))/sizeof(char*));
int tail = strlen(_p)-1;
while (tail > 0 && (_p[tail] & 0xC0) == 0x80) tail--;
_p[tail] = '\0';
DrawInputBoxEx(focus);
}else if(IsKeyDown(KEY_BACKSPACE)&&focus!=NULL){
float dt = GetFrameTime();
if(backspaceHeld){
backspace_timer+=dt;
if(backspace_timer>0.05f){
backspace_timer -= 0.05f;
char *_p = *(text_list._p+((unsigned long)(focus_p)-(unsigned long)(input_box_list._p))/sizeof(char*));
int tail = strlen(_p)-1;
while (tail > 0 && (_p[tail] & 0xC0) == 0x80) tail--;
_p[tail] = '\0';
DrawInputBoxEx(focus);
}
}else{
backspace_timer+=dt;
if(backspace_timer>0.5f){
backspaceHeld = true;
backspace_timer -= 0.5f;
}
}
}else if(IsKeyReleased(KEY_BACKSPACE)){
backspaceHeld = false;
}
if(IsKeyDown(KEY_LEFT_CONTROL)&&IsKeyPressed(KEY_V)&&focus!=NULL){
const char *clipboard = GetClipboardText();
if(strlen(*(text_list._p+((unsigned long)(focus_p)-(unsigned long)(input_box_list._p))/sizeof(char*)))+strlen(clipboard)>=TEXT_MAX_LEN){
continue;
}
strcat(*(text_list._p+((unsigned long)(focus_p)-(unsigned long)(input_box_list._p))/sizeof(char*)),clipboard);
DrawInputBoxEx(focus);
}
if(c!=0&&focus_p!=NULL){
while(c>0){
char *_p = *(text_list._p+((unsigned long)(focus_p)-(unsigned long)input_box_list._p)/sizeof(char*));
int tail = strlen(_p)-1;
if (c >= 32) { // 忽略不可打印字符
char utf8[5] = {0};
int bytes = CodepointToUtf8(c, utf8);
if (strlen(_p) + bytes < TEXT_MAX_LEN - 1) {
strcat(_p,utf8);
_p[strlen(_p)] = '\0';
}
}
c = GetCharPressed();
}
DrawInputBoxEx(focus);
}
DrawTextEx(font_25px,_("background.type"),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f+25*0.25f,GetScreenHeight()*0.5f+25*0.25f},(struct Vector2){3,5},(struct Vector2){1,1},normal_button_size),25*screen_scale,2*screen_scale,text_color);
switch(background_type){
case IMAGE:
DrawTextEx(font_25px,_("background.type.image.path"),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f+25*0.25f,GetScreenHeight()*0.5f+25*0.25f},(struct Vector2){3,5},(struct Vector2){1,2},normal_button_size),25*screen_scale,2*screen_scale,text_color);
break;
case COLOR:
DrawTextEx(font_25px,_("background.type.color.red"),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f+25*0.25f,GetScreenHeight()*0.5f+25*0.25f},(struct Vector2){3,5},(struct Vector2){1,2},normal_button_size),25*screen_scale,2*screen_scale,text_color);
DrawTextEx(font_25px,_("background.type.color.green"),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f+25*0.25f,GetScreenHeight()*0.5f+25*0.25f},(struct Vector2){3,5},(struct Vector2){1,3},normal_button_size),25*screen_scale,2*screen_scale,text_color);
DrawTextEx(font_25px,_("background.type.color.blue"),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f+25*0.25f,GetScreenHeight()*0.5f+25*0.25f},(struct Vector2){3,5},(struct Vector2){1,4},normal_button_size),25*screen_scale,2*screen_scale,text_color);
DrawTextEx(font_25px,_("background.type.color.alpha"),button_in_grid_pos((struct Vector2){GetScreenWidth()*0.5f+25*0.25f,GetScreenHeight()*0.5f+25*0.25f},(struct Vector2){3,5},(struct Vector2){1,5},normal_button_size),25*screen_scale,2*screen_scale,text_color);
break;
}
if(focus==NULL){
focus = WhichInputBoxIsPressed(&input_box_list,&focus_p);
}else if(IsUnFocusInputBox(focus)){
focus = WhichInputBoxIsPressed(&input_box_list,&focus_p);
}
ReDrawButtonList(&button_list);
ReDrawInputBoxList(&input_box_list,focus);
break;
}
if (IsWindowResized()&&background_type==IMAGE){
background_scale = (float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?(float)GetScreenHeight()/background_image.height:(float)GetScreenWidth()/background_image.width;
background_pos = (struct Vector2){(float)GetScreenWidth()/GetScreenHeight()<(float)background_image.width/background_image.height?((float)GetScreenWidth()-background_scale*background_image.width)*0.5f:0,(float)GetScreenWidth()/GetScreenHeight()>=(float)background_image.width/background_image.height?((float)GetScreenHeight()-background_scale*background_image.height)*0.5f:0};
}//在改变了窗口大小时进入,改变图片背景布局
if(showFPS){
DrawFPS(0, 0);
}//显示FPS
EndDrawing();
}
jump_break:
if(background_type==IMAGE){
UnloadTexture(background_image);
}//图片背景模式的纹理单独收回
unload_texture();
//卸载所有纹理
stop_timer(&half_clock_timer,NULL,&hct_t);
json_object_set_int(json_object_object_get(window_config.window_size_config,"x"),GetScreenWidth());
json_object_set_int(json_object_object_get(window_config.window_size_config,"y"),GetScreenHeight());
write_json_file("config/window.json",window_config.root);
//将窗口大小写回配置文件
return 0;
}
+81
View File
@@ -0,0 +1,81 @@
#include <raylib.h>
#include <stdio.h>
#include <linux/fs.h>
#include <json-c/json.h>
#include <libintl.h>
#include <locale.h>
#ifdef _RELEASE_
#include "json.h"
#include "ui.h"
#include "texture.h"
#include "timer.h"
#include "tools.h"
#else
#include "json.c"
#include "ui.c"
#include "texture.c"
#include "timer.c"
#include "tools.c"
#endif
#ifndef MAIN_H
#define MAIN_H
#define _(STRING) gettext(STRING)
struct {
struct json_object *root;
struct json_object *VSync_config;
struct json_object *window_size_config;
struct json_object *background_config;
struct json_object *background_color_config;
struct json_object *showFPS_config;
struct json_object *scale_config;
struct json_object *text_color_config;
struct json_object *language_config;
}window_config;
struct text_list{
char **_p;
int count;
};
enum{
IMAGE,
COLOR
}background_type=COLOR;
enum{
START_SCREEN,
SETTING_SCREEN,
SETTING_BACKGROUND_SCREEN,
EXIT_SCREEN
}screen=START_SCREEN;
extern const char *texture_path_list[];
enum {
BUTTON,
PRESSED_BUTTON,
WIDE_BUTTON,
PRESSED_WIDE_BUTTON,
SQUARE_BUTTON,
PRESSED_SQUARE_BUTTON,
LEFT_INPUT_BOX,
RIGHT_INPUT_BOX,
CENTER_INPUT_BOX
};
enum {
ENGLISH,
SIMPLIFIED_CHINESE
}language;
enum{
NONE,
NEED_RESTART
}setting_tip_type=NONE;
#endif
+33
View File
@@ -0,0 +1,33 @@
#include "texture.h"
struct TextureList texture_list = {NULL, 0};
void load_texture(const char** _i,int count){
if(texture_list.count==0){
texture_list._p = malloc(count*sizeof(struct Texture));
}else{
struct Texture *_tmp = realloc(texture_list._p,count*sizeof(struct Texture));
if(_tmp!=NULL){
texture_list._p = _tmp;
}
}
int save_count = texture_list.count;
for(int i=save_count;i<save_count+count;i++){
*(texture_list._p+i) = LoadTexture(*(_i+i-save_count));
texture_list.count++;
}
return;
}
void unload_texture(){
for(int i=texture_list.count-1;i>-1;i--){
UnloadTexture(texture_list._p[i]);
texture_list.count--;
}
free(texture_list._p);
texture_list._p=NULL;
if(texture_list.count!=0){
fprintf(stderr, "[Warn] %s:%d: Texture unloading failed. %d textures could not be unloaded. %s\n",__FILE__, __LINE__,texture_list.count, __func__);
}
return;
}
+17
View File
@@ -0,0 +1,17 @@
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef INIT_H
#define INIT_H
struct TextureList {
struct Texture *_p;
int count;
};
extern struct TextureList texture_list;
void load_texture(const char** _i,int count);
void unload_texture();
#endif
+35
View File
@@ -0,0 +1,35 @@
#include "timer.h"
static void* sub_thread(void* _ip){
struct timer* _i = (struct timer*)_ip;
uv_timer_start(&(_i->uvtimer), (uv_timer_cb)_i->func, 0, 1000/_i->tps);
uv_run(_i->loop, UV_RUN_DEFAULT);
return NULL;
}
struct timer init_timer(void (*func)(),void* args,unsigned int tps){
struct timer _o;
_o.tps = tps;
_o.loop = uv_default_loop();
uv_timer_init(_o.loop, &(_o.uvtimer));
_o.func = func;
_o.uvtimer.data = args;
return _o;
}
void start_timer(struct timer* _i,void (*func_on_start)(),pthread_t *timer_t){
if (func_on_start != NULL) {
func_on_start();
}
pthread_create(timer_t,NULL,sub_thread,_i);
return;
}
void stop_timer(struct timer* _i,void (*func_on_stop)(),pthread_t *timer_t){
uv_timer_stop(&(_i->uvtimer));
pthread_cancel(*timer_t);
pthread_join(*timer_t,NULL);
if (func_on_stop != NULL) {
func_on_stop();
}
}
+17
View File
@@ -0,0 +1,17 @@
#include <stdio.h>
#include <uv.h>
#ifndef TIMER_H
#define TIMER_H
struct timer {
unsigned int tps;
uv_loop_t* loop;
uv_timer_t uvtimer;
void (*func)(void*);
};
struct timer init_timer(void (*func)(),void* args,unsigned int tps);
void start_timer(struct timer* _i,void (*func_on_start)(),pthread_t *timer_t);
void stop_timer(struct timer* _i,void (*func_on_stop)(),pthread_t *timer_t);
#endif
+17
View File
@@ -0,0 +1,17 @@
#include "tools.h"
struct Vector2 Vector2_quarter(const struct Vector2 _i){
return (struct Vector2){_i.x*0.5f,_i.y*0.5f};
}
struct Vector2 Vector2_add(const struct Vector2 _i1,const struct Vector2 _i2){
return (struct Vector2){_i1.x+_i2.x,_i1.y+_i2.y};
}
struct Vector2 Vector2_minus(const struct Vector2 _i1,const struct Vector2 _i2){
return (struct Vector2){_i1.x-_i2.x,_i1.y-_i2.y};
}
struct Vector2 Vector2_multi(const struct Vector2 _i1,const struct Vector2 _i2){
return (struct Vector2){_i1.x*_i2.x,_i1.y*_i2.y};
}
+11
View File
@@ -0,0 +1,11 @@
#include "raylib.h"
#ifndef TOOLS_H
#define TOOLS_H
struct Vector2 Vector2_quarter(const struct Vector2 _i);
struct Vector2 Vector2_add(const struct Vector2 _i1,const struct Vector2 _i2);
struct Vector2 Vector2_minus(const struct Vector2 _i1,const struct Vector2 _i2);
struct Vector2 Vector2_multi(const struct Vector2 _i1,const struct Vector2 _i2);
#endif
+294
View File
@@ -0,0 +1,294 @@
#include "ui.h"
struct Color text_color = {255, 255, 255, 255};
float screen_scale = 1.0f;
struct Color background_color = {0,0,0,255};
struct Vector2 wide_button_size = {420,50};
struct Vector2 normal_button_size = {200,50};
struct Vector2 square_button_size = {50,50};
int CodepointToUtf8(int codepoint, char *out) {
if (codepoint <= 0x7F) {
out[0] = (char)codepoint;
return 1;
} else if (codepoint <= 0x7FF) {
out[0] = (char)(0xC0 | (codepoint >> 6));
out[1] = (char)(0x80 | (codepoint & 0x3F));
return 2;
} else if (codepoint <= 0xFFFF) {
out[0] = (char)(0xE0 | (codepoint >> 12));
out[1] = (char)(0x80 | ((codepoint >> 6) & 0x3F));
out[2] = (char)(0x80 | (codepoint & 0x3F));
return 3;
} else if (codepoint <= 0x10FFFF) {
out[0] = (char)(0xF0 | (codepoint >> 18));
out[1] = (char)(0x80 | ((codepoint >> 12) & 0x3F));
out[2] = (char)(0x80 | ((codepoint >> 6) & 0x3F));
out[3] = (char)(0x80 | (codepoint & 0x3F));
return 4;
}
return 0;
}
// 计算布局、绘制按钮,并将布局数据存入 btn(后续可直接用 ReDrawButton)
void InitAndDrawButtonEx(struct Button *btn,struct Vector2 pos, struct Vector2 size,struct Texture normal, struct Texture pressed,char *text, struct Font font, float fontSize, float spacing){
float scaleW = size.x / normal.width;
float scaleH = size.y / normal.height;
float scale = (size.x / size.y < (float)normal.width / normal.height) ? scaleW : scaleH;
Vector2 texPos = pos;
if (size.x / size.y < (float)normal.width / normal.height) {
texPos.y += (size.y - normal.height * scale) * 0.5f; // 上下留空
} else {
texPos.x += (size.x - normal.width * scale) * 0.5f; // 左右留空
}
Vector2 textSize = MeasureTextEx(font, text, fontSize, spacing);
Vector2 textPos = texPos;
textPos.x += (size.x - textSize.x) * 0.5f;
textPos.y += (size.y - textSize.y) * 0.5f;
btn->pos = pos;
btn->size = size;
btn->scale = scale;
btn->pos_fixed = texPos;
btn->pos_font_fixed = textPos;
btn->normaltexture = normal;
btn->pressedtexture = pressed;
btn->text = text;
btn->font = font;
btn->fontSize = fontSize;
btn->spacing = spacing;
DrawTextureEx(btn->normaltexture, btn->pos_fixed, 0.0f, scale, WHITE);
DrawTextEx(btn->font, btn->text, btn->pos_font_fixed, fontSize, spacing,text_color);
}
void ReDrawButton(const struct Button *btn){
struct Texture tex = isButtonDown(btn) ? btn->pressedtexture : btn->normaltexture;
DrawTextureEx(tex, btn->pos_fixed, 0.0f, btn->scale, WHITE);
DrawTextEx(btn->font, btn->text, btn->pos_font_fixed, btn->fontSize, btn->spacing, text_color);
}
bool isButtonDown(const struct Button *button){
if(!IsMouseButtonDown(MOUSE_LEFT_BUTTON)){
return false;
}
struct Vector2 pos = GetMousePosition();
if(pos.x>button->pos.x+button->size.x||pos.x<button->pos.x){
return false;
}
if(pos.y<button->pos.y||pos.y>button->pos.y+button->size.y){
return false;
}
return true;
}
struct Vector2 zoom(struct Vector2 _i){
return (struct Vector2){_i.x*screen_scale,_i.y*screen_scale};
}
struct Vector2 button_in_grid_pos(struct Vector2 center,struct Vector2 size,struct Vector2 index,struct Vector2 button_size){
return (struct Vector2){((index.x-size.x)*20+(index.x-1-size.x*0.5f)*button_size.x)*screen_scale+center.x,((index.y-size.y)*20+(index.y-1-size.y*0.5f)*button_size.y)*screen_scale+center.y};
}
bool isButtonPressed(const struct Button *button){
if(!IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
return false;
}
struct Vector2 pos = GetMousePosition();
if(pos.x>button->pos.x+button->size.x||pos.x<button->pos.x){
return false;
}
if(pos.y<button->pos.y||pos.y>button->pos.y+button->size.y){
return false;
}
return true;
}
static float measure_skipped(const char *text, int skip, struct Font font, float fontSize, float spacing) {
char buf[TEXT_MAX_LEN];
if(half_clock){
snprintf(buf, sizeof(buf), "%s_", text + skip);
}else{
snprintf(buf, sizeof(buf), "%s ", text + skip);
}
return MeasureTextEx(font, buf, fontSize, spacing).x;
}
static int find_min_skip(const char *text, float limit_width, Font font, float fontSize, float spacing) {
int low = 0;
int high = (int)strlen(text);
if (measure_skipped(text, high, font, fontSize, spacing) > limit_width) {
return high;
}
while (low < high) {
int mid = low + (high - low) / 2;
if (measure_skipped(text, mid, font, fontSize, spacing) > limit_width)
low = mid + 1;
else
high = mid;
}
return low;
}
void DrawInputBoxEx(struct Input_Box *box){
DrawTextureEx(box->left_texture, box->pos, 0.0f, box->scale, WHITE);
float tileWidth = 5.0f;
for (int i = (int)tileWidth; i < box->size.x / box->scale - tileWidth; i++) {
DrawTextureEx(box->center_texture,(Vector2){ box->pos.x + box->scale * i, box->pos.y },0.0f, box->scale, WHITE);
}
DrawTextureEx(box->right_texture,(Vector2){ box->pos.x + box->size.x - box->scale * tileWidth, box->pos.y },0.0f, box->scale, WHITE);
float limitWidth = box->size.x - box->size.y * 0.5f;
box->cuted_len = find_min_skip(box->text, limitWidth, box->font, box->fontSize, box->spacing);
if(half_clock){
snprintf(box->text_show, TEXT_MAX_LEN, "%s_", box->text + box->cuted_len);
}else{
snprintf(box->text_show, TEXT_MAX_LEN, "%s ", box->text + box->cuted_len);
}
box->last_text = box->text;
box->last_limit_width = limitWidth;
box->last_font_size = box->fontSize;
DrawTextEx(box->font, box->text_show, box->pos_text_fixed,box->fontSize, box->spacing, text_color);
}
void InitAndDrawInputBox(struct Input_Box *box,struct Vector2 pos, struct Vector2 size,struct Texture left, struct Texture center, struct Texture right,struct Font font, float fontSize, float spacing,char *text){
// 基本属性
box->pos = pos;
box->size = size;
box->left_texture = left;
box->center_texture = center;
box->right_texture = right;
box->font = font;
box->fontSize = fontSize;
box->spacing = spacing;
box->text = text;
float scale = size.y / left.height;
box->scale = scale;
box->pos_text_fixed = (Vector2){ pos.x + size.y * 0.25f, pos.y + size.y * 0.25f };
box->last_text = NULL;
box->last_limit_width = -1.0f;
box->last_font_size = -1.0f;
DrawInputBoxEx(box);
}
void ReDrawInputBox(struct Input_Box *box,const struct Input_Box *focus){
DrawTextureEx(box->left_texture, box->pos, 0.0f, box->scale, WHITE);
float tileWidth = 5.0f;
for (int i = (int)tileWidth; i < box->size.x / box->scale - tileWidth; i++) {
DrawTextureEx(box->center_texture,(Vector2){ box->pos.x + box->scale * i, box->pos.y },0.0f, box->scale, WHITE);
}
DrawTextureEx(box->right_texture,(Vector2){ box->pos.x + box->size.x - box->scale * tileWidth, box->pos.y },0.0f, box->scale, WHITE);
if(half_clock&&focus==box){
snprintf(box->text_show, TEXT_MAX_LEN, "%s_", box->text + box->cuted_len);
}else{
snprintf(box->text_show, TEXT_MAX_LEN, "%s ", box->text + box->cuted_len);
}
DrawTextEx(box->font, box->text_show, box->pos_text_fixed,box->fontSize, box->spacing, text_color);
}
bool InitButtonList(struct Button_list *_bl,int count){
_bl->count = count;
_bl->_p = malloc(_bl->count*sizeof(struct Button*));
if(_bl->_p==NULL){
_bl->count=0;
return false;
}
for(int i=0;i<_bl->count;i++){
*(_bl->_p+i) = malloc(sizeof(struct Button));
memset(*(_bl->_p+i),0,sizeof(struct Button));
}
return true;
}
bool InitInputBoxList(struct Input_Box_list *_ib,int count){
_ib->count = count;
_ib->_p = malloc(_ib->count*sizeof(struct Input_Box*));
if(_ib->_p==NULL){
_ib->count=0;
return false;
}
for(int i=0;i<_ib->count;i++){
*(_ib->_p+i) = malloc(sizeof(struct Input_Box));
memset(*(_ib->_p+i),0,sizeof(struct Input_Box));
}
return true;
}
void UninButtonList(struct Button_list *_bl){
for(int i=0;i<_bl->count;i++){
free(*(_bl->_p+i));
}
free(_bl->_p);
_bl->_p=NULL;
_bl->count=0;
}
void UninInputBoxList(struct Input_Box_list *_ib){
for(int i=0;i<_ib->count;i++){
free(*(_ib->_p+i));
}
free(_ib->_p);
_ib->_p=NULL;
_ib->count=0;
}
void ReDrawButtonList(struct Button_list *_bl){
for(int i=0;i<_bl->count;i++){
ReDrawButton(*(_bl->_p+i));
}
}
void ReDrawInputBoxList(struct Input_Box_list *_ib,const struct Input_Box *focus){
for(int i=0;i<_ib->count;i++){
ReDrawInputBox(*(_ib->_p+i),focus);
}
}
static bool IsInputBoxPressed(const struct Input_Box *box){
if(!IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
return false;
}
struct Vector2 pos = GetMousePosition();
if(pos.x>box->pos.x+box->size.x||pos.x<box->pos.x){
return false;
}
if(pos.y<box->pos.y||pos.y>box->pos.y+box->size.y){
return false;
}
return true;
}
bool IsUnFocusInputBox(const struct Input_Box *focus){
if(!IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){
return false;
}
struct Vector2 pos = GetMousePosition();
if(pos.x>focus->pos.x+focus->size.x||pos.x<focus->pos.x){
return true;
}
if(pos.y<focus->pos.y||pos.y>focus->pos.y+focus->size.y){
return true;
}
return false;
}
struct Input_Box *WhichInputBoxIsPressed(struct Input_Box_list *_ib,struct Input_Box ***_focus){
struct Input_Box *_o=NULL;
for(int i=0;i<_ib->count;i++){
if(IsInputBoxPressed(*(_ib->_p+i))){
_o=*(_ib->_p+i);
*_focus = _ib->_p+i;
break;
}
}
return _o;
}
+86
View File
@@ -0,0 +1,86 @@
#include "raylib.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#ifndef UI_H
#define UI_H
#define TEXT_MAX_LEN 1024
struct Button {
struct Vector2 pos;
struct Vector2 pos_fixed;
struct Vector2 pos_font_fixed;
struct Vector2 size;
float scale;
struct Texture normaltexture;
struct Texture pressedtexture;
char *text;
struct Font font;
float fontSize;
float spacing;
};
struct Input_Box {
struct Vector2 pos;
struct Vector2 size;
struct Vector2 pos_text_fixed;
struct Texture left_texture;
struct Texture center_texture;
struct Texture right_texture;
float scale;
struct Font font;
char *text;
char text_show[TEXT_MAX_LEN];
float fontSize;
float spacing;
int cuted_len;
const char *last_text;
float last_limit_width;
float last_font_size;
};
struct Button_list {
struct Button **_p;
int count;
};
struct Input_Box_list {
struct Input_Box **_p;
int count;
};
extern float screen_scale;
extern struct Color background_color;
extern struct Vector2 square_button_size;
extern struct Vector2 wide_button_size;
extern struct Vector2 normal_button_size;
extern bool half_clock;
extern struct Color text_color;
int CodepointToUtf8(int codepoint, char *out);
void InitAndDrawButtonEx(struct Button *btn,struct Vector2 pos, struct Vector2 size,struct Texture normal, struct Texture pressed,char *text, struct Font font, float fontSize, float spacing);
void ReDrawButton(const struct Button *btn);
bool isButtonDown(const struct Button *button);
struct Vector2 zoom(struct Vector2 _i);
struct Vector2 button_in_grid_pos(struct Vector2 center,struct Vector2 size,struct Vector2 index,struct Vector2 button_size);
bool isButtonPressed(const struct Button *button);
void DrawInputBoxEx(struct Input_Box *box);
void InitAndDrawInputBox(struct Input_Box *box,struct Vector2 pos, struct Vector2 size,struct Texture left, struct Texture center, struct Texture right,struct Font font, float fontSize, float spacing,char *text);
void ReDrawInputBox(struct Input_Box *box,const struct Input_Box *focus);
bool InitButtonList(struct Button_list *_bl,int count);
bool InitInputBoxList(struct Input_Box_list *_ib,int count);
void UninButtonList(struct Button_list *_bl);
void UninInputBoxList(struct Input_Box_list *_ib);
void ReDrawButtonList(struct Button_list *_bl);
void ReDrawInputBoxList(struct Input_Box_list *_ib,const struct Input_Box *focus);
bool IsUnFocusInputBox(const struct Input_Box *focus);
struct Input_Box *WhichInputBoxIsPressed(struct Input_Box_list *_ib,struct Input_Box ***_focus);
#endif