Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created April 7, 2023 11:35
Show Gist options
  • Save roman01la/cbc66954e5ab0ff5066471d66f83c114 to your computer and use it in GitHub Desktop.
Save roman01la/cbc66954e5ab0ff5066471d66f83c114 to your computer and use it in GitHub Desktop.
raylib
const r = require("raylib/drm/index");
const net = require("net");
const path = require("path");
const { pexec } = require("./utils");
const renderer = require("./renderer");
function getScreenSize() {
return pexec(`fbset -s | grep "geometry" | awk '{print $2" "$3}'`).then((r) =>
r.split(" ").map((s) => parseInt(s, 10))
);
}
function createSocketConnection(port) {
return new Promise((resolve, reject) => {
const socket = net.createConnection({
host: "localhost",
port: port,
});
socket.on("connect", () => {
resolve(socket);
});
});
}
function formatSeconds(secs) {
const s = Math.floor(secs);
const hh = Math.floor(s / 3600);
const mm = Math.floor((s % 3600) / 60);
const ss = Math.floor(s % 60);
return `${hh < 10 ? `0${hh}` : hh}:${mm < 10 ? `0${mm}` : mm}:${
ss < 10 ? `0${ss}` : ss
}`;
}
function readCommand(socket) {
const str = socket.read();
return str ? str.split(" ") : [];
}
const state = {
passedTimeSecs: 0,
timerDoneTime: null,
};
async function main() {
const [screenWidth, screenHeight] = await getScreenSize();
const socket = await createSocketConnection(9001);
r.InitWindow(screenWidth, screenHeight, "status screen");
const fontInterBold = r.LoadFontEx(
path.join(__dirname, "../../Inter-3.19/Inter-Bold.ttf"),
36,
0,
0
);
const textPosition = r.Vector2(16, 16);
renderer.renderLoop(() => {
r.BeginDrawing();
r.ClearBackground(r.BLACK);
const [cmd, value] = readCommand(socket);
switch (cmd) {
case "0":
state.timerDoneTime = parseInt(value, 10);
break;
default:
break;
}
const timerDone = state.passedTimeSecs === state.timerDoneTime;
if (state.timerDoneTime !== null && !timerDone) {
state.passedTimeSecs += renderer.getFrameTime();
}
r.DrawTextEx(
fontInterBold,
`${formatSeconds(state.passedTimeSecs)} / ${formatSeconds(
state.timerDoneTime || 0
)}`,
textPosition,
36,
0,
timerDone ? r.GREEN : r.WHITE
);
}, 8);
}
main().catch(console.error);
const r = require("raylib/drm/index");
let CORETime = {
current: 0,
update: 0,
previous: 0,
draw: 0,
frame: 0,
frameCounter: 0,
};
function begindDrawing() {
CORETime.current = r.GetTime();
CORETime.update = CORETime.current - CORETime.previous;
CORETime.previous = CORETime.current;
r.BeginDrawing();
}
function endDrawing() {
r.rlDrawRenderBatchActive();
r.SwapScreenBuffer();
CORETime.current = r.GetTime();
CORETime.draw = CORETime.current - CORETime.previous;
CORETime.previous = CORETime.current;
CORETime.frame = CORETime.update + CORETime.draw;
}
function renderLoop(render, fps) {
begindDrawing();
render();
endDrawing();
setTimeout(() => {
CORETime.current = r.GetTime();
const waitTime = CORETime.current - CORETime.previous;
CORETime.previous = CORETime.current;
CORETime.frame += waitTime;
r.PollInputEvents();
CORETime.frameCounter++;
if (r.WindowShouldClose()) {
r.CloseWindow();
} else {
renderLoop(render, fps);
}
}, 1000 / fps);
}
function getFrameTime() {
return CORETime.frame;
}
module.exports = { renderLoop, getFrameTime };
const { exec } = require("child_process");
// promisified child_process.exec
function pexec(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
return reject(error);
}
if (stderr) {
return reject(stderr);
}
resolve(stdout);
});
});
}
module.exports = { pexec };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment