Window Creation in Win 7

Started by
29 comments, last by Dim_Yimma_H 7 years, 12 months ago

Hello,

I need help with creating a Microsoft Window in Win 7. I've experimented with calls to DoCreateWindow() with no luck.

I'd like to create a full fledged window that can switch between full screen mode and windowed mode with many resolution modes available and also with double buffering.

I am attempting this using C++ in MS VS Express 2013 and would like to continue using both.

Any hints, tips or tricks?

Thanks!

Advertisement

winapi + createwindowex

you set size and flag as maximized without border additionally you change screen resolution before window creation.

ideally:

http://nehe.gamedev.net/data/lessons/vc/lesson01.zip

I was told to steer clear of NEHE . But maybe it's better now as that was a long time ago.

revisiting . . .

Thanks!

NeHe tutorials don’t get better as time passes.

https://msdn.microsoft.com/en-us/library/bb384843.aspx

http://www.winprog.org/tutorial/simple_window.html

http://www.win32developer.com/tutorial/windows/windows_tutorial_2.shtm

https://www.google.com/search?q=Win32+Create+Window+Tutorial&oq=Win32+Create+Window+Tutorial

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I found it useful to look at DXUT:

https://dxut.codeplex.com/

Maybe another example to look at would be Freeglut:

http://freeglut.sourceforge.net/

I used both as examples for my physics testbed years ago and never ran into any issues. Both should have examples for what you are trying to achieve.

HTH,

-Dirk

L. Spiro - the NEHE tutorial linked above did actually work after I added old glaux.h and .lib files to my code. But yes the code is from 2000 so, obsolete. At least I got something off the ground so not a complete waste.

And it looks like Win32 was the secret google search sauce I needed. Thanks!

Dirk - Thanks for those links! Checking now. :)
Thanks so much for all the sources guys!

I've made quite a few windows so far. Now my next question...

How do I configure a Microsoft window tomget out of the way and let me render using OpenGL ?

I've been through a fair amount of sell research in OpenGL using the latest versions of the OpenGL superbible. And while some of the example code works, I can't seem to draw using OpenGL in windows I've created myself.

I'd like to draw using OpenGL 4.5 but 4.3 seems to be the most recent version supported by most graphics cards.

Thanks Again!

You need to set the window pixel format and then create an OpenGL context.

You should be able to pick out the parts required to get it running in your window from this example:


#include <windows.h>
#include <gl/gl.h>

#pragma comment (lib, "opengl32.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	// Window class
	WNDCLASSEX wc = {};
	wc.cbSize = sizeof(wc);
	wc.lpszClassName = TEXT("MyClass");
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.style = CS_OWNDC;
	RegisterClassEx(&wc);

	// Window
	HWND hWnd = CreateWindowEx(
		0,
		wc.lpszClassName,
		TEXT("OpenGL Window"),
		WS_OVERLAPPEDWINDOW |
			WS_CLIPSIBLINGS |
			WS_CLIPCHILDREN,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,
		NULL,
		hInstance,
		NULL
	);

	// Get DC
	HDC hDC = GetDC(hWnd);

	// Pixel format
	PIXELFORMATDESCRIPTOR pfd = {};
	pfd.nSize = sizeof(pfd);
	pfd.nVersion = 1;
	pfd.dwFlags =
		PFD_DRAW_TO_WINDOW |
		PFD_SUPPORT_OPENGL |
		PFD_DOUBLEBUFFER |
		PFD_SUPPORT_COMPOSITION;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;

	int pf = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, pf, &pfd);

	// OpenGL context
	HGLRC hGLRC = wglCreateContext(hDC);

	wglMakeCurrent(hDC, hGLRC);

	// Get OpenGL version and set it as window title
	SetWindowTextA(hWnd, (char*)glGetString(GL_VERSION));

	// Main loop
	ShowWindow(hWnd, nCmdShow);
	while(true) {
		MSG msg;
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
			if(msg.message == WM_QUIT)
				break;
			else {
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else {
			// Draw
			glClearColor(1.0f, 0.7f, 0.2f, 1.0f);
			glClear(GL_COLOR_BUFFER_BIT);

			SwapBuffers(hDC);
		}
	}

	// Clean up
	wglMakeCurrent(NULL, NULL);
	wglDeleteContext(hGLRC);

	UnregisterClass(wc.lpszClassName, hInstance);

	return 0;
}

// Window proc
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch(msg) {
		case WM_DESTROY:
			PostQuitMessage(0);
		return 0;

		case WM_SIZE:
			// Resize GL rendering area to match window size
			glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
		return 0;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

However, that will only really allow you to use quite old OpenGL functionality without some extra work, as the newer version functions aren't readily available in opengl32.lib. The default library only provides version 1.1 or something like that (even when the context version is 4.5).

To load more functions you need to request their function pointers from WGL while your GL context is current, for example to use the glCompileShader function:


PFNGLCOMPILESHADERPROC glCompileShader = (PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader");

The function pointer will match a function in the NVidia or AMD driver for example, not in the system OpenGL DLL.

This is a bit tedious, and if you want to do it manually the best way is probably to download glcorearb.h from opengl.org and write a script that goes through it and loads all the functions into some appropriate wrapper or global pointers or something.

There are libraries written specifically to take care of all this, such as GLEW for example: http://glew.sourceforge.net/

It can take care of checking all extensions etc. for you and help make sure everything is loaded properly and detect when it isn't. This is especially useful when loading extensions that may be available only on some drivers. If you make sure the context version is 4.x and you load only core functionality matching that version it's just about loading all the function pointers.

Wow Erik thats an Awesome reply! Thanks!

Can you show me an example of how to use GLEW?

I recall reading about GLEW as well and it was yet another peice I couldnt wrap my head around. :)

This topic is closed to new replies.

Advertisement