void run() { registerWindowClass(); createWindow(); createOpenGLContext(); messageLoop(); }
void AppManager::init(){ /* Initialize the library */ if (glfwInit() != GL_TRUE) { THROW_EXCEPTION("Failed to initialize GLFW"); } glfwSetErrorCallback(error_callback); createOpenGLContext(); setOpenGLStates(); createFBO(); createProgram(); createVAO(); applyInitial(); }
//---------------------------------------------------------------------------------------------------------------------- int main() { // initialize SDL's video and audio subsystem if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)<0) { // or die on error SDLErrorExit("Unable to initialize SDL"); } /// The following section is modified from :- /// Lazy Foo' Productions (2014). SDL Sound Effects and Music [online]. [Accessed 2014]. /// Available from: <http://lazyfoo.net/tutorials/SDL/21_sound_effects_and_music/index.php>. // initialize SDL_mixer if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048 )<0) { printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError()); // or die on error SDLErrorExit("Unable to initialize SDL_mixer"); } /// end of Citation // now get the size of the display and create a window we need to init the video SDL_Rect rect; SDL_GetDisplayBounds(0,&rect); // now create our window to be most of the screen's size SDL_Window *window=SDL_CreateWindow("SDLNGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, rect.w/1.2, rect.h/1.2, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE ); // check to see if that worked or exit if (!window) { SDLErrorExit("Unable to create window"); } // create our opengl context and attach it to our window SDL_GLContext glContext=createOpenGLContext(window); if(!glContext) { SDLErrorExit("Problem creating OpenGL context"); } // make this our current GL context (we can have more than one window but in this case not) SDL_GL_MakeCurrent(window, glContext); // this makes our buffer swap syncronized with the monitor's vertical refresh SDL_GL_SetSwapInterval(1); // we need to initialise the NGL lib which will load all of the OpenGL functions, this must // be done once we have a valid GL context but before we call any GL commands, else // everything will crash ngl::NGLInit::instance(); // now clear the screen and swap whilst NGL inits (which may take time) glClear(GL_COLOR_BUFFER_BIT); SDL_GL_SwapWindow(window); // flag to indicate if we need to exit bool quit=false; // sdl event processing data structure SDL_Event event; // bind the mouse' movement to the game, force the game to interpret the mouse' movements // on mac the mouse is fully locked whilst on linux the mouse is free but movements // are still tied to the window SDL_SetRelativeMouseMode(SDL_TRUE); // now we create an instance of a game class, this will init NGL and setup basic // opengl stuff ext, and when this falls out of scope the dtor will be called and cleanup // our gl stuff Game _game; // resize the ngl to set the screen size and camera parameters _game.resize(rect.w,rect.h); // central opengl loop while(!quit) { while(SDL_PollEvent(&event)) { switch(event.type) { // this is the window x being clicked. case SDL_QUIT : quit = true; break; // process the mouse data by passing it to ngl class case SDL_MOUSEMOTION : _game.mouseMoveEvent(event.motion); break; // if the window is re-sized pass it to the ngl class to change gl viewport // note this is slow as the context is re-create by SDL each time // this shouldn't happen as the mouse is bound to the screen an the screen // shouldn't be resized case SDL_WINDOWEVENT : int w,h; // get the new window size SDL_GetWindowSize(window,&w,&h); _game.resize(w,h); break; // now we look for a keydown event case SDL_KEYDOWN: { switch( event.key.keysym.sym ) { // if it's the escape key quit case SDLK_ESCAPE : quit = true; break; // key for time-slowing case SDLK_s : _game.keyPressEvent(event.key.keysym); break; // key for attacking case SDLK_a : _game.keyPressEvent(event.key.keysym); break; // key for breaking tethers early case SDLK_f : _game.keyPressEvent(event.key.keysym); break; // key for creating a tether case SDLK_SPACE : _game.keyPressEvent(event.key.keysym); break; // key for beginning the game case SDLK_e : _game.keyPressEvent(event.key.keysym); break; default : break; } // end of key process } // end of keydown default : break; } // end of event switch } // end of poll events // all the updates _game.update(); // now we draw the game _game.draw(); // swap the buffers SDL_GL_SwapWindow(window); } // now tidy up and exit SDL and the SDL_mixer Mix_Quit(); SDL_Quit(); }
/** * @brief * Constructor */ ContextWindows::ContextWindows(handle nativeWindowHandle) : mOpenGLRuntimeLinking(new OpenGLRuntimeLinking()), mNativeWindowHandle(nativeWindowHandle), mDummyWindow(NULL_HANDLE), mWindowDeviceContext(NULL_HANDLE), mWindowRenderContext(NULL_HANDLE) { // Is OpenGL available? if (mOpenGLRuntimeLinking->isOpenGLAvaiable()) { // Create a OpenGL dummy window? // -> Under MS Windows, a OpenGL context is always coupled to a window... even if we're not going to render into a window at all... if (NULL_HANDLE == mNativeWindowHandle) { // Setup and register the window class for the OpenGL dummy window WNDCLASS windowDummyClass; windowDummyClass.hInstance = ::GetModuleHandle(nullptr); windowDummyClass.lpszClassName = TEXT("OpenGLDummyWindow"); windowDummyClass.lpfnWndProc = DefWindowProc; windowDummyClass.style = 0; windowDummyClass.hIcon = nullptr; windowDummyClass.hCursor = nullptr; windowDummyClass.lpszMenuName = nullptr; windowDummyClass.cbClsExtra = 0; windowDummyClass.cbWndExtra = 0; windowDummyClass.hbrBackground = nullptr; ::RegisterClass(&windowDummyClass); // Create the OpenGL dummy window mNativeWindowHandle = mDummyWindow = reinterpret_cast<handle>(::CreateWindow(TEXT("OpenGLDummyWindow"), TEXT("PFormat"), WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 8, 8, HWND_DESKTOP, nullptr, ::GetModuleHandle(nullptr), nullptr)); } // Is there a valid window handle? if (NULL_HANDLE != mNativeWindowHandle) { // Get the device context of the OpenGL window mWindowDeviceContext = ::GetDC(reinterpret_cast<HWND>(mNativeWindowHandle)); if (NULL_HANDLE != mWindowDeviceContext) { // Get the color depth of the deskop int bits = 32; { HDC deskTopDC = ::GetDC(nullptr); bits = ::GetDeviceCaps(deskTopDC, BITSPIXEL); ::ReleaseDC(nullptr, deskTopDC); } // Get the first best pixel format const PIXELFORMATDESCRIPTOR pixelFormatDescriptor = { sizeof(PIXELFORMATDESCRIPTOR), // Size of this pixel format descriptor 1, // Version number PFD_DRAW_TO_WINDOW | // Format must support window PFD_SUPPORT_OPENGL | // Format must support OpenGL PFD_DOUBLEBUFFER, // Must support double buffering PFD_TYPE_RGBA, // Request an RGBA format static_cast<UCHAR>(bits), // Select our color depth 0, 0, 0, 0, 0, 0, // Color bits ignored 0, // No alpha buffer 0, // Shift bit ignored 0, // No accumulation buffer 0, 0, 0, 0, // Accumulation bits ignored static_cast<BYTE>(24), // Z-buffer (depth buffer) 0, // No stencil buffer 0, // No auxiliary buffer PFD_MAIN_PLANE, // Main drawing layer 0, // Reserved 0, 0, 0 // Layer masks ignored }; const int pixelFormat = ::ChoosePixelFormat(mWindowDeviceContext, &pixelFormatDescriptor); if (0 != pixelFormat) { // Set the pixel format ::SetPixelFormat(mWindowDeviceContext, pixelFormat, &pixelFormatDescriptor); // Create a legacy OpenGL render context HGLRC legacyRenderContext = wglCreateContext(mWindowDeviceContext); if (NULL_HANDLE != legacyRenderContext) { // Make the legacy OpenGL render context to the current one wglMakeCurrent(mWindowDeviceContext, legacyRenderContext); // Create the render context of the OpenGL window mWindowRenderContext = createOpenGLContext(); // Destroy the legacy OpenGL render context wglMakeCurrent(nullptr, nullptr); wglDeleteContext(legacyRenderContext); // If there's an OpenGL context, do some final initialization steps if (NULL_HANDLE != mWindowRenderContext) { // Make the OpenGL context to the current one wglMakeCurrent(mWindowDeviceContext, mWindowRenderContext); } } else { // Error, failed to create a legacy OpenGL render context! } } else { // Error, failed to choose a pixel format! } } else { // Error, failed to obtain the device context of the OpenGL window! } } else { // Error, failed to create the OpenGL window! } // Is there a valid render context? if (nullptr != mWindowRenderContext) { // Initialize the OpenGL extensions getExtensions().initialize(); #ifdef RENDERER_OUTPUT_DEBUG // "GL_ARB_debug_output"-extension available? if (getExtensions().isGL_ARB_debug_output()) { // Synchronous debug output, please // -> Makes it easier to find the place causing the issue glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); // We don't need to configure the debug output by using "glDebugMessageControlARB()", // by default all messages are enabled and this is good this way // Set the debug message callback function glDebugMessageCallbackARB(&ContextWindows::debugMessageCallback, nullptr); } #endif } } }
int main() { // Initialize SDL's Video subsystem if (SDL_Init(SDL_INIT_VIDEO) < 0 ) { // Or die on error SDLErrorExit("Unable to initialize SDL"); } // now get the size of the display and create a window we need to init the video SDL_Rect rect; SDL_GetDisplayBounds(0,&rect); // now create our window SDL_Window *window=SDL_CreateWindow("SDLNGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, rect.w/2, rect.h/2, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE ); // check to see if that worked or exit if (!window) { SDLErrorExit("Unable to create window"); } // Create our opengl context and attach it to our window SDL_GLContext glContext=createOpenGLContext(window); if(!glContext) { SDLErrorExit("Problem creating OpenGL context"); } // make this our current GL context (we can have more than one window but in this case not) SDL_GL_MakeCurrent(window, glContext); /* This makes our buffer swap syncronized with the monitor's vertical refresh */ SDL_GL_SetSwapInterval(1); // we need to initialise the NGL lib which will load all of the OpenGL functions, this must // be done once we have a valid GL context but before we call any GL commands. If we dont do // this everything will crash ngl::NGLInit::instance(); // now clear the screen and swap whilst NGL inits (which may take time) glClear(GL_COLOR_BUFFER_BIT); SDL_GL_SwapWindow(window); // flag to indicate if we need to exit bool quit=false; // sdl event processing data structure SDL_Event event; // now we create an instance of our ngl class, this will init NGL and setup basic // opengl stuff ext. When this falls out of scope the dtor will be called and cleanup // our gl stuff NGLDraw ngl; // resize the ngl to set the screen size and camera stuff ngl.resize(rect.w,rect.h); while(!quit) { while ( SDL_PollEvent(&event) ) { switch (event.type) { // this is the window x being clicked. case SDL_QUIT : quit = true; break; // process the mouse data by passing it to ngl class case SDL_MOUSEMOTION : ngl.mouseMoveEvent(event.motion); break; case SDL_MOUSEBUTTONDOWN : ngl.mousePressEvent(event.button); break; case SDL_MOUSEBUTTONUP : ngl.mouseReleaseEvent(event.button); break; case SDL_MOUSEWHEEL : ngl.wheelEvent(event.wheel); // if the window is re-sized pass it to the ngl class to change gl viewport // note this is slow as the context is re-create by SDL each time case SDL_WINDOWEVENT : int w,h; // get the new window size SDL_GetWindowSize(window,&w,&h); ngl.resize(w,h); break; // now we look for a keydown event case SDL_KEYDOWN: { ngl.keyEvent(event.key); switch( event.key.keysym.sym ) { // if it's the escape key quit case SDLK_ESCAPE : quit = true; break; case SDLK_w : glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); break; case SDLK_s : glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); break; case SDLK_f : SDL_SetWindowFullscreen(window,SDL_TRUE); glViewport(0,0,rect.w,rect.h); break; case SDLK_g : SDL_SetWindowFullscreen(window,SDL_FALSE); break; default : break; } // end of key process } // end of keydown default : break; } // end of event switch } // end of poll events // now we draw ngl ngl.update(); ngl.draw(); // swap the buffers SDL_GL_SwapWindow(window); } // now tidy up and exit SDL SDL_Quit(); }
OpenGLWindow::OpenGLWindow(const char* aTitle, int aWidth, int aHeight, bool aIsFullScreen) : m_WindowHandle(NULL), m_WindowDeviceContext(NULL), m_OpenGLContext(NULL), m_IsFullScreen(aIsFullScreen) { //Pass in the window title, and register the window class. registerWindowClass(aTitle); // RECT WindowRect; WindowRect.top = WindowRect.left = 0; WindowRect.right = aWidth; WindowRect.bottom = aHeight; //Window Extended Style DWORD extendedWindowStyle = 0; //Windows Style DWORD windowStyle = 0; //Do we hide the cursor? ShowCursor(WINDOW_SHOW_MOUSE_CURSOR); if(m_IsFullScreen == true) { DEVMODE dmScreenSettings; memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); dmScreenSettings.dmSize = sizeof(dmScreenSettings); dmScreenSettings.dmPelsWidth = aWidth; dmScreenSettings.dmPelsHeight = aHeight; dmScreenSettings.dmBitsPerPel = 32; dmScreenSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL; //Change the display settings to fullscreen. On error, throw an exception. if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { throw Win32Exception("Unable to swith to fullscreen mode"); } extendedWindowStyle = WS_EX_APPWINDOW; windowStyle = WS_POPUP; } else { extendedWindowStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; windowStyle = WS_CAPTION | WS_SYSMENU | WS_VISIBLE | WS_MINIMIZEBOX; } //Adjust the window to the true requested size AdjustWindowRectEx(&WindowRect, windowStyle, false, extendedWindowStyle); //Now create the OpenGL window m_WindowHandle = CreateWindowEx(extendedWindowStyle, aTitle, aTitle, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | windowStyle, WINDOW_DEFAULT_X, WINDOW_DEFAULT_Y, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, NULL, NULL, GetModuleHandle(NULL), this); // if(m_WindowHandle == NULL) { throw Win32Exception("Cannot create the main window"); } // createOpenGLContext(); //Show the newly created window ShowWindow(m_WindowHandle, SW_SHOW); // Call OnSize manually because in fullscreen mode it will be // called only when the window is created (which is too early // because OpenGL is not initialized yet). resize(aWidth, aHeight); // m_LastMouseX = -1; m_LastMouseY = -1; }