Skip to content

Commit c720275

Browse files
tiansongyutiansongyu
authored andcommitted
[update] add save && read game state function
1 parent 32d86ee commit c720275

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

6502/6502Emulator/src/olcNes_PPU_Backgrounds.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,24 @@ class Demo_olcNES : public olc::PixelGameEngine
253253
if (GetKey(olc::Key::P).bPressed)
254254
(++nSelectedPalette) &= 0x07;
255255

256+
// 存储游戏状态
257+
if (GetKey(olc::Key::F1).bPressed)
258+
{
259+
std::ofstream ofs;
260+
ofs.open("./save_0.dat", std::ofstream::binary);
261+
ofs.write(reinterpret_cast<char *>(&nes), sizeof(nes));
262+
ofs.close();
263+
}
264+
265+
// 还原游戏状态
266+
if (GetKey(olc::Key::F2).bPressed)
267+
{
268+
std::ifstream ifs;
269+
ifs.open("./save_0.dat", std::ofstream::binary);
270+
ifs.read(reinterpret_cast<char *>(&nes), sizeof(nes));
271+
ifs.close();
272+
}
273+
256274
DrawCpu(516, 2);
257275
// DrawCode(516, 72, 26);
258276

@@ -369,6 +387,14 @@ class Demo_olcNES : public olc::PixelGameEngine
369387
// Reset frame completion flag
370388
nes.ppu.frame_complete = false;
371389
}
390+
// 存储游戏状态
391+
if (GetKey(olc::Key::Z).bPressed)
392+
{
393+
std::ofstream ofs;
394+
ofs.open("./save_0.dat", std::ofstream::binary);
395+
ofs.write(reinterpret_cast<char *>(&nes), sizeof(nes));
396+
ofs.close();
397+
}
372398
}
373399

374400
DrawCpu(516, 2);

6502/6502Lib/src/public/Cartridge.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ Cartridge::Cartridge(const std::string &sFileName)
4848
{
4949
// 读iNES文件头
5050
ifs.read(reinterpret_cast<char *>(&header), sizeof(sHeader));
51-
5251
// 如果有mapper1,则调到512字节开始读取文件内容
5352
if (header.mapper1 & 0x04)
5453
ifs.seekg(512, std::ios_base::cur);

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
| B | J |
1616
| Reset | R |
1717

18+
### 功能键
19+
20+
| function | key |
21+
|-----------------|-----------|
22+
| save game state | F1 |
23+
| load game state | F2 |
24+
| reset game | Backspace |
25+
| change palette | P |
26+
1827
## 实现的模块
1928
- BUS
2029
- Cartridge(卡带)

0 commit comments

Comments
 (0)