Ajout de systeme de dessin de bitmap plus externalisation du buffer
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"@types/node": "^17.0.14",
|
||||
"node-gyp": "^8.4.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,26 +45,22 @@ function removeFromCache(filepath) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @returns { Promise<import('@jimp/core').Bitmap> } BGRA buffer
|
||||
*/
|
||||
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]) {
|
||||
async function readImgFile(filepath) {
|
||||
const img = await jimp.read(filepath)
|
||||
bitmap = img.bitmap
|
||||
const bitmap = img.bitmap
|
||||
bitmap.data = rgbaTObgra(bitmap.data)
|
||||
imageCache[filepath] = bitmap
|
||||
} else {
|
||||
bitmap = imageCache[filepath]
|
||||
return bitmap
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param { number } x
|
||||
* @param { number } y
|
||||
* @param { import('@jimp/core').Bitmap } bitmap
|
||||
*/
|
||||
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 }
|
||||
module.exports = { flip, clear, getScreenDimensions, drawImgFile, readImgFile, drawBitmap, parseHexColor, setPixel, removeFromCache }
|
||||
Reference in New Issue
Block a user