#ifdef G_OS_WIN32 #define WIN32_LEAN_AND_MEAN 1 #include #else #define FALSE 0 #define TRUE !FALSE #endif //#include #include #include #include #include #include #include #include "message_queue.h" #include "message_input.h" #include "typedefs.h" #include "menu.h" #include "textfile.h" #include "C_MatchBloxEngine.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) g_GameState.m_ppWiimotes = wiiuse_init(1); #ifdef WIN32 wiiuse_set_bluetooth_stack(g_GameState.m_ppWiimotes, 1, WIIUSE_STACK_MS); #endif //WIN32 int found = wiiuse_find(g_GameState.m_ppWiimotes, 1, 30); int connected = wiiuse_connect(g_GameState.m_ppWiimotes , 1); if (connected) { printf("Connected to %i wiimotes (of %i found).\n", connected, found); wiiuse_set_leds(g_GameState.m_ppWiimotes[0], WIIMOTE_LED_1 | WIIMOTE_LED_2 | WIIMOTE_LED_3 | WIIMOTE_LED_4); wiiuse_rumble(g_GameState.m_ppWiimotes[0], 1); //wiiuse_motion_sensing(g_GameState.m_ppWiimotes[0], 1); wiiuse_set_ir(g_GameState.m_ppWiimotes[0], 1); Sleep(200); wiiuse_rumble(g_GameState.m_ppWiimotes[0], 0); } else { printf("Failed to connect to any wiimote.\n"); return 0; } // g_GameState.m_pTrackingWiimote = new wiimote(); // g_GameState.m_pTrackingWiimote->ChangedCallback = NULL; //no callbacks, we just poll... //g_GameState.m_pTrackingWiimote->CallbackTriggerFlags = NO_CHANGE; // printf("connecting:\n"); // int retries = 10; // while(!g_GameState.m_pTrackingWiimote->Connect(wiimote::FIRST_AVAILABLE) && retries > 10) // { // Sleep(100); // printf("."); // retries--; // } //if (g_GameState.m_pTrackingWiimote->IsConnected()) //{ // printf("connected\n"); // g_GameState.m_pTrackingWiimote->SetLEDs(0x0f); // // g_GameState.m_pTrackingWiimote->SetReportType(wiimote::IN_BUTTONS_ACCEL_IR, true); // IR dots // g_GameState.m_pTrackingWiimote->SetRumble(true); // Sleep(500); // g_GameState.m_pTrackingWiimote->SetRumble(false); // 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) { //g_GameState.m_pTrackingWiimote->RefreshState(); if (wiiuse_poll(g_GameState.m_ppWiimotes, 1)) { ////put wiimote data in message queue input_payload_wiimote payload; payload.btns = g_GameState.m_ppWiimotes[0]->btns; payload.ir = g_GameState.m_ppWiimotes[0]->ir; ////copy buttons bitmask //payload.Button.Bits = g_GameState.m_pTrackingWiimote->Button.Bits; ////copy the 4 ir dots //memcpy((void*)payload.Dot, g_GameState.m_pTrackingWiimote->IR.Dot, 4*sizeof(wiimote::ir::dot)); struct messageq_s message; message.recipient = MESSAGE_MENU | MESSAGE_RENDERER; message.sender = MESSAGE_INPUT_WIIMOTE; message.payload = &payload; message.payload_size = sizeof(input_payload_wiimote); messageq_send(&message); } g_pEngine->ProcessMsgs(); //call engine idle func //MenuRun(); glutPostRedisplay(); } void render_scene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); g_pEngine->Render(glutGet(GLUT_ELAPSED_TIME)); glDisable(GL_LIGHTING); MenuRender(); 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_LARGE); 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, 2, 2}; g_pEngine = new C_MatchBloxEngine("models", "", l_set); glutMainLoop(); return 0; } // main