/
ViewController.mm
185 lines (142 loc) · 4.7 KB
/
ViewController.mm
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
#import "ViewController.h"
#include <AudioToolbox/AudioQueue.h>
#include <AudioToolbox/AudioFile.h>
#include <AudioToolbox/AudioServices.h>
#include <AudioToolbox/AudioConverter.h>
#include <AudioToolbox/AudioToolbox.h>
#include "game.h"
@interface ViewController ()
@property (strong, nonatomic) EAGLContext *context;
@end
@implementation ViewController
@synthesize context = _context;
#include <mach/mach_time.h>
bool osJoyReady(int index) {
return false;
}
void osJoyVibrate(int index, float L, float R) {
// TODO
}
int osGetTimeMS() {
const int64_t kOneMillion = 1000 * 1000;
static mach_timebase_info_data_t info;
if (info.denom == 0)
mach_timebase_info(&info);
return (int)((mach_absolute_time() * info.numer) / (kOneMillion * info.denom));
}
bool osSave(const char *name, const void *data, int size) {
return false;
}
#define SND_BUF_SIZE 8192
static AudioQueueRef audioQueue;
void soundFill(void* inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
void* frames = inBuffer->mAudioData;
UInt32 count = inBuffer->mAudioDataBytesCapacity / 4;
Sound::fill((Sound::Frame*)frames, count);
inBuffer->mAudioDataByteSize = count * 4;
AudioQueueEnqueueBuffer(audioQueue, inBuffer, 0, NULL);
}
void soundInit() {
AudioStreamBasicDescription deviceFormat;
deviceFormat.mSampleRate = 44100;
deviceFormat.mFormatID = kAudioFormatLinearPCM;
deviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
deviceFormat.mBytesPerPacket = 4;
deviceFormat.mFramesPerPacket = 1;
deviceFormat.mBytesPerFrame = 4;
deviceFormat.mChannelsPerFrame = 2;
deviceFormat.mBitsPerChannel = 16;
deviceFormat.mReserved = 0;
AudioQueueNewOutput(&deviceFormat, soundFill, NULL, NULL, kCFRunLoopCommonModes, 0, &audioQueue);
for (int i = 0; i < 2; i++) {
AudioQueueBufferRef mBuffer;
AudioQueueAllocateBuffer(audioQueue, SND_BUF_SIZE, &mBuffer);
soundFill(NULL, audioQueue, mBuffer);
}
AudioQueueStart(audioQueue, NULL);
}
- (id)init
{
self = [super init];
[[NSMutableDictionary alloc] init];
m_touchList = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++)
[m_touchList addObject:[NSNull null]];
return self;
}
- (void)dealloc
{
AudioQueueDispose(audioQueue, true);
[m_touchList release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
NSLog(@"! core: failed to create ES context");
return;
}
self.preferredFramesPerSecond = 60;
GLKView *view = (GLKView *)self.view;
view.multipleTouchEnabled = YES;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat16;
view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
[EAGLContext setCurrentContext:self.context];
cacheDir[0] = saveDir[0] = contentDir[0] = 0;
NSString *path = [[NSBundle mainBundle] resourcePath];
strcat(contentDir, [path UTF8String]);
strcat(contentDir, "/");
Game::init();
soundInit();
Input::reset();
}
- (void)viewDidUnload {
[super viewDidUnload];
Game::deinit();
if ([EAGLContext currentContext] == self.context)
[EAGLContext setCurrentContext:nil];
self.context = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
[((GLKView *) self.view) bindDrawable];
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&GAPI::defaultFBO);
float scale = [[UIScreen mainScreen] scale];
Core::width = self.view.bounds.size.width * scale;
Core::height = self.view.bounds.size.height * scale;
Game::update();
Game::render();
}
- (void) doTouch:(UIEvent*)event {
float scale = [[UIScreen mainScreen] scale];
NSSet* touchSet = [event allTouches];
for (UITouch *touch in touchSet) {
if (touch.phase == UITouchPhaseStationary)
continue;
CGPoint pos = [touch locationInView:self.view];
NSUInteger id = (uintptr_t)touch;
InputKey key = Input::getTouch(id);
if (key == ikNone) return;
Input::setPos(key, vec2(pos.x, pos.y) * scale);
if (touch.phase != UITouchPhaseMoved) {
Input::setDown(key, touch.phase == UITouchPhaseBegan);
}
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self doTouch:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self doTouch:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self doTouch:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self doTouch:event];
}
@end