adding text

This commit is contained in:
dylan 2023-04-29 20:07:06 -07:00
parent 078b7806dd
commit 1781ae3bba
2 changed files with 292 additions and 10 deletions

294
index.ts
View File

@ -40,21 +40,301 @@ const sprites = [
],
]
// deno-fmt-ignore
const font: {[key: string]: Array<number>} = {
"a": [
0, 0, 0, 0,
0, 1, 1, 1,
0, 1, 0, 1,
0, 1, 1, 1,
0, 1, 0, 1,
0, 1, 0, 1,
],
"b": [
0, 0, 0, 0,
0, 1, 1, 1,
0, 1, 0, 1,
0, 1, 1, 0,
0, 1, 0, 1,
0, 1, 1, 1,
],
"c": [
0, 0, 0, 0,
0, 0, 1, 1,
0, 1, 0, 0,
0, 1, 0, 0,
0, 1, 0, 0,
0, 0, 1, 1,
],
"d": [
0, 0, 0, 0,
0, 1, 1, 0,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 1, 0,
],
"e": [
0, 0, 0, 0,
0, 1, 1, 1,
0, 1, 0, 0,
0, 1, 1, 0,
0, 1, 0, 0,
0, 1, 1, 1,
],
"f": [
0, 0, 0, 0,
0, 1, 1, 1,
0, 1, 0, 0,
0, 1, 1, 0,
0, 1, 0, 0,
0, 1, 0, 0,
],
"g": [
0, 0, 0, 0,
0, 0, 1, 1,
0, 1, 0, 0,
0, 1, 0, 1,
0, 1, 0, 1,
0, 0, 1, 1,
],
"h": [
0, 0, 0, 0,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 1, 1,
0, 1, 0, 1,
0, 1, 0, 1,
],
"i": [
0, 0, 0, 0,
0, 1, 1, 1,
0, 0, 1, 0,
0, 0, 1, 0,
0, 0, 1, 0,
0, 1, 1, 1,
],
"j": [
0, 0, 0, 0,
0, 1, 1, 1,
0, 0, 1, 0,
0, 0, 1, 0,
0, 0, 1, 0,
0, 1, 1, 0,
],
"k": [
0, 0, 0, 0,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 1, 0,
0, 1, 0, 1,
0, 1, 0, 1,
],
"l": [
0, 0, 0,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 0, 0,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 1, 1,
0, 0, 1, 0,
0, 1, 0, 0,
],
"z": [
0, 0, 0, 0,
0, 1, 1, 1,
0, 0, 0, 1,
0, 0, 1, 0,
0, 1, 0, 0,
0, 1, 1, 1,
],
",": [
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 1, 0,
0, 1, 0, 0,
],
".": [
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 1, 0,
],
" ": [
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
],
"<": [
0, 0, 0, 0,
0, 0, 0, 1,
0, 0, 1, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
],
">": [
0, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
0, 0, 1, 0,
0, 1, 0, 0,
],
"=": [
0, 0, 0, 0,
0, 0, 0, 0,
0, 1, 1, 1,
0, 0, 0, 0,
0, 1, 1, 1,
0, 0, 0, 0,
],
}
const drawSprite = (x: number, y: number, spr: number) => {
setPixelsInRect(x, y, 8, sprites[spr]);
}
await mainloop((t) => {
console.log(t/1000);
const drawChar = (x: number, y: number, char: string) => {
setPixelsInRect(x, y, 4, font[char]);
}
const drawText = (x: number, y: number, text: string) => {
[...text].forEach((char, i) => {
drawChar(x+4*i, y, char);
});
}
const draw = () => {
clearScreen();
for (let i = 0; i < 256; i++) {
drawSprite(Math.floor(Math.random()*120), Math.floor(Math.random()*120), 0);
drawSprite(Math.floor(Math.random()*120), Math.floor(Math.random()*120), 1);
drawSprite(Math.floor(Math.random()*120), Math.floor(Math.random()*120), 2);
}
drawText(5, 5, "=> hello, world <=");
drawText(5, 11, "the quick brown fox");
drawText(5, 17, "jumps over the lazy");
drawText(5, 23, "dog.");
// for (let i = 0; i < 256; i++) {
// drawSprite(Math.floor(Math.random()*120), Math.floor(Math.random()*120), 0);
// drawSprite(Math.floor(Math.random()*120), Math.floor(Math.random()*120), 1);
// drawSprite(Math.floor(Math.random()*120), Math.floor(Math.random()*120), 2);
// }
setPixelColor(0, 0, 3);
setPixelColor(0, 127, 4);
setPixelColor(127, 0, 5);
setPixelColor(127, 127, 6);
}
draw();
await mainloop((t) => {
// console.log(t/1000);
draw();
frame();
});

View File

@ -115,8 +115,8 @@ const px = (x: number, y: number) => {
const palette: Array<[number, number, number, number]> = [
[0, 0, 0, 0],
[0, 0, 0, 1],
[1, 1, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1],
[1, 1, 0, 1],
[0, 1, 0, 1],
@ -138,8 +138,10 @@ const allPixelColors = new Float32Array(
)
export const setPixelColor = (x: number, y: number, color: number) => {
const col = c(color);
allPixelColors.set(col, 4*6*(128*y+x));
if (color !== 0) {
const col = c(color);
allPixelColors.set(col, 4*6*(128*y+x));
}
}
export const setPixelsInRect = (x: number, y: number, w: number, pixels: Array<number>) => {