hedgewars/hwengine.pas
author nemo
Mon, 08 Aug 2011 23:57:04 -0400
changeset 5528 c539e5c81870
parent 5521 3be9fda5c3c2
child 5530 25d4118056e1
child 5562 8056d82b4d23
permissions -rw-r--r--
slight visual tweak. birdy's wings beat faster if he's just about tired out
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     1
(*
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     2
* Hedgewars, a free turn based strategy game
4976
088d40d8aba2 Happy 2011 :)
koda
parents: 4939
diff changeset
     3
* Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com>
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     4
*
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     5
* This program is free software; you can redistribute it and/or modify
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     6
* it under the terms of the GNU General Public License as published by
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     7
* the Free Software Foundation; version 2 of the License
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     8
*
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
     9
* This program is distributed in the hope that it will be useful,
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    12
* GNU General Public License for more details.
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    13
*
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    14
* You should have received a copy of the GNU General Public License
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    15
* along with this program; if not, write to the Free Software
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    17
*)
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
    18
2599
c7153d2348f3 move compiler directives to standard pascal
koda
parents: 2591
diff changeset
    19
{$INCLUDE "options.inc"}
2587
0dfa56a8513c fix a segfault in the iphone simulator by moving options.inc at the beginning of the file
koda
parents: 2586
diff changeset
    20
2800
f286d91157b8 Engine:
smxx
parents: 2799
diff changeset
    21
{$IFDEF WIN32}
f286d91157b8 Engine:
smxx
parents: 2799
diff changeset
    22
{$R hwengine.rc}
f286d91157b8 Engine:
smxx
parents: 2799
diff changeset
    23
{$ENDIF}
f286d91157b8 Engine:
smxx
parents: 2799
diff changeset
    24
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2800
diff changeset
    25
{$IFDEF HWLIBRARY}
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    26
unit hwengine;
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    27
interface
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    28
{$ELSE}
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
    29
program hwengine;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    30
{$ENDIF}
3407
dcc129c4352e Engine:
smxx
parents: 3387
diff changeset
    31
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
    32
uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uKeys, uSound,
5174
f5294509783e initial refactoring of ObjcExports and OverlayViewController
koda
parents: 5167
diff changeset
    33
     uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uRandom, uLandTexture, uCollisions,
4490
f6840f7e2f60 Stub to recieve /draw command
unc0rr
parents: 4454
diff changeset
    34
     sysutils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
    35
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2800
diff changeset
    36
{$IFDEF HWLIBRARY}
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
    37
procedure initEverything(complete:boolean);
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
    38
procedure freeEverything(complete:boolean);
4929
3dca560e6510 I need this export in order to not have the wrapper.c fail to find Game() on linux
sheepluva
parents: 4901
diff changeset
    39
procedure Game(gameArgs: PPChar); cdecl; export;
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
    40
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    41
implementation
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    42
{$ELSE}
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
    43
procedure OnDestroy; forward;
3611
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
    44
procedure initEverything(complete:boolean); forward;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
    45
procedure freeEverything(complete:boolean); forward;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
    46
{$ENDIF}
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
    47
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
    48
////////////////////////////////
371
731ad6d27bd1 integer -> LongInt
unc0rr
parents: 357
diff changeset
    49
procedure DoTimer(Lag: LongInt);
2905
f3c79f7193a9 Engine:
smxx
parents: 2825
diff changeset
    50
var s: shortstring;
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
    51
begin
4454
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
    52
    if isPaused = false then
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
    53
        inc(RealTicks, Lag);
564
17fb45c670c1 Finalize basic playlists implementation
unc0rr
parents: 529
diff changeset
    54
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    55
    case GameState of
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    56
        gsLandGen: begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    57
                GenMap;
4389
d1c65b60cd68 Move land hash checking into commands
unc0rr
parents: 4376
diff changeset
    58
                ParseCommand('sendlanddigest', true);
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    59
                GameState:= gsStart;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    60
                end;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    61
        gsStart: begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    62
                if HasBorder then DisableSomeWeapons;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    63
                AddClouds;
4806
48c1a395f0a7 added flake configuration also in sudden death and SDClouds for underwater
Henek
parents: 4490
diff changeset
    64
                AddFlakes;
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    65
                AssignHHCoords;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    66
                AddMiscGears;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    67
                StoreLoad;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    68
                InitWorld;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    69
                ResetKbd;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    70
                SoundLoad;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    71
                if GameType = gmtSave then
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    72
                    begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    73
                    isSEBackup:= isSoundEnabled;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    74
                    isSoundEnabled:= false
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    75
                    end;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    76
                FinishProgress;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    77
                PlayMusic;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    78
                SetScale(zoom);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    79
                ScriptCall('onGameStart');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    80
                GameState:= gsGame;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    81
                end;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    82
        gsConfirm,
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    83
        gsGame: begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    84
                DrawWorld(Lag); // never place between ProcessKbd and DoGameTick - bugs due to /put cmd and isCursorVisible
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    85
                ProcessKbd;
