Ajout de systeme de dessin de bitmap plus externalisation du buffer

This commit is contained in:
Marc
2022-02-02 16:31:40 +01:00
parent a6dbee234c
commit a3547ac244
2 changed files with 34 additions and 18 deletions
+1
View File
@@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"@types/node": "^17.0.14",
"node-gyp": "^8.4.1" "node-gyp": "^8.4.1"
} }
} }
+33 -18
View File
@@ -44,27 +44,23 @@ function removeFromCache(filepath) {
delete imageCache[filepath] delete imageCache[filepath]
} }
/**
* @param {string} filepath
* @returns { Promise<import('@jimp/core').Bitmap> } 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 } x
* @param { number } y * @param { number } y
* @param { string } filepath * @param { import('@jimp/core').Bitmap } bitmap
* //!\\ THE CACHE DOESN'T CLEAN ITSELF UP //!\\
* once you finished displaying a image use removeFromCache to free the associated memory
*/ */
async function drawImgFile(x, y, filepath) { function drawBitmap(x, y, bitmap) {
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]
}
for(let dy = 0; dy < bitmap.height; dy++) { for(let dy = 0; dy < bitmap.height; dy++) {
let bitmapLineEnd = bitmap.width * screen.byteDepth * (dy + 1) let bitmapLineEnd = bitmap.width * screen.byteDepth * (dy + 1)
let bitmapLineStart = bitmap.width * screen.byteDepth * dy 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 * @param { Buffer } buff
*/ */
@@ -147,4 +162,4 @@ function setPixel(x, y, colorCode) {
buffer.set(colorCode, (x + y * screen.width) * screen.byteDepth) buffer.set(colorCode, (x + y * screen.width) * screen.byteDepth)
} }
module.exports = { flip, clear, getScreenDimensions, drawImgFile, parseHexColor, setPixel } module.exports = { flip, clear, getScreenDimensions, drawImgFile, readImgFile, drawBitmap, parseHexColor, setPixel, removeFromCache }