// From <https://www.opengl.org/wiki/Programming_OpenGL_in_Linux:_GLX_and_Xlib> int main(int argc, char** argv){ Initialised = false; StartTime = GetClock(); double time; Display* dpy; Window root; XVisualInfo* vi; Colormap cmap; XSetWindowAttributes swa; Window win; GLXContext glc; XWindowAttributes gwa; XEvent xev; GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None}; dpy = XOpenDisplay(0); if(dpy == 0){ printf("\n\tcannot connect to X server\n\n"); return 1; } root = DefaultRootWindow(dpy); vi = glXChooseVisual(dpy, 0, att); if(vi == 0){ printf("\n\tno appropriate visual found\n\n"); return 1; }else{ // %p creates hexadecimal output like in glxinfo printf("\n\tvisual %p selected\n", (void*)vi->visualid); } cmap = XCreateColormap(dpy, root, vi->visual, AllocNone); swa.colormap = cmap; swa.event_mask = ExposureMask | KeyPressMask; win = XCreateWindow( dpy, root, 0, 0, 640, 480, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa ); XMapWindow(dpy, win); XStoreName(dpy, win, "OpenGL Sample"); glc = glXCreateContext(dpy, vi, 0, GL_TRUE); glXMakeCurrent(dpy, win, glc); if(!InitGLEW()){ glXMakeCurrent (dpy, None, 0); glXDestroyContext(dpy, glc); XDestroyWindow (dpy, win); XCloseDisplay (dpy); return 1; } if(!LoadShader ("OpenGL/Texture.vp", "OpenGL/Texture.fp")) return 1; if(!LoadTexture("Pic/greatwall.jpg")) return 1; Initialised = true; bool running = true; while(running){ while(XCheckWindowEvent(dpy, win, 0xFFFFFFFF, &xev)){ switch(xev.type){ case Expose: XGetWindowAttributes(dpy, win, &gwa); Render(gwa.width, gwa.height); glXSwapBuffers(dpy, win); break; case KeyPress: // printf("KeyPress: keycode %u state %u\n", xev.xkey.keycode, xev.xkey.state); if(xev.xkey.keycode == Key_Escape) running = false; else OnKeyDown(xev.xkey.keycode); break; } } XGetWindowAttributes(dpy, win, &gwa); Render(gwa.width, gwa.height); glXSwapBuffers(dpy, win); RenderTime(GetClock()-time); time = GetClock(); usleep(1000); } glXMakeCurrent (dpy, None, 0); glXDestroyContext(dpy, glc); XDestroyWindow (dpy, win); XCloseDisplay (dpy); free(InputBuffer ); free(OutputBuffer); return 0; }
//**************渲染道具函数************************* void Props::RenderProps () { std ::list <PROPS >::iterator i; for(i=_allprops.begin ();i!=_allprops.end ();i++) { if(i->_isalive==true) { //设置世界变换 D3DXMATRIX position,scaling,V; D3DXMatrixScaling(&scaling,0.5f,0.3f,0.5f); D3DXMatrixTranslation(&position,i->_position.x ,i->_position.y ,i->_position.z); position=scaling*position; Camera.SetViewMatrix(&V); if(i->_id==1) { prop1->RanderBorad ( &V,position); } if(i->_id==2) { prop2->RanderBorad (&V,position); } if(i->_id==3) { prop3->RanderBorad(&V,position); } if(i->_id==4) { D3DXMatrixScaling(&scaling,1.0f,1.0f,1.0f); position=scaling*position; prop4->RanderBorad (&V,position); } switch (i->_id) { case 5: { prop5TimeExit +=0.1; if(prop5TimeLeft-(int)prop5TimeExit >0) { //指定时间出现的位置 RECT rect; rect.left=400,rect.top=50,rect.right=800,rect.bottom=600; //渲染时间 RenderTime(prop5TimeLeft-(int) prop5TimeExit,rect ); //渲染动态纹理 RenderDynaminc(prop5tex,position,V); } else //这里表示时间到了 没吃到那个道具 { isaddscore=false; //表示没有吃到那个道具 i->_isalive=false; //道具消失 i->_timeHappen=0; //道具出现时间置零 prop5TimeExit=0; //道具5时间绘制置零 } break; } case 6: { RenderDynaminc(prop6tex,position,V); break; } case 7: { if(prop7TimeLeft-prop7TimeExit>0) { RenderDynaminc (prop7tex,position,V); prop7TimeExit += 0.1; } else { prop7TimeExit =0; i->_isalive=false; i->_timeHappen=0; } break; } } } } }
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){ Initialised = false; StartTime = GetClock(); double time = StartTime; WNDCLASSEX wcex; HGLRC hRC; RECT rect; MSG msg; // register window class wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_OWNDC; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(0, IDI_APPLICATION); wcex.hCursor = LoadCursor(0, IDC_ARROW); wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wcex.lpszMenuName = 0; wcex.lpszClassName = "GLSample"; wcex.hIconSm = LoadIcon(0, IDI_APPLICATION);; if(!RegisterClassEx(&wcex)) return 0; // create main window hwnd = CreateWindowEx( 0, "GLSample", "OpenGL Sample", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, hInstance, 0 ); ShowWindow(hwnd, nCmdShow); // enable OpenGL for the window PIXELFORMATDESCRIPTOR pfd; int iFormat; // get the device context (DC) hDC = GetDC(hwnd); // set the pixel format for the DC ZeroMemory(&pfd, sizeof(pfd)); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 0; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat(hDC, &pfd); SetPixelFormat(hDC, iFormat, &pfd); // create and enable the render context (RC) hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); if(!InitGLEW()){ wglMakeCurrent(0, 0); wglDeleteContext(hRC); ReleaseDC(hwnd, hDC); DestroyWindow(hwnd); return 1; } if(!LoadShader ("..\\OpenGL\\Texture.vp", "..\\OpenGL\\Texture.fp")) return 1; if(!LoadTexture("..\\Pic\\greatwall.jpg")) return 1; Initialised = true; Initialised = true; // program main loop while(true){ while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){ if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage (&msg); } if(msg.message == WM_QUIT) break; GetClientRect(hwnd, &rect); Render(rect.right, rect.bottom); SwapBuffers(hDC); RenderTime(GetClock()-time); time = GetClock(); Sleep(1); } wglMakeCurrent(0, 0); wglDeleteContext(hRC); ReleaseDC(hwnd, hDC); DestroyWindow(hwnd); free(InputBuffer ); free(OutputBuffer); return msg.wParam; }