Results 1 to 10 of 10

Thread: Shooting at the Player

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Hi,

    The good News is that I found the Memory leak... it is indeed BASS . For example I have in menu the Cursor with Sound. Simply by moving trough the menu... Memory goes higher by every Sound played.

    THis is what I am doing in the doMenu procedure :


    Code:
    procedure TMarsInvadors.doMenu;
    begin
        // up arrow
        if MarsInvadors.Keyboard.KeyPressed[DIK_UP] then
          begin
            dec(MenuChoice);
            if MenuChoice < 1 then MenuChoice := 3;
            PlaySound(ExtractFilePath(Application.Exename)+'sounds\choice.mp3');
          end;
        // down arrow
        if MarsInvadors.Keyboard.KeyPressed[DIK_DOWN] then
          begin
            inc(MenuChoice);
            if MenuChoice > 3 then MenuChoice := 1;
            PlaySound(ExtractFilePath(Application.Exename)+'sounds\choice.mp3');
          end;
        case MenuChoice of
            1 : MarsCursor.Y:= 320;
            2 : MarsCursor.Y:= 352;
            3 : MarsCursor.Y:= 416;
        end;
        // enter
        if MarsInvadors.KeyBoard.KeyPressed[DIK_RETURN] then
          begin
            case MenuChoice of
                1:
                 begin
                   // game
                   MarsCursor.Dead;
                   GameState:=gsReset;
                 end;
                2:
                 begin
                   // info
                   MarsCursor.Dead;
                   GameState:=gsInsLoad;
                 end;
                3:
                 begin
                   // exit
                 end;
            end;
          end;
    
    end;
    Nothing fancy... the Playsound procedure Looks like this :


    Code:
    procedure PlaySound(const SampleFilename : String);
    var mySound :  HStream;
    begin
      // free both MOD and stream, it must be one of them! :)
     BASS_MusicFree(mySound);
     BASS_StreamFree(mySound);
    
     mySound := BASS_StreamCreateFile(FALSE, PChar(SampleFilename), 0, 0, BASS_SAMPLE_FX {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF});
    // BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 4000);
     BASS_ChannelPlay(mySound, False);
    end;
    Is there some obvious error here ? I must confess the bass Thing I did in quiet a haste . I googled now... found someone else mentioning some Problems related to BASS.dll free Version with mp3 ... from year 2012.... I am not
    sure if it's that same bug still present. Or I am just doing something totaly wrong here.

    Thank you

  2. #2
    Solved it I think :


    Code:
    procedure PlaySound(const SampleFilename : String);
    var mySound :  HStream;
    begin
      // free both MOD and stream, it must be one of them! :)
    // BASS_MusicFree(mySound);
    // BASS_StreamFree(mySound);
     mySound := BASS_StreamCreateFile(FALSE, PChar(SampleFilename), 0, 0, BASS_SAMPLE_FX {$IFDEF UNICODE} or BASS_UNICODE {$ENDIF} or BASS_STREAM_AUTOFREE);
    // BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 4000);
     BASS_ChannelPlay(mySound, False);
     if BASS_ChannelIsActive(mySound) = 0 then
        begin
          BASS_ChannelStop(mySound);
          BASS_Streamfree(mySound);
        end;
    end;
    BASS_STREAM_AUTOFREE was needed , note though it stops it correctly but it was not freed no idea why....

    Thanx

  3. #3
    It's been a while since I used BASS, but unless the workflow has changed, you shouldn't really free streams while the program is running. Calling StreamCreateFile basically reads the file again from the disk, and that's bad. Instead, what you want is:
    1. Load file at program start
    2. Use channels to play the sound (the BASS Stream). If you want to change the volume, stop, pause, or however manipulate the playing sound, keep the channel number and use the BASS_ChannelSomething() functions. You don't have to free channels manually, as managing memory for channels is done automatically by BASS.
    3. Only free the Stream when you're closing the application, or for some reason need to load a different file to the same stream handle (say, your game hero underwent a transformation and now uses a different sound set)

  4. #4
    Super Vegeta , thank you!

    Now that you told me , it makes sense ... and a lot of sense. Especially the disk io part . My game hangs for a sec when the Music changes.

    I Need to rewrite the functions.

    Load all Sound and Music effects on Startup . And then just Play the appropriate channel instead.
    So if a Bullett is shoot by 5 different alliens instead of wasting disk i/o I just Play the channel from Memory when needed.


    Thank you for this great advice!!!

  5. #5
    Hi all,

    Just wanted to post, I managed to solve the bullet calculation . Not alone unfortunately... google helped. And I don't understand it 100%.... I think I may end up taking private Math Classes

    Code:
                        vx := (PlayerSprite.X+(PlayerSprite.ImageWidth/2)) - (Self.X+(Self.ImageWidth/2));
                        vy := (PlayerSprite.Y+(PlayerSprite.ImageHeight/2)) - (Self.Y+(Self.ImageHeight/2));
    
                        len := sqrt(sqr(vx) + sqr(vy));
    
                        vx := vx  / len;
                        vy := vy / len;
    
                        Bullet.Direction:= vx * 4;
                        Bullet.MoveSpeed:= vy * 4;
    What happens here is the vector lenth is calculated... then normalised , I understand what it does... goes from 0 to 1 .... but this formula is something I would have never figured out

    Also I realised my mistake...I need to alter the speed on both X and Y axis in order to work perfectly ... it even shots backwards...

    Greetings...

    ps.: I dont have any new material, I have level 1 done... altered the background, enemies use different attacks... level 2 is done..it turned out quiet cool... level 2 ending is done
    It took a while to come up with a crazy story... Its god damn hard, but I will try and not post anything until done....4 more levels

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •