DEADSOFTWARE

fix sdlmixer on bigendian machines (i hope fully)
[d2df-sdl.git] / src / engine / e_sound_sdl.inc
index ade82620b1dd0bec1c121a52b7e43ed28b787428..afbb8758f3658b92da93419d4f11737c80b3a87f 100644 (file)
@@ -2,8 +2,7 @@
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation, version 3 of the License ONLY.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 interface
 
 uses
-  sdl2,
-  SDL2_mixer,
   {$IFDEF USE_MEMPOOL}mempool,{$ENDIF}
-  e_log,
-  SysUtils;
+  SDL2, SDL2_mixer, envvars,
+  e_log, SysUtils;
 
 type
   TSoundRec = record
@@ -28,6 +25,7 @@ type
     Sound: PMix_Chunk;
     Music: PMix_Music;
     isMusic: Boolean;
+    Loops: Boolean;
     nRefs: Integer;
   end;
 
@@ -71,8 +69,8 @@ const
 
 function e_InitSoundSystem(NoOutput: Boolean = False): Boolean;
 
-function e_LoadSound(FileName: string; var ID: DWORD; isMusic: Boolean): Boolean;
-function e_LoadSoundMem(pData: Pointer; Length: Integer; var ID: DWORD; isMusic: Boolean): Boolean;
+function e_LoadSound(FileName: string; var ID: DWORD; isMusic: Boolean; ForceNoLoop: Boolean = False): Boolean;
+function e_LoadSoundMem(pData: Pointer; Length: Integer; var ID: DWORD; isMusic: Boolean; ForceNoLoop: Boolean = False): Boolean;
 
 // returns channel number or -1
 function e_PlaySound(ID: DWORD): Integer;
@@ -180,6 +178,11 @@ begin
   Result := False;
   SoundInitialized := False;
 
+  {$IFDEF HEADLESS}
+    // HACK: shit this into env and hope for the best
+    SetEnvVar('SDL_AUDIODRIVER', 'dummy');
+  {$ENDIF}
+
   if NoOutput then begin Result := true; Exit end;
 
   // wow, this is actually MIDI player!
@@ -194,7 +197,7 @@ begin
   if (res and MIX_INIT_FLUIDSYNTH) <> 0 then e_WriteLog('SDL: FLUIDSYNTH playback is active', TMsgType.Notify);
 
   e_WriteLog(Format('SDL: initializing mixer at %d with buffer %d', [gsSDLSampleRate, gsSDLBufferSize]), TMsgType.Notify);
-  res := Mix_OpenAudio(gsSDLSampleRate, AUDIO_S16LSB, 2, gsSDLBufferSize);
+  res := Mix_OpenAudio(gsSDLSampleRate, {$IFDEF FPC_LITTLE_ENDIAN}AUDIO_S16LSB{$ELSE}AUDIO_S16MSB{$ENDIF}, 2, gsSDLBufferSize);
   if res = -1 then
   begin
     e_WriteLog('Error initializing SDL mixer:', TMsgType.Fatal);
@@ -283,7 +286,7 @@ begin
   end;
 end;
 
-function e_LoadSound(FileName: String; var ID: DWORD; isMusic: Boolean): Boolean;
+function e_LoadSound(FileName: String; var ID: DWORD; isMusic: Boolean; ForceNoLoop: Boolean = False): Boolean;
 var
   find_id: DWORD;
 begin
@@ -306,6 +309,7 @@ begin
 
   e_SoundsArray[find_id].Data := nil;
   e_SoundsArray[find_id].isMusic := isMusic;
+  e_SoundsArray[find_id].Loops := isMusic and not ForceNoLoop;
   e_SoundsArray[find_id].nRefs := 0;
 
   if isMusic then
@@ -331,7 +335,7 @@ begin
   Result := True;
 end;
 
-function e_LoadSoundMem(pData: Pointer; Length: Integer; var ID: DWORD; isMusic: Boolean): Boolean;
+function e_LoadSoundMem(pData: Pointer; Length: Integer; var ID: DWORD; isMusic: Boolean; ForceNoLoop: Boolean = False): Boolean;
 var
   find_id: DWORD;
   rw: PSDL_RWops;
@@ -372,6 +376,7 @@ begin
   e_SoundsArray[find_id].Data := pData;
   if isid3 then e_SoundsArray[find_id].Data := nil;
   e_SoundsArray[find_id].isMusic := isMusic;
+  e_SoundsArray[find_id].Loops := isMusic and not ForceNoLoop;
   e_SoundsArray[find_id].nRefs := 0;
 
   if isMusic then
@@ -416,6 +421,7 @@ end;
 function e_PlaySound (ID: DWORD): Integer;
 var
   res: Integer = -1;
+  loops: Integer = 0;
 begin
   Result := -1;
   if not SoundInitialized then Exit;
@@ -424,7 +430,8 @@ begin
   begin
     if e_SoundsArray[ID].nRefs >= gMaxSimSounds then Exit;
     Inc(e_SoundsArray[ID].nRefs);
-    res := Mix_PlayChannel(-1, e_SoundsArray[ID].Sound, 0);
+    if e_SoundsArray[ID].Loops then loops := -1;
+    res := Mix_PlayChannel(-1, e_SoundsArray[ID].Sound, loops);
     if res >= 0 then
     begin
       ChanSIds[res].id := ID;
@@ -442,7 +449,8 @@ begin
   begin
     if not e_isMusic(ID) then Exit;
     Mix_HaltMusic();
-    res := Mix_PlayMusic(e_SoundsArray[ID].Music, -1);
+    if e_SoundsArray[ID].Loops then loops := -1;
+    res := Mix_PlayMusic(e_SoundsArray[ID].Music, loops);
     if res >= 0 then res := N_MUSCHAN;
     if SoundMuted then Mix_VolumeMusic(0) else Mix_VolumeMusic(MusVolume);
     Result := res;