/*
 * Main.c - Contains messy Windoz API startup stuffs
 * Generated by Dev-C++, original is now hacked among several files
 * Modified by Odis
 */
#include <windows.h>
#include "main.h"

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

LONG PRG_WIDTH = 500;
LONG PRG_HEIGHT = 400;

int WINAPI WinMain (HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int iCmdShow)

{
    MSG msg;                 /* Here messages to the application are saved */
    static int trigger = 0;
    int tickcount;
    
    if ( init_game(hInstance,hPrevInstance,lpszArgument,iCmdShow) == TRUE )
    {
         while (TRUE)
         {
            if ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                 if (msg.message == WM_QUIT)
                  break;
                  
                  TranslateMessage(&msg);
                  DispatchMessage(&msg);
            }
            else
            {
               /* no processing while the game is sleeping */
               if ( game->sleep == FALSE )
               {
                 tickcount = GetTickCount();

                 if ( tickcount > trigger )
                 {
                   trigger = tickcount + game->framedelay;
                   handle_keys();
                   game_loop();
                 }
               }
            }
         }
         return (int)msg.wParam;
    }
    else
     return FALSE;

   game_end();
   return TRUE;
}


/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure (HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam)
{
  return handle_event( hWindow, msg, wParam, lParam );
}