Added writing cvars back to file from memory

dev
shariq 8 years ago
parent c81d3f9d12
commit a573482f4c
  1. 21
      assets/config.cfg
  2. 6
      orgfile.org
  3. 40
      src/config_vars.c
  4. 2
      src/config_vars.h
  5. 7
      src/editor.c

@ -1,18 +1,13 @@
# This is a comment msaa_levels: 4
render_width: 800 debug_draw_color: 0.000, 1.000, 0.000, 1.000
render_width: 800
fog_density: 0.0020
render_height: 600 render_height: 600
#fog_mode : 0 = off, 1 = linear, 2 = exponential, 3 = exponential squared
fog_mode: 2 fog_mode: 2
fog_color: 0.5 0.2 0.2 fog_color: 0.500, 0.200, 0.200
fog_density: 0.002 fog_max_dist: 150.0000
fog_start_dist: 20
fog_max_dist: 150
# There can be comments or empty newlines in between unlike entity definitions
ambient_light: 0.1 0.1 0.12
msaa_enabled: true msaa_enabled: true
msaa_levels: 4 fog_start_dist: 20.0000
debug_draw_enabled: false debug_draw_enabled: false
debug_draw_mode: 0 debug_draw_mode: 0
debug_draw_color: 0 1 0 1 ambient_light: 0.100, 0.100, 0.120

@ -140,7 +140,8 @@ x Font atlas proper cleanup
- Custom rendering for gui - Custom rendering for gui
** TODO Allow passsing base path as commandline argument? ** TODO Allow passsing base path as commandline argument?
** TODO Show SDL dialogbox if we cannot launch at all? ** TODO Show SDL dialogbox if we cannot launch at all?
** TODO Writing back to config file + maybe with comments intact? ** DONE Writing back to config file
- State "DONE" from "TODO" [2017-05-08 Mon 00:57]
** DONE Reading from config file ** DONE Reading from config file
- State "DONE" from "TODO" [2017-05-07 Sun 23:52] - State "DONE" from "TODO" [2017-05-07 Sun 23:52]
** DONE Variant -> String conversion procedure. Use in editor for debug var slots ** DONE Variant -> String conversion procedure. Use in editor for debug var slots
@ -166,6 +167,7 @@ x Font atlas proper cleanup
** TODO Sprite sheet animations ** TODO Sprite sheet animations
** TODO Replace orgfile with simple text readme and reduce duplication? ** TODO Replace orgfile with simple text readme and reduce duplication?
** TODO Ray picking ** TODO Ray picking
** TODO Remove reduntant "settings" structures and move all configuration stuff to config variables
** TODO Shadow maps ** TODO Shadow maps
** TODO Log output to file on every run ** TODO Log output to file on every run
** TODO Print processor stats and machine capabilites RAM etc on every run to log. ** TODO Print processor stats and machine capabilites RAM etc on every run to log.
@ -175,7 +177,7 @@ x Font atlas proper cleanup
- Makefile additions. Try to compile game as a dynamically loaded library with ability to reload on recompile - Makefile additions. Try to compile game as a dynamically loaded library with ability to reload on recompile
- Separation between game and engine base - Separation between game and engine base
- Game .so with init, update and cleanup functions - Game .so with init, update and cleanup functions
- Configuration files and "cvars" load/reload x Configuration files and "cvars" load/reload
- Keybindings in config - Keybindings in config
- Log output on every run. - Log output on every run.
** TODO Do input maps really need to be queried by their string names? ** TODO Do input maps really need to be queried by their string names?

