-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathNESGameCore+NESFeatures.mm
More file actions
355 lines (309 loc) · 10.9 KB
/
NESGameCore+NESFeatures.mm
File metadata and controls
355 lines (309 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright (c) 2009, OpenEmu Team
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the OpenEmu Team nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY OpenEmu Team ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL OpenEmu Team BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import <OpenEmuBase/OEGameCore.h>
#import "NESGameCore.h"
#include <NstBase.hpp>
#include <NstApiEmulator.hpp>
#include <NstApiMachine.hpp>
#include <NstApiCartridge.hpp>
#include <NstApiVideo.hpp>
#include <NstApiSound.hpp>
#include <NstApiUser.hpp>
#include <NstApiCheats.hpp>
#include <NstApiRewinder.hpp>
//#include <NstApiRam.h>
#include <NstApiMovie.hpp>
#include <NstMachine.hpp>
#include <iostream>
#include <fstream>
#pragma mark --NES-specific features--
@implementation NESGameCore (NesAdditions)
NSString *const NESNTSC = @"NESNTSC";
NSString *const NESBrightness = @"NESBrightness";
NSString *const NESSaturation = @"NESSaturation";
NSString *const NESContrast = @"NESContrast";
NSString *const NESSharpness = @"NESSharpness";
NSString *const NESColorRes = @"NESColorRes";
NSString *const NESColorBleed = @"NESColorBleed";
NSString *const NESColorArtifacts = @"NESColorArtifacts";
NSString *const NESColorFringing = @"NESColorFringing";
NSString *const NESHue = @"NESHue";
NSString *const NESUnlimitedSprites = @"NESUnlimitedSprites";
- (BOOL)isUnlimitedSpritesEnabled
{
return [[NSUserDefaults standardUserDefaults] boolForKey:NESUnlimitedSprites];
}
- (BOOL)isNTSCEnabled
{
return [[NSUserDefaults standardUserDefaults] boolForKey:NESNTSC];
}
- (int)brightness
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESBrightness];
}
- (int)saturation
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESSaturation];
}
- (int)contrast
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESContrast];
}
- (int)sharpness
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESSharpness];
}
- (int)colorRes
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESColorRes];
}
- (int)colorBleed
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESColorBleed];
}
- (int)colorArtifacts
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESColorArtifacts];
}
- (int)colorFringing
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESColorFringing];
}
- (int)hue
{
return [[NSUserDefaults standardUserDefaults] integerForKey:NESHue];
}
- (void)toggleNTSC:(id)sender
{
if([self isNTSCEnabled])
[self setupVideo:emu withFilter:1];//[[[OpenNestopiaPreferencesController sharedPreferencesController:self] filter] intValue]];
else
[self setupVideo:emu withFilter:0];
}
- (void)applyNTSC:(id)sender
{
NSLog(@"Filters!");
Nes::Api::Video video( *emu );
video.SetSharpness([self sharpness]);
video.SetColorResolution([self colorRes]);
video.SetColorBleed([self colorBleed]);
video.SetBrightness([self brightness]);
video.SetContrast([self contrast]);
video.SetColorArtifacts([self colorArtifacts]);
video.SetHue([self hue]);
video.SetColorFringing([self colorFringing]);
video.SetSaturation([self saturation]);
// [self setupVideo:emu withFilter: 0];
}
- (void)toggleUnlimitedSprites:(id)sender
{
// FIXME: the value returned by -isUnlimitedSpritesEnabled never changes because the value is never saved in the user defaults.
//[self enableUnlimitedSprites:[self isUnlimitedSpritesEnabled]];
}
- (void)enableUnlimitedSprites:(BOOL)enable
{
Nes::Api::Video video( *emu );
video.EnableUnlimSprites(enable ? true : false);
}
-(void)setCode:(NSString*)code
{
if(![code isEqualToString:@""])
{
char cCode[9];
[code cStringUsingEncoding:NSUTF8StringEncoding];
Nes::Api::Cheats cheater(*emu);
Nes::Api::Cheats::Code ggCode;
Nes::Api::Cheats::GameGenieDecode(cCode, ggCode); //FIXME: something's going awry here --dmw
cheater.SetCode(ggCode);
}
else
{
NSLog(@"Null code");
}
}
- (void)enableRewinder:(BOOL)rewind
{
Nes::Api::Rewinder rewinder(*emu);
rewinder.Enable(rewind);
}
- (BOOL)isRewinderEnabled
{
Nes::Api::Rewinder rewinder(*emu);
return rewinder.IsEnabled();
}
- (void)rewinderDirection: (NSUInteger) rewinderDirection
{
Nes::Api::Rewinder rewinder(*emu);
if(rewinderDirection == 1) {
rewinder.SetDirection(Nes::Api::Rewinder::FORWARD);
DLog(@"rewinder direction is forward");
}
else {
rewinder.SetDirection(Nes::Api::Rewinder::BACKWARD);
DLog(@"rewinder direction is backward");
}
}
- (void)enableRewinderBackwardsSound: (BOOL) rewindSound
{
Nes::Api::Rewinder rewinder(*emu);
if(rewindSound)
{
rewinder.EnableSound(YES);
}
else
{
rewinder.EnableSound(NO);
}
}
- (BOOL)isRewinderBackwardsSoundEnabled
{
Nes::Api::Rewinder rewinder(*emu);
return rewinder.IsSoundEnabled();
}
//- (void)setRamBytes:(double)off value:(double)val
//{
// Nes::Api::Ram ram(*emu);
//
// int prgRomOffset = (off * 0x2000) + 0x8000;
// int prgRomValue = (int) (val * 256);
//
// ram.SetRAM(prgRomOffset, prgRomValue);
//}
//
//- (int)cartVRamSize
//{
// // note: some cartridges had VRAM some, some did not. If they didn't, they used the PPU's 2K of RAM alone.
// Nes::Api::Cartridge cart(*emu);
// const Nes::Api::Cartridge::Profile* profile = cart.GetProfile();
// int vRamSize = profile->board.GetVram();
//
// // DLog(@"vRamSize is %U",vRamSize);
// if(vRamSize == 0) //in other words, if the cartridge has no VRAM, set the size to 2K
// return vRamSize = 2048;
// else
// return vRamSize;
//}
//
//// incoming doubles are 0.0 - > 1.0 range. We un-normalize
//- (void)setNmtRamBytes:(double)off value:(double)val
//{
// int nameTableSize = 960; // each nametable (there are 4) is 1024bytes, but the last 64 bytes of each
// // nametable is an attribute table, which controls the palette of the preceding nametable's tiles.
// // note: depending on the cartridge's mirroring mode, there can be one, two or four nametables (functionally speaking), while the others are just mirrors (copies).
//
// Nes::Api::Machine machine(*emu);
//
// int numPages = 3;
//
// int unnormalizedOffset = off * nameTableSize; // subtract 64bytes to stay out of the attribute table
// int unnormalizedVal = (int) (val * 256); // Nametable values are addresses in the pattern tables, which are at $0000 and $1000 in the PPU's RAM and $1000bytes each.
// // Here's a really good, short explanation of all the tables: http://nesdev.parodius.com/NESTechFAQ.htm#namattpat
//
// for(int i = 0; i < numPages; i++)
// {
// machine.PokeNmt((i * 0x400) + 0x2000 + unnormalizedOffset, unnormalizedVal); //note: 0x400 = 1024bytes
// }
//}
//
//- (void)setNMTRamByTable:(NSNumber*)table array:(NSArray*)nmtValueArray
//{
// Nes::Api::Machine machine(*emu);
//
// int startOfNameTable = [table unsignedIntValue] * 0x400;
//
// for (int i = 0; i < 960; i++)
// {
// machine.PokeNmt(i + startOfNameTable + 0x2000, [[nmtValueArray objectAtIndex:i] intValue]);
// }
//}
//
//- (int)chrRomSize //TODO: OK, sometimes games have CHR ROM, sometimes CHR RAM, sometimes both (or none). maybe just cut the ROM and call it chrSize? that's what GetChr() seems to be returning anyway. or maybe we'd be better served just replacing this with sprite-only-gltiching methods.
//{
// Nes::Api::Cartridge cart(*emu);
// const Nes::Api::Cartridge::Profile* profile = cart.GetProfile();
// int romSize = profile->board.GetChr();
// // DLog(@"called chrRomSize. result: %U", romSize);
// return romSize;
//}
//
//// incoming doubles are 0.0 - > 1.0 range. We un-normalize
//- (void)setChrRamBytes:(double)off value:(double)val
//{
// // FIXME: this method needs to be rethunk. and i shall do the rethunking! --dan
//
// int pageSize = 2048; // this should be 1024 (according to Josh), but it seems
// // using 2K makes glitches across all screens, whereas 1K
// // occasionally would leave entire screen widths unaltered...
//
// Nes::Api::Ram ram(*emu);
//
// int romSize = [self chrRomSize]; // should be 4096 --ORLY? :P TODO: prove it.
// int numPages = romSize / pageSize;
//
// int unnormalizedOffset = (int) (off * pageSize);
// int unnormalizedVal = ((int) (val * pageSize)) % 64; // this % may fix a crash in glitching chr ram when looking up a palette above max val.
//
// for(int i = 0; i < numPages ; i++)
// {
// ram.SetCHR(0, unnormalizedOffset , unnormalizedVal);
// }
//}
- (void)recordMovie:(NSString*) moviePath mode:(BOOL)append
{
Nes::Api::Movie movie(*emu);
Nes::Result result;
std::fstream movieFile([moviePath cStringUsingEncoding:NSUTF8StringEncoding], std::ios::in | std::ios::binary);
if(append)
result = movie.Record(movieFile,Nes::Api::Movie::APPEND);
else
result = movie.Record(movieFile, Nes::Api::Movie::CLEAN);
}
- (void)playMovie:(NSString*) moviePath
{
Nes::Api::Movie movie(*emu);
Nes::Result result;
std::ifstream movieFile([moviePath cStringUsingEncoding:NSUTF8StringEncoding], std::ios::in | std::ios::binary);
result = movie.Play(movieFile);
}
- (void)stopMovie
{
Nes::Api::Movie movie(*emu);
movie.Stop();
}
- (BOOL)isMovieRecording
{
Nes::Api::Movie movie(*emu);
return movie.IsRecording();
}
- (BOOL)isMoviePlaying
{
Nes::Api::Movie movie(*emu);
return movie.IsPlaying();
}
- (BOOL)isMovieStopped
{
Nes::Api::Movie movie(*emu);
return movie.IsStopped();
}
@end