Skip to content

Commit 1d2d82b

Browse files
author
Gabriel O'Flaherty-Chan
committed
Revert "improve reliability of loading and saving"
This reverts commit 259102d.
1 parent 22241dd commit 1d2d82b

File tree

5 files changed

+62
-66
lines changed

5 files changed

+62
-66
lines changed

gambatte_watchOS/GameCore.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ static int const kScreenHeight = 144;
5757
@property (strong, nonatomic) NSURL *workingDirectory;
5858
@property (nonatomic, strong) void (^didRender)(uint32_t *);
5959
@property (nonatomic) uint32_t *activeInput;
60-
@property (nonatomic) BOOL paused;
60+
61+
@property (nonatomic) BOOL enableFrameSkip;
6162

6263
- (void)startEmulation;
6364
- (void)stopEmulation;

gambatte_watchOS/GameCore.mm

+28-22
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,23 @@ @implementation GameCore {
5454
uint32_t *videoBuffer;
5555
uint32_t *unusedBuffer;
5656
NSTimer *updateTimer;
57+
BOOL running;
58+
}
59+
60+
- (instancetype)init
61+
{
62+
self = [super init];
63+
if (self) {
64+
videoBuffer = (uint32_t *)malloc(kScreenWidth * kScreenHeight * 4);
65+
unusedBuffer = (uint32_t *)malloc(2064 * 2 * 4);
66+
}
67+
return self;
5768
}
5869

5970
- (void)dealloc
6071
{
61-
[self stopEmulation];
72+
free(videoBuffer);
73+
free(unusedBuffer);
6274
}
6375

