Original Post
Could someone please tell me what is wrong with this code. I''m learning OGL from the Superbible thats online. When I typed in the code myself it doesn''t work. It''s supposed to make a blue window with a red square bouncing around inside of it. The only thing that shows up is the blue window. It doesn''t show the red square. All my functions are being called, I tested that already. What else is wrong with this. I cut and paisted out of the book and it worked. I''ve spent the past two days trying to figure out what was wrong but it didn''t work. Thanks.
HINSTANCE hInstance;
HWND hWND;
MSG message;
HGLRC hRC;
HDC hDC;
GLfloat x1=100.0f;
GLfloat y1=150.0f;
GLsizei rsize=50;
GLfloat xstep=1.0f;
GLfloat ystep=1.0f;
GLfloat windowWidth;
GLfloat windowHeight;
LRESULT CALLBACK WinProc(HWND hWND,
UINT message,
WPARAM wParam,
LPARAM lParam);
void CALLBACK ChangeSize(GLsizei w, GLsizei h)
{
if(h==0)
h=1;
glViewport(0,0,w,h);
glLoadIdentity();
if(w <= h)
{
windowHeight=250.0f*h/w;
windowWidth=250.0f;
}
else
{
windowWidth=250.0f*w/h;
windowHeight=250.0f;
}
glOrtho(0.0f, windowWidth, 0.0f, windowHeight, 1.0f, -1.0f);
}
void CALLBACK RenderScene(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rsize,y1+rsize);
glFlush();
}
void CALLBACK IdleFunction(void)
{
if(x1 > windowWidth-rsize || x1 < 0)
xstep=-xstep;
if(y1 > windowHeight-rsize || y1 < 0)
ystep=-ystep;
if(x1 > windowWidth-rsize)
x1=windowWidth-rsize-1;
if(y1 > windowHeight-rsize)
y1=windowHeight-rsize-1;
x1+=xstep;
y1+=ystep;
RenderScene();
}
void SetDCPixelFormat(HDC hDC)
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,0,
0,0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0};
nPixelFormat=ChoosePixelFormat(hDC,&pfd);
SetPixelFormat(hDC,nPixelFormat,&pfd);
}
BOOL CreateGLWindow()
{
WNDCLASS wClass;
wClass.style =CS_HREDRAW|CS_VREDRAW;
wClass.lpfnWndProc =(WNDPROC)WinProc;
wClass.cbClsExtra =0;
wClass.cbWndExtra =0;
wClass.hInstance =hInstance;
wClass.hCursor =LoadCursor(NULL,IDC_ARROW);
wClass.hIcon =NULL;
wClass.lpszClassName="MY WINDOW";
wClass.lpszMenuName =NULL;
wClass.hbrBackground=NULL;
if(RegisterClass(&wClass)==0)
return FALSE;
hWND=CreateWindow("MY WINDOW",
"MY WINDOW",
WS_OVERLAPPEDWINDOW|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
100, 100,
250, 250,
NULL,
NULL,
hInstance,
NULL);
if(hWND==NULL)
return FALSE;
return TRUE;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CreateGLWindow();
ShowWindow(hWND, SW_SHOW);
UpdateWindow(hWND);
while(GetMessage(&message,NULL, 0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
LRESULT CALLBACK WinProc(HWND hWND,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
hDC=GetDC(hWND);
SetDCPixelFormat(hDC);
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
SetTimer(hWND,101,1,NULL);
break;
case WM_DESTROY:
KillTimer(hWND,101);
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hRC);
PostQuitMessage(0);
break;
case WM_SIZE:
ChangeSize(LOWORD(lParam),HIWORD(wParam));
break;
case WM_TIMER:
{
IdleFunction();
InvalidateRect(hWND,NULL,FALSE);
}
break;
case WM_PAINT:
{
RenderScene();
SwapBuffers(hDC);
ValidateRect(hWND,NULL);
}
break;
default:
return (DefWindowProc(hWND,message,wParam,lParam));
}
return 0;
}
Mat