/
RetroView.m
259 lines (237 loc) · 8.65 KB
/
RetroView.m
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
/**
* ©2022, SUZUKI PLAN
* License: https://github.com/suzukiplan/tohovgs4-ios/blob/master/LICENSE.txt
*/
#import "RetroView.h"
#import "RetroLayer.h"
#import "vge.h"
extern int g_flingY;
extern int g_flingX;
struct MoveDir {
NSTimeInterval time;
int movedX;
int movedY;
};
static struct MoveDir _moveDir[256];
static int _moveCur;
@interface RetroView ()
@property (nonatomic, weak) NSUserDefaults* userDefaults;
@property (nonatomic, weak) MusicManager* musicManager;
@property (nonatomic) NSMutableArray<Album*>* unlockedAlbums;
@property (nonatomic) CADisplayLink* displayLink;
@property (nonatomic) BOOL destroyed;
@property (nonatomic) NSTimeInterval timestampBegan;
@property (nonatomic) CGPoint pointBegan;
@property (nonatomic) CGPoint zoom;
@end
@implementation RetroView
+ (Class)layerClass
{
return [RetroLayer class];
}
- (instancetype)initWithControlDelegate:(id<ControlDelegate>)controlDelegate
{
if (self = [super init]) {
_userDefaults = [NSUserDefaults standardUserDefaults];
_moveCur = 0;
memset(&_moveDir, 0, sizeof(_moveDir));
_musicManager = [controlDelegate getViewController].musicManager;
_unlockedAlbums = [NSMutableArray arrayWithCapacity:_musicManager.albums.count];
for (Song* song in _musicManager.allUnlockedSongs) {
if (NSNotFound == [_unlockedAlbums indexOfObject:song.parentAlbum]) {
[_unlockedAlbums addObject:song.parentAlbum];
}
}
[self _restart];
}
return self;
}
- (void)_restart
{
tohovgs_cleanUp();
tohovgs_allocate((int)_unlockedAlbums.count, (int)_musicManager.allUnlockedSongs.count);
int titleIndex = 0;
int songIndex = 0;
int albumId = 0x10;
for (Album* album in _unlockedAlbums) {
const char* albumTitle = [album.formalName cStringUsingEncoding:NSShiftJISStringEncoding];
const char* albumCopyright = [album.copyright cStringUsingEncoding:NSShiftJISStringEncoding];
int songNum = 0;
int color = (int)album.compatColor;
for (Song* song in album.songs) {
if (![_musicManager isLockedSong:song]) {
const char* mmlPath = [_musicManager mmlPathOfSong:song].UTF8String;
const char* titleJ = [song.name cStringUsingEncoding:NSShiftJISStringEncoding];
const char* titleE = titleJ;
if (song.english) {
titleE = [song.english cStringUsingEncoding:NSShiftJISStringEncoding];
}
const char* cp = strstr(song.mml.UTF8String, "-");
int no = 0;
if (cp) {
cp++;
no = atoi(cp);
}
tohovgs_setSong(songIndex,
albumId,
no,
(int)song.loop,
color,
(void*)mmlPath,
strlen(mmlPath),
(void*)titleJ,
strlen(titleJ),
(void*)titleE,
strlen(titleE));
songNum++;
songIndex++;
}
}
tohovgs_setTitle(titleIndex,
albumId,
songNum,
(void*)albumTitle,
strlen(albumTitle),
(void*)albumCopyright,
strlen(albumCopyright));
titleIndex++;
albumId += 0x10;
}
NSString* kanjiPath = [[NSBundle mainBundle] pathForResource:@"assets/compat/DSLOT255"
ofType:@"DAT"];
NSData* kanji = [NSData dataWithContentsOfFile:kanjiPath];
tohovgs_loadKanji(kanji.bytes, kanji.length);
NSString* c0Path = [[NSBundle mainBundle] pathForResource:@"assets/compat/GSLOT000"
ofType:@"CHR"];
NSData* c0 = [NSData dataWithContentsOfFile:c0Path];
vge_gload(0, c0.bytes);
NSString* c1Path = [[NSBundle mainBundle] pathForResource:@"assets/compat/GSLOT255"
ofType:@"CHR"];
NSData* c1 = [NSData dataWithContentsOfFile:c1Path];
vge_gload(1, c1.bytes);
tohovgs_setPreference((int)[_userDefaults integerForKey:@"compat_current_title_id"],
(int)[_userDefaults integerForKey:@"compat_loop"],
(int)[_userDefaults integerForKey:@"compat_base"],
(int)[_userDefaults integerForKey:@"compat_infinity"],
(int)[_userDefaults integerForKey:@"compat_kobushi"],
(int)[_userDefaults integerForKey:@"compat_locale_id"],
(int)[_userDefaults integerForKey:@"compat_list_type"],
(int)_musicManager.playbackSpeed);
self.opaque = NO;
self.clearsContextBeforeDrawing = NO;
self.multipleTouchEnabled = NO;
self.userInteractionEnabled = YES;
((RetroLayer*)self.layer).retroView = self;
_displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(_detectVsync:)];
_displayLink.preferredFramesPerSecond = 60;
[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
_destroyed = NO;
UIApplication* application = [UIApplication sharedApplication];
application.idleTimerDisabled = YES;
}
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
_zoom = CGPointMake(240.0 / frame.size.width, 320.0 / frame.size.height);
}
- (void)_detectVsync:(CADisplayLink*)sender;
{
if (!_destroyed) {
[(RetroLayer*)self.layer drawFrame];
}
}
- (void)dealloc
{
[self destroy];
}
- (void)savePreferences
{
struct Preferences* prf = tohovgs_getPreference();
[_userDefaults setInteger:prf->base forKey:@"compat_base"];
[_userDefaults setInteger:prf->currentTitleId forKey:@"compat_current_title_id"];
[_userDefaults setInteger:prf->infinity forKey:@"compat_infinity"];
[_userDefaults setInteger:prf->kobushi forKey:@"compat_kobushi"];
[_userDefaults setInteger:prf->listType forKey:@"compat_list_type"];
[_userDefaults setInteger:prf->loop forKey:@"compat_loop"];
[_userDefaults setInteger:prf->localeId forKey:@"compat_locale_id"];
}
- (void)destroy
{
if (!_destroyed) {
[_displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_displayLink invalidate];
_displayLink = nil;
_destroyed = YES;
[self savePreferences];
tohovgs_cleanUp();
}
UIApplication* application = [UIApplication sharedApplication];
application.idleTimerDisabled = NO;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
_timestampBegan = event.timestamp;
_pointBegan = [touch locationInView:self];
_moveCur = 0;
memset(_moveDir, 0, sizeof(_moveDir));
touches=[event allTouches];
UITouch* aTouch=[touches anyObject];
CGPoint point=[aTouch locationInView:self];
_touch.s = 1;
_touch.cx = (int)(point.x * _zoom.x);
_touch.cy = (int)(point.y * _zoom.y);
_touch.px = _touch.cx;
_touch.py = _touch.cy;
_touch.dx = 0;
_touch.dy = 0;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
touches=[event allTouches];
UITouch* aTouch=[touches anyObject];
CGPoint point=[aTouch locationInView:self];
_touch.s = 1;
_touch.px = _touch.cx;
_touch.py = _touch.cy;
_touch.cx = (int)(point.x * _zoom.x);
_touch.cy = (int)(point.y * _zoom.y);
_moveDir[_moveCur].time = [[NSDate date] timeIntervalSince1970];
_moveDir[_moveCur].movedX = _touch.cx-_touch.px;
_moveDir[_moveCur].movedY = _touch.cy-_touch.py;
_moveCur++;
_moveCur &= 0xff;
if(_touch.px) _touch.dx += _touch.cx-_touch.px;
if(_touch.py) _touch.dy += _touch.cy-_touch.py;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
memset(&_touch, 0, sizeof(_touch));
int sp = (_moveCur + 1) & 0xff;
g_flingX = 0;
g_flingY = 0;
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
while (sp != _moveCur) {
if (now - _moveDir[sp].time < 0.1) {
g_flingX += _moveDir[sp].movedX;
g_flingY += _moveDir[sp].movedY;
}
sp++;
sp &= 0xff;
}
if (abs(g_flingY) < abs(g_flingX)) {
g_flingY = 0;
} else {
g_flingX = 0;
}
}
- (void)enterBackground
{
[self destroy];
}
- (void)enterForeground
{
[self _restart];
}
@end