#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 "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; } } bool LoadGreyScaleShader() { GLint l_bStatus; char *vs_text = NULL,*fs_text = NULL; GLuint vs = glCreateShader(GL_VERTEX_SHADER), fs = glCreateShader(GL_FRAGMENT_SHADER); vs_text = textFileRead("shaders\\greyscale.vert"); if (vs_text == NULL) { return false; } fs_text = textFileRead("shaders\\greyscale.frag"); if (fs_text == NULL) { free(vs_text); return false; } glShaderSource(vs, 1, (const char**)&vs_text,NULL); glShaderSource(fs, 1, (const char**)&fs_text,NULL); free(vs_text); free(fs_text); glCompileShader(vs); // printShaderInfoLog(vs); glGetShaderiv(vs, GL_COMPILE_STATUS, &l_bStatus); if (l_bStatus == GL_FALSE) { return false; } glCompileShader(fs); // printShaderInfoLog(fs); glGetShaderiv(fs, GL_COMPILE_STATUS, &l_bStatus); if (l_bStatus == GL_FALSE) { return false; } g_GameState.m_GreyScaleShaderProgram = glCreateProgram(); glAttachShader(g_GameState.m_GreyScaleShaderProgram,fs); glAttachShader(g_GameState.m_GreyScaleShaderProgram,vs); glLinkProgram(g_GameState.m_GreyScaleShaderProgram); // printProgramInfoLog(g_GameState.m_GreyScaleShaderProgram); glGetProgramiv(g_GameState.m_GreyScaleShaderProgram, GL_LINK_STATUS, &l_bStatus); if (l_bStatus == GL_FALSE) { return false; } g_GameState.m_GSConvScaleLoc = glGetUniformLocation(g_GameState.m_GreyScaleShaderProgram, "g_ConversionWeights"); return true; } 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; 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); g_pEngine->Render(glutGet(GLUT_ELAPSED_TIME)); 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; case 'n': g_pEngine->Abort(); g_pEngine->NewGame(0, 0, BS_SMALL); break; case 'p': if (!g_pEngine->Pause()) { g_pEngine->Resume(); } 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 int main(int argc, char **argv) { messageq_init(); 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(); MenuInit(SCREEN_WIDTH, SCREEN_HEIGHT); InitWiiMotes(); GameSettings l_set = {10, 1.1, 2}; g_pEngine = new C_MatchBloxEngine("models", "", l_set); //glutFullScreen(); glutMainLoop(); return 0; } // main