Changeset 55 for trunk/Sound.pas


Ignore:
Timestamp:
Jan 12, 2017, 9:52:50 PM (5 years ago)
Author:
chronos
Message:
  • Fixed: Bad allocation memory during loading sound files under 64-bit architecture.
  • Added: Ubuntu packaging files.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Sound.pas

    r21 r55  
    44
    55uses
    6   Messages, SysUtils, Classes, Graphics, Controls, Forms {$IFDEF WINDOWS}, MMSystem, Windows{$ENDIF};
     6  Messages, SysUtils, Classes, Graphics, Controls, Forms, fgl
     7  {$IFDEF WINDOWS}, MMSystem, Windows{$ENDIF};
    78
    89function PrepareSound(FileName: string): integer;
     
    9293end;
    9394
    94 type
    95   TSoundList = array [0 .. 99999] of TSound;
    9695
    9796var
    98   nSoundList: integer;
    9997  SoundPlayer: TSoundPlayer;
    100   SoundList: ^TSoundList;
     98  SoundList: TFPGObjectList<TSound>;
    10199  PlayingSound: TSound;
    102100
     
    114112function PrepareSound(FileName: string): integer;
    115113begin
    116   for result := 1 to Length(FileName) do
    117     FileName[result] := upcase(FileName[result]);
    118   result := 0;
    119   while (result < nSoundList) and (SoundList[result].FFileName <> FileName) do
     114  Result := 0;
     115  while (result < SoundList.Count) and (SoundList[result].FFileName <> FileName) do
    120116    inc(result);
    121   if result = nSoundList then
    122   begin // first time this sound is played
    123     if nSoundList = 0 then
    124       ReallocMem(SoundList, 16 * 4)
    125     else if (nSoundList >= 16) and (nSoundList and (nSoundList - 1) = 0) then
    126       ReallocMem(SoundList, nSoundList * (2 * 4));
    127     inc(nSoundList);
    128     SoundList[result] := TSound.Create(FileName);
     117  if result = SoundList.Count then begin
     118    // first time this sound is played
     119    SoundList.Add(TSound.Create(FileName));
     120    Result := SoundList.Count - 1;
    129121  end;
    130122end;
     
    146138  i: integer;
    147139
     140procedure UnitInit;
     141begin
     142  SoundList := TFPGObjectList<TSound>.Create;
     143  PlayingSound := nil;
     144  SoundPlayer := nil;
     145end;
     146
     147procedure UnitDone;
     148begin
     149  if PlayingSound <> nil then begin
     150    PlayingSound.Stop;
     151    Sleep(222);
     152  end;
     153  FreeAndNil(SoundList);
     154end;
     155
    148156initialization
    149157
    150 nSoundList := 0;
    151 SoundList := nil;
    152 PlayingSound := nil;
    153 SoundPlayer := nil;
     158UnitInit;
    154159
    155160finalization
    156161
    157 if PlayingSound <> nil then
    158 begin
    159   PlayingSound.Stop;
    160   Sleep(222);
    161 end;
    162 for i := 0 to nSoundList - 1 do
    163   SoundList[i].Free;
    164 ReallocMem(SoundList, 0);
     162UnitDone;
    165163
    166164end.
Note: See TracChangeset for help on using the changeset viewer.