fixed: gcc8 compile issues
[opensg.git] / Source / Contrib / ComplexSceneManager / GLUT / appClusterServerGLUT.cpp
blobebcf897572dba68dc929ae5fc792c9efee5a34fb
1 #include "OSGGLUT.h"
2 #include "OSGConfig.h"
3 #include <iostream>
4 #include "OSGLog.h"
5 #include "OSGClusterServer.h"
6 #include "OSGGLUTWindow.h"
7 #include "OSGRenderAction.h"
8 #include "OSGSceneFileHandler.h"
9 #include "OSGViewport.h"
10 #include "OSGFrameHandler.h"
12 int winid;
13 OSG::ClusterServer *server;
14 OSG::GLUTWindowRecPtr window;
15 OSG::RenderActionRefPtr ract;
16 bool running=false;
17 bool exitOnError=false;
18 OSG::UInt32 servicePort=8437;
19 std::string serviceGroup="224.245.211.234";
20 int winWidth=0,winHeight=0;
22 void cleanup(void)
24 window = NULL;
25 ract = NULL;
27 server->stop();
28 delete server;
30 server = NULL;
32 OSG::osgExit();
35 void display()
37 if(!running)
39 server->start();
40 running=true;
41 glutShowWindow();
43 /*! ignore rendering in zero sized windows */
44 if(!winWidth || !winHeight)
45 return;
46 try
48 OSG::FrameHandler::the()->frame();
49 OSG::commitChanges();
50 server->render(ract);
51 // clear changelist from prototypes
52 OSG::Thread::getCurrentChangeList()->clear();
54 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
56 if(exitOnError)
58 SLOG << e.what() << std::endl;
59 printf("Exit on error %s",e.what());
60 try
62 cleanup();
65 catch(...)
69 exit(0);
71 else
73 window->clearPorts();
74 // try to restart server
75 try
77 server->stop();
79 catch(...)
82 running=false;
83 glutHideWindow();
88 void reshape( int width, int height )
90 winWidth = width;
91 winHeight = height;
92 std::cout << "reshape " << width << " " << height << std::endl;
93 window->resize( width, height );
96 void key(unsigned char key, int /*x*/, int /*y*/)
98 switch ( key )
100 case 'd':
101 window->getPort(0)->getRoot()->dump();
102 break;
103 case 's':
104 OSG::SceneFileHandler::the()->write(
105 window->getPort(0)->getRoot(),"server.osg");
106 cleanup();
107 exit(0);
108 break;
112 int main(int argc,char **argv)
114 const char *name ="ClusterServer";
115 const char *connectionType="StreamSock";
116 // const char *connectionType="Multicast";
117 bool fullscreen =true;
118 std::string address ="";
119 int width=-1,height=300,x=0,y=0;
120 bool doStereo=false;
121 char *str;
123 for(int i = 1 ; i < argc ; ++i)
125 if(argv[i][0] == '-')
127 switch(argv[i][1])
129 case 'm':
130 connectionType="Multicast";
131 break;
132 case 's':
133 doStereo=true;
134 break;
135 case 'w':
136 fullscreen=false;
137 break;
138 case 'e':
139 exitOnError=true;
140 break;
141 case 'a':
142 address=&(argv[i][2]);
143 break;
144 case 'g':
145 if(argv[i][2] != '\0')
146 str=argv[i]+2;
147 else
148 str=argv[++i];
149 if(sscanf(str,"%d,%d,%d,%d",
150 &width,&height,&x,&y)!=4)
152 SWARNING << "Wrong args in -g. Use -gw,h,x,y"
153 << std::endl;
154 cleanup();
155 exit(0);
157 break;
158 case 'p':
159 if(argv[i][2] != '\0')
160 servicePort=atoi(argv[i]+2);
161 else
162 servicePort=atoi(argv[++i]);
163 break;
164 case 'j':
165 if(argv[i][2] != '\0')
166 serviceGroup=argv[i]+2;
167 else
168 serviceGroup=argv[++i];
169 break;
170 case 'h':
171 std::cout << argv[0]
172 << "-m "
173 << "-s "
174 << "-w "
175 << "-e "
176 << "-g w,h,x,y "
177 << "-a Address "
178 << "-j group "
179 << "-p servicePort "
180 << std::endl;
181 std::cout << "-m use multicast" << std::endl;
182 std::cout << "-s enable stereo" << std::endl;
183 std::cout << "-w no fullscreen" << std::endl;
184 std::cout << "-e exit after closed connection"
185 << std::endl;
186 std::cout << "-g geometry" << std::endl;
187 std::cout << "-a Address Server network address"
188 << std::endl;
189 std::cout << "-m Address wait for requests on "
190 << "multicast group" << std::endl;
191 std::cout << "-p port wait for requests on port"
192 << std::endl;
193 return 0;
196 else
198 name=argv[i];
204 OSG::osgInit (argc, argv);
205 OSG::ClusterServer::init(argc, argv);
207 glutInit(&argc, argv);
209 if(doStereo)
210 glutInitDisplayMode( GLUT_STEREO |
211 GLUT_RGB |
212 GLUT_DEPTH |
213 GLUT_DOUBLE);
214 else
215 glutInitDisplayMode( GLUT_RGB |
216 GLUT_DEPTH |
217 GLUT_DOUBLE);
218 glutInitWindowPosition(x,y);
219 if(width>0)
220 glutInitWindowSize(width,height);
221 winid = glutCreateWindow("OpenSG Cluster Client");
222 if(fullscreen)
223 glutFullScreen();
225 else
226 glutReshapeWindow(300,300);
228 if(width>0)
229 glutReshapeWindow(width,height);
230 glutPositionWindow(x, y);
232 glutPopWindow();
233 glutDisplayFunc(display);
234 glutIdleFunc(display);
235 glutKeyboardFunc(key);
236 glutReshapeFunc(reshape);
237 glutSetWindowTitle(name);
238 // switch off cursor
239 glutSetCursor(GLUT_CURSOR_NONE);
240 glEnable( GL_LIGHTING );
241 glEnable( GL_LIGHT0 );
242 glEnable( GL_DEPTH_TEST );
243 glEnable( GL_NORMALIZE );
244 ract = OSG::RenderAction::create();
245 // ract->setFrustumCulling(false);
246 window = OSG::GLUTWindow::create();
247 window->setGlutId(winid);
248 window->init();
249 server = new OSG::ClusterServer(window,
250 name,
251 connectionType,
252 address,
253 servicePort,
254 serviceGroup);
255 server->start();
256 running=true;
257 glutMainLoop();
259 catch(OSG_STDEXCEPTION_NAMESPACE::exception &e)
261 SLOG << e.what() << std::endl;
262 delete server;
263 cleanup();
264 OSG::osgExit();
266 return 0;