macintosh.js/src/renderer/screen.js

53 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-07-14 19:45:04 +00:00
const { videoModeBufferView } = require('./video');
const { audioContext } = require('./audio');
const SCREEN_WIDTH = 800;
const SCREEN_HEIGHT = 600;
const SCREEN_BUFFER_SIZE = SCREEN_WIDTH * SCREEN_HEIGHT * 4; // 32bpp;
const screenBuffer = new SharedArrayBuffer(SCREEN_BUFFER_SIZE);
const screenBufferView = new Uint8Array(screenBuffer);
canvas.width = SCREEN_WIDTH;
canvas.height = SCREEN_HEIGHT;
const canvasCtx = canvas.getContext('2d');
const imageData = canvasCtx.createImageData(SCREEN_WIDTH, SCREEN_HEIGHT);
function drawScreen() {
const pixelsRGBA = imageData.data;
const numPixels = SCREEN_WIDTH * SCREEN_HEIGHT;
const expandedFromPalettedMode = videoModeBufferView[3];
const start = audioContext.currentTime;
if (expandedFromPalettedMode) {
for (var i = 0; i < numPixels; i++) {
// palette
pixelsRGBA[i * 4 + 0] = screenBufferView[i * 4 + 0];
pixelsRGBA[i * 4 + 1] = screenBufferView[i * 4 + 1];
pixelsRGBA[i * 4 + 2] = screenBufferView[i * 4 + 2];
pixelsRGBA[i * 4 + 3] = 255; // full opacity
}
} else {
for (var i = 0; i < numPixels; i++) {
// ARGB
pixelsRGBA[i * 4 + 0] = screenBufferView[i * 4 + 1];
pixelsRGBA[i * 4 + 1] = screenBufferView[i * 4 + 2];
pixelsRGBA[i * 4 + 2] = screenBufferView[i * 4 + 3];
pixelsRGBA[i * 4 + 3] = 255; // full opacity
}
}
canvasCtx.putImageData(imageData, 0, 0);
}
module.exports = {
screenBuffer,
screenBufferView,
SCREEN_BUFFER_SIZE,
drawScreen,
SCREEN_WIDTH,
SCREEN_HEIGHT
}