1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include "OGLAbstr.h"
LRESULT CALLBACK WndProcFake(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
}
return (DefWindowProc(hWnd, msg, wParam, lParam));
}
OGLAbstr::OGLAbstr(HINSTANCE hInst, LogMgr* mLog)
{
hInstance = hInst;
log = mLog;
fakeClass.cbSize = sizeof(WNDCLASSEX);
fakeClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
fakeClass.lpfnWndProc = WndProcFake;
fakeClass.cbClsExtra = 0; fakeClass.cbWndExtra = 0;
fakeClass.hInstance = hInstance;
fakeClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
fakeClass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
fakeClass.hCursor = LoadCursor(NULL, IDC_ARROW);
fakeClass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
fakeClass.lpszMenuName = NULL;
fakeClass.lpszClassName = "temp";
RegisterClassEx(&fakeClass);
log->logMessage(log->WND, "Registered dummy class");
}
OGLAbstr::~OGLAbstr()
{
}
bool OGLAbstr::initGLEW()
{
HWND hWndFake = CreateWindow("temp", "FAKE", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_CLIPCHILDREN, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
hDC = GetDC(hWndFake);
// First, choose false pixel format
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 iPixelFormat = ChoosePixelFormat(hDC, &pfd);
if (iPixelFormat == 0)return false;
if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false;
// Create the false, old style context (OpenGL 2.1 and before)
HGLRC hRCFake = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRCFake);
if(glewInit() != GLEW_OK)
{
MessageBox(NULL, "Couldn't initialize GLEW!", "Fatal Error", MB_ICONERROR | MB_OK);
log->logMessage(log->GLERR, "Could not initialize GLEW!");
return false;
}
log->logMessage(log->GL, "Initialized GLEW");
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRCFake);
DestroyWindow(hWndFake);
UnregisterClass("temp", hInstance);
return true;
}
bool OGLAbstr::initGL(int AA)
{
#ifdef _EDITOR
hDC = GetDC(RenderWindow);
#endif
#ifdef _GAME
hDC = GetDC(Window);
#endif
if(hDC == NULL)
{
log->logMessage(log->GLERR, "Could not get DC");
return false;
}
PIXELFORMATDESCRIPTOR pfd;
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,
WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
0 // End of attributes list
};
int iContextAttribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0 // End of attributes list
};
int iPixelFormat, iNumFormats;
if(!wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats))
{
log->logMessage(log->GLERR, "Could not find Pixel Format!");
return false;
}
// 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);
log->logMessage(log->GL, "Created DC");
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
return true;
}
bool OGLAbstr::openGameWindow(WNDCLASSEX wc, std::string title, int x, int y)
{
Window = CreateWindowEx(0, wc.lpszClassName, title.c_str(), WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_VISIBLE | WS_CLIPCHILDREN, x, y, 800, 600, 0, 0, hInstance, 0);
if(Window == NULL)
return false;
ShowWindow(Window, SW_SHOW);
ShowWindow(Window, SW_MAXIMIZE);
log->logMessage(log->WND, "Created Game window");
return true;
}
void OGLAbstr::renderFrame()
{
glClear(GL_COLOR_BUFFER_BIT);
SwapBuffers(hDC);
//Rendering code
}
bool OGLAbstr::openEditorWindow(WNDCLASSEX wc)
{
Window = CreateWindowEx(0, wc.lpszClassName, "Echeos Editor", WS_OVERLAPPEDWINDOW | WS_MAXIMIZE | WS_VISIBLE | WS_CLIPCHILDREN, 0, 0, 800, 600, 0, 0, hInstance, 0);
if(!Window)
{
log->logMessage(log->WNDERR, "Could not create Window");
return false;
}
Menu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU));
RECT rect;
GetClientRect(Window, &rect);
RenderWindow = CreateWindowEx(0, "STATIC", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 0, 0, rect.right-rect.left, rect.bottom-rect.top, Window, NULL, hInstance, 0);
SetMenu(Window, Menu);
ShowWindow(Window, SW_SHOW);
ShowWindow(Window, SW_MAXIMIZE);
log->logMessage(log->WND, "Created Editor window");
return true;
}
|