I tried to draw a white square in a window, but I must be doing something wrong, since it doesn't work.
When I debug it, I get an access violation here:
void ResizeWind(void){
glViewport(0, 0, (int)windW, (int)windH); //<-- I think it's here
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-windW/2., -windH/2., windW/2., windH/2., scrnDist, windD);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
I can't figure out what I'm doing wrong.
The rest of the code.
#include "windows.h"
#include "gl\gl.h"
#include "math.h"
#include "time.h"
#include "string.h"
HGLRC hRC=NULL;
HDC hDC=NULL;
HWND hWnd=NULL;
HINSTANCE hInst;
short FPSVal=1000/40,key[256];
double scrnDist=20.,windH=240.,windW=320.,windD=100.;
long timer1;
char className[]="Thingy", windTitle[]="Thingy";
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void Stuff(void){
}
void GLDraw(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4d(1.,1.,1.,1.);
glLoadIdentity();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(50.,50.,50.);
glVertex3f(-50.,50.,50.);
glVertex3f(50.,-50.,50.);
glVertex3f(-50.,-50.,50.);
glEnd();
return;
}
void ResizeWind(void){
glViewport(0, 0, (int)windW, (int)windH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-windW/2., -windH/2., windW/2., windH/2., scrnDist, windD);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLSetup(void){
//glEnable(GL_TEXTURE_2D);
//glEnable(GL_CULL_FACE);
//glClearDepth(1.);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_FLAT);
glClearColor(0.,0.,0.,0.);
//glEnable(GL_ALPHA_TEST);
//glAlphaFunc(GL_GEQUAL,.9);
//glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
ResizeWind();
}
void DestroyWind(void){
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd,hDC);
DestroyWindow(hWnd);
UnregisterClass(className,hInst);
}
short MakeWind(void){
int PixelFormat;
WNDCLASS wc;
DWORD dwExStyle, dwStyle;
RECT WindRect;
WindRect.left=(long)0;
WindRect.right=(long)windW;
WindRect.top=(long)0;
WindRect.bottom=(long)windH;
hInst = GetModuleHandle(NULL);
wc.style = CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, NULL);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
if (!RegisterClass(&wc)){
MessageBox(NULL,"Unable to RegisterClass","Oh Noes!",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle=WS_OVERLAPPEDWINDOW;
AdjustWindowRectEx(&WindRect, dwStyle, FALSE, dwExStyle);
if (!(hWnd=CreateWindowEx(dwExStyle,
className,
windTitle,
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN |
dwStyle,
0, 0,
WindRect.right-WindRect.left,
WindRect.bottom-WindRect.top,
NULL,
NULL,
hInst,
NULL))){
DestroyWind();
MessageBox(NULL,"Unable to Make Window","...",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
static PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
16, //Color Depth
0, 0, 0, 0, 0, 0, //Color Bits
0, //Alpha Buffer
0, //Shift Bit
0, //Accumulation Buffer
0, 0, 0, 0, //Accumulation Bits
16, //Depth Buffer
0, //Stencil Buffer
0, //Auxiliary Buffer
PFD_MAIN_PLANE,
0,
0, 0, 0 /*Layer Masks*/};
hDC=GetDC(hWnd);
if(!(PixelFormat=ChoosePixelFormat(hDC,&pfd))){
DestroyWind();
MessageBox(NULL,"Unable to Find a PixelFormat","Pixels are Good",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd)){
DestroyWind();
MessageBox(NULL,"Unable to Set PixelFormat","Grr...",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
ShowWindow(hWnd,SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
GLSetup();
return 1;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow){
srand(time(NULL));
rand();
timer1=clock()-FPSVal/2;
MSG msg;
short done=0;
if (!MakeWind()){
return 0;
}
while(!done){
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
if (msg.message==WM_QUIT){
done=1;
}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else{
if (GetAsyncKeyState(27)!=0){//Esc
done=1;
}
else{
if((clock()-timer1)>FPSVal){
timer1=timer1+FPSVal;
Stuff();
GLDraw();
SwapBuffers(hDC);
}
sleep(1);
}
}
}
DestroyWind();
return (msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch (uMsg)
{
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
/*case WM_SIZE:
{
ResizeWind(LOWORD(lParam),HIWORD(lParam));
return 0;
}*/
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}