From 6a7f4c990309aa1d755281ec0fbe047e97fe604d Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Mon, 21 Jul 2025 21:28:00 +0200 Subject: [PATCH] Main menu, loading into game, etc --- game/res/manifest.cfg | 13 ++--- .../{global/ui_styles.scm => g_styles.scm} | 8 +++ .../ui_composites.scm => g_uielements.scm} | 0 .../{ui/debug_window.scm => u_debug.scm} | 0 game/res/scripts/u_main_menu.scm | 11 ++++ .../scripts/{ui/tooltip.scm => u_tooltip.scm} | 3 +- game/src/main.c | 6 +-- game/src/pakman.c | 8 +++ game/src/states/state_game.c | 54 +++++++++++++++++++ game/src/states/state_game.h | 9 ++++ game/src/states/state_global.c | 3 -- game/src/states/state_main_menu.c | 22 -------- game/src/states/stateman.c | 15 ++++++ game/src/states/stateman.h | 2 + game/src/ui/ui_context.c | 2 - 15 files changed, 118 insertions(+), 38 deletions(-) rename game/res/scripts/{global/ui_styles.scm => g_styles.scm} (84%) rename game/res/scripts/{global/ui_composites.scm => g_uielements.scm} (100%) rename game/res/scripts/{ui/debug_window.scm => u_debug.scm} (100%) create mode 100644 game/res/scripts/u_main_menu.scm rename game/res/scripts/{ui/tooltip.scm => u_tooltip.scm} (63%) create mode 100644 game/src/states/state_game.c create mode 100644 game/src/states/state_game.h diff --git a/game/res/manifest.cfg b/game/res/manifest.cfg index 90d97a2..2f32e92 100644 --- a/game/res/manifest.cfg +++ b/game/res/manifest.cfg @@ -47,10 +47,11 @@ texturemap=res/map/texturemap.png entitymap=res/map/entitymap.png [UIDefs] -tooltip=res/scripts/ui/tooltip.scm -main_menu=res/scripts/ui/debug_window.scm +tooltip=res/scripts/u_tooltip.scm +main_menu=res/scripts/u_main_menu.scm +debug_window=res/scripts/u_debug.scm -# The scripts are loaded and executed after everything above has been -# loaded, so that Guile has some stuff to work with. -[Scripts] -global=res/scripts/global/ui_styles.scm;res/scripts/global/ui_composites.scm +# The names of the scripts are ignored. +[RunScripts] +uistyles=res/scripts/g_styles.scm +uielements=res/scripts/g_uielements.scm \ No newline at end of file diff --git a/game/res/scripts/global/ui_styles.scm b/game/res/scripts/g_styles.scm similarity index 84% rename from game/res/scripts/global/ui_styles.scm rename to game/res/scripts/g_styles.scm index bb814cf..f8db6dd 100644 --- a/game/res/scripts/global/ui_styles.scm +++ b/game/res/scripts/g_styles.scm @@ -40,6 +40,14 @@ #:icon-color-hovered '(1.0 1.0 0.0 1.0) #:icon-color-clicked '(1.0 0.0 0.0 1.0))) +(define style-button + (style-new #:background-color '(0.1 0.1 0.1 1.0) + #:background-color-hovered '(0.4 0.4 0.4 1.0) + #:background-color-clicked '(0.2 0.2 0.2 1.0) + #:text-color COLOR_WHITE + #:text-color-hovered COLOR_WHITE + #:text-color-clicked COLOR_WHITE)) + (define style-tooltip (style-new #:text-color COLOR_WHITE diff --git a/game/res/scripts/global/ui_composites.scm b/game/res/scripts/g_uielements.scm similarity index 100% rename from game/res/scripts/global/ui_composites.scm rename to game/res/scripts/g_uielements.scm diff --git a/game/res/scripts/ui/debug_window.scm b/game/res/scripts/u_debug.scm similarity index 100% rename from game/res/scripts/ui/debug_window.scm rename to game/res/scripts/u_debug.scm diff --git a/game/res/scripts/u_main_menu.scm b/game/res/scripts/u_main_menu.scm new file mode 100644 index 0000000..7e08680 --- /dev/null +++ b/game/res/scripts/u_main_menu.scm @@ -0,0 +1,11 @@ +(widget-new + 'flowbox + #:style style-container + #:layout-padding 8 + #:layout-spacing 8 + #:horizontal #t + (widget-new 'label #:style style-button #:layout-weight 1 #:h 20 #:text-alignment 'center #:text "New game" + #:on-click (lambda (w e) (stateman-switch-state "game"))) + (widget-new 'label #:style style-button #:layout-weight 1 #:h 20 #:text-alignment 'center #:text "Options") + (widget-new 'label #:style style-button #:layout-weight 1 #:h 20 #:text-alignment 'center #:text "Quit game")) + diff --git a/game/res/scripts/ui/tooltip.scm b/game/res/scripts/u_tooltip.scm similarity index 63% rename from game/res/scripts/ui/tooltip.scm rename to game/res/scripts/u_tooltip.scm index 63e7ec6..7672380 100644 --- a/game/res/scripts/ui/tooltip.scm +++ b/game/res/scripts/u_tooltip.scm @@ -3,4 +3,5 @@ #:visible #f #:layout-padding 4 #:line-height 20 - #:style style-tooltip) + #:style style-tooltip + #:text-alignment 'center) diff --git a/game/src/main.c b/game/src/main.c index 2f309e6..30aa04d 100644 --- a/game/src/main.c +++ b/game/src/main.c @@ -21,14 +21,11 @@ SDL_AppInit (void **appstate, int argc, char **argv) things. */ scm_init_guile (); gvar_guile_register (); + stateman_guile_register (); widget_guile_register (); entity_guile_register (); pakman_guile_register (); - /* TODO: move this somewhere. */ - scm_c_primitive_load ("res/scripts/global/ui_styles.scm"); - scm_c_primitive_load ("res/scripts/global/ui_composites.scm"); - /* The basics; game variables, graphics, the state manager, and the pak manager. */ gvar_init (); @@ -44,6 +41,7 @@ SDL_AppInit (void **appstate, int argc, char **argv) ui_widget_mesh_init (); /* The UI, entity and chunk subsystems. */ + ent_templates_init (); entman_init (); chunkman_init (); diff --git a/game/src/pakman.c b/game/src/pakman.c index 66e9a16..628a37c 100644 --- a/game/src/pakman.c +++ b/game/src/pakman.c @@ -124,6 +124,14 @@ pakman_load_pak (const char *path) g_hash_table_insert (pak->textures, g_strdup (*t), GUINT_TO_POINTER (_pm_load_texture (texture))); } + char **run = g_key_file_get_keys (key_file, "RunScripts", NULL, &err); + + for (char **r = run; *r != NULL; r++) + { + char *run_path = g_key_file_get_string (key_file, "RunScripts", *r, &err); + scm_c_primitive_load (run_path); + } + char **uidefs = g_key_file_get_keys (key_file, "UIDefs", NULL, &err); for (char **u = uidefs; *u != NULL; u++) diff --git a/game/src/states/state_game.c b/game/src/states/state_game.c new file mode 100644 index 0000000..6f9735d --- /dev/null +++ b/game/src/states/state_game.c @@ -0,0 +1,54 @@ +#include "state_game.h" + +extern vec3 camera_position; + +static void +_on_enter () +{ + gvar_set ("ui_has_focus", TRUE); +} + +static void +_on_update () +{ + chunkman_tick (camera_position); + entman_tick (); +} + +static void +_on_render () +{ + mat4 projection, view; + camera_get_projection (gvar_get ("window_width") / gvar_get ("window_height"), projection); + camera_get_view (view); + + pakman_all_paks_set_mat4 ("projection", projection); + pakman_all_paks_set_mat4 ("view", view); + pakman_all_paks_set_float ("time", gvar_get ("last_time")); + + vec4 planes[6]; +#if FRUSTUM_CULL + mat4 view_proj; + glm_mat4_mul (projection, view, view_proj); + glm_frustum_planes (view_proj, planes); +#endif + + chunkman_render (camera_position, planes); + entman_render (view, planes); +} + +GameState * +state_game_new () +{ + GameState *g = GameState_new (); + + g->on_enter = _on_enter; + g->on_update = _on_update; + g->on_render = _on_render; + + /* If your GameState uses the UI, always call this. */ + GameState_register_UIContext_handlers (g); + g->ui_context->root = PMUID ("debug_window"); + + return g; +} diff --git a/game/src/states/state_game.h b/game/src/states/state_game.h new file mode 100644 index 0000000..8e6b6f0 --- /dev/null +++ b/game/src/states/state_game.h @@ -0,0 +1,9 @@ +#pragma once + +#include "state_common.h" + +#include "../graphics.h" +#include "../gvar.h" +#include "../camera.h" + +GameState *state_game_new (); diff --git a/game/src/states/state_global.c b/game/src/states/state_global.c index 6fbdc4b..794b328 100644 --- a/game/src/states/state_global.c +++ b/game/src/states/state_global.c @@ -19,8 +19,6 @@ _on_update () gvar_set ("last_time", now); } -static void _on_render () { return; } - GameState * state_global_new () { @@ -35,7 +33,6 @@ state_global_new () self->on_enter = _on_enter; self->on_exit = _on_exit; self->on_update = _on_update; - self->on_render = _on_render; return self; } diff --git a/game/src/states/state_main_menu.c b/game/src/states/state_main_menu.c index 3f38147..d5da027 100644 --- a/game/src/states/state_main_menu.c +++ b/game/src/states/state_main_menu.c @@ -6,37 +6,15 @@ _on_enter () gvar_set ("ui_has_focus", TRUE); } -static void -_on_exit () -{ - -} - -static void -_on_update () -{ - -} - -static void -_on_render () -{ - -} - GameState * state_main_menu_new () { GameState *g = GameState_new (); g->on_enter = _on_enter; - g->on_exit = _on_exit; - g->on_update = _on_update; - g->on_render = _on_render; /* If your GameState uses the UI, always call this. */ GameState_register_UIContext_handlers (g); - g->ui_context->root = PMUID ("main_menu"); return g; diff --git a/game/src/states/stateman.c b/game/src/states/stateman.c index b533235..ef85370 100644 --- a/game/src/states/stateman.c +++ b/game/src/states/stateman.c @@ -11,11 +11,26 @@ stateman_init () game_states = g_hash_table_new (g_str_hash, g_str_equal); stateman_add_state ("main_menu", state_main_menu_new ()); + stateman_add_state ("game", state_game_new ()); global_state = state_global_new (); global_state->on_enter (global_state); } +static SCM +_scm_switch_state (SCM state_val) +{ + char *state = scm_to_utf8_string (state_val); + stateman_switch_state (state); + return SCM_UNSPECIFIED; +} + +void +stateman_guile_register () +{ + scm_c_define_gsubr ("stateman-switch-state", 1, 0, 0, _scm_switch_state); +} + void stateman_add_state (const char *name, GameState *state) { diff --git a/game/src/states/stateman.h b/game/src/states/stateman.h index d1e3ac3..ff3f082 100644 --- a/game/src/states/stateman.h +++ b/game/src/states/stateman.h @@ -6,8 +6,10 @@ #include "state_global.h" #include "state_main_menu.h" +#include "state_game.h" void stateman_init (); +void stateman_guile_register (); static inline void stateman_free () { } /* TODO: implement */ void stateman_add_state (const char *name, GameState *state); void stateman_switch_state (const char *name); diff --git a/game/src/ui/ui_context.c b/game/src/ui/ui_context.c index 3013db2..a33d84a 100644 --- a/game/src/ui/ui_context.c +++ b/game/src/ui/ui_context.c @@ -141,8 +141,6 @@ UIContext_mouse_motion (SDL_Event *event, void *data) like scrollbars. */ _call_on_event (self->clicked_widget, event, EVENT_MOUSE_MOVE); - /* TODO: Later... */ - Widget *tooltip_source = Widget_find_xy_cond (self->root, pos, wcond_tooltip, NULL); if (tooltip_source && tooltip_source->tooltip && tooltip_source->tooltip->len > 0)