3449
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    86
                if not isPaused then
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    87
                    begin
3799
5341dc5a8919 Engine:
smaxx
parents: 3779
diff changeset
    88
                    DoGameTick(Lag);
3449
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    89
                    ProcessVisualGears(Lag);
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    90
                    end;
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    91
                end;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    92
        gsChat: begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    93
                DrawWorld(Lag);
3449
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    94
                if not isPaused then
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    95
                    begin
3843
50e75cb38c18 Remove ready timer halting game ticks in chat
nemo
parents: 3803
diff changeset
    96
                    DoGameTick(Lag);
3449
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    97
                    ProcessVisualGears(Lag);
033e4a8a9c74 wait for AI thread to finish before freeing ressources (to avoid segfaults on game exit)
sheepluva
parents: 3444
diff changeset
    98
                    end;
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
    99
                end;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   100
        gsExit: begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   101
                isTerminated:= true;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   102
                end;
4454
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
   103
        gsSuspend: exit;
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   104
        end;
564
17fb45c670c1 Finalize basic playlists implementation
unc0rr
parents: 529
diff changeset
   105
2716
b9ca1bfca24f complete the replacement of init/free wrappers for every unit
koda
parents: 2715
diff changeset
   106
{$IFDEF SDL13}
5505
a55aab592950 Ditch the renderer system in sdl1.3 and use the 'old fashioned' sdl/opengl context. This gives us more flexibility and less problem in receiving video events (expecially on mobile platform) as well as not having to care to reset the gl context every time sdl interferes.
koda
parents: 5242
diff changeset
   107
    SDL_GL_SwapWindow(SDLwindow);
3523
6592fbb969da fix zoom smoothness
koda
parents: 3495
diff changeset
   108
{$ELSE}
6592fbb969da fix zoom smoothness
koda
parents: 3495
diff changeset
   109
    SDL_GL_SwapBuffers();
2716
b9ca1bfca24f complete the replacement of init/free wrappers for every unit
koda
parents: 2715
diff changeset
   110
{$ENDIF}
4454
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
   111
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   112
    if flagMakeCapture then
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   113
    begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   114
        flagMakeCapture:= false;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   115
        s:= 'hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   116
        WriteLnToConsole('Saving ' + s + '...');
4359
83ef50815535 Move variables from uMisc to uVariables
unC0Rr
parents: 4357
diff changeset
   117
        playSound(sndShutter);
4939
6d512ba87f72 screenshot binding for library (on ios it just plays the sound)
koda
parents: 4935
diff changeset
   118
        {$IFNDEF IPHONEOS}MakeScreenshot(s);{$ENDIF}
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   119
    end;
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   120
end;
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   121
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   122
////////////////////
79
29b477319854 - New test map
unc0rr
parents: 74
diff changeset
   123
procedure OnDestroy;
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   124
begin
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   125
    WriteLnToConsole('Freeing resources...');
3617
1df21e06b8ba a couple of fixes i missed previously
koda
parents: 3615
diff changeset
   126
    FreeActionsList();
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   127
    StoreRelease();
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   128
    ControllerClose();
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   129
    CloseIPC();
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   130
    TTF_Quit();
3598
a8aa06bae895 tiy new overlay graphics
koda
parents: 3594
diff changeset
   131
{$IFDEF SDL13}
5505
a55aab592950 Ditch the renderer system in sdl1.3 and use the 'old fashioned' sdl/opengl context. This gives us more flexibility and less problem in receiving video events (expecially on mobile platform) as well as not having to care to reset the gl context every time sdl interferes.
koda
parents: 5242
diff changeset
   132
    SDL_GL_DeleteContext(SDLGLcontext);
3598
a8aa06bae895 tiy new overlay graphics
koda
parents: 3594
diff changeset
   133
    SDL_DestroyWindow(SDLwindow);
5505
a55aab592950 Ditch the renderer system in sdl1.3 and use the 'old fashioned' sdl/opengl context. This gives us more flexibility and less problem in receiving video events (expecially on mobile platform) as well as not having to care to reset the gl context every time sdl interferes.
koda
parents: 5242
diff changeset
   134
    SDLGLcontext:= nil;
5166
d1eb1560b4d5 and now for something completely different, overlay refactoring!
koda
parents: 5130
diff changeset
   135
    SDLwindow:= nil;
3598
a8aa06bae895 tiy new overlay graphics
koda
parents: 3594
diff changeset
   136
{$ENDIF}
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   137
    SDL_Quit();
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   138
    isTerminated:= false;
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   139
end;
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   140
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   141
///////////////////
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
   142
