From 9b0901eaf8bca916def7e7f2fec501160404b360 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 5 Aug 2022 19:23:39 +0000 Subject: [PATCH] Put nuklear into its own ffi --- source/editor/editor.c | 21 ++---------- source/editor/editor.h | 4 +++ source/engine/mrbffi.c | 73 ++++++++++++++++++++++++++++++++++++++++++ source/engine/nuke.c | 43 +++++++++++++++++++++++++ source/engine/nuke.h | 15 +++++++++ source/engine/script.c | 8 +++++ source/engine/script.h | 1 + 7 files changed, 146 insertions(+), 19 deletions(-) create mode 100644 source/engine/nuke.c create mode 100644 source/engine/nuke.h diff --git a/source/editor/editor.c b/source/editor/editor.c index 0f14fc6..58df09a 100755 --- a/source/editor/editor.c +++ b/source/editor/editor.c @@ -1,19 +1,4 @@ -#define NK_INCLUDE_FIXED_TYPES -#define NK_INCLUDE_STANDARD_IO -#define NK_INCLUDE_STANDARD_VARARGS -#define NK_INCLUDE_DEFAULT_ALLOCATOR -#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT -#define NK_INCLUDE_FONT_BAKING -#define NK_INCLUDE_DEFAULT_FONT -#define NK_IMPLEMENTATION -#define NK_GLFW_GL3_IMPLEMENTATION -#define NK_KEYSTATE_BASED_INPUT - -#include "nuklear.h" -#include "nuklear_glfw_gl3.h" - -#define MAX_VERTEX_BUFFER 512 * 1024 -#define MAX_ELEMENT_BUFFER 128 * 1024 +#include nuke.h #include "openglrender.h" #include "editor.h" @@ -57,7 +42,6 @@ struct gameproject *cur_project; struct vec *projects; static char setpath[MAXPATH]; - // Menus // TODO: Pack this into a bitfield static struct editorVars editor = { 0 }; @@ -421,8 +405,7 @@ static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) } } -static struct nk_context *ctx; -static struct nk_glfw nkglfw = {0}; + void editor_init(struct mSDLWindow *window) { diff --git a/source/editor/editor.h b/source/editor/editor.h index a623a6f..42af9d5 100755 --- a/source/editor/editor.h +++ b/source/editor/editor.h @@ -45,6 +45,10 @@ struct mSDLWindow; void pickGameObject(int pickID); int is_allowed_extension(const char *ext); +void nuk_init(struct mSDLWindow *window); + + + void editor_init(struct mSDLWindow *window); void editor_input(); void editor_render(); diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c index 6906e6f..0783be2 100644 --- a/source/engine/mrbffi.c +++ b/source/engine/mrbffi.c @@ -2,8 +2,13 @@ #include "mruby.h" #include "mruby/compile.h" +#include "mruby/string.h" + +#include "font.h" + #include "script.h" +#include "string.h" extern mrb_state *mrb; @@ -38,9 +43,77 @@ mrb_value mrb_ui_label(mrb_state *mrb, mrb_value self) { return self; } +mrb_value mrb_ui_btn(mrb_state *mrb, mrb_value self) { + char *str; + mrb_get_args(mrb, "z", &str); + return mrb_bool_value(nk_button_label(ctx, str)); +} + +mrb_value mrb_ui_nel(mrb_state *mrb, mrb_value self) { + int height, cols; + mrb_get_args(mrb, "ii", &height, &cols); + nk_layout_row_dynamic(ctx, height, cols); + return self; +} + +mrb_value mrb_ui_prop(mrb_state *mrb, mrb_value self) { + mrb_float min, max, step1, step2, val; + char *s; + mrb_get_args(mrb, "zfffff", &s, &val, &min, &max, &step1, &step2); + nk_property_float(ctx, s, min, &val, max, step1, step2); + return mrb_float_value(mrb, val); +} + +mrb_value mrb_ui_text(mrb_state *mrb, mrb_value self) { + char *s; + mrb_float len; + + mrb_get_args(mrb, "zf", &s, &len); + + char str[(int)len+1]; + strncpy(str, s,len); + nk_edit_string_zero_terminated(ctx, NK_EDIT_BOX|NK_EDIT_NO_HORIZONTAL_SCROLL, str, 130, nk_filter_ascii); + + return mrb_str_new_cstr(mrb, str); +} + +mrb_value mrb_ui_begin(mrb_state *mrb, mrb_value self) { + char *title; + mrb_float w, h; + + mrb_get_args(mrb, "zff", &title, &w, &h); + + return mrb_bool_value(nk_begin(ctx, title, nk_rect(0,0,w,h), NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_SCALABLE|NK_WINDOW_MOVABLE|NK_WINDOW_NO_SCROLLBAR)); +} + +mrb_value mrb_ui_rendertext(mrb_state *mrb, mrb_value self) { + char *s; + mrb_float pos[2], size, ol; + mrb_get_args(mrb, "zffff", &s, &pos[0], &pos[1], &size, &ol); + + static float white[3] = {1.f, 1.f, 1.f}; + renderText(s, pos, size, white, ol); + return self; +} + +mrb_value mrb_c_reload(mrb_state *mrb, mrb_value self) { + +} + void ffi_load() { mrb_define_method(mrb, mrb->object_class, "fib", mrb_fib, MRB_ARGS_REQ(1)); mrb_define_method(mrb, mrb->object_class, "load", mrb_load, MRB_ARGS_REQ(1)); mrb_define_method(mrb, mrb->object_class, "ui_label", mrb_ui_label, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, mrb->object_class, "ui_btn", mrb_ui_btn, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, mrb->object_class, "ui_nel", mrb_ui_nel, MRB_ARGS_REQ(2)); + mrb_define_method(mrb, mrb->object_class, "ui_begin", mrb_ui_begin, MRB_ARGS_REQ(3)); + + mrb_define_method(mrb, mrb->object_class, "ui_prop", mrb_ui_prop, MRB_ARGS_REQ(6)); + mrb_define_method(mrb, mrb->object_class, "ui_text", mrb_ui_text, MRB_ARGS_REQ(2)); + + + mrb_define_method(mrb, mrb->object_class, "ui_rendertext", mrb_ui_rendertext, MRB_ARGS_REQ(5)); + + mrb_define_method(mrb, mrb->object_class, "c_reload", mrb_c_reload, MRB_ARGS_REQ(1)); } diff --git a/source/engine/nuke.c b/source/engine/nuke.c new file mode 100644 index 0000000..ef2ce0b --- /dev/null +++ b/source/engine/nuke.c @@ -0,0 +1,43 @@ +#define NK_INCLUDE_FIXED_TYPES +#define NK_INCLUDE_STANDARD_IO +#define NK_INCLUDE_STANDARD_VARARGS +#define NK_INCLUDE_DEFAULT_ALLOCATOR +#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT +#define NK_INCLUDE_FONT_BAKING +#define NK_INCLUDE_DEFAULT_FONT +#define NK_IMPLEMENTATION +#define NK_GLFW_GL3_IMPLEMENTATION +#define NK_KEYSTATE_BASED_INPUT + +#include "nuke.h" + +#include "window.h" + +#define MAX_VERTEX_BUFFER 512 * 1024 +#define MAX_ELEMENT_BUFFER 128 * 1024 + +struct nk_context *ctx; +static struct nk_glfw nkglfw = {0}; + + +void nuke_init(struct mSDLWindow *win) { + window_makecurrent(win); + + ctx = nk_glfw3_init(&nkglfw, win->window, NK_GLFW3_INSTALL_CALLBACKS); + + struct nk_font_atlas *atlas; + nk_glfw3_font_stash_begin(&nkglfw, &atlas); + struct nk_font *noto = nk_font_atlas_add_from_file(atlas, "fonts/notosans.tff", 14, 0); + nk_glfw3_font_stash_end(&nkglfw); +} + +void nuke_start() +{ + nk_glfw3_new_frame(&nkglfw); +} + +void nuke_end() +{ + nk_end(ctx); + nk_glfw3_render(&nkglfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER); +} \ No newline at end of file diff --git a/source/engine/nuke.h b/source/engine/nuke.h new file mode 100644 index 0000000..90f0c16 --- /dev/null +++ b/source/engine/nuke.h @@ -0,0 +1,15 @@ +#ifndef NUKE_H +#define NUKE_H + +#include "nuklear.h" +#include "nuklear_glfw_gl3.h" + +extern struct nk_context *ctx; + +struct mSDLWindow; + +void nuke_init(struct mSDLWindow *win); +void nuke_start(); +void nuke_end(); + +#endif \ No newline at end of file diff --git a/source/engine/script.c b/source/engine/script.c index 06d9eba..c344764 100755 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -36,12 +36,20 @@ void script_dofile(const char *file) { void script_update() { mrb_funcall(mrb, obj, "update", 0); + mrb_print_error(mrb); } void script_draw() { mrb_funcall(mrb, obj, "draw", 0); + mrb_print_error(mrb); } void script_editor() { mrb_funcall(mrb, obj, "editor", 0); + mrb_print_error(mrb); +} + +void script_call(const char *f) { + mrb_funcall(mrb, obj, f, 0); + mrb_print_error(mrb); } \ No newline at end of file diff --git a/source/engine/script.h b/source/engine/script.h index 0cdb760..7bf358e 100755 --- a/source/engine/script.h +++ b/source/engine/script.h @@ -7,5 +7,6 @@ void script_dofile(const char *file); void script_update(); void script_draw(); void script_editor(); +void script_call(const char *f); #endif