I've been messing around with threads, and I want to create a thread for the drawing of a opengl scene, and one for handling windows messages. This is what i've got so far:
main.cpp
[source lang="cpp]
//includes
#include <gl/gl.h>
#include <gl/glu.h>
#include <windows.h>
//classes
#include "classes.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmd, int show) {
GAME gam("This is a Window", 400, 400, false, show);
gam.go();
system("PAUSE");
}
classes.h
//global vars
bool REG = false;
//global functions
bool resize(RECT _rect) {
int h;
int w;
w = _rect.right;
h = _rect.bottom;
if (_rect.right == 0)
h = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, 1.0 * w / h, 1, 1000);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0.0,1.0,1.0,
0.0,0.0,0.0,
0.0f,1.0f,0.0f);
}
bool drawCube(float _size) {
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
}
class WINDOW {
public:
bool makeWindow(char *_title, int _x, int _y, bool _fullscreen, int _show) {
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
if (!REG) {
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(0);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);;
wc.lpszMenuName = NULL;
wc.lpszClassName = "window";
RegisterClassEx(&wc);
REG = true;
}
hwnd = CreateWindowEx(
0,
"window",
_title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
_x, _y,
NULL, NULL, GetModuleHandle(0), NULL);
dc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.cColorBits = 32;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
int i = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc, i, &pfd);
rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);
ShowWindow(hwnd, _show);
}
//WndProc
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM w, LPARAM l) {
static RECT rect;
switch (message) {
case WM_CLOSE:
exit(0);
case WM_SIZE:
GetClientRect(hwnd, &rect);
//resize
resize(rect);
return 0;
default:
break;
}
return DefWindowProc(hwnd, message, w, l);
}
//vars
HWND hwnd;
MSG message;
HGLRC rc;
HDC dc;
private:
};
class GAME {
public:
GAME(char *_title, int _x, int _y, bool _fullscreen, int _show) {
win.makeWindow(_title, _x, _y, _fullscreen, _show);
QUIT = false;
//initiate opengl properly
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0);
}
GAME() {}
bool go() {
thread = CreateThread(NULL, 0, StartThreadDraw, (LPVOID)this, NULL, &threadID);
while (!QUIT) {
if (PeekMessage(&win.message, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&win.message);
DispatchMessage(&win.message);
}
}
}
//vars
WINDOW win;
float theta;
bool QUIT;
HANDLE thread;
DWORD threadID;
private:
static DWORD WINAPI StartThreadDraw(LPVOID pthis) {
GAME *game = (GAME*)pthis;
game->draw();
//shouldn't get here
}
//draw
bool draw() {
wglMakeCurrent(win.dc, win.rc);
while (!QUIT) {
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glRotatef (theta, 0.0f, 0.3f, 1.0f);
drawCube(0.4);
glPopMatrix();
SwapBuffers(win.dc);
theta += 1.0f;
Sleep (1);
}
}
};
this compiles, but all i get is this: