Began work on in-game gui with separate context and fixed crash when sound source instance is destroyed without checking if its valid

dev
Shariq Shah 6 years ago
parent 3902328628
commit c1454e2a8d
  1. 2
      src/common/version.h
  2. 2
      src/game/debug_vars.c
  3. 16
      src/game/editor.c
  4. 53
      src/game/game.c
  5. 6
      src/game/game.h
  6. 35
      src/game/gui.c
  7. 50
      src/game/gui_game.c
  8. 10
      src/game/gui_game.h
  9. 2
      src/game/input.c
  10. 3
      src/game/renderer.c
  11. 3
      src/system/sound.c

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

@ -57,7 +57,7 @@ void debug_vars_post_update(struct Debug_Vars* debug_vars)
int display_width = 0;
int display_height = 0;
struct Game_State* game_state = game_state_get();
struct Gui* gui = game_state->gui;
struct Gui* gui = game_state->gui_editor;
struct nk_context* context = &gui->context;
window_get_drawable_size(game_state->window, &display_width, &display_height);

@ -363,7 +363,7 @@ void editor_update(struct Editor* editor, float dt)
editor_camera_update(editor, dt);
struct Game_State* game_state = game_state_get();
struct nk_context* context = &game_state->gui->context;
struct nk_context* context = &game_state->gui_editor->context;
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;
@ -842,7 +842,7 @@ void editor_on_mousebutton_release(const struct Event* event)
{
struct Game_State* game_state = game_state_get();
struct Editor* editor = game_state->editor;
struct Gui* gui = game_state->gui;
struct Gui* gui = game_state->gui_editor;
if(game_state->game_mode != GAME_MODE_EDITOR || game_state->console->visible)
return;
@ -942,7 +942,7 @@ void editor_on_mousebutton_press(const struct Event* event)
{
struct Game_State* game_state = game_state_get();
struct Editor* editor = game_state->editor;
struct Gui* gui = game_state->gui;
struct Gui* gui = game_state->gui_editor;
if(game_state->game_mode != GAME_MODE_EDITOR || nk_window_is_any_hovered(&gui->context) || game_state->console->visible)
return;
@ -972,7 +972,7 @@ void editor_on_mousemotion(const struct Event* event)
{
struct Game_State* game_state = game_state_get();
struct Editor* editor = game_state->editor;
struct Gui* gui = game_state->gui;
struct Gui* gui = game_state->gui_editor;
if(game_state->game_mode != GAME_MODE_EDITOR || nk_window_is_any_hovered(&gui->context) || game_state->console->visible)
return;
@ -1164,7 +1164,7 @@ void editor_on_key_release(const struct Event* event)
{
struct Game_State* game_state = game_state_get();
struct Editor* editor = game_state->editor;
struct Gui* gui = game_state->gui;
struct Gui* gui = game_state->gui_editor;
if(game_state->game_mode != GAME_MODE_EDITOR || game_state->console->visible)
return;
@ -1351,11 +1351,11 @@ void editor_on_key_press(const struct Event* event)
{
struct Game_State* game_state = game_state_get();
struct Editor* editor = game_state->editor;
struct Gui* gui = game_state->gui;
struct Gui* gui = game_state->gui_editor;
if(game_state->game_mode != GAME_MODE_EDITOR || nk_window_is_any_hovered(&gui->context) || game_state->console->visible)
return;
if(!nk_window_is_any_hovered(&game_state_get()->gui->context))
if(!nk_window_is_any_hovered(&game_state_get()->gui_editor->context))
{
if(event->key.key == KEY_ALT && editor->current_tool == EDITOR_TOOL_TRANSLATE && editor->current_axis != EDITOR_AXIS_Y) editor_axis_set(editor, EDITOR_AXIS_Y);
}
@ -1447,7 +1447,7 @@ void editor_axis_set(struct Editor* editor, int axis)
void editor_camera_update(struct Editor* editor, float dt)
{
struct Game_State* game_state = game_state_get();
struct Gui* gui = game_state->gui;
struct Gui* gui = game_state->gui_editor;
if(game_state->console->visible || nk_item_is_any_active(&gui->context))
return;

@ -36,6 +36,7 @@
#include "event.h"
#include "../common/limits.h"
#include "scene_funcs.h"
#include "gui_game.h"
#define UNUSED(a) (void)a
#define MIN_NUM(a,b) ((a) < (b) ? (a) : (b))
@ -74,7 +75,8 @@ bool game_init(struct Window* window, struct Hashmap* cvars)
game_state->scene = calloc(1, sizeof(*game_state->scene));
game_state->console = calloc(1, sizeof(*game_state->console));
game_state->editor = calloc(1, sizeof(*game_state->editor));
game_state->gui = calloc(1, sizeof(*game_state->gui));
game_state->gui_editor = calloc(1, sizeof(*game_state->gui_editor));
game_state->gui_game = calloc(1, sizeof(*game_state->gui_game));
game_state->event_manager = calloc(1, sizeof(*game_state->event_manager));
game_state->sound = calloc(1, sizeof(*game_state->sound));
game_state->debug_vars = calloc(1, sizeof(*game_state->debug_vars));
@ -103,7 +105,8 @@ bool game_init(struct Window* window, struct Hashmap* cvars)
shader_init();
texture_init();
framebuffer_init();
gui_init(game_state->gui);
gui_init(game_state->gui_editor);
gui_init(game_state->gui_game);
console_init(game_state->console);
geom_init();
sound_init(game_state->sound);
@ -112,12 +115,12 @@ bool game_init(struct Window* window, struct Hashmap* cvars)
renderer_init(game_state->renderer);
scene_init(game_state->scene);
editor_init(game_state->editor);
}
/* Debug scene setup */
//game_scene_setup();
scene_load(game_state->scene, "scene_1", DIRT_INSTALL);
game_state->is_initialized = true;
game_state->is_initialized = scene_load(game_state->scene, "scene_1", DIRT_INSTALL) ? true : false;
return game_state->is_initialized;
}
@ -524,9 +527,10 @@ bool game_run(void)
if(frame_time > MAX_FRAME_TIME) frame_time = (1.f / 60.f); /* To deal with resuming from breakpoint we artificially set delta time */
accumulator += frame_time;
gui_input_begin(game_state->gui);
struct Gui* gui = game_state->game_mode == GAME_MODE_EDITOR ? game_state->gui_editor : game_state->gui_game;
gui_input_begin(gui);
event_manager_poll_events(game_state->event_manager, &should_window_close);
gui_input_end(game_state->gui);
gui_input_end(gui);
struct Game_State* game_state = game_state_get();
while(accumulator >= game_state->fixed_delta_time)
@ -580,19 +584,34 @@ void game_update(float dt, bool* window_should_close)
platform_mouse_position_set(game_state_get()->window, width / 2, height / 2);
}
}
if(input_map_state_get("Pause", KS_RELEASED))
{
if(game_state->game_mode == GAME_MODE_PAUSE)
{
game_state->game_mode = GAME_MODE_GAME;
}
else if(game_state->game_mode == GAME_MODE_GAME)
{
game_state->game_mode = GAME_MODE_PAUSE;
input_mouse_mode_set(MM_NORMAL);
int width = 0, height = 0;
window_get_drawable_size(game_state_get()->window, &width, &height);
platform_mouse_position_set(game_state_get()->window, width / 2, height / 2);
}
}
//game_debug(dt);
//game_debug_gui(dt);
console_update(game_state->console, game_state->gui, dt);
console_update(game_state->console, game_state->gui_editor, dt);
scene_update(game_state->scene, dt);
if(game_state->game_mode == GAME_MODE_GAME)
{
//physics_step(dt);
}
else if(game_state->game_mode == GAME_MODE_EDITOR)
if(game_state->game_mode == GAME_MODE_EDITOR)
{
editor_update(game_state->editor, dt);
}
else
{
gui_game_update(game_state->gui_game, dt);
}
}
void game_post_update(float dt)
@ -606,7 +625,7 @@ void game_post_update(float dt)
void game_debug_gui(float dt)
{
struct Gui* gui_state = game_state->gui;
struct Gui* gui_state = game_state->gui_editor;
struct nk_context* ctx = &gui_state->context;
/* window flags */
@ -1937,7 +1956,8 @@ void game_cleanup(void)
scene_destroy(game_state->scene);
input_cleanup();
renderer_cleanup(game_state->renderer);
gui_cleanup(game_state->gui);
gui_cleanup(game_state->gui_editor);
gui_cleanup(game_state->gui_game);
console_destroy(game_state->console);
geom_cleanup();
framebuffer_cleanup();
@ -1953,7 +1973,8 @@ void game_cleanup(void)
free(game_state->scene);
free(game_state->renderer);
free(game_state->event_manager);
free(game_state->gui);
free(game_state->gui_editor);
free(game_state->gui_game);
free(game_state->sound);
free(game_state->debug_vars);
hashmap_free(game_state->scene_init_func_table);

@ -18,7 +18,8 @@ struct Sound;
enum Game_Mode
{
GAME_MODE_GAME = 0,
GAME_MODE_EDITOR
GAME_MODE_EDITOR,
GAME_MODE_PAUSE
};
struct Game_State
@ -30,7 +31,8 @@ struct Game_State
struct Renderer* renderer;
struct Scene* scene;
struct Console* console;
struct Gui* gui;
struct Gui* gui_editor;
struct Gui* gui_game;
struct Event_Manager* event_manager;
struct Editor* editor;
struct Hashmap* cvars;

@ -268,9 +268,10 @@ void gui_on_key(const struct Event* event)
{
assert(event->type == EVT_KEY_PRESSED || event->type == EVT_KEY_RELEASED);
int key = event->key.key;
bool mod_ctrl = event->key.mod_ctrl;
struct nk_context* ctx = &game_state_get()->gui->context;
struct Game_State* game_state = game_state_get();
int key = event->key.key;
bool mod_ctrl = event->key.mod_ctrl;
struct nk_context* ctx = game_state->game_mode == GAME_MODE_EDITOR ? &game_state->gui_editor->context : &game_state->gui_game->context;
int down = event->type == EVT_KEY_PRESSED ? 1 : 0;
@ -339,11 +340,12 @@ void gui_on_mousebutton(const struct Event* event)
{
assert(event->type == EVT_MOUSEBUTTON_PRESSED || event->type == EVT_MOUSEBUTTON_RELEASED);
int button = event->mousebutton.button;
int x = event->mousebutton.x;
int y = event->mousebutton.y;
int down = event->type == EVT_MOUSEBUTTON_PRESSED ? 1 : 0;
struct nk_context* ctx = &game_state_get()->gui->context;
struct Game_State* game_state = game_state_get();
int button = event->mousebutton.button;
int x = event->mousebutton.x;
int y = event->mousebutton.y;
int down = event->type == EVT_MOUSEBUTTON_PRESSED ? 1 : 0;
struct nk_context* ctx = game_state->game_mode == GAME_MODE_EDITOR ? &game_state->gui_editor->context : &game_state->gui_game;
if(button == MSB_LEFT) nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
if(button == MSB_MIDDLE) nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
@ -352,11 +354,12 @@ void gui_on_mousebutton(const struct Event* event)
void gui_on_mousemotion(const struct Event* event)
{
int x = event->mousemotion.x;
int y = event->mousemotion.y;
int xrel = event->mousemotion.xrel;
int yrel = event->mousemotion.yrel;
struct nk_context* ctx = &game_state_get()->gui->context;
struct Game_State* game_state = game_state_get();
int x = event->mousemotion.x;
int y = event->mousemotion.y;
int xrel = event->mousemotion.xrel;
int yrel = event->mousemotion.yrel;
struct nk_context* ctx = game_state->game_mode == GAME_MODE_EDITOR ? &game_state->gui_editor->context : &game_state->gui_game;
if(ctx->input.mouse.grabbed)
{
@ -371,7 +374,8 @@ void gui_on_mousemotion(const struct Event* event)
void gui_on_textinput(const struct Event* event)
{
struct nk_context *ctx = &game_state_get()->gui->context;
struct Game_State* game_state = game_state_get();
struct nk_context* ctx = game_state->game_mode == GAME_MODE_EDITOR ? &game_state->gui_editor->context : &game_state->gui_game;
nk_glyph glyph;
memcpy(glyph, event->text_input.text, NK_UTF_SIZE);
nk_input_glyph(ctx, glyph);
@ -381,7 +385,8 @@ void gui_on_mousewheel(const struct Event* event)
{
int x = event->mousewheel.x;
int y = event->mousewheel.y;
struct nk_context* ctx = &game_state_get()->gui->context;
struct Game_State* game_state = game_state_get();
struct nk_context* ctx = game_state->game_mode == GAME_MODE_EDITOR ? &game_state->gui_editor->context : &game_state->gui_game;
nk_input_scroll(ctx, nk_vec2(x, y));
}

@ -0,0 +1,50 @@
#include "gui_game.h"
#include "gui.h"
#include "game.h"
#include "../system/platform.h"
static void gui_game_pause_menu(struct nk_context* context);
void gui_game_init(struct Gui* game_gui)
{
gui_theme_set(game_state_get()->gui_game, GT_RED);
}
void gui_game_cleanup(struct Gui* game_gui)
{
}
void gui_game_update(struct Gui* gui_game, float dt)
{
struct nk_context* context = &gui_game->context;
struct Game_State* game_state = game_state_get();
if(game_state->game_mode == GAME_MODE_GAME)
{
if(nk_begin(context, "Game Gui", nk_rect(50, 50, 400, 200), NK_WINDOW_CLOSABLE))
{
nk_layout_row_dynamic(context, 30, 1);
nk_label(context, "Hello from the game gui!", NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_MIDDLE);
nk_end(context);
}
}
else if(game_state->game_mode == GAME_MODE_PAUSE)
{
gui_game_pause_menu(context);
}
}
void gui_game_pause_menu(struct nk_context* context)
{
struct Game_State* game_state = game_state_get();
int window_width = 0, window_height = 0;
window_get_drawable_size(game_state->window, &window_width, &window_height);
if(nk_begin(context, "Pause Gui", nk_rect(0, 0, window_width, window_height), NK_WINDOW_NO_SCROLLBAR))
{
nk_layout_row_dynamic(context, 30, 1);
nk_label(context, "Hello from the Pause Menu!", NK_TEXT_ALIGN_CENTERED | NK_TEXT_ALIGN_MIDDLE);
nk_end(context);
}
}

@ -0,0 +1,10 @@
#ifndef GUI_GAME_H
#define GUI_GAME_H
struct Gui;
void gui_game_init(struct Gui* game_gui);
void gui_game_cleanup(struct Gui* game_gui);
void gui_game_update(struct Gui* gui_game, float dt);
#endif

@ -41,6 +41,7 @@ void input_init(void)
struct Key_Binding jump_keys = {KEY_SPACE, KMOD_NONE, KEY_NONE, KMOD_NONE, KS_INACTIVE};
struct Key_Binding sprint_keys = {KEY_LSHIFT, KMOD_NONE, KEY_RSHIFT, KMOD_NONE, KS_INACTIVE};
struct Key_Binding editor_toggle_keys = {KEY_F1, KMOD_NONE, KEY_NONE, KMOD_NONE, KS_INACTIVE};
struct Key_Binding pause_keys = {KEY_ESCAPE, KMOD_NONE, KEY_NONE, KMOD_NONE, KS_INACTIVE};
struct Key_Binding console_toggle_keys = {KEY_TILDE, KMOD_NONE, KEY_NONE, KMOD_NONE, KS_INACTIVE};
struct Key_Binding debug_vars_toggle_keys = {KEY_F2, KMOD_NONE, KEY_NONE, KMOD_NONE, KS_INACTIVE};
struct Key_Binding debug_vars_cycle_keys = {KEY_F3, KMOD_NONE, KEY_NONE, KMOD_NONE, KS_INACTIVE};
@ -58,6 +59,7 @@ void input_init(void)
input_map_create("Turn_Down", turn_down_keys);
input_map_create("Sprint", sprint_keys);
input_map_create("Jump", jump_keys);
input_map_create("Pause", pause_keys);
input_map_create("Editor_Toggle", editor_toggle_keys);
input_map_create("Console_Toggle", console_toggle_keys);
input_map_create("Debug_Vars_Toggle", debug_vars_toggle_keys);

@ -296,7 +296,8 @@ void renderer_render(struct Renderer* renderer, struct Scene* scene)
shader_unbind();
/* Render UI */
gui_render(game_state->gui, NK_ANTI_ALIASING_ON);
gui_render(game_state->gui_editor, NK_ANTI_ALIASING_ON);
gui_render(game_state->gui_game, NK_ANTI_ALIASING_ON);
}
void renderer_cleanup(struct Renderer* renderer)

@ -100,7 +100,8 @@ void sound_cleanup(struct Sound* sound)
void sound_source_instance_destroy(struct Sound* sound, uint source_instance)
{
Soloud_stop(sound->soloud_context, source_instance);
if(sound_source_instance_is_valid(sound, source_instance))
Soloud_stop(sound->soloud_context, source_instance);
}
void sound_source_instance_update_position(struct Sound* sound, uint source_instance, vec3 abs_pos)

Loading…
Cancel
Save