Skip to content

Instantly share code, notes, and snippets.

@ThePhD
Created April 3, 2015 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThePhD/ac29907b35aa68347fa9 to your computer and use it in GitHub Desktop.
Save ThePhD/ac29907b35aa68347fa9 to your computer and use it in GitHub Desktop.
Initialization.... need to get back buffer texture and stuff as well!
bool GraphicsDevice::Create( IWindow& window ) {
targetwindow = &window;
// HDC requires hwnd in order to be released, so we need to give the device context deleter
// a reference
// TODO: a shared deleter?
devicecontextresource.get_deleter( ).windowresource = targetwindow;
HWND hwnd = static_cast<HWND>( targetwindow->Handle( ) );
devicecontextresource = static_cast<void*>( GetDC( hwnd ) );
HDC hdc = static_cast<HDC>( devicecontextresource.get().dx );
if ( hdc == null ) {
return false;
}
static PIXELFORMATDESCRIPTOR pfd = {
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
32, // 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
24, // 16Bit Z-Buffer (Depth Buffer)
8, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
GLuint pixelformat = ChoosePixelFormat( hdc, &pfd );
cbool pixelformatresult = SetPixelFormat( hdc, pixelformat, &pfd );
if ( pixelformatresult == 0 ) {
return false;
}
// Windows oddity:
// because of the way opengl32.lib is, we need
// to first query a device context (which by default hands us an OpenGL ~2.1 context)
unique<HGLRC, device_deleter> hglrc = wglCreateContext( hdc );
cbool wglresult = wglMakeCurrent( hdc, hglrc );
if (wglresult == 0) {
return false;
}
struct gl_version {
int major;
int minor;
};
static const std::array<gl_version, 17> featurelevels = { {
{ 4, 5 },
{ 4, 4 },
{ 4, 3 },
{ 4, 2 },
{ 4, 1 },
{ 4, 0 },
{ 3, 3 },
{ 3, 2 },
{ 3, 1 },
{ 3, 0 },
{ 2, 1 },
{ 2, 0 },
{ 1, 5 },
{ 1, 4 },
{ 1, 3 },
{ 1, 2 },
{ 1, 1 }
} };
// TODO: make an OpenGL context of higher levels, starting from the greater version,
// and snowball downwards until we reach a version we can use
for ( std::size_t i = 0; i < featurelevels.size(); ++i ) {
const auto& potential = featurelevels[i];
int attribs[ ] = {
wgl::CONTEXT_MAJOR_VERSION_ARB, potential.major,
wgl::CONTEXT_MINOR_VERSION_ARB, potential.minor,
wgl::CONTEXT_FLAGS_ARB, wgl::CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0
};
unique<HGLRC, device_deleter> levelhglrc = wgl::CreateContextAttribsARB( hdc, hglrc, attribs );
if (levelhglrc == null) {
continue;
}
featurelevel = static_cast<FeatureLevel>( featurelevels.size( ) - ( i + 1 ) + enums::to_underlying( FeatureLevel::LevelGl011 ) );
// Pick this one, set Feature Level
// TODO: properly communicate feature level
hglrc = levelhglrc.release();
break;
}
deviceresource = static_cast<void*>( hglrc );
wglresult = wglMakeCurrent( hdc, hglrc );
if ( wglresult == 0 ) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment