Jump to content
  • Advertisement

Archived

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

wild_pointer

My windows wrapper

This topic is 5931 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

I sat down for an hour and came up with the simplest wrapper I could, looking for comments/suggestions/errors Win.h
  
class CWin  
{
public:
	CWin( HINSTANCE app_inst );
	virtual ~CWin();

    bool MakeWindow( std::string title, RECT Res );
    bool ProcessMessages( MSG &msg );
    static LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param );

private:
    WNDCLASSEX ClassInfo;
    HWND hwnd;
};
  
Win.cpp
  
CWin::CWin( HINSTANCE app_inst )
{
    WNDCLASSEX WndClass =
    {
        sizeof( WNDCLASSEX ),               // cbSize

        CS_HREDRAW | CS_VREDRAW,            // style

        WndProc,                            // lpfnWndProc

        0, 0,                               // cbClsExtra, cbWndExtra

        app_inst,                           // hInstance

        LoadIcon( NULL, IDI_APPLICATION ),  // hIcon

        LoadCursor( NULL, IDC_ARROW ),      // hCursor

        (HBRUSH)GetStockObject( WHITE_BRUSH ),  // hbrBackground

        NULL,                               // lpszMenuName

        "lpszClassName",                    // lpszClassName

        LoadIcon( NULL, IDI_WINLOGO )       // hIconSm

    };

    ClassInfo = WndClass;
}

CWin::~CWin()
{
    DestroyWindow( hwnd );
}

bool CWin::MakeWindow( std::string title, RECT Res )
{
    if( 0 == RegisterClassEx( &ClassInfo ) )
        return false;

    hwnd = CreateWindowEx(
        NULL,                               // dwExStyle

        ClassInfo.lpszClassName,            // lpClassName

        title.c_str(),                      // lpWindowName

        WS_OVERLAPPEDWINDOW | WS_VISIBLE,   // dwStyle

        Res.left, Res.top,                  // x, y

        Res.right, Res.bottom,              // nWidth, nHeight

		NULL,                               // hWndParent

		NULL,                               // hMenu

		ClassInfo.hInstance,                // hInstance

		NULL );                             // lpParam


    if( NULL == hwnd )
        return false;

    return true;
}

LRESULT CALLBACK CWin::WndProc( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param )
{
    switch( msg )
    {
        case WM_CLOSE:			            // Windows is closing

            DestroyWindow( hwnd );
            PostQuitMessage( 0 );
            return 0;
            break;

        default:
            break;
    }

    return DefWindowProc( hwnd, msg, w_param, l_param );
}

bool CWin::ProcessMessages( MSG &msg )
{
    if( PeekMessage( &msg, hwnd, NULL, NULL, PM_REMOVE ) )
    {
        if( msg.message == WM_QUIT )
        {
            return false;
        }
        else
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }

    return true;
}
  
WinMain
  
int APIENTRY WinMain( HINSTANCE hInstance,
                      HINSTANCE hPrevInstance,
                      LPSTR     lpCmdLine,
                      int       nCmdShow )
{
    CWin        MyWin( hInstance );

    RECT rect = { 0, 0, 800, 600 };

    if( false == MyWin.MakeWindow( "My Window", rect ) )
    {
        MessageBox( NULL, "MakeWindow", "ERROR", MB_OK );
        return 0;
    }

    MSG	        msg;

    while( MyWin.ProcessMessages( msg ) )
    {
        // Do Stuff

    }

	return msg.wParam;
}
  
I was thinking about passing a function pointer to ProcessMessages, and putting the loop inside the process method.

MyWin.ProcessMessages( msg, UpdateScene )

void UpdateScene()
{
    // Do Stuff
}
 

[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

Share this post


Link to post
Share on other sites
Advertisement
Nothing? Ok, well I just made a opengl class that is derived from the one above. Seems to work fairly well, just had to overload the makewindow method.

Only reason i''m asking is this is the start of my first non-procedural "engine", i don''t want to paint myself into a corner. Anything to watch out for?

Share this post


Link to post
Share on other sites
I made such a class too. I just added virtual mouse and keyboard functions to my application-class that can be overloaded by any class that extends it.

Share this post


Link to post
Share on other sites
The code you have for process messages function is the same code used for all the controls (windows even) in your program. You need ONE copy of that code in your main function.

Image what would happen if you have 2 windows and you call both of their processmessage functions in the main code.

I suggest you create a CApplication class and put the main function in there as static and wrap the message handling code in there.

Also, you might want to give access to the window handle (HWND) to other objects, so why not make an accessor function getHandle(). Just an idea.

-Viktor


-G|aD-

Share this post


Link to post
Share on other sites
A single window app is pretty limiting. You can see one way to accomplish what Viktor is suggesting here:

http://home.earthlink.net/~deadlyo/Programming/WinApi/WindowsAPI_Overview.html

David

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!