font setting

This commit is contained in:
John Alanbrook 2024-03-02 08:59:50 +00:00
parent 0054c7ab2e
commit f948dac73f
8 changed files with 44 additions and 9 deletions

View file

@ -143,7 +143,7 @@ global.mixin("scripts/tween.js");
global.mixin("scripts/render.js");
global.mixin("scripts/physics.js");
global.mixin("scripts/input.js");
global.mixin("scripts/sound.js");
global.mixin("scripts/ai.js");
global.mixin("scripts/geometry.js");
@ -300,8 +300,6 @@ global.mixin("scripts/components.js");
var Game = {
engine_start(fn) {
cmd(257, fn);
Sound.bus.master = cmd(180);
Sound.master = Sound.bus.master;
},
native: render.device.pc,

View file

@ -109,7 +109,7 @@ var Mum = {
selectable: false,
selected: false,
font_size: 1,
text_align: "left",
text_align: "left", /* left, center, right */
scale: 1,
angle: 0,
anchor: [0,1],
@ -157,6 +157,7 @@ var Mum = {
Mum.text = Mum.extend({
draw(cursor, cnt) {
cursor ??= [0,0];
cnt ??= Mum;
if (this.hide) return;
if (this.selectable) GUI.controls.check_bb(this);
@ -175,6 +176,7 @@ Mum.text = Mum.extend({
this.height = this.wh.y;
var aa = [0,1].sub(params.anchor);
var pos = cursor.add(params.wh.scale(aa)).add(params.offset);
cmd(263, params.font);
ui_text(params.str, pos, params.font_size, params.color, this.width, params.caret);
},
@ -208,6 +210,7 @@ Mum.window = Mum.extend({
this.bb = bbox.fromcwh([0,0], this.wh);
},
draw(cursor, cnt) {
cursor ??= [0,0];
cnt ??= Mum;
var p = cursor.sub(this.wh.scale(this.anchor)).add(this.padding);
GUI.window(p,this.wh, this.color);
@ -256,6 +259,7 @@ Mum.image = Mum.extend({
Mum.column = Mum.extend({
draw(cursor, cnt) {
cursor ??= [0,0];
cnt ??= Mum;
if (this.hide) return;
cursor = cursor.add(this.offset);

View file

@ -10,7 +10,7 @@ audio.sound = {
}
var src = audio.dsp.source(file);
bus ??= audio.sound.bus.master;
// src.plugin(bus);
src.plugin(bus);
return src;
},

View file

@ -259,6 +259,7 @@ Cmdline.register_order("play", function() {
if (project.title) Window.title(project.title);
Game.engine_start(function() {
global.mixin("scripts/sound.js");
global.mixin("game.js");
if (project.icon) Window.icon(project.icon);
});

View file

@ -64,6 +64,8 @@ void mYughLog(int category, int priority, int line, const char *file, const char
snprintf(buffer, len, logfmt, file, line, logstr[priority], catstr[category], msg);
fprintf(stderr, "%s", buffer);
if (priority >= 2)
js_stacktrace();
fflush(stderr);
free(msg);

View file

@ -17,11 +17,17 @@
#include "stb_image_write.h"
#include "stb_rect_pack.h"
#include "stb_truetype.h"
#include "stb_ds.h"
#include "HandmadeMath.h"
struct sFont *font;
static struct {
char *key;
struct sFont *value;
} *fonthash = NULL;
#define max_chars 10000
static sg_shader fontshader;
@ -78,13 +84,33 @@ void font_init() {
.label = "text buffer"
});
font = MakeFont("fonts/LessPerfectDOSVGA.ttf", 16);
// font = MakeFont("fonts/c64.ttf", 8);
// font = MakeFont("fonts/teenytinypixels.ttf", 16);
font_set("fonts/LessPerfectDOSVGA.ttf");
bind_text.fs.images[0] = font->texID;
bind_text.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
}
void font_set(const char *path)
{
if (shlen(fonthash) == 0) sh_new_arena(fonthash);
int index = shgeti(fonthash, path);
if (index != -1) {
if (font == fonthash[index].value) return;
font = fonthash[index].value;
bind_text.fs.images[0] = font->texID;
return;
}
struct sFont *newfont = MakeFont(path, 16);
if (!newfont) {
YughError("Could not make font from %s.", path);
return;
}
font = newfont;
shput(fonthash, path, newfont);
bind_text.fs.images[0] = font->texID;
}
struct sFont *MakeSDFFont(const char *fontfile, int height)
{
YughInfo("Making sdf font %s.", fontfile);
@ -329,7 +355,6 @@ void check_caret(int caret, int l, HMM_Vec2 pos, float scale, struct rgba color)
draw_underline_cursor(pos,scale,color);
}
/* pos given in screen coordinates */
int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking) {
int len = strlen(text);

View file

@ -30,6 +30,7 @@ struct sFont {
void font_init();
struct sFont *MakeFont(const char *fontfile, int height);
void font_set(const char *path);
void sdrawCharacter(struct Character c, HMM_Vec2 cursor, float scale, struct rgba color);
void text_settype(struct sFont *font);
struct boundingbox text_bb(const char *text, float scale, float lw, float tracking);

View file

@ -1452,6 +1452,10 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
str = js2str(argv[1]);
save_qoa(str);
break;
case 263:
str = js2str(argv[1]);
font_set(str);
break;
}
if (str) JS_FreeCString(js, str);