what is the simplest way possible to create a OpenGL window
what is the simplest way possible to create a OpenGL window
How do you mean? Using the Win32 API or maybe Glut?
Need more info ...
which ever one is quickest
This is my first night on this board - how do I post source formatted code?
Seeing as no one answered, this will look like crap, but here goes
HINSTANCE hInstance;
HWND hWnd;
HDC hDC;
HGLRC hRC;
DISPLAY_PROPERTIES *CurrentDisplaySettings;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hInstance = hInst;
dspGetDefaultSettings(*CurrentDisplaySettings);
//GetDisplaySettings();
//GetDisplayAdapters();
if (wndCreateOpenGLWindow(1024, 768, 32, 24, 0))
{
//game init
while (MessageHandler() == true)
{
//main game loop
}
//game shutdown
}
wndDestroyOpenGLWindow();
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return false;
break;
case WM_SYSCOMMAND:
switch(lParam)
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
default: break;
}
break;
case WM_CLOSE:
PostQuitMessage(0);
return false;
break;
case WM_SIZE:
//dspOpenGLAspect(LOWORD(lparam), HIWORD(lparam));
return false;
break;
default:break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
bool MessageHandler(void)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
{
return false;
}
}
return true;
}
void dspGetDefaultSettings(DISPLAY_PROPERTIES &dspProp)
{
}
bool wndCreateOpenGLWindow(long Width, long Height, char ColorBits, char DepthBits, char StencilBits)
{
if (!wndRegisterClass()) return false;
if (!wndChangeDisplayMode(Width, Height, ColorBits)) return false;
hWnd = wndCreateWindow(Width, Height); if (hWnd == NULL) return false;
hDC = GetDC(hWnd); if (hDC == NULL) return false;
if (!wndSetPixelFormat(Width, Height, ColorBits, DepthBits, StencilBits)) return false;
if (!wndCreateOpenGLContext()) return false;
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
dspOpenGLAspect(Width, Height);
glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SwapBuffers(hDC);
return true;
}
unsigned short wndRegisterClass(void)
{
WNDCLASS wndClass;
wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndClass.lpfnWndProc = (WNDPROC) WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = NULL;
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = "Game";
return RegisterClass(&wndClass);
}
bool wndChangeDisplayMode(long Width, long Height, long ColorBits)
{
DEVMODE dmScreenSettings;
ZeroMemory (&dmScreenSettings, sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = Width;
dmScreenSettings.dmPelsHeight = Height;
dmScreenSettings.dmBitsPerPel = ColorBits;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
return (ChangeDisplaySettingsEx(NULL, &dmScreenSettings, NULL, CDS_FULLSCREEN, NULL) == DISP_CHANGE_SUCCESSFUL);
}
HWND wndCreateWindow(long Width, long Height)
{
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;
ShowCursor(FALSE);
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
return CreateWindowEx(dwExStyle, "Game", "Game", dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, Width, Height, NULL, NULL, hInstance, NULL);
}
int wndSetPixelFormat(long Width, long Height, char ColorBits, char DepthBits, char StencilBits)
{
PIXELFORMATDESCRIPTOR pfd;
int PixelFormat = 0;
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = ColorBits;
pfd.cRedBits = 0;
pfd.cRedShift = 0;
pfd.cGreenBits = 0;
pfd.cGreenShift = 0;
pfd.cBlueBits = 0;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 0;
pfd.cAccumRedBits = 0;
pfd.cAccumGreenBits = 0;
pfd.cAccumBlueBits = 0;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = DepthBits;
pfd.cStencilBits = StencilBits;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;
PixelFormat = ChoosePixelFormat(hDC, &pfd);
if (!PixelFormat) return false;
return SetPixelFormat(hDC, PixelFormat, &pfd);
}
int wndCreateOpenGLContext(void)
{
hRC = wglCreateContext(hDC);
if (hRC == NULL) return false;
return wglMakeCurrent(hDC, hRC);
}
void wndDestroyOpenGLWindow(void)
{
ChangeDisplaySettings(NULL, 0);
ShowCursor(TRUE);
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
hRC = NULL;
ReleaseDC(hWnd, hDC);
hDC = NULL;
DestroyWindow(hWnd);
hWnd=NULL;
UnregisterClass("OpenGL", hInstance);
hInstance = NULL;
}
void dspOpenGLAspect(long Width, long Height)
{
if (Height == 0)
{
Height = 1;
}
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLdouble)Width / (GLdouble)Height, 1.0f, 1000.0f);
//glFrustum( -0.5, 0.5, (Height/Width)/2, -(Height/Width)/2, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Don't forget to prototype these functions!
How about this for createing you window.
int main() {
glh.Config();
glh.CreateWin();
glh.Perspective(60,0.1,100);
glh.EnterLoop();
}
Check out the OpenGL Helper Library
home.earthlink.net/~eberkain/GLH.html
Hi Eber!
But that looks awfully like GLUT to me! Which is a BAD thing! ... just my opinion though.
wasnt that kinda long though theres gotta be an easier or shorter wa of doing the same thing without using glut... or is there
It's nothing to do with OpenGL ... it's to do with creating an OpenGL window within the Microsoft Windows Operating System! And other OS's are much the same. What Eber is saying, is that he has a library which hides much of the overhead involved in this - which may or may not suit your purposes ... make up you own mind ... or copy and paste the example I gave you!
Good Luck
Besides ... about the most lengthy piece of code in there sets up the PIXELFORMATDESCRIPTOR ... bearing in mind that ZeroMemory has already cleared most of it, you can reduce that piece of code to just a few lines ...
And if you take out the error checking (making sure each stage of window creation succeeds) then it will reduce much further too - but beware of the consequences if something goes wrong!
GLUT enables you to learn OpenGL without learning the huge Win32 API. It also makes your OpenGL apps portable to other operating systems, which is one of the main benefits of OpenGL over Direct3D.
Here is an free online book with many examples using GLUT.
http://ask.ii.uib.no/ebt-bin/nph-dwe...per/OpenGL_PG/
Shag, it may look like glut, but my GLH library is only for Windows systems, and only creates fullscreen stuff.
The opposite of glut in implementation, similar to glut in design.