-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNowPlayingController.m
executable file
·1099 lines (998 loc) · 31.9 KB
/
NowPlayingController.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// NowPlayingController.m
// ShoutOut
//
// Created by ME on 9/13/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "SOApplication.h"
#import <pthread.h>
#define IPHONE_MENU_DISABLED 0
#define IPHONE_MENU_MAIN 1
#define IPHONE_MENU_SAVE_CURRENT 2
#define IPHONE_MENU_SAVE_NEW 3
#define IPHONE_MENU_QUIT_LOAD 4
#define IPHONE_MENU_QUIT 5
#define IPHONE_MENU_MAIN_LOAD 6
char romfile[1024];
char* ourArgs[255];
int ourArgsCnt = 0;
unsigned short* screenbuffer;
int iphone_touches = 0;
int iphone_menu = IPHONE_MENU_DISABLED;
long long iphone_last_upd_ticks = 0;
int iphone_controller_opacity = 100;
int iphone_is_landscape = 0;
int iphone_soundon = 0;
pthread_t sound_tid;
static unsigned long newtouches[10];
static unsigned long oldtouches[10];
enum { GP2X_UP=0x1, GP2X_LEFT=0x4, GP2X_DOWN=0x10, GP2X_RIGHT=0x40,
GP2X_START=1<<8, GP2X_SELECT=1<<9, GP2X_L=1<<10, GP2X_R=1<<11,
GP2X_A=1<<12, GP2X_B=1<<13, GP2X_X=1<<14, GP2X_Y=1<<15,
GP2X_VOL_UP=1<<23, GP2X_VOL_DOWN=1<<22, GP2X_PUSH=1<<27 };
extern float __audioVolume;
extern volatile unsigned short BaseAddress[240*160];
extern unsigned long gp2x_pad_status;
extern volatile int __emulation_run;
extern volatile int __emulation_saving;
extern volatile int __emulation_paused;
extern int tArgc;
extern char** tArgv;
extern pthread_t main_tid;
extern unsigned char gamepak_filename[512];
extern char test_print_buffer[128];
extern unsigned short* videobuffer;
extern unsigned char *vrambuffer;
extern void save_state(char *savestate_filename, unsigned short *screen_capture);
extern void set_save_state(void);
extern int iphone_main(char* filename);
static ScreenView *sharedInstance = nil;
void updateScreen()
{
//usleep(100);
//sched_yield();
[sharedInstance performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
}
void* app_Thread_Start(void* args)
{
__emulation_run = 1;
iphone_main(ourArgs[1]);
/* if(tArgv)
{
int i;
for(i = 0; i < tArgc; i++)
{
if(tArgv[i] != NULL) free(tArgv[i]);
}
free(tArgv);
tArgv = NULL;
}
*/
__emulation_run = 0;
__emulation_saving = 0;
return NULL;
}
@implementation ScreenView
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])!=nil) {
CFMutableDictionaryRef dict;
int w = 256; //rect.size.width;
int h = 224; //rect.size.height;
int pitch = w * 2, allocSize = 2 * w * h;
char *pixelFormat = "565L";
self.opaque = YES;
self.clearsContextBeforeDrawing = NO;
self.userInteractionEnabled = NO;
self.multipleTouchEnabled = NO;
self.contentMode = UIViewContentModeTopLeft;
if([SOApp.optionsView getCurrentSmoothScaling])
{
[[self layer] setMagnificationFilter:kCAFilterLinear];
[[self layer] setMinificationFilter:kCAFilterLinear];
}
else
{
[[self layer] setMagnificationFilter:kCAFilterNearest];
[[self layer] setMinificationFilter:kCAFilterNearest];
}
dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(dict, kCoreSurfaceBufferGlobal, kCFBooleanTrue);
CFDictionarySetValue(dict, kCoreSurfaceBufferMemoryRegion,
@"IOSurfaceMemoryRegion");
CFDictionarySetValue(dict, kCoreSurfaceBufferPitch,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pitch));
CFDictionarySetValue(dict, kCoreSurfaceBufferWidth,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &w));
CFDictionarySetValue(dict, kCoreSurfaceBufferHeight,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &h));
CFDictionarySetValue(dict, kCoreSurfaceBufferPixelFormat,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, pixelFormat));
CFDictionarySetValue(dict, kCoreSurfaceBufferAllocSize,
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &allocSize));
_screenSurface = CoreSurfaceBufferCreate(dict);
CoreSurfaceBufferLock(_screenSurface, 3);
//CALayer* screenLayer = [CALayer layer];
screenLayer = [[CALayer layer] retain];
/*
CGAffineTransform affineTransform = CGAffineTransformIdentity;
affineTransform = CGAffineTransformConcat( affineTransform, CGAffineTransformMakeRotation(90));
self.transform = affineTransform;
*/
if(!iphone_is_landscape)
{
if([SOApp.optionsView getCurrentScaling])
{
screenLayer.frame = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
[ screenLayer setOpaque: YES ];
}
else
{
screenLayer.frame = CGRectMake(32.0f, 8.0f, 258.0f, 226.0f);
[ screenLayer setOpaque: YES ];
}
}
else
{
if([SOApp.optionsView getCurrentScaling])
{
CGAffineTransform transform = CGAffineTransformMakeRotation(3.0 * M_PI / 2.0f); // = CGAffineTransformMakeTranslation(1.0, 1.0);
[screenLayer setAffineTransform:transform];
screenLayer.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
//[screenLayer setCenter:CGPointMake(240.0f,160.0f)];
[ screenLayer setOpaque:YES ];
}
else
{
CGAffineTransform transform = CGAffineTransformMakeRotation(3.0 * M_PI / 2.0f); // = CGAffineTransformMakeTranslation(1.0, 1.0);
[screenLayer setAffineTransform:transform];
screenLayer.frame = CGRectMake(48.0f, 112.0f, 224.0f, 256.0f);
//[screenLayer setCenter:CGPointMake(240.0f,160.0f)];
[ screenLayer setOpaque:YES ];
}
}
if([SOApp.optionsView getCurrentSmoothScaling])
{
[screenLayer setMagnificationFilter:kCAFilterLinear];
[screenLayer setMinificationFilter:kCAFilterLinear];
}
else
{
[screenLayer setMagnificationFilter:kCAFilterNearest];
[screenLayer setMinificationFilter:kCAFilterNearest];
}
screenLayer.contents = (id)_screenSurface;
[[self layer] addSublayer:screenLayer];
/*
screenLayer = [CALayer layer];
screenLayer.doubleSided = NO;
screenLayer.bounds = rect;
screenLayer.contents = (id)_screenSurface;
screenLayer.anchorPoint = CGPointMake(0, 0); // set anchor point to top-left
[self.layer addSublayer: screenLayer];
*/
CoreSurfaceBufferUnlock(_screenSurface);
screenbuffer = CoreSurfaceBufferGetBaseAddress(_screenSurface);
//[NSThread setThreadPriority:0.0];
//[NSThread detachNewThreadSelector:@selector(updateScreen) toTarget:self withObject:nil];
iphone_last_upd_ticks = 0;
/*
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0f / 50.0f)
target:self
selector:@selector(setNeedsDisplay)
userInfo:nil
repeats:YES];
*/
}
sharedInstance = self;
return self;
}
- (void)dealloc
{
//[timer invalidate];
[ screenLayer release ];
[ super dealloc ];
}
- (CoreSurfaceBufferRef)getSurface
{
return _screenSurface;
}
- (void)drawRect:(CGRect)rect
{
if(screenbuffer && vrambuffer)
{
memcpy(screenbuffer, vrambuffer, 256*224*2);
}
}
- (void)dummy
{
}
- (void)updateScreen
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//[NSThread setThreadPriority:0.9];
/*
struct sched_param param;
param.sched_priority = 46;
if(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m) != 0)
{
fprintf(stderr, "Error setting pthread priority\n");
}
*/
/*
struct sched_param param;
param.sched_priority = -47;
if(pthread_setschedparam(pthread_self(), SCHED_OTHER, ¶m) != 0)
{
fprintf(stderr, "Error setting pthread priority\n");
}
*/
while(__emulation_run)
{
[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
//usleep(16666);
//sched_yield();
}
[pool release];
}
@end
@implementation NowPlayingController
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if(__emulation_run)
{
if(iphone_menu == IPHONE_MENU_MAIN)
{
if(buttonIndex == 0)
{
iphone_menu = IPHONE_MENU_SAVE_CURRENT;
}
else if(buttonIndex == 1)
{
iphone_menu = IPHONE_MENU_SAVE_NEW;
}
else if(buttonIndex == 2)
{
iphone_menu = IPHONE_MENU_QUIT_LOAD;
}
else
{
iphone_menu = IPHONE_MENU_DISABLED;
}
}
if(iphone_menu == IPHONE_MENU_QUIT_LOAD)
{
iphone_menu = IPHONE_MENU_QUIT;
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle:@"Save your state before quitting? You can overwrite a currently loaded savestate, save to a new file, or cancel." delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save [Currently Loaded] State",@"Save State To New File",@"Don't Save",@"Cancel", nil];
[alert showInView:screenView];
[alert release];
return;
}
if(iphone_menu == IPHONE_MENU_QUIT)
{
if (buttonIndex == 0)
{
[SOApp.delegate switchToBrowse];
[tabBar didMoveToWindow];
__emulation_saving = 2;
__emulation_run = 0;
while(__emulation_saving)
{
usleep(16666);
//sched_yield();
}
//pthread_join(main_tid, NULL);
[screenView removeFromSuperview];
[screenView release];
//[controllerImageView removeFromSuperview];
//[controllerImageView release];
__emulation_run = 0;
__emulation_saving = 0;
#ifdef WITH_ADS
[SOApp.delegate unpauseAdViews];
#endif
[SOApp.saveStatesView startSaveData:0.1f];
[SOApp.delegate switchToSaveStates];
[tabBar didMoveToWindowSaveStates];
}
else if (buttonIndex == 1)
{
[SOApp.delegate switchToBrowse];
[tabBar didMoveToWindow];
__emulation_saving = 1;
__emulation_run = 0;
while(__emulation_saving)
{
usleep(1000000);
sched_yield();
}
//pthread_join(main_tid, NULL);
[screenView removeFromSuperview];
[screenView release];
//[controllerImageView removeFromSuperview];
//[controllerImageView release];
__emulation_run = 0;
__emulation_saving = 0;
#ifdef WITH_ADS
[SOApp.delegate unpauseAdViews];
#endif
[SOApp.saveStatesView startSaveData:0.1f];
[SOApp.delegate switchToSaveStates];
[tabBar didMoveToWindowSaveStates];
}
else if (buttonIndex == 2)
{
[SOApp.delegate switchToBrowse];
[tabBar didMoveToWindow];
__emulation_saving = 0;
__emulation_run = 0;
//pthread_join(main_tid, NULL);
[screenView removeFromSuperview];
[screenView release];
//[controllerImageView removeFromSuperview];
//[controllerImageView release];
__emulation_run = 0;
__emulation_saving = 0;
#ifdef WITH_ADS
[SOApp.delegate unpauseAdViews];
#endif
}
iphone_menu = IPHONE_MENU_DISABLED;
}
else if(iphone_menu == IPHONE_MENU_SAVE_CURRENT)
{
__emulation_saving = 2;
iphone_menu = IPHONE_MENU_DISABLED;
}
else if(iphone_menu == IPHONE_MENU_SAVE_NEW)
{
__emulation_saving = 1;
iphone_menu = IPHONE_MENU_DISABLED;
}
else
{
iphone_menu = IPHONE_MENU_DISABLED;
}
}
else
{
if (buttonIndex == 0)
{
iphone_is_landscape = 0;
iphone_soundon = 1;
[ self getControllerCoords:0 ];
[ self fixRects ];
numFingers = 0;
__emulation_run = 1;
screenView = [ [ScreenView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
[self.view addSubview: screenView];
/*
controllerImageView = [ [ UIImageView alloc ] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"controller_hs%d.png", [SOApp.optionsView getCurrentSkin]]]];
controllerImageView.frame = CGRectMake(0.0f, 240.0f, 320.0f, 240.0f); // Set the frame in which the UIImage should be drawn in.
controllerImageView.userInteractionEnabled = NO;
controllerImageView.multipleTouchEnabled = NO;
controllerImageView.clearsContextBeforeDrawing = NO;
[controllerImageView setOpaque:YES];
[controllerImageView setAlpha:((float)iphone_controller_opacity / 100.0f)];
[self.view addSubview: controllerImageView]; // Draw the image in self.view.
*/
}
else if (buttonIndex == 1)
{
iphone_is_landscape = 0;
iphone_soundon = 0;
[ self getControllerCoords:0 ];
[ self fixRects ];
numFingers = 0;
__emulation_run = 1;
screenView = [ [ScreenView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
[self.view addSubview: screenView];
/*
controllerImageView = [ [ UIImageView alloc ] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"controller_hs%d.png", [SOApp.optionsView getCurrentSkin]]]];
controllerImageView.frame = CGRectMake(0.0f, 240.0f, 320.0f, 240.0f); // Set the frame in which the UIImage should be drawn in.
controllerImageView.userInteractionEnabled = NO;
controllerImageView.multipleTouchEnabled = NO;
controllerImageView.clearsContextBeforeDrawing = NO;
[controllerImageView setOpaque:YES];
[controllerImageView setAlpha:((float)iphone_controller_opacity / 100.0f)];
[self.view addSubview: controllerImageView]; // Draw the image in self.view.
*/
}
else if (buttonIndex == 2)
{
iphone_is_landscape = 1;
iphone_soundon = 1;
[ self getControllerCoords:1 ];
[ self fixRects ];
numFingers = 0;
__emulation_run = 1;
screenView = [ [ScreenView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
[self.view addSubview: screenView];
/*
controllerImageView = [ [ UIImageView alloc ] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"controller_fs%d.png", [SOApp.optionsView getCurrentSkin]]]];
controllerImageView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f); // Set the frame in which the UIImage should be drawn in.
controllerImageView.userInteractionEnabled = NO;
controllerImageView.multipleTouchEnabled = NO;
controllerImageView.clearsContextBeforeDrawing = NO;
[controllerImageView setOpaque:YES];
[controllerImageView setAlpha:((float)iphone_controller_opacity / 100.0f)];
[self.view addSubview: controllerImageView]; // Draw the image in self.view.
*/
}
else
{
iphone_is_landscape = 1;
iphone_soundon = 0;
[ self getControllerCoords:1 ];
[ self fixRects ];
numFingers = 0;
__emulation_run = 1;
screenView = [ [ScreenView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
[self.view addSubview: screenView];
/*
controllerImageView = [ [ UIImageView alloc ] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"controller_fs%d.png", [SOApp.optionsView getCurrentSkin]]]];
controllerImageView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f); // Set the frame in which the UIImage should be drawn in.
[controllerImageView setOpaque:YES];
[controllerImageView setAlpha:((float)iphone_controller_opacity / 100.0f)];
controllerImageView.userInteractionEnabled = NO;
controllerImageView.multipleTouchEnabled = NO;
controllerImageView.clearsContextBeforeDrawing = NO;
[self.view addSubview: controllerImageView]; // Draw the image in self.view.
*/
}
#ifdef WITH_ADS
[SOApp.delegate pauseAdViews];
#endif
pthread_create(&main_tid, NULL, app_Thread_Start, NULL);
/*
struct sched_param param;
param.sched_priority = 46;
if(pthread_setschedparam(main_tid, SCHED_RR, ¶m) != 0)
{
fprintf(stderr, "Error setting pthread priority\n");
}
*/
struct sched_param param;
param.sched_priority = 46;
if(pthread_setschedparam(main_tid, SCHED_OTHER, ¶m) != 0)
{
fprintf(stderr, "Error setting pthread priority\n");
}
//[NSThread detachNewThreadSelector:@selector(runSound) toTarget:self withObject:nil];
}
}
- (void)runProgram
{
__emulation_run = 1;
iphone_main(ourArgs[1]);
__emulation_run = 0;
__emulation_saving = 0;
}
- (void)startEmu:(char*)path {
int i;
iphone_menu = IPHONE_MENU_DISABLED;
ourArgsCnt = 0;
/* faked executable path */
ourArgs[ourArgsCnt]=get_resource_path("snes4iphone"); ourArgsCnt++;
/* playgame */
sprintf(romfile, "%s", path);
ourArgs[ourArgsCnt]=romfile; ourArgsCnt++;
for (i=0; i<ourArgsCnt; i++)
{
fprintf(stderr, "%s ",ourArgs[i]);
}
fprintf(stderr, "\n");
self.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle:@"Choose Your Screen Orientation & Sound Options" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Portrait & Sound", @"Portrait & No Sound", @"Landscape & Sound", @"Landscape & No Sound", nil];
[alert showInView:self.view];
[alert release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (void)awakeFromNib {
//[ self setTapDelegate: self ];
//[ self setGestureDelegate: self ];
//[ self setMultipleTouchEnabled: YES ];
[ self getControllerCoords:0 ];
[ self fixRects ];
numFingers = 0;
iphone_touches = 0;
self.navigationItem.hidesBackButton = YES;
self.view.opaque = YES;
self.view.clearsContextBeforeDrawing = NO;
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;
self.view.contentMode = UIViewContentModeTopLeft;
[[self.view layer] setMagnificationFilter:kCAFilterNearest];
[[self.view layer] setMinificationFilter:kCAFilterNearest];
[NSThread setThreadPriority:1.0];
/*
struct sched_param param;
param.sched_priority = 40;
if(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m) != 0)
{
fprintf(stderr, "Error setting pthread priority\n");
}
*/
/*
struct sched_param param;
param.sched_priority = 47;
if(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m) != 0)
{
fprintf(stderr, "Error setting pthread priority\n");
}
*/
}
- (void)drawRect:(CGRect)rect
{
}
- (void)fixRects {
/*
UpLeft = [ self convertRect: UpLeft toView: self ];
DownLeft = [ self convertRect: DownLeft toView: self ];
UpRight = [ self convertRect: UpRight toView: self ];
DownRight = [ self convertRect: DownRight toView: self ];
Up = [ self convertRect: Up toView: self ];
Down = [ self convertRect: Down toView: self ];
Left = [ self convertRect: Left toView: self ];
Right = [ self convertRect: Right toView: self ];
Select = [ self convertRect: Select toView: self ];
Start = [ self convertRect: Start toView: self ];
ButtonUpLeft = [ self convertRect: ButtonUpLeft toView: self ];
ButtonDownLeft = [ self convertRect: ButtonDownLeft toView: self ];
ButtonUpRight = [ self convertRect: ButtonUpRight toView: self ];
ButtonDownRight = [ self convertRect: ButtonDownRight toView: self ];
ButtonUp = [ self convertRect: ButtonUp toView: self ];
ButtonDown = [ self convertRect: ButtonDown toView: self ];
ButtonLeft = [ self convertRect: ButtonLeft toView: self ];
ButtonRight = [ self convertRect: ButtonRight toView: self ];
LPad = [ self convertRect: LPad toView: self ];
RPad = [ self convertRect: RPad toView: self ];
LPad2 = [ self convertRect: LPad2 toView: self ];
RPad2 = [ self convertRect: RPad2 toView: self ];
Menu = [ self convertRect: Menu toView: self ];
*/
}
- (void)runMenu
{
if(__emulation_paused)
{
return;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
__emulation_paused = 1;
iphone_menu = IPHONE_MENU_MAIN_LOAD;
while(iphone_menu != IPHONE_MENU_DISABLED)
{
if(iphone_menu == IPHONE_MENU_MAIN_LOAD)
{
iphone_menu = IPHONE_MENU_MAIN;
[NSThread detachNewThreadSelector:@selector(runMainMenu) toTarget:self withObject:nil];
}
else
{
sched_yield();
usleep(1000000);
}
}
__emulation_paused = 0;
[pool release];
}
- (void)runMainMenu
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
//[NSThread setThreadPriority:0.5];
/*
struct sched_param param;
param.sched_priority = 45;
if(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m) != 0)
{
fprintf(stderr, "Error setting pthread priority\n");
}
*/
UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle:@"Choose an option from the menu. Press cancel to go back." delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save [Currently Loaded] State",@"Save State To New File",@"Quit To Menu",@"Cancel", nil];
[alert showInView:screenView];
[alert release];
[pool release];
}
#define MyCGRectContainsPoint(rect, point) \
(((point.x >= rect.origin.x) && \
(point.y >= rect.origin.y) && \
(point.x <= rect.origin.x + rect.size.width) && \
(point.y <= rect.origin.y + rect.size.height)) ? 1 : 0)
#if 1
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
int touchstate[10];
//Get all the touches.
int i;
NSSet *allTouches = [event allTouches];
int touchcount = [allTouches count];
if(!__emulation_run)
{
return;
}
for (i = 0; i < 10; i++)
{
touchstate[i] = 0;
oldtouches[i] = newtouches[i];
}
for (i = 0; i < touchcount; i++)
{
UITouch *touch = [[allTouches allObjects] objectAtIndex:i];
if( touch != nil &&
( touch.phase == UITouchPhaseBegan ||
touch.phase == UITouchPhaseMoved ||
touch.phase == UITouchPhaseStationary) )
{
struct CGPoint point;
point = [touch locationInView:self.view];
touchstate[i] = 1;
if (MyCGRectContainsPoint(Left, point))
{
gp2x_pad_status |= GP2X_LEFT;
newtouches[i] = GP2X_LEFT;
}
else if (MyCGRectContainsPoint(Right, point))
{
gp2x_pad_status |= GP2X_RIGHT;
newtouches[i] = GP2X_RIGHT;
}
else if (MyCGRectContainsPoint(Up, point))
{
gp2x_pad_status |= GP2X_UP;
newtouches[i] = GP2X_UP;
}
else if (MyCGRectContainsPoint(Down, point))
{
gp2x_pad_status |= GP2X_DOWN;
newtouches[i] = GP2X_DOWN;
}
else if (MyCGRectContainsPoint(ButtonLeft, point))
{
gp2x_pad_status |= GP2X_A;
newtouches[i] = GP2X_A;
}
else if (MyCGRectContainsPoint(ButtonRight, point))
{
gp2x_pad_status |= GP2X_B;
newtouches[i] = GP2X_B;
}
else if (MyCGRectContainsPoint(ButtonUp, point))
{
gp2x_pad_status |= GP2X_Y;
newtouches[i] = GP2X_Y;
}
else if (MyCGRectContainsPoint(ButtonDown, point))
{
gp2x_pad_status |= GP2X_X;
newtouches[i] = GP2X_X;
}
else if (MyCGRectContainsPoint(ButtonUpLeft, point))
{
gp2x_pad_status |= GP2X_A | GP2X_Y;
newtouches[i] = GP2X_A | GP2X_Y;
}
else if (MyCGRectContainsPoint(ButtonDownLeft, point))
{
gp2x_pad_status |= GP2X_X | GP2X_A;
newtouches[i] = GP2X_X | GP2X_A;
}
else if (MyCGRectContainsPoint(ButtonUpRight, point))
{
gp2x_pad_status |= GP2X_B | GP2X_Y;
newtouches[i] = GP2X_B | GP2X_Y;
}
else if (MyCGRectContainsPoint(ButtonDownRight, point))
{
gp2x_pad_status |= GP2X_X | GP2X_B;
newtouches[i] = GP2X_X | GP2X_B;
}
else if (MyCGRectContainsPoint(UpLeft, point))
{
gp2x_pad_status |= GP2X_UP | GP2X_LEFT;
newtouches[i] = GP2X_UP | GP2X_LEFT;
}
else if (MyCGRectContainsPoint(DownLeft, point))
{
gp2x_pad_status |= GP2X_DOWN | GP2X_LEFT;
newtouches[i] = GP2X_DOWN | GP2X_LEFT;
}
else if (MyCGRectContainsPoint(UpRight, point))
{
gp2x_pad_status |= GP2X_UP | GP2X_RIGHT;
newtouches[i] = GP2X_UP | GP2X_RIGHT;
}
else if (MyCGRectContainsPoint(DownRight, point))
{
gp2x_pad_status |= GP2X_DOWN | GP2X_RIGHT;
newtouches[i] = GP2X_DOWN | GP2X_RIGHT;
}
else if (MyCGRectContainsPoint(LPad, point))
{
gp2x_pad_status |= GP2X_L;
newtouches[i] = GP2X_L;
}
else if (MyCGRectContainsPoint(RPad, point))
{
gp2x_pad_status |= GP2X_R;
newtouches[i] = GP2X_R;
}
else if (MyCGRectContainsPoint(LPad2, point))
{
gp2x_pad_status |= GP2X_VOL_DOWN;
newtouches[i] = GP2X_VOL_DOWN;
}
else if (MyCGRectContainsPoint(RPad2, point))
{
gp2x_pad_status |= GP2X_VOL_UP;
newtouches[i] = GP2X_VOL_UP;
}
else if (MyCGRectContainsPoint(Select, point))
{
gp2x_pad_status |= GP2X_SELECT;
newtouches[i] = GP2X_SELECT;
}
else if (MyCGRectContainsPoint(Start, point))
{
gp2x_pad_status |= GP2X_START;
newtouches[i] = GP2X_START;
}
else if (MyCGRectContainsPoint(Menu, point))
{
if(touch.phase == UITouchPhaseBegan || touch.phase == UITouchPhaseStationary)
{
if(__emulation_run)
{
[NSThread detachNewThreadSelector:@selector(runMenu) toTarget:self withObject:nil];
}
else
{
[SOApp.delegate switchToBrowse];
[tabBar didMoveToWindow];
}
//return;
}
}
if(oldtouches[i] != newtouches[i])
{
gp2x_pad_status &= ~(oldtouches[i]);
}
}
}
for (i = 0; i < 10; i++)
{
if(touchstate[i] == 0)
{
gp2x_pad_status &= ~(newtouches[i]);
newtouches[i] = 0;
oldtouches[i] = 0;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
#endif
/*
- (void)view:(UIView *)view handleTapWithCount:(int)count event:(myGSEvent *)event {
//NSLog(@"handleTapWithCount: %d", count);
[self dumpEvent: event];
}
*/
/*
- (double)viewTouchPauseThreshold:(UIView *)view {
NSLog(@"TouchPause!");
return 0.0;
}
*/
#if 0
- (void)mouseEvent:(myGSEvent*)event {
int i;
int touchcount = event->fingerCount;
gp2x_pad_status = 0;
for (i = 0; i < touchcount; i++)
{
struct CGPoint point = CGPointMake(event->points[i].x, event->points[i].y);
if (MyCGRectContainsPoint(Left, point)) {
gp2x_pad_status |= GP2X_LEFT;
}
else if (MyCGRectContainsPoint(Right, point)) {
gp2x_pad_status |= GP2X_RIGHT;
}
else if (MyCGRectContainsPoint(Up, point)) {
gp2x_pad_status |= GP2X_UP;
}
else if (MyCGRectContainsPoint(Down, point)) {
gp2x_pad_status |= GP2X_DOWN;
}
else if (MyCGRectContainsPoint(A, point)) {
gp2x_pad_status |= GP2X_B;
}
else if (MyCGRectContainsPoint(B, point)) {
gp2x_pad_status |= GP2X_X;
}
else if (MyCGRectContainsPoint(AB, point)) {
gp2x_pad_status |= GP2X_B | GP2X_X;
}
else if (MyCGRectContainsPoint(UpLeft, point)) {
gp2x_pad_status |= GP2X_UP | GP2X_LEFT;
}
else if (MyCGRectContainsPoint(DownLeft, point)) {
gp2x_pad_status |= GP2X_DOWN | GP2X_LEFT;
}
else if (MyCGRectContainsPoint(UpRight, point)) {
gp2x_pad_status |= GP2X_UP | GP2X_RIGHT;
}
else if (MyCGRectContainsPoint(DownRight, point)) {
gp2x_pad_status |= GP2X_DOWN | GP2X_RIGHT;
}
else if (MyCGRectContainsPoint(LPad, point)) {
gp2x_pad_status |= GP2X_L;
}
else if (MyCGRectContainsPoint(RPad, point)) {
gp2x_pad_status |= GP2X_R;
}
else if (MyCGRectContainsPoint(Select, point)) {
gp2x_pad_status |= GP2X_SELECT;
}
else if (MyCGRectContainsPoint(Start, point)) {
gp2x_pad_status |= GP2X_START;
}
else if (MyCGRectContainsPoint(Menu, point)) {
//if(touch.phase == UITouchPhaseBegan || touch.phase == UITouchPhaseStationary)
{
[SOApp.delegate switchToBrowse];
[tabBar didMoveToWindow];
if(__emulation_run)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Save Game State" message:@"Save Game State?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Save Game!",@"Don't Save?", nil];
[alert show];
[alert release];
}
}
}
}
}
- (void)mouseDown:(myGSEvent*)event {
//NSLog(@"mouseDown:");
[self mouseEvent: event];
}
- (void)mouseDragged:(myGSEvent*)event {
//NSLog(@"mouseDragged:");
[self mouseEvent: event];
}
- (void)mouseEntered:(myGSEvent*)event {
//NSLog(@"mouseEntered:");
[self mouseEvent: event];
}
- (void)mouseExited:(myGSEvent*)event {
//NSLog(@"mouseExited:");
[self mouseEvent: event];
}
- (void)mouseMoved:(myGSEvent*)event {
//NSLog(@"mouseMoved:");
[self mouseEvent: event];
}
- (void)mouseUp:(myGSEvent*)event {
[self mouseEvent: event];
}
#endif
/*
- (BOOL)isFirstResponder {
return YES;
}
*/
- (void)getControllerCoords:(int)orientation {
char string[256];
FILE *fp;
if(!orientation)
{
fp = fopen([[NSString stringWithFormat:@"%scontroller_hs%d.txt", get_resource_path("/"), [SOApp.optionsView getCurrentSkin]] UTF8String], "r");
}
else
{
fp = fopen([[NSString stringWithFormat:@"%scontroller_fs%d.txt", get_resource_path("/"), [SOApp.optionsView getCurrentSkin]] UTF8String], "r");
}
if (fp)
{
int i = 0;