Reworked how scene cleanup/init functions are stored and loaded, added functionality in editor to specify next scene, init and cleanup func names

dev
Shariq Shah 5 years ago
parent 238d1518a3
commit 2adcef4a1f
  1. 5
      assets/scenes/scene_1.symtres
  2. 8
      src/common/hashmap.c
  3. 2
      src/common/limits.h
  4. 40
      src/common/parser.c
  5. 2
      src/common/version.h
  6. 211
      src/game/editor.c
  7. 1
      src/game/editor.h
  8. 2
      src/game/event.h
  9. 44
      src/game/game.c
  10. 3
      src/game/game.h
  11. 32
      src/game/gui_game.c
  12. 4
      src/game/input.c
  13. 2
      src/game/player.c
  14. 57
      src/game/scene.c
  15. 11
      src/game/scene.h
  16. 9
      src/game/scene_funcs.c
  17. 3
      src/game/scene_funcs.h
  18. 2
      todo.txt

@ -7,10 +7,11 @@ Scene_Config
debug_draw_physics : false debug_draw_physics : false
fog_start_distance : 10.0000 fog_start_distance : 10.0000
fog_max_distance : 450.0000 fog_max_distance : 450.0000
cleanup_func : scene_1 cleanup_func : scene_1_cleanup
init_func : scene_1 init_func : scene_1_init
debug_draw_enabled : false debug_draw_enabled : false
debug_draw_mode : 0 debug_draw_mode : 0
next_scene : test
ambient_light : 0.100 0.100 0.100 ambient_light : 0.100 0.100 0.100
} }

