Win32 programs without global variables

This topic is 2771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm starting up on my new game, and, having a new approach, I'm trying to restrain from global variables, but it seems to be impossible due to the message procedure functions. I have found global variables to be excruciatingly painful when trying to split a program into multiple source files, but it seems that windows doesn't allow things otherwise. This wouldn't be a problem if you could pass arguments into the WndProc functions. Anyone have any idea how can local scoped variables be accessed from there?

Share on other sites
To be honest i have just started rewriting in C++ and my project uses only 1 global function. the AppWndProc() i did this;

Window.h
class Window{private:    //All my Priv varaibles etc.    public:   //Constructor etc.long FAR PASCAL HandleEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);};//Define the Window proclong FAR PASCAL AppWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

Window.cpp
long FAR PASCAL Window::HandleEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){	switch(msg)	{	case WM_DESTROY:		{			PostQuitMessage(0);			return 0;			break;		}	}	return DefWindowProc(hWnd, msg, wParam, lParam);}//The Message procurelong FAR PASCAL AppWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){	CWindow Wind;	return Wind.HandleEvent(hWnd, msg, wParam, lParam);}

This means all message are handled inside my Windowclass, nothing is used by the window procedure so no arguments need to be passed.

If there is another arg i need to pass, i pass itinto the Window::HandleEvent(...)

Share on other sites
You can effectively pass arguments to your WindowProc using SetWindowLongPtr and GetWindowLongPtr with the GWLP_USERDATA flag. It's not exactly pretty, but it works.

Share on other sites
Thanks guys but this is just way too much screwing around for me ^.^ I'll just stick with the regular way to go.