Skip to content

Commit

Permalink
config now saves/applies
Browse files Browse the repository at this point in the history
  • Loading branch information
xtreme8000 committed Jul 13, 2018
1 parent 58b1925 commit 4e59531
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 58 deletions.
105 changes: 90 additions & 15 deletions src/config.c
Expand Up @@ -19,11 +19,71 @@

#include "common.h"

struct RENDER_OPTIONS settings;
struct RENDER_OPTIONS settings, settings_tmp;
struct list config_keys;
struct list config_settings;

struct list config_file;

static void config_sets(const char* name, const char* value) {
for(int k=0;k<list_size(&config_file);k++) {
struct config_file_entry* e = list_get(&config_file,k);
if(strcmp(e->name,name)==0) {
strncpy(e->value,value,sizeof(e->value)-1);
break;
}
}
}

static void config_seti(const char* name, int value) {
char tmp[32];
sprintf(tmp,"%i",value);
config_sets(name,tmp);
}

static void config_setf(const char* name, float value) {
char tmp[32];
sprintf(tmp,"%0.6f",value);
config_sets(name,tmp);
}

void config_save() {
config_sets("name",settings.name);
config_seti("xres",settings.window_width);
config_seti("yres",settings.window_height);
config_seti("windowed",!settings.fullscreen);
config_seti("multisamples",settings.multisamples);
config_seti("greedy_meshing",settings.greedy_meshing);
config_seti("vsync",settings.vsync);
config_setf("mouse_sensitivity",settings.mouse_sensitivity);
config_seti("show_news",settings.show_news);
config_seti("vol",settings.volume);
config_seti("show_fps",settings.show_fps);


FILE* f = fopen("config.ini","w");
char last_section[32] = {0};
for(int k=0;k<list_size(&config_file);k++) {
struct config_file_entry* e = list_get(&config_file,k);
if(strcmp(e->section,last_section)!=0) {
fprintf(f,"\r\n[%s]\r\n",e->section);
strcpy(last_section,e->section);
}
fprintf(f,"%s",e->name);
for(int l=0;l<31-strlen(e->name);l++)
fprintf(f," ");
fprintf(f,"= %s\r\n",e->value);
}
fclose(f);
}

