DEADSOFTWARE

Added SFS support (resource wads only) (#4)
[d2df-editor.git] / src / editor / f_addresource_sound.pas
index 4efa9878cc1be4d8c082dd6f64beb944b94a6131..15ea34df25db38089339dacd558aafa618435ba1 100644 (file)
@@ -1,13 +1,13 @@
 unit f_addresource_sound;
 
-{$MODE Delphi}
+{$INCLUDE ../shared/a_modes.inc}
 
 interface
 
 uses
   LCLIntf, LCLType, LMessages, SysUtils, Variants, Classes,
   Graphics, Controls, Forms, Dialogs, f_addresource,
-  ExtCtrls, StdCtrls, spectrum, Buttons, ComCtrls;
+  ExtCtrls, StdCtrls, spectrum, Buttons, ComCtrls, utils;
 
 type
   TAddSoundForm = class (TAddResourceForm)
@@ -43,11 +43,24 @@ var
 implementation
 
 uses
-  BinEditor, fmod, fmodtypes, fmoderrors, WADEDITOR, e_log, f_main,
-  g_language;
+  BinEditor, WADEDITOR, e_log, f_main, g_language, g_resources
+{$IFNDEF NOSOUND}, fmod, fmodtypes, fmoderrors;{$ELSE};{$ENDIF}
 
 {$R *.lfm}
 
+{$IFDEF NOSOUND}
+// fuck my life
+const
+  FMOD_OK = 0;
+
+type
+  FMOD_SYSTEM = Pointer;
+  FMOD_CHANNEL = Pointer;
+  FMOD_SOUND = Pointer;
+  FMOD_CREATESOUNDEXINFO = Pointer;
+  FMOD_RESULT = Integer;
+{$ENDIF}
+
 var
   F_System: FMOD_SYSTEM;
   SoundData: Pointer = nil;
@@ -65,6 +78,7 @@ begin
 
   res := FMOD_OK;
 
+{$IFNDEF NOSOUND}
   try
     res := FMOD_System_Create(F_System);
     if res <> FMOD_OK then
@@ -94,6 +108,7 @@ begin
     Application.MessageBox(FMOD_ErrorString(res), 'Initialization', MB_OK or MB_ICONHAND);
     raise;
   end;
+{$ENDIF}
 
   FSpectrum := TMiniSpectrum.Create(pSpectrum);
   FSpectrum.Align := alClient;
@@ -103,53 +118,48 @@ end;
 
 function CreateSoundWAD(Resource: String): Boolean;
 var
-  WAD: TWADEditor_1;
   FileName, SectionName, ResourceName: String;
-  ResLength, sz: Integer;
+  ResLength: Integer;
+  sz: LongWord;
   soundExInfo: FMOD_CREATESOUNDEXINFO;
   res: FMOD_RESULT;
 
 begin
   Result := False;
-
   SoundData := nil;
   Sound := nil;
   Channel := nil;
+{$IFNDEF NOSOUND}
   g_ProcessResourceStr(Resource, FileName, SectionName, ResourceName);
+  g_ReadResource(FileName, SectionName, ResourceName, SoundData, ResLength);
 
-  WAD := TWADEditor_1.Create;
-  WAD.ReadFile(FileName);
-
-  if WAD.GetResource(SectionName, ResourceName, SoundData, ResLength) then
+  if SoundData <> nil then
     begin
       sz := SizeOf(FMOD_CREATESOUNDEXINFO);
       FillMemory(@soundExInfo, sz, 0);
       soundExInfo.cbsize := sz;
-      soundExInfo.length := ResLength;
+      soundExInfo.length := LongWord(ResLength);
 
       res := FMOD_System_CreateStream(F_System, SoundData,
-        FMOD_LOOP_OFF + FMOD_2D + FMOD_SOFTWARE + FMOD_OPENMEMORY,
+        FMOD_LOOP_OFF or FMOD_2D or FMOD_OPENMEMORY,
         @soundExInfo, Sound);
         
       if res <> FMOD_OK then
       begin
         e_WriteLog(Format('Error creating sound %s', [Resource]), MSG_WARNING);
         e_WriteLog(FMOD_ErrorString(res), MSG_WARNING);
-        WAD.Free();
         Exit;
       end;
     end
   else
     begin
       e_WriteLog(Format('Error loading sound %s', [Resource]), MSG_WARNING);
-      e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
-      WAD.Free();
+      //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
       Exit;
     end;
  
-  WAD.Free();
   Result := True;
+{$ENDIF}
 end;
 
 procedure TAddSoundForm.bbPlayClick(Sender: TObject);
@@ -166,7 +176,7 @@ begin
 
     if not CreateSoundWAD(FFullResourceName) then
       Exit;
-
+{$IFNDEF NOSOUND}
     res := FMOD_System_PlaySound(F_System, FMOD_CHANNEL_FREE,
              Sound, False, Channel);
     if res <> FMOD_OK then
@@ -182,6 +192,7 @@ begin
     FMOD_Channel_SetVolume(Channel, 1.0);
 
     FSpectrum.SetChannel(Channel);
+{$ENDIF}
   end;
 end;
 
@@ -198,7 +209,7 @@ var
 
 begin
   Inherited;
-
+{$IFNDEF NOSOUND}
   FMOD_System_Update(F_System);
   
   ShowSpectrum();
@@ -206,6 +217,7 @@ begin
   res := FMOD_Channel_IsPlaying(Channel, b);
   if (res <> FMOD_OK) or (not b) then
     bbStop.Click();
+{$ENDIF}
 end;
 
 procedure TAddSoundForm.FormDestroy(Sender: TObject);
@@ -216,7 +228,7 @@ begin
   Inherited;
 
   FSpectrum.Free;
-
+{$IFNDEF NOSOUND}
   res := FMOD_System_Close(F_System);
   if res <> FMOD_OK then
   begin
@@ -231,18 +243,19 @@ begin
     e_WriteLog('Error releasing FMOD system!', MSG_FATALERROR);
     e_WriteLog(FMOD_ErrorString(res), MSG_FATALERROR);
   end;
+{$ENDIF}
 end;
 
 procedure Sound_StopRelease();
 begin
   Playing := False;
-
+{$IFNDEF NOSOUND}
   if Channel <> nil then
     FMOD_Channel_Stop(Channel);
 
   if Sound <> nil then
     FMOD_Sound_Release(Sound);
-
+{$ENDIF}
   if SoundData <> nil then
     FreeMem(SoundData);