GLWindow

bool GLWindow::createWindow()

createWindow, guess what, creates a window! It’s also where the openGL context is created if you were wondering. It’s also a pain to understand as well, but that’s why they created MSDN and Google. I’m not here to teach, but to inspire you to learn. If you were looking where to set up all your extra buffers (stencil buffer and so on), it’s right there in the pixel format descripter. One last thing. When CreateWindowEx is called, you’ll notice that a pointer to this object is sent in as the last argument. We’ll see why we need to do that later, with the static window proc.

bool
GLWindow::createWindow()
{
DWORD windowStyle = WS_OVERLAPPEDWINDOW;
DWORD windowExtendedStyle = WS_EX_APPWINDOW;

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_SWAP_LAYER_BUFFERS |
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bitsPerPixel, // 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, // 24Bit Z-Buffer (Depth Buffer)
8, // 8 bit Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

RECT windowRect = {0, 0, starting_width, starting_height};

GLuint PixelFormat;

if(isFullScreen == true)
{
if(changeScreenResolution(starting_width, starting_height, bitsPerPixel) == FALSE)
{
// Fullscreen Mode Failed. Run In Windowed Mode Instead
MessageBox(HWND_DESKTOP, "Mode Switch Failed.\nRunning In Windowed Mode.", "Error", MB_OK | MB_ICONEXCLAMATION);
isFullScreen = false;
}
else
{
ShowCursor(FALSE);
windowStyle = WS_POPUP;
windowExtendedStyle |= WS_EX_TOPMOST;
}
}
else
{
// Adjust Window, Account For Window Borders
AdjustWindowRectEx(&windowRect, windowStyle, 0, windowExtendedStyle);
}

// Create The OpenGL Window
hWnd = CreateWindowEx(
windowExtendedStyle, // Extended Style
className, // Class Name
title, // Window Title
windowStyle, // Window Style
0, 0, // Window X,Y Position
windowRect.right - windowRect.left, // Window Width
windowRect.bottom - windowRect.top, // Window Height
HWND_DESKTOP, // Desktop Is Window's Parent
0, // No Menu
hInstance, // Pass The Window Instance
(LPVOID)this);

if(hWnd == 0)
{
return false;
}

hDC = GetDC(hWnd);
if(hDC == 0)
{
// Failed
DestroyWindow(hWnd);
hWnd = 0;
return false;
}

PixelFormat = ChoosePixelFormat(hDC, &pfd);
if(PixelFormat == 0)
{
// Failed
ReleaseDC(hWnd, hDC);
hDC = 0;
DestroyWindow(hWnd);
hWnd = 0;
return false;
}

if(SetPixelFormat(hDC, PixelFormat, &pfd) == FALSE)
{
// Failed
ReleaseDC(hWnd, hDC);
hDC = 0;
DestroyWindow(hWnd);
hWnd = 0;
return false;
}

hRC = wglCreateContext(hDC);
if(hRC == 0)
{
// Failed
ReleaseDC(hWnd, hDC);
hDC = 0;
DestroyWindow(hWnd);
hWnd = 0;
return false;
}

// Make The Rendering Context Our Current Rendering Context
if(wglMakeCurrent(hDC, hRC) == FALSE)
{
// Failed
wglDeleteContext(hRC);
hRC = 0;
ReleaseDC(hWnd, hDC);
hDC = 0;
DestroyWindow(hWnd);
hWnd = 0;
return false;
}

ShowWindow(hWnd, SW_NORMAL);
isVisible = true;

reshape(current_width, current_height);
ZeroMemory(keys, sizeof(bool)*256);
lastTickCount = GetTickCount();
return true;
}

bool GLWindow::destroyWindow()

Yep, destroys the window.

bool
GLWindow::destroyWindow()
{
if(hWnd != 0)
{
if(hDC != 0)
{
wglMakeCurrent(hDC, 0);
if(hRC != 0)
{
wglDeleteContext(hRC);
hRC = 0;
}
ReleaseDC(hWnd, hDC);
hDC = 0;
}
DestroyWindow(hWnd);
hWnd = 0;
}

if(isFullScreen)
{
ChangeDisplaySettings(NULL,0);
ShowCursor(TRUE);
}
return true;
}

Pages: 1 2 3 4 5 6 7 8

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>