window.cpp window.h #include"window.h" #ifdef__linux voidOpenArena::Window::SwapBuffers() { if(doubleBuffered) { glXSwapBuffers(display,window); } } #endif #ifdef__APPLE__ voidOpenArena::Window::SwapBuffers() { if(doubleBuffered) { glXSwapBuffers(display,window); } } #endif #ifdefWIN32 voidOpenArena::Window::SwapBuffers() { ::SwapBuffers(deviceContext); } #endif #ifdef__linux voidOpenArena::Window::Close() { if(hRC) { if(!glXMakeCurrent(display,None,NULL)) { printf("Couldnotreleasedrawingcontext.\n"); } glXDestroyContext(display,hRC); hRC=NULL; } if(_fullscreen) { XF86VidModeSwitchToMode(display,screen,&vidMode); XF86VidModeSetViewPort(display,screen,0,0); } XCloseDisplay(display); } #endif #ifdef__APPLE__ voidOpenArena::Window::Close() { if(hRC) { if(!glXMakeCurrent(display,None,NULL)) { printf("Couldnotreleasedrawingcontext.\n"); } glXDestroyContext(display,hRC); hRC=NULL; } if(_fullscreen) { //XF86VidModeSwitchToMode(display,screen,&vidMode); //XF86VidModeSetViewPort(display,screen,0,0); } XCloseDisplay(display); } #endif #ifdefWIN32 voidOpenArena::Window::Close() { if(_fullscreen) { ChangeDisplaySettings(NULL,0); ShowCursor(true/*false*/); } if(glContext) { if(!wglMakeCurrent(NULL,NULL)) MessageBox(NULL,"ReleaseOfDCAndRCFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION); if(!wglDeleteContext(glContext)) MessageBox(NULL,"ReleaseRenderingContextFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION); glContext=NULL; } if(deviceContext&&!ReleaseDC(window,deviceContext)) { MessageBox(NULL,"ReleaseDeviceContextFailed.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION); deviceContext=NULL; } if(window&&!DestroyWindow(window)) { MessageBox(NULL,"CouldNotReleasehWnd.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION); window=NULL; } if(!UnregisterClass("OpenArenav0.1.0",instance)) { MessageBox(NULL,"CouldNotUnregisterClass.","SHUTDOWNERROR",MB_OK|MB_ICONINFORMATION); instance=NULL; } }//EndOpenArena::Window::Close() #endif #ifdef__linux boolOpenArena::Window::Open() { XVisualInfo*vi; Colormapcmap; intbestMode=0; intvidModeMajorVersion; intvidModeMinorVersion; intglxMajorVersion; intglxMinorVersion; intmodeNum; XF86VidModeModeInfo**modes; AtomwmDelete; ::WindowwinDummy; unsignedintborderDummy; display=XOpenDisplay(0); screen=DefaultScreen(display); XF86VidModeQueryVersion(display,&vidModeMajorVersion,&vidModeMinorVersion); printf("XF86VidModeExtension-Version%d.%d\n",vidModeMajorVersion,vidModeMinorVersion); XF86VidModeGetAllModeLines(display,screen,&modeNum,&modes); vidMode=*modes[0]; inti; for(i=0;i<modeNum;i++) { //Addacheckforcolordepthhere if((modes[i]->hdisplay==_width)&&(modes[i]->vdisplay==_height)) { bestMode=i; } } vi=glXChooseVisual(display,screen,attrListDbl); if(vi==NULL) vi=glXChooseVisual(display,screen,attrListSgl); doubleBuffered=false; printf("OnlySinglebufferedVisual!\n"); } else { doubleBuffered=true; printf("GotDoublebufferedVisual!\n"); } glXQueryVersion(display,&glxMajorVersion,&glxMinorVersion); printf("glX-Version%d.%d\n",glxMajorVersion,glxMinorVersion); hRC=glXCreateContext(display,vi,0,GL_TRUE); cmap=XCreateColormap(display,RootWindow(display,vi->screen),vi->visual,AllocNone); attributes.colormap=cmap; attributes.border_pixel=0; attributes.event_mask=ExposureMask| KeyPressMask|KeyReleaseMask| ButtonPressMask|ButtonReleaseMask| //PointerMotionMask|ButtonMotionMask| StructureNotifyMask; if(_fullscreen) { XF86VidModeSwitchToMode(display,screen,modes[bestMode]); XF86VidModeSetViewPort(display,screen,0,0); XFree(modes); attributes.override_redirect=true; window=XCreateWindow(display,RootWindow(display,vi->screen),0,0,_width,_height,0,vi->depth,InputOutput,vi->visual,CWBorderPixel|CWColormap|CWEventMask|CWOverrideRedirect,&attributes); XWarpPointer(display,None,window,0,0,0,0,0,0); XMapRaised(display,window); XGrabKeyboard(display,window,true,GrabModeAsync,GrabModeAsync,CurrentTime); XGrabPointer(display,window,true,ButtonPressMask,GrabModeAsync,GrabModeAsync,window,None,CurrentTime); XDefineCursor(display,window,CreateFullscreenCursor()); } else { window=XCreateWindow(display,RootWindow(display,vi->screen),0,0,_width,_height,0,vi->depth,InputOutput,vi->visual,CWBorderPixel|CWColormap|CWEventMask,&attributes); wmDelete=XInternAtom(display,"WM_DELETE_WINDOW",true); XSetWMProtocols(display,window,&wmDelete,1); XSetStandardProperties(display,window,GetName(),GetName(),None,NULL,0,NULL); XMapRaised(display,window); XDefineCursor(display,window,CreateWindowedCursor()); } glXMakeCurrent(display,window,hRC); unsignedinttwidth,theight,depth; XGetGeometry(display,window,&winDummy,&x,&y,&twidth,&theight,&borderDummy,&depth); _colorDepth=(char)depth; _height=(short)twidth; _width=(short)theight; printf("Resolution%dx%d\n",twidth,theight); printf("Depth%d\n",depth); if(glXIsDirect(display,hRC)) { printf("Congrats,youhaveDirectRendering!\n"); } else { printf("Sorry,noDirectRenderingpossible!\n"); } _initializer->Initialize(); returntrue; } #endif #ifdef__APPLE__ boolOpenArena::Window::Open() { XVisualInfo*vi; Colormapcmap; intbestMode=0; intvidModeMajorVersion; intvidModeMinorVersion; intglxMajorVersion; intglxMinorVersion; intmodeNum; //XF86VidModeModeInfo**modes; AtomwmDelete; ::WindowwinDummy; unsignedintborderDummy; display=XOpenDisplay(0); screen=DefaultScreen(display); //XF86VidModeQueryVersion(display,&vidModeMajorVersion,&vidModeMinorVersion); //printf("XF86VidModeExtension-Version%d.%d\n",vidModeMajorVersion,vidModeMinorVersion); //XF86VidModeGetAllModeLines(display,screen,&modeNum,&modes); //vidMode=*modes[0]; inti; for(i=0;i<modeNum;i++) { //Addacheckforcolordepthhere //if((modes[i]->hdisplay==_width)&&(modes[i]->vdisplay==_height)) //{ //bestMode=i; //} } vi=glXChooseVisual(display,screen,attrListDbl); if(vi==NULL) { vi=glXChooseVisual(display,screen,attrListSgl); doubleBuffered=false; printf("OnlySinglebufferedVisual!\n"); } else { doubleBuffered=true; printf("GotDoublebufferedVisual!\n"); } glXQueryVersion(display,&glxMajorVersion,&glxMinorVersion); printf("glX-Version%d.%d\n",glxMajorVersion,glxMinorVersion); hRC=glXCreateContext(display,vi,0,GL_TRUE); cmap=XCreateColormap(display,RootWindow(display,vi->screen),vi->visual,AllocNone); attributes.colormap=cmap; attributes.border_pixel=0; attributes.event_mask=ExposureMask| KeyPressMask|KeyReleaseMask| ButtonPressMask|ButtonReleaseMask| //PointerMotionMask|ButtonMotionMask| StructureNotifyMask; if(_fullscreen) { //XF86VidModeSwitchToMode(display,screen,modes[bestMode]); //XF86VidModeSetViewPort(display,screen,0,0); //XFree(modes); attributes.override_redirect=true; window=XCreateWindow(display,RootWindow(display,vi->screen),0,0,_width,_height,0,vi->depth,InputOutput,vi->visual,CWBorderPixel|CWColormap|CWEventMask|CWOverrideRedirect,&attributes); XWarpPointer(display,None,window,0,0,0,0,0,0); XMapRaised(display,window); XGrabKeyboard(display,window,true,GrabModeAsync,GrabModeAsync,CurrentTime); XGrabPointer(display,window,true,ButtonPressMask,GrabModeAsync,GrabModeAsync,window,None,CurrentTime); XDefineCursor(display,window,CreateFullscreenCursor()); } else { window=XCreateWindow(display,RootWindow(display,vi->screen),0,0,_width,_height,0,vi->depth,InputOutput,vi->visual,CWBorderPixel|CWColormap|CWEventMask,&attributes); wmDelete=XInternAtom(display,"WM_DELETE_WINDOW",true); XSetWMProtocols(display,window,&wmDelete,1); XSetStandardProperties(display,window,GetName(),GetName(),None,NULL,0,NULL); XMapRaised(display,window); XDefineCursor(display,window,CreateWindowedCursor()); } glXMakeCurrent(display,window,hRC); unsignedinttwidth,theight,depth; XGetGeometry(display,window,&winDummy,&x,&y,&twidth,&theight,&borderDummy,&depth); _colorDepth=(char)depth; _height=(short)twidth; _width=(short)theight; printf("Resolution%dx%d\n",twidth,theight); printf("Depth%d\n",depth); if(glXIsDirect(display,hRC)) { printf("Congrats,youhaveDirectRendering!\n"); } else { printf("Sorry,noDirectRenderingpossible!\n"); } _initializer->Initialize(); returntrue; } #endif #ifdefWIN32 boolOpenArena::Window::Open() { unsignedintPixelFormat; WNDCLASSwc; DWORDdwExStyle; DWORDdwStyle; RECTWindowRect; WindowRect.left=(long)0; WindowRect.right=(long)_width; WindowRect.top=(long)0; WindowRect.bottom=(long)_height; instance=GetModuleHandle(NULL); wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC; wc.lpfnWndProc=(WNDPROC)WndProc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hInstance=instance; wc.hIcon=LoadIcon(NULL,IDI_WINLOGO); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hbrBackground=NULL; wc.lpszMenuName=NULL; wc.lpszClassName="OpenArenav0.1.0"; if(!RegisterClass(&wc)) { MessageBox(NULL,"FailedToRegisterTheWindowClass.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } if(_fullscreen) { DEVMODEdmScreenSettings; memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); dmScreenSettings.dmSize=sizeof(dmScreenSettings); dmScreenSettings.dmPelsWidth=_width; dmScreenSettings.dmPelsHeight=_height; dmScreenSettings.dmBitsPerPel=_colorDepth; dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { if(MessageBox(NULL,"TheRequestedFullscreenModeIsNotSupportedBy\nYourVideoCard.UseWindowedModeInstead?","OpenArena",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) _fullscreen=false; else { MessageBox(NULL,"ProgramWillNowClose.","ERROR",MB_OK|MB_ICONSTOP); returnfalse; } } } if(_fullscreen) { dwExStyle=WS_EX_APPWINDOW; dwStyle=WS_POPUP; ShowCursor(/*true*/false); } else { ShowCursor(false); dwExStyle=WS_EX_APPWINDOW|WS_EX_WINDOWEDGE; dwStyle=WS_OVERLAPPEDWINDOW; } AdjustWindowRectEx(&WindowRect,dwStyle,false,dwExStyle); if(!(window=CreateWindowEx(dwExStyle,"OpenArenav0.1.0",GetName(),WS_CLIPSIBLINGS|WS_CLIPCHILDREN|dwStyle,0,0,WindowRect.right-WindowRect.left,WindowRect.bottom-WindowRect.top,NULL,NULL,instance,NULL))) { Close(); MessageBox(NULL,"WindowCreationError.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } staticPIXELFORMATDESCRIPTORpfd={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,_colorDepth,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,PFD_MAIN_PLANE,0,0,0,0}; if(!(deviceContext=GetDC(window))) { Close(); MessageBox(NULL,"Can'tCreateAGLDeviceContext.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } if(!(PixelFormat=ChoosePixelFormat(deviceContext,&pfd))) { Close(); MessageBox(NULL,"Can'tFindASuitablePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } if(!SetPixelFormat(deviceContext,PixelFormat,&pfd)) { Close(); MessageBox(NULL,"Can'tSetThePixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } if(!(glContext=wglCreateContext(deviceContext))) { Close(); MessageBox(NULL,"Can'tCreateAGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } if(!wglMakeCurrent(deviceContext,glContext)) { Close(); MessageBox(NULL,"Can'tActivateTheGLRenderingContext.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } ShowWindow(window,SW_SHOW); SetForegroundWindow(window); SetFocus(window); _resizer->Resize(_width,_height); if(!_initializer->Initialize()) { Close(); MessageBox(NULL,"InitializationFailed.","ERROR",MB_OK|MB_ICONEXCLAMATION); returnfalse; } returntrue; } #endif boolOpenArena::Window::Open(stringtitle,intwidth,intheight,intbits,boolfullscreenflag) { _fullscreen=fullscreenflag; _width=width; _height=height; _colorDepth=bits; _name=title; returnOpen(); } OpenArena::Window::Window() { _initializer=newInitializer(); _resizer=newResizer(); } OpenArena::Window::~Window() { } voidOpenArena::Window::SetInitializer(OpenArena::Window::Initializer*initializer) { _initializer=initializer; } voidOpenArena::Window::SetResizer(OpenArena::Window::Resizer*resizer) { _resizer=resizer; } voidOpenArena::Window::Resizer::Resize(GLsizeiwidth,GLsizeiheight) { if(height==0) height=1; glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } intOpenArena::Window::Initializer::Initialize() { glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(0.0f,0.0f,0.0f,0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glBlendFunc(GL_SRC_ALPHA,GL_ONE); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); returntrue; } voidOpenArena::Window::Resize(GLsizeiwidth,GLsizeiheight) { _resizer->Resize(width,height); } #ifdef__linux Display*OpenArena::Window::GetDisplay() { returndisplay; } #endif #ifdef__APPLE__ Display*OpenArena::Window::GetDisplay() { returndisplay; } #endif #ifdef__linux Vec2iOpenArena::Window::GetMousePosition() { ::WindowrootWindow; ::WindowchildWindow; introotX; introotY; intmouseX; intmouseY; unsignedintmask; if(!XQueryPointer(display,window,&rootWindow,&childWindow,&rootX,&rootY,&mouseX,&mouseY,&mask)) { returnVec2i(-1,-1); } else { returnVec2i(mouseX,mouseY); } } #endif #ifdef__APPLE__ OpenArena::Vec2iOpenArena::Window::GetMousePosition() { ::WindowrootWindow; ::WindowchildWindow; introotX; introotY; intmouseX; intmouseY; unsignedintmask; if(!XQueryPointer(display,window,&rootWindow,&childWindow,&rootX,&rootY,&mouseX,&mouseY,&mask)) { returnVec2i(-1,-1); } else { returnVec2i(mouseX,mouseY); } } #endif #ifdefWIN32 OpenArena::Vec2iOpenArena::Window::GetMousePosition() { POINTpos; GetCursorPos(&pos); returnVec2i(pos.x,pos.y); } #endif #ifdef__linux voidOpenArena::Window::SetMousePosition(Vec2ipos) { Vec2imiddle=Vec2i(_width,_height)/2; XWarpPointer(display,None,window,0,0,0,0,middle.x,middle.y); } #endif #ifdef__APPLE__ voidOpenArena::Window::SetMousePosition(Vec2ipos) { Vec2imiddle=Vec2i(_width,_height)/2; XWarpPointer(display,None,window,0,0,0,0,middle.x,middle.y); } #endif #ifdefWIN32 voidOpenArena::Window::SetMousePosition(Vec2ipos) { SetCursorPos(pos.x,pos.y); } #endif #ifdef__linux CursorOpenArena::Window::CreateWindowedCursor() { returnCreateFullscreenCursor(); } CursorOpenArena::Window::CreateFullscreenCursor() { Pixmappixmap=XCreatePixmap(display,window,1,1,1); XColorcolor; color.pixel=0; color.red=0; color.flags=DoRed; Cursorcur=XCreatePixmapCursor(display,pixmap,pixmap,&color,&color,0,0); XFreePixmap(display,pixmap); returncur; } #endif #ifdef__APPLE__ CursorOpenArena::Window::CreateWindowedCursor() { returnCreateFullscreenCursor(); } CursorOpenArena::Window::CreateFullscreenCursor() { Pixmappixmap=XCreatePixmap(display,window,1,1,1); XColorcolor; color.pixel=0; color.red=0; color.flags=DoRed; Cursorcur=XCreatePixmapCursor(display,pixmap,pixmap,&color,&color,0,0); XFreePixmap(display,pixmap); returncur; } #endif