home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 13
/
mediashare_13.zip
/
mediashare_13
/
ZIPPED
/
PROGRAM
/
WTJ9403.ZIP
/
BARNHART
/
GM.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-11-21
|
5KB
|
208 lines
// gm.cpp - Main program for C++ graphics animation example
// Andy Barnhart
// This program is provided as a sample to accompany an article on graphics
// animation using C++
// This seems to work for me and I hope it works for you. No other warranty
// is expressed or implied.
#include <so_all.hpp>
#define WM_STARTGAME WM_USER+1
long FAR PASCAL _export WndProc( HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam );
static int keystate = 0;
extern int WinSX, WinSY;
static HDC hDC;
static ScreenObj *so;
class Main
{
public:
static HANDLE hInstance;
static HANDLE hPrevInstance;
static int nCmdShow;
static int MessageLoop( void );
};
Player *player = NULL;
HANDLE Main::hInstance = 0;
HANDLE Main::hPrevInstance = 0;
int Main::nCmdShow = 0;
int Main::MessageLoop( void )
{
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
long FAR PASCAL _export WndProc( HWND hWnd, UINT iMessage, WPARAM wParam,
LPARAM lParam )
{
switch (iMessage)
{
case WM_CREATE:
PostMessage( hWnd, WM_STARTGAME, 0, 0);
SetTimer(hWnd, 100, 100, NULL);
hDC = GetDC( hWnd);
InitScreen( hWnd, hDC, Main::hInstance);
so = new ScreenBack(GMX, GMY);
break;
case WM_STARTGAME:
InitScene( 1);
break;
case WM_SIZE:
SizeScreen( LOWORD( lParam), HIWORD( lParam));
return DefWindowProc( hWnd, iMessage, wParam, lParam );
case WM_DESTROY:
DeleteScene();
TermScreen();
ReleaseDC( hWnd, hDC);
PostQuitMessage( 0 );
break;
case WM_ERASEBKGND:
return( 0L);
case WM_PAINT:
{
PAINTSTRUCT ps;
ScreenObj *nso;
BeginPaint( hWnd, &ps);
ps.rcPaint.top += WinSY;
ps.rcPaint.bottom += WinSY;
ps.rcPaint.left += WinSX;
ps.rcPaint.right += WinSX;
for( nso = ScreenObj::Background; nso != NULL; nso = nso->Next())
nso->Paint( &ps.rcPaint, hDC);
EndPaint( hWnd, &ps);
break;
}
case WM_KEYDOWN:
switch( wParam)
{
case VK_RIGHT:
keystate |= KEY_RIGHT;
keystate &= ~KEY_LEFT;
break;
case VK_LEFT:
keystate |= KEY_LEFT;
keystate &= ~KEY_RIGHT;
break;
case VK_UP:
keystate |= KEY_UP;
keystate &= ~KEY_DOWN;
break;
case VK_DOWN:
keystate |= KEY_DOWN;
keystate &=~KEY_UP;
break;
case VK_SPACE:
player->Fire();
break;
default:
return DefWindowProc( hWnd, iMessage, wParam, lParam );
}
break;
case WM_KEYUP:
switch( wParam)
{
case VK_RIGHT:
keystate &= ~KEY_RIGHT;
if( GetKeyState(VK_LEFT) & 0x8000) keystate |= KEY_LEFT;
break;
case VK_LEFT:
keystate &= ~KEY_LEFT;
if( GetKeyState(VK_RIGHT) & 0x8000) keystate |= KEY_RIGHT;
break;
case VK_UP:
keystate &= ~KEY_UP;
if( GetKeyState(VK_DOWN) & 0x8000) keystate |= KEY_DOWN;
break;
case VK_DOWN:
keystate &= ~KEY_DOWN;
if( GetKeyState(VK_UP) & 0x8000) keystate |= KEY_UP;
break;
default:
return DefWindowProc( hWnd, iMessage, wParam, lParam );
}
player->Update( keystate);
break;
case WM_TIMER:
{
ScreenObj *nso;
for( nso = ScreenObj::Background; nso != NULL; nso = nso->Next())
if( nso == player)
player->Update( keystate);
else
nso->Update();
}
break;
default:
return DefWindowProc( hWnd, iMessage, wParam, lParam );
}
return 0;
}
int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
int nCmdShow )
{
HWND hWnd;
Main::hInstance = hInstance;
Main::hPrevInstance = hPrevInstance;
Main::nCmdShow = nCmdShow;
if ( ! Main::hPrevInstance )
{
WNDCLASS wndclass; // Structure used to register Windows class.
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_SAVEBITS;
wndclass.lpfnWndProc = ::WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = Main::hInstance;
wndclass.hIcon = LoadIcon( Main::hInstance, "gameicon" );
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "GAME_WINx";
if ( ! RegisterClass( &wndclass ) )
exit( FALSE );
}
hWnd = CreateWindow( "GAME_WINx","Romero, son of...",
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
Main::hInstance,
NULL );
if ( ! hWnd )
exit( FALSE );
ShowWindow( hWnd, SW_SHOWMAXIMIZED);
UpdateWindow( hWnd );
return Main::MessageLoop();
}