/*
 * Init.c - Main functions, intialization and what not, usually dealing with
 *          struct game_data stuff.
 *
 * By: Odis
 */
#include <windows.h>
#include "main.h"
#include "game.h"

GAME *game;
PLAYER_DATA *player;
HGLOBAL songmem = NULL;
HRSRC res = NULL;

DWORD tpause = 0;

/*  Make the class name into a global variable  */
char szClassName[ ] = "StickManGameEngine";
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* get this windows api stuff out of the way */
bool init_game(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int iCmdShow)
{
    WNDCLASSEX wincl;        /* Data structure for the windowclass */
    HWND hwnd;               /* This is the handle for our window */

    CREATE(game, GAME, 1 );  /* alloc game */

    if (game == NULL)
     return FALSE;

    /* The Window structure */
    wincl.hInstance = hInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_SKELETON));
    wincl.hIconSm = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_SKELETON_SM));
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return FALSE;


    /* Calculate the window size and position based upon the game size */
    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;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Stickman Game Engine",       /* Title Text */
           WS_POPUP|WS_SYSMENU|WS_VISIBLE|WS_CAPTION|WS_MINIMIZEBOX, /* default window */
           xpos,
           ypos,/* where the window ends up on the screen */
           width,                 /* The programs width */
           height,                /* and height in pixels */
           /*HWND_DESKTOP,         The window is a child-window to desktop */
           NULL,
           NULL,                /* No menu */
           hInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    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; /* game starts at 0 */

    SetFrameRate(PRG_DEFAULT_FPS);

    /* create player info */
    CREATE( player, PLAYER_DATA, 1 );
    
    player->score = 0;
    player->lives = 1;
    player->health = 5;
    player->name = (char *)strdup("");
    

/* Load song via resource
    res = FindResourceEx(hInstance, "WAVE", MAKEINTRESOURCE(IDW_SONG), MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL));
    songmem = LoadResource(hInstance, res);
*/

    game_start(hwnd);

    /* Make the window visible on the screen */
    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);  /* set window and start game */
      return 0;

    case WM_ACTIVATE:
      if (wParam != WA_INACTIVE)  /* update sleep */
      {
        SetSleep(FALSE);
        game_activate();
        
        /* correct time */
       if ( gpause == FALSE )
       {
        start += (GetTickCount()-tpause);
        tpause = GetTickCount();
        SetTimer( game->hWindow, T_ID, SECOND, NULL );
       }
      }
      else /* deactivate */
      {
        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);  /* all "painting" is done within this function */

      EndPaint(hWindow, &ps);
      return 0;

    case WM_DESTROY:
      game_end();       /* end game and quit */
      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: /* handle this timer! */
#ifdef DX
         if ( wParam == STREAM_TIMER )
           handle_dx(FALSE);
         else
#endif
           if ( game->sleep == FALSE )
           {}
         return 0;
  }
  return DefWindowProc(hWindow, msg, wParam, lParam);
}

/* helping functions */
void SetWindow( HWND hWnd )
{
  game->hWindow = hWnd;
}

void SetFrameRate( int rate )
{
  game->framedelay = (1000/rate);
}

void SetSleep( bool bSleep )
{
   game->sleep = bSleep;
}