DEADSOFTWARE

sdlmixer: more midi configuration
[d2df-sdl.git] / src / game / g_main.pas
index 16085cead25ca40398edda2a2a43bb8a4b6f51e6..5c458d5159025a8f4b376bb425802b8986f1a877 100644 (file)
@@ -28,10 +28,6 @@ procedure KeyPress (K: Word);
 procedure CharPress (C: AnsiChar);
 
 var
-  {--- TO REMOVE ---}
-  //GameDir: string;
-  {-----------------}
-
   {--- Read-only dirs ---}
   GameWAD: string;
   DataDirs: SSArray;
@@ -48,9 +44,12 @@ var
   CacheDirs: SSArray;
   ConfigDirs: SSArray;
   ScreenshotDirs: SSArray;
+  StatsDirs: SSArray;
   MapDownloadDirs: SSArray;
   WadDownloadDirs: SSArray;
 
+  GameWADName: string = 'GAME';
+
 implementation
 
 uses
@@ -61,6 +60,9 @@ uses
 {$IFDEF LINUX}
   BaseUnix,
 {$ENDIF}
+{$IFDEF DARWIN}
+  MacOSAll, CocoaAll,
+{$ENDIF}
 {$IFDEF USE_SDL2}
   SDL2,
 {$ENDIF}
@@ -76,8 +78,10 @@ uses
 var
   charbuff: packed array [0..15] of AnsiChar;
   binPath: AnsiString = '';
-  forceCurrentDir: Boolean = false;
-
+  forceBinDir: Boolean;
+  {$IFDEF USE_SDLMIXER}
+    UseNativeMusic: Boolean;
+  {$ENDIF}
 
 function GetBinaryPath (): AnsiString;
 {$IFDEF LINUX}
@@ -109,69 +113,107 @@ begin
     e_LogWriteln('  ' + dir);
 end;
 
+{$IFDEF DARWIN}
+  function NSStringToAnsiString (s: NSString): AnsiString;
+    var i: Integer;
+  begin
+    result := '';
+    for i := 0 to s.length - 1 do
+      result := result + AnsiChar(s.characterAtIndex(i));
+  end;
+
+  function GetBundlePath (): AnsiString;
+    var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
+  begin
+    pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
+    pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
+    CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
+    CFRelease(pathRef);
+    CFRelease(pathCFStr);
+    Result := pathStr;
+  end;
+{$ENDIF}
+
 procedure InitPath;
   var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
-  //first: Boolean = true;
 
-  procedure xput (s: AnsiString);
-  {
-  var
-    f: TextFile;
+  procedure AddDir (var dirs: SSArray; append: AnsiString);
   begin
-    AssignFile(f, 'zzz.log');
-    if (first) then
-    begin
-      Rewrite(f);
-      first := false;
-    end
-    else
-    begin
-      Append(f);
-    end;
-    writeln(f, s);
-    CloseFile(f);
+    SetLength(dirs, Length(dirs) + 1);
+    dirs[High(dirs)] := ExpandFileName(append)
   end;
-  }
+
+  function IsSep (ch: Char): Boolean;
   begin
