unit iz_main;

interface
uses
  windows, OpenGL, messages, iz_gfx, iz_Data, iz_options;

function WinProc(Window: hWnd; Message, wParam: Word; lParam: LongInt): LongInt; stdcall;
procedure VSync(interval : GLint);

implementation

function WinProc(Window: hWnd; Message, wParam: Word; lParam: LongInt): LongInt; stdcall;
begin
  result := 0;
  case Message of
    WM_ACTIVATE :  begin
                     if Options.FScreen = 1 then
                       case LoWord(wParam) of
                           WA_ACTIVE,WA_CLICKACTIVE : begin
                             PostMessage(Window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
                             gfx.Minimize := false;
                             gfx.SetFullscreen(Options.ScreenWidth,Options.ScreenHeight,Options.BitDepth,Options.Freq);
                             StartTime := GetTickCount;
                             gfx.FActive:=True;                          //Установка флага активности программы
                             ShowCursor( false );
                           end;
                           WA_INACTIVE : begin
                             gfx.FActive:=False;                         //Перешли на другую задачу, окно не активно!
                             PostMessage(Window, WM_SYSCOMMAND, SC_MINIMIZE, 0);
                             gfx.Minimize := true;
                             gfx.RestoreResolution();
                           end;
                       end  //case
                    else
                     case LoWord(wParam) of
                     WA_ACTIVE,WA_CLICKACTIVE : begin
                                                  gfx.FActive:=True;     //Установка флага активности программы
                                                  StartTime := GetTickCount;
                                                  gfx.Minimize := false;
                                                end;
                     WA_INACTIVE              : begin
                                                  gfx.FActive:=False;    //Перешли на другую задачу, окно не активно!
                                                  gfx.Minimize := True;
                                                end;
                     end;  //case Lo(wParam)
                   end;

   WM_KILLFOCUS :  begin
                     gfx.Fokus := false;
                     gfx.Minimize := false;
                   end;
   WM_SETFOCUS  :  begin
                     gfx.Fokus := True;
                   end;
   WM_MOUSEMOVE: begin
      Gfx.Input.Y  := HIWORD( lParam );
      Gfx.Input.X := LOWORD( lParam );
    end;
   WM_LBUTTONDOWN: Gfx.Input.mKeys[0] := true;
   WM_LBUTTONUP: Gfx.Input.mKeys[0] := false;
   WM_RBUTTONDOWN: Gfx.Input.mKeys[2] := true;
   WM_RBUTTONUP: Gfx.Input.mKeys[2] := false;
   WM_LBUTTONDBLCLK: Gfx.Input.mKeys[4] := true;
   WM_KEYDOWN:
    begin
      Gfx.Input.Keys[wParam] := true;
      Gfx.Input.LastKey := wParam;
    end;
    WM_KEYUP:
    begin
      Gfx.Input.Keys[wParam] := false;
      {if wParam=13 then Game.EnterName := false else
      if wParam=8 then Delete(Game.Player_Name,length(Game.Player_Name),1) else
      if ((wParam in [65..90]) and (Game.EnterName)) then
      if length(Game.Player_Name) < 11 then
      Game.Player_Name := Game.Player_Name + Chr(wParam);}
    end;
    WM_CHAR:
    begin
      Gfx.Input.LastChar := Chr(wParam);
    end;

   WM_SIZE : begin
               {Options.ScreenWidth := LOWORD( lParam );
               Options.ScreenHeight := HIWORD( lParam );}
               glViewport(0, 0, Options.ScreenWidth, Options.ScreenHeight);
               Gfx.SetCoord(Gfx.Coord, true);
             end;

   WM_CLOSE : begin
                gfx.Quit := true;
                PostQuitMessage(0);
                Exit;                             //Завершение работы приложения
              end;
  else    //case
    Result := DefWindowProc(Window,Message,WParam,LParam);
  end;
end;

procedure VSync(interval : GLint);
var
  wglSwapIntervalEXT : function(interval : GLint) : GLboolean; stdcall;
begin
  wglSwapIntervalEXT := wglGetProcAddress('wglSwapIntervalEXT');
  wglSwapIntervalEXT(interval);
end;

end.

