changed: gcc8 base update
[opensg.git] / Examples / Simple / clusterserverx.cpp
blobe8d10b3d7a0fbad2fe5b1607d627a94df3181376
1 // OpenSG Tutorial Example: Cluster Server
2 //
3 // This is a full functional OpenSG cluster server. In OpenSG
4 // the terms server and client are used similar to X11. The
5 // application is the client. Instances that are used for
6 // rendering are called server.
7 //
8 // See the clusterclient.cpp for an example of how to use them.
9 //
10 // Libs: Cluster WindowX
12 #ifndef WIN32
14 #ifdef OSG_BUILD_ACTIVE
15 // General OpenSG configuration, needed everywhere
16 #include <OSGConfig.h>
17 #include <OSGGL.h>
18 #else
19 // General OpenSG configuration, needed everywhere
20 #include <OpenSG/OSGConfig.h>
21 #include <OpenSG/OSGGL.h>
22 #endif
24 #include <iostream>
25 #include <GL/glx.h>
27 #ifdef OSG_BUILD_ACTIVE
28 // The Cluster server definition
29 #include <OSGClusterServer.h>
30 // The GLUT-OpenSG connection class
31 #include <OSGXWindow.h>
32 // Render action definition.
33 #include <OSGRenderAction.h>
34 #else
35 // The Cluster server definition
36 #include <OpenSG/OSGClusterServer.h>
37 // The GLUT-OpenSG connection class
38 #include <OpenSG/OSGXWindow.h>
39 // Render action definition.
40 #include <OpenSG/OSGRenderAction.h>
41 #endif
43 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
44 // CDash balks even if they're just warnings... :(
45 #pragma GCC diagnostic ignored "-Wold-style-cast"
46 #endif
48 // local glut window
49 OSG::XWindowRefPtr window;
50 // render action
51 OSG::RenderActionRefPtr ract = NULL;
52 // pointer the the cluster server instance
53 OSG::ClusterServer *server = NULL;
54 bool exitOnError = false;
56 // forward declaration so we can have the interesting stuff upfront
57 void display();
58 void reshape( int width, int height );
60 int wait_for_map_notify(Display *, XEvent *event, char *arg)
62 return( event->type == MapNotify &&
63 event->xmap.window == ::Window(arg));
66 // Initialize GLUT & OpenSG and start the cluster server
67 int main(int argc,char **argv)
69 const char *name = "ClusterServer";
70 const char *connectionType = "StreamSock";
71 bool fullscreen = true;
72 std::string address = "";
73 bool doStereo = false;
74 std::string serviceGroup = "224.245.211.234";
77 // evaluate params
78 for(int a=1 ; a<argc ; ++a)
80 if(argv[a][0] == '-')
82 switch(argv[a][1])
84 case 'm':
85 connectionType="Multicast";
86 break;
88 case 's':
89 doStereo=true;
90 break;
92 case 'w':
93 fullscreen=false;
94 break;
96 case 'e':
97 exitOnError=true;
98 break;
100 case 'a': address = argv[a][2] ? argv[a]+2 : argv[++a];
101 if(address == argv[argc])
103 SLOG << "address missing" << OSG::endLog;
104 return 0;
106 std::cout << address << OSG::endLog;
107 break;
110 case 'j':
111 if(argv[a][2] != '\0')
112 serviceGroup=argv[a]+2;
113 else
114 serviceGroup=argv[++a];
115 break;
117 case 'h':
118 default:
119 std::cout << argv[0]
120 << "-m "
121 << "-s "
122 << "-w "
123 << "-e "
124 << "-a Address "
125 << "-j group "
126 << std::endl;
127 std::cout << "-m use multicast" << std::endl;
128 std::cout << "-s enable stereo" << std::endl;
129 std::cout << "-w no fullscreen" << std::endl;
130 std::cout << "-e exit after closed connection"
131 << std::endl;
132 std::cout << "-a Address Server network address"
133 << std::endl;
134 std::cout << "-m Address wait for requests on "
135 << "multicast group" << std::endl;
136 std::cout << "-p port wait for requests on port"
137 << std::endl;
138 return 0;
141 else
143 name=argv[a];
148 // init OpenSG
149 OSG::osgInit(argc, argv);
151 int snglBuf[] = {GLX_RGBA,
152 GLX_DEPTH_SIZE, 16,
153 None};
155 int dblBuf[16];
157 dblBuf[0] = GLX_RGBA;
158 dblBuf[1] = GLX_DEPTH_SIZE;
159 dblBuf[2] = 16;
160 dblBuf[3] = GLX_DOUBLEBUFFER;
161 dblBuf[4] = (doStereo == true) ? GLX_STEREO : None;
162 dblBuf[5] = None;
164 // X init
165 OSG::DisplayP dpy = XOpenDisplay(NULL);
167 if(dpy == NULL)
169 std::cerr << "Error: Could not open display!" << std::endl;
172 int dummy;
174 if(!glXQueryExtension( dpy, &dummy, &dummy))
176 std::cerr << "Error: X server has no OpenGL GLX extension"
177 << std::endl;
180 XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
182 if(vi == NULL)
184 vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
186 if(vi == NULL)
188 std::cerr << "no RGB visual with depth buffer" << std::endl;
192 if(vi->c_class != TrueColor)
194 std::cerr << "TrueColor visual required for this program"
195 << std::endl;
198 Colormap cmap = XCreateColormap(dpy,
199 RootWindow(dpy, vi->screen),
200 vi->visual,
201 AllocNone);
202 XSetWindowAttributes swa;
204 swa.colormap = cmap;
205 swa.border_pixel = 0;
206 swa.event_mask =
207 ExposureMask |
208 ButtonPressMask |
209 ButtonReleaseMask |
210 KeyPressMask |
211 Button1MotionMask |
212 Button2MotionMask |
213 Button3MotionMask |
214 StructureNotifyMask;
216 // Create Window
218 // Create a Window and connect it to the main display dpy
219 OSG::X11Window hwin = XCreateWindow(dpy,
220 RootWindow(dpy, vi->screen),
221 0, 0, 300, 300,
223 vi->depth,
224 InputOutput,
225 vi->visual,
226 CWBorderPixel |
227 CWColormap |
228 CWEventMask,
229 &swa );
231 XSetStandardProperties(dpy, hwin, "testWindowX", "testWindowX",
232 None, argv, argc, NULL);
234 if(fullscreen == true)
236 Atom noDecorAtom = XInternAtom(dpy,
237 "_MOTIF_WM_HINTS",
240 if(noDecorAtom == None)
242 fprintf(stderr,
243 "Could not intern X atom for _MOTIF_WM_HINTS.\n");
246 struct NoDecorHints
248 long flags;
249 long functions;
250 long decorations;
251 long input_mode;
254 NoDecorHints oHints;
256 oHints.flags = 2;
257 oHints.decorations = 0;
259 XChangeProperty(dpy,
260 hwin,
261 noDecorAtom,
262 noDecorAtom,
264 PropModeReplace,
265 reinterpret_cast<unsigned char *>(&oHints), 4);
270 // create the render action
271 ract = OSG::RenderAction::create();
273 // setup the OpenSG Glut window
274 window = OSG::XWindow::create();
275 window->setDisplay ( dpy );
276 window->setWindow ( hwin );
277 window->init();
279 XEvent event;
281 XMapWindow(dpy, hwin);
282 XIfEvent(dpy, &event, wait_for_map_notify, reinterpret_cast<char *>(hwin));
284 if(fullscreen == true)
286 XMoveWindow (dpy, hwin, 0, 0);
287 XResizeWindow(dpy, hwin,
288 DisplayWidth (dpy, vi->screen),
289 DisplayHeight(dpy, vi->screen));
291 static char data[1] = {0};
293 Cursor cursor;
294 Pixmap blank;
295 XColor dummyCol;
297 blank = XCreateBitmapFromData(dpy,
298 hwin,
299 data, 1, 1);
301 cursor = XCreatePixmapCursor(dpy,
302 blank,
303 blank,
304 &dummyCol, &dummyCol, 0, 0);
306 XFreePixmap(dpy, blank);
308 XDefineCursor(dpy,
309 hwin,
310 cursor);
311 XFlush(dpy);
314 window->activate();
316 glEnable( GL_LIGHTING );
317 glEnable( GL_LIGHT0 );
318 glEnable( GL_NORMALIZE );
320 // create the cluster server
321 server = new OSG::ClusterServer(window,name,connectionType,address);
322 // start the server
323 server->start();
325 bool stopIt = false;
326 int ip;
328 while(!stopIt)
330 while((ip = XPending(dpy)) != 0)
332 XNextEvent(dpy, &event);
334 switch (event.type)
336 case ConfigureNotify:
338 reshape(event.xconfigure.width,
339 event.xconfigure.height);
341 break;
343 case Expose:
344 display();
345 break;
351 display();
354 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
356 SLOG << e.what() << OSG::endLog;
357 delete server;
359 ract = NULL;
360 window = NULL;
362 OSG::osgExit();
364 return 0;
367 /* render loop */
368 void display()
372 // receive scenegraph and do rendering
373 server->render(ract);
374 // clear changelist
375 OSG::Thread::getCurrentChangeList()->clear();
377 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
379 if(exitOnError)
381 SLOG << e.what() << std::endl;
384 delete server;
386 ract = NULL;
387 window = NULL;
389 catch(...)
392 printf("Exit on error %s",e.what());
393 OSG::osgExit();
394 exit(0);
396 else
398 window->clearPorts();
400 SLOG << e.what() << OSG::endLog;
401 // try to restart server
402 server->stop();
403 // start server, wait for client to connect
404 server->start();
409 /* window reshape */
410 void reshape( int width, int height )
412 // set new window size
413 window->resize( width, height );
416 #else
418 #include <iostream>
420 int main(int argc,char **argv)
422 std::cerr << "Not supported on windows platform!" << std::endl;
424 #endif // WIN32