procedure MainLoop;
5217
5612e01252ce Don't allocate SDL_event structure on stack
unc0rr
parents: 5174
diff changeset
   143
{$WARNINGS OFF}
5612e01252ce Don't allocate SDL_event structure on stack
unc0rr
parents: 5174
diff changeset
   144
// disable "Some fields weren't initialized" warning
5612e01252ce Don't allocate SDL_event structure on stack
unc0rr
parents: 5174
diff changeset
   145
const event: TSDL_Event = ();
5612e01252ce Don't allocate SDL_event structure on stack
unc0rr
parents: 5174
diff changeset
   146
{$WARNINGS ON}
2716
b9ca1bfca24f complete the replacement of init/free wrappers for every unit
koda
parents: 2715
diff changeset
   147
var PrevTime, CurrTime: Longword;
5521
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   148
    prevFocusState: boolean;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   149
begin
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   150
    PrevTime:= SDL_GetTicks;
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   151
    while isTerminated = false do
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   152
    begin
3523
6592fbb969da fix zoom smoothness
koda
parents: 3495
diff changeset
   153
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   154
        while SDL_PollEvent(@event) <> 0 do
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   155
        begin
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   156
            case event.type_ of
3647
0d0df215fb52 making chat work... (keyboard support is heavily broken in sdl upstream)
koda
parents: 3634
diff changeset
   157
                SDL_KEYDOWN: if GameState = gsChat then
4454
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
   158
{$IFDEF SDL13}
3647
0d0df215fb52 making chat work... (keyboard support is heavily broken in sdl upstream)
koda
parents: 3634
diff changeset
   159
                    // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
0d0df215fb52 making chat work... (keyboard support is heavily broken in sdl upstream)
koda
parents: 3634
diff changeset
   160
                    KeyPressChat(event.key.keysym.sym);
4454
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
   161
                SDL_WINDOWEVENT:
5052
74a81c276d67 fix a couple of loose ends
koda
parents: 4976
diff changeset
   162
                    if event.window.event = SDL_WINDOWEVENT_SHOWN then
5521
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   163
                        begin
4454
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
   164
                        cHasFocus:= true;
5521
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   165
                        onFocusStateChanged()
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   166
                        end;
3647
0d0df215fb52 making chat work... (keyboard support is heavily broken in sdl upstream)
koda
parents: 3634
diff changeset
   167
{$ELSE}
0d0df215fb52 making chat work... (keyboard support is heavily broken in sdl upstream)
koda
parents: 3634
diff changeset
   168
                    KeyPressChat(event.key.keysym.unicode);
4744
ecc2c757d0df general uKey refactor in preparaiton of two new shortcuts
koda
parents: 4490
diff changeset
   169
                SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then wheelDown:= true;
ecc2c757d0df general uKey refactor in preparaiton of two new shortcuts
koda
parents: 4490
diff changeset
   170
                SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then wheelUp:= true;
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   171
                SDL_ACTIVEEVENT:
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   172
                    if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
5521
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   173
                        begin
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   174
                        prevFocusState:= cHasFocus;
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   175
                        cHasFocus:= event.active.gain = 1;
5521
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   176
                        if prevFocusState xor cHasFocus then
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   177
                            onFocusStateChanged()
3be9fda5c3c2 Enter gsConfirm state when loosing focus
unc0rr
parents: 5505
diff changeset
   178
                        end;
3463
23c50be687a9 update sdl functions to latest revision
koda
parents: 3449
diff changeset
   179
{$ENDIF}
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   180
                SDL_JOYAXISMOTION: ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   181
                SDL_JOYHATMOTION: ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   182
                SDL_JOYBUTTONDOWN: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, true);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   183
                SDL_JOYBUTTONUP: ControllerButtonEvent(event.jbutton.which, event.jbutton.button, false);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   184
                SDL_QUITEV: isTerminated:= true
4454
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
   185
            end; //end case event.type_ of
42bfc1a70968 more retina support and multitasking support
koda
parents: 4437
diff changeset
   186
        end; //end while SDL_PollEvent(@event) <> 0 do
3463
23c50be687a9 update sdl functions to latest revision
koda
parents: 3449
diff changeset
   187
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   188
        if isTerminated = false then
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   189
        begin
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   190
            CurrTime:= SDL_GetTicks;
3976
abaf741a4e21 less warning, no rotation glitch, more comments
koda
parents: 3971
diff changeset
   191
            if PrevTime + longword(cTimerInterval) <= CurrTime then
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   192
            begin
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   193
                DoTimer(CurrTime - PrevTime);
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   194
                PrevTime:= CurrTime
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
   195
            end
3063
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   196
            else SDL_Delay(1);
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   197
            IPCCheckSock();
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   198
        end;
0092dc37fbd6 reworked interaction of engine/frontend when closing
koda
parents: 3058
diff changeset
   199
    end;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   200
end;
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   201
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   202
/////////////////////////
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   203
procedure ShowMainWindow;
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   204
begin
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   205
    if cFullScreen then ParseCommand('fullscr 1', true)
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   206
    else ParseCommand('fullscr 0', true);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   207
    SDL_ShowCursor(0)
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   208
end;
2591
c6597b65caea other controls implementation + sdlh revisited (once again)
koda
parents: 2590
diff changeset
   209
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   210
///////////////
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2800
diff changeset
   211
{$IFDEF HWLIBRARY}
3971
5c82ee165ed5 minor stuff
koda
parents: 3935
diff changeset
   212
procedure Game(gameArgs: PPChar); cdecl; export;
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   213
{$ELSE}
3064
7458ec3666c3 testing ftw
koda
parents: 3063
diff changeset
   214
procedure Game;
2702
48fc46a922fd rewrite gamesetup to use less memory, rename effects to sound plus some nice actions, settings work in game again
koda
parents: 2699
diff changeset
   215
{$ENDIF}
2948
3f21a9dc93d0 Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents: 2947
diff changeset
   216
var p: TPathType;
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   217
    s: shortstring;
3611
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   218
    i: LongInt;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   219
begin
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2800
diff changeset
   220
{$IFDEF HWLIBRARY}
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   221
    cBits:= 32;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   222
    cFullScreen:= false;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   223
    cTimerInterval:= 8;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   224
    PathPrefix:= 'Data';
5239
f34f391a223b Bit more reusability
nemo
parents: 5238
diff changeset
   225
    UserPathPrefix:= '.';
5109
6d2e8a24277e strangely enough, the new sdl rotation code is incompatible with our system... this is a workaround that should hold up until their code becomes more stable
koda
parents: 5066
diff changeset
   226
    cShowFPS:= {$IFDEF DEBUGFILE}true{$ELSE}false{$ENDIF};
3779
3351a017d4ad tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents: 3774
diff changeset
   227
    val(gameArgs[0], ipcPort);
3922
44804043b691 iPad Video Out support (+less warnings +code update for latest SDL)
koda
parents: 3914
diff changeset
   228
    val(gameArgs[1], cScreenWidth);
44804043b691 iPad Video Out support (+less warnings +code update for latest SDL)
koda
parents: 3914
diff changeset
   229
    val(gameArgs[2], cScreenHeight);
3779
3351a017d4ad tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents: 3774
diff changeset
   230
    val(gameArgs[3], cReducedQuality);
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   231
    cLocaleFName:= gameArgs[4];
3779
3351a017d4ad tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents: 3774
diff changeset
   232
    UserNick:= gameArgs[5];
3351a017d4ad tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents: 3774
diff changeset
   233
    isSoundEnabled:= gameArgs[6] = '1';
3351a017d4ad tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents: 3774
diff changeset
   234
    isMusicEnabled:= gameArgs[7] = '1';
3351a017d4ad tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents: 3774
diff changeset
   235
    cAltDamage:= gameArgs[8] = '1';
3922
44804043b691 iPad Video Out support (+less warnings +code update for latest SDL)
koda
parents: 3914
diff changeset
   236
    val(gameArgs[9], rotationQt);
3779
3351a017d4ad tap to dismiss 'get ready', add a toggle to enable/disable it
koda
parents: 3774
diff changeset
   237
    recordFileName:= gameArgs[10];
4814
e19791f08443 smaller rearrangement of (non stereo related) variables
koda
parents: 4812
diff changeset
   238
    cStereoMode:= smNone;
3624
304c6d32383a button to open ammomenu
koda
parents: 3617
diff changeset
   239
{$ENDIF}
3613
cfd3c6536a09 make runtime quality check for iDevices
koda
parents: 3612
diff changeset
   240
3914
c5c903c6225b Engine:
smaxx
parents: 3904
diff changeset
   241
    cLogfileBase:= 'game';
3611
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   242
    initEverything(true);
3691
34fe2149f75d Engine:
smaxx
parents: 3670
diff changeset
   243
3611
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   244
    WriteLnToConsole('Hedgewars ' + cVersionString + ' engine (network protocol: ' + inttostr(cNetProtoVersion) + ')');
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   245
    AddFileLog('Prefix: "' + PathPrefix +'"');
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   246
    AddFileLog('UserPrefix: "' + UserPathPrefix +'"');
3611
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   247
    for i:= 0 to ParamCount do
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   248
        AddFileLog(inttostr(i) + ': ' + ParamStr(i));
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   249
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   250
    for p:= Succ(Low(TPathType)) to High(TPathType) do
5242
e4f6aa3250eb corrected user ptData path
nemo
parents: 5239
diff changeset
   251
        if (p <> ptMapCurrent) and (p <> ptData) then UserPathz[p]:= UserPathPrefix + '/Data/' + Pathz[p];
