From a3547ac2448ce06fa4e7ef752cb6683f8bb11fef Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 2 Feb 2022 16:31:40 +0100 Subject: [PATCH] Ajout de systeme de dessin de bitmap plus externalisation du buffer --- package.json | 1 + rootfs overrides/chibrax/graphics.js | 51 ++++++++++++++++++---------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 66450fc..2ed4678 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "@types/node": "^17.0.14", "node-gyp": "^8.4.1" } } diff --git a/rootfs overrides/chibrax/graphics.js b/rootfs overrides/chibrax/graphics.js index f4f434f..2b15cbf 100644 --- a/rootfs overrides/chibrax/graphics.js +++ b/rootfs overrides/chibrax/graphics.js @@ -44,27 +44,23 @@ function removeFromCache(filepath) { delete imageCache[filepath] } +/** + * @param {string} filepath + * @returns { Promise } BGRA buffer + */ +async function readImgFile(filepath) { + const img = await jimp.read(filepath) + const bitmap = img.bitmap + bitmap.data = rgbaTObgra(bitmap.data) + return bitmap +} + /** * @param { number } x * @param { number } y - * @param { string } filepath - * //!\\ THE CACHE DOESN'T CLEAN ITSELF UP //!\\ - * once you finished displaying a image use removeFromCache to free the associated memory + * @param { import('@jimp/core').Bitmap } bitmap */ -async function drawImgFile(x, y, filepath) { - let bitmap - //not caching would imply reading the image each time we want to write it to the screen even for small movement - if(!imageCache[filepath]) { - const img = await jimp.read(filepath) - bitmap = img.bitmap - bitmap.data = rgbaTObgra(bitmap.data) - imageCache[filepath] = bitmap - } else { - bitmap = imageCache[filepath] - } - - - +function drawBitmap(x, y, bitmap) { for(let dy = 0; dy < bitmap.height; dy++) { let bitmapLineEnd = bitmap.width * screen.byteDepth * (dy + 1) let bitmapLineStart = bitmap.width * screen.byteDepth * dy @@ -102,6 +98,25 @@ async function drawImgFile(x, y, filepath) { } } +/** + * @param { number } x + * @param { number } y + * @param { string } filepath + * @description this function work fine but will use caching. to clear the cache you can use removeFromCache(filepath) or you can keep the cache on the + * client side and use readImgFile and drawBitmap methods instead + */ +async function drawImgFile(x, y, filepath) { + //not caching would imply reading the image each time we want to write it to the screen even for small movement + let bitmap; + if(!imageCache[filepath]) { + bitmap = await readImgFile(filepath) + imageCache[filepath] = bitmap + } else { + bitmap = imageCache[filepath] + } + drawBitmap(x, y, bitmap) +} + /** * @param { Buffer } buff */ @@ -147,4 +162,4 @@ function setPixel(x, y, colorCode) { buffer.set(colorCode, (x + y * screen.width) * screen.byteDepth) } -module.exports = { flip, clear, getScreenDimensions, drawImgFile, parseHexColor, setPixel } \ No newline at end of file +module.exports = { flip, clear, getScreenDimensions, drawImgFile, readImgFile, drawBitmap, parseHexColor, setPixel, removeFromCache } \ No newline at end of file