program XTropper;
(*************************************)
(* XTropper project                  *)
(* Created by XProger                *)
(* begin : 4.07.05                   *)
(* end   : 1.08.05                   *)
(*************************************)
(* site : http://XProger.mirgames.ru *)
(* mail : XProger@list.ru            *)
(*************************************)
uses
  Windows,
  Messages,
  OpenGL,
  KOL in 'KOL\kol.pas',
  err in 'KOL\err.pas',
  JpegObj in 'KOL\JpegObj.pas',
  MiniFMOD in 'MiniFMOD\MiniFMOD.pas',
  l_gl,
  l_sound,
  l_input,
  l_utils,
  l_game,
  l_map,
  l_spider,
  l_particle,
  l_menu;

{$R *.RES}

var
 Time, Time_Delta, Time_Old : DWORD;

// Маленькое баловство - коппирайт в ехе файле ;)
procedure Copyright;
begin
end;

exports
 Copyright name '<<< Copyright (c) XTropper by XProger >>>';

(*********************)
(*    ПРИЛОЖЕНИЕ     *)
(*********************)
function WndProc(hwnd: DWORD; message: UINT; wParam: Longint; lParam: LongInt): LongInt; stdcall;
begin
 Case message of
  WM_QUERYENDSESSION, WM_CLOSE:
   begin
   Game_Free;
   glClose;
   snd_deInit;
   isQuit := true;
   end;

  WM_ACTIVATEAPP:
   if Initialized then
    if LOWORD(wParam) = WA_ACTIVE then
     begin
     ResetKeys;
     glActive := true;
     ShowWindow(Handle, SW_SHOWNORMAL);
     if glFullScreen then
      glDisplay(glWidth, glHeight, glBPP, glFreq);
     Time_Old := GetTimer;
     end
    else
     begin
     ResetKeys;
     glActive := false;
     ShowWindow(Handle, SW_SHOWMINIMIZED);
     if glFullScreen then
      ChangeDisplaySettings(_devicemodeA(nil^), CDS_FULLSCREEN);
     end;
 // обработка клавы
  WM_KEYDOWN   : KeyOnDown(wParam);
  WM_KEYUP     : KeyOnUp(wParam);
 // обработка мыши
  WM_LBUTTONUP   : KeyOnUp(0);
  WM_RBUTTONUP   : KeyOnUp(1);
  WM_MBUTTONUP   : KeyOnUp(2);
  WM_LBUTTONDOWN : KeyOnDown(0);
  WM_RBUTTONDOWN : KeyOnDown(1);
  WM_MBUTTONDOWN : KeyOnDown(2);
 end;
if inMenu then
 Menu_Message(message, wParam, lParam);
Result := DefWindowProc(hwnd, message, wParam, lParam);
end;

function CreateWindow: HWND;
var
 wndclass : WNDCLASSEX;
begin
ZeroMemory(@wndclass, sizeof(WNDCLASSEX));
with wndclass do
 begin
 cbSize        := sizeof(WNDCLASSEX);
 style         := CS_HREDRAW or CS_VREDRAW or CS_OWNDC;
 lpfnWndProc   := @WndProc;
 cbClsExtra    := 0;
 cbWndExtra    := 0;
 hInstance     := 0;
 hIcon         := LoadIcon (hInstance, 'MAINICON');
 hCursor       := LoadCursor (0, IDC_ARROW);
 hbrBackground := HBRUSH(GetStockObject(BLACK_BRUSH));
 lpszMenuName  := nil;
 lpszClassName := 'XTropper_Wnd';
 end;
RegisterClassEx(wndclass);

Result := CreateWindowEx(WS_EX_TOPMOST,
                         'XTropper_Wnd',
                         'XTropper',
                         WS_POPUP,
                         0, 0,
                         glWidth, glHeight,
			                   0, 0, 0, nil);

SendMessage(Result, WM_SETICON, 1, LoadIcon(hInstance, 'MAINICON'));
end;

var
 i    : integer;
 flag : boolean;
 msg  : TMSG;
 str  : string;
begin
// читаем параметры коммандной строки
for i := 1 to ParamCount do
 str := str + ParamStr(i);
glFullScreen := pos('-nofullscreen', str) = 0;
snd_off      := pos('-nosound', str) <> 0;
noblood      := pos('-noblood', str) <> 0;
nogibs       := pos('-nogibs', str) <> 0;
if pos('-optimize', str) <> 0 then
 begin
 glWidth  := 320;
 glHeight := 240;
 glFreq   := 60;
 glBPP    := 16;
 end;

Handle := CreateWindow;
if (Handle = 0) or not glInit then
 Exit;
snd_Init; 
Game_Init;
SetForegroundWindow(Handle);
ShowWindow(Handle, SW_SHOW);
ShowCursor(false);
UpdateWindow(Handle);
SetCursorPos(glWidth div 2, glHeight div 2);
if glFullScreen then
 glDisplay(glWidth, glHeight, glBPP, glFreq);

Initialized := true;

Time_Old := GetTimer - 20;
while not isQuit do
 begin
 while PeekMessage(msg, 0, 0, 0, PM_REMOVE) do
  begin
  TranslateMessage(msg);
  DispatchMessage(msg);
  end;

 if not glActive then
  WaitMessage
 else
  begin
  Time       := GetTimer;
  Time_Delta := Time - Time_Old;

  flag := false;
  for i := 1 to Time_Delta div 20 do
   begin
   Game_Update;
   snd_Update;
   flag := true;
   end;

  if flag = true then
   Time_Old := Time - Time_Delta mod 20;
  Game_Render;
  end;
 end;
 
WndProc(Handle, WM_CLOSE, 0, 0);
end.