e4f6aa3250eb corrected user ptData path
nemo
parents: 5239
diff changeset
   252
e4f6aa3250eb corrected user ptData path
nemo
parents: 5239
diff changeset
   253
    UserPathz[ptData]:= UserPathPrefix + '/Data';
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   254
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   255
    for p:= Succ(Low(TPathType)) to High(TPathType) do
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   256
        if p <> ptMapCurrent then Pathz[p]:= PathPrefix + '/' + Pathz[p];
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
   257
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   258
    WriteToConsole('Init SDL... ');
4773
69f8431a5d20 yay, sdl resumed development and accepted some of our patches in mainstream
koda
parents: 4744
diff changeset
   259
    SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, true);
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   260
    WriteLnToConsole(msgOK);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   261
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   262
    SDL_EnableUNICODE(1);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   263
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   264
    WriteToConsole('Init SDL_ttf... ');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   265
    SDLTry(TTF_Init() <> -1, true);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   266
    WriteLnToConsole(msgOK);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   267
3162
663e0e55e5c7 Engine:
smxx
parents: 3154
diff changeset
   268
{$IFDEF WIN32}
663e0e55e5c7 Engine:
smxx
parents: 3154
diff changeset
   269
    s:= SDL_getenv('SDL_VIDEO_CENTERED');
3153
adfe8a91658f Engine:
smxx
parents: 3078
diff changeset
   270
    SDL_putenv('SDL_VIDEO_CENTERED=1');
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   271
    ShowMainWindow();
3162
663e0e55e5c7 Engine:
smxx
parents: 3154
diff changeset
   272
    SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));
663e0e55e5c7 Engine:
smxx
parents: 3154
diff changeset
   273
{$ELSE}
3154
32e572debb9c Engine:
smxx
parents: 3153
diff changeset
   274
    ShowMainWindow();
3162
663e0e55e5c7 Engine:
smxx
parents: 3154
diff changeset
   275
{$ENDIF}
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   276
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   277
    ControllerInit(); // has to happen before InitKbdKeyTable to map keys
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   278
    InitKbdKeyTable();
5174
f5294509783e initial refactoring of ObjcExports and OverlayViewController
koda
parents: 5167
diff changeset
   279
    AddProgress();
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   280
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   281
    LoadLocale(UserPathz[ptLocale] + '/en.txt');  // Do an initial load with english
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   282
    LoadLocale(Pathz[ptLocale] + '/en.txt');  // Do an initial load with english
5110
d48230ef9447 set cLocale even for en
nemo
parents: 5107
diff changeset
   283
    if (Length(cLocaleFName) > 6) then cLocale := Copy(cLocaleFName,1,5)
d48230ef9447 set cLocale even for en
nemo
parents: 5107
diff changeset
   284
    else cLocale := Copy(cLocaleFName,1,2);
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   285
    if cLocaleFName <> 'en.txt' then
4046
cfdbddc4b385 making indentation consistent (noticed while debugging)
nemo
parents: 3976
diff changeset
   286
        begin
2719
0efcaa69d30c Tweak load order
nemo
parents: 2717
diff changeset
   287
        // Try two letter locale first before trying specific locale overrides
