-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathController.m
More file actions
258 lines (199 loc) · 6.21 KB
/
Copy pathController.m
File metadata and controls
258 lines (199 loc) · 6.21 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
/*
KOSX
Controller.m
Copyright (C)2005 Dan Potter
*/
// Most of this really ought to be moved into KOSX itself...
#import "Controller.h"
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
#import <OpenGL/glext.h>
#include <unistd.h>
#include <maple/MapleEventReceiver.h>
@interface Controller (PrivateAPI)
- (NSOpenGLPixelFormat *) _createPixelFormat;
- (void)glThread;
+ (void)glThreadStatic: (id)obj;
@end
#include <plx/port.h>
#include <unistd.h>
#include <sys/time.h>
void plx_main();
void plx_wait_ready();
static volatile NSOpenGLContext * g_view = NULL;
static NSWindow * g_wnd = NULL;
static int frames = 0;
void waitgview() {
while (!g_view) {
NSLog(@"waiting for view...");
usleep(1000*1000);
}
}
void plx_scene_begin_hook() {
waitgview();
[g_view makeCurrentContext];
}
void plx_scene_finish_hook() {
waitgview();
[g_view flushBuffer];
}
static struct timeval lastFrame = { 0,0 };
// This makes sure we have a GL visual available and frame limits it to 60fps
// if needed.
void plx_wait_ready() {
waitgview();
if (lastFrame.tv_sec == 0) {
gettimeofday(&lastFrame, NULL);
return;
}
struct timeval now;
gettimeofday(&now, NULL);
long long nowu = ((long long)now.tv_sec) * 1000 * 1000 + now.tv_usec;
long long lastu = ((long long)lastFrame.tv_sec) * 1000 * 1000 + lastFrame.tv_usec;
long long diffu = nowu - lastu;
if (diffu < (1000*1000 / 60)) {
usleep((1000*1000 / 60) - diffu);
}
gettimeofday(&lastFrame, NULL);
frames++;
}
@implementation Controller
- (void) applicationDidFinishLaunching: (NSNotification *) note
{
// Put your desired width/height here.
width = 640;
height = 480;
// You can query the display for its current depth and use that, but I
// find that you get better results by just bumping it up to 32/32
// right away.
/*
NSDictionary * originalDisplayMode = (NSDictionary *)
CGDisplayCurrentMode(kCGDirectMainDisplay);
depthBits = colorBits = [[originalDisplayMode objectForKey: (id)kCGDisplayBitsPerPixel] intValue];
NSDictionary * displayMode = (NSDictionary *)CGDisplayBestModeForParameters(
kCGDirectMainDisplay, colorBits, width, height, NULL);
*/
depthBits = colorBits = 32;
NSOpenGLPixelFormat * pixelFormat = [self _createPixelFormat];
NSOpenGLContext * context = [[NSOpenGLContext alloc]
initWithFormat: pixelFormat shareContext: nil];
[pixelFormat release];
NSRect rect = NSMakeRect(0, 0, width, height);
NSWindow * window = [[NSWindow alloc]
initWithContentRect:rect
styleMask:NSTitledWindowMask
| NSTexturedBackgroundWindowMask // Not compatible with Jaguar 10.2.x
| NSClosableWindowMask
| NSMiniaturizableWindowMask
| NSResizableWindowMask
backing:NSBackingStoreRetained
defer:NO];
[window setTitle: @"KOSX Test Program"];
[window makeKeyAndOrderFront:nil];
[window setHasShadow:YES];
g_wnd = window;
g_view = context;
// Attach the GL context to the content of the window
[context setView: [window contentView]];
// Make the context we created be the current GL context
[context makeCurrentContext];
// Set the refresh sync on the context (or not)
{
// Get the underlying CGL context
CGLContextObj cglContext = CGLGetCurrentContext();
long param = 0;
CGLSetParameter(cglContext, kCGLCPSwapInterval, ¶m);
}
plx_set_scene_hooks(plx_scene_begin_hook, plx_scene_finish_hook);
[MapleEventReceiver init];
[NSThread detachNewThreadSelector:@selector(glThreadStatic:)
toTarget:[Controller class] withObject:self];
NSDate * distantPast = [[NSDate distantPast] retain];
NSDate * distantFuture = [[NSDate distantFuture] retain];
BOOL done = NO;
NSAutoreleasePool * pool = nil;
unsigned int kbNewFlags, kbPreviousFlags = 0, kbChanged, down;
// Process events
while (!done) {
NSEvent * event;
NSEventType type;
pool = [[NSAutoreleasePool alloc] init];
// This polls for an event. If there isn't an event read immediately, it will return nil. In a real game, we would loop until we had processed all pending events and would then go about our normal game processing
event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantFuture inMode:NSDefaultRunLoopMode dequeue:YES];
if (!event) {
[pool release];
pool = nil;
// We'd break out of this loop instead of continuing in a real game (as mentioned above).
continue;
}
// Handle the event we got
type = [event type];
switch (type) {
case NSKeyDown:
case NSKeyUp:
// Ignore key repeats (we don't turn them off because doing
// so is risky in case of a program crash).
if ([event isARepeat])
break;
// If Command or Ctrl is down, pass this on to NSApp.
if (([event modifierFlags] & NSCommandKeyMask) ||
([event modifierFlags] & NSControlKeyMask))
{
[NSApp sendEvent: event];
break;
}
[MapleEventReceiver processEvent: event];
// NSLog([NSString stringWithFormat: @"%@", event]);
break;
case NSFlagsChanged:
kbNewFlags = [event modifierFlags];
kbChanged = kbNewFlags ^ kbPreviousFlags;
/* if (kbChanged & NSControlKeyMask) {
down = kbNewFlags & NSControlKeyMask;
}
if (kbChanged & NSCommandKeyMask) {
down = kbNewFlags & NSCommandKeyMask;
} */
kbPreviousFlags = kbNewFlags;
break;
}
}
[MapleEventReceiver shutdown];
if (pool)
[pool release];
}
@end
@implementation Controller (PrivateAPI)
- (void)glThread
{
// We need our own autorelease pool for Cocoa.
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"glThread started");
// Make sure we've selected our context and have a view.
plx_wait_ready();
plx_scene_begin_hook();
plx_scene_finish_hook();
plx_main();
NSLog(@"glThread exiting");
[pool release];
}
+ (void)glThreadStatic:(id)anObject
{
Controller * us = (Controller *)anObject;
[us glThread];
}
- (NSOpenGLPixelFormat *) _createPixelFormat;
{
NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFAColorSize, colorBits,
NSOpenGLPFADepthSize, depthBits,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
// NSOpenGLPFAFullScreen,
0
};
return [[NSOpenGLPixelFormat alloc] initWithAttributes: attributes];
}
@end