Advertisement
ZeronSix

Context.cpp

Feb 24th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include "Allotropy\Allotropy.h"
  2. using namespace al;
  3.  
  4. // specify OpenGL version here
  5. static const unsigned contextVersionMajor = 3;
  6. static const unsigned contextVersionMinor = 2;
  7.  
  8. bool Context::isGlewInit = false;
  9.  
  10. static void SetupContextParameters() {
  11.     glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, contextVersionMajor );
  12.     glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, contextVersionMinor );
  13.     glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
  14.     glfwWindowHint( GLFW_OPENGL_PROFILE,        GLFW_OPENGL_CORE_PROFILE );
  15.     glfwWindowHint( GLFW_RESIZABLE,             GL_FALSE );
  16. }
  17.  
  18. Context::Context() : videoMode( VideoMode::GetDesktopVideoMode() ), windowTitle( "Allotropy Application" ) { }
  19.  
  20. Context::Context( const VideoMode mVideoMode, const char* mWindowTitle ) : videoMode( mVideoMode ) {
  21.     windowTitle = new char[strlen( mWindowTitle + 1 )];
  22.     strcpy( windowTitle, mWindowTitle );
  23. }
  24.  
  25. Context::~Context() {
  26.     delete[] windowTitle;
  27. }
  28.  
  29. bool Context::Restart() {
  30.     glfwMakeContextCurrent( 0 ); // disable any current contexts
  31.     SetupContextParameters();
  32.     if ( videoMode.fullscreen ) {
  33.         _window = glfwCreateWindow( videoMode.width, videoMode.height, windowTitle, glfwGetPrimaryMonitor(), nullptr );
  34.     }
  35.     else {
  36.         _window = glfwCreateWindow( videoMode.width, videoMode.height, windowTitle, nullptr, nullptr );
  37.     }
  38.     glfwMakeContextCurrent( _window );
  39.  
  40.     if ( _window == nullptr ) {
  41.         fprintf( stderr, "Failed to create a window!" ); // TODO: log error messages
  42.         return false;
  43.     }
  44.  
  45.     if ( !Context::isGlewInit ) {
  46.         if ( glewInit() != GLEW_OK ) {
  47.             fprintf( stderr, "Failed to initialize GLEW!" ); // TODO: log error messages
  48.             return false;
  49.         }
  50.         else {
  51.             Context::isGlewInit = true;
  52.         }
  53.     }
  54.  
  55.     return true;
  56. }
  57.  
  58. void Context::StartFrame() {
  59.  
  60. }
  61.  
  62. void Context::EndFrame() {
  63.     glfwSwapBuffers( _window );
  64.     glfwPollEvents();
  65. }
  66.  
  67. bool Context::ShouldClose() {
  68.     return glfwWindowShouldClose( _window );
  69. }
  70.  
  71. Context* Context::Create( const VideoMode mVideoMode, const char* mWindowTitle ) {
  72.     Context* context = new Context( mVideoMode, mWindowTitle );
  73.     if ( context->Restart() ) {
  74.         return context;
  75.     }
  76.     else {
  77.         delete context;
  78.         return nullptr;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement