program freezes for a moment

Blitz3D Forums/Blitz3D Beginners Area/program freezes for a moment

ando(Posted 2012) [#1]
G-day to all.
I've been working on a little 2D 2 player game and there is a wierd problem that I can't figure out.
I can run around and build some basic structures like walls, floors and ramps but when I fire the rocket the program freezes for about 4 seconds. This freeze only occurs for the first shot then everything works normally.
In the latest version there are 3 kinds of rockets and they all share the same sounds. R2 (rocket 2) and R3 work fine but when I fire R1 it's freeze time.

The only fix I could come up with was a bit of a cheat. I made the program fire the first shot when it boots up so the problem doesn't show up during game play.

I really can't find the cause in the code so if you've got any ideas ?

While I'm here, question 2. Do I really have to FreeImage when a program ends or is the memory cleared automatically ?

A big thanks for any help.


Yasha(Posted 2012) [#2]
Do I really have to FreeImage when a program ends or is the memory cleared automatically


Anything a program was using is freed by the OS when the program ends (otherwise... you'd likely have some serious problems anytime an application goes wrong).

This freeze only occurs for the first shot


Are you loading or creating assets? Something like a "if rocket doesn't exist then load mesh and generate texture"? Or are you using PlayMusic instead of PlaySound? (I'm not up to date on matters of sound but it's possible the engine would cache the sound for the second+ uses.)


ando(Posted 2012) [#3]
Thanks Yasha, now I will leave out the bla bla at the end of my code. :)
Obviously I will still use FreeImage in between levels.

Reguarding the main problem, I do this in the early prep....

Global firesound=LoadSound("data/firemissile2.wav")

And when the sound is needed...

PlaySound firesound

The strange thing is that the problem doesn't occur for the other rockets.
I can try to post an early version of the code because that is where the problem is likely hiding.
Is there a limit to the size of code that can be posted ?


ando(Posted 2012) [#4]
Well I'll just post the older code which still has the fault and if anyone's got some spare time maybe you can figure it out.
If you find the very well hidden fault related to my beginner style of coding.... um, don't worry about it, I know about that one. :)

I added two sections of code that push the problem to when it's first run called (commented) shoot for first run. It's in player 1 section and another in player 2 section.



Last edited 2012


Ross C(Posted 2012) [#5]
Do you have the assoicated media so we may run the code? It's hard to read through your code, as you avoid using functions and have evrything happening in the main loop.

For me, and it may help you, if you structure your code something like:


Set up graphics
Load media
Set Globals

While Not Keyhit(1) ; Main loop, exited via escape key

   Update_Game_Mode()
   Draw_Game_Mode()
   Flip

Wend ; loop till condition is met
End

Function Update_Game_Mode()

   If Game_Mode = 1 then
      Update_Title_Screen()
   ElseIf Game_Mode = 2 then
      Update_Main_Game()
   ElseIf Game_Mode = 3 then
      Update_Game_Over_Screen()
   End if

End Function


Function Update_Title_Screen()

   If Keyhit(continue) then
      Game_Mode = 2
   End  If

End Function


Etc Etc. This way, you essentially have your game broken down easily into sections. If you have a problem, you can narrow it down very easily, and add new modes easily.

Regarding your issue, as I say, if you could post the media, or zip the whole project up, that would be super helpful!


Guy Fawkes(Posted 2012) [#6]
nice code! :)


Adam Novagen(Posted 2012) [#7]
Is there a limit to the size of code that can be posted ?
No, but using the {codebox} tag is better for large code postings simply because it saves a lot of scrolling ;D

I'm just cruising this while getting breakfast, but one thing I would definitely remark is that using a Select...Case instead of many sequential If...EndIf statements makes code a lot more readable. There are quite a few places where this could be used, for instance when determining which tool the player's carrying.

Also, to second Ross' comment with something that you'll probably learn yourself if you stay serious about this long enough: MORE FUNCTIONS. Break your code down into functions constantly. I mean no disrespect, just honesty, when I say that this code here is a really long near-spaghetti mess of If...EndIf, and that's not just hard for us to read, but it'll become hard for you to read the larger your program gets.

Last edited 2012


Yasha(Posted 2012) [#8]
3 kinds of rockets and they all share the same sounds

The strange thing is that the problem doesn't occur for the other rockets.


So... you mean that there's one sound file/asset shared by all three rockets, right? (Haven't looked through the above code ion detail yet...)

So does it affect the first type of rocket, or does it affect all three types of rocket and it's just that the first type is also the first one being fired ingame? Because if the same sound can play without delay for types 2 and 3, and then pauses for type 1, that'd be pretty weird (or rather, a sign that sound probably isn't the problem).

Is there a limit to the size of code that can be posted ?


Actually there is, something like 60-70 KB I think (I've run up against it trying to post completed projects a couple of times). If the end of your post is cut off... you can always post a second one below!


Adam Novagen(Posted 2012) [#9]
Alright, I've had a look through your code, and while I haven't traced the problem itself I have come to a few conclusions.

The problem does not appear to be in your code itself. Bad structure and arrangement notwithstanding, it's quite solid; at the game's startup, tool is 8, shoot1 is 0 and booming1 is also 0, and those are the only three conditions needed to fire the rocket, other than button2 which is immediately set by JoyDown(). This means that as soon as you press the fire button, there is no reason that I can find in your code for PlaySound firesound not to activate immediately.

Now, since I'm running this entirely in my head, I could be wrong, but it looks as though the problem may be either with your audio drivers, or the audio file itself. First step is to add this line to your code:

RuntimeError "Rocket fired!"


Put this right above PlaySound firesound, like this:

rcount1=11
RuntimeError "Rocket fired!"
PlaySound firesound


This will crash your program, with the error message, right before it can play the audio file. This is useful because if it crashes immediately when you press the button, then you know the problem is not in your code, but in the audio itself. ALSO REMEMBER TO COMMENT OUT BOTH OF THE firstrun BITS. These will mess up your diagnosis, so temporarily remove them.

Now, if the program still locks up when you fire the rocket, even with the crash-before-audio bit, then your problem is definitely still in the code. Let me know how that works out.


Ross C(Posted 2012) [#10]
How much main RAM do you have on your computer? Maybe it doesn't all fit in...


Adam Novagen(Posted 2012) [#11]
How much main RAM do you have on your computer? Maybe it doesn't all fit in...
Unless he's using the most bloated sound files in the history of PCM, I doubt very much that the few resources his game uses are taking up too much space.


Ross C(Posted 2012) [#12]
I record songs at 24 bit 96000 hz (i think?) wav, and one of my songs (3 mins odd) is 610 MB! I'm not saying you need that quality of sound, but 1 second could equal 3 MB. If your really pushed for RAM, say XP with 256 MB or 512MB. Highly unlikely I know, but just a thought, as it seems very strange. Would be nice to run it and see though.

Last edited 2012


JBR(Posted 2012) [#13]
Hi ando, when I load a few sounds and then playsound i get a delay when playing for the first time. It's like when you play any sound it loads all the sounds at one time.

I get around it by loading all the sounds and then play one of them and then no further lags!

Jim


*(Posted 2012) [#14]
One thing to consider here, is your sound driver upto date. I had a similar problem years ago and updating the sound driver sorted it. I fudged it by setting the volume to 0 and playing the sounds when loaded so when they were required they worked immediately.


Ross C(Posted 2012) [#15]
What an odd bug. Thanks for the information guys!


*(Posted 2012) [#16]
Nah not really odd, more the problem with the sound drivers I had took a while to allocate the ram and use it when the sound was first called. Something about getting the sound to play was the problem a simple driver update sorted it.


ando(Posted 2012) [#17]
Thanks for the good responses, I'm still half asleep so this could take me a while..

First, you can find it here-

http://uploading.com/files/14e322d2/BLASTED.zip/

Sorry about the 45sec wait to download...

The older version only has 1 rocket for each player.
The firesound actually plays before the freeze.

And thanks for all the advice about better coding , I've been meaning to get around to that (one day).

EDIT

I tried some of the ideas with no luck until I tried using the RuntimeError command. Thanks Adam I haven't used that one before. Although the problem isn't solved, I might have homed in on it.

I left shoot for first run active. Then started cutting and pasting the RuntimeError command at different locations starting from the tail end of the code and working forward.

The 4 second delay didn't disappear until I put the command in front of this line.

If ImagesCollide(earth,0,0,0,mask1,mask1x,mask1y,0)

Maybe it has something to do with the first time it checks for collision of the LARGE earth image ???

EDIT 2
I ended up putting this line before the main loop...

If ImagesCollide(earth,0,0,0,mask1,0,0,0):EndIf

Then I deleted both of the shoot for first run bits. The 4 second delay is still there before the gameplay starts but now I don't have to make it shoot first. :)

That's much better than what it was so I'm happy with that.
Maybe I would have avoided the freeze up if I used tiles. Not so big.
Thanks for your help.

Last edited 2012


Ross C(Posted 2012) [#18]
I'll give this a run tomorrow when I get in.


ando(Posted 2012) [#19]
Thanks 4 comments.

Last edited 2012


Adam Novagen(Posted 2012) [#20]
Oh hey this actually had more posts after I forgot about it. Downloading the ZIP now, will fart about with it tomorrow and see what I can find.