6476
- (void)loadFileAtPath:(NSString *)path success:(void (^)(GameCore * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure
@@ -87,14 +99,10 @@ - (void)loadFileAtPath:(NSString *)path success:(void (^)(GameCore * _Nonnull))s
8799
- (void)startEmulation
88100
{
89101
// Using a Timer instead to trigger the game loop is slower, but ensures all frames are rendered
90-
// TODO use enableFrameSkip property to switch betwen timer and NSThread
91102
// NSTimeInterval interval = 1 / (frameInterval * 0.5);
92103
// updateTimer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(runGameLoop) userInfo:nil repeats:YES];
93104

94-
videoBuffer = (uint32_t *)malloc(kScreenWidth * kScreenHeight * 4);
95-
unusedBuffer = (uint32_t *)malloc(2064 * 2 * 4);
96-
97-
_paused = NO;
105+
running = YES;
98106
gameCoreThread = [[NSThread alloc] initWithTarget:self selector:@selector(runGameLoop) object:nil];
99107
gameCoreThread.name = @"Giovanni Game Core";
100108
gameCoreThread.qualityOfService = NSQualityOfServiceUserInteractive;
@@ -107,12 +115,12 @@ - (void)runGameLoop
107115

108116
OESetThreadRealtime(1. / (1. * frameInterval), .007, .03);
109117

110-
while (!_paused) { @autoreleasepool {
118+
while (running) {
119+
@autoreleasepool {
111120
size_t samples = 2064;
112-
121+
113122
while (gb.runFor((gambatte::uint_least32_t *)videoBuffer, kScreenWidth,
114-
(gambatte::uint_least32_t *)unusedBuffer, samples) == -1 && !_paused) {
115-
123+
(gambatte::uint_least32_t *)unusedBuffer, samples) == -1 && running) {
116124
}
117125

118126
NSTimeInterval advance = 1.0 / (1. * frameInterval);
@@ -131,28 +139,27 @@ - (void)runGameLoop
131139
if (_didRender != nil) {
132140
_didRender(videoBuffer);
133141
}
134-
}}
142+
}
143+
}
135144
}
136145

137146
- (void)stopEmulation
138147
{
139-
if (gameCoreThread == nil) {
140-
return;
141-
}
142-
143148
gb.saveSavedata();
144-
free(videoBuffer);
145-
free(unusedBuffer);
146-
147149
// [updateTimer invalidate];
148-
_paused = YES;
150+
running = NO;
149151
[gameCoreThread cancel];
150-
gameCoreThread = nil;
151152
}
152153

153154
- (void)resetEmulation
154155
{
155156
[self stopEmulation];
157+
158+
free(videoBuffer);
159+
free(unusedBuffer);
160+
videoBuffer = (uint32_t *)malloc(kScreenWidth * kScreenHeight * 4);
161+
unusedBuffer = (uint32_t *)malloc(2064 * 2 * 4);
162+
156163
gb.reset();
157164
[self startEmulation];
158165
}
@@ -179,8 +186,7 @@ - (void)saveToSlot:(NSInteger)slot
179186
{
180187
gb.saveSavedata();
181188
gb.selectState(slot);
182-
int saved = gb.saveState(0, 0);
183-
if (saved != 1) {
189+
if (!gb.saveState((gambatte::uint_least32_t *)videoBuffer, kScreenWidth)) {
184190
NSLog(@"Error saving to slot %@", @(slot));
185191
}
186192
}

gambatte_watchOS/GameLoader.swift

+9-21
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,8 @@ public class GameLoader: NSObject {
6464
var reason: String
6565
}
6666

67-
var core: GameCore? {
68-
didSet {
69-
if let oldCore = oldValue {
70-
oldCore.stopEmulation()
71-
}
72-
if let newCore = core {
73-
newCore.startEmulation()
74-
}
75-
}
76-
}
67+
let core: GameCore
68+
let documentDirectory: URL
7769
let session = WCSession.default()
7870
var activationState: WCSessionActivationState = .notActivated
7971

@@ -84,6 +76,12 @@ public class GameLoader: NSObject {
8476
static let shared = GameLoader()
8577

8678
private override init() {
79+
80+
documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
81+
82+
core = GameCore()
83+
core.workingDirectory = documentDirectory
84+
8785
super.init()
8886
}
8987

@@ -104,13 +102,6 @@ public class GameLoader: NSObject {
104102
}
105103
}
106104

107-
func createCore() -> GameCore {
108-
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
109-
let core = GameCore()
110-
core.workingDirectory = documentDirectory
111-
return core
112-
}
113-
114105
func requestGames(_ success: @escaping (([Game]) -> Void), failure: @escaping ((Error) -> Void)) {
115106

116107
let replyHandler: (([String: Any]) -> Void) = { (response) in
@@ -128,10 +119,7 @@ public class GameLoader: NSObject {
128119
func loadGame(_ game: Game, _ success: @escaping ((GameCore) -> Void), failure: @escaping ((Error) -> Void)) {
129120

130121
gameResponse = { [unowned self] (dataPath) in
131-
132-
let core = self.createCore()
133-
self.core = core
134-
core.loadFile(atPath: dataPath, success: { (core) in
122+
self.core.loadFile(atPath: dataPath, success: { (core) in
135123
UserDefaults.standard.lastPlayed = game
136124
success(core)
137125
}, failure: failure)

giovanni WatchKit Extension/GameplayController.swift

+23-17
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class GameplayController: WKInterfaceController {
8484
@IBAction func BSelected() { pressInputOnce(.B) }
8585

8686
@IBAction func resetSelected() {
87-
loader.core?.resetEmulation()
87+
core?.resetEmulation()
8888
}
8989

9090
@IBOutlet var ALabel: WKInterfaceLabel!
@@ -94,7 +94,7 @@ class GameplayController: WKInterfaceController {
9494

9595
func updateDirectionalInputs() {
9696

97-
guard let core = loader.core else {
97+
guard let core = core else {
9898
DPadLabel.setText(GameInput(rawValue: 0).displaySymbol)
9999
return
100100
}
@@ -126,6 +126,18 @@ class GameplayController: WKInterfaceController {
126126
}
127127

128128
let loader = GameLoader.shared
129+
var core: GameCore? {
130+
didSet {
131+
if let core = core {
132+
core.didRender = { [weak self] buffer in
133+
guard let s = self else {
134+
return
135+
}
136+
s.updateSnapshotIfNeeded(with: buffer)
137+
}
138+
}
139+
}
140+
}
129141

130142
var tick = 0
131143
let refreshRate = 20;
@@ -139,14 +151,8 @@ class GameplayController: WKInterfaceController {
139151
}
140152

141153
let success: ((GameCore) -> Void) = { [unowned self] (core) in
142-
core.didRender = { [weak self] buffer in
143-
guard let s = self else {
144-
return
145-
}
146-
s.updateSnapshotIfNeeded(with: buffer)
147-
}
154+
self.core = core
148155
}
149-
150156
let failureHandler: ((Error) -> Void) = { [unowned self] (error) in
151157
print("error loading game \(error)")
152158
self.presentAlert(withTitle: "There was an issue loading this game", message: nil, preferredStyle: .alert, actions: [
@@ -166,9 +172,9 @@ class GameplayController: WKInterfaceController {
166172
override func didAppear() {
167173
super.didAppear()
168174

169-
if let core = loader.core {
175+
if let core = core {
176+
core.startEmulation()
170177
core.load(fromSlot: 0)
171-
core.paused = false
172178
}
173179

174180
crownSequencer.delegate = self
@@ -178,25 +184,25 @@ class GameplayController: WKInterfaceController {
178184
override func didDeactivate() {
179185
super.didDeactivate()
180186

181-
if let core = loader.core {
187+
if let core = core {
182188
core.save(toSlot: 0)
183-
core.paused = true;
189+
core.stopEmulation()
184190
}
185191

186192
crownSequencer.delegate = nil
187193
crownSequencer.resignFocus()
188194
}
189-
195+
190196
var lastSnapshot: UIImage?
191197

192198
func updateSnapshotIfNeeded(with buffer: UnsafeMutablePointer<UInt32>) {
193199

194200
tick += 1
195-
if tick > refreshRate || loader.core == nil {
201+
if tick > refreshRate || core == nil {
196202
return
197203
}
198204

199-
let snapshot = loader.core!.createSnapshot(from: buffer)
205+
let snapshot = core!.createSnapshot(from: buffer)
200206

201207
// compare before updating. Not sure if faster.
202208
// if let lhs = lastSnapshot, let rhs = snapshot, lhs.equalPixels(to: rhs) {
@@ -224,7 +230,7 @@ class GameplayController: WKInterfaceController {
224230

225231
func setInputSelected(_ input: GameInput, selected: Bool) {
226232

227-
if let core = loader.core {
233+
if let core = core {
228234
core.update(input, selected: selected)
229235
}
230236

giovanni WatchKit Extension/LibraryController.swift

-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ class LibraryController: WKInterfaceController {
6060
// if let game = UserDefaults.standard.lastPlayed {
6161
// presentGame(game)
6262
// }
63-
64-
let loader = GameLoader.shared
65-
if loader.core != nil {
66-
loader.core = nil
67-
}
6863
}
6964

7065
func reloadGames() {

0 commit comments

Comments
 (0)