Names on players
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { AssetManager } from "../assets.ts";
|
||||
import { flush_batch, get_current_texture, push_quad, set_current_texture } from "./core.ts";
|
||||
import { type Plane3d, push_plane_quad } from "./shapes.ts";
|
||||
import { FntChar, FntFont, Texture } from "./types.ts";
|
||||
|
||||
let font_texture: Texture;
|
||||
@@ -102,6 +103,52 @@ export function draw_text(str: string, x: number, y: number, scale: number = 1,
|
||||
}
|
||||
}
|
||||
|
||||
// text on a plane in the world (see Plane3d), x and y in the plane's units like draw_text's are in pixels. scale is
|
||||
// how big a font pixel is in them
|
||||
export function draw_text_3d(
|
||||
plane: Plane3d,
|
||||
str: string,
|
||||
x: number,
|
||||
y: number,
|
||||
scale: number,
|
||||
color: readonly number[] = [1, 1, 1, 1],
|
||||
) {
|
||||
if (!font_texture) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (get_current_texture() !== font_texture.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(font_texture.tex);
|
||||
}
|
||||
|
||||
for (const ch of str) {
|
||||
const glyph = font_fnt.chars[ch.charCodeAt(0)];
|
||||
if (!glyph) {
|
||||
continue;
|
||||
}
|
||||
|
||||
push_plane_quad(
|
||||
plane,
|
||||
x + glyph.xoffset * scale,
|
||||
y + glyph.yoffset * scale,
|
||||
glyph.width * scale,
|
||||
glyph.height * scale,
|
||||
glyph.x / font_texture.width,
|
||||
glyph.y / font_texture.height,
|
||||
(glyph.x + glyph.width) / font_texture.width,
|
||||
(glyph.y + glyph.height) / font_texture.height,
|
||||
color,
|
||||
);
|
||||
|
||||
x += glyph.xadvance * scale;
|
||||
}
|
||||
}
|
||||
|
||||
export function font_line_height() {
|
||||
return font_fnt?.line_height ?? 0;
|
||||
}
|
||||
|
||||
export function measure_text(str: string, scale: number = 1) {
|
||||
let x = 0;
|
||||
for (const ch of str) {
|
||||
|
||||
Reference in New Issue
Block a user