HGLRC CL_GL1CreationHelper::create_opengl3_context(HGLRC share_context, int major_version, int minor_version) { set_active(); ptr_wglCreateContextAttribsARB wglCreateContextAttribsARB = (ptr_wglCreateContextAttribsARB) wglGetProcAddress("wglCreateContextAttribsARB"); reset_active(); HGLRC opengl3_context = 0; if (wglCreateContextAttribsARB) { std::vector<int> int_attributes; int_attributes.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB); int_attributes.push_back(major_version); int_attributes.push_back(WGL_CONTEXT_MINOR_VERSION_ARB); int_attributes.push_back(minor_version); //int_attributes.push_back(WGL_CONTEXT_LAYER_PLANE_ARB); //int_attributes.push_back(layer_plane); //int_attributes.push_back(WGL_CONTEXT_FLAGS_ARB); //int_attributes.push_back(flags); //int_attributes.push_back(WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB); //int_attributes.push_back(forward_compatible_bit); int_attributes.push_back(0); opengl3_context = wglCreateContextAttribsARB(hdc, share_context, &int_attributes[0]); } return opengl3_context; }
void CGSH_HleOglWin32::InitializeImpl() { m_dc = GetDC(m_outputWnd->m_hWnd); unsigned int pf = ChoosePixelFormat(m_dc, &m_pfd); SetPixelFormat(m_dc, pf, &m_pfd); m_context = wglCreateContext(m_dc); wglMakeCurrent(m_dc, m_context); auto result = glewInit(); assert(result == GLEW_OK); #ifdef GLES_COMPATIBILITY if(wglCreateContextAttribsARB != nullptr) { auto prevContext = m_context; static const int attributes[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 2, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0 }; m_context = wglCreateContextAttribsARB(m_dc, nullptr, attributes); assert(m_context != nullptr); wglMakeCurrent(m_dc, m_context); auto deleteResult = wglDeleteContext(prevContext); assert(deleteResult == TRUE); } #endif CGSH_HleOgl::InitializeImpl(); }
void EnableDrawing (HGLRC *hRC) { /** * Edited by Cool Breeze on 16th October 2013 * + Updated the Pixel Format to support 24-bitdepth buffers * + Correctly create a GL 3.x compliant context */ HGLRC LegacyRC; PIXELFORMATDESCRIPTOR pfd; int iFormat; enigma::window_hDC = GetDC (hWnd); 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 = 24; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat (enigma::window_hDC, &pfd); if (iFormat==0) { show_error("Failed to set the format of the OpenGL graphics device.",1); } SetPixelFormat ( enigma::window_hDC, iFormat, &pfd ); LegacyRC = wglCreateContext( enigma::window_hDC ); wglMakeCurrent( enigma::window_hDC, LegacyRC ); // -- Initialise GLEW GLenum err = glewInit(); if (GLEW_OK != err) { return; } // -- Define an array of Context Attributes int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, WGL_CONTEXT_FLAGS_ARB, 0, 0 }; if ( wglewIsSupported("WGL_ARB_create_context") ) { *hRC = wglCreateContextAttribsARB( enigma::window_hDC,0, attribs ); wglMakeCurrent( NULL,NULL ); wglDeleteContext( LegacyRC ); wglMakeCurrent(enigma::window_hDC, *hRC ); } else // Unable to get a 3.0 Core Context, use the Legacy 1.x context { *hRC = LegacyRC; } //TODO: This never reports higher than 8, but display_aa should be 14 if 2,4,and 8 are supported and 8 only when only 8 is supported glGetIntegerv(GL_MAX_SAMPLES_EXT, &enigma_user::display_aa); }
bool cOpenGl::CreateGLContext(CDC* pDC) { PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int nPixelFormat = ChoosePixelFormat(pDC->m_hDC, &pfd); if (nPixelFormat == 0) return false; BOOL bResult = SetPixelFormat (pDC->m_hDC, nPixelFormat, &pfd); if (!bResult) return false; HGLRC tempContext = wglCreateContext(pDC->m_hDC); wglMakeCurrent(pDC->m_hDC, tempContext); GLenum err = glewInit(); if (GLEW_OK != err) { AfxMessageBox(_T("GLEW is not initialized!")); } int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 1, WGL_CONTEXT_FLAGS_ARB, 0, 0 }; if(wglewIsSupported("WGL_ARB_create_context") == 1) { m_hrc = wglCreateContextAttribsARB(pDC->m_hDC,0, attribs); wglMakeCurrent(NULL,NULL); wglDeleteContext(tempContext); wglMakeCurrent(pDC->m_hDC, m_hrc); } else { //It's not possible to make a GL 3.x context. Use the old style context (GL 2.1 and before) m_hrc = tempContext; } //Checking GL version const GLubyte *GLVersionString = glGetString(GL_VERSION); //Or better yet, use the GL3 way to get the version number int OpenGLVersion[2]; glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0]); glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1]); if (!m_hrc) return false; return true; }
void initializeGL() { HGLRC tempContext = wglCreateContext(ghDC); wglMakeCurrent(ghDC, tempContext); GLenum err = glewInit(); if (err != GLEW_OK) { // failed to initialize GLEW! } // My card currently only supports OpenGL 4.1 -- jbl int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 4, WGL_CONTEXT_MINOR_VERSION_ARB, 1, WGL_CONTEXT_FLAGS_ARB, 0, 0 }; if (wglewIsSupported("WGL_ARB_create_context") == 1) { ghRC = wglCreateContextAttribsARB(ghDC, 0, attribs); wglMakeCurrent(NULL, NULL); wglDeleteContext(tempContext); wglMakeCurrent(ghDC, ghRC); } else { // It's not possible to make a GL 3.x context. Use the old style context (GL 2.1 and before) ghRC = tempContext; } int OpenGLVersion[2]; glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0]); glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1]); }
HRESULT InitContext() { int iMajorVersion=3; int iMinorVersion=0; HDC hDC = GetDC(g_hWnd); PIXELFORMATDESCRIPTOR pfd; ZeroMemory(&pfd,sizeof(pfd)); bool bError=false; if(WGLEW_ARB_create_context && WGLEW_ARB_pixel_format) { const int iPixelFormatAttribList[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, 0 // End of attributes list }; int iContextAttribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, iMajorVersion, WGL_CONTEXT_MINOR_VERSION_ARB, iMinorVersion, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 // End of attributes list }; int iPixelFormat, iNumFormats; wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats); if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; hRC = wglCreateContextAttribsARB(hDC, 0, iContextAttribs); // If everything went OK if(hRC) wglMakeCurrent(hDC, hRC); else bError = true; } else bError = true; if(bError) { // Generate error messages char sErrorMessage[255], sErrorTitle[255]; sprintf(sErrorMessage, "OpenGL %d.%d is not supported! Please download latest GPU drivers!", iMajorVersion, iMinorVersion); sprintf(sErrorTitle, "OpenGL %d.%d Not Supported", iMajorVersion, iMinorVersion); MessageBoxA(g_hWnd, sErrorMessage, sErrorTitle, MB_ICONINFORMATION); return false; } return S_OK; }
/* create30Context creates an OpenGL context and attaches it to the window provided by the HWND. This method currently creates and OpenGL 3.2 context by default, but will default to an OpenGL 2.1 capable context if the OpenGL 3.2 context cannot be created. */ bool OpenGLContext::create30Context(HWND hwnd) { this->hwnd = hwnd; // Set the hwnd for our window hdc = GetDC(hwnd); // Get the device context for our window PIXELFORMATDESCRIPTOR pfd; // Create a new PIXELFORMATDESCRIPTOR (PFD) memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); // Clear our PFD pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); // Set the size of the PFD to the size of the class pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; //Enable double buffering, opengl support, and drawing to a window pfd.iPixelType = PFD_TYPE_RGBA; // Set our application to use RGBA pixels pfd.cColorBits = 32; // Give us 32 bits of color information (the higher, the more colors) pfd.cDepthBits = 32; // Give us 32 bits of depth information (the higher, the more depth levels) pfd.iLayerType = PFD_MAIN_PLANE; // Set the layer of the PFD int nPixelFormat = ChoosePixelFormat(hdc, &pfd); // Check if our PFD is valid and get a pixel format back if (nPixelFormat == 0) // If it fails return false; bool bResult = SetPixelFormat(hdc, nPixelFormat, &pfd); // Try and set the pixel format based on our PFD if (!bResult) // If it fails return false; HGLRC tempOpenGLContext = wglCreateContext(hdc); // Create an OpenGL 2.1 context for our device context wglMakeCurrent(hdc, tempOpenGLContext); // Make the OpenGL 2.1 context current and active GLenum error = glewInit(); // Enable GLEW if (error != GLEW_OK) // If GLEW fails return false; int attributes[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, // Set the MAJOR version of OpenGL to 3 WGL_CONTEXT_MINOR_VERSION_ARB, 2, // Set the MINOR version of OpenGL to 2 WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, // Set our OpenGL context to be forward compatible 0 }; if (wglewIsSupported("WGL_ARB_create_context") == 1) { // If the OpenGL 3.x context creation extension is available hrc = wglCreateContextAttribsARB(hdc, NULL, attributes); // Create and OpenGL 3.x context based on the given attributes wglMakeCurrent(NULL, NULL); // Remove the temporary context from being active wglDeleteContext(tempOpenGLContext); // Delete the temporary OpenGL 2.1 context wglMakeCurrent(hdc, hrc); // Make our OpenGL 3.0 context current } else { hrc = tempOpenGLContext; // If we didn't have support for OpenGL 3.x and up, use the OpenGL 2.1 context } int glVersion[2] = { -1, -1 }; // Set some default values for the version glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]); // Get back the OpenGL MAJOR version we are using glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]); // Get back the OpenGL MAJOR version we are using //std::cout << "Using OpenGL: " << glVersion[0] << "." << glVersion[1] << std::endl; // Output which version of OpenGL we are using On Windows, you won’t get a console for a Win32 Application, but a nifty trick to get console output, is to open up Command Prompt, navigate to your directory with your executable file, and use something like: “program.exe > temp.txt” return true; // We have successfully created a context, return true }
internal void Win32InitOpenGL(HDC WindowDC) { Win32LoadWGLExtensions(); HGLRC OpenGLRC = 0; bool32 ModernContext = true; if(wglCreateContextAttribsARB) { int Win32OpenGLAttribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 1, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB #if LUDUS_INTERNAL | WGL_CONTEXT_DEBUG_BIT_ARB #endif , WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0 }; Win32SetPixelFormat(WindowDC); OpenGLRC = wglCreateContextAttribsARB(WindowDC, 0, Win32OpenGLAttribs); } if(!OpenGLRC) { ModernContext = false; OpenGLRC = wglCreateContext(WindowDC); } if(wglMakeCurrent(WindowDC, OpenGLRC)) { InitOpenGL(ModernContext); if(wglSwapInterval) { wglSwapInterval(1); } } else { Win32Log("Couldnt set OpenGL context"); LOG_FORMATTED_ERROR(4096); } }
void WindowGL3::setupDC() { if (!wglChoosePixelFormatARB || !wglCreateContextAttribsARB) { Window::setupDC(); return; } int nPixCount = 0; int nPixelFormat = -1; int pixAttribs[] = { WGL_SUPPORT_OPENGL_ARB, 1, WGL_DRAW_TO_WINDOW_ARB, 1, WGL_ACCELERATION_ARB, 1, WGL_RED_BITS_ARB, 8, WGL_GREEN_BITS_ARB, 8, WGL_BLUE_BITS_ARB, 8, WGL_DEPTH_BITS_ARB, 16, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, 0 }; wglChoosePixelFormatARB(mHDC, pixAttribs, NULL, 1, &nPixelFormat, (UINT*)&nPixCount); if (nPixelFormat == -1) { std::cerr << "wglChoosePixelFormatARB failed\n"; mHDC = 0; return; } if (mSettings.profile == Context::GL32) { mSettings.context.vMajor = 3; mSettings.context.vMinor = 2; } GLint attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, mSettings.context.vMajor, WGL_CONTEXT_MINOR_VERSION_ARB, mSettings.context.vMinor, 0 }; mHGLRC = wglCreateContextAttribsARB(mHDC, 0, attribs); if (!mHGLRC) { std::cerr << "wglCreateContextAttribsARB failed\n"; return; } }
//================================================================================================= void eve::ogl::SubContext::init(void) { // Get DC from window handle. m_hDC = ::GetDC(m_hWnd); if (m_hDC == 0) { EVE_LOG_ERROR("Paint device cannot be null. GetDC() failed %s", eve::mess::get_error_msg().c_str()); EVE_ASSERT_FAILURE; } // Apply pixel format to DC. if (::SetPixelFormat(m_hDC, eve::ogl::Context::get_pixel_format_ID(), &eve::ogl::Context::get_pixel_format_descriptor()) == 0) { EVE_LOG_ERROR("Unable to link pixel format to DC, SetPixelFormat() failed %s", eve::mess::get_error_msg().c_str()); EVE_ASSERT_FAILURE; } #ifndef NDEBUG static const int contextAttribs[] = { WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB, 0 }; #else static const int contextAttribs[] = { 0 }; #endif // Create context (GLRC). m_hGLRC = wglCreateContextAttribsARB(m_hDC, eve::ogl::Context::get_handle(), contextAttribs); if (m_hGLRC == 0) { EVE_LOG_ERROR("Unable to create rendering context, wglCreateContext() failed %s", eve::mess::get_error_msg().c_str()); EVE_ASSERT_FAILURE; } // Make context current (has to be activated here to enforce DC bound). if (::wglMakeCurrent(m_hDC, m_hGLRC) == 0) { EVE_LOG_ERROR("Unable to attach context, wglMakeCurrent() failed %s", eve::mess::get_error_msg().c_str()); EVE_ASSERT_FAILURE; } // Init OpenGL extensions for this context. eve::ogl::Context::init_OpenGL(); // Release context. if (::wglMakeCurrent(0, 0) == 0) { EVE_LOG_ERROR("Unable to detach context, wglMakeCurrent(0, 0) failed %s", eve::mess::get_error_msg().c_str()); EVE_ASSERT_FAILURE; } }
COffscreenGLContext::COffscreenGLContext() { //! this creates a 2nd OpenGL context on the >onscreen< window/HDC //! so don't render to the the default framebuffer (always bind FBOs,DLs,...) !!! if (!mainGlActiveTexture) mainGlActiveTexture = (PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture"); //! get the main (onscreen) GL context HGLRC mainRC = wglGetCurrentContext(); hdc = wglGetCurrentDC(); if (!hdc || !mainRC) { throw opengl_error("Couldn't create an offscreen GL context: wglGetCurrentDC failed!"); } int status = TRUE; offscreenRC = NULL; #ifdef WGL_ARB_create_context if (wglCreateContextAttribsARB) { static const int contextAttribs[] = { WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB, 0 }; offscreenRC = wglCreateContextAttribsARB(hdc, mainRC, contextAttribs); if (!offscreenRC) LOG_L(L_WARNING, "Couldn't create an offscreen GL context: wglCreateContextAttribsARB failed!"); } #endif if (!offscreenRC) { //! create a 2nd GL context offscreenRC = wglCreateContext(hdc); if (!offscreenRC) { throw opengl_error("Couldn't create an offscreen GL context: wglCreateContext failed!"); } //! share the GL resources (textures,DLists,shaders,...) if (!wglMakeCurrent(NULL, NULL)) throw opengl_error("Could not deactivate rendering context"); status = wglShareLists(mainRC, offscreenRC); if (!wglMakeCurrent(hdc, mainRC)) throw opengl_error("Could not activate rendering context"); } if (!status) { DWORD err = GetLastError(); char msg[256]; SNPRINTF(msg, 255, "Couldn't create an offscreen GL context: wglShareLists failed (error: %i)!", (int)err); throw opengl_error(msg); } }
static HGLRC CreateOpenGLContextOnDC( const HDC hdc, const bool debugContext ) { HGLRC m_hrc = NULL; int useOpenGL32 = r_useOpenGL32.GetInteger(); for ( int i = 0; i < 2; i++ ) { const int glMajorVersion = ( useOpenGL32 != 0 ) ? 3 : 2; const int glMinorVersion = ( useOpenGL32 != 0 ) ? 2 : 0; const int glDebugFlag = debugContext ? WGL_CONTEXT_DEBUG_BIT_ARB : 0; const int glProfileMask = ( useOpenGL32 != 0 ) ? WGL_CONTEXT_PROFILE_MASK_ARB : 0; const int glProfile = ( useOpenGL32 == 1 ) ? WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB : ( ( useOpenGL32 == 2 ) ? WGL_CONTEXT_CORE_PROFILE_BIT_ARB : 0 ); const int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, glMajorVersion, WGL_CONTEXT_MINOR_VERSION_ARB, glMinorVersion, WGL_CONTEXT_FLAGS_ARB, glDebugFlag, glProfileMask, glProfile, 0 }; m_hrc = wglCreateContextAttribsARB( hdc, 0, attribs ); if ( m_hrc != NULL ) { idLib::Printf( "created OpenGL %d.%d context\n", glMajorVersion, glMinorVersion ); break; } idLib::Printf( "failed to create OpenGL %d.%d context\n", glMajorVersion, glMinorVersion ); useOpenGL32 = 0; // fall back to OpenGL 2.0 } if ( m_hrc == NULL ) { int err = GetLastError(); switch( err ) { case ERROR_INVALID_VERSION_ARB: idLib::Printf( "ERROR_INVALID_VERSION_ARB\n" ); break; case ERROR_INVALID_PROFILE_ARB: idLib::Printf( "ERROR_INVALID_PROFILE_ARB\n" ); break; default: idLib::Printf( "unknown error: 0x%x\n", err ); break; } } return m_hrc; }
HGLRC OpenGLCreationHelper::create_opengl3_context(HGLRC share_context, int major_version, int minor_version, const OpenGLContextDescription &gldesc) { set_active(); ptr_wglCreateContextAttribsARB wglCreateContextAttribsARB = (ptr_wglCreateContextAttribsARB)wglGetProcAddress("wglCreateContextAttribsARB"); HGLRC opengl3_context = 0; if (wglCreateContextAttribsARB) { std::vector<int> int_attributes; int_attributes.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB); int_attributes.push_back(major_version); int_attributes.push_back(WGL_CONTEXT_MINOR_VERSION_ARB); int_attributes.push_back(minor_version); int_attributes.push_back(0x2093); // WGL_CONTEXT_LAYER_PLANE_ARB int_attributes.push_back(gldesc.get_layer_plane()); int_attributes.push_back(0x2094); // WGL_CONTEXT_FLAGS_ARB int flags = 0; if (gldesc.get_debug()) flags |= 0x1; // WGL_CONTEXT_DEBUG_BIT_ARB if (gldesc.get_forward_compatible()) // WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB flags |= 0x2; int_attributes.push_back(flags); int_attributes.push_back(0x9126); // WGL_CONTEXT_PROFILE_MASK_ARB flags = 0; if (gldesc.get_core_profile()) flags |= 0x1; // WGL_CONTEXT_CORE_PROFILE_BIT_ARB if (gldesc.get_compatibility_profile()) // WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB flags |= 0x2; int_attributes.push_back(flags); int_attributes.push_back(0); opengl3_context = wglCreateContextAttribsARB(hdc, share_context, &int_attributes[0]); } reset_active(); return opengl3_context; }
bool zglCoreContextWin::CreateCoreContext() { if ( !m_tempContext ) return false; bool makeCurResult = wglMakeCurrent( m_hDC, m_tempContext ); bool bCore = GL_LoadCreateWinCoreContext(); if (!bCore) return false; GetGLVersion(&m_major, &m_minor); if( !makeCurResult || m_major < MAJOR || ( m_major == MAJOR && m_minor < MINOR ) ) { wglMakeCurrent(NULL,NULL); wglDeleteContext(m_tempContext); return false; } int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, m_major, WGL_CONTEXT_MINOR_VERSION_ARB, m_minor, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0 }; m_Context = wglCreateContextAttribsARB(m_hDC,0, attribs); if ( !m_Context ) return false; LoadGL(); wglMakeCurrent(NULL,NULL); wglDeleteContext(m_tempContext); return true; }
static inline HGLRC gl_init_context(HDC hdc) { #ifdef _DEBUG if (wgl_ext_ARB_create_context) { HGLRC hglrc = wglCreateContextAttribsARB(hdc, 0, attribs); if (!hglrc) { blog(LOG_ERROR, "wglCreateContextAttribsARB failed, %u", GetLastError()); return NULL; } if (!wgl_make_current(hdc, hglrc)) { wglDeleteContext(hglrc); return NULL; } return hglrc; } #endif return gl_init_basic_context(hdc); }
WGLContextImpl::WGLContextImpl(const WindowHandle& windowHandle) : ContextImpl(windowHandle) { deviceContext = GetDC(windowHandle.hwnd); PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int nPixelFormat = ChoosePixelFormat(deviceContext, &pfd); if (nPixelFormat == 0) { MessageBox(NULL, "ChoosePixelFormat Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); } int bResult = SetPixelFormat(deviceContext, nPixelFormat, &pfd); if (!bResult) { MessageBox(NULL, "SetPixelFormat Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); } HGLRC tempContext = wglCreateContext(deviceContext); wglMakeCurrent(deviceContext, tempContext); int attributes[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 3, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 }; renderContext = wglCreateContextAttribsARB(deviceContext, NULL, attributes); wglMakeCurrent(NULL, NULL); wglDeleteContext(tempContext); wglMakeCurrent(deviceContext, renderContext); }
bool GLContext::create30Context() { running = true; render = NULL; hdc = GetDC(hwnd); PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL;// | PFD_DRAW_TO_WINDOW; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int nPixelFormat = ChoosePixelFormat(hdc, &pfd); if (nPixelFormat == 0) return false; int bResult = SetPixelFormat(hdc, nPixelFormat, &pfd); if (!bResult) return false; HGLRC tempOGLWidget = wglCreateContext(hdc); wglMakeCurrent(hdc, tempOGLWidget); GLenum error = glewInit(); if (error != GLEW_OK) return false; int attributes[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 4, WGL_CONTEXT_MINOR_VERSION_ARB, 1, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 }; if (wglewIsSupported("WGL_ARB_create_context") == 1) { hrc = wglCreateContextAttribsARB(hdc, NULL, attributes); wglMakeCurrent(NULL, NULL); wglDeleteContext(tempOGLWidget); wglMakeCurrent(hdc, hrc); } else { hrc = tempOGLWidget; } int glVersion[2] = {-1, -1}; glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]); glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]); std::cout << "Using OpenGL: " << glVersion[0] << "." << glVersion[1] << std::endl; // Output which version of OpenGL we are using render = new Render(); render->Init(); SetWindowLongPtr( hwnd, GWLP_USERDATA, reinterpret_cast<long>(render)); return true; // We have successfully created a context, return true }
// 创建opengl窗口 BOOL GLWindow::CreateGlWnd(const char* title, int x, int y, int width, int height) { m_width = width; m_height = height; RECT windowRect = { 0, 0, width, height }; DWORD windowStyle = WS_OVERLAPPEDWINDOW; // 预留做全屏 DWORD windowExStyle = WS_EX_APPWINDOW; // 扩展样式 // 全屏处理 - 预留 // 窗口样式 //windowStyle = WS_POPUP; //windowExStyle |= WS_EX_TOPMOST; ::AdjustWindowRectEx(&windowRect, windowStyle, 0, windowExStyle); // 调整窗口 CreateEx(0, "OpenGL", title, WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, x, y, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, NULL, NULL); if (!(m_hDc = GetDC(m_hWnd))) // 获取DC { DestroyGL(); // 销毁 return FALSE; // 不能获取DC } m_timerFrame = 1000; // 初始化 GLuint PixelFormat; static PIXELFORMATDESCRIPTOR pdf = { sizeof(PIXELFORMATDESCRIPTOR), // 结构体大小 1, // 版本号 PFD_DRAW_TO_WINDOW | // 支持窗口 PFD_SUPPORT_OPENGL | // 支持opengl PFD_DOUBLEBUFFER, // 支持双缓冲 PFD_TYPE_RGBA, // 申请RGBA格式 32, // 色彩深度 0, 0, 0, 0, 0, 0, // 忽略的色彩位 0, // 无alpha缓存 0, // 无shift Bit 0, // 无累加缓存 0, 0, 0, 0, // 忽略聚集位 24, // 16位Z-缓存 8, // 无蒙版缓存 0, // 无辅助缓存 PFD_MAIN_PLANE, // 主绘图层 0, // Reserved 0, 0, 0 // 忽略层遮罩 }; if (!(PixelFormat = ChoosePixelFormat(m_hDc, &pdf))) // 寻找相应像素格式 { DestroyGL(); // 销毁 // printf("1====error choose===="); return FALSE; } if (!SetPixelFormat(m_hDc, PixelFormat, &pdf)) { DestroyGL(); // printf("1====error choose===="); return FALSE; // 不能设置像素格式 } HGLRC tempContext; if (!(tempContext = wglCreateContext(m_hDc))) { DestroyGL(); // printf("2====error create context===="); return FALSE; // 不能获得着色描述表 } if (!wglMakeCurrent(m_hDc, tempContext)) // 开启低版本opengl { DestroyGL(); // printf("3========"); return FALSE; // 不能激活当前opengl渲染描述表 } if (GLEW_OK != glewInit()) { DestroyGL(); return FALSE; // glew初始化失败 } // 开启 opengl 4.3 支持 GLint attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 4, // 主版本4 WGL_CONTEXT_MINOR_VERSION_ARB, 3, // 次版本号3 WGL_CONTEXT_PROFILE_MASK_ARB,WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,//要求返回兼容模式环境,如果不指定或指定为WGL_CONTEXT_CORE_PROFILE_BIT_ARB会返回只包含核心功能的环境 0 }; if (wglewIsSupported("WGL_ARB_create_context") == 1) { m_hRc = wglCreateContextAttribsARB(m_hDc, 0, attribs); wglMakeCurrent(NULL, NULL); wglDeleteContext(tempContext); int result = wglMakeCurrent(m_hDc, m_hRc); // 设置opengl 4.3 if (result != 1) { return FALSE; } } else { // 不支持opengl4.X 还原为opengl 2.1 m_hRc = tempContext; } //RECT rect; // 客户区大小 //::GetClientRect(m_hWnd, &rect); //ResizeGLScene(rect.right - rect.left, rect.bottom - rect.top); // 设置GL屏幕 (注意,这里只使用客户区计算) ResizeGLScene(width, height); if (!initGL()) // 初始化opengl { DestroyGL(); return FALSE; } // 设定计时器 每秒刷新60次 SetTimer(m_hWnd, m_timerFrame, 1000 / 60, NULL); return TRUE; }
LRESULT WindowsEventLoop::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { int zDelta=0; switch(uMsg) { case WM_CREATE: { m_hdc = GetDC(hWnd); SetupPixelFormat(); int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, 0}; HGLRC tmpContext = wglCreateContext(m_hdc); wglMakeCurrent(m_hdc, tmpContext); wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress("wglCreateContextAttribsARB"); if (!wglCreateContextAttribsARB) { //m_logger->Print(CLogger::Failure,"OpenGL 3.0 is not supported, falling back to GL 2.1"); m_hglrc = tmpContext; } else { m_hglrc = wglCreateContextAttribsARB(m_hdc, 0, attribs); wglDeleteContext(tmpContext); } wglMakeCurrent(m_hdc, m_hglrc); m_isRunning = true; } break; case WM_DESTROY: case WM_CLOSE: wglMakeCurrent(m_hdc, NULL); wglDeleteContext(m_hglrc); m_isRunning = false; PostQuitMessage(0); return 0; break; case WM_SIZE: { m_height = HIWORD(lParam); m_width = LOWORD(lParam); m_pActivityHandler->onDeactivate(); Render::SetScreenSize(m_width,m_height); m_pActivityHandler->onActivate(); } break; case WM_KEYDOWN: Global::pContext->pInputService->GetKeyboard()->HandleKeyDown(Global::pContext->pInputService->GetKeyboard()->TranslateCode(wParam)); break; case WM_KEYUP: Global::pContext->pInputService->GetKeyboard()->HandleKeyUp(Global::pContext->pInputService->GetKeyboard()->TranslateCode(wParam)); break; case WM_MOUSEWHEEL: zDelta = GET_WHEEL_DELTA_WPARAM(wParam); //m_input->m_currMouse->Wheel+=zDelta; break; case WM_KILLFOCUS: m_enabled=true; break; case WM_SETFOCUS: m_enabled=false; break; default: break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
/////////////////////////////////////////////////////////////////////////////// // Setup the actual window and related state. // Create the window, find a pixel format, create the OpenGL context bool SetupWindow(int nWidth, int nHeight) { bool bRetVal = true; int nWindowX = 0; int nWindowY = 0; int nPixelFormat = -1; PIXELFORMATDESCRIPTOR pfd; DWORD dwExtStyle; DWORD dwWindStyle; HINSTANCE g_hInstance = GetModuleHandle(NULL); TCHAR szWindowName[50] = TEXT("Block Redux"); TCHAR szClassName[50] = TEXT("OGL_CLASS"); // setup window class g_windClass.lpszClassName = szClassName; // Set the name of the Class g_windClass.lpfnWndProc = (WNDPROC)WndProc; g_windClass.hInstance = g_hInstance; // Use this module for the module handle g_windClass.hCursor = LoadCursor(NULL, IDC_ARROW);// Pick the default mouse cursor g_windClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);// Pick the default windows icons g_windClass.hbrBackground = NULL; // No Background g_windClass.lpszMenuName = NULL; // No menu for this window g_windClass.style = CS_HREDRAW | CS_OWNDC | // set styles for this class, specifically to catch CS_VREDRAW; // window redraws, unique DC, and resize g_windClass.cbClsExtra = 0; // Extra class memory g_windClass.cbWndExtra = 0; // Extra window memory // Register the newly defined class if(!RegisterClass( &g_windClass )) bRetVal = false; dwExtStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwWindStyle = WS_OVERLAPPEDWINDOW; ShowCursor(TRUE); g_windowRect.left = nWindowX; g_windowRect.right = nWindowX + nWidth; g_windowRect.top = nWindowY; g_windowRect.bottom = nWindowY + nHeight; // Setup window width and height AdjustWindowRectEx(&g_windowRect, dwWindStyle, FALSE, dwExtStyle); //Adjust for adornments int nWindowWidth = g_windowRect.right - g_windowRect.left; int nWindowHeight = g_windowRect.bottom - g_windowRect.top; // Create window g_hWnd = CreateWindowEx(dwExtStyle, // Extended style szClassName, // class name szWindowName, // window name dwWindStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,// window stlye nWindowX, // window position, x nWindowY, // window position, y nWindowWidth, // height nWindowHeight, // width NULL, // Parent window NULL, // menu g_hInstance, // instance NULL); // pass this to WM_CREATE // now that we have a window, setup the pixel format descriptor g_hDC = GetDC(g_hWnd); // Set a dummy pixel format so that we can get access to wgl functions SetPixelFormat( g_hDC, 1,&pfd); // Create OGL context and make it current g_hRC = wglCreateContext( g_hDC ); wglMakeCurrent( g_hDC, g_hRC ); if (g_hDC == 0 || g_hDC == 0) { bRetVal = false; printf("!!! An error occured creating an OpenGL window.\n"); } // Setup GLEW which loads OGL function pointers GLenum err = glewInit(); if (GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ bRetVal = false; printf("Error: %s\n", glewGetErrorString(err)); } const GLubyte *oglVersion = glGetString(GL_VERSION); printf("This system supports OpenGL Version %s.\n", oglVersion); // Now that extensions are setup, delete window and start over picking a real format. wglMakeCurrent(NULL, NULL); wglDeleteContext(g_hRC); ReleaseDC(g_hWnd, g_hDC); DestroyWindow(g_hWnd); // Create the window again g_hWnd = CreateWindowEx(dwExtStyle, // Extended style szClassName, // class name szWindowName, // window name dwWindStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,// window stlye nWindowX, // window position, x nWindowY, // window position, y nWindowWidth, // height nWindowHeight, // width NULL, // Parent window NULL, // menu g_hInstance, // instance NULL); // pass this to WM_CREATE g_hDC = GetDC(g_hWnd); int nPixCount = 0; // Specify the important attributes we care about int pixAttribs[] = { WGL_SUPPORT_OPENGL_ARB, 1, // Must support OGL rendering WGL_DRAW_TO_WINDOW_ARB, 1, // pf that can run a window WGL_ACCELERATION_ARB, 1, // must be HW accelerated WGL_RED_BITS_ARB, 8, // 8 bits of red precision in window WGL_GREEN_BITS_ARB, 8, // 8 bits of green precision in window WGL_BLUE_BITS_ARB, 8, // 8 bits of blue precision in window WGL_DEPTH_BITS_ARB, 16, // 16 bits of depth precision for window WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, // pf should be RGBA type 0}; // NULL termination // Ask OpenGL to find the most relevant format matching our attribs // Only get one format back. wglChoosePixelFormatARB(g_hDC, &pixAttribs[0], NULL, 1, &nPixelFormat, (UINT*)&nPixCount); if(nPixelFormat == -1) { // Couldn't find a format, perhaps no 3D HW or drivers are installed g_hDC = 0; g_hDC = 0; bRetVal = false; printf("!!! An error occurred trying to find a pixel format with the requested attribs.\n"); } else { // Got a format, now set it as the current one SetPixelFormat( g_hDC, nPixelFormat, &pfd ); GLint attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 3, 0 }; g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); if (g_hRC == NULL) { printf("!!! Could not create an OpenGL 3.3 context.\n"); attribs[3] = 2; g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); if (g_hRC == NULL) { printf("!!! Could not create an OpenGL 3.2 context.\n"); attribs[3] = 1; g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); if (g_hRC == NULL) { printf("!!! Could not create an OpenGL 3.1 context.\n"); attribs[3] = 0; g_hRC = wglCreateContextAttribsARB(g_hDC, 0, attribs); if (g_hRC == NULL) { printf("!!! Could not create an OpenGL 3.0 context.\n"); printf("!!! OpenGL 3.0 and higher are not supported on this system.\n"); } } } } wglMakeCurrent( g_hDC, g_hRC ); } if (g_hDC == 0 || g_hDC == 0) { bRetVal = false; printf("!!! An error occured creating an OpenGL window.\n"); } // If everything went as planned, display the window if( bRetVal ) { ShowWindow( g_hWnd, SW_SHOW ); SetForegroundWindow( g_hWnd ); SetFocus( g_hWnd ); g_ContinueRendering = true; } return bRetVal; }
GLboolean glewCreateContext (struct createParams* params) { WNDCLASS wc; PIXELFORMATDESCRIPTOR pfd; /* register window class */ ZeroMemory(&wc, sizeof(WNDCLASS)); wc.hInstance = GetModuleHandle(NULL); wc.lpfnWndProc = DefWindowProc; wc.lpszClassName = "GLEW"; if (0 == RegisterClass(&wc)) return GL_TRUE; /* create window */ wnd = CreateWindow("GLEW", "GLEW", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL); if (NULL == wnd) return GL_TRUE; /* get the device context */ dc = GetDC(wnd); if (NULL == dc) return GL_TRUE; /* find pixel format */ ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR)); if (params->pixelformat == -1) /* find default */ { pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; params->pixelformat = ChoosePixelFormat(dc, &pfd); if (params->pixelformat == 0) return GL_TRUE; } /* set the pixel format for the dc */ if (FALSE == SetPixelFormat(dc, params->pixelformat, &pfd)) return GL_TRUE; /* create rendering context */ rc = wglCreateContext(dc); if (NULL == rc) return GL_TRUE; if (FALSE == wglMakeCurrent(dc, rc)) return GL_TRUE; if (params->major || params->profile || params->flags) { HGLRC oldRC = rc; int contextAttrs[20]; int i; wglewInit(); /* Intel HD 3000 has WGL_ARB_create_context, but not WGL_ARB_create_context_profile */ if (!wglewGetExtension("WGL_ARB_create_context")) return GL_TRUE; i = 0; if (params->major) { contextAttrs[i++] = WGL_CONTEXT_MAJOR_VERSION_ARB; contextAttrs[i++] = params->major; contextAttrs[i++] = WGL_CONTEXT_MINOR_VERSION_ARB; contextAttrs[i++] = params->minor; } if (params->profile) { contextAttrs[i++] = WGL_CONTEXT_PROFILE_MASK_ARB; contextAttrs[i++] = params->profile; } if (params->flags) { contextAttrs[i++] = WGL_CONTEXT_FLAGS_ARB; contextAttrs[i++] = params->flags; } contextAttrs[i++] = 0; rc = wglCreateContextAttribsARB(dc, 0, contextAttrs); if (NULL == rc) return GL_TRUE; if (!wglMakeCurrent(dc, rc)) return GL_TRUE; wglDeleteContext(oldRC); } return GL_FALSE; }
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE ignoreMe0, LPSTR ignoreMe1, INT ignoreMe2) { LPCSTR szName = "Pez App"; WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(0), 0, 0, 0, 0, szName, 0 }; DWORD dwStyle = WS_SYSMENU | WS_VISIBLE | WS_POPUP; DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; RECT rect; int windowWidth, windowHeight, windowLeft, windowTop; HWND hWnd; PIXELFORMATDESCRIPTOR pfd; HDC hDC; HGLRC hRC; int pixelFormat; GLenum err; DWORD previousTime = GetTickCount(); MSG msg = {0}; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClassEx(&wc); SetRect(&rect, 0, 0, PEZ_VIEWPORT_WIDTH, PEZ_VIEWPORT_HEIGHT); AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle); windowWidth = rect.right - rect.left; windowHeight = rect.bottom - rect.top; windowLeft = GetSystemMetrics(SM_CXSCREEN) / 2 - windowWidth / 2; windowTop = GetSystemMetrics(SM_CYSCREEN) / 2 - windowHeight / 2; hWnd = CreateWindowExA(0, szName, szName, dwStyle, windowLeft, windowTop, windowWidth, windowHeight, 0, 0, 0, 0); // Create the GL context. 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 = 24; pfd.cStencilBits = 8; pfd.iLayerType = PFD_MAIN_PLANE; hDC = GetDC(hWnd); pixelFormat = ChoosePixelFormat(hDC, &pfd); SetPixelFormat(hDC, pixelFormat, &pfd); hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); if (PEZ_ENABLE_MULTISAMPLING) { int pixelAttribs[] = { WGL_SAMPLES_ARB, 16, WGL_SAMPLE_BUFFERS_ARB, GL_TRUE, WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_RED_BITS_ARB, 8, WGL_GREEN_BITS_ARB, 8, WGL_BLUE_BITS_ARB, 8, WGL_ALPHA_BITS_ARB, 8, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, 0 }; int* sampleCount = pixelAttribs + 1; int* useSampleBuffer = pixelAttribs + 3; int pixelFormat = -1; PROC proc = wglGetProcAddress("wglChoosePixelFormatARB"); unsigned int numFormats; PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC) proc; if (!wglChoosePixelFormatARB) { PezFatalError("Could not load function pointer for 'wglChoosePixelFormatARB'. Is your driver properly installed?"); } // Try fewer and fewer samples per pixel till we find one that is supported: while (pixelFormat <= 0 && *sampleCount >= 0) { wglChoosePixelFormatARB(hDC, pixelAttribs, 0, 1, &pixelFormat, &numFormats); (*sampleCount)--; if (*sampleCount <= 1) { *useSampleBuffer = GL_FALSE; } } // Win32 allows the pixel format to be set only once per app, so destroy and re-create the app: DestroyWindow(hWnd); hWnd = CreateWindowExA(0, szName, szName, dwStyle, windowLeft, windowTop, windowWidth, windowHeight, 0, 0, 0, 0); SetWindowPos(hWnd, HWND_TOP, windowLeft, windowTop, windowWidth, windowHeight, 0); hDC = GetDC(hWnd); SetPixelFormat(hDC, pixelFormat, &pfd); hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); } #define PEZ_TRANSPARENT_WINDOW 0 // For transparency, this doesn't seem to work. I think I'd need to read back from an OpenGL FBO and blit it with GDI. if (PEZ_TRANSPARENT_WINDOW) { long flag = GetWindowLong(hWnd, GWL_EXSTYLE); int opacity = 128; flag |= WS_EX_LAYERED; SetWindowLong(hWnd, GWL_EXSTYLE, flag); SetLayeredWindowAttributes(hWnd, 0, opacity, LWA_ALPHA); } err = glewInit(); if (GLEW_OK != err) { PezFatalError("GLEW Error: %s\n", glewGetErrorString(err)); } PezDebugString("OpenGL Version: %s\n", glGetString(GL_VERSION)); if (!PEZ_VERTICAL_SYNC) { wglSwapIntervalEXT(0); } if (PEZ_FORWARD_COMPATIBLE_GL && glewIsSupported("GL_VERSION_3_0")) { const int contextAttribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 }; HGLRC newRC = wglCreateContextAttribsARB(hDC, 0, contextAttribs); wglMakeCurrent(0, 0); wglDeleteContext(hRC); hRC = newRC; wglMakeCurrent(hDC, hRC); } { const char* szWindowTitle = PezInitialize(PEZ_VIEWPORT_WIDTH, PEZ_VIEWPORT_HEIGHT); SetWindowTextA(hWnd, szWindowTitle); } // ------------------- // Start the Game Loop // ------------------- while (msg.message != WM_QUIT) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { DWORD currentTime = GetTickCount(); DWORD deltaTime = currentTime - previousTime; previousTime = currentTime; PezUpdate(deltaTime); PezRender(); SwapBuffers(hDC); if (glGetError() != GL_NO_ERROR) { PezFatalError("OpenGL error.\n"); } } } UnregisterClass(szName, wc.hInstance); return 0; }
// TODO: Fix Fullscreen + More error handling. Platform* Platform::create(Game* game) { FileSystem::setResourcePath("./"); Platform* platform = new Platform(game); // Get the application module handle. __hinstance = ::GetModuleHandle(NULL); LPCTSTR windowClass = L"gameplay"; LPCTSTR windowName = L""; RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT }; // Register our window class. WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = (WNDPROC)__WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = __hinstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; // No brush - we are going to paint our own background wc.lpszMenuName = NULL; // No default menu wc.lpszClassName = windowClass; if (!::RegisterClassEx(&wc)) goto error; // Set the window style. DWORD style = ( WINDOW_FULLSCREEN ? WS_POPUP : WS_POPUP | WS_BORDER | WS_CAPTION | WS_SYSMENU) | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; DWORD styleEx = (WINDOW_FULLSCREEN ? WS_EX_APPWINDOW : WS_EX_APPWINDOW | WS_EX_WINDOWEDGE); // Adjust the window rectangle so the client size is the requested size. AdjustWindowRectEx(&rect, style, FALSE, styleEx); // Create the native Windows window. __hwnd = CreateWindowEx(styleEx, windowClass, windowName, style, 0, 0, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, __hinstance, NULL); // Get the drawing context. __hdc = GetDC(__hwnd); // Choose pixel format. 32-bit. RGBA. PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int pixelFormat = ChoosePixelFormat(__hdc, &pfd); if (pixelFormat == 0 || !SetPixelFormat (__hdc, pixelFormat, &pfd)) goto error; HGLRC tempContext = wglCreateContext(__hdc); wglMakeCurrent(__hdc, tempContext); if (GLEW_OK != glewInit()) goto error; int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 1, 0 }; if (!(__hrc = wglCreateContextAttribsARB(__hdc, 0, attribs) ) ) { wglDeleteContext(tempContext); goto error; } wglDeleteContext(tempContext); if (!wglMakeCurrent(__hdc, __hrc) || !__hrc) goto error; // Vertical sync. wglSwapIntervalEXT(__vsync ? 1 : 0); // Show the window ShowWindow(__hwnd, SW_SHOW); return platform; error: // TODO: cleanup exit(0); return NULL; }
bool initGLBase() { GLuint PixelFormat; PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 24; pfd.cStencilBits = 8; g_hDC = GetDC( g_hWnd ); PixelFormat = ChoosePixelFormat( g_hDC, &pfd ); SetPixelFormat( g_hDC, PixelFormat, &pfd); g_hRC = wglCreateContext( g_hDC ); wglMakeCurrent( g_hDC, g_hRC ); // calling glewinit NOW because the inside glew, there is mistake to fix... // This is the joy of using Core. The query glGetString(GL_EXTENSIONS) is deprecated from the Core profile. // You need to use glGetStringi(GL_EXTENSIONS, <index>) instead. Sounds like a "bug" in GLEW. glewInit(); //wglSwapInterval(0); #if 1 #define GLCOMPAT if(!wglCreateContextAttribsARB) wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); if(wglCreateContextAttribsARB) { HGLRC hRC = NULL; int attribList[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 3, #ifdef GLCOMPAT WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, #else WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, #endif WGL_CONTEXT_FLAGS_ARB, //WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB| #ifndef GLCOMPAT //WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB| // use it if you want errors when compat options still used #endif #ifdef _DEBUG WGL_CONTEXT_DEBUG_BIT_ARB #else 0 #endif ,0, 0 }; if (!(hRC = wglCreateContextAttribsARB(g_hDC, 0, attribList))) { LOGE("wglCreateContextAttribsARB() failed for OpenGL context.\n"); return false; //attribList[3] = 0; //if (!(hRC = wglCreateContextAttribsARB(g_hDC, 0, attribList))) // LOGE("wglCreateContextAttribsARB() failed for OpenGL context.\n"); } if (!wglMakeCurrent(g_hDC, hRC)) { LOGE("wglMakeCurrent() failed for OpenGL context.\n"); } else { wglDeleteContext( g_hRC ); g_hRC = hRC; #ifdef _DEBUG if(!glDebugMessageCallbackARB) { glDebugMessageCallbackARB = (PFNGLDEBUGMESSAGECALLBACKARBPROC)wglGetProcAddress("glDebugMessageCallbackARB"); glDebugMessageControlARB = (PFNGLDEBUGMESSAGECONTROLARBPROC)wglGetProcAddress("glDebugMessageControlARB"); } if(glDebugMessageCallbackARB) { glDebugMessageCallbackARB(myOpenGLCallback, NULL); glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH_ARB, 0, NULL, GL_TRUE); } #endif } } #endif glewInit(); LOGOK("Loaded Glew\n"); LOGOK("initialized OpenGL basis\n"); return true; }
static bool create_context_w32_gl3(struct MPGLContext *ctx) { struct w32_context *w32_ctx = ctx->priv; HDC windc = w32_ctx->hdc; HGLRC context = 0; // A legacy context is needed to get access to the new functions. HGLRC legacy_context = wglCreateContext(windc); if (!legacy_context) { MP_FATAL(ctx->vo, "Could not create GL context!\n"); return false; } // set context if (!wglMakeCurrent(windc, legacy_context)) { MP_FATAL(ctx->vo, "Could not set GL context!\n"); goto out; } const char *(GLAPIENTRY *wglGetExtensionsStringARB)(HDC hdc) = w32gpa((const GLubyte*)"wglGetExtensionsStringARB"); if (!wglGetExtensionsStringARB) goto unsupported; const char *wgl_exts = wglGetExtensionsStringARB(windc); if (!strstr(wgl_exts, "WGL_ARB_create_context")) goto unsupported; HGLRC (GLAPIENTRY *wglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext, const int *attribList) = w32gpa((const GLubyte*)"wglCreateContextAttribsARB"); if (!wglCreateContextAttribsARB) goto unsupported; int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 0, WGL_CONTEXT_FLAGS_ARB, 0, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0 }; context = wglCreateContextAttribsARB(windc, 0, attribs); if (!context) { // NVidia, instead of ignoring WGL_CONTEXT_FLAGS_ARB, will error out if // it's present on pre-3.2 contexts. // Remove it from attribs and retry the context creation. attribs[6] = attribs[7] = 0; context = wglCreateContextAttribsARB(windc, 0, attribs); } if (!context) { int err = GetLastError(); MP_FATAL(ctx->vo, "Could not create an OpenGL 3.x context: error 0x%x\n", err); goto out; } wglMakeCurrent(windc, NULL); wglDeleteContext(legacy_context); if (!wglMakeCurrent(windc, context)) { MP_FATAL(ctx->vo, "Could not set GL3 context!\n"); wglDeleteContext(context); return false; } w32_ctx->context = context; /* update function pointers */ mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log); return true; unsupported: MP_ERR(ctx->vo, "The OpenGL driver does not support OpenGL 3.x \n"); out: wglMakeCurrent(windc, NULL); wglDeleteContext(legacy_context); return false; }
// Initializes OpenGL rendering context. If succeeds, returns true. // hInstance - application instance // a_hWnd - window to init OpenGL into // a_initScene - pointer to init function // a_renderScene - pointer to render function // a_releaseScene - optional parameter of release function bool COpenGLControl::InitOpenGL(HINSTANCE hInstance, HWND* a_hWnd, void (*a_InitScene)(LPVOID), void (*a_RenderScene)(LPVOID), void(*a_ReleaseScene)(LPVOID), LPVOID lpParam) { if(!InitGLEW(hInstance))return false; hWnd = a_hWnd; hDC = GetDC(*hWnd); bool bError = false; PIXELFORMATDESCRIPTOR pfd; // if we have access to these functions if(WGLEW_ARB_create_context && WGLEW_ARB_pixel_format) { const int iPixelFormatAttribList[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, // Enable OpenGL support WGL_DOUBLE_BUFFER_ARB, GL_TRUE, // and double buffer WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, // Depth buffer size WGL_STENCIL_BITS_ARB, 8, 0 // End of attributes list }; int iContextAttribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, // OpenGL WGL_CONTEXT_MINOR_VERSION_ARB, 3, // version 3.3 WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 // End of attributes list }; int iPixelFormat, iNumFormats; wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats); // PFD seems to be only redundant parameter now if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; hRC = wglCreateContextAttribsARB(hDC, 0, iContextAttribs); // If everything went OK if(hRC) wglMakeCurrent(hDC, hRC); else bError = true; } else bError = true; if(bError) { // Generate error messages char sErrorMessage[255], sErrorTitle[255]; sprintf(sErrorMessage, "OpenGL 3.3 is not supported! Please download latest GPU drivers!"); sprintf(sErrorTitle, "OpenGL 3.3 Not Supported"); MessageBox(*hWnd, sErrorMessage, sErrorTitle, MB_ICONINFORMATION); return false; } RenderScene = a_RenderScene; InitScene = a_InitScene; ReleaseScene = a_ReleaseScene; if(InitScene != NULL)InitScene(lpParam); return true; }
static bool create_context_w32_gl3(struct MPGLContext *ctx) { struct w32_context *w32_ctx = ctx->priv; HGLRC *context = &w32_ctx->context; if (*context) // reuse existing context return true; // not reusing it breaks gl3! HWND win = ctx->vo->w32->window; HDC windc = GetDC(win); HGLRC new_context = 0; new_context = wglCreateContext(windc); if (!new_context) { MP_FATAL(ctx->vo, "Could not create GL context!\n"); return false; } // set context if (!wglMakeCurrent(windc, new_context)) { MP_FATAL(ctx->vo, "Could not set GL context!\n"); goto out; } const char *(GLAPIENTRY *wglGetExtensionsStringARB)(HDC hdc) = w32gpa((const GLubyte*)"wglGetExtensionsStringARB"); if (!wglGetExtensionsStringARB) goto unsupported; const char *wgl_exts = wglGetExtensionsStringARB(windc); if (!strstr(wgl_exts, "WGL_ARB_create_context")) goto unsupported; HGLRC (GLAPIENTRY *wglCreateContextAttribsARB)(HDC hDC, HGLRC hShareContext, const int *attribList) = w32gpa((const GLubyte*)"wglCreateContextAttribsARB"); if (!wglCreateContextAttribsARB) goto unsupported; int gl_version = ctx->requested_gl_version; int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, MPGL_VER_GET_MAJOR(gl_version), WGL_CONTEXT_MINOR_VERSION_ARB, MPGL_VER_GET_MINOR(gl_version), WGL_CONTEXT_FLAGS_ARB, 0, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0 }; *context = wglCreateContextAttribsARB(windc, 0, attribs); if (! *context) { // NVidia, instead of ignoring WGL_CONTEXT_FLAGS_ARB, will error out if // it's present on pre-3.2 contexts. // Remove it from attribs and retry the context creation. attribs[6] = attribs[7] = 0; *context = wglCreateContextAttribsARB(windc, 0, attribs); } if (! *context) { int err = GetLastError(); MP_FATAL(ctx->vo, "Could not create an OpenGL 3.x context: error 0x%x\n", err); goto out; } wglMakeCurrent(NULL, NULL); wglDeleteContext(new_context); if (!wglMakeCurrent(windc, *context)) { MP_FATAL(ctx->vo, "Could not set GL3 context!\n"); wglDeleteContext(*context); return false; } /* update function pointers */ mpgl_load_functions(ctx->gl, w32gpa, NULL, ctx->vo->log); int pfmt = GetPixelFormat(windc); PIXELFORMATDESCRIPTOR pfd; if (DescribePixelFormat(windc, pfmt, sizeof(PIXELFORMATDESCRIPTOR), &pfd)) { ctx->depth_r = pfd.cRedBits; ctx->depth_g = pfd.cGreenBits; ctx->depth_b = pfd.cBlueBits; } return true; unsupported: MP_ERR(ctx->vo, "The current OpenGL implementation does not support OpenGL 3.x \n"); out: wglDeleteContext(new_context); return false; }
void entrypoint( void ) { // full screen #ifdef SETRESOLUTION if( ChangeDisplaySettings(&screenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) return; #endif // create window HWND hWnd = CreateWindow( "static",0,WS_POPUP|WS_VISIBLE|WS_MAXIMIZE,0,0,0,0,0,0,0,0); HDC hDC = GetDC(hWnd); // initalize opengl if( !SetPixelFormat(hDC,ChoosePixelFormat(hDC,&pfd),&pfd) ) return; HGLRC tempOpenGLContext; tempOpenGLContext = wglCreateContext(hDC); wglMakeCurrent(hDC, tempOpenGLContext); // create openGL functions for (int i=0; i<NUM_GL_NAMES; i++) glFP[i] = (GenFP)wglGetProcAddress(glnames[i]); HGLRC hRC = wglCreateContextAttribsARB(hDC, NULL, glAttribs); // Remove temporary context and set new one wglMakeCurrent(NULL, NULL); wglDeleteContext(tempOpenGLContext); wglMakeCurrent(hDC, hRC); // init intro intro_init(); // open audio device waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL); // create music block mzk_init(); // prepare and play music block header[0].lpData = (char *)myMuzikBlock[0]; header[1].lpData = (char *)myMuzikBlock[1]; header[0].dwBufferLength = AUDIO_BUFFER_SIZE * MZK_NUMCHANNELS * 2; header[1].dwBufferLength = AUDIO_BUFFER_SIZE * MZK_NUMCHANNELS * 2; waveOutPrepareHeader(hWaveOut, &(header[0]), sizeof(WAVEHDR)); waveOutWrite(hWaveOut, &(header[0]), sizeof(WAVEHDR)); waveOutPrepareHeader(hWaveOut, &(header[1]), sizeof(WAVEHDR)); waveOutWrite(hWaveOut, &(header[1]), sizeof(WAVEHDR)); timer.wType = TIME_SAMPLES; do { MSG msg; PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); ShowCursor(0); waveOutGetPosition(hWaveOut, &timer, sizeof(timer)); DWORD t = timer.u.sample; intro_do(t); //SwapBuffers ( hDC ); wglSwapLayerBuffers( hDC, WGL_SWAP_MAIN_PLANE ); // Try to unprepare header if (waveOutUnprepareHeader(hWaveOut, &(header[nextPlayBlock]), sizeof(WAVEHDR)) != WAVERR_STILLPLAYING) { mzk_prepare_block(myMuzikBlock[nextPlayBlock]); waveOutPrepareHeader(hWaveOut, &(header[nextPlayBlock]), sizeof(WAVEHDR)); waveOutWrite(hWaveOut, &(header[nextPlayBlock]), sizeof(WAVEHDR)); nextPlayBlock = 1 - nextPlayBlock; } } while ( !(GetAsyncKeyState(VK_ESCAPE) || GetAsyncKeyState(VK_F4))); sndPlaySound(0,0); ExitProcess(0); }
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND shwnd; static HDC hdc; static HGLRC hglrc, hglrc_legacy; static Engine altEngine; static POINT center; static WSADATA wsadata; switch (message) { case WM_CREATE: WSAStartup(MAKEWORD(2, 2), &wsadata); AllocConsole(); RedirectIOToConsole(); SetTimer ( hwnd, TICK_TIMER, 16, NULL ); hdc = GetDC(hwnd); setupPixelFormat(hdc); #ifndef DIRECTX hglrc_legacy = wglCreateContext(hdc); wglMakeCurrent(hdc, hglrc_legacy); glewInit(); if(wglewIsSupported("WGL_ARB_create_context") == TRUE) { const int context[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 1, // WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 }; const int pixelformat[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, 0, }; int format; unsigned int num_formats; wglChoosePixelFormatARB(hdc, (int *)pixelformat, NULL, 1, &format, &num_formats); hglrc = wglCreateContextAttribsARB(hdc, 0, context); wglMakeCurrent(NULL,NULL); wglDeleteContext(hglrc_legacy); wglMakeCurrent(hdc, hglrc); } else { //opengl 2.0 hglrc = hglrc_legacy; } #endif shwnd = hwnd; altEngine.init(&shwnd, &hdc); return 0; case WMU_RENDER: altEngine.render(); return 0; case WM_TIMER: if (glGetError() != GL_NO_ERROR) { printf("GL_ERROR\n"); } switch(wParam) { case TICK_TIMER: altEngine.step(); break; } return 0; case WM_MOUSEMOVE: { int x, y; x = LOWORD(lParam); y = HIWORD(lParam); if ((x == center.x) && (y == center.y)) return 0; if ( altEngine.mousepos(x, y, x - center.x, y - center.y) ) { POINT screen = center; ClientToScreen(hwnd, &screen); SetCursorPos(screen.x, screen.y); } } return 0; case WM_LBUTTONDOWN: case WM_LBUTTONUP: { bool pressed = (message == WM_LBUTTONDOWN) ? true : false; altEngine.keypress("leftbutton", pressed); return 0; } case WM_MBUTTONDOWN: case WM_MBUTTONUP: { bool pressed = (message == WM_MBUTTONDOWN) ? true : false; altEngine.keypress("middlebutton", pressed); return 0; } case WM_RBUTTONDOWN: case WM_RBUTTONUP: { bool pressed = (message == WM_RBUTTONDOWN) ? true : false; altEngine.keypress("rightbutton", pressed); return 0; } case WM_KEYDOWN: case WM_KEYUP: { bool pressed = (message == WM_KEYDOWN) ? true : false; switch (wParam) { case VK_PAUSE: break; case VK_TAB: break; case VK_RETURN: altEngine.keypress("enter", pressed); break; case VK_SHIFT: altEngine.keypress("shift", pressed); break; case VK_CONTROL: altEngine.keypress("control", pressed); break; case VK_ESCAPE: altEngine.keypress("escape", pressed); break; case VK_UP: altEngine.keypress("up", pressed); break; case VK_LEFT: altEngine.keypress("left", pressed); break; case VK_DOWN: altEngine.keypress("down", pressed); break; case VK_RIGHT: altEngine.keypress("right", pressed); break; } return 0; } case WM_CHAR: altEngine.keystroke((char)wParam); return 0; case WM_SIZE: { int width, height; width = LOWORD(lParam); height = HIWORD(lParam); center.x = width / 2; center.y = height / 2; altEngine.resize(width, height); } return 0; case WM_SYSCOMMAND: switch(wParam) { case SC_SCREENSAVE: case SC_MONITORPOWER: return 0; } break; case WM_DESTROY: #ifndef DIRECTX if (hglrc) { wglMakeCurrent(NULL, NULL); wglDeleteContext(hglrc); ReleaseDC(hwnd, hdc); } #endif altEngine.destroy(); WSACleanup(); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }
void screen_init(struct rdp_config* config) { // make window resizable for the user if (!fullscreen) { LONG style = GetWindowLong(gfx.hWnd, GWL_STYLE); style |= WS_SIZEBOX | WS_MAXIMIZEBOX; SetWindowLong(gfx.hWnd, GWL_STYLE, style); BOOL zoomed = IsZoomed(gfx.hWnd); if (zoomed) { ShowWindow(gfx.hWnd, SW_RESTORE); } // Fix client size after changing the window style, otherwise the PJ64 // menu will be displayed incorrectly. // For some reason, this needs to be called twice, probably because the // style set above isn't applied immediately. for (int i = 0; i < 2; i++) { win32_client_resize(gfx.hWnd, gfx.hStatusBar, WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT); } if (zoomed) { ShowWindow(gfx.hWnd, SW_MAXIMIZE); } } PIXELFORMATDESCRIPTOR win_pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, // Flags PFD_TYPE_RGBA, // The kind of framebuffer. RGBA or palette. 32, // Colordepth of the framebuffer. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, // Number of bits for the depthbuffer 8, // Number of bits for the stencilbuffer 0, // Number of Aux buffers in the framebuffer. PFD_MAIN_PLANE, 0, 0, 0, 0 }; dc = GetDC(gfx.hWnd); if (!dc) { msg_error("Can't get device context."); } int32_t win_pf = ChoosePixelFormat(dc, &win_pfd); if (!win_pf) { msg_error("Can't choose pixel format."); } SetPixelFormat(dc, win_pf, &win_pfd); // create legacy context, required for wglGetProcAddress to work properly glrc = wglCreateContext(dc); if (!glrc || !wglMakeCurrent(dc, glrc)) { msg_error("Can't create OpenGL context."); } // load wgl extension wgl_LoadFunctions(dc); // attributes for a 3.3 core profile without all the legacy stuff GLint attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 3, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0 }; // create the actual context glrc_core = wglCreateContextAttribsARB(dc, glrc, attribs); if (!glrc_core || !wglMakeCurrent(dc, glrc_core)) { // rendering probably still works with the legacy context, so just send // a warning msg_warning("Can't create OpenGL 3.3 core context."); } // enable vsync wglSwapIntervalEXT(1); gl_screen_init(config); }