I have never created an actual game loop using a window that I have created myself and implimenting messages and whatnot ... but what I thought would simply work doesnt work. Could someone help me out a bit?
This is the main hassle bit:
do
{
while(GetMessage(&msg, hWnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
XPos = 5.0;
//Frame?
} while (GameRuns == 1);
The
XPos = 5.0 bit is never reached :-( Why is this?
If you want a look at ALL of the code then here it is:
#include <windows.h> /* must include this before GL/gl.h */
#include <GL/gl.h> /* OpenGL header file */
#include <GL/glu.h> /* OpenGL utilities header file */
//#include <stdio.h>
int GameRuns = 1;
GLfloat XPos = 0.0;
//int FORM_WIDTH, FORM_HEIGHT;
void display()
{
// MODELVIEW TRANSFORMATION
//glMatrixMode(GL_MODELVIEW);
//clearing the background with the color specified
glClearColor(0.0f,1.0f,0.0f,1.0f);
//glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(50.0, 1.0, 3.0, 7.0);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//glTranslatef(-0.5f,0.0f,-0.0f);
// Finished Drawing The Triangle
//gluPerspective(50.0, 1.0, 1.0, 100.0);
//gluLookAt(-1.0, 0.0, 1.5, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/* PUSHING THE MATRIX STACK */
//glPushMatrix();
/*glLoadIdentity();
glTranslatef(0.0, 0.0, -0.56);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(0.0f, 0.0f, 1.0f); // Blue Colour
glVertex3f( 0.0f, 1.0f, -1.0f); // Top
glVertex3f(-1.0f,-1.0f, -1.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, -1.0f); // Bottom Right
glEnd();*/
//glPopMatrix();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
/*glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, -1.0f); // Top
glVertex3f(-1.0f,-1.0f, -1.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, -1.0f); // Bottom Right
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, -2.0f); // Top
glVertex3f(-1.0f,-1.0f, -2.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, -2.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle*/
glBegin(GL_TRIANGLES);
glColor3f(
1.0f,
0.0f,
0.0f);
glVertex3f( -
9.5f + XPos,
1.0f, -
2.0f);
glVertex3f(-
10.0f + XPos,-
1.0f, -
2.0f);
glVertex3f( -
9.0f + XPos,-
1.0f, -
2.0f);
glEnd();
glFlush();
}
LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static PAINTSTRUCT ps;
switch(uMsg)
{
case WM_PAINT:
display();
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
return 0;
case WM_SIZE:
glViewport(
0,
0, LOWORD(lParam), HIWORD(lParam));
PostMessage(hWnd, WM_PAINT,
0,
0);
return 0;
case WM_CHAR:
switch (wParam)
{
case 27:
GameRuns =
0;
PostQuitMessage(
0);
break;
}
return 0;
case WM_CLOSE:
GameRuns =
0;
PostQuitMessage(
0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
HWND CreateOpenGLWindow(
char* title,
int x,
int y,
int width,
int height, BYTE type, DWORD flags)
{
int pf;
HDC hDC;
HWND hWnd;
WNDCLASS wc;
PIXELFORMATDESCRIPTOR pfd;
static HINSTANCE hInstance =
0;
if (!hInstance)
{
hInstance = GetModuleHandle(NULL);
wc.style = CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.cbClsExtra =
0;
wc.cbWndExtra =
0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName =
"OpenGL";
if (!RegisterClass(&wc))
{
MessageBox(NULL,
"RegisterClass() failed: Cannot register window class.",
"Error", MB_OK);
return NULL;
}
}
hWnd = CreateWindow(
"OpenGL", title, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, x, y, width, height, NULL, NULL, hInstance, NULL);
if (hWnd == NULL)
{
MessageBox(NULL,
"CreateWindow() failed: Cannot create a window.",
"Error", MB_OK);
return NULL;
}
hDC = GetDC(hWnd);
memset(&pfd,
0,
sizeof(pfd));
pfd.nSize =
sizeof(pfd);
pfd.nVersion =
1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
pfd.iPixelType = type;
pfd.cColorBits =
32;
pf = ChoosePixelFormat(hDC, &pfd);
if (pf ==
0)
{
MessageBox(NULL,
"ChoosePixelFormat() failed: Cannot find a suitable pixel format.",
"Error", MB_OK);
return 0;
}
if (SetPixelFormat(hDC, pf, &pfd) ==
FALSE)
{
MessageBox(NULL,
"SetPixelFormat() failed: Cannot set format specified.",
"Error", MB_OK);
return 0;
}
DescribePixelFormat(hDC, pf,
sizeof(PIXELFORMATDESCRIPTOR), &pfd);
ReleaseDC(hWnd, hDC);
return hWnd;
}
int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine,
int nCmdShow)
{
HDC hDC;
HGLRC hRC;
HWND hWnd;
MSG msg;
hWnd = CreateOpenGLWindow(
"XyuRi",
100,
100,
640,
480, PFD_TYPE_RGBA,
0);
if (hWnd == NULL) {exit(
1);}
hDC = GetDC(hWnd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
ShowWindow(hWnd, nCmdShow);
glEnable(GL_DEPTH_TEST);
do
{
while(GetMessage(&msg, hWnd,
0,
0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
XPos =
5.
0;
}
while (GameRuns ==
1);
wglMakeCurrent(NULL, NULL);
ReleaseDC(hWnd, hDC);
wglDeleteContext(hRC);
DestroyWindow(hWnd);
return msg.wParam;
}
Any help with
this is greatly appreciated as per usual :-)
Thanks.
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend