Text rendering via script

This commit is contained in:
John Alanbrook 2022-12-21 01:34:22 +00:00
parent df907aec2b
commit 2bcee44677
9 changed files with 109 additions and 44 deletions

View file

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

View file

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

View file

@ -127,6 +127,7 @@ void openglRender(struct window *window)
////// TEXT && GUI
//script_call_sym(window->gui_cb);
call_gui();
///// Sprites
glDepthFunc(GL_LESS);

View file

@ -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);
}
@ -107,3 +112,21 @@ 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]);
}

View file

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

View file

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

View file

@ -166,6 +166,7 @@ int main(int argc, char **args) {
if (physlag >= physMS) {
physlag -= physMS;
phys2d_update(physMS);
call_physics();
}

View file

@ -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,11 +54,19 @@
(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)))

View file

@ -9,4 +9,7 @@ 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;
}