@ -28,7 +28,7 @@ static struct Hashmap_Entry* hashmap_entry_new(struct Hashmap* hashmap, const ch
struct Hashmap_Entry* new_entry = NULL; struct Hashmap_Entry* new_entry = NULL;
for(int i = 0; i < array_len(hashmap->buckets[index]); i++) /* Look for duplicates and over-write if found */ for(int i = 0; i < array_len(hashmap->buckets[index]); i++) /* Look for duplicates and over-write if found */
{ {
if(strncmp(key, hashmap->buckets[index][i].key, HASH_MAX_KEY_LEN) == 0) if(strncmp(key, hashmap->buckets[index][i].key, MAX_HASH_KEY_LEN) == 0)
{ {
new_entry = &hashmap->buckets[index][i]; new_entry = &hashmap->buckets[index][i];
if(new_entry->key) free(new_entry->key); if(new_entry->key) free(new_entry->key);
@ -98,13 +98,13 @@ struct Variant* hashmap_value_get(const struct Hashmap* hashmap, const char* key
struct Variant* value = NULL; struct Variant* value = NULL;
unsigned int index = hashmap_generate_hash(key); unsigned int index = hashmap_generate_hash(key);
int key_len = (int)strlen(key); int key_len = (int)strlen(key);
int compare_len = key_len < HASH_MAX_KEY_LEN ? key_len : HASH_MAX_KEY_LEN; int compare_len = key_len < MAX_HASH_KEY_LEN ? key_len : MAX_HASH_KEY_LEN;
for(int i = 0; i < array_len(hashmap->buckets[index]); i++) for(int i = 0; i < array_len(hashmap->buckets[index]); i++)
{ {
// Check for the length of the key to avoid partial matches. We might be looking for // Check for the length of the key to avoid partial matches. We might be looking for
// "Diffuse" and we'll get matched to "Diffuse_Color" if we're relying on the length // "Diffuse" and we'll get matched to "Diffuse_Color" if we're relying on the length
// of "Diffuse" only // of "Diffuse" only
if(strnlen(hashmap->buckets[index][i].key, HASH_MAX_KEY_LEN) == compare_len && strncmp(key, hashmap->buckets[index][i].key, compare_len) == 0) if(strnlen(hashmap->buckets[index][i].key, MAX_HASH_KEY_LEN) == compare_len && strncmp(key, hashmap->buckets[index][i].key, compare_len) == 0)
{ {
value = &hashmap->buckets[index][i].value; value = &hashmap->buckets[index][i].value;
break; break;
@ -120,7 +120,7 @@ void hashmap_value_remove(struct Hashmap* hashmap, const char* key)
int index_to_remove = -1; int index_to_remove = -1;
for(int i = 0; i < array_len(hashmap->buckets[index]); i++) for(int i = 0; i < array_len(hashmap->buckets[index]); i++)
{ {
if(strncmp(key, hashmap->buckets[index][i].key, HASH_MAX_KEY_LEN) == 0) if(strncmp(key, hashmap->buckets[index][i].key, MAX_HASH_KEY_LEN) == 0)
{ {
index_to_remove = i; index_to_remove = i;
break; break;

@ -37,6 +37,6 @@
#define MAX_DEBUG_VARS_PER_FRAME_TEXTURES 8 #define MAX_DEBUG_VARS_PER_FRAME_TEXTURES 8
#define HASH_MAP_NUM_BUCKETS 10 #define HASH_MAP_NUM_BUCKETS 10
#define HASH_MAX_KEY_LEN 128 #define MAX_HASH_KEY_LEN 128
#endif #endif

@ -29,19 +29,19 @@ bool parser_load(FILE* file, const char* filename, Parser_Assign_Func assign_fun
/* Read line by line, ignore comments */ /* Read line by line, ignore comments */
char format_str[64]; char format_str[64];
char key_str[HASH_MAX_KEY_LEN]; char key_str[MAX_HASH_KEY_LEN];
char value_str[MAX_VALUE_LEN]; char value_str[MAX_VALUE_LEN];
char line_buffer[MAX_LINE_LEN]; char line_buffer[MAX_LINE_LEN];
memset(key_str, '\0', HASH_MAX_KEY_LEN); memset(key_str, '\0', MAX_HASH_KEY_LEN);
memset(line_buffer, '\0', MAX_LINE_LEN); memset(line_buffer, '\0', MAX_LINE_LEN);
memset(format_str, '\0', 64); memset(format_str, '\0', 64);
snprintf(format_str, 64, " %%%d[^: ] : %%%d[^\n]", HASH_MAX_KEY_LEN, MAX_VALUE_LEN); snprintf(format_str, 64, " %%%d[^: ] : %%%d[^\n]", MAX_HASH_KEY_LEN, MAX_VALUE_LEN);
while(fgets(line_buffer, MAX_LINE_LEN - 1, file)) while(fgets(line_buffer, MAX_LINE_LEN - 1, file))
{ {
current_line++; current_line++;
memset(key_str, '\0', HASH_MAX_KEY_LEN); memset(key_str, '\0', MAX_HASH_KEY_LEN);
memset(value_str, '\0', HASH_MAX_KEY_LEN); memset(value_str, '\0', MAX_HASH_KEY_LEN);
if(strlen(line_buffer) == 0 || isspace(line_buffer[0]) != 0) if(strlen(line_buffer) == 0 || isspace(line_buffer[0]) != 0)
{ {
@ -91,13 +91,13 @@ struct Parser* parser_load_objects(FILE* file, const char* filename)
int current_line = 0; int current_line = 0;
char line_buffer[MAX_LINE_LEN]; char line_buffer[MAX_LINE_LEN];
char type_str[HASH_MAX_KEY_LEN]; char type_str[MAX_HASH_KEY_LEN];
char obj_str[1024]; char obj_str[1024];
while(fgets(line_buffer, MAX_LINE_LEN - 1, file)) while(fgets(line_buffer, MAX_LINE_LEN - 1, file))
{ {
current_line++; current_line++;
memset(type_str, '\0', HASH_MAX_KEY_LEN); memset(type_str, '\0', MAX_HASH_KEY_LEN);
if(strlen(line_buffer) == 0 || isspace(line_buffer[0]) != 0) if(strlen(line_buffer) == 0 || isspace(line_buffer[0]) != 0)
{ {
@ -115,8 +115,8 @@ struct Parser* parser_load_objects(FILE* file, const char* filename)
} }
// Check if type string is valid // Check if type string is valid
size_t type_str_len = strnlen(type_str, HASH_MAX_KEY_LEN); size_t type_str_len = strnlen(type_str, MAX_HASH_KEY_LEN);
if(type_str_len < 3 || strncmp(type_str, "{", HASH_MAX_KEY_LEN) == 0 || strncmp(type_str, "}", HASH_MAX_KEY_LEN) == 0) if(type_str_len < 3 || strncmp(type_str, "{", MAX_HASH_KEY_LEN) == 0 || strncmp(type_str, "}", MAX_HASH_KEY_LEN) == 0)
{ {
log_warning("Invalid object type '%s' on line %d", type_str, current_line); log_warning("Invalid object type '%s' on line %d", type_str, current_line);
continue; continue;
@ -200,15 +200,15 @@ struct Parser* parser_load_objects(FILE* file, const char* filename)
object->data = hashmap_create(); object->data = hashmap_create();
char format_str[64]; char format_str[64];
char key_str[HASH_MAX_KEY_LEN]; char key_str[MAX_HASH_KEY_LEN];
char value_str[MAX_VALUE_LEN]; char value_str[MAX_VALUE_LEN];
memset(format_str, '\0', 64); memset(format_str, '\0', 64);
snprintf(format_str, 64, " %%%d[^: ] : %%%d[^\r\n]", HASH_MAX_KEY_LEN, MAX_VALUE_LEN); snprintf(format_str, 64, " %%%d[^: ] : %%%d[^\r\n]", MAX_HASH_KEY_LEN, MAX_VALUE_LEN);
char* line = strtok(obj_str, "\r\n"); char* line = strtok(obj_str, "\r\n");
do do
{ {
memset(key_str, '\0', HASH_MAX_KEY_LEN); memset(key_str, '\0', MAX_HASH_KEY_LEN);
memset(value_str, '\0', MAX_VALUE_LEN); memset(value_str, '\0', MAX_VALUE_LEN);
if(strlen(line) == 0) if(strlen(line) == 0)
@ -238,14 +238,14 @@ int parser_object_type_from_str(const char* str)
{ {
int object_type = PO_UNKNOWN; int object_type = PO_UNKNOWN;
if(strncmp(str, "Entity", HASH_MAX_KEY_LEN) == 0) object_type = PO_ENTITY; if(strncmp(str, "Entity", MAX_HASH_KEY_LEN) == 0) object_type = PO_ENTITY;
else if(strncmp(str, "Model", HASH_MAX_KEY_LEN) == 0) object_type = PO_MODEL; else if(strncmp(str, "Model", MAX_HASH_KEY_LEN) == 0) object_type = PO_MODEL;
else if(strncmp(str, "Material", HASH_MAX_KEY_LEN) == 0) object_type = PO_MATERIAL; else if(strncmp(str, "Material", MAX_HASH_KEY_LEN) == 0) object_type = PO_MATERIAL;
else if(strncmp(str, "Config", HASH_MAX_KEY_LEN) == 0) object_type = PO_CONFIG; else if(strncmp(str, "Config", MAX_HASH_KEY_LEN) == 0) object_type = PO_CONFIG;
else if(strncmp(str, "Key", HASH_MAX_KEY_LEN) == 0) object_type = PO_KEY; else if(strncmp(str, "Key", MAX_HASH_KEY_LEN) == 0) object_type = PO_KEY;
else if(strncmp(str, "Scene_Entity_Entry", HASH_MAX_KEY_LEN) == 0) object_type = PO_SCENE_ENTITY_ENTRY; else if(strncmp(str, "Scene_Entity_Entry", MAX_HASH_KEY_LEN) == 0) object_type = PO_SCENE_ENTITY_ENTRY;
else if(strncmp(str, "Scene_Config", HASH_MAX_KEY_LEN) == 0) object_type = PO_SCENE_CONFIG; else if(strncmp(str, "Scene_Config", MAX_HASH_KEY_LEN) == 0) object_type = PO_SCENE_CONFIG;
else if(strncmp(str, "Player", HASH_MAX_KEY_LEN) == 0) object_type = PO_PLAYER; else if(strncmp(str, "Player", MAX_HASH_KEY_LEN) == 0) object_type = PO_PLAYER;
return object_type; return object_type;
} }

@ -4,7 +4,7 @@
/* Auto generated version file. DO NOT MODIFY */ /* Auto generated version file. DO NOT MODIFY */
#define SYMMETRY_VERSION_MAJOR 0 #define SYMMETRY_VERSION_MAJOR 0
#define SYMMETRY_VERSION_MINOR 1 #define SYMMETRY_VERSION_MINOR 1
#define SYMMETRY_VERSION_REVISION 354 #define SYMMETRY_VERSION_REVISION 355
#define SYMMETRY_VERSION_BRANCH "dev" #define SYMMETRY_VERSION_BRANCH "dev"
#endif #endif

@ -106,6 +106,7 @@ static void editor_window_scene_hierarchy(struct nk_context* context, struct Edi
static void editor_window_property_inspector(struct nk_context* context, struct Editor* editor, struct Game_State* game_state); static void editor_window_property_inspector(struct nk_context* context, struct Editor* editor, struct Game_State* game_state);
static void editor_window_renderer_settings(struct nk_context* context, struct Editor* editor, struct Game_State* game_state); static void editor_window_renderer_settings(struct nk_context* context, struct Editor* editor, struct Game_State* game_state);
static void editor_window_settings_editor(struct nk_context* context, struct Editor* editor, struct Game_State* game_state); static void editor_window_settings_editor(struct nk_context* context, struct Editor* editor, struct Game_State* game_state);
static void editor_window_settings_scene(struct nk_context* context, struct Editor* editor, struct Game_State* game_state);
static void editor_axis_set(struct Editor* editor, int axis); static void editor_axis_set(struct Editor* editor, int axis);
static void editor_entity_select(struct Editor* editor, struct Entity* entity); static void editor_entity_select(struct Editor* editor, struct Entity* entity);
static void editor_tool_set(struct Editor* editor, int mode); static void editor_tool_set(struct Editor* editor, int mode);
@ -117,6 +118,7 @@ void editor_init(struct Editor* editor)
{ {
editor->window_settings_renderer = 0; editor->window_settings_renderer = 0;
editor->window_settings_editor = 0; editor->window_settings_editor = 0;
editor->window_settings_scene = 0;
editor->window_property_inspector = 0; editor->window_property_inspector = 0;
editor->window_scene_heirarchy = 0; editor->window_scene_heirarchy = 0;
editor->window_scene_dialog = 0; editor->window_scene_dialog = 0;
@ -425,8 +427,9 @@ void editor_update(struct Editor* editor, float dt)
if(nk_menu_begin_label(context, "Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, nk_vec2(150, 100))) if(nk_menu_begin_label(context, "Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, nk_vec2(150, 100)))
{ {
nk_layout_row_dynamic(context, row_height, 1); nk_layout_row_dynamic(context, row_height, 1);
if(nk_menu_item_label(context, "Editor Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_editor = !editor->window_settings_editor; if(nk_menu_item_label(context, "Editor Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_editor = !editor->window_settings_editor;
if(nk_menu_item_label(context, "Render Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_renderer = !editor->window_settings_renderer; if(nk_menu_item_label(context, "Render Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_renderer = !editor->window_settings_renderer;
if(nk_menu_item_label(context, "Scene Settings", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE)) editor->window_settings_scene = !editor->window_settings_scene;
nk_menu_end(context); nk_menu_end(context);
} }
@ -643,6 +646,7 @@ void editor_update(struct Editor* editor, float dt)
if(editor->window_property_inspector) editor_window_property_inspector(context, editor, game_state); if(editor->window_property_inspector) editor_window_property_inspector(context, editor, game_state);
if(editor->window_settings_renderer) editor_window_renderer_settings(context, editor, game_state); if(editor->window_settings_renderer) editor_window_renderer_settings(context, editor, game_state);
if(editor->window_settings_editor) editor_window_settings_editor(context, editor, game_state); if(editor->window_settings_editor) editor_window_settings_editor(context, editor, game_state);
if(editor->window_settings_scene) editor_window_settings_scene(context, editor, game_state);
if(editor->tool_mesh_draw_enabled && editor->selected_entity) if(editor->tool_mesh_draw_enabled && editor->selected_entity)
{ {
@ -2484,12 +2488,14 @@ void editor_window_property_inspector(struct nk_context* context, struct Editor*
void editor_window_renderer_settings(struct nk_context* context, struct Editor* editor, struct Game_State* game_state) void editor_window_renderer_settings(struct nk_context* context, struct Editor* editor, struct Game_State* game_state)
{ {
int win_width = 0, win_height = 0; int drawable_width = 0, drawable_height = 0;
window_get_drawable_size(game_state->window, &win_width, &win_height); window_get_drawable_size(game_state->window, &drawable_width, &drawable_height);
int half_width = win_width / 2, half_height = win_height / 2; int window_width = 300;
int window_height = 350;
int window_x = (drawable_width / 2) - (window_width / 2), window_y = (drawable_height / 2) - (window_height / 2);
const int row_height = 25; const int row_height = 25;
if(nk_begin_titled(context, "Renderer_Settings_Window", "Renderer Settings", nk_rect(half_width, half_height, 300, 350), window_flags)) if(nk_begin_titled(context, "Renderer_Settings_Window", "Renderer Settings", nk_rect(window_x, window_y, window_width, window_height), window_flags))
{ {
struct Render_Settings* render_settings = &game_state->renderer->settings; struct Render_Settings* render_settings = &game_state->renderer->settings;
if(nk_tree_push(context, NK_TREE_TAB, "Debug", NK_MAXIMIZED)) if(nk_tree_push(context, NK_TREE_TAB, "Debug", NK_MAXIMIZED))
@ -2508,7 +2514,142 @@ void editor_window_renderer_settings(struct nk_context* context, struct Editor*
editor_widget_color_combov4(context, &render_settings->debug_draw_color, 200, 400); editor_widget_color_combov4(context, &render_settings->debug_draw_color, 200, 400);
nk_tree_pop(context); nk_tree_pop(context);
} }
}
else
{
editor->window_settings_renderer = 0;
}
nk_end(context);
}
void editor_window_settings_editor(struct nk_context* context, struct Editor* editor, struct Game_State* game_state)
{
int drawable_width = 0, drawable_height = 0;
window_get_drawable_size(game_state->window, &drawable_width, &drawable_height);
int window_width = 300;
int window_height = 350;
int window_x = (drawable_width / 2) - (window_width / 2), window_y = (drawable_height / 2) - (window_height / 2);
const int row_height = 25;
if(nk_begin_titled(context, "Window_Settings_Editor", "Editor Settings", nk_rect(window_x, window_y, window_width, window_height), window_flags))
{
nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Grid Enabled", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
nk_checkbox_label(context, "", &editor->grid_enabled);
nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Grid Color", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
editor_widget_color_combov4(context, &editor->grid_color, 200, 400);
nk_layout_row_dynamic(context, row_height, 1);
nk_property_int(context, "Grid Lines", 10, &editor->grid_num_lines, 200, 1, 1);
nk_layout_row_dynamic(context, row_height, 1);
nk_property_float(context, "Grid Scale", 0.25f, &editor->grid_scale, 10.f, 1, 0.25f);
}
else
{
editor->window_settings_editor = 0;
}
nk_end(context);
}
void editor_window_settings_scene(struct nk_context* context, struct Editor* editor, struct Game_State* game_state)
{
struct Scene* scene = game_state->scene;
int drawable_width = 0, drawable_height = 0;
window_get_drawable_size(game_state->window, &drawable_width, &drawable_height);
int window_width = 300;
int window_height = 350;
int window_x = (drawable_width / 2) - (window_width / 2), window_y = (drawable_height / 2) - (window_height / 2);
const int row_height = 25;
if(nk_begin_titled(context, "Scene_Settings_Window", "Scene Settings", nk_recti(window_x, window_y, window_width, window_height), window_flags))
{
/* Next Scene */
nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Next Scene", LABEL_FLAGS_ALIGN_LEFT);
static char next_scene_filename_buffer[MAX_FILENAME_LEN];
static bool next_scene_filename_copied = false;
if(!next_scene_filename_copied)
{
strncpy(next_scene_filename_buffer, scene->next_level_filename, MAX_FILENAME_LEN);
next_scene_filename_copied = true;
}
nk_tooltip(context, "Enter the name of the scene file without extension");
int edit_flags = NK_EDIT_GOTO_END_ON_ACTIVATE | NK_EDIT_FIELD | NK_EDIT_SIG_ENTER;
int next_scene_edit_state = nk_edit_string_zero_terminated(context, edit_flags, next_scene_filename_buffer, MAX_FILENAME_LEN, NULL);
if(next_scene_edit_state & NK_EDIT_COMMITED)
{
scene_save(scene, scene->filename, DIRT_INSTALL);
strncpy(scene->next_level_filename, next_scene_filename_buffer, MAX_FILENAME_LEN);
nk_edit_unfocus(context);
}
else if((next_scene_edit_state & NK_EDIT_DEACTIVATED) && next_scene_filename_copied)
{
next_scene_filename_copied = false;
}
/* Init Func */
nk_label(context, "Init Func", LABEL_FLAGS_ALIGN_LEFT);
static char init_func_name_buffer[MAX_HASH_KEY_LEN];
static bool init_func_name_copied = false;
if(!init_func_name_copied)
{
strncpy(init_func_name_buffer, scene->init_func_name, MAX_HASH_KEY_LEN);
init_func_name_copied = true;
}
int scene_init_edit_state = nk_edit_string_zero_terminated(context, edit_flags, init_func_name_buffer, MAX_HASH_KEY_LEN, NULL);
if(scene_init_edit_state & NK_EDIT_COMMITED)
{
if(scene_init_func_assign(scene, init_func_name_buffer))
{
nk_edit_unfocus(context);
scene_save(scene, scene->filename, DIRT_INSTALL);
}
else
{
init_func_name_copied = false;
}
}
else if(scene_init_edit_state & NK_EDIT_DEACTIVATED && init_func_name_copied)
{
init_func_name_copied = false;
}
/* Cleanup Func */
nk_label(context, "Cleanup Func", LABEL_FLAGS_ALIGN_LEFT);
static char cleanup_func_name_buffer[MAX_HASH_KEY_LEN];
static bool cleanup_func_name_copied = false;
if(!cleanup_func_name_copied)
{
strncpy(cleanup_func_name_buffer, scene->cleanup_func_name, MAX_HASH_KEY_LEN);
cleanup_func_name_copied = true;
}
int cleanup_func_edit_state = nk_edit_string_zero_terminated(context, edit_flags, cleanup_func_name_buffer, MAX_HASH_KEY_LEN, NULL);
if(cleanup_func_edit_state & NK_EDIT_COMMITED)
{
if(scene_cleanup_func_assign(scene, cleanup_func_name_buffer))
{
scene_save(scene, scene->filename, DIRT_INSTALL);
nk_edit_unfocus(context);
}
else
{
cleanup_func_name_copied = false;
}
}
else if(cleanup_func_edit_state & NK_EDIT_DEACTIVATED && cleanup_func_name_copied)
{
cleanup_func_name_copied = false;
}
/* Render Settings */
struct Render_Settings* render_settings = &game_state->renderer->settings;
if(nk_tree_push(context, NK_TREE_TAB, "Fog", NK_MAXIMIZED)) if(nk_tree_push(context, NK_TREE_TAB, "Fog", NK_MAXIMIZED))
{ {
static const char* fog_modes[] = { "None", "Linear", "Exponential", "Exponential Squared" }; static const char* fog_modes[] = { "None", "Linear", "Exponential", "Exponential Squared" };
@ -2519,11 +2660,11 @@ void editor_window_renderer_settings(struct nk_context* context, struct Editor*
nk_layout_row_dynamic(context, row_height, 2); nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Fog Mode", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE); nk_label(context, "Fog Mode", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
render_settings->fog.mode = nk_combo(context, render_settings->fog.mode = nk_combo(context,
fog_modes, fog_modes,
4, 4,
render_settings->fog.mode, render_settings->fog.mode,
20, 20,
nk_vec2(180, 100)); nk_vec2(180, 100));
nk_layout_row_dynamic(context, row_height, 2); nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Density", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE); nk_label(context, "Density", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
@ -2541,19 +2682,19 @@ void editor_window_renderer_settings(struct nk_context* context, struct Editor*
nk_layout_row_dynamic(context, row_height, 1); nk_layout_row_dynamic(context, row_height, 1);
nk_property_float(context, nk_property_float(context,
"Start Distance", "Start Distance",
0.f, 0.f,
&render_settings->fog.start_dist, &render_settings->fog.start_dist,
render_settings->fog.max_dist, render_settings->fog.max_dist,
5.f, 10.f); 5.f, 10.f);
nk_layout_row_dynamic(context, row_height, 1); nk_layout_row_dynamic(context, row_height, 1);
nk_property_float(context, nk_property_float(context,
"Max Distance", "Max Distance",
render_settings->fog.start_dist, render_settings->fog.start_dist,
&render_settings->fog.max_dist, &render_settings->fog.max_dist,
10000.f, 10000.f,
5.f, 10.f); 5.f, 10.f);
nk_tree_pop(context); nk_tree_pop(context);
} }
@ -2565,36 +2706,6 @@ void editor_window_renderer_settings(struct nk_context* context, struct Editor*
nk_end(context); nk_end(context);
} }
void editor_window_settings_editor(struct nk_context* context, struct Editor* editor, struct Game_State* game_state)
{
int win_width = 0, win_height = 0;
window_get_drawable_size(game_state->window, &win_width, &win_height);
int half_width = win_width / 2, half_height = win_height / 2;
const int row_height = 25;
if(nk_begin_titled(context, "Window_Settings_Editor", "Editor Settings", nk_rect(half_width, half_height, 300, 350), window_flags))
{
nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Grid Enabled", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
nk_checkbox_label(context, "", &editor->grid_enabled);
nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Grid Color", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE);
editor_widget_color_combov4(context, &editor->grid_color, 200, 400);
nk_layout_row_dynamic(context, row_height, 1);
nk_property_int(context, "Grid Lines", 10, &editor->grid_num_lines, 200, 1, 1);
nk_layout_row_dynamic(context, row_height, 1);
nk_property_float(context, "Grid Scale", 0.25f, &editor->grid_scale, 10.f, 1, 0.25f);
}
else
{
editor->window_settings_editor = 0;
}
nk_end(context);
}
void editor_entity_dialog(struct Editor* editor, struct nk_context* context) void editor_entity_dialog(struct Editor* editor, struct nk_context* context)
{ {
struct Game_State* game_state = game_state_get(); struct Game_State* game_state = game_state_get();

@ -13,6 +13,7 @@ struct Editor
{ {
int window_settings_renderer; int window_settings_renderer;
int window_settings_editor; int window_settings_editor;
int window_settings_scene;
int window_scene_heirarchy; int window_scene_heirarchy;
int window_property_inspector; int window_property_inspector;
int window_scene_dialog; int window_scene_dialog;

@ -45,7 +45,7 @@ enum Event_Subscription_Type
struct Input_Map_Event struct Input_Map_Event
{ {
char name[HASH_MAX_KEY_LEN]; char name[MAX_HASH_KEY_LEN];
}; };
struct Key_Event struct Key_Event

@ -69,24 +69,23 @@ bool game_init(struct Window* window, struct Hashmap* cvars)
} }
else else
{ {
game_state->window = window; game_state->window = window;
game_state->cvars = cvars; game_state->cvars = cvars;
game_state->is_initialized = false; game_state->is_initialized = false;
game_state->quit = false; game_state->quit = false;
game_state->update_scene = true; game_state->update_scene = true;
game_state->fixed_delta_time = 1.f / 60.f; game_state->fixed_delta_time = 1.f / 60.f;
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));
game_state->scene = calloc(1, sizeof(*game_state->scene)); game_state->scene = calloc(1, sizeof(*game_state->scene));
game_state->console = calloc(1, sizeof(*game_state->console)); game_state->console = calloc(1, sizeof(*game_state->console));
game_state->editor = calloc(1, sizeof(*game_state->editor)); game_state->editor = calloc(1, sizeof(*game_state->editor));
game_state->gui_editor = calloc(1, sizeof(*game_state->gui_editor)); game_state->gui_editor = calloc(1, sizeof(*game_state->gui_editor));
game_state->gui_game = calloc(1, sizeof(*game_state->gui_game)); game_state->gui_game = calloc(1, sizeof(*game_state->gui_game));
game_state->event_manager = calloc(1, sizeof(*game_state->event_manager)); game_state->event_manager = calloc(1, sizeof(*game_state->event_manager));
game_state->sound = calloc(1, sizeof(*game_state->sound)); game_state->sound = calloc(1, sizeof(*game_state->sound));
game_state->debug_vars = calloc(1, sizeof(*game_state->debug_vars)); game_state->debug_vars = calloc(1, sizeof(*game_state->debug_vars));
game_state->scene_init_func_table = hashmap_create(); game_state->scene_func_table = hashmap_create();
game_state->scene_cleanup_func_table = hashmap_create();
log_message_callback_set(game_on_log_message); log_message_callback_set(game_on_log_message);
log_warning_callback_set(game_on_log_warning); log_warning_callback_set(game_on_log_warning);
@ -102,8 +101,9 @@ bool game_init(struct Window* window, struct Hashmap* cvars)
log_message("Loaded GL extentions"); log_message("Loaded GL extentions");
} }
hashmap_ptr_set(game_state->scene_init_func_table, "scene_1", &scene_1_init); hashmap_ptr_set(game_state->scene_func_table, "scene_func_stub", &scene_func_stub);
hashmap_ptr_set(game_state->scene_cleanup_func_table, "scene_1", &scene_1_cleanup); hashmap_ptr_set(game_state->scene_func_table, "scene_1_init", &scene_1_init);
hashmap_ptr_set(game_state->scene_func_table, "scene_1_cleanup", &scene_1_cleanup);
srand(time(NULL)); srand(time(NULL));
@ -2000,8 +2000,7 @@ void game_cleanup(void)
free(game_state->gui_game); free(game_state->gui_game);
free(game_state->sound); free(game_state->sound);
free(game_state->debug_vars); free(game_state->debug_vars);
hashmap_free(game_state->scene_init_func_table); hashmap_free(game_state->scene_func_table);
hashmap_free(game_state->scene_cleanup_func_table);
} }
free(game_state); free(game_state);
game_state = NULL; game_state = NULL;
@ -2013,7 +2012,6 @@ struct Game_State* game_state_get(void)
return game_state; return game_state;
} }
void game_on_log_message(const char* message, va_list args) void game_on_log_message(const char* message, va_list args)
{ {
console_on_log_message(game_state->console, message, args); console_on_log_message(game_state->console, message, args);

@ -41,8 +41,7 @@ struct Game_State
struct Hashmap* cvars; struct Hashmap* cvars;
struct Sound* sound; struct Sound* sound;
struct Debug_Vars* debug_vars; struct Debug_Vars* debug_vars;
struct Hashmap* scene_init_func_table; struct Hashmap* scene_func_table;
struct Hashmap* scene_cleanup_func_table;
}; };

@ -11,8 +11,6 @@
#define SKIN_TEXTURE_WIDTH 256 #define SKIN_TEXTURE_WIDTH 256
#define SKIN_TEXTURE_HEIGHT 256 #define SKIN_TEXTURE_HEIGHT 256
static float key_opacity_full = 1.f;
static float key_opacity_reduced = 0.3f;
static void gui_game_pause_menu(struct nk_context* context); static void gui_game_pause_menu(struct nk_context* context);
static void gui_game_next_level_dialog(struct nk_context* context); static void gui_game_next_level_dialog(struct nk_context* context);
@ -71,11 +69,13 @@ void gui_game_update(struct Game_Gui* game_gui, float dt)
if(game_state->game_mode == GAME_MODE_GAME) if(game_state->game_mode == GAME_MODE_GAME)
{ {
// HUD // HUD
int hud_offset_x = 50; const float key_opacity_full = 1.f;
int hud_offset_y = 50; const float key_opacity_reduced = 0.3f;
int hp_gui_width = 130; int hud_offset_x = 50;
int hp_gui_height = 48; int hud_offset_y = 50;
struct Player* player = &game_state->scene->player; int hp_gui_width = 130;
int hp_gui_height = 48;
struct Player* player = &game_state->scene->player;
if(nk_begin(context, "HP Gui", nk_recti(hud_offset_x, hud_offset_y, hp_gui_width, hp_gui_height), NK_WINDOW_BACKGROUND | NK_WINDOW_NO_SCROLLBAR)) if(nk_begin(context, "HP Gui", nk_recti(hud_offset_x, hud_offset_y, hp_gui_width, hp_gui_height), NK_WINDOW_BACKGROUND | NK_WINDOW_NO_SCROLLBAR))
{ {
nk_layout_row_dynamic(context, 40, 2); nk_layout_row_dynamic(context, 40, 2);
@ -194,15 +194,15 @@ void gui_game_show_door_locked_dialog(struct Game_Gui* game_gui, struct Door* do
void gui_game_pause_menu(struct nk_context* context) void gui_game_pause_menu(struct nk_context* context)
{ {
struct Game_State* game_state = game_state_get(); struct Game_State* game_state = game_state_get();
int row_height = 30; int row_height = 30;
int popup_x = 0; int popup_x = 0;
int popup_y = 0; int popup_y = 0;
int popup_width = 300; int popup_width = 300;
int popup_height = 200; int popup_height = 200;
int display_width = 0; int display_width = 0;
int display_height = 0; int display_height = 0;
int popup_flags = NK_WINDOW_TITLE | NK_WINDOW_BORDER; int popup_flags = NK_WINDOW_TITLE | NK_WINDOW_BORDER;
window_get_drawable_size(game_state_get()->window, &display_width, &display_height); window_get_drawable_size(game_state_get()->window, &display_width, &display_height);
popup_x = (display_width / 2) - (popup_width / 2); popup_x = (display_width / 2) - (popup_width / 2);
popup_y = (display_height / 2) - (popup_height / 2); popup_y = (display_height / 2) - (popup_height / 2);

@ -295,7 +295,7 @@ void input_on_key(const struct Event* event)
key_binding->state = event->type == EVT_KEY_PRESSED ? KS_PRESSED : KS_RELEASED; key_binding->state = event->type == EVT_KEY_PRESSED ? KS_PRESSED : KS_RELEASED;
struct Event* input_map_event = event_manager_create_new_event(event_manager); struct Event* input_map_event = event_manager_create_new_event(event_manager);
input_map_event->type = event->type == EVT_KEY_PRESSED ? EVT_INPUT_MAP_PRESSED : EVT_INPUT_MAP_RELEASED; input_map_event->type = event->type == EVT_KEY_PRESSED ? EVT_INPUT_MAP_PRESSED : EVT_INPUT_MAP_RELEASED;
strncpy(&input_map_event->input_map.name, map_key, HASH_MAX_KEY_LEN); strncpy(&input_map_event->input_map.name, map_key, MAX_HASH_KEY_LEN);
event_manager_send_event(event_manager, input_map_event); event_manager_send_event(event_manager, input_map_event);
break; break;
} }
@ -306,7 +306,7 @@ void input_on_key(const struct Event* event)
key_binding->state = event->type == EVT_KEY_PRESSED ? KS_PRESSED : KS_RELEASED; key_binding->state = event->type == EVT_KEY_PRESSED ? KS_PRESSED : KS_RELEASED;
struct Event* input_map_event = event_manager_create_new_event(event_manager); struct Event* input_map_event = event_manager_create_new_event(event_manager);
input_map_event->type = event->type == EVT_KEY_PRESSED ? EVT_INPUT_MAP_PRESSED : EVT_INPUT_MAP_RELEASED; input_map_event->type = event->type == EVT_KEY_PRESSED ? EVT_INPUT_MAP_PRESSED : EVT_INPUT_MAP_RELEASED;
strncpy(&input_map_event->input_map.name, map_key, HASH_MAX_KEY_LEN); strncpy(&input_map_event->input_map.name, map_key, MAX_HASH_KEY_LEN);
event_manager_send_event(event_manager, input_map_event); event_manager_send_event(event_manager, input_map_event);
break; break;
} }

@ -309,7 +309,7 @@ void player_update_physics(struct Player* player, struct Scene* scene, float fix
void player_on_input_map_released(const struct Event* event) void player_on_input_map_released(const struct Event* event)
{ {
struct Game_State* game_state = game_state_get(); struct Game_State* game_state = game_state_get();
if(strncmp("Jump", event->input_map.name, HASH_MAX_KEY_LEN) == 0) if(strncmp("Jump", event->input_map.name, MAX_HASH_KEY_LEN) == 0)
{ {
struct Player* player = &game_state->scene->player; struct Player* player = &game_state->scene->player;
player->can_jump = true; player->can_jump = true;

@ -94,8 +94,50 @@ void scene_init(struct Scene* scene)
if(game_state->game_mode == GAME_MODE_PAUSE) if(game_state->game_mode == GAME_MODE_PAUSE)
game_state->game_mode = GAME_MODE_GAME; game_state->game_mode = GAME_MODE_GAME;
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;
scene->init = &scene_init_stub; scene->init = NULL;
scene->cleanup = &scene_cleanup_stub; scene->cleanup = NULL;
scene_init_func_assign(scene, "scene_func_stub");
scene_cleanup_func_assign(scene, "scene_func_stub");
}
bool scene_init_func_assign(struct Scene* scene, const char* init_func_name)
{
struct Game_State* game_state = game_state_get();
if(!hashmap_value_exists(game_state->scene_func_table, init_func_name))
{
log_error("scene:init_func_assign", "Could not find func called '%s' in scene function table", init_func_name);
if(!scene->init)
{
scene->init = (Scene_Func)hashmap_ptr_get(game_state->scene_func_table, "scene_func_stub");
strncpy(scene->init_func_name, "scene_func_stub", MAX_HASH_KEY_LEN);
log_warning("Assigned Stub Func as init for scene '%s'", scene->filename);
}
return false;
}
scene->init = (Scene_Func)hashmap_ptr_get(game_state->scene_func_table, init_func_name);
strncpy(scene->init_func_name, init_func_name, MAX_HASH_KEY_LEN);
return true;
}
bool scene_cleanup_func_assign(struct Scene* scene, const char* cleanup_func_name)
{
struct Game_State* game_state = game_state_get();
if(!hashmap_value_exists(game_state->scene_func_table, cleanup_func_name))
{
log_error("scene:cleanup_func_assign", "Could not find func called '%s' in scene function table", cleanup_func_name);
if(!scene->cleanup)
{
scene->cleanup = (Scene_Func)hashmap_ptr_get(game_state->scene_func_table, "scene_func_stub");
strncpy(scene->cleanup_func_name, "scene_func_stub", MAX_HASH_KEY_LEN);
log_warning("Assigned Stub Func as cleanup for scene '%s'", scene->filename);
}
return false;
}
scene->cleanup = (Scene_Func)hashmap_ptr_get(game_state->scene_func_table, cleanup_func_name);
strncpy(scene->cleanup_func_name, cleanup_func_name, MAX_HASH_KEY_LEN);
return true;
} }
bool scene_load(struct Scene* scene, const char* filename, int directory_type) bool scene_load(struct Scene* scene, const char* filename, int directory_type)
@ -105,7 +147,7 @@ bool scene_load(struct Scene* scene, const char* filename, int directory_type)
FILE* scene_file = io_file_open(directory_type, prefixed_filename, "rb"); FILE* scene_file = io_file_open(directory_type, prefixed_filename, "rb");
if(!scene_file) if(!scene_file)
{ {
log_error("scene:load", "Failed to open scene file %s for reading"); log_error("scene:load", "Failed to open scene file %s for reading", filename);
return false; return false;
} }
@ -155,8 +197,8 @@ bool scene_load(struct Scene* scene, const char* filename, int directory_type)
if(hashmap_value_exists(scene_data, "debug_draw_mode")) render_settings->debug_draw_mode = hashmap_int_get(scene_data, "debug_draw_mode"); if(hashmap_value_exists(scene_data, "debug_draw_mode")) render_settings->debug_draw_mode = hashmap_int_get(scene_data, "debug_draw_mode");
if(hashmap_value_exists(scene_data, "debug_draw_physics")) render_settings->debug_draw_physics = hashmap_bool_get(scene_data, "debug_draw_physics"); if(hashmap_value_exists(scene_data, "debug_draw_physics")) render_settings->debug_draw_physics = hashmap_bool_get(scene_data, "debug_draw_physics");
scene->init = hashmap_value_exists(scene_data, "init_func") ? hashmap_ptr_get(game_state->scene_init_func_table, hashmap_str_get(scene_data, "init_func")) : &scene_init_stub; scene_init_func_assign(scene, hashmap_value_exists(scene_data, "init_func") ? hashmap_str_get(scene_data, "init_func") : "scene_func_stub");
scene->cleanup = hashmap_value_exists(scene_data, "cleanup_func") ? hashmap_ptr_get(game_state->scene_cleanup_func_table, hashmap_str_get(scene_data, "cleanup_func")) : &scene_init_stub; scene_cleanup_func_assign(scene, hashmap_value_exists(scene_data, "cleanup_func") ? hashmap_str_get(scene_data, "cleanup_func") : "scene_func_stub");
if(hashmap_value_exists(scene_data, "next_scene")) if(hashmap_value_exists(scene_data, "next_scene"))
strncpy(scene->next_level_filename, hashmap_value_exists(scene_data, "next_scene") ? hashmap_str_get(scene_data, "next_scene") : "NONE", MAX_FILENAME_LEN); strncpy(scene->next_level_filename, hashmap_value_exists(scene_data, "next_scene") ? hashmap_str_get(scene_data, "next_scene") : "NONE", MAX_FILENAME_LEN);
@ -271,8 +313,8 @@ bool scene_save(struct Scene* scene, const char* filename, int directory_type)
hashmap_bool_set(scene_data, "debug_draw_enabled", render_settings->debug_draw_enabled); hashmap_bool_set(scene_data, "debug_draw_enabled", render_settings->debug_draw_enabled);
hashmap_int_set(scene_data, "debug_draw_mode", render_settings->debug_draw_mode); hashmap_int_set(scene_data, "debug_draw_mode", render_settings->debug_draw_mode);
hashmap_bool_set(scene_data, "debug_draw_physics", render_settings->debug_draw_physics); hashmap_bool_set(scene_data, "debug_draw_physics", render_settings->debug_draw_physics);
if(scene->init) hashmap_str_set(scene_data, "init_func", filename); if(scene->init) hashmap_str_set(scene_data, "init_func", scene->init_func_name);
if(scene->cleanup) hashmap_str_set(scene_data, "cleanup_func", filename); if(scene->cleanup) hashmap_str_set(scene_data, "cleanup_func", scene->cleanup_func_name);
hashmap_str_set(scene_data, "next_scene", scene->next_level_filename != '\0' ? scene->next_level_filename : "NONE"); hashmap_str_set(scene_data, "next_scene", scene->next_level_filename != '\0' ? scene->next_level_filename : "NONE");
// Player // Player
@ -282,6 +324,7 @@ bool scene_save(struct Scene* scene, const char* filename, int directory_type)
hashmap_int_set(player_object->data, "player_health", scene->player.health); hashmap_int_set(player_object->data, "player_health", scene->player.health);
hashmap_int_set(player_object->data, "player_key_mask", scene->player.key_mask); hashmap_int_set(player_object->data, "player_key_mask", scene->player.key_mask);
// Entity Lists
scene_write_entity_list(scene, ET_DEFAULT, parser); scene_write_entity_list(scene, ET_DEFAULT, parser);
scene_write_entity_list(scene, ET_LIGHT, parser); scene_write_entity_list(scene, ET_LIGHT, parser);
scene_write_entity_list(scene, ET_STATIC_MESH, parser); scene_write_entity_list(scene, ET_STATIC_MESH, parser);

@ -8,8 +8,7 @@
struct Ray; struct Ray;
struct Raycast_Result; struct Raycast_Result;
typedef void (*Scene_Init_Func)(struct Scene* scene); typedef void (*Scene_Func)(struct Scene* scene);
typedef void (*Scene_Cleanup_Func)(struct Scene* scene);
struct Scene struct Scene
{ {
@ -27,8 +26,10 @@ struct Scene
struct Door doors[MAX_SCENE_DOORS]; struct Door doors[MAX_SCENE_DOORS];
char entity_archetypes[MAX_SCENE_ENTITY_ARCHETYPES][MAX_FILENAME_LEN]; char entity_archetypes[MAX_SCENE_ENTITY_ARCHETYPES][MAX_FILENAME_LEN];
int active_camera_index; int active_camera_index;
Scene_Init_Func init; char init_func_name[MAX_HASH_KEY_LEN];
Scene_Cleanup_Func cleanup; char cleanup_func_name[MAX_HASH_KEY_LEN];
Scene_Func init;
Scene_Func cleanup;
}; };
void scene_init(struct Scene* scene); void scene_init(struct Scene* scene);
@ -38,6 +39,8 @@ void scene_destroy(struct Scene* scene);
void scene_update(struct Scene* scene, float dt); void scene_update(struct Scene* scene, float dt);
void scene_update_physics(struct Scene* scene, float fixed_dt); void scene_update_physics(struct Scene* scene, float fixed_dt);
void scene_post_update(struct Scene* scene); void scene_post_update(struct Scene* scene);
bool scene_cleanup_func_assign(struct Scene* scene, const char* cleanup_func_name);
bool scene_init_func_assign(struct Scene* scene, const char* init_func_name);
struct Entity* scene_entity_duplicate(struct Scene* scene, struct Entity* entity); struct Entity* scene_entity_duplicate(struct Scene* scene, struct Entity* entity);
struct Entity* scene_entity_create(struct Scene* scene, const char* name, struct Entity* parent); struct Entity* scene_entity_create(struct Scene* scene, const char* name, struct Entity* parent);

@ -6,14 +6,9 @@
static void scene_on_end_trigger(const struct Event* event, void* sender); static void scene_on_end_trigger(const struct Event* event, void* sender);
void scene_init_stub(struct Scene* scene) void scene_func_stub(struct Scene* scene)
{ {
log_warning("Scene Init Stub Called"); log_warning("Scene Func Stub Called");
}
void scene_cleanup_stub(struct Scene* scene)
{
log_warning("Scene Cleanup Stub Called");
} }
void scene_1_init(struct Scene* scene) void scene_1_init(struct Scene* scene)

@ -3,8 +3,7 @@
#include "scene.h" #include "scene.h"
void scene_init_stub(struct Scene* scene); void scene_func_stub(struct Scene* scene);
void scene_cleanup_stub(struct Scene* scene);
void scene_1_init(struct Scene* scene); void scene_1_init(struct Scene* scene);
void scene_1_cleanup(struct Scene* scene); void scene_1_cleanup(struct Scene* scene);

@ -424,4 +424,4 @@ Done:
* Audio cues when player does not have the right key combination to open a particular door * Audio cues when player does not have the right key combination to open a particular door
* Added saving scene init/cleanup funcs if there are any assigned when saving scene * Added saving scene init/cleanup funcs if there are any assigned when saving scene
* Win/fail States * Win/fail States
x In-Game Gui * In-Game Gui
Loading…
Cancel
Save