From 406b2b491f4b7ac4602afcf2264d2dd8b69b6f4a Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sun, 7 Aug 2022 06:43:45 +0000 Subject: [PATCH] Brainstorm is now fully in ruby --- Makefile | 1 - source/engine/font.c | 5 +++ source/engine/mrbffi.c | 43 +++++++++++++++++++++++ source/engine/nuke.c | 1 + source/engine/nuke.h | 2 +- source/engine/openglrender.c | 25 +++++++++----- source/engine/openglrender.h | 2 +- source/engine/script.c | 18 +++++++--- source/engine/script.h | 6 +++- source/engine/window.c | 66 +++++++++++++++++++++++++++--------- source/engine/window.h | 9 +++-- source/engine/yugine.c | 62 ++++++++++++--------------------- 12 files changed, 164 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index 0bbe3a5..309dac5 100755 --- a/Makefile +++ b/Makefile @@ -11,7 +11,6 @@ UNAME_P != uname -m CCACHE = ccache -#CC specifies which compiler we're using CC = $(CCACHE) clang -DSDL_DISABLE_IMMINTRIN_H CLINK = clang diff --git a/source/engine/font.c b/source/engine/font.c index a03ee4e..5ad2ff4 100755 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -20,6 +20,11 @@ unsigned char temp_bitmap[512 * 512]; struct sFont *font; static struct mShader *shader; +/* + mfont = MakeFont("notosans.ttf", 300); + text_settype(mfont); +*/ + void font_init(struct mShader *textshader) { shader = textshader; diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c index 0783be2..ffa3ad3 100644 --- a/source/engine/mrbffi.c +++ b/source/engine/mrbffi.c @@ -3,12 +3,14 @@ #include "mruby.h" #include "mruby/compile.h" #include "mruby/string.h" +#include "mruby/hash.h" #include "font.h" #include "script.h" #include "string.h" +#include "window.h" extern mrb_state *mrb; @@ -97,6 +99,40 @@ mrb_value mrb_ui_rendertext(mrb_state *mrb, mrb_value self) { } mrb_value mrb_c_reload(mrb_state *mrb, mrb_value self) { + return self; +} + +mrb_value mrb_win_make(mrb_state *mrb, mrb_value self) { + char name[50] = "New Window"; + struct mSDLWindow *new = MakeSDLWindow(name, 500, 500, 0); + return mrb_float_value(mrb, new->id); +} + +mrb_value mrb_nuke_cb(mrb_state *mrb, mrb_value self) { + mrb_float win; + mrb_sym cb; + mrb_get_args(mrb, "fn", &win, &cb); + window_i((int)win)->nuke_cb = cb; + return self; +} + +mrb_value mrb_gui_cb(mrb_state *mrb, mrb_value self) { + mrb_float win; + mrb_sym cb; + mrb_get_args(mrb, "fn", &win, &cb); + window_i((int)win)->gui_cb = cb; + return self; +} + +mrb_value mrb_sound_make(mrb_state *mrb, mrb_value self) { + mrb_value vals; + mrb_get_args(mrb, "H", &vals); + char *name = mrb_str_to_cstr(mrb, mrb_hash_fetch(mrb, vals, mrb_symbol_value(mrb_intern_cstr(mrb, "name")), mrb_str_new_cstr(mrb, "New Window"))); + printf("Window name is %s.\n", name); + return self; +} + +mrb_value mrb_sound_cmd(mrb_state *mrb, mrb_value self) { } @@ -115,5 +151,12 @@ void ffi_load() { 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)); + + mrb_define_method(mrb, mrb->object_class, "win_make", mrb_win_make, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, mrb->object_class, "nuke_cb", mrb_nuke_cb, MRB_ARGS_REQ(2)); + mrb_define_method(mrb, mrb->object_class, "gui_cb", mrb_gui_cb, MRB_ARGS_REQ(2)); + + mrb_define_method(mrb, mrb->object_class, "sound_make", mrb_sound_make, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, mrb->object_class, "sound_cmd", mrb_sound_cmd, MRB_ARGS_REQ(2)); } diff --git a/source/engine/nuke.c b/source/engine/nuke.c index ef2ce0b..04dcd6d 100644 --- a/source/engine/nuke.c +++ b/source/engine/nuke.c @@ -10,6 +10,7 @@ #define NK_KEYSTATE_BASED_INPUT #include "nuke.h" +#include "nuklear_glfw_gl3.h" #include "window.h" diff --git a/source/engine/nuke.h b/source/engine/nuke.h index 90f0c16..33c05d0 100644 --- a/source/engine/nuke.h +++ b/source/engine/nuke.h @@ -2,7 +2,7 @@ #define NUKE_H #include "nuklear.h" -#include "nuklear_glfw_gl3.h" + extern struct nk_context *ctx; diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 677bf81..cb6422b 100755 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -172,33 +172,40 @@ void openglInit() } -void openglRender(struct mSDLWindow *window, struct mCamera *mcamera) + +static struct mCamera mcamera = {0}; +void openglRender(struct mSDLWindow *window) { + glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.3f, 0.3f, 0.3f, 1.f); + + //////////// 2D projection mfloat_t projection[16] = { 0.f }; - mat4_ortho(projection, mcamera->transform.position[0], - window->width + mcamera->transform.position[0], - mcamera->transform.position[1], - window->height + mcamera->transform.position[1], -1.f, 1.f); + mat4_ortho(projection, mcamera.transform.position[0], + window->width + mcamera.transform.position[0], + mcamera.transform.position[1], + window->height + mcamera.transform.position[1], -1.f, 1.f); glBindBuffer(GL_UNIFORM_BUFFER, projUBO); glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection); - shader_setmat4(vid_shader, "projection", projection); + //shader_setmat4(vid_shader, "projection", projection); glEnable(GL_DEPTH_TEST); // Clear color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); ////// TEXT && GUI + script_call_sym(window->gui_cb); ///// Sprites glDepthFunc(GL_LESS); shader_use(spriteShader); sprite_draw_all(); - glDepthFunc(GL_ALWAYS); - shader_use(textShader); - shader_setmat4(textShader, "projection", projection); + glDepthFunc(GL_ALWAYS); + shader_use(textShader); + shader_setmat4(textShader, "projection", projection); } diff --git a/source/engine/openglrender.h b/source/engine/openglrender.h index 42c9832..36e31f8 100755 --- a/source/engine/openglrender.h +++ b/source/engine/openglrender.h @@ -43,7 +43,7 @@ enum RenderMode { }; void openglInit(); -void openglRender(struct mSDLWindow *window, struct mCamera *camera); +void openglRender(struct mSDLWindow *window); void openglInit3d(struct mSDLWindow *window); void openglRender3d(struct mSDLWindow *window, struct mCamera *camera); diff --git a/source/engine/script.c b/source/engine/script.c index c344764..a10deba 100755 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -3,7 +3,7 @@ #include "stdio.h" #include "log.h" -#include "mruby.h" + #include "mruby/compile.h" #include "mrbffi.h" @@ -34,8 +34,8 @@ void script_dofile(const char *file) { fclose(mrbf); } -void script_update() { - mrb_funcall(mrb, obj, "update", 0); +void script_update(double dt) { + mrb_funcall(mrb, obj, "update", 1, mrb_float_value(mrb, dt)); mrb_print_error(mrb); } @@ -50,6 +50,16 @@ void script_editor() { } void script_call(const char *f) { - mrb_funcall(mrb, obj, f, 0); + script_call_sym(mrb_intern_cstr(mrb, f)); +} + +void script_call_sym(mrb_sym sym) +{ + if (mrb_respond_to(mrb, obj, sym)) mrb_funcall_argv(mrb, obj, sym, 0, NULL); + mrb_print_error(mrb); +} + +int script_has_sym(mrb_sym sym) { + return mrb_respond_to(mrb, obj, sym); } \ No newline at end of file diff --git a/source/engine/script.h b/source/engine/script.h index 7bf358e..cfcbcd7 100755 --- a/source/engine/script.h +++ b/source/engine/script.h @@ -1,12 +1,16 @@ #ifndef SCRIPT_H #define SCRIPT_H +#include "mruby.h" + void script_init(); void script_run(const char *script); void script_dofile(const char *file); -void script_update(); +void script_update(double dt); void script_draw(); void script_editor(); void script_call(const char *f); +void script_call_sym(mrb_sym sym); +int script_has_sym(mrb_sym sym); #endif diff --git a/source/engine/window.c b/source/engine/window.c index 8727fcc..132b7be 100755 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -54,6 +54,34 @@ void window_close_callback(GLFWwindow *w) quit = 1; } +void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods) +{ + char keystr[50] = {'\0'}; + strcat(keystr, "input_"); + strcat(keystr, glfwGetKeyName(key, 0)); + switch (action) { + case GLFW_PRESS: + strcat(keystr, "_down"); + break; + + case GLFW_RELEASE: + strcat(keystr, "_up"); + break; + + case GLFW_REPEAT: + strcat(keystr, "_rep"); + break; + } + script_call(keystr); + +/* Example callback function + if (key == GLFW_KEY_F && action == GLFW_PRESS) { + printf("Pressed F.\n"); + if (w == customerWindow->window) window_togglefullscreen(customerWindow); + } +*/ +} + struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32_t flags) { struct mSDLWindow *w; @@ -91,6 +119,12 @@ struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32 glfwSetWindowSizeCallback(w->window, window_size_callback); glfwSetFramebufferSizeCallback(w->window, window_framebuffer_size_cb); glfwSetWindowFocusCallback(w->window, window_focus_callback); + glfwSetKeyCallback(w->window, win_key_callback); + + nuke_init(w); + + w->nuke_cb = 0; + w->gui_cb = 0; return w; } @@ -106,7 +140,9 @@ void window_destroy(struct mSDLWindow *w) vec_delete(&windows, w->id); } - +struct mSDLWindow *window_i(int index) { + return vec_get(&windows, index); +} void window_handle_event(struct mSDLWindow *w) { @@ -244,20 +280,18 @@ int window_hasfocus(struct mSDLWindow *w) return glfwGetWindowAttrib(w->window, GLFW_FOCUSED); } -double frame_time() -{ - return glfwGetTime(); +void window_render(struct mSDLWindow *w) { + window_makecurrent(w); + openglRender(w); + + if (script_has_sym(w->nuke_cb)) { + nuke_start(); + script_call_sym(w->nuke_cb); + nuke_end(); + } + window_swap(w); } -int elapsed_time() -{ - static double last_time; - double elapsed; - elapsed = frame_time() - last_time; - last_time = frame_time(); - //printf("Elapsed: %d.\n", elapsed); - return elapsed * 1000; -} - - - +void window_renderall() { + vec_walk(&windows, window_render); +} \ No newline at end of file diff --git a/source/engine/window.h b/source/engine/window.h index c2d88a3..fbc9cc8 100755 --- a/source/engine/window.h +++ b/source/engine/window.h @@ -5,6 +5,8 @@ #include #include +#include "mruby.h" + struct mSDLWindow { GLFWwindow *window; int id; @@ -19,6 +21,8 @@ struct mSDLWindow { bool iconified; bool shown; float projection[16]; + mrb_sym nuke_cb; + mrb_sym gui_cb; }; struct Texture; @@ -37,8 +41,9 @@ void window_togglefullscreen(struct mSDLWindow *w); void window_swap(struct mSDLWindow *w); void window_seticon(struct mSDLWindow *w, struct Texture *icon); int window_hasfocus(struct mSDLWindow *w); +struct mSDLWindow *window_i(int index); -double frame_time(); -int elapsed_time(); +void window_render(struct mSDLWindow *w); +void window_renderall(); #endif diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 7192ccb..b8013d2 100755 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -3,67 +3,47 @@ #include "window.h" #include "engine.h" #include "input.h" -#include "2dphysics.h" #include "openglrender.h" -#include "gameobject.h" int physOn = 0; -unsigned int frameCount = 0; -double physMS = sFPS144; -double physlag = 0; -double renderMS = sFPS144; double renderlag = 0; -struct mCamera camera = {0}; - -int main(int argc, char **args) -{ - camera.speed = 500; +static double renderMS = 0.033; +int main(int argc, char **args) { engine_init(); - struct mSDLWindow *window = MakeSDLWindow("Untitled Game", 1920, 1080, 0); + window_set_icon("icon.png"); + + script_dofile("game.rb"); openglInit(); - editor_init(window); + renderMS = 0.033; - int quit = 0; + double lastTick; + double frameTick; - //While application is running while (!quit) { - deltaT = elapsed_time(); + double elapsed; + double lastTick; + frameTick = glfwGetTime(); + elapsed = frameTick - lastTick; + lastTick = frameTick; - physlag += deltaT; - renderlag += deltaT; + renderlag += elapsed; - input_poll(); + input_poll(); - if (physlag >= physMS) { - phys2d_update(physMS); + window_all_handle_events(); - physlag -= physMS; - } + script_update(elapsed); - if (renderlag >= renderMS) { - if (physOn) { - update_gameobjects(); - } + if (renderlag >= renderMS) { + renderlag -= renderMS; + window_renderall(); + } - camera_2d_update(&camera, renderMS); - - openglRender(window, &camera); - - editor_render(); - - window_swap(window); - - renderlag -= renderMS; - } } - - engine_stop(); - - return 0; }