fantasy-console/index.ts

341 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-04-28 20:01:48 -07:00
import {
mainloop,
2023-04-29 15:16:35 -07:00
frame,
setPixelColor,
clearScreen,
setPixelsInRect,
} from "./window.ts";
// deno-fmt-ignore
const sprites = [
[
2, 2, 2, 2, 2, 2, 2, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2,
],
[
2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 2, 2, 2, 2, 2, 2, 2,
],
[
2, 2, 2, 2, 2, 2, 2, 2,
2, 4, 4, 4, 4, 5, 5, 2,
2, 4, 4, 4, 5, 5, 5, 2,
2, 4, 4, 5, 5, 5, 6, 2,
2, 4, 5, 5, 5, 6, 6, 2,
2, 5, 5, 5, 6, 6, 6, 2,
2, 5, 5, 6, 6, 6, 6, 2,
2, 2, 2, 2, 2, 2, 2, 2,
],
2023-04-29 14:34:26 -07:00
]
2023-04-29 20:07:06 -07:00
// 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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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,
],
}
2023-04-29 15:16:35 -07:00
const drawSprite = (x: number, y: number, spr: number) => {
setPixelsInRect(x, y, 8, sprites[spr]);
2023-04-29 14:34:26 -07:00
}
2023-04-29 20:07:06 -07:00
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 = () => {
2023-04-29 15:16:35 -07:00
clearScreen();
2023-04-29 20:07:06 -07:00
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);
// }
2023-04-29 15:16:35 -07:00
setPixelColor(0, 0, 3);
setPixelColor(0, 127, 4);
setPixelColor(127, 0, 5);
setPixelColor(127, 127, 6);
2023-04-29 20:07:06 -07:00
}
draw();
await mainloop((t) => {
// console.log(t/1000);
draw();
2023-04-29 14:34:26 -07:00
frame();
});