For Bored Coders, 1 File Ogl/windows Program Isn't
<div class="IPBDescription">Responding to messages, and I'm tired.</div> Keep in mind you'll need to include opengl32.lib and glu32.lib.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
int WindowWidth = 800;
int WindowHeight = 600;
int WindowBPP = 32;
bool Fullscreen = false;
HDC hDC;
bool Loop = true;
void SetupPixelFormat(HDC hDC);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void SetupProjection(int Width, int Height);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX WinClass;
HWND hWnd;
MSG msg;
DWORD ExStyle;
DWORD Style;
RECT WindowRect;
float angle;
WindowRect.left = (long)0;
WindowRect.right = (long)WindowWidth;
WindowRect.top = (long)0;
WindowRect.bottom = (long)WindowHeight;
WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.style = CS_HREDRAW | CS_VREDRAW;
WinClass.lpfnWndProc = WindowProc;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hInstance = hInstance;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.hbrBackground = NULL;
WinClass.lpszMenuName = NULL;
WinClass.lpszClassName = "WinClass";
WinClass.hIconSm = WinClass.hIcon;
if(!RegisterClassEx(&WinClass))
return 0;
if(Fullscreen)
{
DEVMODE ScreenSettings;
ZeroMemory(&ScreenSettings, sizeof(ScreenSettings));
ScreenSettings.dmSize = sizeof(ScreenSettings);
ScreenSettings.dmPelsWidth = WindowWidth;
ScreenSettings.dmPelsHeight = WindowHeight;
ScreenSettings.dmBitsPerPel = WindowBPP;
ScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if(ChangeDisplaySettings(&ScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
MessageBox(NULL, "Display mode failed", "Error", MB_ICONEXCLAMATION);
Fullscreen = false;
}
if(Fullscreen)
{
ExStyle = WS_EX_APPWINDOW;
Style = WS_POPUP;
ShowCursor(FALSE);
}
else
{
ExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
Style = WS_OVERLAPPEDWINDOW;
}
}
AdjustWindowRectEx(&WindowRect, Style, NULL, ExStyle);
hWnd = CreateWindowEx(NULL,
"WinClass",
"OpenGL Window",
Style | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
NULL,
NULL,
hInstance,
NULL);
hDC = GetDC(hWnd);
if(!hWnd)
return 0;
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
glClearColor(0.0, 0.0, 0.0, 0.0);
angle = 0.0f;
while(Loop == true)
{
angle += 0.1f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0f);
glRotatef(angle, 1.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glColor3f(0.7f, 1.0f, 0.3f);
glBegin(GL_TRIANGLES);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
SwapBuffers(hDC);
while(PeekMessage(&msg, hWnd, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, hWnd, 0, 0))
{
Loop = false;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(Fullscreen)
{
ChangeDisplaySettings(NULL, 0);
ShowCursor(TRUE);
}
return (int)msg.wParam;
}
void SetupPixelFormat(HDC hDC)
{
int PixelFormat;
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW |
PFD_DOUBLEBUFFER |
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0,
};
PixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, PixelFormat, &pfd);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
int Height, Width;
switch(msg)
{
case WM_CREATE:
hDC = GetDC(hWnd);
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
break;
case WM_DESTROY:
case WM_QUIT:
case WM_CLOSE:
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
PostQuitMessage(0);
break;
case WM_SIZE:
Height = HIWORD(lParam);
Width = LOWORD(lParam);
SetupProjection(Width, Height);
break;
case WM_KEYDOWN:
int Keys;
LPARAM KeyData;
Keys = (int)wParam;
KeyData = lParam;
switch(Keys)
{
case VK_ESCAPE:
PostQuitMessage(0);
default:
break;
}
break;
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
void SetupProjection(int Width, int Height)
{
if(Height == 0)
Height = 1;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(52.0f,(float)Width/(float)Height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
WindowWidth = Width;
WindowHeight= Height;
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
It'll compile, it just won't handle any messages. I know the answers in front of my face, but I just powertyped for ten/fifteen minutes and have about had it up to here.
/points finger up and out some
Oh, and yes, I know the design sucks, but the point was to just get everything working before I organize and put it all in classes and etcetera, etcetera...
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
int WindowWidth = 800;
int WindowHeight = 600;
int WindowBPP = 32;
bool Fullscreen = false;
HDC hDC;
bool Loop = true;
void SetupPixelFormat(HDC hDC);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void SetupProjection(int Width, int Height);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX WinClass;
HWND hWnd;
MSG msg;
DWORD ExStyle;
DWORD Style;
RECT WindowRect;
float angle;
WindowRect.left = (long)0;
WindowRect.right = (long)WindowWidth;
WindowRect.top = (long)0;
WindowRect.bottom = (long)WindowHeight;
WinClass.cbSize = sizeof(WNDCLASSEX);
WinClass.style = CS_HREDRAW | CS_VREDRAW;
WinClass.lpfnWndProc = WindowProc;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hInstance = hInstance;
WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.hbrBackground = NULL;
WinClass.lpszMenuName = NULL;
WinClass.lpszClassName = "WinClass";
WinClass.hIconSm = WinClass.hIcon;
if(!RegisterClassEx(&WinClass))
return 0;
if(Fullscreen)
{
DEVMODE ScreenSettings;
ZeroMemory(&ScreenSettings, sizeof(ScreenSettings));
ScreenSettings.dmSize = sizeof(ScreenSettings);
ScreenSettings.dmPelsWidth = WindowWidth;
ScreenSettings.dmPelsHeight = WindowHeight;
ScreenSettings.dmBitsPerPel = WindowBPP;
ScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if(ChangeDisplaySettings(&ScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
MessageBox(NULL, "Display mode failed", "Error", MB_ICONEXCLAMATION);
Fullscreen = false;
}
if(Fullscreen)
{
ExStyle = WS_EX_APPWINDOW;
Style = WS_POPUP;
ShowCursor(FALSE);
}
else
{
ExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
Style = WS_OVERLAPPEDWINDOW;
}
}
AdjustWindowRectEx(&WindowRect, Style, NULL, ExStyle);
hWnd = CreateWindowEx(NULL,
"WinClass",
"OpenGL Window",
Style | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
NULL,
NULL,
hInstance,
NULL);
hDC = GetDC(hWnd);
if(!hWnd)
return 0;
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
glClearColor(0.0, 0.0, 0.0, 0.0);
angle = 0.0f;
while(Loop == true)
{
angle += 0.1f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0f);
glRotatef(angle, 1.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glColor3f(0.7f, 1.0f, 0.3f);
glBegin(GL_TRIANGLES);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
SwapBuffers(hDC);
while(PeekMessage(&msg, hWnd, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, hWnd, 0, 0))
{
Loop = false;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if(Fullscreen)
{
ChangeDisplaySettings(NULL, 0);
ShowCursor(TRUE);
}
return (int)msg.wParam;
}
void SetupPixelFormat(HDC hDC)
{
int PixelFormat;
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_SUPPORT_OPENGL |
PFD_DRAW_TO_WINDOW |
PFD_DOUBLEBUFFER |
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0,
};
PixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, PixelFormat, &pfd);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
int Height, Width;
switch(msg)
{
case WM_CREATE:
hDC = GetDC(hWnd);
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
break;
case WM_DESTROY:
case WM_QUIT:
case WM_CLOSE:
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
PostQuitMessage(0);
break;
case WM_SIZE:
Height = HIWORD(lParam);
Width = LOWORD(lParam);
SetupProjection(Width, Height);
break;
case WM_KEYDOWN:
int Keys;
LPARAM KeyData;
Keys = (int)wParam;
KeyData = lParam;
switch(Keys)
{
case VK_ESCAPE:
PostQuitMessage(0);
default:
break;
}
break;
default:
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
void SetupProjection(int Width, int Height)
{
if(Height == 0)
Height = 1;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(52.0f,(float)Width/(float)Height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
WindowWidth = Width;
WindowHeight= Height;
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
It'll compile, it just won't handle any messages. I know the answers in front of my face, but I just powertyped for ten/fifteen minutes and have about had it up to here.
/points finger up and out some
Oh, and yes, I know the design sucks, but the point was to just get everything working before I organize and put it all in classes and etcetera, etcetera...
Comments
Help? Please?
So do you recommend GLUT, then?
I can't read it <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo--> We learn windows APi next semester. Try me on some fandabidozie data structures they are fun <!--emo&???--><img src='http://www.unknownworlds.com/forums/html//emoticons/confused-fix.gif' border='0' style='vertical-align:middle' alt='confused-fix.gif' /><!--endemo-->
EDIT: What school?