BinBashBanana / webretro

RetroArch in your browser
https://binbashbanana.github.io/webretro/
MIT License
257 stars 367 forks source link

get emulation screenshot #1

Closed dimabubenok closed 3 years ago

dimabubenok commented 3 years ago

Hi! Is there any way to get the result of calling _Module._cmd_takescreenshot() method?

BinBashBanana commented 3 years ago

That seems like a good idea to implement in the future! Should I have the screenshot immediately downloaded? Or did you have something else in mind?

dimabubenok commented 3 years ago

I am trying to implement saving game state with screenshot display. When i try to read a file in the virtual file system along the path _/home/webuser/retroarch/userdata/screenshots/rom-210621-190311.png, an error occurs.

BinBashBanana commented 3 years ago

You may have to put it on a short setTimeout.

dimabubenok commented 3 years ago

I did save screenshot to file:

function takeScreenshot(){
    Module._cmd_take_screenshot();
    var screenshot_path = "/home/web_user/retroarch/userdata/screenshots/";
    var screenshot_name = makeScreenshotFilename();

    window.setTimeout(function() {
        if(FS.analyzePath(screenshot_path+screenshot_name).exists) {
            var file = FS.readFile(screenshot_path+screenshot_name);
            downloadFile(file,screenshot_name);
        } else {
            console.log('screenshot not found')
        }
    }, 500);
}

function makeScreenshotFilename(){
    var now = new Date();
    var y = now.getFullYear().toString().slice(-2),
        m = ("0" + (now.getMonth() + 1)).slice(-2),
        d = ("0" + now.getDate()).slice(-2),
        h = ("0" + now.getHours()).slice(-2),
        i = ("0" + now.getMinutes()).slice(-2),
        s = ("0" + now.getSeconds()).slice(-2);

    return `rom-${y}${m}${d}-${h}${i}${s}.png`;
}
BinBashBanana commented 3 years ago

Nice! That works very well! I was planning on making a screenshot gallery feature, so that the user could browse their screenshots, and then download them whenever they want. Will be in the next update.