1 | /************************************************************************* |
---|
2 | * * |
---|
3 | * DrawStuff Library, Copyright (C) 2001 Russell L. Smith. * |
---|
4 | * Email: russ@q12.org Web: www.q12.org * |
---|
5 | * * |
---|
6 | * This library is free software; you can redistribute it and/or * |
---|
7 | * modify it under the terms of the GNU Lesser General Public * |
---|
8 | * License as published by the Free Software Foundation; either * |
---|
9 | * version 2.1 of the License, or (at your option) any later version. * |
---|
10 | * * |
---|
11 | * This library is distributed in the hope that it will be useful, * |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
14 | * Lesser General Public License for more details. * |
---|
15 | * * |
---|
16 | * You should have received a copy of the GNU Lesser General Public * |
---|
17 | * License along with this library (see the file LICENSE.TXT); if not, * |
---|
18 | * write to the Free Software Foundation, Inc., 59 Temple Place, * |
---|
19 | * Suite 330, Boston, MA 02111-1307 USA. * |
---|
20 | * * |
---|
21 | *************************************************************************/ |
---|
22 | |
---|
23 | // main window and event handling for Mac CFM Carbon |
---|
24 | |
---|
25 | #include <ode/config.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | #include <stdarg.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <glut.h> |
---|
31 | #include <SIOUX.h> |
---|
32 | |
---|
33 | #include <MacTypes.h> |
---|
34 | #include <Timer.h> |
---|
35 | |
---|
36 | #include <drawstuff/drawstuff.h> |
---|
37 | #include <drawstuff/version.h> |
---|
38 | #include "internal.h" |
---|
39 | |
---|
40 | |
---|
41 | //*************************************************************************** |
---|
42 | // error handling for unix (works just fine with SIOUX) |
---|
43 | |
---|
44 | static void printMessage (char *msg1, char *msg2, va_list ap) |
---|
45 | { |
---|
46 | fflush (stderr); |
---|
47 | fflush (stdout); |
---|
48 | fprintf (stderr,"\n%s: ",msg1); |
---|
49 | vfprintf (stderr,msg2,ap); |
---|
50 | fprintf (stderr,"\n"); |
---|
51 | fflush (stderr); |
---|
52 | } |
---|
53 | |
---|
54 | extern "C" void dsError (char *msg, ...) |
---|
55 | { |
---|
56 | va_list ap; |
---|
57 | va_start (ap,msg); |
---|
58 | printMessage ("Error",msg,ap); |
---|
59 | exit (1); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | extern "C" void dsDebug (char *msg, ...) |
---|
64 | { |
---|
65 | va_list ap; |
---|
66 | va_start (ap,msg); |
---|
67 | printMessage ("INTERNAL ERROR",msg,ap); |
---|
68 | // *((char *)0) = 0; ... commit SEGVicide ? |
---|
69 | abort(); |
---|
70 | } |
---|
71 | |
---|
72 | extern "C" void dsPrint (char *msg, ...) |
---|
73 | { |
---|
74 | va_list ap; |
---|
75 | va_start (ap,msg); |
---|
76 | vprintf (msg,ap); |
---|
77 | } |
---|
78 | |
---|
79 | //*************************************************************************** |
---|
80 | // openGL window |
---|
81 | |
---|
82 | // window and openGL |
---|
83 | static int width=0,height=0; // window size |
---|
84 | static int last_key_pressed=0; // last key pressed in the window |
---|
85 | static int pause=0; // 1 if in `pause' mode |
---|
86 | static int singlestep=0; // 1 if single step key pressed |
---|
87 | static int writeframes=0; // 1 if frame files to be written |
---|
88 | static dsFunctions *gfn; |
---|
89 | static int frame = 1; |
---|
90 | |
---|
91 | float getTime (void) |
---|
92 | { |
---|
93 | UnsignedWide ms; |
---|
94 | |
---|
95 | Microseconds(&ms); |
---|
96 | return ms.lo / 1000000.0; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | static void captureFrame (int num) |
---|
101 | { |
---|
102 | // TODO |
---|
103 | } |
---|
104 | |
---|
105 | static void reshape(int w, int h) |
---|
106 | { |
---|
107 | width = w; |
---|
108 | height = h; |
---|
109 | } |
---|
110 | |
---|
111 | static void draw(void) |
---|
112 | { |
---|
113 | dsDrawFrame (width,height,gfn,pause && !singlestep); |
---|
114 | singlestep = 0; |
---|
115 | glutSwapBuffers(); |
---|
116 | |
---|
117 | if (pause==0 && writeframes) { |
---|
118 | captureFrame (frame); |
---|
119 | frame++; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | static void idle(void) |
---|
124 | { |
---|
125 | static float lasttime=0; |
---|
126 | float t; |
---|
127 | |
---|
128 | // Try to maintain a reasonable rate (good enough for testing anyway) |
---|
129 | t = getTime(); |
---|
130 | if (lasttime < t) { |
---|
131 | lasttime = t+0.005; |
---|
132 | draw(); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | static void key(unsigned char key, int x, int y) |
---|
137 | { |
---|
138 | if (!glutGetModifiers()) { |
---|
139 | |
---|
140 | if (key >= ' ' && key <= 126 && gfn->command) gfn->command (key); |
---|
141 | |
---|
142 | // GLUT_ACTIVE_CTRL doesn't seem to be working, so we use Alt |
---|
143 | } else if (glutGetModifiers()&GLUT_ACTIVE_ALT) { |
---|
144 | |
---|
145 | switch (key) { |
---|
146 | case 't': case 'T': |
---|
147 | dsSetTextures (dsGetTextures() ^ 1); |
---|
148 | break; |
---|
149 | case 's': case 'S': |
---|
150 | dsSetShadows (dsGetShadows() ^ 1); |
---|
151 | break; |
---|
152 | case 'p': case 'P': |
---|
153 | pause ^= 1; |
---|
154 | singlestep = 0; |
---|
155 | break; |
---|
156 | case 'o': case 'O': |
---|
157 | if (pause) singlestep = 1; |
---|
158 | break; |
---|
159 | case 'v': case 'V': { |
---|
160 | float xyz[3],hpr[3]; |
---|
161 | dsGetViewpoint (xyz,hpr); |
---|
162 | printf ("Viewpoint = (%.4f,%.4f,%.4f,%.4f,%.4f,%.4f)\n", |
---|
163 | xyz[0],xyz[1],xyz[2],hpr[0],hpr[1],hpr[2]); |
---|
164 | break; |
---|
165 | } |
---|
166 | // No case 'X' - Quit works through the Mac system menu, or cmd-q |
---|
167 | case 'w': case 'W': |
---|
168 | writeframes ^= 1; |
---|
169 | if (writeframes) printf ("Write frames not done yet!\n");// TODO |
---|
170 | break; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | last_key_pressed = key; |
---|
175 | } |
---|
176 | |
---|
177 | static int mx=0,my=0; // mouse position |
---|
178 | static int mode = 0; // mouse button bits |
---|
179 | |
---|
180 | static void MouseDown(int button, int state, int x, int y) |
---|
181 | { |
---|
182 | if(button == GLUT_LEFT_BUTTON) |
---|
183 | { |
---|
184 | if(state == GLUT_DOWN) |
---|
185 | mode |= 1; |
---|
186 | else if(state == GLUT_UP) |
---|
187 | mode &= (~1); |
---|
188 | } |
---|
189 | else if (button == GLUT_MIDDLE_BUTTON) |
---|
190 | { |
---|
191 | if(state == GLUT_DOWN) |
---|
192 | mode |= 3; |
---|
193 | else if(state == GLUT_UP) |
---|
194 | mode &= (~3); |
---|
195 | } |
---|
196 | else if (button == GLUT_RIGHT_BUTTON) |
---|
197 | { |
---|
198 | if(state == GLUT_DOWN) |
---|
199 | mode |= 2; |
---|
200 | else if(state == GLUT_UP) |
---|
201 | mode &= (~2); |
---|
202 | } |
---|
203 | |
---|
204 | mx = x; |
---|
205 | my = y; |
---|
206 | } |
---|
207 | |
---|
208 | static void MouseMove(int x, int y) |
---|
209 | { |
---|
210 | dsMotion (mode, x - mx, y - my); |
---|
211 | mx = x; |
---|
212 | my = y; |
---|
213 | } |
---|
214 | |
---|
215 | static void createMainWindow (int _width, int _height) |
---|
216 | { |
---|
217 | // So GLUT doesn't complain |
---|
218 | int argc = 0; |
---|
219 | char **argv = NULL; |
---|
220 | |
---|
221 | // initialize variables |
---|
222 | width = _width; |
---|
223 | height = _height; |
---|
224 | last_key_pressed = 0; |
---|
225 | |
---|
226 | if (width < 1 || height < 1) dsDebug (0,"bad window width or height"); |
---|
227 | |
---|
228 | glutInit(&argc, argv); |
---|
229 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); |
---|
230 | glutInitWindowSize(_width, _height); |
---|
231 | glutInitWindowPosition(100, 100); |
---|
232 | glutCreateWindow("ODE Simulation"); |
---|
233 | |
---|
234 | glutKeyboardFunc(key); |
---|
235 | glutMotionFunc(MouseMove); |
---|
236 | glutMouseFunc(MouseDown); |
---|
237 | glutReshapeFunc(reshape); |
---|
238 | glutDisplayFunc(idle); |
---|
239 | glutIdleFunc(idle); |
---|
240 | } |
---|
241 | |
---|
242 | void dsPlatformSimLoop (int window_width, int window_height, dsFunctions *fn, |
---|
243 | int initial_pause) |
---|
244 | { |
---|
245 | SIOUXSettings.initializeTB = false; |
---|
246 | SIOUXSettings.standalone = false; |
---|
247 | SIOUXSettings.setupmenus = false; |
---|
248 | SIOUXSettings.autocloseonquit = true; |
---|
249 | SIOUXSettings.asktosaveonclose = false; |
---|
250 | |
---|
251 | gfn = fn; |
---|
252 | pause = initial_pause; |
---|
253 | |
---|
254 | printf ( |
---|
255 | "\n" |
---|
256 | "Simulation test environment v%d.%02d\n" |
---|
257 | " Option-P : pause / unpause (or say `-pause' on command line).\n" |
---|
258 | " Option-O : single step when paused.\n" |
---|
259 | " Option-T : toggle textures (or say `-notex' on command line).\n" |
---|
260 | " Option-S : toggle shadows (or say `-noshadow' on command line).\n" |
---|
261 | " Option-V : print current viewpoint coordinates (x,y,z,h,p,r).\n" |
---|
262 | " Option-W : write frames to ppm files: frame/frameNNN.ppm\n" |
---|
263 | "\n" |
---|
264 | "Change the camera position by clicking + dragging in the window.\n" |
---|
265 | " Left button - pan and tilt.\n" |
---|
266 | " Right button - forward and sideways.\n" |
---|
267 | " Left + Right button (or middle button) - sideways and up.\n" |
---|
268 | "\n",DS_VERSION >> 8,DS_VERSION & 0xff); |
---|
269 | |
---|
270 | createMainWindow (window_width, window_height); |
---|
271 | dsStartGraphics (window_width,window_height,fn); |
---|
272 | |
---|
273 | if (fn->start) fn->start(); |
---|
274 | |
---|
275 | glutMainLoop(); |
---|
276 | |
---|
277 | if (fn->stop) fn->stop(); |
---|
278 | dsStopGraphics(); |
---|
279 | } |
---|
280 | |
---|
281 | extern "C" void dsStop(){ }// GLUT/MSL hooks into the system to exit |
---|