Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save state #16

Closed
jancborchardt opened this issue Sep 26, 2012 · 33 comments
Closed

save state #16

jancborchardt opened this issue Sep 26, 2012 · 33 comments

Comments

@jancborchardt
Copy link
Contributor

To be able to actually play the games properly rather than just fiddle around, the game state should be saved. Not sure if internal game save functions can be accessed or if it’s feasible to just always have the latest state changed. The Gameboy Emulator by Grant Galitz seems to do both.

It’s probably best to always automatically save in localStorage/IndexedDB so that people can return to their game. Maybe even use remotestorage so you can change browsers.

@omarduarte
Copy link

@bfirsh, I may be able to add this feature given a few hints on which parts of the code need to be changed. Any ideas?

@josephlewis42
Copy link

I've got this working over in my repo at https://github.com/josephlewis42/jsnes

@jancborchardt
Copy link
Contributor Author

@josephlewis42 can you open a pull request then? :)

@josephlewis42
Copy link

No problem, just did it!

The save is localstorage though rather than remotestorage.

@lazyxu
Copy link

lazyxu commented Nov 18, 2016

@josephlewis42
I found an error while testing the localstorage function.
below "apply" should be replaced by "call" since s is a json object.

JSNES.Mappers[4].prototype.fromJSON = function(s) {
JSNES.Mappers[0].prototype.fromJSON.apply(this, s);
...
}

@bfirsh
Copy link
Owner

bfirsh commented Sep 11, 2017

I've moved the web emulator to a different repository and created an issue for it. Thanks all for your input! bfirsh/jsnes-web#18

@bfirsh bfirsh closed this as completed Sep 11, 2017
@oknoorap
Copy link

oknoorap commented Nov 7, 2017

how to save and load state? any snippet here? @bfirsh

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

I have done this by localStorage, hope this helps you. @oknoorap
save:

try{
    var saveData = JSON.stringify(nes.toJSON());
    localStorage.setItem(romName, saveData);
}catch(oException){
    if(oException.name == 'QuotaExceededError'){
        console.log('localStorage out of memory');
        localStorage.clear();
        localStorage.setItem(calcMD5(rompath), saveData);
    }
}

load:

var saveData = localStorage.getItem(romName);
if( saveData == null ) {
    console.log("nothing to load");
    return;
}
var decodedData = JSON.parse(saveData);
nes.fromJSON(decodedData);

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

I just did that but doesn't works with jsnes-web, load state doesn't overwrite existing canvas

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

Maybe you forgot to run nes.frame() after load state.

Or perhaps bfirsh removed some related code from jsnes to jsnes-web.

Anyway, I successed to do this with the old versions of jsnes

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

okay, will try, I'll post again when succeed

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

@MeteorKL: great, it's succeed (without nes.frame())

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

@MeteorKL After state loaded, I don't think that save state write bytes correctly, this glitch should be a number (score when mario kill mushroom monster)

image

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

I don't quite understand this glitch should be a number (score when mario kill mushroom monster)
Did you mean that score is not the same as the old one?

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

This glitch graphics
image

should be a number (score)
image

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

Did you mean that
the first graphics: after load
the second graphics: before load

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

yep, after load state there was glitchy graphics in the game

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

Maybe you can stop the frame interval brefore save state, and continue it after load.

I guess it is just because you still run the game after saving state.

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

I do this.stop() or

this.frameTimer.stop();
this.speakers.stop();

before save to local storage, it's still has glitch

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

It seems that the second graphics happens after the first graphics. It is pretty confusing and I have no idea about this.
Maybe you can try another rom to see whether this happens again.

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

Pacman doesn't have any trouble with graphics, but after load state, sounds are muted... 😄

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

Mario 2 after load state 😄

image

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

Interesting bug :)
Maybe there are some mistakes in transforming data in localStorage.
You can compare the JSON string before saving state and after loading state
by saving them to txt and using some tools like vimdiffer.

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

I've modify nes.js by encoding romData to base64 in toJSON function, and decodes back in fromJSON function, it should be the same data... and it's still buggy 😄

@oknoorap
Copy link

oknoorap commented Nov 7, 2017

may be it's related with Tile or ppu issue? can you help me 😭 @MeteorKL, I want to submit PR but I'm too dumb to understand jsnes's code...

@lazyxu
Copy link

lazyxu commented Nov 7, 2017

I'm also on the way to understanding jsnes's code.
Maybe you can show your code to me by your Github or sending mail to me.

@bfirsh
Copy link
Owner

bfirsh commented Nov 15, 2017

This is probably because toJSON or fromJSON somewhere is missing some data...

@oknoorap
Copy link

@bfirsh personally as developer I want to submit PR but it's too complicated to me to read low level code like yours, I'm sorry I just support you here, good luck, hope this issue will be solved 😄

@oknoorap
Copy link

any news related to glitch issue?

@oknoorap
Copy link

oknoorap commented Dec 27, 2017

@bfirsh is this issue still can't be fixed? 😃

@bfirsh
Copy link
Owner

bfirsh commented Dec 29, 2017

I don’t have the time to look at it at the moment, but others are very welcome to tackle it! The main issue tracking is here: bfirsh/jsnes-web#18

@Skypow2012
Copy link

I want to save data by save the nes.cpu.mem.(So this game state can lower from 1.9MB to 187KB) but in this way. when I load the mem, the frame's background is before background. Is there any method can refresh the background? I try to save the nes.ppu.bgbuffer, nes.ppu.vramMem, do nes.ppu.updatePalettes(), but not work, mayby I have sth wrong. Can anyone help me?
@bfirsh

localStorage.mem = JSON.stringify(nes.cpu.mem); // save
nes.cpu.mem = JSON.parse(localStorage.mem); // load

35f3dd7e27e2713b629c8a1b44464e3

JoeMeeks added a commit to JoeMeeks/jsnes that referenced this issue Aug 21, 2021
* fixed nes.fromJSON graphic glitch:
   bfirsh#16
@JoeMeeks JoeMeeks mentioned this issue Aug 21, 2021
@JoeMeeks
Copy link
Contributor

@bfirsh, @Skypow2012, @oknoorap, @lazyxu, @josephlewis42, @jancborchardt I have fixed the load via fromJSON in my PR:
#397

bfirsh pushed a commit that referenced this issue Aug 24, 2021
* fixed nes.fromJSON graphic glitch:
   #16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants