From 2bcee4467759a0927b9822fce70563a9cc153cd8 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 21 Dec 2022 01:34:22 +0000 Subject: [PATCH] Text rendering via script --- source/engine/font.c | 27 ++++++++---------------- source/engine/mrbffi.c | 38 +++++++++++++++++++++++++++++++--- source/engine/openglrender.c | 1 + source/engine/script.c | 25 +++++++++++++++++++++- source/engine/script.h | 6 ++++++ source/engine/window.c | 10 ++------- source/engine/yugine.c | 1 + source/scripts/engine.scm | 40 ++++++++++++++++++++++++------------ source/shaders/textfrag.glsl | 5 ++++- 9 files changed, 109 insertions(+), 44 deletions(-) diff --git a/source/engine/font.c b/source/engine/font.c index a731998..e3e16c9 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -94,8 +94,8 @@ struct sFont *MakeFont(const char *fontfile, int height) glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); newfont->Characters[c].TextureID = ftexture; newfont->Characters[c].Advance = advance * scale; @@ -123,6 +123,9 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct xpos + w, ypos + h, 1.f, 1.f }; + glBindTexture(GL_TEXTURE_2D, c.TextureID); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); ////// Outline calculation // float outlineWidth = 1.1; @@ -143,7 +146,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct /////////// Shadow calculation - +/* float shadowOffset = 6.f; float sxpos = cursor[0] + c.Bearing[0] * scale + (scale * shadowOffset); float sypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale - (scale * shadowOffset); @@ -156,7 +159,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct }; - glBindTexture(GL_TEXTURE_2D, c.TextureID); + //// Shadow pass @@ -165,12 +168,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(sverts), sverts); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - - //// Character pass - shader_setvec3(shader, "textColor", color); - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - +*/ } void text_settype(struct sFont *mfont) @@ -180,7 +178,7 @@ void text_settype(struct sFont *mfont) void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw) { - //shader_use(shader); + shader_use(shader); shader_setvec3(shader, "textColor", color); mfloat_t cursor[2] = { 0.f }; @@ -230,11 +228,4 @@ void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3 } } } - - -/* - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - glBindTexture(GL_TEXTURE_2D, 0); - */ } diff --git a/source/engine/mrbffi.c b/source/engine/mrbffi.c index 4c44caa..87fb483 100644 --- a/source/engine/mrbffi.c +++ b/source/engine/mrbffi.c @@ -52,6 +52,20 @@ s7_pointer s7_ui_text(s7_scheme *sc, s7_pointer args) { return s7_make_string(sc, str); } +s7_pointer s7_gui_text(s7_scheme *sc, s7_pointer args) { + const char *s = s7_string(s7_car(args)); + float pos[2]; + pos[0] = s7_real(s7_cadr(args)); + pos[1] = s7_real(s7_caddr(args)); + + float size = s7_real(s7_cadddr(args)); + const float white[3] = {1.f, 1.f, 1.f}; + + renderText(s, pos, size, white, 200); + + return s7_car(args); +} + s7_pointer s7_settings_cmd(s7_scheme *sc, s7_pointer args) { int cmd = s7_integer(s7_car(args)); double val = s7_real(s7_cadr(args)); @@ -79,7 +93,6 @@ s7_pointer s7_log(s7_scheme *sc, s7_pointer args) { const char *file = s7_string(s7_caddr(args)); int line = s7_integer(s7_cadddr(args)); mYughLog(1, lvl, line, file, msg); - //YughInfo(s7_string(s7_object_to_string(sc, s7_car(args), 0))); return args; } @@ -193,9 +206,27 @@ s7_pointer s7_gui_hook(s7_scheme *sc, s7_pointer args) { s7_pointer s7_register(s7_scheme *sc, s7_pointer args) { int hook = s7_integer(s7_car(args)); - register_update(s7_cadr(args)); + s7_pointer sym = s7_cadr(args); - return args; + /* 0 : update */ + /* 1 : gui */ + /* 2 : physics */ + + switch (hook) { + case 0: + register_update(sym); + break; + + case 1: + register_gui(sym); + break; + + case 2: + register_physics(sym); + break; + } + + return sym; } s7_pointer s7_set_pawn(s7_scheme *sc, s7_pointer args) { @@ -264,6 +295,7 @@ void ffi_load() { S7_FUNC(ui_nel, 1); S7_FUNC(ui_prop, 6); S7_FUNC(ui_text, 2); + S7_FUNC(gui_text, 4); S7_FUNC(settings_cmd, 2); S7_FUNC(win_cmd, 2); S7_FUNC(ui_rendertext, 3); diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index a39bf4d..4a128e3 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -127,6 +127,7 @@ void openglRender(struct window *window) ////// TEXT && GUI //script_call_sym(window->gui_cb); + call_gui(); ///// Sprites glDepthFunc(GL_LESS); diff --git a/source/engine/script.c b/source/engine/script.c index c204d82..bc7839c 100644 --- a/source/engine/script.c +++ b/source/engine/script.c @@ -9,7 +9,7 @@ s7_scheme *s7 = NULL; -s7_pointer *updates; + static void null_port(s7_scheme *sc, uint8_t c, s7_pointer port) { @@ -99,6 +99,11 @@ int script_has_sym(s7_pointer sym) { return 1; } + +s7_pointer *updates; +s7_pointer *guis; +s7_pointer *physics; + void register_update(s7_pointer sym) { arrput(updates, sym); } @@ -106,4 +111,22 @@ void register_update(s7_pointer sym) { void call_updates() { for (int i = 0; i < arrlen(updates); i++) script_call_sym(updates[i]); +} + +void register_gui(s7_pointer sym) { + arrput(guis, sym); +} + +void call_gui() { + for (int i = 0; i < arrlen(guis); i++) + script_call_sym(guis[i]); +} + +void register_physics(s7_pointer sym) { + arrput(physics, sym); +} + +void call_physics() { + for (int i = 0; i < arrlen(physics); i++) + script_call_sym(physics[i]); } \ No newline at end of file diff --git a/source/engine/script.h b/source/engine/script.h index 31bb5f9..ad54e4c 100644 --- a/source/engine/script.h +++ b/source/engine/script.h @@ -17,4 +17,10 @@ void script_eval_w_env(const char *s, s7_pointer env); void register_update(s7_pointer sym); void call_updates(); +void register_gui(s7_pointer sym); +void call_gui(); + +void register_physics(s7_pointer sym); +void call_physics(); + #endif diff --git a/source/engine/window.c b/source/engine/window.c index 58f1f16..97e9217 100644 --- a/source/engine/window.c +++ b/source/engine/window.c @@ -277,15 +277,9 @@ int window_hasfocus(struct window *w) void window_render(struct window *w) { window_makecurrent(w); openglRender(w); -/* - if (script_has_sym(w->nuke_cb)) { - nuke_start(); - script_call_sym(w->nuke_cb); - nk_end(ctx); - nuke_end(); - } else -*/if (w->nuke_gui != NULL) { + + if (w->nuke_gui != NULL) { nuke_start(); w->nuke_gui(); nuke_end(); diff --git a/source/engine/yugine.c b/source/engine/yugine.c index 68806f1..78998f2 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -166,6 +166,7 @@ int main(int argc, char **args) { if (physlag >= physMS) { physlag -= physMS; phys2d_update(physMS); + call_physics(); } diff --git a/source/scripts/engine.scm b/source/scripts/engine.scm index 570f93e..29f134c 100644 --- a/source/scripts/engine.scm +++ b/source/scripts/engine.scm @@ -14,20 +14,26 @@ `(funcinfo ,func 'source)) (define (objects->string . objs) - (apply string-append (map (lambda (obj) (object->string obj #t)) objs))) + (apply string-append (map (lambda (obj) (object->string obj #f)) objs))) -(define-macro (glog data lvl) - (let ((file (*function* (outlet (curlet)) 'file)) - (line (*function* (outlet (curlet)) 'line))) - `(log ,lvl ,data ,file ,line))) +(define-bacro (glog data lvl) + (let ((file (port-filename)) + (line (port-line-number))) + `(log ,lvl ,data + (if (equal? ,file "*stdin*") (*function* (outlet (curlet)) 'file) ,file) + (if (equal? ,file "*stdin*") (*function* (outlet (curlet)) 'line) ,line)))) -(define-macro (flog data lvl) - `(log ,lvl ,data (funcinfo (*function*) 'file) (funcinfo (*function*) 'line))) +(define-bacro (loginfo . data) + `(glog (objects->string ,@data) 0)) -(define (loginfo . data) (glog (objects->string data) 0)) -(define (logwarn . data) (glog (objects->string data) 1)) -(define (logerr . data) (glog (objects->string data) 2)) -(define (logcrit . data) (glog (objects->string data) 3)) +(define-bacro (logwarn . data) + `(glog (objects->string ,@data) 1)) + +(define-bacro (logerr . data) + `(glog (objects->string ,@data) 2)) + +(define-bacro (logcrit . data) + `(glog (objects->string ,@data) 3)) (define (set_fps fps) (settings_cmd 0 (/ 1 fps))) (define (set_update fps) (settings_cmd 1 (/ 1 fps))) @@ -48,12 +54,20 @@ (define (sound_stop sound) (sound_cmd sound 2)) (define (sound_restart sound) (sound_cmd sound 3)) -(define-macro (update . expr) +(define-macro (registertype type . expr) (let ((f (gensym))) `(begin (define (,f) (begin . ,expr)) - (register 0 ,f)))) + (register ,(case type + ((update) 0) + ((gui) 1)) ,f)))) +(define-macro (update . expr) + `(registertype update ,@expr)) + +(define-macro (gui . expr) + `(registertype gui ,@expr)) + (define-macro (while condition . body) (let ((loop (gensym))) `(let ,loop () diff --git a/source/shaders/textfrag.glsl b/source/shaders/textfrag.glsl index bbd0c17..075fb66 100644 --- a/source/shaders/textfrag.glsl +++ b/source/shaders/textfrag.glsl @@ -6,7 +6,10 @@ uniform sampler2D text; uniform vec3 textColor; void main() -{ +{ vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); color = vec4(textColor, 1.0) * sampled; + + if (color.a <= 0.1f) + discard; } \ No newline at end of file