#ifdef G_OS_WIN32 #define WIN32_LEAN_AND_MEAN 1 #include #define sleep Sleep #else #define FALSE 0 #define TRUE !FALSE #include #endif #include #include #include #include #include #include "typedefs.h" #include "menu.h" #include "textfile.h" #include "C_MatchBloxEngine.h" #include "wiimote_utils.h" #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 #define KEY_ESCAPE 27 GameState_t g_GameState; C_MatchBloxEngine *g_pEngine; bool InitWiiMotes() { //connect to the wiimote(s) //create an abstract wiimote interface #ifdef USE_WIIYOURSELF g_GameState.m_pWiimote[0] = new WiiYourselfWiimote(); #endif //USE_WIIYOURSELF printf("connecting:\n"); int retries = 10; while(!g_GameState.m_pWiimote[0]->Connect() && retries > 0) { sleep(100); printf("."); //retries--; } if (g_GameState.m_pWiimote[0]->IsConnected()) { printf("connected\n"); g_GameState.m_pWiimote[0]->SetLeds(0x0f); g_GameState.m_pWiimote[0]->StartRumble(); sleep(500); g_GameState.m_pWiimote[0]->StopRumble(); return true; } else { return false; } } void init_gl(void) { GLfloat l_fLightpos[4] = {0.0, 1.0, 1.0, 0.0}; glClearColor(1, 0.4, 0.2, 1.0); glClearDepth(1000.0); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, l_fLightpos); glEnable(GL_NORMALIZE); glDisable(GL_TEXTURE_2D); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); } // init_gl void idle_func(void) { struct input_payload_wiimote wiimote_payload; struct messageq_s message; //wiimote_payload.posDataValid = false; g_GameState.m_pWiimote[0]->FillWiimoteMsgPayload(wiimote_payload, 205.0); // << should be a sensor bar led distance message.recipient = MESSAGE_MENU | MESSAGE_RENDERER; message.sender = MESSAGE_INPUT_WIIMOTE; message.payload = &wiimote_payload; message.payload_size = sizeof(input_payload_wiimote); messageq_send(&message); g_pEngine->ProcessMsgs(); //call engine idle func MenuProcessMessage(); glutPostRedisplay(); } void render_scene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); input_payload_wiimote l_HTWiimote; l_HTWiimote.posDataValid = false; g_GameState.m_pWiimote[0]->FillWiimoteMsgPayload(l_HTWiimote, 210.0); g_pEngine->Render(glutGet(GLUT_ELAPSED_TIME), l_HTWiimote, g_GameState.m_FrustumParms); //render the menu glDisable(GL_LIGHTING); glPushMatrix(); MenuRender(); glPopMatrix(); glEnable(GL_LIGHTING); glutSwapBuffers(); } // render_scene void reshape(int w, int h) { //set the new viewport dimension glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.0, ((GLdouble)h)/((GLdouble)w), 0.5, 100.0); glTranslated(0.0, 0.0, -0.5); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void process_normal_keys(unsigned char key, int x, int y) { struct messageq_s message; struct input_payload_keyboard payload; message.recipient = MESSAGE_MENU | MESSAGE_RENDERER; message.sender = MESSAGE_INPUT_KEYBOARD; switch (key) { case KEY_ESCAPE: exit(0); break; } payload.specialkey = FALSE; payload.key = (int)key; payload.modifier = glutGetModifiers(); payload.x = x; payload.y = y; message.payload = (struct input_payload_keyboard *)&payload; message.payload_size = sizeof(payload); messageq_send(&message); } // process_normal_keys void process_special_keys(int key, int x, int y) { switch (key) { // do sumting case 0: default: break; } } // process_special_keys void process_mouse(int button, int state, int x, int y) { MenuMouseClick(button, state, x, y); } // process_mouse void process_passive_mouse(int x, int y) { MenuMouseMove(x, y); } // process_passive_mouse void load_settings() { fstream l_Stream; string l_strLine, l_strValueName, l_strValue; double l_dValue; string::size_type l_posEqualSign,l_posDivideSign; //Open file l_Stream.open("settings.ini"); if(!l_Stream.is_open()) { std::cout << "Error, settings-file was unable to be opened."; } else { //Go through whole file while(!l_Stream.eof()) { getline(l_Stream, l_strLine); //Search for = if (( l_posEqualSign = l_strLine.find_first_of("=")) != string::npos) { //found one l_strValueName = l_strLine.substr( 0, l_posEqualSign); l_strValue = l_strLine.substr( l_posEqualSign + 1); l_dValue = (41.0*(M_PI/180.0))/1024.0; //Cast to double if ((l_posDivideSign = l_strValue.find_first_of("/")) != string::npos) //Check if it has a dividesign { l_dValue = atof(l_strValue.substr(0,l_posDivideSign).c_str())/atof(l_strValue.substr(l_posDivideSign+1).c_str()); } else { l_dValue = atof(l_strValue.c_str()); } if (l_strValueName.compare("HeadTrackLedDistance") == 0) g_GameState.m_FrustumParms.m_dHeadTrackLedDist = l_dValue; if (l_strValueName.compare("RadiansPerCameraPixel") == 0) g_GameState.m_FrustumParms.m_dRadPerCameraPixel = l_dValue; if (l_strValueName.compare("YAngleCorrection") == 0) g_GameState.m_FrustumParms.m_dYAngleCorrection = l_dValue; if (l_strValueName.compare("CameraYOffset") == 0) g_GameState.m_FrustumParms.m_dCameraYOffset = l_dValue; if (l_strValueName.compare("ScreenHeightMM") == 0) g_GameState.m_FrustumParms.m_dScreenHeightMM = l_dValue; if (l_strValueName.compare("ScreenHeightWorld") == 0) g_GameState.m_FrustumParms.m_dScreenHeightWorld = l_dValue; if (l_strValueName.compare("EyeDistanceMM") == 0) g_GameState.m_FrustumParms.m_dEyeDistMM = l_dValue; if (l_strValueName.compare("DefHeadDistanceMM") == 0) g_GameState.m_FrustumParms.m_dDefHeadDistMM = l_dValue; if (l_strValueName.compare("CameraXCenter") == 0) g_GameState.m_FrustumParms.m_dCameraXCenter = l_dValue; if (l_strValueName.compare("CameraYCenter") == 0) g_GameState.m_FrustumParms.m_dCameraYCenter = l_dValue; if (l_strValueName.compare("ScreenAspect") == 0) g_GameState.m_FrustumParms.m_dScreenAspect = l_dValue; if (l_strValueName.compare("SensorbarLedDistanceMM") == 0) g_GameState.m_FrustumParms.m_dSensorbarLedDistMM = l_dValue; }//end found =-sign }//end while l_Stream.close(); } } int main(int argc, char **argv) { messageq_init(); load_settings(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT); glutCreateWindow("MatchBlox"); glutReshapeFunc(reshape); glutDisplayFunc(render_scene); glutIdleFunc(idle_func); glutKeyboardFunc(process_normal_keys); glutSpecialFunc(process_special_keys); glutMouseFunc(process_mouse); glutPassiveMotionFunc(process_passive_mouse); glutSetCursor(GLUT_CURSOR_NONE); init_gl(); //init the GL Extension Wrangler library glewInit(); //check for OpenGL 2.0 support if (!glewIsSupported("GL_VERSION_2_0")) { printf("Your graphics hardware/software does not support OpenGL 2.0\n"); exit(1); } MenuInit(SCREEN_WIDTH, SCREEN_HEIGHT); InitWiiMotes(); // initialise the stereo vision parameters << should be loaded from a textfile IS DONE /* g_GameState.m_FrustumParms.m_dEyeDistMM = 70.0; //7 cm g_GameState.m_FrustumParms.m_dDefHeadDistMM = 1000.0; //100 cm g_GameState.m_FrustumParms.m_dHeadTrackLedDist = 205.0; g_GameState.m_FrustumParms.m_dRadPerCameraPixel = (41.0*(M_PI/180.0))/1016.0; g_GameState.m_FrustumParms.m_dCameraXCenter = 1016.0/2.0; g_GameState.m_FrustumParms.m_dCameraYCenter = 760.0/2.0; g_GameState.m_FrustumParms.m_dYAngleCorrection = 0.0; //to be initialised correctly g_GameState.m_FrustumParms.m_dScreenHeightWorld = 20.0; g_GameState.m_FrustumParms.m_dCameraYOffset = 130.0; g_GameState.m_FrustumParms.m_dScreenHeightMM = 500.0;//210.0; g_GameState.m_FrustumParms.m_dScreenAspect = 4.0/3.0;*/ g_GameState.m_GSConvScale[0] = 0.299; g_GameState.m_GSConvScale[1] = 0.587; g_GameState.m_GSConvScale[2] = 0.114; /*g_GameState.m_FrustumParms.m_dCameraYOffset = 205.0; g_GameState.m_FrustumParms.m_dScreenHeightMM = 320.0; g_GameState.m_FrustumParms.m_dScreenAspect = 16.0/10.0;*/ g_GameState.m_bHeadTrackingEnabled = false; g_GameState.m_bStereoEnabled = false; GameSettings l_set = {10, 1.1, 2}; g_pEngine = new C_MatchBloxEngine("models", "", l_set); glutFullScreen(); glutMainLoop(); return 0; } // main