#include <windows.h>
#include "main.h"
#include "game.h"
GAME *game;
PLAYER_DATA *player;
HGLOBAL songmem = NULL;
HRSRC res = NULL;
DWORD tpause = 0;
char szClassName[ ] = "StickManGameEngine";
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
bool init_game(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int iCmdShow)
{
WNDCLASSEX wincl; HWND hwnd; CREATE(game, GAME, 1 ); if (game == NULL)
return FALSE;
wincl.hInstance = hInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; wincl.style = CS_DBLCLKS; wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_SKELETON));
wincl.hIconSm = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_SKELETON_SM));
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; wincl.cbClsExtra = 0; wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return FALSE;
int width = PRG_WIDTH + GetSystemMetrics(SM_CXFIXEDFRAME) * 2;
int height = PRG_HEIGHT + GetSystemMetrics(SM_CYFIXEDFRAME) * 2 + GetSystemMetrics(SM_CYCAPTION);
if (wincl.lpszMenuName != NULL)
height += GetSystemMetrics(SM_CYMENU);
int xpos = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
int ypos = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
hwnd = CreateWindowEx (
0, szClassName, "Stickman Game Engine", WS_POPUP|WS_SYSMENU|WS_VISIBLE|WS_CAPTION|WS_MINIMIZEBOX, xpos,
ypos, width, height, NULL,
NULL, hInstance, NULL );
if ( !hwnd )
return FALSE;
game->hInstance = hInstance;
game->width = PRG_WIDTH;
game->height = PRG_HEIGHT;
game->sleep = FALSE;
game->first_bmp = NULL;
game->last_bmp = NULL;
game->current_level = 0; SetFrameRate(PRG_DEFAULT_FPS);
CREATE( player, PLAYER_DATA, 1 );
player->score = 0;
player->lives = 1;
player->health = 5;
player->name = (char *)strdup("");
game_start(hwnd);
ShowWindow (hwnd, iCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
LRESULT handle_event(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
switch (msg)
{
case WM_CREATE:
SetWindow(hWindow); return 0;
case WM_ACTIVATE:
if (wParam != WA_INACTIVE) {
SetSleep(FALSE);
game_activate();
if ( gpause == FALSE )
{
start += (GetTickCount()-tpause);
tpause = GetTickCount();
SetTimer( game->hWindow, T_ID, SECOND, NULL );
}
}
else {
SetSleep(TRUE);
game_deactivate();
if ( gpause == FALSE )
{
tpause = GetTickCount();
KillTimer(game->hWindow ,T_ID);
}
}
return 0;
case WM_PAINT:
hDC = BeginPaint(hWindow, &ps);
game_paint(hDC); EndPaint(hWindow, &ps);
return 0;
case WM_DESTROY:
game_end(); PostQuitMessage(0);
return 0;
case WM_LBUTTONDOWN:
mousebuttondown(LOWORD(lParam), HIWORD(lParam), TRUE );
return 0;
case WM_LBUTTONUP:
mousebuttonup(LOWORD(lParam), HIWORD(lParam), TRUE);
return 0;
case WM_RBUTTONDOWN:
mousebuttondown(LOWORD(lParam), HIWORD(lParam), FALSE );
return 0;
case WM_RBUTTONUP:
mousebuttonup(LOWORD(lParam), HIWORD(lParam), FALSE );
return 0;
case WM_MOUSEMOVE:
mousemove(LOWORD(lParam), HIWORD(lParam));
return 0;
case WM_TIMER: #ifdef DX
if ( wParam == STREAM_TIMER )
handle_dx(FALSE);
else
#endif
if ( game->sleep == FALSE )
{}
return 0;
}
return DefWindowProc(hWindow, msg, wParam, lParam);
}
void SetWindow( HWND hWnd )
{
game->hWindow = hWnd;
}
void SetFrameRate( int rate )
{
game->framedelay = (1000/rate);
}
void SetSleep( bool bSleep )
{
game->sleep = bSleep;
}