Codice:
#include "stdafx.h"
#include "CFramework.h"
CFramework::CFramework( HINSTANCE hInstance )
{
this->m_hInstance = hInstance;
this->m_loop = false;
}
void CFramework::Release()
{
graphic->Release();
UnregisterClass( m_className, this->m_hInstance );
}
bool CFramework::Initialize( char *title, int width, int height, int bpp, bool fullscreen )
{
this->m_className = title;
WNDCLASSEX wc;
wc.cbSize = sizeof( WNDCLASSEX );
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = StaticWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = this->m_hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = title;
wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
if( !RegisterClassEx( &wc ) ) return false;
this->m_hWnd = CreateWindowEx( NULL,
title,
title,
WS_OVERLAPPEDWINDOW | WS_VISIBLE |
WS_SYSMENU | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
100, 100,
width, height,
NULL,
NULL,
this->m_hInstance,
NULL );
if( !this->m_hWnd ) return false;
ShowWindow( this->m_hWnd, SW_SHOW );
UpdateWindow( this->m_hWnd );
graphic = new CGraphics( this->m_hWnd );
if( !graphic->Initialize() ) return false;
return true;
}
void CFramework::Run()
{
MSG msg;
while(!this->m_loop)
{
if( PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
{
this->m_loop = true;
}
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
graphic->Run();
}
}
}
LRESULT CALLBACK CFramework::StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
if ( msg == WM_CREATE )
{
SetWindowLongPtr( hWnd, GWLP_USERDATA, (LONG)((CREATESTRUCT *)lParam)->lpCreateParams );
}
CFramework *targetApp = (CFramework*)GetWindowLongPtr( hWnd, GWLP_USERDATA );
if ( targetApp )
{
return targetApp->WndProc( hWnd, msg, wParam, lParam );
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
LRESULT CALLBACK CFramework::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN:
// Send keystrokes to application to handle
return 0;
}
return DefWindowProc( hWnd, message, wParam, lParam );
}