-
Notifications
You must be signed in to change notification settings - Fork 0
/
osxmain.cpp
187 lines (146 loc) · 5.2 KB
/
osxmain.cpp
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
#include "include.h"
#ifdef __MACH__
//eliminate these globals
AGLContext aglContext;
int WindowVisible;
Engine altEngine;
void DisplayFunc(void);
static pascal OSStatus EventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
void IdleFunc(void)
{
QuitApplicationEventLoop();
}
int main(int argc, char *argv[])
{
AGLPixelFormat format; /* OpenGL pixel format */
WindowPtr window; /* Window */
int winattrs; /* Window attributes */
Str255 title; /* Title of window */
Rect rect; /* Rectangle definition */
EventHandlerUPP handler; /* Event handler */
EventLoopTimerUPP thandler; /* Timer handler */
EventLoopTimerRef timer; /* Timer for animating the window */
ProcessSerialNumber psn; /* Process serial number */
static EventTypeSpec events[] = /* Events we are interested in... */
{
{ kEventClassMouse, kEventMouseDown },
{ kEventClassMouse, kEventMouseUp },
{ kEventClassMouse, kEventMouseMoved },
{ kEventClassMouse, kEventMouseDragged },
{ kEventClassWindow, kEventWindowDrawContent },
{ kEventClassWindow, kEventWindowShown },
{ kEventClassWindow, kEventWindowHidden },
{ kEventClassWindow, kEventWindowActivated },
{ kEventClassWindow, kEventWindowDeactivated },
{ kEventClassWindow, kEventWindowClose },
{ kEventClassWindow, kEventWindowBoundsChanged },
{ kCoreEventClass, kAEOpenApplication }
};
static GLint attributes[] = /* OpenGL attributes */
{
AGL_RGBA,
AGL_GREEN_SIZE, 1,
AGL_DOUBLEBUFFER,
AGL_DEPTH_SIZE, 16,
AGL_NONE
};
//Set initial values for window
const int origWinHeight = 628;
const int origWinWidth = 850;
const int origWinXOffset = 50;
const int origWinYOffset = 50;
// Create the window...
aglContext = 0;
WindowVisible = 0;
SetRect(&rect, origWinXOffset, origWinYOffset, origWinWidth, origWinHeight);
winattrs = kWindowStandardHandlerAttribute | kWindowCloseBoxAttribute |
kWindowCollapseBoxAttribute | kWindowFullZoomAttribute |
kWindowResizableAttribute | kWindowLiveResizeAttribute;
winattrs &= GetAvailableWindowAttributes(kDocumentWindowClass);
strcpy((char *)(title + 1), "Rigid Body Dynamics");
title[0] = strlen((char *)(title + 1));
CreateNewWindow(kDocumentWindowClass, winattrs, &rect, &window);
SetWTitle(window, title);
handler = NewEventHandlerUPP(EventHandler);
InstallWindowEventHandler(window, handler, sizeof(events) / sizeof(events[0]), events, NULL, 0L);
thandler = NewEventLoopTimerUPP((void (*)(EventLoopTimerRef, void *))IdleFunc);
InstallEventLoopTimer(GetMainEventLoop(), 0, 0, thandler, 0, &timer);
GetCurrentProcess(&psn);
SetFrontProcess(&psn);
DrawGrowIcon(window);
ShowWindow(window);
// Create the OpenGL context and bind it to the window...
format = aglChoosePixelFormat(NULL, 0, attributes);
aglContext = aglCreateContext(format, NULL);
aglSetCurrentContext(aglContext);
if (aglContext == NULL)
{
printf("Unable to create OpenGL context.\n");
return 1;
}
aglDestroyPixelFormat(format);
aglSetDrawable(aglContext, GetWindowPort(window));
// Set the initial size of the cube
altEngine.init((void *)&window, (void *)&aglContext);
altEngine.resize(origWinWidth - origWinXOffset, origWinHeight - origWinYOffset);
for (;;)
{
if (WindowVisible)
SetEventLoopTimerNextFireTime(timer, 0.05);
RunApplicationEventLoop();
if (WindowVisible)
{
altEngine.step();
//render frame, must pass a message to event handler
altEngine.render();
}
}
}
static pascal OSStatus EventHandler(
EventHandlerCallRef nextHandler, /* I - Next handler to call */
EventRef event, /* I - Event reference */
void *userData) /* I - User data (not used) */
{
UInt32 kind; /* Kind of event */
Rect rect; /* New window size */
EventMouseButton button; /* Mouse button */
Point point; /* Mouse position */
kind = GetEventKind(event);
switch (kind)
{
case kEventWindowBoundsChanged:
GetEventParameter(event, kEventParamCurrentBounds, typeQDRectangle, NULL, sizeof(Rect), NULL, &rect);
if (aglContext)
aglUpdateContext(aglContext);
altEngine.resize(rect.right - rect.left, rect.bottom - rect.top);
break;
case kEventWindowShown:
WindowVisible = 1;
break;
case kEventWindowHidden:
WindowVisible = 0;
break;
case kEventMouseDown:
GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(EventMouseButton), NULL, &button);
GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point);
//MouseFunc(button, 0, point.h, point.v);
break;
case kEventMouseUp:
GetEventParameter(event, kEventParamMouseButton, typeMouseButton, NULL, sizeof(EventMouseButton), NULL, &button);
GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point);
//MouseFunc(button, 1, point.h, point.v);
break;
case kEventMouseDragged:
GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &point);
//MotionFunc(point.h, point.v);
break;
case kEventWindowClose:
altEngine.destroy();
ExitToShell();
break;
default:
return CallNextEventHandler(nextHandler, event);
}
return noErr;
}
#endif