Changed how config files or cvars are used in the code

dev
Shariq Shah 6 years ago
parent 124b02e566
commit 1d86626840
  1. 8
      src/game/editor.c
  2. 3
      src/game/editor.h
  3. 3
      src/game/game.c
  4. 4
      src/game/game.h
  5. 4
      src/game/player.c
  6. 47
      src/game/renderer.c
  7. 3
      src/game/scene.c
  8. 18
      src/system/config_vars.c
  9. 10
      src/system/config_vars.h
  10. 16
      src/system/main.c
  11. 3
      todo.txt

@ -93,7 +93,7 @@ void editor_init(struct Editor* editor)
event_manager_subscribe(game_state_get()->event_manager, EVT_MOUSEBUTTON_RELEASED, &editor_on_mousebutton); event_manager_subscribe(game_state_get()->event_manager, EVT_MOUSEBUTTON_RELEASED, &editor_on_mousebutton);
} }
void editor_init_camera(struct Editor* editor) void editor_init_camera(struct Editor* editor, struct Hashmap* cvars)
{ {
struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR]; struct Camera* editor_camera = &game_state_get()->scene->cameras[CAM_EDITOR];
entity_rename(editor_camera, "Editor_Camera"); entity_rename(editor_camera, "Editor_Camera");
@ -103,9 +103,8 @@ void editor_init_camera(struct Editor* editor)
editor_camera->clear_color.z = 0.9f; editor_camera->clear_color.z = 0.9f;
editor_camera->clear_color.w = 1.f; editor_camera->clear_color.w = 1.f;
struct Hashmap* config = config_vars_get(); int render_width = hashmap_int_get(cvars, "render_width");
int render_width = hashmap_int_get(config, "render_width"); int render_height = hashmap_int_get(cvars, "render_height");
int render_height = hashmap_int_get(config, "render_height");
camera_attach_fbo(editor_camera, render_width, render_height, true, true, true); camera_attach_fbo(editor_camera, render_width, render_height, true, true, true);
vec3 cam_pos = {5.f, 20.f, 50.f}; vec3 cam_pos = {5.f, 20.f, 50.f};
@ -217,7 +216,6 @@ void editor_update(struct Editor* editor, float dt)
window_get_drawable_size(game_state->window, &win_width, &win_height); window_get_drawable_size(game_state->window, &win_width, &win_height);
int half_width = win_width / 2, half_height = win_height / 2; int half_width = win_width / 2, half_height = win_height / 2;
/* Top Panel */ /* Top Panel */
if(nk_begin(context, "Top Panel", nk_recti(0, 0, win_width, editor->top_panel_height), NK_WINDOW_NO_SCROLLBAR)) if(nk_begin(context, "Top Panel", nk_recti(0, 0, win_width, editor->top_panel_height), NK_WINDOW_NO_SCROLLBAR))
{ {

@ -6,6 +6,7 @@
struct Camera; struct Camera;
struct Entity; struct Entity;
struct Hashmap;
struct Editor struct Editor
{ {
@ -23,7 +24,7 @@ struct Editor
}; };
void editor_init(struct Editor* editor_state); void editor_init(struct Editor* editor_state);
void editor_init_camera(struct Editor* editor_state); void editor_init_camera(struct Editor* editor_state, struct Hashmap* cvars);
void editor_render(struct Editor* editor_state, struct Camera* active_camera); void editor_render(struct Editor* editor_state, struct Camera* active_camera);
void editor_update(struct Editor* editor_state, float dt); void editor_update(struct Editor* editor_state, float dt);
void editor_cleanup(struct Editor* editor_state); void editor_cleanup(struct Editor* editor_state);

@ -57,7 +57,7 @@ static void on_collision_test(struct Entity* this_ent, struct Entity* other_ent,
static struct Game_State* game_state = NULL; static struct Game_State* game_state = NULL;
bool game_init(struct Window* window) bool game_init(struct Window* window, struct Hashmap* cvars)
{ {
game_state = malloc(sizeof(*game_state)); game_state = malloc(sizeof(*game_state));
if(!game_state) if(!game_state)
@ -68,6 +68,7 @@ bool game_init(struct Window* window)
else else
{ {
game_state->window = window; game_state->window = window;
game_state->cvars = cvars;
game_state->is_initialized = false; game_state->is_initialized = false;
game_state->game_mode = GAME_MODE_GAME; game_state->game_mode = GAME_MODE_GAME;
game_state->renderer = calloc(1, sizeof(*game_state->renderer)); game_state->renderer = calloc(1, sizeof(*game_state->renderer));

@ -12,6 +12,7 @@ struct Console;
struct Gui; struct Gui;
struct Event_Manager; struct Event_Manager;
struct Editor; struct Editor;
struct Hashmap;
enum Game_Mode enum Game_Mode
{ {
@ -30,11 +31,12 @@ struct Game_State
struct Gui* gui; struct Gui* gui;
struct Event_Manager* event_manager; struct Event_Manager* event_manager;
struct Editor* editor; struct Editor* editor;
struct Hashmap* cvars;
}; };
struct Game_State* game_state_get(void); struct Game_State* game_state_get(void);
bool game_init(struct Window* window); bool game_init(struct Window* window, struct Hashmap* cvars);
bool game_run(void); bool game_run(void);
void game_cleanup(void); void game_cleanup(void);

@ -9,7 +9,7 @@
#include "../common/log.h" #include "../common/log.h"
#include "entity.h" #include "entity.h"
#include "../system/config_vars.h" #include "../system/config_vars.h"
#include "game.h"
void player_init(struct Player* player, struct Scene* scene) void player_init(struct Player* player, struct Scene* scene)
{ {
@ -18,7 +18,7 @@ void player_init(struct Player* player, struct Scene* scene)
player->base.id = 1; player->base.id = 1;
player->base.type = ET_PLAYER; player->base.type = ET_PLAYER;
struct Hashmap* config = config_vars_get(); struct Hashmap* config = game_state_get()->cvars;
player->move_speed = hashmap_int_get(config, "player_move_speed"); player->move_speed = hashmap_int_get(config, "player_move_speed");
player->move_speed_multiplier = hashmap_int_get(config, "player_move_speed_multiplier"); player->move_speed_multiplier = hashmap_int_get(config, "player_move_speed_multiplier");
player->turn_speed = hashmap_int_get(config, "player_turn_speed"); player->turn_speed = hashmap_int_get(config, "player_turn_speed");

@ -41,9 +41,11 @@ void renderer_init(struct Renderer* renderer)
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
event_manager_subscribe(game_state_get()->event_manager, EVT_WINDOW_RESIZED, &renderer_on_framebuffer_size_changed);
struct Hashmap* cvars = config_vars_get(); struct Game_State* game_state = game_state_get();
event_manager_subscribe(game_state->event_manager, EVT_WINDOW_RESIZED, &renderer_on_framebuffer_size_changed);
struct Hashmap* cvars = game_state->cvars;
renderer->settings.fog.mode = hashmap_int_get(cvars, "fog_mode"); renderer->settings.fog.mode = hashmap_int_get(cvars, "fog_mode");
renderer->settings.fog.density = hashmap_float_get(cvars, "fog_density"); renderer->settings.fog.density = hashmap_float_get(cvars, "fog_density");
renderer->settings.fog.start_dist = hashmap_float_get(cvars, "fog_start_dist"); renderer->settings.fog.start_dist = hashmap_float_get(cvars, "fog_start_dist");
@ -86,7 +88,6 @@ void renderer_init(struct Renderer* renderer)
array_free(indices); array_free(indices);
int width = -1, height = -1; int width = -1, height = -1;
struct Game_State* game_state = game_state_get();
window_get_size(game_state->window, &width, &height); window_get_size(game_state->window, &width, &height);
renderer->def_albedo_tex = texture_create("def_albedo_texture", renderer->def_albedo_tex = texture_create("def_albedo_texture",
TU_DIFFUSE, TU_DIFFUSE,
@ -340,15 +341,13 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
shader_unbind(); shader_unbind();
/* Debug Render */ /* Debug Render */
struct Hashmap* cvars = config_vars_get(); if(renderer->settings.debug_draw_enabled)
if(hashmap_bool_get(cvars, "debug_draw_enabled"))
{ {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
vec4 debug_draw_color = hashmap_vec4_get(cvars, "debug_draw_color");
shader_bind(renderer->debug_shader); shader_bind(renderer->debug_shader);
{ {
static mat4 mvp; static mat4 mvp;
shader_set_uniform_vec4(renderer->debug_shader, "debug_color", &debug_draw_color); shader_set_uniform_vec4(renderer->debug_shader, "debug_color", &renderer->settings.debug_draw_color);
for(int i = 0; i < MAX_STATIC_MESHES; i++) for(int i = 0; i < MAX_STATIC_MESHES; i++)
{ {
struct Static_Mesh* mesh = &scene->static_meshes[i]; struct Static_Mesh* mesh = &scene->static_meshes[i];
@ -359,7 +358,7 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
mat4_identity(&mvp); mat4_identity(&mvp);
mat4_mul(&mvp, &active_camera->view_proj_mat, &transform->trans_mat); mat4_mul(&mvp, &active_camera->view_proj_mat, &transform->trans_mat);
shader_set_uniform_mat4(renderer->debug_shader, "mvp", &mvp); shader_set_uniform_mat4(renderer->debug_shader, "mvp", &mvp);
geom_render(geometry, hashmap_int_get(cvars, "debug_draw_mode")); geom_render(geometry, renderer->settings.debug_draw_mode);
} }
} }
shader_unbind(); shader_unbind();
@ -367,7 +366,7 @@ void renderer_draw(struct Renderer* renderer, struct Scene* scene)
} }
// Debug Physics render // Debug Physics render
if(hashmap_bool_get(cvars, "debug_draw_physics")) if(renderer->settings.debug_draw_physics)
{ {
static vec4 physics_draw_color = { 0.f, 0.f, 1.f, 1.f }; static vec4 physics_draw_color = { 0.f, 0.f, 1.f, 1.f };
for(int i = 0; i < MAX_STATIC_MESHES; i++) for(int i = 0; i < MAX_STATIC_MESHES; i++)
@ -517,33 +516,3 @@ void renderer_debug_draw_enabled(struct Renderer* renderer, bool enabled)
{ {
renderer->settings.debug_draw_mode = enabled; renderer->settings.debug_draw_mode = enabled;
} }
/* void renderer_settings_get(struct Render_Settings* settings) */
/* { */
/* struct Hashmap* cvars = platform->config.get(); */
/* settings->fog.mode = hashmap_int_get(cvars, "fog_mode"); */
/* settings->fog.density = hashmap_float_get(cvars, "fog_density"); */
/* settings->fog.start_dist = hashmap_float_get(cvars, "fog_start_dist"); */
/* settings->fog.max_dist = hashmap_float_get(cvars, "fog_max_dist"); */
/* settings->fog.color = hashmap_vec3_get(cvars, "fog_color"); */
/* settings->debug_draw_enabled = hashmap_bool_get(cvars, "debug_draw_enabled"); */
/* settings->debug_draw_physics = hashmap_bool_get(cvars, "debug_draw_physics"); */
/* settings->debug_draw_mode = hashmap_int_get(cvars, "debug_draw_mode"); */
/* settings->debug_draw_color = hashmap_vec4_get(cvars, "debug_draw_color"); */
/* settings->ambient_light = hashmap_vec3_get(cvars, "ambient_light"); */
/* } */
/* void renderer_settings_set(const struct Render_Settings* settings) */
/* { */
/* struct Hashmap* cvars = platform->config.get(); */
/* hashmap_int_set(cvars, "fog_mode", settings->fog.mode); */
/* hashmap_float_set(cvars, "fog_density", settings->fog.density); */
/* hashmap_float_set(cvars, "fog_start_dist", settings->fog.start_dist); */
/* hashmap_float_set(cvars, "fog_max_dist", settings->fog.max_dist); */
/* hashmap_vec3_set(cvars, "fog_color", &settings->fog.color); */
/* hashmap_bool_set(cvars, "debug_draw_enabled", settings->debug_draw_enabled); */
/* hashmap_bool_set(cvars, "debug_draw_physics", settings->debug_draw_physics); */
/* hashmap_int_set(cvars, "debug_draw_mode", settings->debug_draw_mode); */
/* hashmap_vec4_set(cvars, "debug_draw_color", &settings->debug_draw_color); */
/* hashmap_vec3_set(cvars, "ambient_light", &settings->ambient_light); */
/* } */

@ -53,7 +53,8 @@ void scene_init(struct Scene* scene)
} }
player_init(&scene->player, scene); player_init(&scene->player, scene);
editor_init_camera(game_state_get()->editor); struct Game_State* game_state = game_state_get();
editor_init_camera(game_state->editor, game_state->cvars);
scene->active_camera_index = game_state_get()->game_mode == GAME_MODE_GAME ? CAM_GAME : CAM_EDITOR; scene->active_camera_index = game_state_get()->game_mode == GAME_MODE_GAME ? CAM_GAME : CAM_EDITOR;
} }

@ -10,13 +10,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_LINE_LEN 512 void config_vars_init(struct Hashmap* cvars)
static struct Hashmap* cvars = NULL;
void config_vars_init(void)
{ {
cvars = hashmap_new();
/* Initialize with default values incase there is no config file */ /* Initialize with default values incase there is no config file */
hashmap_int_set(cvars, "render_width", 1024); hashmap_int_set(cvars, "render_width", 1024);
hashmap_int_set(cvars, "render_height", 768); hashmap_int_set(cvars, "render_height", 768);
@ -38,17 +33,12 @@ void config_vars_init(void)
hashmap_float_set(cvars, "player_turn_speed", 5.f); hashmap_float_set(cvars, "player_turn_speed", 5.f);
} }
void config_vars_cleanup(void) void config_vars_cleanup(struct Hashmap* cvars)
{ {
hashmap_free(cvars); hashmap_free(cvars);
} }
struct Hashmap* config_vars_get(void) bool config_vars_load(struct Hashmap* cvars, const char* filename, int directory_type)
{
return cvars;
}
bool config_vars_load(const char* filename, int directory_type)
{ {
FILE* config_file = io_file_open(directory_type, filename, "rb"); FILE* config_file = io_file_open(directory_type, filename, "rb");
if(!config_file) if(!config_file)
@ -96,7 +86,7 @@ bool config_vars_load(const char* filename, int directory_type)
return config_loaded; return config_loaded;
} }
bool config_vars_save(const char* filename, int directory_type) bool config_vars_save(struct Hashmap* cvars, const char* filename, int directory_type)
{ {
bool success = false; bool success = false;
FILE* config_file = io_file_open(directory_type, filename, "w"); FILE* config_file = io_file_open(directory_type, filename, "w");

@ -5,11 +5,9 @@
struct Hashmap; struct Hashmap;
void config_vars_init(void); void config_vars_init(struct Hashmap* cvars);
void config_vars_cleanup(void); void config_vars_cleanup(struct Hashmap* cvars);
bool config_vars_load(const char* filename, int directory_type); bool config_vars_load(struct Hashmap* cvars, const char* filename, int directory_type);
bool config_vars_save(const char* filename, int directory_types); bool config_vars_save(struct Hashmap* cvars, const char* filename, int directory_types);
struct Hashmap* config_vars_get(void);
#endif #endif

@ -11,7 +11,9 @@
#include "../game/game.h" #include "../game/game.h"
struct Wndow; struct Wndow;
static struct Window* window;
static struct Window* window = NULL;
static struct Hashmap* cvars = NULL;
bool init(void); bool init(void);
void cleanup(void); void cleanup(void);
@ -27,7 +29,7 @@ int main(int argc, char** args)
bool done = false; bool done = false;
while(!done) while(!done)
{ {
bool game_init_status = game_init(window); bool game_init_status = game_init(window, cvars);
if(!game_init_status) if(!game_init_status)
{ {
log_error("main", "Game init failed"); log_error("main", "Game init failed");
@ -50,7 +52,8 @@ bool init(void)
return false; return false;
} }
config_vars_init(); cvars = hashmap_new();
config_vars_init(cvars);
if(!platform_init()) return false; if(!platform_init()) return false;
char* install_path = platform_install_directory_get(); char* install_path = platform_install_directory_get();
@ -59,10 +62,10 @@ bool init(void)
io_file_init(install_path, user_path); io_file_init(install_path, user_path);
free(install_path); free(install_path);
free(user_path); free(user_path);
if(!config_vars_load("config.symtres", DIRT_USER)) if(!config_vars_load(cvars, "config.symtres", DIRT_USER))
{ {
log_error("main:init", "Could not load config, reverting to defaults"); log_error("main:init", "Could not load config, reverting to defaults");
config_vars_save("config.symtres", DIRT_USER); config_vars_save(cvars, "config.symtres", DIRT_USER);
} }
if(!platform_init_video()) return false; if(!platform_init_video()) return false;
@ -73,7 +76,6 @@ bool init(void)
return false; return false;
} }
struct Hashmap* cvars = config_vars_get();
int width = hashmap_int_get(cvars, "render_width"); int width = hashmap_int_get(cvars, "render_width");
int height = hashmap_int_get(cvars, "render_height"); int height = hashmap_int_get(cvars, "render_height");
int msaa = hashmap_bool_get(cvars, "msaa_enabled"); int msaa = hashmap_bool_get(cvars, "msaa_enabled");
@ -102,7 +104,7 @@ void cleanup(void)
sound_cleanup(); sound_cleanup();
platform_unload_gl(); platform_unload_gl();
platform_cleanup(); platform_cleanup();
config_vars_cleanup(); config_vars_cleanup(cvars);
io_file_cleanup(); io_file_cleanup();
log_message("Program exiting!"); log_message("Program exiting!");
log_cleanup(); log_cleanup();

@ -71,7 +71,7 @@ Bugs:
- Fix lights type not being correctly saved/loaded from file - Fix lights type not being correctly saved/loaded from file
- Fix culling - Fix culling
- Fix bounding boxes not aligning in editor - Fix bounding boxes not aligning in editor
- Investigate memory usage increase when window is maximized - Investigate memory usage increase when window is minimized
Done: Done:
* Input * Input
@ -208,4 +208,5 @@ Done:
* Prevented ray casting when clicking on editor window and buttons * Prevented ray casting when clicking on editor window and buttons
* Implemented handling drawing other entity types that can be selected in the editor but are not static meshes * Implemented handling drawing other entity types that can be selected in the editor but are not static meshes
* Implemented proper topbar * Implemented proper topbar
* Changed config vars are used.

Loading…
Cancel
Save