static int config_read_key(void* user, const char* section, const char* name, const char* value) {
struct config_file_entry e;
strncpy(e.section,section,sizeof(e.section)-1);
strncpy(e.name,name,sizeof(e.name)-1);
strncpy(e.value,value,sizeof(e.value)-1);
list_add(&config_file,&e);

if(!strcmp(section,"client")) {
if(!strcmp(name,"name")) {
strcpy(settings.name,value);
Expand Down Expand Up @@ -53,8 +113,8 @@ static int config_read_key(void* user, const char* section, const char* name, co
settings.show_news = atoi(value);
}
if(!strcmp(name,"vol")) {
sound_global_volume = max(min(atoi(value),10),0);
sound_volume(sound_global_volume/10.0F);
settings.volume = max(min(atoi(value),10),0);
sound_volume(settings.volume/10.0F);
}
if(!strcmp(name,"show_fps")) {
settings.show_fps = atoi(value);
Expand Down Expand Up @@ -114,6 +174,12 @@ void config_key_reset_togglestates() {
}

void config_reload() {
if(!list_created(&config_file))
list_create(&config_file,sizeof(struct config_file_entry));
else
list_clear(&config_file);


if(!list_created(&config_keys))
list_create(&config_keys,sizeof(struct config_key_pair));
else
Expand Down Expand Up @@ -205,39 +271,50 @@ void config_reload() {
config_register_key(WINDOW_KEY_HIDEHUD,GLFW_KEY_F6,"hide_hud",1);
#endif

list_create(&config_settings,sizeof(struct config_setting));
ini_parse("config.ini",config_read_key,NULL);

if(!list_created(&config_settings))
list_create(&config_settings,sizeof(struct config_setting));
else
list_clear(&config_settings);

list_add(&config_settings,&(struct config_setting){
.value=settings.name,
.value=settings_tmp.name,
.type=CONFIG_TYPE_STRING,
.max=sizeof(settings.name)-1,
.name="Name",
.help="ingame player name"
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.mouse_sensitivity,
.value=&settings_tmp.mouse_sensitivity,
.type=CONFIG_TYPE_FLOAT,
.max=INT_MAX,
.name="Mouse sensitivity"
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.window_width,
.value=&settings_tmp.volume,
.type=CONFIG_TYPE_INT,
.max=10,
.name="Volume"
});
list_add(&config_settings,&(struct config_setting){
.value=&settings_tmp.window_width,
.type=CONFIG_TYPE_INT,
.max=INT_MAX,
.name="Game width",
.defaults=640,800,854,1024,1280,1920,
.defaults_length=6
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.window_height,
.value=&settings_tmp.window_height,
.type=CONFIG_TYPE_INT,
.max=INT_MAX,
.name="Game height",
.defaults=480,600,720,768,1024,1080,
.defaults_length=6
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.vsync,
.value=&settings_tmp.vsync,
.type=CONFIG_TYPE_INT,
.max=INT_MAX,
.name="V-Sync",
Expand All @@ -246,13 +323,13 @@ void config_reload() {
.defaults_length=5
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.fullscreen,
.value=&settings_tmp.fullscreen,
.type=CONFIG_TYPE_INT,
.max=1,
.name="Fullscreen"
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.multisamples,
.value=&settings_tmp.multisamples,
.type=CONFIG_TYPE_INT,
.max=16,
.name="Multisamples",
Expand All @@ -261,19 +338,17 @@ void config_reload() {
.defaults_length=6
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.show_fps,
.value=&settings_tmp.show_fps,
.type=CONFIG_TYPE_INT,
.max=1,
.name="Show fps",
.help="show your current fps and ping"
});
list_add(&config_settings,&(struct config_setting){
.value=&settings.show_news,
.value=&settings_tmp.show_news,
.type=CONFIG_TYPE_INT,
.max=1,
.name="Show news",
.help="opens the bns news on exit"
});

ini_parse("config.ini",config_read_key,NULL);
}
10 changes: 9 additions & 1 deletion src/config.h
Expand Up @@ -17,6 +17,12 @@
along with BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/

struct config_file_entry {
char section[32];
char name[32];
char value[32];
};

extern struct RENDER_OPTIONS {
char name[16];
int opengl14;
Expand All @@ -34,7 +40,8 @@ extern struct RENDER_OPTIONS {
float mouse_sensitivity;
int show_news;
int show_fps;
} settings;
int volume;
} settings, settings_tmp;

extern struct list config_keys;

Expand Down Expand Up @@ -68,3 +75,4 @@ int config_key_translate(int key, int dir);
struct config_key_pair* config_key(int key);
void config_key_reset_togglestates();
void config_reload(void);
void config_save(void);
76 changes: 45 additions & 31 deletions src/hud.c
Expand Up @@ -1080,15 +1080,15 @@ static void hud_ingame_keyboard(int key, int action, int mods) {
}

if(key==WINDOW_KEY_VOLUME_UP) {
sound_global_volume = min(sound_global_volume+1,10);
settings.volume = min(settings.volume+1,10);
}
if(key==WINDOW_KEY_VOLUME_DOWN) {
sound_global_volume = max(sound_global_volume-1,0);
settings.volume = max(settings.volume-1,0);
}
if(key==WINDOW_KEY_VOLUME_UP || key==WINDOW_KEY_VOLUME_DOWN) {
sound_volume(sound_global_volume/10.0F);
sound_volume(settings.volume/10.0F);
char volstr[64];
sprintf(volstr,"Volume: %i",(int)sound_global_volume);
sprintf(volstr,"Volume: %i",settings.volume);
chat_add(0,0x0000FF,volstr);
}

Expand Down Expand Up @@ -1665,25 +1665,30 @@ struct hud hud_serverlist = {

static struct config_setting* hud_settings_edit = NULL;

static void hud_settings_render(float scalex, float scaley) {
glColor3f(0.5F,0.5F,0.5F);
float t = window_time()*0.03125F;
texture_draw_sector(&texture_ui_bg,0.0F,settings.window_height,settings.window_width,settings.window_height,t,t,settings.window_width/512.0F,settings.window_height/512.0F);

glColor4f(0.0F,0.0F,0.0F,0.66F);
glEnable(GL_BLEND);
texture_draw_empty((settings.window_width-640*scaley)/2.0F,550*scaley,640*scaley,600*scaley);
glDisable(GL_BLEND);
static void hud_settings_init() {
memcpy(&settings_tmp,&settings,sizeof(struct RENDER_OPTIONS));
}

glColor3f(1.0F,1.0F,0.0F);
font_render((settings.window_width-600*scaley)/2.0F+0*scaley,535*scaley,36*scaley,"Settings");
glColor3f(0.5F,0.5F,0.5F);
font_centered((settings.window_width-600*scaley)/2.0F+210*scaley,535*scaley-12*scaley,20*scaley,"Controls");
font_centered((settings.window_width-600*scaley)/2.0F+320*scaley,535*scaley-12*scaley,20*scaley,"Server list");
static void hud_settings_render(float scalex, float scaley) {
glColor3f(0.5F,0.5F,0.5F);
float t = window_time()*0.03125F;
texture_draw_sector(&texture_ui_bg,0.0F,settings.window_height,settings.window_width,settings.window_height,t,t,settings.window_width/512.0F,settings.window_height/512.0F);

glColor4f(0.0F,0.0F,0.0F,0.66F);
glEnable(GL_BLEND);
texture_draw_empty((settings.window_width-640*scaley)/2.0F,550*scaley,640*scaley,600*scaley);
glDisable(GL_BLEND);

glColor3f(1.0F,1.0F,0.0F);
font_render((settings.window_width-600*scaley)/2.0F+0*scaley,535*scaley,36*scaley,"Settings");
font_centered((settings.window_width-600*scaley)/2.0F+174*scaley,70*scaley,30*scaley,"Apply");
glColor3f(0.5F,0.5F,0.5F);
font_centered((settings.window_width-600*scaley)/2.0F+210*scaley,535*scaley-12*scaley,20*scaley,"Controls");
font_centered((settings.window_width-600*scaley)/2.0F+320*scaley,535*scaley-12*scaley,20*scaley,"Server list");

for(int k=0;k<list_size(&config_settings);k++) {
struct config_setting* a = list_get(&config_settings,k);
glColor3f(1.0F,1.0F,0.0F);
glColor3f(1.0F,1.0F,1.0F);
font_render((settings.window_width-600*scaley)/2.0F,(460-40*k)*scaley,16*scaley,a->name);

char tmp[32];
Expand Down Expand Up @@ -1769,7 +1774,7 @@ static int is_float(char* x) {
static void hud_settings_keyboard(int key, int action, int mods) {
if(hud_settings_edit && action!=WINDOW_RELEASE && key==WINDOW_KEY_BACKSPACE) {
size_t text_len = strlen(chat[0][0]);
if(text_len>0){
if(text_len>0) {
chat[0][0][text_len-1] = 0;
}
}
Expand Down Expand Up @@ -1814,6 +1819,15 @@ static void hud_settings_mouseclick(int button, int action, int mods) {

y = settings.window_height-y;

if(x>=(settings.window_width-600*scaley)/2.0F+174*scaley-font_length(30*scaley,"Apply")/2
&& x<(settings.window_width-600*scaley)/2.0F+174*scaley+font_length(30*scaley,"Apply")/2
&& y>=40*scaley && y<70*scaley) {
memcpy(&settings,&settings_tmp,sizeof(struct RENDER_OPTIONS));
window_fromsettings();
sound_volume(settings.volume/10.0F);
config_save();
}

for(int k=0;k<list_size(&config_settings);k++) {
struct config_setting* a = list_get(&config_settings,k);
if(is_inside_centered(x,y,
Expand Down Expand Up @@ -1845,7 +1859,7 @@ static void hud_settings_mouseclick(int button, int action, int mods) {
if(is_inside_centered(x,y,
(settings.window_width-600*scaley)/2.0F+290*scaley,
(450-40*k)*scaley,32*scaley,32*scaley)) { //right arrow pressed
int next = (*(int*)a->value)+1;
int next = a->defaults_length?(*(int*)a->value):((*(int*)a->value)+1);
int diff = INT_MAX;
for(int l=0;l<a->defaults_length;l++) {
if(a->defaults[l]>*(int*)a->value && a->defaults[l]-*(int*)a->value<diff) {
Expand All @@ -1858,7 +1872,7 @@ static void hud_settings_mouseclick(int button, int action, int mods) {
if(is_inside_centered(x,y,
(settings.window_width-600*scaley)/2.0F+190*scaley,
(450-40*k)*scaley,32*scaley,32*scaley)) { //left arrow pressed
int next = (*(int*)a->value)-1;
int next = a->defaults_length?(*(int*)a->value):((*(int*)a->value)-1);
int diff = INT_MAX;
for(int l=0;l<a->defaults_length;l++) {
if(a->defaults[l]<*(int*)a->value && *(int*)a->value-a->defaults[l]<diff) {
Expand Down Expand Up @@ -1888,15 +1902,15 @@ static void hud_settings_mouseclick(int button, int action, int mods) {
}

struct hud hud_settings = {
(void*)NULL,
(void*)NULL,
hud_settings_render,
hud_settings_keyboard,
(void*)NULL,
hud_settings_mouseclick,
(void*)NULL,
0,
0
hud_settings_init,
(void*)NULL,
hud_settings_render,
hud_settings_keyboard,
(void*)NULL,
hud_settings_mouseclick,
(void*)NULL,
0,
0
};


Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Expand Up @@ -575,7 +575,8 @@ int main(int argc, char** argv) {
settings.greedy_meshing = 0;
settings.mouse_sensitivity = MOUSE_SENSITIVITY;
settings.show_news = 1;
settings.show_fps = 1;
settings.show_fps = 0;
settings.volume = 0;
strcpy(settings.name,"DEV_CLIENT");

config_reload();
Expand Down
5 changes: 1 addition & 4 deletions src/sound.c
Expand Up @@ -76,8 +76,6 @@ struct Sound_wav sound_debris;
struct Sound_wav sound_bounce;
struct Sound_wav sound_impact;

float sound_global_volume = 10.0F;

void sound_volume(float vol) {
if(sound_enabled)
alListenerf(AL_GAIN,vol);
Expand All @@ -95,7 +93,6 @@ struct Sound_source* sound_create(struct Sound_source* s, int option, struct Sou
return sound_createEx(s,option,w,x,y,z,0.0F,0.0F,0.0F);
}


static struct Sound_source dummy;
struct Sound_source* sound_createEx(struct Sound_source* s, int option, struct Sound_wav* w, float x, float y, float z, float vx, float vy, float vz) {
if(!sound_enabled)
Expand Down Expand Up @@ -227,7 +224,7 @@ void sound_init() {

alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);

sound_volume(sound_global_volume/10.0F);
sound_volume(settings.volume/10.0F);

sound_load(&sound_footstep1,"wav/footstep1.wav",0.1F,32.0F);
sound_load(&sound_footstep2,"wav/footstep2.wav",0.1F,32.0F);
Expand Down
3 changes: 0 additions & 3 deletions src/sound.h
Expand Up @@ -101,9 +101,6 @@ extern struct Sound_wav sound_debris;
extern struct Sound_wav sound_bounce;
extern struct Sound_wav sound_impact;

extern float sound_global_volume;


void sound_volume(float vol);
struct Sound_source* sound_create(struct Sound_source* s, int option, struct Sound_wav* w, float x, float y, float z);
struct Sound_source* sound_createEx(struct Sound_source* s, int option, struct Sound_wav* w, float x, float y, float z, float vx, float vy, float vz);
Expand Down

0 comments on commit 4e59531

Please sign in to comment.