///////////////////////////////////////////////////////////////////////
// Copyright (C) Andru 2007                                          //
// mail: dr.andru@gmail.com                                          //
// site: http://isada.mirgames.ru                                    //
//                                                                   //
// OMEGA Engine is free software.                                    //
//                                                                   //
// You may redistribute it and/or modify it under the terms of the   //
// GNU General Public License, as published by the Free Software     //
// Foundation; either version 2 of the License, or (at your option)  //
// any later version.                                                //
//                                                                   //
// this module is distributed in the hope that it will be useful,    //
// but WITHOUT ANY WARRANTY; without even the implied warranty of    //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              //
// See the GNU General Public License for more details.              //
//                                                                   //
// You should have received a copy of the GNU General Public License //
// along with this module.  If not, write to:                        //
// 	The Free Software Foundation, Inc.,                              //
// 	51 Franklin Street, Fifth Floor                                  //
// 	Boston, MA  02110-1301, USA.                                     //
///////////////////////////////////////////////////////////////////////
library OMEGA;

{$I defines.pas}
{$IFNDEF LINUX}
  {$R OMEGA.res}
{$ENDIF}

uses
  Messages,
  Windows,

  U_TWindowInfo   in 'SYS_UNITS\U_TWindowInfo.pas',
  OpenGL          in 'SYS_UNITS\OpenGL.pas',

  minFile         in 'SYS_UNITS\minFile.pas',
  minClasses      in 'SYS_UNITS\minClasses.pas',

  ENGINE_TYPES    in 'ENGINE_TYPES.pas',
  ENGINE_VAR      in 'ENGINE_VAR.pas',
  ENGINE_CONST    in 'ENGINE_CONST.pas',

  ENGINE_UTILS    in 'ENGINE_UTILS.pas',

  ENGINE_MAIN     in 'ENGINE_MAIN.pas',
  ENGINE_CALLBACK in 'ENGINE_CALLBACK.pas',
  ENGINE_TIMERS   in 'ENGINE_TIMERS.pas',

  ENGINE_FLAGS    in 'ENGINE_FLAGS.pas',

  ENGINE_WINDOW   in 'ENGINE_WINDOW.pas',
  ENGINE_SCREEN   in 'ENGINE_SCREEN.pas',
  ENGINE_GL       in 'ENGINE_GL.pas',
  
  GL_UTILS        in 'GL_UTILS.pas',

  ENGINE_CONSOLE  in 'ENGINE_CONSOLE.pas',
  ENGINE_LOG      in 'ENGINE_LOG.pas',
  ENGINE_INI      in 'ENGINE_INI.pas',

  INPUT_KEYBOARD  in 'INPUT_KEYBOARD.pas',
  INPUT_MOUSE     in 'INPUT_MOUSE.pas',
  INPUT_JOY       in 'INPUT_JOY.pas',

  TEXTURE_FORMATS in 'TEXTURE_FORMATS.pas',
  TEXTURE_TGA     in 'TEXTURE_TGA.pas',
  TEXTURE_LOADER  in 'TEXTURE_LOADER.pas',
  TEXTURE_MANAGER in 'TEXTURE_MANAGER.pas',
  TEXTURE_EFFECTS in 'TEXTURE_EFFECTS.pas',

  G2D_CAMERA      in 'G2D_CAMERA.pas',

  G2D_FX          in 'G2D_FX.pas',
  FX_BLUR         in 'FX_BLUR.pas',

  G2D_PRIMITIVES  in 'G2D_PRIMITIVES.pas',
  
  G2D_TEXT         in 'G2D_TEXT.pas',

  G2D_SSPRITE     in 'G2D_SSPRITE.pas',
  G2D_ASPRITE     in 'G2D_ASPRITE.pas',
  G2D_CSPRITE     in 'G2D_CSPRITE.pas',
  G2D_STILE       in 'G2D_STILE.pas',
  G2D_ATILE       in 'G2D_ATILE.pas',

  SOUND_WAVE      in 'SOUND_WAVE.pas',
  MEDIA_DSHOW     in 'MEDIA_DSHOW.pas',

  COLLISION_MAIN  in 'COLLISION_MAIN.pas',

  MATH_FAST       in 'MATH_FAST.pas',

  PLUGIN_MAIN     in 'PLUGIN_MAIN.pas',
  UTILS           in 'UTILS.pas'
  ;