+    {$IFDEF WINDOWS}
+    result := (ch = '/') or (ch = '\');
+    {$ELSE}
+    result := (ch = '/');
+    {$ENDIF}
   end;
 
-  procedure AddPath (var arr: SSArray; str: AnsiString; usecwd: Boolean=true);
-  var
-    ss: ShortString;
+  function OptimizePath (dir: AnsiString): AnsiString;
+    var i, len: Integer; s: AnsiString;
   begin
-    if (length(str) = 0) then exit;
-    //writeln('NEW PATH(0): ['+str+']');
-    if (forceCurrentDir or usecwd) then
+    i := 1; len := Length(dir); s := '';
+    while i <= len do
     begin
-      str := fixSlashes(ExpandFileName(str));
-    end
-    else
-    begin
-      str := fixSlashes(str);
-      if (not isAbsolutePath(str)) then str := binPath+str;
-      while (length(str) > 0) do
+      if IsSep(dir[i]) then
       begin
-        if (isRootPath(str)) then exit;
-        if (str[length(str)] = '/') then begin Delete(str, length(str), 1); continue; end;
-        if (length(str) >= 2) and (Copy(str, length(str)-1, 2) = '/.') then begin Delete(str, length(str)-1, 2); continue; end;
-        break;
-      end;
-    end;
-    if (length(str) = 0) then exit;
-    if (length(str) > 255) then
-    begin
-      xput('path too long: ['+str+']');
-      raise Exception.Create(Format('path "%s" too long', [str]));
+        s := s + DirectorySeparator;
+        Inc(i);
+        while (i <= len) and IsSep(dir[i]) do Inc(i);
+        if (i <= len) and (dir[i] = '.') then
+        begin
+          if (i = len) or IsSep(dir[i + 1]) then
+          begin
+            Inc(i)
+          end
+          else if (i + 1 <= len) and (dir[i + 1] = '.') then
+          begin
+            if (i + 1 = len) or IsSep(dir[i + 2]) then
+            begin
+              s := e_UpperDir(s);
+              Inc(i, 2)
+            end
+          end
+        end
+      end
+      else
+      begin
+        s := s + dir[i];
+        Inc(i)
+      end
     end;
-    for ss in arr do
+    result := s
+  end;
+
+  procedure OptimizeDirs (var dirs: SSArray);
+    var i, j, k: Integer;
+  begin
+    for i := 0 to High(dirs) do
+      dirs[i] := OptimizePath(dirs[i]);
+    // deduplicate
+    i := High(dirs);
+    while i >= 0 do
     begin
-      //writeln('<<<', ss, '>>> : [', str, ']');
-      if (ss = str) then exit;
-    end;
-    SetLength(arr, Length(arr)+1);
-    //arr[High(arr)] := ExpandFileName(str);
-    arr[High(arr)] := str;
-    //writeln('NEW PATH(1): ['+str+']');
+      j := 0;
+      while j < i do
+      begin
+        if dirs[j] = dirs[i] then
+        begin
+          for k := j + 1 to High(dirs) do
+            dirs[k - 1] := dirs[k];
+          Dec(i);
+          SetLength(dirs, High(dirs))
+        end
+        else
+        begin
+          Inc(j)
+        end
+      end;
+      Dec(i)
+    end
   end;
 
   procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
@@ -179,111 +221,177 @@ procedure InitPath;
   begin
     if Length(dirs) = 0 then
       for s in base do
-        AddPath(dirs, e_CatPath(s, append), false)
-  end;
-
-  procedure AddDir (var dirs: SSArray; append: AnsiString);
-  begin
-    SetLength(dirs, Length(dirs) + 1);
-    dirs[High(dirs)] := append
+        AddDir(dirs, e_CatPath(s, append));
+    OptimizeDirs(dirs)
   end;
 
   function GetDefaultRODirs (): SSArray;
-    {$IFDEF UNIX}
+    {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
       var home: AnsiString;
     {$ENDIF}
-  begin
-    {$IFDEF USE_SDL2}
-      AddDir(result, SDL_GetBasePath());
-      AddDir(result, SDL_GetPrefPath('', 'doom2df'));
+    {$IFDEF WINDOWS}
+      var appdata: AnsiString;
     {$ENDIF}
-    {$IFDEF UNIX}
-      AddDir(result, '/usr/share/doom2df');
-      AddDir(result, '/usr/local/share/doom2df');
-      home := GetEnvironmentVariable('HOME');
-      if home <> '' then
-        AddDir(result, e_CatPath(home, '.doom2df'));
+    {$IFDEF DARWIN}
+      var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
     {$ENDIF}
-    {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
-      AddDir(result, SDL_AndroidGetInternalStoragePath());
-      if SDL_AndroidGetExternalStorageState() <> 0 then
-        AddDir(result, SDL_AndroidGetExternalStoragePath());
+  begin
+    result := nil;
+    {$IFDEF DARWIN}
+      bundle := GetBundlePath();
+      if ExtractFileExt(bundle) <> '.app' then
+        AddDir(result, binpath);
+    {$ELSE}
+      AddDir(result, binPath);
     {$ENDIF}
-    AddDir(result, '.');
+    if forceBinDir = false then
+    begin
+      {$IFDEF USE_SDL2}
+        AddDir(result, SDL_GetBasePath());
+        AddDir(result, SDL_GetPrefPath('', 'doom2df'));
+      {$ENDIF}
+      {$IFDEF WINDOWS}
+        appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
+        if appdata <> '' then
+          AddDir(result, appdata);
+      {$ENDIF}
+      {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
+        AddDir(result, '/usr/share/doom2df');
+        AddDir(result, '/usr/local/share/doom2df');
+        home := GetEnvironmentVariable('HOME');
+        if home <> '' then
+          AddDir(result, e_CatPath(home, '.doom2df'));
+      {$ENDIF}
+      {$IFDEF DARWIN}
+        bundle := GetBundlePath();
+        if bundle <> '' then
+          AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
+        dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
+        for i := 0 to dirArr.count - 1 do
+        begin
+          s := NSStringToAnsiString(dirArr.objectAtIndex(i));
+          AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
+        end;
+      {$ENDIF}
+      {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
+        AddDir(result, SDL_AndroidGetInternalStoragePath());
+        if SDL_AndroidGetExternalStorageState() <> 0 then
+          AddDir(result, SDL_AndroidGetExternalStoragePath());
+      {$ENDIF}
+    end
   end;
 
   function GetDefaultRWDirs (): SSArray;
-    {$IFDEF UNIX}
+    {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
       var home: AnsiString;
     {$ENDIF}
-  begin
-    {$IF DEFINED(USE_SDL2)}
-      AddDir(result, SDL_GetPrefPath('', 'doom2df'));
+    {$IFDEF WINDOWS}
+      var appdata: AnsiString;
     {$ENDIF}
-    {$IFDEF UNIX}
-      home := GetEnvironmentVariable('HOME');
-      if home <> '' then
-        AddDir(result, e_CatPath(home, '.doom2df'));
+    {$IFDEF DARWIN}
+      var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
     {$ENDIF}
-    {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
-      if SDL_AndroidGetExternalStorageState() <> 0 then
-        AddDir(result, SDL_AndroidGetExternalStoragePath());
+  begin
+    result := nil;
+    {$IFDEF DARWIN}
+      bundle := GetBundlePath();
+      if ExtractFileExt(bundle) <> '.app' then
+        AddDir(result, binPath);
+    {$ELSE}
+      AddDir(result, binPath);
     {$ENDIF}
-    AddDir(result, '.');
+    if forceBinDir = false then
+    begin
+      {$IFDEF USE_SDL2}
+        AddDir(result, SDL_GetPrefPath('', 'doom2df'));
+      {$ENDIF}
+      {$IFDEF WINDOWS}
+        appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
+        if appdata <> '' then
+          AddDir(result, appdata);
+      {$ENDIF}
+      {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
+        home := GetEnvironmentVariable('HOME');
+        if home <> '' then
+          AddDir(result, e_CatPath(home, '.doom2df'));
+      {$ENDIF}
+      {$IFDEF DARWIN}
+        dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
+        for i := 0 to dirArr.count - 1 do
+        begin
+          s := NSStringToAnsiString(dirArr.objectAtIndex(i));
+          AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
+        end;
+      {$ENDIF}
+      {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
+        if SDL_AndroidGetExternalStorageState() <> 0 then
+          AddDir(result, SDL_AndroidGetExternalStoragePath());
+      {$ENDIF}
+    end
   end;
 
 begin
-  //GetDir(0, GameDir);
+  forceBinDir := false;
   binPath := GetBinaryPath();
-  xput('binPath=['+binPath+']');
-
-  for i := 1 to ParamCount do
-  begin
-    if (ParamStr(i) = '--cwd') then
-    begin
-      forceCurrentDir := true;
-      break
-    end
-  end;
 
   i := 1;
   while i < ParamCount do
   begin
     case ParamStr(i) of
+    '--like-windoze': forceBinDir := true;
     '--rw-dir':
       begin
         Inc(i);
         rwdir := ParamStr(i);
         (* RW *)
-        AddPath(LogDirs, e_CatPath(rwdir, ''));
-        AddPath(SaveDirs, e_CatPath(rwdir, 'data'));
-        AddPath(CacheDirs, e_CatPath(rwdir, 'data/cache'));
-        AddPath(ConfigDirs, e_CatPath(rwdir, ''));
-        AddPath(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
-        AddPath(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
-        AddPath(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
+        AddDir(LogDirs, e_CatPath(rwdir, ''));
+        AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
+        AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
+        AddDir(ConfigDirs, e_CatPath(rwdir, ''));
+        AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
+        AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
+        AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
+        AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
         (* RO *)
-        AddPath(DataDirs, e_CatPath(rwdir, 'data'));
-        AddPath(ModelDirs, e_CatPath(rwdir, 'data/models'));
-        AddPath(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
-        AddPath(MapDirs, e_CatPath(rwdir, 'maps'));
-        AddPath(WadDirs, e_CatPath(rwdir, 'wads'));
+        AddDir(DataDirs, e_CatPath(rwdir, 'data'));
+        AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
+        AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
+        AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
+        AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
       end;
     '--ro-dir':
       begin
         Inc(i);
         rodir := ParamStr(i);
         (* RO *)
-        AddPath(DataDirs, e_CatPath(rodir, 'data'));
-        AddPath(ModelDirs, e_CatPath(rodir, 'data/models'));
-        AddPath(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
-        AddPath(MapDirs, e_CatPath(rodir, 'maps'));
-        AddPath(WadDirs, e_CatPath(rodir, 'wads'));
+        AddDir(DataDirs, e_CatPath(rodir, 'data'));
+        AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
+        AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
+        AddDir(MapDirs, e_CatPath(rodir, 'maps'));
+        AddDir(WadDirs, e_CatPath(rodir, 'wads'));
+      end;
+    '--game-wad':
+      begin
+        Inc(i);
+        GameWADName := ParamStr(i);
+      end;
+    '--config':
+      begin
+        Inc(i);
+        gConfigScript := ParamStr(i);
       end;
     end;
     Inc(i)
   end;
 
+  // prefer bin dir if it writable and contains game.wad
+  if forceBinDir = false then
+  begin
+    if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
+      if e_CanCreateFilesAt(binPath) then
+        forceBinDir := true
+  end;
+
   (* RO *)
   rodirs := GetDefaultRODirs();
   AddDef(DataDirs, rodirs, 'data');
@@ -301,11 +409,13 @@ begin
   AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
   AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
   AddDef(ScreenshotDirs, rwdirs, 'screenshots');
+  AddDef(StatsDirs, rwdirs, 'stats');
 
   for i := 0 to High(MapDirs) do
-    AddPath(AllMapDirs, MapDirs[i]);
+    AddDir(AllMapDirs, MapDirs[i]);
   for i := 0 to High(MegawadDirs) do
-    AddPath(AllMapDirs, MegawadDirs[i]);
+    AddDir(AllMapDirs, MegawadDirs[i]);
+  OptimizeDirs(AllMapDirs);
 
   if LogFileName = '' then
   begin
@@ -319,14 +429,13 @@ begin
       {$ENDIF}
     end
   end;
-
-  xput('binPath=['+binPath+']');
+  
+  // HACK: ensure the screenshots folder also has a stats subfolder in it
+  rwdir := e_GetWriteableDir(ScreenshotDirs, false);
+  if rwdir <> '' then CreateDir(rwdir + '/stats');
 end;
 
 procedure InitPrep;
-  {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
-    var timiditycfg: AnsiString;
-  {$ENDIF}
   var i: Integer;
 begin
   {$IFDEF HEADLESS}
@@ -334,10 +443,9 @@ begin
   {$ENDIF}
   for i := 1 to ParamCount do
   begin
-    if (ParamStr(i) = '--con-stdout') then
-    begin
-      conbufDumpToStdOut := true;
-      break
+    case ParamStr(i) of
+      '--con-stdout': conbufDumpToStdOut := true;
+      '--no-fbo': glRenderToFBO := false;
     end
   end;
 
@@ -346,7 +454,10 @@ begin
   e_InitWritelnDriver();
   e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
   e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
+  e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
+  e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
 
+  e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
   e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
 
   PrintDirs('DataDirs', DataDirs);
@@ -360,55 +471,44 @@ begin
   PrintDirs('CacheDirs', CacheDirs);
   PrintDirs('ConfigDirs', ConfigDirs);
   PrintDirs('ScreenshotDirs', ScreenshotDirs);
+  PrintDirs('StatsDirs', StatsDirs);
   PrintDirs('MapDownloadDirs', MapDownloadDirs);
   PrintDirs('WadDownloadDirs', WadDownloadDirs);
 
-  GameWAD := e_FindWad(DataDirs, 'GAME');
+  GameWAD := e_FindWad(DataDirs, GameWADName);
   if GameWad = '' then
   begin
-    e_WriteLog('GAME.WAD not installed?', TMsgType.Fatal);
-    {$IFDEF USE_SDL2}
-      SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 'Doom 2D Forever', 'GAME.WAD not installed?', nil);
+    e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
+    {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
+      if forceBinDir = false then
+        SDL_ShowSimpleMessageBox(
+          SDL_MESSAGEBOX_ERROR, 
+          'Doom 2D Forever',
+          PChar('WAD ' + GameWADName + ' not found in data directories.'),
+          nil
+        );
     {$ENDIF}
+    e_DeinitLog;
     Halt(1);
   end;
-
-  {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
-    timiditycfg := 'timidity.cfg';
-    if e_FindResource(ConfigDirs, timiditycfg) = true then
-    begin
-      timiditycfg := ExpandFileName(timiditycfg);
-      SetEnvVar('TIMIDITY_CFG', timiditycfg);
-      e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
-    end;
-  {$ENDIF}
 end;
 
 procedure Main();
 {$IFDEF ENABLE_HOLMES}
   var flexloaded: Boolean;
 {$ENDIF}
-  var s: AnsiString;
 begin
   InitPath;
   InitPrep;
   e_InitInput;
   sys_Init;
 
-  s := CONFIG_FILENAME;
-  if e_FindResource(ConfigDirs, s) = true then
-  begin
-    g_Options_Read(s)
-  end
-  else
-  begin
-    g_Options_SetDefault;
-    g_Options_SetDefaultVideo
-  end;
-  if sys_SetDisplayMode(gScreenWidth, gScreenHeight, gBPP, gFullScreen) = False then
+  g_Options_SetDefault;
+  g_Options_SetDefaultVideo;
+  g_Console_SysInit;
+  if sys_SetDisplayMode(gRC_Width, gRC_Height, gBPP, gRC_FullScreen, gRC_Maximized) = False then
     raise Exception.Create('Failed to set videomode on startup.');
 
-  g_Console_SysInit;
   e_WriteLog(gLanguage, TMsgType.Notify);
   g_Language_Set(gLanguage);
 
@@ -477,12 +577,16 @@ begin
     if assigned(oglDeinitCB) then oglDeinitCB;
   {$ENDIF}
 
+  g_Console_WriteGameConfig;
   sys_Final;
 end;
 
 procedure Init();
-var
-  NoSound: Boolean;
+  {$IFDEF USE_SDLMIXER}
+    var timiditycfg: AnsiString;
+    var oldcwd, newcwd: RawByteString;
+  {$ENDIF}
+  var NoSound: Boolean;
 begin
   Randomize;
 
@@ -505,10 +609,43 @@ begin
     e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
 *)
 
-  if (not gNoSound) then
+  if gNoSound = false then
   begin
     e_WriteLog('Initializing sound system', TMsgType.Notify);
+    {$IFDEF USE_SDLMIXER}
+      newcwd := '';
+      if UseNativeMusic then
+        SetEnvVar('SDL_NATIVE_MUSIC', '1');
+      timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
+      if timiditycfg = '' then
+      begin
+        timiditycfg := 'timidity.cfg';
+        if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
+        begin
+          timiditycfg := ExpandFileName(timiditycfg);
+          newcwd := ExtractFileDir(timiditycfg);
+          SetEnvVar('TIMIDITY_CFG', timiditycfg);
+        end
+        else
+          timiditycfg := '';
+      end;
+      e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
+      e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
+    {$ENDIF}
     e_InitSoundSystem(NoSound);
+    {$IFDEF USE_SDLMIXER}
+      if e_TimidityDecoder and (newcwd <> '') then
+      begin
+        (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
+        (*       CWD not restored after sound init because timidity  *)
+        (*       store relative pathes internally and load patches   *)
+        (*       later. I hope game never relies on CWD.             *)
+        oldcwd := '';
+        GetDir(0, oldcwd);
+        ChDir(newcwd);
+        e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
+      end;
+    {$ENDIF}
   end;
 
   e_WriteLog('Init game', TMsgType.Notify);
@@ -536,7 +673,16 @@ end;
 
 procedure Update ();
 begin
+  // remember old mobj positions, prepare for update
+  g_Game_PreUpdate();
+  // server: receive client commands for new frame
+  // client: receive game state changes from server
+       if (NetMode = NET_SERVER) then g_Net_Host_Update()
+  else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
+  // think
   g_Game_Update();
+  // server: send any accumulated outgoing data to clients
+  if NetMode = NET_SERVER then g_Net_Flush();
 end;
 
 
@@ -888,4 +1034,13 @@ begin
   end;
 end;
 
+initialization
+{$IFDEF USE_SDLMIXER}
+  conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
+  {$IFDEF DARWIN}
+    UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
+  {$ELSE}
+    UseNativeMusic := false;
+  {$ENDIF}
+{$ENDIF}
 end.