Files
industrial-genesis/src/main.c
T
2026-09-12 05:38:04 +09:00

650 lines
44 KiB
C

#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;
}