aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ode-0.9/drawstuff/src/x11.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ode-0.9/drawstuff/src/x11.cpp')
-rw-r--r--libraries/ode-0.9/drawstuff/src/x11.cpp418
1 files changed, 418 insertions, 0 deletions
diff --git a/libraries/ode-0.9/drawstuff/src/x11.cpp b/libraries/ode-0.9/drawstuff/src/x11.cpp
new file mode 100644
index 0000000..639ce7b
--- /dev/null
+++ b/libraries/ode-0.9/drawstuff/src/x11.cpp
@@ -0,0 +1,418 @@
1/*************************************************************************
2 * *
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. 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 EITHER: *
8 * (1) The GNU Lesser General Public License as published by the Free *
9 * Software Foundation; either version 2.1 of the License, or (at *
10 * your option) any later version. The text of the GNU Lesser *
11 * General Public License is included with this library in the *
12 * file LICENSE.TXT. *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
15 * *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
20 * *
21 *************************************************************************/
22
23// main window and event handling for X11
24
25#include <ode/config.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdarg.h>
29#include <X11/Xlib.h>
30#include <X11/Xatom.h>
31#include <X11/keysym.h>
32#include <GL/glx.h>
33
34#ifdef HAVE_SYS_TIME_H
35#include <sys/time.h>
36#endif
37
38#include <drawstuff/drawstuff.h>
39#include <drawstuff/version.h>
40#include "internal.h"
41
42//***************************************************************************
43// error handling for unix
44
45static void printMessage (char *msg1, char *msg2, va_list ap)
46{
47 fflush (stderr);
48 fflush (stdout);
49 fprintf (stderr,"\n%s: ",msg1);
50 vfprintf (stderr,msg2,ap);
51 fprintf (stderr,"\n");
52 fflush (stderr);
53}
54
55
56extern "C" void dsError (char *msg, ...)
57{
58 va_list ap;
59 va_start (ap,msg);
60 printMessage ("Error",msg,ap);
61 exit (1);
62}
63
64
65extern "C" void dsDebug (char *msg, ...)
66{
67 va_list ap;
68 va_start (ap,msg);
69 printMessage ("INTERNAL ERROR",msg,ap);
70 // *((char *)0) = 0; ... commit SEGVicide ?
71 abort();
72}
73
74
75extern "C" void dsPrint (char *msg, ...)
76{
77 va_list ap;
78 va_start (ap,msg);
79 vprintf (msg,ap);
80}
81
82//***************************************************************************
83// openGL window
84
85// X11 display info
86static Display *display=0;
87static int screen=0;
88static XVisualInfo *visual=0; // best visual for openGL
89static Colormap colormap=0; // window's colormap
90static Atom wm_protocols_atom = 0;
91static Atom wm_delete_window_atom = 0;
92
93// window and openGL
94static Window win=0; // X11 window, 0 if not initialized
95static int width=0,height=0; // window size
96static GLXContext glx_context=0; // openGL rendering context
97static int last_key_pressed=0; // last key pressed in the window
98static int run=1; // 1 if simulation running
99static int pause=0; // 1 if in `pause' mode
100static int singlestep=0; // 1 if single step key pressed
101static int writeframes=0; // 1 if frame files to be written
102
103
104static void createMainWindow (int _width, int _height)
105{
106 // create X11 display connection
107 display = XOpenDisplay (NULL);
108 if (!display) dsError ("can not open X11 display");
109 screen = DefaultScreen(display);
110
111 // get GL visual
112 static int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,16,
113 GLX_RED_SIZE,4, GLX_GREEN_SIZE,4,
114 GLX_BLUE_SIZE,4, None};
115 visual = glXChooseVisual (display,screen,attribList);
116 if (!visual) dsError ("no good X11 visual found for OpenGL");
117
118 // create colormap
119 colormap = XCreateColormap (display,RootWindow(display,screen),
120 visual->visual,AllocNone);
121
122 // initialize variables
123 win = 0;
124 width = _width;
125 height = _height;
126 glx_context = 0;
127 last_key_pressed = 0;
128
129 if (width < 1 || height < 1) dsDebug (0,"bad window width or height");
130
131 // create the window
132 XSetWindowAttributes attributes;
133 attributes.background_pixel = BlackPixel(display,screen);
134 attributes.colormap = colormap;
135 attributes.event_mask = ButtonPressMask | ButtonReleaseMask |
136 KeyPressMask | KeyReleaseMask | ButtonMotionMask | PointerMotionHintMask |
137 StructureNotifyMask;
138 win = XCreateWindow (display,RootWindow(display,screen),50,50,width,height,
139 0,visual->depth, InputOutput,visual->visual,
140 CWBackPixel | CWColormap | CWEventMask,&attributes);
141
142 // associate a GLX context with the window
143 glx_context = glXCreateContext (display,visual,0,GL_TRUE);
144 if (!glx_context) dsError ("can't make an OpenGL context");
145
146 // set the window title
147 XTextProperty window_name;
148 window_name.value = (unsigned char *) "Simulation";
149 window_name.encoding = XA_STRING;
150 window_name.format = 8;
151 window_name.nitems = strlen((char *) window_name.value);
152 XSetWMName (display,win,&window_name);
153
154 // participate in the window manager 'delete yourself' protocol
155 wm_protocols_atom = XInternAtom (display,"WM_PROTOCOLS",False);
156 wm_delete_window_atom = XInternAtom (display,"WM_DELETE_WINDOW",False);
157 if (XSetWMProtocols (display,win,&wm_delete_window_atom,1)==0)
158 dsError ("XSetWMProtocols() call failed");
159
160 // pop up the window
161 XMapWindow (display,win);
162 XSync (display,win);
163}
164
165
166static void destroyMainWindow()
167{
168 glXDestroyContext (display,glx_context);
169 XDestroyWindow (display,win);
170 XSync (display,0);
171 XCloseDisplay(display);
172 display = 0;
173 win = 0;
174 glx_context = 0;
175}
176
177
178static void handleEvent (XEvent &event, dsFunctions *fn)
179{
180 static int mx=0,my=0; // mouse position
181 static int mode = 0; // mouse button bits
182
183 switch (event.type) {
184
185 case ButtonPress: {
186 if (event.xbutton.button == Button1) mode |= 1;
187 if (event.xbutton.button == Button2) mode |= 2;
188 if (event.xbutton.button == Button3) mode |= 4;
189 mx = event.xbutton.x;
190 my = event.xbutton.y;
191 }
192 return;
193
194 case ButtonRelease: {
195 if (event.xbutton.button == Button1) mode &= (~1);
196 if (event.xbutton.button == Button2) mode &= (~2);
197 if (event.xbutton.button == Button3) mode &= (~4);
198 mx = event.xbutton.x;
199 my = event.xbutton.x;
200 }
201 return;
202
203 case MotionNotify: {
204 if (event.xmotion.is_hint) {
205 Window root,child;
206 unsigned int mask;
207 XQueryPointer (display,win,&root,&child,&event.xbutton.x_root,
208 &event.xbutton.y_root,&event.xbutton.x,&event.xbutton.y,
209 &mask);
210 }
211 dsMotion (mode, event.xmotion.x - mx, event.xmotion.y - my);
212 mx = event.xmotion.x;
213 my = event.xmotion.y;
214 }
215 return;
216
217 case KeyPress: {
218 KeySym key;
219 XLookupString (&event.xkey,NULL,0,&key,0);
220 if ((event.xkey.state & ControlMask) == 0) {
221 if (key >= ' ' && key <= 126 && fn->command) fn->command (key);
222 }
223 else if (event.xkey.state & ControlMask) {
224 switch (key) {
225 case 't': case 'T':
226 dsSetTextures (dsGetTextures() ^ 1);
227 break;
228 case 's': case 'S':
229 dsSetShadows (dsGetShadows() ^ 1);
230 break;
231 case 'x': case 'X':
232 run = 0;
233 break;
234 case 'p': case 'P':
235 pause ^= 1;
236 singlestep = 0;
237 break;
238 case 'o': case 'O':
239 if (pause) singlestep = 1;
240 break;
241 case 'v': case 'V': {
242 float xyz[3],hpr[3];
243 dsGetViewpoint (xyz,hpr);
244 printf ("Viewpoint = (%.4f,%.4f,%.4f,%.4f,%.4f,%.4f)\n",
245 xyz[0],xyz[1],xyz[2],hpr[0],hpr[1],hpr[2]);
246 break;
247 }
248 case 'w': case 'W':
249 writeframes ^= 1;
250 if (writeframes) printf ("Now writing frames to PPM files\n");
251 break;
252 }
253 }
254 last_key_pressed = key; // a kludgy place to put this...
255 }
256 return;
257
258 case KeyRelease: {
259 // hmmmm...
260 }
261 return;
262
263 case ClientMessage:
264 if (event.xclient.message_type == wm_protocols_atom &&
265 event.xclient.format == 32 &&
266 Atom(event.xclient.data.l[0]) == wm_delete_window_atom) {
267 run = 0;
268 return;
269 }
270 return;
271
272 case ConfigureNotify:
273 width = event.xconfigure.width;
274 height = event.xconfigure.height;
275 return;
276 }
277}
278
279
280// return the index of the highest bit
281static int getHighBitIndex (unsigned int x)
282{
283 int i = 0;
284 while (x) {
285 i++;
286 x >>= 1;
287 }
288 return i-1;
289}
290
291
292// shift x left by i, where i can be positive or negative
293#define SHIFTL(x,i) (((i) >= 0) ? ((x) << (i)) : ((x) >> (-i)))
294
295
296static void captureFrame (int num)
297{
298 fprintf (stderr,"capturing frame %04d\n",num);
299
300 char s[100];
301 sprintf (s,"frame/frame%04d.ppm",num);
302 FILE *f = fopen (s,"wb");
303 if (!f) dsError ("can't open \"%s\" for writing",s);
304 fprintf (f,"P6\n%d %d\n255\n",width,height);
305 XImage *image = XGetImage (display,win,0,0,width,height,~0,ZPixmap);
306
307 int rshift = 7 - getHighBitIndex (image->red_mask);
308 int gshift = 7 - getHighBitIndex (image->green_mask);
309 int bshift = 7 - getHighBitIndex (image->blue_mask);
310
311 for (int y=0; y<height; y++) {
312 for (int x=0; x<width; x++) {
313 unsigned long pixel = XGetPixel (image,x,y);
314 unsigned char b[3];
315 b[0] = SHIFTL(pixel & image->red_mask,rshift);
316 b[1] = SHIFTL(pixel & image->green_mask,gshift);
317 b[2] = SHIFTL(pixel & image->blue_mask,bshift);
318 fwrite (b,3,1,f);
319 }
320 }
321 fclose (f);
322 XDestroyImage (image);
323}
324
325
326void dsPlatformSimLoop (int window_width, int window_height, dsFunctions *fn,
327 int initial_pause)
328{
329 pause = initial_pause;
330 createMainWindow (window_width, window_height);
331 glXMakeCurrent (display,win,glx_context);
332
333 dsStartGraphics (window_width,window_height,fn);
334
335 static bool firsttime=true;
336 if (firsttime)
337 {
338 fprintf
339 (
340 stderr,
341 "\n"
342 "Simulation test environment v%d.%02d\n"
343 " Ctrl-P : pause / unpause (or say `-pause' on command line).\n"
344 " Ctrl-O : single step when paused.\n"
345 " Ctrl-T : toggle textures (or say `-notex' on command line).\n"
346 " Ctrl-S : toggle shadows (or say `-noshadow' on command line).\n"
347 " Ctrl-V : print current viewpoint coordinates (x,y,z,h,p,r).\n"
348 " Ctrl-W : write frames to ppm files: frame/frameNNN.ppm\n"
349 " Ctrl-X : exit.\n"
350 "\n"
351 "Change the camera position by clicking + dragging in the window.\n"
352 " Left button - pan and tilt.\n"
353 " Right button - forward and sideways.\n"
354 " Left + Right button (or middle button) - sideways and up.\n"
355 "\n",DS_VERSION >> 8,DS_VERSION & 0xff
356 );
357 firsttime = false;
358 }
359
360 if (fn->start) fn->start();
361
362 int frame = 1;
363 run = 1;
364 while (run) {
365 // read in and process all pending events for the main window
366 XEvent event;
367 while (run && XPending (display)) {
368 XNextEvent (display,&event);
369 handleEvent (event,fn);
370 }
371
372 dsDrawFrame (width,height,fn,pause && !singlestep);
373 singlestep = 0;
374
375 glFlush();
376 glXSwapBuffers (display,win);
377 XSync (display,0);
378
379 // capture frames if necessary
380 if (pause==0 && writeframes) {
381 captureFrame (frame);
382 frame++;
383 }
384 };
385
386 if (fn->stop) fn->stop();
387 dsStopGraphics();
388
389 destroyMainWindow();
390}
391
392
393extern "C" void dsStop()
394{
395 run = 0;
396}
397
398
399extern "C" double dsElapsedTime()
400{
401#if HAVE_GETTIMEOFDAY
402 static double prev=0.0;
403 timeval tv ;
404
405 gettimeofday(&tv, 0);
406 double curr = tv.tv_sec + (double) tv.tv_usec / 1000000.0 ;
407 if (!prev)
408 prev=curr;
409 double retval = curr-prev;
410 prev=curr;
411 if (retval>1.0) retval=1.0;
412 if (retval<dEpsilon) retval=dEpsilon;
413 return retval;
414#else
415 return 0.01666; // Assume 60 fps
416#endif
417}
418