Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

client/guiclient.cpp

Go to the documentation of this file.
00001 // gluicli.cpp : Defines the entry point for the console application.
00002 //
00003 
00004 #include <unistd.h>
00005 #include "../trucknet/trucknet.h"
00006 #include "../trucknet/unp.h"
00007 #include "../trucknet/TCPmsg.h"
00008 #include "../trucknet/msg.h"
00009 
00010 #include "joystick.h"
00011 #include "command.h"
00012 
00013 #include <GL/openglut.h>
00014 //#include <stdarg.h>
00015 //#include <stdio.h>
00016 //#include <stdlib.h>
00017 
00018 
00019 //#include <string>
00020 using std::string;
00021 using std::cout;
00022 using std::endl;
00023 using std::cerr;
00024 
00025 
00026 TCPmsg *m;
00027 int processNet(void);
00028 int joystickEnable = 0;
00029 float joyX=0, joyY=0, joyZ=0;
00030 Joystick joy;
00031 Command cmd;
00032 
00033 
00042 void usage(void) {
00043         cout <<
00044                 "usage : guiclient ip <address> | [no-net] \n"
00045                 "  ip <address>  - use this as server address (default = 'localhost')\n"
00046                 "  no-net        - don't use networking, won't attempt to connect server\n";
00047 }
00048 
00059 void sendMessages(int data) {
00060         if (m != NULL) {
00061         
00062                 // Send output messages
00063                 if (joystickEnable) {
00064                         *m << startmsg("joyX") << - joyX << endmsg;
00065                         *m << startmsg("joyY") << - joyY << (joyZ+1)*0.1 << endmsg;
00066                 }
00067 
00068                 *m << startmsg("heartbeat") << endmsg;
00069                 
00070                 // Processes any input messages
00071                 processNet();
00072         }
00073         
00074         // reschedule this function
00075         glutTimerFunc(500, sendMessages, data);
00076         //cerr << '.';
00077 }
00078 
00089 int processNet(void) {
00090         int result;
00091         result = m->recvNoWait();       
00092         if (result == -1) {     
00093                 if (m->error() == EAGAIN) {
00094                         return 0;
00095                 }
00096                 else {
00097                         cerr << "processNET() : error with TCPmsg.recv()" << endl;
00098                         exit(EXIT_FAILURE);
00099                 }
00100         }
00101         
00102         while (m->dispatch()) { 
00103                 cout << "Recieved messge : '" << m->type();
00104                 while(m) {      
00105                         cout << ' ' << *m;
00106                 }
00107                 cout << '\'' << endl;
00108         }
00109         
00110         return 0;
00111 }
00112 
00113 
00115 // GLUT callback Handlers 
00117 
00127 static void
00128 display(void) {
00129         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00130 
00131         joy.display();
00132         cmd.display();
00133         
00134         glutSwapBuffers();
00135 }
00136 
00147 static void
00148 resize(int width, int height)
00149 {
00150     glViewport(0, 0, width, height);
00151 
00152         joy.resize(width, height);
00153 }
00154 
00165 static void
00166 joystick(unsigned int buttons, int xaxis, int yaxis, int zaxis)
00167 {
00168         joy.joystick(buttons, xaxis, yaxis, zaxis);
00169 
00170         joyX = xaxis * 0.001;
00171         joyY = yaxis * 0.001;
00172         joyZ = zaxis * 0.001;
00173 }
00174 
00185 static void
00186 key(unsigned char key, int x, int y)
00187 {
00188         cout << "key = " << (int) key << endl;
00189 
00190         cmd.key(key,x,y);
00191         
00192         switch (key)
00193     {
00194     case 27 :
00195                 cout << "Quiting..." << endl; 
00196                 //exit(0); 
00197                 glutLeaveMainLoop();
00198                 break;
00199         }
00200 
00201         //cout << "ctrl-'" << (int) key << "'" << endl;
00202         if (glutGetModifiers() == GLUT_ACTIVE_ALT) { 
00203                 switch (key)
00204                 {
00205                         case 'j':
00206                         case 'J':                       
00207                                 joystickEnable ^= 1;                    
00208                                 cout << "joystick " << (joystickEnable ? "enabled" : "disabled") << endl; 
00209                                 break;
00210                         case 'q' :
00211                         case 'Q' :
00212                                 cout << "Quiting..." << endl; 
00213                                 //exit(0); 
00214                                 glutLeaveMainLoop();
00215                                 break;
00216                 }
00217         }
00218 
00219         glutPostRedisplay();
00220 }
00221 
00222 
00223 
00224 /* Program entry point */
00225 int
00226 main(int argc, char *argv[])
00227 {
00228     int i;
00229 
00230         // let glut parse out its command line flags
00231     glutInit(&argc, argv);
00232 
00233         // parse the command line flags for us
00234         char *serverIpAddr = "localhost";       
00235         for (int i = 1; i<argc; ++i) {
00236                 if (strcmp(argv[i], "ip") == 0) {
00237                         ++i;
00238                         if (argc > i) {
00239                                 serverIpAddr = argv[i];
00240                         }
00241                         else {
00242                                 cerr << "error : missing IP address / name after 'ip'" << endl;
00243                                 usage();
00244                                 return -1;
00245                         }
00246                 }
00247                 else if (strcmp(argv[i], "no-net")==0) {
00248                         serverIpAddr = NULL;
00249                 }
00250                 else {
00251                         cerr << "error : invalue option '" << argv[i] << "'" << endl;
00252                         usage();
00253                         return -1;
00254                 }
00255         }
00256         
00257         // create TcpMsg connection to server
00258         if (serverIpAddr != NULL) {
00259                 cout << "Connecting to server at '" << serverIpAddr << "' ..." << endl;
00260                 m = new TCPmsg(serverIpAddr, GUI_PORT);
00261                 cout << "Connected!'" << endl;
00262         } else {
00263                 m = NULL;
00264         }               
00265         
00266     glutInitWindowSize(640,480);
00267     glutInitWindowPosition(40,40);
00268     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);                              
00269     glutCreateWindow("RAPTOR truck client + OpenGLUT Joystick");
00270 
00271     glutReshapeFunc(resize);
00272     glutDisplayFunc(display);
00273     glutJoystickFunc(joystick,50);
00274     glutKeyboardFunc(key);
00275 
00276     glClearColor(1,1,1,1);
00277     glEnable(GL_CULL_FACE);
00278     glCullFace(GL_BACK);
00279 
00280     glEnable(GL_DEPTH_TEST);
00281     glDepthFunc(GL_LESS);
00282 
00283     glEnable(GL_LIGHT0);
00284     glEnable(GL_NORMALIZE);
00285     glEnable(GL_COLOR_MATERIAL);
00286 
00287         // Setup joystick
00288     if( glutDeviceGet(GLUT_HAS_JOYSTICK) )
00289     {
00290         printf("Joystick detected.\n");
00291         printf("%d joystick axes.\n",glutDeviceGet(GLUT_JOYSTICK_AXES));
00292         printf("Joystick poll rate is %d.\n",glutDeviceGet(GLUT_JOYSTICK_POLL_RATE));
00293         printf("Joystick %savailable.\n",glutDeviceGet(GLUT_OWNS_JOYSTICK) ? "" : "un");
00294     }
00295     else
00296         printf("Joystick not detected.\n");
00297 
00298         
00299         // Setup periodic function for sending / recieving messages
00300         // from server / truck
00301         glutTimerFunc(1000, sendMessages, 0 /*data*/);
00302 
00303     glutMainLoop();
00304 
00305     return EXIT_SUCCESS;
00306 }
00307 
00308 

Generated on Fri Sep 1 14:25:44 2006 for Raptor by  doxygen 1.4.4