const
  CopyleftText = '***OMEGA ENGINE*** '         +
                 '©2006-2007 '                 +
                 'Andru ''aka'' KEMKA ANDREY. '+
                 'HTTP://andru.2x4.ru';
function Copyleft2006 : PChar;
Begin

Result := PChar( CopyleftText );

End;

procedure OE_Quit; stdcall;
Begin

app_Work := FALSE;

End;

procedure OE_AnalyzeHardware;
Begin
if app_Log Then
  begin
    LOG_AddMessage( LOGT_ENGINE, PChar( LOG_CPU + U_GetCPUName ) );
    LOG_AddMessage( LOGT_ENGINE, PChar( LOG_TOTAL_RAM + U_IntToStr( U_GetTotalRAM ) + ' Mb' ) );
  end;
End;

procedure OE_Init( zBits, stencilBits : Byte ); stdcall;
Begin

ogl_ZBits   := zBits;
ogl_Stencil := stencilBits;
if app_Log Then
  begin
    LOG_Init( 'Log.htm', TRUE );

    if app_FullScreen Then
      LOG_AddMessage( LOGT_ENGINE,
                      PChar( LOG_OMEGA_START +
                             {$IFDEF RUS} ' в полноэкранном режиме: '
                             {$ELSE}      ' in fullscreen mode: '
                             {$ENDIF}
                             + U_IntToStr( scr_Width  ) + ' x '
                             + U_IntToStr( scr_Height ) + ' x '
                             + U_IntToStr( scr_BPP ) ) )
    else
      LOG_AddMessage( LOGT_ENGINE,
                      PChar( LOG_OMEGA_START +
                             {$IFDEF RUS} ' в оконном режиме: '
                             {$ELSE}      ' in windowed mode: '
                             {$ENDIF}
                             + U_IntToStr( wnd_Width  ) + ' x '
                             + U_IntToStr( wnd_Height ) + ' x '
                             + U_IntToStr( scr_BPP ) ) );
  end;

OE_AnalyzeHardware;

app_InitType := 0;

if ( WinBoot = 1 ) or ( WinBoot = 2 ) Then
  begin
    E_Error( ERR_SAFE_BOOT );
    app_Work := FALSE;
    Exit;
  end;

app_Work := TRUE;

if not wnd_Create Then Exit;
ShowCursor( app_Cursor );
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_CREATING_WINDOW );

if not GL_SetPixelFormat Then Exit;
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_SET_PIXEL_FORMAT );
if not GL_CreateContext  Then Exit;
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_INIT_OPENGL );

GL_Init;
GL_Set2DMatrix;
Wnd_Resize;

OE_SetVSync( scr_VSync );

eng_Main(
          wnd_INST{hInstance},
          0       {hPrevInst},
          ''      {CmdLine},
          0       {CmdShow}
        );

End;

function OE_Test( zBits, stencilBits : Byte ) : Boolean; stdcall;
Begin
OE_Disable( FLAG_SHOW_WARNINGS );
OE_Disable( FLAG_SHOW_ERRORS );
Result := FALSE;

ogl_ZBits   := zBits;
ogl_Stencil := stencilBits;
if app_Log Then
  begin
    LOG_Init( 'Log.htm', TRUE );

    if app_FullScreen Then
      LOG_AddMessage( LOGT_ENGINE,
                      PChar( LOG_OMEGA_START +
                             {$IFDEF RUS} ' в полноэкранном режиме: '
                             {$ELSE}      ' in fullscreen mode: '
                             {$ENDIF}
                             + U_IntToStr( scr_Width  ) + ' x '
                             + U_IntToStr( scr_Height ) + ' x '
                             + U_IntToStr( scr_BPP ) ) )
    else
      LOG_AddMessage( LOGT_ENGINE,
                      PChar( LOG_OMEGA_START +
                             {$IFDEF RUS} ' в оконном режиме: '
                             {$ELSE}      ' in windowed mode: '
                             {$ENDIF}
                             + U_IntToStr( wnd_Width  ) + ' x '
                             + U_IntToStr( wnd_Height ) + ' x '
                             + U_IntToStr( scr_BPP ) ) );
  end;

OE_AnalyzeHardware;

app_InitType := 0;

if ( WinBoot = 1 ) or ( WinBoot = 2 ) Then
  begin
    E_Error( ERR_SAFE_BOOT );
    app_Work := FALSE;
    Exit;
  end;

app_Work := TRUE;

if not wnd_Create Then Exit;
ShowCursor( app_Cursor );
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_CREATING_WINDOW );

if not GL_SetPixelFormat Then Exit;
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_SET_PIXEL_FORMAT );
if not GL_CreateContext Then Exit;
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_INIT_OPENGL );

if GL_Init Then exit;
GL_Set2DMatrix;
Wnd_Resize;

OE_SetVSync( scr_VSync );

wnd_Destroy;

if ( ogl_Version < 1.2 )     or
   ( not ogl_3DAccelerator ) or
   ( glGetString( GL_VENDOR ) = 'Microsoft Corporation' ) Then Exit;

Result := TRUE;
OE_Enable( FLAG_SHOW_WARNINGS );
OE_Enable( FLAG_SHOW_ERRORS );
End;

procedure OE_Init2( HWND : DWORD; zBits, stencilBits : Byte ); stdcall;
  var
    WndI : TWindowInfo;
Begin

ogl_ZBits   := zBits;
ogl_Stencil := stencilBits;

if app_Log Then
  begin
    LOG_Init( 'Log.htm', TRUE );

    LOG_AddMessage( LOGT_ENGINE,
                    PChar( LOG_OMEGA_START +
                           {$IFDEF RUS} ' в оконном режиме'
                           {$ELSE}      ' in windowed mode'
                           {$ENDIF} ) );
  end;

OE_AnalyzeHardware;

app_InitType := 1;
Key_LoadLayout( K_LAYOUT_ENG );

if ( WinBoot = 1 ) or ( WinBoot = 2 ) Then
  begin
    E_Error( ERR_SAFE_BOOT );
    app_Work := FALSE;
    Exit;
  end;

wnd_HWND := HWND;
wnd_DC   := GetDC( HWND );

GetWindowInfo( wnd_HWND, WndI );
wnd_Width  := WndI.rcClient.Right - WndI.rcClient.Left;
wnd_Height := WndI.rcClient.Bottom - WndI.rcClient.Top;
OE_ResizeWindow( wnd_Width, wnd_Height );
ShowCursor( app_Cursor );

if not GL_SetPixelFormat Then Exit;
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_SET_PIXEL_FORMAT );
if not GL_CreateContext Then Exit;
if app_Log Then
  LOG_AddMessage( LOGT_ENGINE, LOG_INIT_OPENGL );

GL_Init;
scr_BPP := GetDisplayColors;

GL_Set2DMatrix;
Wnd_Resize;

app_Work  := TRUE;

SetFocus( wnd_HWND );

eng_Main(
          wnd_INST{hInstance},
          0       {hPrevInst},
          ''      {CmdLine},
          0       {CmdShow}
        );

End;

{---------------------- Экспортируемые функции/процедуры ----------------------}
exports
// COPYRIGHTS
  Copyleft2006 name CopyleftText,

// PLUGIN
  P_GetParams,
  Tex_PostProcess,

// INIT
  OE_Test,
  OE_Init,
  OE_Init2,
  OE_Quit,

// MAIN
  OE_RegProcedure,
  OE_UnRegProcedure,
  Timer_Create,
  Timer_Free,
  Timer_FreeAll,
  // OPTIONS
  OE_Enable,
  OE_Disable,
  OE_SetWndOnTop,
  OE_SetWndPosition,
  OE_SetWndName,
  OE_ShowCursor,
  OE_SetScreenOptions,
  OE_ChangeScreenOptions,
  OE_SetResolutionDependence,
  OE_SetAdvancedOptions,
  OE_GetOptions,
  OE_SetVSync,

  OE_ResizeWindow,
  // ADVANCED
  OE_GetFPS,

// INI
  INI_LoadFromFile,
  INI_LoadFromMemory,
  INI_SaveToFile,
  INI_Add,
  INI_ReadKeyStr,
  INI_ReadKeyInt,
  INI_WriteKeyStr,
  INI_WriteKeyInt,
//  INI_ReadKeyFloat,

// LOG
  LOG_SetOptions,
  LOG_Init,
  LOG_AddMessage,
  LOG_AddLine,

// CONSOLE
  Console_Active,
  Console_SetOptions,
  Console_ToLog,
  Console_Draw,
  Console_RegCommand,
  Console_RegVariable,
  Console_RegProcedure,
  Console_AddString,

// INPUT
  // Keyboard
  KeyEx_Down,

  Key_Down,
  Key_Press,
  Key_LastDown,
  Key_LastPress,
  Key_LoadLayout,
  Key_BeginReadText,
  Key_EndReadText,
  Key_MaxReadText,
  // Mouse
  MouseEx_X,
  MouseEx_Y,
  MouseEx_Down,

  Mouse_X,
  Mouse_Y,
  Mouse_Move,
  Mouse_Down,
  Mouse_Press,
  Mouse_Click,
  Mouse_DblClick,
  Mouse_WheelUp,
  Mouse_WheelDown,
  Mouse_Lock,
  Mouse_DeltaX,
  Mouse_DeltaY,
  // Joy
  Joy_Down,
  Joy_Press,
  Joy_AxePos,

// TEXTURES
  Tex_BeginRenderIn,
  Tex_EndRenderIn,
  Tex_CreateZero,

  Tex_Filter,
  Tex_SetAnisotropyLevel,
  Tex_LoadFromFile,
  Tex_LoadFromMemory,
  Tex_Free,
  Tex_FreeAll,
  Tex_LoadMaskFromFile,
  Tex_LoadMaskFromMemory,
  Tex_FreeMask,
  Tex_SetFramesSize,

// CAMERA 2D
  Cam2D_Create,
  Cam2D_Free,
  Cam2D_FreeAll,
  Cam2D_Set,

// FX
  FX_SetColorMix,
  FX_SetBlendMode,
  FX_SetScale,
  FX_SetVertexes,
  FX_SetVCA,
  // BLUR
  FX_Blur_SetTex,
  FX_Blur_SetParams,
  FX_Blur_Calc,
  FX_Blur_Draw,
  FX_RadBlur_SetTex,
  FX_RadBlur_SetParams,
  FX_RadBlur_Calc,
  FX_RadBlur_Draw,

// TEXT
  Text_LoadFontFromFile,
  Text_LoadFontFromMemory,
  Text_FreeFont,
  Text_FreeFontAll,
  Text_Draw,
  Text_GetWidth,

// PRIMITIVES
  Pr2D_PixelSize,
  Pr2D_LineWidth,

  Pr2D_Pixel,
  Pr2D_Line,
  Pr2D_TLine,
  Pr2D_Rect,
  Pr2D_TRect,
  Pr2D_Triangle,
  Pr2D_TTriangle,
  Pr2D_Circle,
  Pr2D_TCircle,
  Pr2D_Ellipse,
  Pr2D_Polygon,
  Pr2D_PolygonCA,

// SSPRITE
  SSprite_Draw,
// ASPRITE
  ASprite_Draw,
// CSPRITE
  CSprite_Draw,
// STILE
  STile_Draw,
// ATILE
  ATile_Draw,

// SCISSOR
  Scissor_Begin,
  Scissor_End,

// SOUND
  Snd_Init,
  Snd_Active,
  Snd_LoadFromFile,
  Snd_LoadFromMemory,
  Snd_Free,
  Snd_FreeAll,
  Snd_SetBuffersCount,
  Snd_Play,
  Snd_PlayID,
  Snd_Stop,
  Snd_StopID,
  Snd_StopAll,
  Snd_ChangeParam,

// MEDIA
  Media_Play,
  Media_Replay,
  Media_Pause,
  Media_Resume,
  Media_Stop,
  Media_SetVolume,
  Media_GetLength,
  Media_GetPosition,
  Media_SetPosition,
  Media_SetWindowRect,

// COLLISION
  // Point's 2D
  Col_PointInAABB,
  Col_PointInCircle,
  Col_PointInPolyLine,
  // Line's 2D
  Col_Line,
  Col_LineVsAABB,
  Col_LineVsCircle,
  Col_LineVsCircleXY,
  Col_LineVsPolyLine,
  // PolyLine's 2D
  Col_PolyLine,
  Col_PolyLineVsAABB,
  Col_PolyLineVsCircle,
  Col_PolyLineVsCircleXY,
  // Aabb's 2D
  Col_AABB,
  Col_AABBInAABB,
  Col_AABBInCircle,
  Col_AABBVsCircle,
  // Circle's 2D
  Col_Circle,
  Col_CircleInCircle,
  Col_CircleInAABB,
// EXTENDED COLLISION
  // Get's
  ColEx_LastX,
  ColEx_LastY,
  ColEx_LastLineA,
  ColEx_LastLineB,
  // Transformation's
  ColEx_PolyRotate,
  ColEx_PolyScale,
  ColEx_PolyMove,
  ColEx_PolyCenter,
  ColEx_PolyRect,
  // Line's 2D
  ColEx_CalcLineCross,

// MATH
  M_Cos,
  M_Sin,
  M_CosF,
  M_SinF,

  M_Round,
  M_Trunc,

  M_Distance,
  M_FindAngle,

// MEM
  Mem_Get,
  Mem_Realloc,
  Mem_Free,

// UTILS
  U_SnapShot,

  U_GetCPUName,
  U_GetTotalRAM,
  U_Is3DAccelerator,
  U_GetMaxTexSize,
  U_GetMaxAnisotropy,

  U_IntToStr,
  U_StrToInt,
  U_FloatToStr,

  U_GetTime
  ;

var
  FL, FP : PChar;
  S      : String;
begin
wnd_INST := GetModuleHandle( nil );
GetMem( FL, 65535 );
GetMem( FP, 65535 );
GetModuleFileName( wnd_INST, FL, 65535 );
GetFullPathName( FL, 65535, FP, FL );
S := copy( String( FP ), 1, length( FP ) - length( FL ) );
app_Dir := PChar( S );
FL := nil;
FP := nil;

with scr_OLDSettings do
  begin
    dmSize             := SizeOf( DEVMODE );
    dmPelsWidth        := GetSystemMetrics( SM_CXSCREEN );
    dmPelsHeight       := GetSystemMetrics( SM_CYSCREEN );
    dmBitsPerPel       := GetDisplayColors;
    dmDisplayFrequency := GetDisplayRefresh;
    dmFields           := DM_PELSWIDTH or DM_PELSHEIGHT or DM_BITSPERPEL or DM_DISPLAYFREQUENCY;
  end;
end.
