c++ the main class designs, in win32 application

Started by
7 comments, last by antareus 19 years, 7 months ago
Hi, im using c++ win32 application. I'm curious how I should layout my classes etc Usually I've just had a main.cpp file that contained the WinMain and WinProc(MsgProc, whatever you call it) and declarations of classes such as, the game engine, render, window etc. But in college they taught us java, and class designs. They had a main class called controller with everything declared inside of that. But I don't see how thats possible as you can't call winmain inside of a class and if you did call WinProc inside a class, it would have to be static and then I can't put other class's messages into there unless they are static, which i dont think is a good idea at all. So how do you usually layout your c++ win32app programs? Thankyou.
Advertisement
I am currently setting up some info to show how to wrap some classes and some design pointers. It currently shows you how to make your engine have no actual reference of the windows message loop.

http://dmoney.no-ip.org/tutorials.php

I am currently zipping up a solution in which I use both opengl and directx as wrappers and depending upon a single comment you can decide which one you use. It is a pretty good example of wrapping. It should be on it once anyone reads this.
Make the window procedure static as normal.
Each window you create, do it with a member function of the class. Use SetWindowLongPtr with index of GWLP_USERDATA to use the "this" pointer as the windows "extra user data".

In the windows static proc, immediately get the pointer back (via GetWindowLongPtr), cast it back to a pointer of the correct type, and invoke a non-static window function with it.

Hope that helps.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Most of my apps have a Main.cpp containing only this:
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int){CApp theApp;   theApp.Run();   return 0;}

And I have my WndProc in a class. Its still perfectly possible, if you save a pointer to the class in the GWL_USERDATA field for use with GetWindowLong() / SetWindowLong().
A damn FAQ.

You could just use WTL and let it handle the gory details of mapping a WndProc to a class instance. Thunking is probably the most elegant way to do this but you'll need to know some assembly.

Oluyesi(sp) wrote a tutorial on this very topic. Read it.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
A class to do it seems like a complete waste of time.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
WTL is probably the best way of doing this. Check codeproject.com for a WTL game loop.

It also compiles down to the smallest executable for a windows framework
daerid@gmail.com
Quote:Original post by Paradigm Shifter
Make the window procedure static as normal.
Each window you create, do it with a member function of the class. Use SetWindowLongPtr with index of GWLP_USERDATA to use the "this" pointer as the windows "extra user data".

In the windows static proc, immediately get the pointer back (via GetWindowLongPtr), cast it back to a pointer of the correct type, and invoke a non-static window function with it.

Hope that helps.


would you be able to give me a quick example?

Thanks

Also I'm not sure that I want to use WTL, as I rather not have any extra code, or dlls etc

Is this for a game? Or for an app?

If its for a game then its totally useless to wrap the message loop since you're only going to have one window. If its for an app with more than one window I can see wanting to do this.

WTL has no runtime dependencies and is really, really damn small.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement