IntPtr WndEvents(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { switch (msg) { case USER32.WM_CREATE: return(IntPtr.Zero); case USER32.WM_ACTIVATEAPP: this.wActive = (wParam != IntPtr.Zero); return(IntPtr.Zero); case USER32.WM_CLOSE: USER32.DestroyWindow(this.wHandle); return(IntPtr.Zero); case USER32.WM_DESTROY: OPENGL32.wglMakeCurrent(this.dcHandle, IntPtr.Zero); OPENGL32.wglDeleteContext(this.wglContext); USER32.PostQuitMessage(0); return(IntPtr.Zero); default: return(USER32.DefWindowProc(hWnd, msg, wParam, lParam)); } }
public void Run( ) { USER32.MSG msg = new USER32.MSG( ); USER32.PeekMessage(out msg, this.hInstance, 0, 0, USER32.PM_NOREMOVE); while (msg.message != USER32.WM_QUIT) { if (USER32.PeekMessage(out msg, this.hInstance, 0, 0, USER32.PM_REMOVE)) { USER32.TranslateMessage(ref msg); USER32.DispatchMessage(ref msg); } else { if (this.canUpdate) { this.elapsedTime = ( float )((this.oldTime - this.newTime) / ( float )this.ticksPerSecond); this.Update(this.elapsedTime); this.canUpdate = false; } KERNEL32.QueryPerformanceCounter(ref newTime); if (this.newTime > this.oldTime) { OPENGL32.glClearColor(0, 0, 0, 0); OPENGL32.glClear(OPENGL32.GL_COLOR_BUFFER_BIT | OPENGL32.GL_DEPTH_BUFFER_BIT); OPENGL32.glLoadIdentity( ); this.Draw( ); OPENGL32.glFlush( ); GDI32.SwapBuffers(this.dcHandle); this.oldTime += this.ticksPerFrame; if (this.oldTime < this.newTime) { this.oldTime = (this.newTime + this.ticksPerFrame); } this.canUpdate = true; } } } }
public void InitializeOpenGL( ) { this.dcHandle = USER32.GetDC(this.wHandle); GDI32.PIXELFORMATDESCRIPTOR pfd = new GDI32.PIXELFORMATDESCRIPTOR( ); pfd.nSize = ( ushort )Marshal.SizeOf(pfd); pfd.nVersion = 1; pfd.dwFlags = (OPENGL32.PFD_DRAW_TO_WINDOW | OPENGL32.PFD_SUPPORT_OPENGL | OPENGL32.PFD_DOUBLEBUFFER); pfd.iPixelType = OPENGL32.PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 16; pfd.cStencilBits = 8; pfd.iLayerType = OPENGL32.PFD_MAIN_PLANE; int pixelFormat = GDI32.ChoosePixelFormat(this.dcHandle, pfd); GDI32.SetPixelFormat(this.dcHandle, pixelFormat, pfd); this.wglContext = OPENGL32.wglCreateContext(this.dcHandle); OPENGL32.wglMakeCurrent(this.dcHandle, this.wglContext); this.Initialize( ); }