Skip to content

Commit

Permalink
Save screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Sep 2, 2024
1 parent de30f27 commit 122c4bc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
3 changes: 2 additions & 1 deletion nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@kmamal/sdl": "~0.9.5",
"commander": "~12.0.0",
"fflate": "~0.8.2"
"fflate": "~0.8.2",
"pngjs": "~7.0.0"
}
}
59 changes: 51 additions & 8 deletions nodejs/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const fs = __non_webpack_require__('fs').promises
const fs = __non_webpack_require__('fs')
const fsPromises = fs.promises

import {unzip, AsyncUnzipOptions, Unzipped} from 'fflate'
import {PNG} from 'pngjs'

import {Nes, NesEvent} from '../../src/nes/nes'
import {Cartridge} from '../../src/nes/cartridge'
import {IDeltaModulationChannel, INoiseChannel, IPulseChannel, PadValue, WaveType} from '../../src/nes/apu'
Expand All @@ -25,6 +28,7 @@ const ARROW_UP = 82
const RETURN = 40
const ESCAPE = 41
const SPACE = 44
const F12 = 69

const kScanCode2PadValue: Record<number, number> = {
[ARROW_RIGHT]: PadValue.R,
Expand Down Expand Up @@ -59,6 +63,8 @@ class MyApp {
private additionalPad = 0
private timer: NodeJS.Timer | undefined
private controller: any
private takeScreenshot = false
private screenshotIndex = 0

private audioManager = new AudioManager()

Expand Down Expand Up @@ -102,12 +108,21 @@ class MyApp {
clearInterval(this.timer)
})
this.win.on('keyDown', (key) => {
if (key.scancode === ESCAPE)
switch (key.scancode) {
case ESCAPE:
process.exit(0)

const v = kScanCode2PadValue[key.scancode]
if (v)
this.pad |= v
break
case F12:
this.takeScreenshot = true
break
default:
{
const v = kScanCode2PadValue[key.scancode]
if (v)
this.pad |= v
}
break
}
})
this.win.on('keyUp', (key) => {
const v = kScanCode2PadValue[key.scancode]
Expand Down Expand Up @@ -218,6 +233,34 @@ class MyApp {
}
}
this.win.render(width, height, width * 4, 'rgba32', this.buffer)

if (this.takeScreenshot) {
this.takeScreenshot = false
const fn = `ss${this.screenshotIndex++}.png`
this.saveScreenshot(fn, width, height, u8buf)
.then(() => {
console.log(`Screenshot saved as ${fn}`)
})
}
}

private async saveScreenshot(fn: string, width: number, height: number, pixels: Uint8Array): Promise<void> {
const png = new PNG({
width,
height,
})

for (let i = 0; i < height; ++i) {
for (let j = 0; j < width; ++j) {
const idx = (png.width * i + j) << 2
png.data[idx + 0] = pixels[idx + 0]
png.data[idx + 1] = pixels[idx + 1]
png.data[idx + 2] = pixels[idx + 2]
png.data[idx + 3] = pixels[idx + 3]
}
}

await png.pack().pipe(fs.createWriteStream(fn))
}

private setupAudioManager(): void {
Expand Down Expand Up @@ -293,11 +336,11 @@ class MyApp {
async function loadNesRomData(fileName: string): Promise<Uint8Array> {
switch (Util.getExt(fileName).toLowerCase()) {
case 'nes':
return await fs.readFile(fileName)
return await fsPromises.readFile(fileName)

case 'zip':
{
const buffer = await fs.readFile(fileName)
const buffer = await fsPromises.readFile(fileName)
const options = {
filter(file: any) {
return Util.getExt(file.name).toLowerCase() === 'nes'
Expand Down

0 comments on commit 122c4bc

Please sign in to comment.