@ -81,28 +81,13 @@ int config_vars_load(const char* filename)
key_str[key_str_len] = '\0'; key_str[key_str_len] = '\0';
value_str++; /* Ignore the colon(:) */ value_str++; /* Ignore the colon(:) */
//char* key = NULL;
struct Variant* value = hashmap_value_get(cvars, key_str); struct Variant* value = hashmap_value_get(cvars, key_str);
if(!value) if(!value)
{ {
log_warning("Unknown value in config file %s, line %d", filename, current_line); log_warning("Unknown value in config file %s, line %d", filename, current_line);
continue; continue;
} }
variant_from_str(value, value_str, value->type); variant_from_str(value, value_str, value->type);
/* HASHMAP_FOREACH(cvars, key, value) */
/* { */
/* if(strncmp(line_buffer, key, strlen(key)) == 0) */
/* { */
/* char* value_str = strstr(line_buffer, ":"); */
/* if(value_str) */
/* { */
/* value_str++; */
/* variant_from_str(value, value_str, value->type); */
/* } */
/* break; */
/* } */
/* } */
} }
success = 1; success = 1;
@ -110,7 +95,28 @@ int config_vars_load(const char* filename)
return success; return success;
} }
void config_vars_save(const char* filename) int config_vars_save(const char* filename)
{ {
int success = 0;
FILE* config_file = io_file_open(filename, "w");
if(!config_file)
{
log_error("config:vars_save", "Failed to open config file %s for writing");
return success;
}
char* key = NULL;
struct Variant* value = NULL;
char variant_str[MAX_VARIANT_STR_LEN];
HASHMAP_FOREACH(cvars, key, value)
{
memset(variant_str, '\0', MAX_VARIANT_STR_LEN);
variant_to_str(value, variant_str, MAX_VARIANT_STR_LEN);
log_message("Writing : %s: %s", key, variant_str);
fprintf(config_file, "%s: %s\n", key, variant_str);
}
log_message("Config file %s written.", filename);
success = 1;
fclose(config_file);
return success;
} }

@ -6,7 +6,7 @@ struct Hashmap;
void config_vars_init(void); void config_vars_init(void);
void config_vars_cleanup(void); void config_vars_cleanup(void);
int config_vars_load(const char* filename); int config_vars_load(const char* filename);
void config_vars_save(const char* filename); int config_vars_save(const char* filename);
struct Hashmap* config_vars_get(void); struct Hashmap* config_vars_get(void);

@ -16,6 +16,7 @@
#include "gui.h" #include "gui.h"
#include "array.h" #include "array.h"
#include "variant.h" #include "variant.h"
#include "config_vars.h"
#include "string_utils.h" #include "string_utils.h"
#include <stdio.h> #include <stdio.h>
@ -147,7 +148,7 @@ void editor_update(float dt)
if(nk_begin(context, "Top_Panel", nk_recti(0, 0, win_width, win_height - (win_height - editor_state.top_panel_height)), if(nk_begin(context, "Top_Panel", nk_recti(0, 0, win_width, win_height - (win_height - editor_state.top_panel_height)),
NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR)) NK_WINDOW_BORDER | NK_WINDOW_NO_SCROLLBAR))
{ {
float ratios[] = {0.1f, 0.1f, 0.7f, 0.1f}; float ratios[] = {0.1f, 0.1f, 0.1f, 0.6f, 0.1f};
static int frames = 0; static int frames = 0;
static int fps = 0; static int fps = 0;
static float seconds = 0.f; static float seconds = 0.f;
@ -159,11 +160,13 @@ void editor_update(float dt)
seconds = 0.f; seconds = 0.f;
frames = 0; frames = 0;
} }
nk_layout_row(context, NK_DYNAMIC, 22, 4, ratios); nk_layout_row(context, NK_DYNAMIC, 22, sizeof(ratios), ratios);
if(nk_button_label(context, "Render Settings")) if(nk_button_label(context, "Render Settings"))
editor_state.renderer_settings_window = !editor_state.renderer_settings_window; editor_state.renderer_settings_window = !editor_state.renderer_settings_window;
if(nk_button_label(context, "Debug Variables")) if(nk_button_label(context, "Debug Variables"))
editor_state.debug_vars_window = !editor_state.debug_vars_window; editor_state.debug_vars_window = !editor_state.debug_vars_window;
if(nk_button_label(context, "Save config"))
config_vars_save("config.cfg");
nk_spacing(context, 1); nk_spacing(context, 1);
nk_labelf(context, NK_TEXT_ALIGN_RIGHT | NK_TEXT_ALIGN_MIDDLE, "FPS : %.d", fps); nk_labelf(context, NK_TEXT_ALIGN_RIGHT | NK_TEXT_ALIGN_MIDDLE, "FPS : %.d", fps);
} }

Loading…
Cancel
Save