Beginnings of Guile integration

This commit is contained in:
2025-07-20 12:46:54 +02:00
parent 6e5234b7da
commit e708f319f3
8 changed files with 160 additions and 67 deletions
+2 -2
View File
@@ -7,8 +7,8 @@ DEP := $(OBJ:.o=.d)
CC := gcc
LD := gcc
CFLAGS := -g -Werror -Wextra -Wall -MMD -MP `pkg-config --cflags sdl3 sdl3-image cglm gl glib-2.0`
LDFLAGS := -g -lm `pkg-config --libs sdl3 sdl3-image cglm gl glib-2.0`
CFLAGS := -g -Werror -Wextra -Wall -MMD -MP `pkg-config --cflags sdl3 sdl3-image cglm gl glib-2.0 guile-3.0`
LDFLAGS := -g -lm `pkg-config --libs sdl3 sdl3-image cglm gl glib-2.0 guile-3.0`
TARGET := wschod
.PHONY: all clean
+1 -1
View File
@@ -85,7 +85,7 @@ void main ()
if (in_progress)
{
gl_FragColor = vec4 (1.0, 1.0, 1.0, 1.0);
gl_FragColor = progress_color;
return;
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ pkgs.mkShellNoCC {
cglm
glew
glib
lua
guile
gcc
pkg-config
+5
View File
@@ -1,10 +1,12 @@
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL_main.h>
#include <libguile.h>
#include "event/evtman.h"
#include "chunk/chunkman.h"
#include "entity/templates.h"
#include "ui/uiman.h"
#include "ui/widget_guile.h"
#include "state.h"
#include "resources.h"
#include "graphics.h"
@@ -17,6 +19,9 @@ SDL_AppInit (void **appstate, int argc, char **argv)
UNUSED (appstate, argc, argv);
SDL_SetAppMetadata (GAME_NAME, GAME_VERSION, "eur.chopininteractive.wschod-game");
scm_init_guile ();
widget_guile_register ();
graphics_init ();
resources_init ();
flags_init ();
+9 -52
View File
@@ -20,60 +20,17 @@ static Widget *hovered_widget = NULL;
static Widget *clicked_widget = NULL;
static Widget *focused_widget = NULL;
static Widget *tooltip_widget = NULL;
static Widget *tooltip_text = NULL;
/* static Widget *tooltip_widget = NULL; */
/* static Widget *tooltip_text = NULL; */
void
uiman_init ()
{
ui_widget_mesh_init ();
glm_ortho (0.0, sdl_window_w, sdl_window_h, 0.0, -1.0, 1.0, projection);
tooltip_widget = Widget_flowbox_new (TRUE);
tooltip_widget->visible = FALSE;
tooltip_widget->size[0] = 200;
tooltip_widget->layout_padding = 4;
tooltip_widget->layout_spacing = 8;
tooltip_widget->style = WSTYLE (STYLE_TOOLTIP);
tooltip_text = Widget_multiline_text_new (NULL, 20, TEXT_ALIGN_CENTER);
Widget_add_child_flowbox (tooltip_widget, tooltip_text, 1, 0);
ui_root = Widget_plain_new ();
IVEC2_SETI (ui_root->size, sdl_window_w, sdl_window_h);
Widget *debug_window = Widget_window_new ("Debug menu", 20);
debug_window->size[0] = 350;
debug_window->window.resizable_v = FALSE;
debug_window->layout_padding = 8;
debug_window->style = WSTYLE (STYLE_WINDOW);
Widget *debug_menu = Widget_flowbox_new (TRUE);
debug_menu->layout_padding = 8;
debug_menu->layout_spacing = 8;
debug_menu->style = WSTYLE (STYLE_CONTAINER);
Widget_add_child_flowbox (debug_menu, Widget_label_make_float ("Frame time: %.2f", &delta_time, TEXT_ALIGN_LEFT), 1, 20);
Widget_add_child_flowbox (debug_menu, Widget_label_make_int ("Chunks: %d", &n_frame_chunks, TEXT_ALIGN_LEFT), 1./2., 20);
Widget_add_child_flowbox (debug_menu, Widget_label_make_int ("Entities: %d", &n_frame_ents, TEXT_ALIGN_LEFT), 1./2., 20);
Widget_add_child_flowbox (debug_menu, Widget_label_make_float ("X: %.1f", &camera_position[0], TEXT_ALIGN_LEFT), 1./3., 20);
Widget_add_child_flowbox (debug_menu, Widget_label_make_float ("Y: %.1f", &camera_position[1], TEXT_ALIGN_CENTER), 1./3., 20);
Widget_add_child_flowbox (debug_menu, Widget_label_make_float ("Z: %.1f", &camera_position[2], TEXT_ALIGN_RIGHT), 1./3., 20);
Widget *spinner = Widget_composite_spinner_make_int ("Render distance: %d", &render_distance);
spinner->tooltip = g_string_new ("Right click to show a context menu");
Widget_add_child_flowbox (debug_menu, spinner, 1, 20);
Widget_add_child_flowbox (debug_menu, Widget_composite_checkbox_make ("Wireframe", "wireframe_enabled"), 1./2., 20);
Widget_add_child_flowbox (debug_menu, Widget_composite_checkbox_make ("Flight", "flight_enabled"), 1./2., 20);
Widget_add_child_flowbox (debug_menu, Widget_entry_new ("Test entry"), 1, 20);
Widget_add_child_flowbox (debug_menu, Widget_progress_new (TRUE, 0, 0.1, &delta_time), 1, 20);
Widget_add_child (debug_window, debug_menu);
Widget_add_child (ui_root, debug_window);
Widget_dump (ui_root, 0);
}
void
@@ -86,7 +43,7 @@ void
uiman_render ()
{
Widget_render (ui_root, projection, sdl_window_h);
Widget_render (tooltip_widget, projection, sdl_window_h);
/* Widget_render (tooltip_widget, projection, sdl_window_h); */
}
void
@@ -150,7 +107,7 @@ uiman_cb_switch_focus (SDL_Event *event, void *data)
_uiman_set_clicked (clicked_widget, FALSE);
clicked_widget = NULL;
tooltip_widget->visible = FALSE;
/* tooltip_widget->visible = FALSE; */
return TRUE;
}
@@ -217,13 +174,13 @@ uiman_cb_mouse_motion (SDL_Event *event, void *data)
if (tooltip_source && tooltip_source->tooltip && tooltip_source->tooltip->len > 0)
{
Widget_multiline_text_set_text (tooltip_text, tooltip_source->tooltip->str);
tooltip_widget->visible = TRUE;
IVEC2_SETI (tooltip_widget->position, pos[0] + 12, pos[1] + 12);
/* Widget_multiline_text_set_text (tooltip_text, tooltip_source->tooltip->str); */
/* tooltip_widget->visible = TRUE; */
/* IVEC2_SETI (tooltip_widget->position, pos[0] + 12, pos[1] + 12); */
}
else
{
tooltip_widget->visible = FALSE;
/* tooltip_widget->visible = FALSE; */
}
/* This callback always eats the event, because why would the game
+131
View File
@@ -0,0 +1,131 @@
#include "widget_guile.h"
static Widget *
_wgg_build (const char *type)
{
Widget *e = g_new0 (Widget, 1);
e->children = g_array_new (false, false, sizeof (Widget *));
e->style = WSTYLE (STYLE_DEFAULT);
if (g_strcmp0 (type, "plain") == 0)
{
e->kind = WIDGET_PLAIN;
}
if (g_strcmp0 (type, "label") == 0)
{
e->kind = WIDGET_LABEL;
}
if (g_strcmp0 (type, "scrollbar") == 0)
{
e->kind = WIDGET_SCROLLBAR;
}
if (g_strcmp0 (type, "progress") == 0)
{
e->kind = WIDGET_PROGRESS;
}
if (g_strcmp0 (type, "entry") == 0)
{
e->kind = WIDGET_ENTRY;
}
if (g_strcmp0 (type, "multiline") == 0)
{
e->kind = WIDGET_MULTILINE_TEXT;
}
if (g_strcmp0 (type, "container") == 0)
{
e->kind = WIDGET_CONTAINER;
}
if (g_strcmp0 (type, "scrollable") == 0)
{
e->kind = WIDGET_SCROLLABLE;
}
if (g_strcmp0 (type, "flowbox") == 0)
{
e->kind = WIDGET_FLOWBOX;
}
if (g_strcmp0 (type, "window") == 0)
{
e->kind = WIDGET_WINDOW;
}
return NULL;
}
static void
_wgg_set_property (Widget *w, const char *key, SCM val)
{
UNUSED (w, key, val);
}
// (widget-new TYPE #:PROP1 VALUE1 ... #:PROPN VALUEN child1 ... childn)
static SCM
_wg_widget_new (SCM args)
{
if (!scm_is_pair (args))
{
scm_wrong_num_args (SCM_UNDEFINED);
}
SCM type_sym = scm_car (args);
args = scm_cdr (args);
const char *type = scm_symbol_to_string (type_sym) ? scm_to_utf8_string (scm_symbol_to_string (type_sym)) : NULL;
if (!type)
{
scm_misc_error ("widget-new", "Invalid widget type symbol", SCM_EOL);
}
Widget *w = _wgg_build (type);
if (!w)
{
scm_misc_error ("widget-new", "Unknown widget type", scm_list_1 (type_sym));
}
// Parse keyword args
while (scm_is_pair (args))
{
SCM key = scm_car (args);
args = scm_cdr (args);
if (!scm_is_keyword (key))
{
break;
}
if (!scm_is_pair (args))
{
scm_misc_error ("widget-new", "Expected value after keyword", SCM_EOL);
}
SCM val = scm_car (args);
args = scm_cdr (args);
_wgg_set_property (w, scm_to_utf8_string (scm_keyword_to_symbol (key)), val);
}
// Remaining args = children
while (scm_is_pair (args))
{
SCM child = scm_car (args);
args = scm_cdr (args);
Widget *cw = scm_to_pointer (child);
Widget_add_child (w, cw);
}
return scm_from_pointer (w, NULL);
}
void
widget_guile_register ()
{
scm_c_define_gsubr ("widget-new", 0, 0, 1, _wg_widget_new);
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include <libguile.h>
#include "widget_fw.h"
#include "widget_core.h"
void widget_guile_register ();
+3 -11
View File
@@ -67,27 +67,19 @@ static const WidgetStyle widget_styles[NSTYLES] = {
},
[STYLE_TOOLTIP] = {
.background_color = COLOR_BLACK,
.background_color_hovered = COLOR_BLACK,
.background_color_clicked = COLOR_BLACK,
.background_color = { 0.1, 0.1, 0.1, 1.0 },
.background_color_hovered = { 0.1, 0.1, 0.1, 1.0 },
.background_color_clicked = { 0.1, 0.1, 0.1, 1.0 },
.text_color = COLOR_WHITE,
.text_color_hovered = COLOR_WHITE,
.text_color_clicked = COLOR_WHITE,
.border_width = 0,
},
[STYLE_ICON_BUTTON] = {
.text_color = { 0.0, 0.0, 0.0, 1.0 },
.text_color_hovered = { 0.0, 0.0, 0.0, 1.0 },
.text_color_clicked = { 0.0, 0.0, 0.0, 1.0 },
.icon_color = COLOR_WHITE,
.icon_color_hovered = { 1.0, 1.0, 0.0, 1.0 },
.icon_color_clicked = { 1.0, 0.0, 0.0, 1.0 },
.border_width = 0,
},
[STYLE_WINDOW] = {