split stuff out

This commit is contained in:
dylan 2023-05-01 11:12:08 -07:00
parent 1781ae3bba
commit 5d742d5964
5 changed files with 345 additions and 331 deletions

59
builtins.ts Normal file
View File

@ -0,0 +1,59 @@
import {
setPixelsInRect,
clearScreen,
} from "./window.ts";
import { font } from "./font.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,
],
]
const drawSprite = (x: number, y: number, spr: number) => {
setPixelsInRect(x, y, 8, sprites[spr]);
}
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);
});
}
export default {
clearScreen,
drawSprite,
drawText,
}

259
font.ts Normal file
View File

@ -0,0 +1,259 @@
// deno-fmt-ignore
export const font: {[key: string]: Array<number>} = {
"a": [
0, 0, 0, 0,
0, 1, 1,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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,
],
}

19
game.ts Normal file
View File

@ -0,0 +1,19 @@
import faux from "./builtins.ts";
let y = 0;
export default {
init() {
},
update() {
y++;
if (y>127) {
y=-5;
}
},
draw() {
faux.clearScreen();
faux.drawText(0, y, "hello, world");
},
}

336
index.ts
View File

@ -1,340 +1,14 @@
import { import {
mainloop, mainloop,
frame, frame,
setPixelColor,
clearScreen,
setPixelsInRect,
} from "./window.ts"; } from "./window.ts";
import game from "./game.ts";
// deno-fmt-ignore game.init();
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,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, 0, 0,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 0, 1,
0, 1, 1, 1,
0, 1, 1, 1,
],
"x": [
0, 0, 0, 0,
0, 1, 0, 1,
0, 1, 0, 1,
0, 0, 1, 0,
0, 1, 0, 1,
0, 1, 0, 1,
],
"y": [
0, 0, 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]);
}
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();
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) => { await mainloop((t) => {
// console.log(t/1000); // TODO: use t
draw(); game.update();
game.draw();
frame(); frame();
}); });

View File

@ -138,6 +138,9 @@ const allPixelColors = new Float32Array(
) )
export const setPixelColor = (x: number, y: number, color: number) => { export const setPixelColor = (x: number, y: number, color: number) => {
if (x < 0 || y < 0 || x > 127 || y > 127) {
return;
}
if (color !== 0) { if (color !== 0) {
const col = c(color); const col = c(color);
allPixelColors.set(col, 4*6*(128*y+x)); allPixelColors.set(col, 4*6*(128*y+x));