Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

HexDump

Any Way to Encapsulate WinMain and WindowProc in a Application class?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, I´m making a Application class and I would like to know if there is any way of encapsulate WinMain+WindowProc from the user. Well perhaps this is a bit confusing, lets see an pseudo implementation of the thing I need. I would like to achieve something like this: class CApplication { .... public: virtual int WINAPI WinMain(HINSTANCE,HINSTANCE hPrevInstance,\ LPSTR, int)=0; virtual int CALLBACK WindowProc(HWND, UINT, WPARAM,LPARAM)=0; .... ); This way, when an user inherits from this class it implements its own WinMain & WindowProc methods, what I think is better than have winmain/windowproc global. Is anyway to do this?. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
You could try doing this they way the ALLEGRO game library does it. They do a bit of preprocessor magic in a header file and this then calls the user defined winmain function (which in the case of allegro happens to be the ANSI C int main() function)

IMPLEMENTATION:
If you implement the WinMain function in a header file, than you can call your user defined function from there.i.e.

header.h
int WINAPI WinMain(HINSTANCE ....)
{
return USERWinMain(hInstance...);
}

hope this helps!




Does it matter? Even if it does matter, does it matter that it matters?

Share this post


Link to post
Share on other sites
Concerning WinMain() you are out of luck - it can''t be done since that name is hardcoded in to Windows.

But WindowsProc is doable since you define and reference it yourself from your setup code. But to do that it must match the prototype of the standard WindowProc exactly. To do that make it static and non-virtual.

static LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM,LPARAM);


Jacob Marner, M.Sc.
Console Programmer, Deadline Games

Share this post


Link to post
Share on other sites
actually...

All PE .exe''s use an "entry point" offset into the exe - the linker searches the object code for a symbol called "WinMainCRTStartup" to determine which function this offset points at.

That symbol is defined in crt0.c which is the REAL entry point for programs made in MSVC, it does various setup things then calls any WinMain() function present. The crt0.c file and the rest of the CRT source comes with the Platform SDK and MSVC.

You can tell MSVC to tell the linker to use a *different* entry point to "WinMainCRTStartup" in the Project->Settings->Link->Output->Entry-point Symbol box.

However the extra processing which takes place in crt0.c IS required by most programs to ensure the C runtime library is setup, static and global class constructors are called etc.

What you CAN do is edit crt0.c and add a new entry point to call something other than WinMain and link the results together.

However IMO trying to eradicate every global function is total overkill - you''re not writing code for an OO OS so don''t try to make it look that way. I really don''t get the people who think you''re not programming in C++ unless _everything_ is a class etc, even if that way of programming is the least suitable for the task at hand...

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Hi,
Here is an exemple:
you inherit an child class from this base class
the base class has 3 virtual methods you can implement:
init(),frame(),shutdown().
In init you place the initialisation of your game
in frame you place your actual game
and in shutdown...
those 3 functions are called from the method run in the base class.

So,all you still have to do is call run().

thx to the book "programming roles playing games"

#include "stdafx.h"
#include "cIncludes.h"

/*-------------------------------------------------------------
Class die windows en game-loop verzorgt.
virtual functions : init,frame,shutdown (en msgproc)
-------------------------------------------------------------*/

#ifndef _SYSTEM_H_
#define _SYSTEM_H_

class cWindows
{
private:
//instance handle
HINSTANCE hInst;
HWND hWnd;

protected:
//class name
char className[MAX_PATH];
//window title
char caption[MAX_PATH];

//wndclassex structure
WNDCLASSEX wcex;

DWORD style;
DWORD xPos;
DWORD yPos;
DWORD width;
DWORD height;


public:

//constructor
cWindows();

//game loop functie
bool run();

//getters
HINSTANCE gethInst();
HWND gethWnd();

//mouse
void showMouse(bool show = true);

//messageHandler
virtual FAR PASCAL msgProc (HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
virtual bool init(){return true;}
virtual bool frame(){return true;}
virtual bool shutdown(){return true;}
};
static cWindows *pWindow = NULL;
//deze functie ontvangt msg van windows
static long FAR PASCAL msgHandler(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);



cWindows::cWindows()
{
//save instantie
pWindow = this;
//get instance handle
hInst = GetModuleHandle(NULL);

//default className en title
strcpy(className,"AppClass");
strcpy(caption,"Application");

style = WS_OVERLAPPEDWINDOW;;
xPos = 0;
yPos = 0;
width = 800;
height = 600;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = msgHandler;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = className;
wcex.hIconSm = LoadIcon(NULL,IDI_APPLICATION);


}

long FAR PASCAL msgHandler(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default: return pWindow->msgProc(hWnd,uMsg,wParam,lParam);
}
}

bool cWindows::run ()
{
MSG msg;

//register
if(!RegisterClassEx(&wcex))
return false;

//create window
hWnd = CreateWindow(className,caption,
style,
xPos,yPos,
width,height,
NULL,NULL,hInst,NULL);
if(!hWnd)
return false;

ShowWindow(hWnd,SW_NORMAL);
UpdateWindow(hWnd);


//Resize(width,height);

//virtuele init functie
if(init()==true)
{
ZeroMemory(&msg,sizeof(MSG));
//enter message loop
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if(frame()==false)
break;
}
}
}

shutdown();


//unregister window class
UnregisterClass(className,hInst);
return true;
}

HINSTANCE cWindows::gethInst()
{
return hInst;
}

HWND cWindows::gethWnd()
{
return hWnd;
}

void cWindows::showMouse(bool show)
{
ShowCursor (show);

Share this post


Link to post
Share on other sites
Don''t bother using OOP for a low level thing like that. Have your WinMain and Window proc call a class that handles the events (need a global variable ) that handles the windows stuff

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!