5110
d48230ef9447 set cLocale even for en
nemo
parents: 5107
diff changeset
   288
        if (Length(cLocale) > 2) and (Copy(cLocale,1,2) <> 'en') then
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   289
            begin
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   290
            LoadLocale(UserPathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt');
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   291
            LoadLocale(Pathz[ptLocale] + '/' + Copy(cLocale,1,2)+'.txt')
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   292
            end;
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   293
        LoadLocale(UserPathz[ptLocale] + '/' + cLocaleFName);
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   294
        LoadLocale(Pathz[ptLocale] + '/' + cLocaleFName)
5107
d7fc678d78f4 Allow override of voicepack with a locale voicepack. Untested, but should mean that a Default voice should use the Ukranian version if that is their locale.
nemo
parents: 5066
diff changeset
   295
        end
d7fc678d78f4 Allow override of voicepack with a locale voicepack. Untested, but should mean that a Default voice should use the Ukranian version if that is their locale.
nemo
parents: 5066
diff changeset
   296
    else cLocale := 'en';
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   297
3904
22e4d74240e5 finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents: 3843
diff changeset
   298
    WriteLnToConsole(msgGettingConfig);
4046
cfdbddc4b385 making indentation consistent (noticed while debugging)
nemo
parents: 3976
diff changeset
   299
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   300
    if recordFileName = '' then
4046
cfdbddc4b385 making indentation consistent (noticed while debugging)
nemo
parents: 3976
diff changeset
   301
        begin
3904
22e4d74240e5 finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents: 3843
diff changeset
   302
        InitIPC;
22e4d74240e5 finishing touches to save games handling (help label, dim on overlay, edit text only when table is editable)
koda
parents: 3843
diff changeset
   303
        SendIPCAndWaitReply('C');        // ask for game config
4046
cfdbddc4b385 making indentation consistent (noticed while debugging)
nemo
parents: 3976
diff changeset
   304
        end
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   305
    else
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   306
        LoadRecordFromFile(recordFileName);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   307
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   308
    ScriptOnGameInit;
2786
85f6425a4d74 Engine:
smxx
parents: 2747
diff changeset
   309
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   310
    s:= 'eproto ' + inttostr(cNetProtoVersion);
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   311
    SendIPCRaw(@s[0], Length(s) + 1); // send proto version
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   312
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   313
    InitTeams();
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   314
    AssignStores();
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   315
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   316
    if isSoundEnabled then
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   317
        InitSound();
2590
e7e87e3c67db touch control completely revamped
koda
parents: 2587
diff changeset
   318
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   319
    isDeveloperMode:= false;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   320
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   321
    TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   322
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   323
    ParseCommand('rotmask', true);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   324
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   325
    MainLoop();
3615
b78d7959540a further code cleanup and less redundancy
koda
parents: 3613
diff changeset
   326
    // clean up SDL and GL context
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   327
    OnDestroy();
3615
b78d7959540a further code cleanup and less redundancy
koda
parents: 3613
diff changeset
   328
    // clean up all the other memory allocated
3611
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   329
    freeEverything(true);
3165
3ec07a7d8456 just some very sane stuff for the iphone port (plus some macro on pascal files)
koda
parents: 3162
diff changeset
   330
    if alsoShutdownFrontend then halt;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   331
end;
2590
e7e87e3c67db touch control completely revamped
koda
parents: 2587
diff changeset
   332
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   333
procedure initEverything (complete:boolean);
2699
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2698
diff changeset
   334
begin
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3312
diff changeset
   335
    Randomize();
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3312
diff changeset
   336
3709
c7849b74748d clean and reorder arguments passed to engine
koda
parents: 3697
diff changeset
   337
    // uConsts does not need initialization as they are all consts
4374
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents: 4373
diff changeset
   338
    uUtils.initModule;
3038
4e48c276a468 In pascal unit is a namespace
unc0rr
parents: 3029
diff changeset
   339
    uMisc.initModule;
4359
83ef50815535 Move variables from uMisc to uVariables
unC0Rr
parents: 4357
diff changeset
   340
    uVariables.initModule;
3038
4e48c276a468 In pascal unit is a namespace
unc0rr
parents: 3029
diff changeset
   341
    uConsole.initModule;    // MUST happen after uMisc
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents: 4359
diff changeset
   342
    uCommands.initModule;
4413
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4408
diff changeset
   343
    uCommandHandlers.initModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   344
3038
4e48c276a468 In pascal unit is a namespace
unc0rr
parents: 3029
diff changeset
   345
    uLand.initModule;
4490
f6840f7e2f60 Stub to recieve /draw command
unc0rr
parents: 4454
diff changeset
   346
    uLandPainted.initModule;
f6840f7e2f60 Stub to recieve /draw command
unc0rr
parents: 4454
diff changeset
   347
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   348
    uIO.initModule;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
   349
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   350
    if complete then
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   351
    begin
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   352
        uAI.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   353
        //uAIActions does not need initialization
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   354
        //uAIAmmoTests does not need initialization
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   355
        uAIMisc.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   356
        uAmmos.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   357
        uChat.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   358
        uCollisions.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   359
        //uFloat does not need initialization
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   360
        //uGame does not need initialization
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   361
        uGears.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   362
        uKeys.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   363
        //uLandGraphics does not need initialization
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   364
        //uLandObjects does not need initialization
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   365
        //uLandTemplates does not need initialization
3612
b50215a8a43d land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents: 3611
diff changeset
   366
        uLandTexture.initModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   367
        //uLocale does not need initialization
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
   368
        uRandom.initModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   369
        uScript.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   370
        uSound.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   371
        uStats.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   372
        uStore.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   373
        uTeams.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   374
        uVisualGears.initModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   375
        uWorld.initModule;
4393
883b979697e4 uCaptions
unc0rr
parents: 4389
diff changeset
   376
        uCaptions.initModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   377
    end;
2699
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2698
diff changeset
   378
end;
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2698
diff changeset
   379
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   380
procedure freeEverything (complete:boolean);
2699
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2698
diff changeset
   381
begin
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   382
    if complete then
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   383
    begin
4393
883b979697e4 uCaptions
unc0rr
parents: 4389
diff changeset
   384
        uCaptions.freeModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   385
        uWorld.freeModule;
3615
b78d7959540a further code cleanup and less redundancy
koda
parents: 3613
diff changeset
   386
        uVisualGears.freeModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   387
        uTeams.freeModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   388
        uStore.freeModule;          //stub
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   389
        uStats.freeModule;          //stub
3615
b78d7959540a further code cleanup and less redundancy
koda
parents: 3613
diff changeset
   390
        uSound.freeModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   391
        uScript.freeModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   392
        uRandom.freeModule;         //stub
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   393
        //uLocale does not need to be freed
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   394
        //uLandTemplates does not need to be freed
3612
b50215a8a43d land arrays are allocated dynamically, so DOWNSCALE and LOWRES macros are now removed and replaced by run time flags rqBlurryLand and rqLowRes
koda
parents: 3611
diff changeset
   395
        uLandTexture.freeModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   396
        //uLandObjects does not need to be freed
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   397
        //uLandGraphics does not need to be freed
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   398
        uKeys.freeModule;           //stub
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   399
        uGears.freeModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   400
        //uGame does not need to be freed
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   401
        //uFloat does not need to be freed
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   402
        uCollisions.freeModule;     //stub
4901
d1e2d82d9ccc Thou shalt not leak!
sheepluva
parents: 4900
diff changeset
   403
        uChat.freeModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   404
        uAmmos.freeModule;
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   405
        uAIMisc.freeModule;         //stub
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   406
        //uAIAmmoTests does not need to be freed
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   407
        //uAIActions does not need to be freed
3617
1df21e06b8ba a couple of fixes i missed previously
koda
parents: 3615
diff changeset
   408
        uAI.freeModule;             //stub
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   409
    end;
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
   410
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   411
    uIO.freeModule;             //stub
3038
4e48c276a468 In pascal unit is a namespace
unc0rr
parents: 3029
diff changeset
   412
    uLand.freeModule;
5066
d2684b6f02ce fix leaking of hand-drawn maps
sheepluva
parents: 5052
diff changeset
   413
    uLandPainted.freeModule;
2716
b9ca1bfca24f complete the replacement of init/free wrappers for every unit
koda
parents: 2715
diff changeset
   414
4413
46caab3a8f84 uCommandHandlers
unc0rr
parents: 4408
diff changeset
   415
    uCommandHandlers.freeModule;
4373
fe0e3903bb9e Introduce uCommands.pas
unC0Rr
parents: 4359
diff changeset
   416
    uCommands.freeModule;
3038
4e48c276a468 In pascal unit is a namespace
unc0rr
parents: 3029
diff changeset
   417
    uConsole.freeModule;
4359
83ef50815535 Move variables from uMisc to uVariables
unC0Rr
parents: 4357
diff changeset
   418
    uVariables.freeModule;
4374
bcefeeabaa33 Move some stuff from uMisc to uUtils
unC0Rr
parents: 4373
diff changeset
   419
    uUtils.freeModule;
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   420
    uMisc.freeModule;           // uMisc closes the debug log.
2699
249adefa9c1c replace initialization/finalization statements with custom init functions
koda
parents: 2698
diff changeset
   421
end;
2803
1f446fc5c8ec allow to compile engine as library with HWLIBRARY symbol
koda
parents: 2800
diff changeset
   422
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   423
/////////////////////////
3610
8590424bdfb0 small cleanup
koda
parents: 3598
diff changeset
   424
procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   425
var Preview: TPreview;
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   426
begin
3914
c5c903c6225b Engine:
smaxx
parents: 3904
diff changeset
   427
    cLogfileBase:= 'preview';
3611
ed00aa2b339e interpret parameters before initializing everything
koda
parents: 3610
diff changeset
   428
    initEverything(false);
3610
8590424bdfb0 small cleanup
koda
parents: 3598
diff changeset
   429
{$IFDEF HWLIBRARY}
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3312
diff changeset
   430
    WriteLnToConsole('Preview connecting on port ' + inttostr(port));
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3312
diff changeset
   431
    ipcPort:= port;
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3312
diff changeset
   432
{$ENDIF}
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   433
    InitIPC;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   434
    IPCWaitPongEvent;
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   435
    TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   436
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3312
diff changeset
   437
    Preview:= GenPreview();
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   438
    WriteLnToConsole('Sending preview...');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   439
    SendIPCRaw(@Preview, sizeof(Preview));
3365
37ac593e9027 wow all these files only for land preview and seed generation
koda
parents: 3312
diff changeset
   440
    SendIPCRaw(@MaxHedgehogs, sizeof(byte));
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   441
    WriteLnToConsole('Preview sent, disconnect');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   442
    CloseIPC();
3525
1d7b056ff866 some memory caring code
koda
parents: 3523
diff changeset
   443
    freeEverything(false);
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   444
end;
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   445
3021
1d3302ac371f move the buttons a little
koda
parents: 2990
diff changeset
   446
{$IFNDEF HWLIBRARY}
2008
fc2fb5c938c3 Patch by koda:
unc0rr
parents: 2006
diff changeset
   447
/////////////////////
fc2fb5c938c3 Patch by koda:
unc0rr
parents: 2006
diff changeset
   448
procedure DisplayUsage;
2691
c0da3a98c01c initial support for engine protocol
koda
parents: 2690
diff changeset
   449
var i: LongInt;
2008
fc2fb5c938c3 Patch by koda:
unc0rr
parents: 2006
diff changeset
   450
begin
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   451
    WriteLn('Wrong argument format: correct configurations is');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   452
    WriteLn();
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   453
    WriteLn('  hwengine <path to user hedgewars folder> <path to global data folder> <path to replay file> [options]');
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   454
    WriteLn();
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   455
    WriteLn('where [options] must be specified either as:');
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   456
    WriteLn(' --set-video [screen width] [screen height] [color dept]');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   457
    WriteLn(' --set-audio [volume] [enable music] [enable sounds]');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   458
    WriteLn(' --set-other [language file] [full screen] [show FPS]');
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   459
    WriteLn(' --set-multimedia [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen]');
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   460
    WriteLn(' --set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]');
5130
3602ede67ec5 Add a parameter for game simulation with no gui/sound enabled, just to get statistics
unc0rr
parents: 5111
diff changeset
   461
    WriteLn(' --stats-only');
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   462
    WriteLn();
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   463
    WriteLn('Read documentation online at http://code.google.com/p/hedgewars/wiki/CommandLineOptions for more information');
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   464
    WriteLn();
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   465
    Write('PARSED COMMAND: ');
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   466
    for i:=0 to ParamCount do
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   467
        Write(ParamStr(i) + ' ');
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   468
    WriteLn();
2008
fc2fb5c938c3 Patch by koda:
unc0rr
parents: 2006
diff changeset
   469
end;
fc2fb5c938c3 Patch by koda:
unc0rr
parents: 2006
diff changeset
   470
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   471
////////////////////
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   472
{$INCLUDE "ArgParsers.inc"}
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   473
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   474
procedure GetParams;
97
e7c1df9cce2c - make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents: 81
diff changeset
   475
begin
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   476
    if (ParamCount < 3) then
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   477
        GameType:= gmtSyntax
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   478
    else
5238
46ddaf14509d Enable ~/.hedgewars/Data (or platform equivalent) to override/extend pretty much everything in system Data dir. Obviously desyncing can occur, so this is at user's own risk. Should simplify map etc install. Needs testing.
nemo
parents: 5217
diff changeset
   479
        if (ParamCount = 3) and ((ParamStr(3) = '--stats-only') or (ParamStr(3) = 'landpreview')) then
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   480
            internalSetGameTypeLandPreviewFromParameters()
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   481
        else
3709
c7849b74748d clean and reorder arguments passed to engine
koda
parents: 3697
diff changeset
   482
            if (ParamCount = cDefaultParamNum) then
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   483
                internalStartGameWithParameters()
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   484
            else
3678
00428183300f patch by lucass (polished by me) - command line parsing is now much more flexible
koda
parents: 3670
diff changeset
   485
                playReplayFileWithParameters();
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   486
end;
3021
1d3302ac371f move the buttons a little
koda
parents: 2990
diff changeset
   487
155
401f4ea24715 Engine can generate land preview and send it via IPC
unc0rr
parents: 109
diff changeset
   488
////////////////////////////////////////////////////////////////////////////////
401f4ea24715 Engine can generate land preview and send it via IPC
unc0rr
parents: 109
diff changeset
   489
/////////////////////////////// m a i n ////////////////////////////////////////
401f4ea24715 Engine can generate land preview and send it via IPC
unc0rr
parents: 109
diff changeset
   490
////////////////////////////////////////////////////////////////////////////////
401f4ea24715 Engine can generate land preview and send it via IPC
unc0rr
parents: 109
diff changeset
   491
begin
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   492
    GetParams();
2008
fc2fb5c938c3 Patch by koda:
unc0rr
parents: 2006
diff changeset
   493
3078
07d4ca3afce3 Remove doubled call of freeEverything
nemo
parents: 3064
diff changeset
   494
    if GameType = gmtLandPreview then GenLandPreview()
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   495
    else if GameType = gmtSyntax then DisplayUsage()
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   496
    else Game();
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 3693
diff changeset
   497
2947
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   498
    if GameType = gmtSyntax then
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   499
        ExitCode:= 1
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   500
    else
803b277e4894 hate Smaxx (tested)
unc0rr
parents: 2905
diff changeset
   501
        ExitCode:= 0;
2698
90585aba87ad objc/pascal finally working
koda
parents: 2697
diff changeset
   502
{$ENDIF}
51
b6e3ae05857f - Get rid of hwserv and runhelper
unc0rr
parents:
diff changeset
   503
end.