Sign in to follow this  
johnnyBravo

c++ the main class designs, in win32 application

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this