Put nuklear into its own ffi

This commit is contained in:
John Alanbrook 2022-08-05 19:23:39 +00:00
parent a296b77c45
commit 9b0901eaf8
7 changed files with 146 additions and 19 deletions

View file

@ -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)
{

View file

@ -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();

View file

@ -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));
}

43
source/engine/nuke.c Normal file
View file

@ -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);
}

15
source/engine/nuke.h Normal file
View file

@ -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

View file

@ -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);
}

View file

@ -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