Simple application slow down, FPS drops

Member
Posts: 24
Joined: 2009.08
Post: #1
Hi forum,

I am new to this group, and I learned a lot about iphone/ipod programming... many thanks so far to all contributors Smile

here is my problem: my game app runs well, no slow down from begin to end... but sometimes the game FPS is going down maybe 30-50% (I have to log that, to know for sure)

and it recovers after 5-15 seconds, but sometimes it does not recover. It appears to me like there is some OS background thing going on....

OK, here are the facts:

*2D Open GL game
*I use NSTimer with 60Hz
*3 textures, 2 small PNG with transparency, 1 background *Texture 480*200 with no transparency
*I move the background 5 Pixels per second or more
*almost every second I create/release a texture2D for my update points representation
* the 2 small sprites move on the background texture using touch events
* no problems with touch events

what I notice is that occasionally the background movement slows down with the other sprite movement for no obvious reason ...

as I remember I had this slowdown also when I did not use texture2d for my points text label...


what I don't understand here is that I play the game 10 times and 1 time I start the game and anywhere during the play session the slow down occurs, like sometimes after 1 second of game play, sometimes after 10, 20, 50 seconds...

and then I have 9 games with no slow down.... and during gameplay there is not much going on, only 3 sprites...


I would expect that I have slowdown every time when I play the game, if I do something stupid with Open GL, right?

Is there some memory management in the background which is killing my FPS? Maybe there is one function call
which is using more than 16,6 ms so that timer events get rescheduled...

I will post results, if I find the problem... first I will disable points representation, because there I have creation/deletion of texture2Ds... but as I wrote, I had slowdown without that, too....

I have my game logic and drawing in the timer function,
because the game is simple, I did not seperate rendering from physics/logic, yet. But I will try that. Also I will try 120 Hz ticks, and 60 Hz drawing, then 30 Hz drawing...

thanks for any suggestions, I will post some relevant code when required....
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #2
Definitely very important to decouple your update from your draw.

Also, be sure to check for low memory in your view controller:
Code:
- (void)didReceiveMemoryWarning
{
    NSLog(@"received memory warning!");
    [super didReceiveMemoryWarning];
}

While it doesn't seem like your game uses too many resources according to your description, you never know...
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #3
thanks,

I forgot to mention that I quit the game and then start the game from my iphone (so every game session is new from start... I did not implement a restart new game feature)... and the strange thing is that even though the game is freshly loaded I get the slowdown after 5-10 seconds of play... but only in 1 of 10 games (approx.)

I will try to de-couple logic from drawing (I re-read the NSTimer thread many times Wink )

like this?


myTick( ) // at 200 Hz
{
tNow= getCurrentTime() ...
delta= compareWithLastTime(tNow)...

UpdateLogic(delta);

timeCounter+= delta;

if ( timeCounter >= 16.67 ) //ms only at 60 FPS
DrawObjects();
timeCounter= 0.0f;

}


is this de-coupling? My logic gets more updates than my drawing code?... looking forward to get home and experimenting... Smile I will take everything out except the scrolling background, and will see if I get those slow downs...

btw: my PNG is not a square of 2... but it might that texture2d will handle that, right?


i think I am doing something very wrong here, because I know many complex 2D games which run fine .. and my game is so simple (just a guy jumping over hurdles with scrolling background)
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #4
ok, I found it!

I have problems with my music playing. Without the AVAudioplayer, I get solid 200 ticks per second from my timer and can update my drawing with 60 FPS.


My FPs was getting worse, when I was switching music during play...
I think I have to use audio queueing or something like that. I have to investigate how to play music without AVAudioplayer.

BTW: I get FPS problems when I use the IPod music player (I think it might be internally the same AVAudioplayer...)... I am happy that it is not the OpenGL stuff or the NSTimer causing trouble...

does anyone else ran into AVAudioplayer problems?


NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:
[[NSBundle mainBundle] pathForResource:@"beet60" ofType:@"mp3"]];

m_avp = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil];
m_avp.volume = 0.7;

[fileURL release];
[m_avp prepareToPlay];
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #5
Have you run it through leaks and profiled it with shark when the slowdown happens? Those 2 things are the sure fire way to find out whats going on.
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #6
I am going to use GBMusicTrack and I knock on wood that it will not cause a slow down... I have IPod Touch with OS 3.0 and in a different thread there was mentioned that with Os 3.0 AVAudioplayer is using software rather than hardware for playing music... for playing background music should I use Open AL or AudioQueues or something else? Too bad I have problems with AVAudioPlayer... hm.. maybe I should test with a WAV file, too.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #7
I would imagine GBMusicTrack will likely offer the same performance as AVAudioPlayer, but I'd be interested to hear if there is in fact any performance difference with 3.0, since I haven't tested it myself.

I'm guessing something else is afoot here.
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #8
ok, I did some things wrong here with AVAudioplayer (btw: maybe we should change the Thread title to: FPS problems with AVAudioplayer, OS 3.0)

I have to use kAudioSessionCategory_SoloAmbientSound so that the hardware decoding is used in OS 3.0 (can't test with 2.2 anymore)

I have 3 mp3 files, which I was changing during gameplay on the fly... changing the music caused the FPS drops, because I preloaded them all at the same time at the beginning of my game using [ ... prepareToPlay] for all of them one after the other....

as I understand prepareToPlay is preparing/preloading a buffer for the music. so there is a conflict when you are calling prepareToPlay for different files when you are not playing them immediate after the call.

I also used [... pause] when I was switching music. That was the also a problem, because when I paused one file and wanted to play the other file I had the FPS drop.... probably because the hardware decoding was locked by the first avp player...

but when I used [... stop] instead of [... pause] and then played the other file (calling prepareToPlay immediately before playing),everything was (almost) fine. Frame rate is stable during gameplay and when switching music files...

I used [pause] because I wanted to resume the music at the paused position, when switching back, but [stop] does not reset the music position, so that is really good...

I noticed the FPS drops when printing them out on screen, before that it was less obvious, when they started. But I still have one problem left...

it sounds similar to my first post, though.

I get FPS drops during gameplay not related to switching music occasionaly... and they don't recover. Instead of 200 NSTimer ticks, I get suddenly less and less until they stay at 40 or 60 usually, but not less than 40. Strange, but I will investigate... I don't get these drops when I switch the music completely off. And usually I get the drops in the first 5-10 seconds after game start...
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #9
sad, but same problem with GBMusic ... if I use an uncompressed WAV file I have no FPS problems... unfortunately I can't ship the game with 20MB of data Wink

so I will try to use a thread instead of NSTimer ... maybe I can prevent the FPS drop there?

cheers, George
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #10
Newbrof Wrote:sad, but same problem with GBMusic ... if I use an uncompressed WAV file I have no FPS problems... unfortunately I can't ship the game with 20MB of data Wink

Aha! Now I know what your problem is. You cannot preload more than one track at a time. You have to completely release each track as you are finished with it. This is just the way it works with compressed audio on iPhone when using Audio Queues (which is presumably what AVAudioPlayer uses as well). More than one at a time causes huge performance problems at the very least, and can freeze the device at worst. There is no known way around this, other than to decode your own in software, using something like ogg/vorbis.

One possible trade-off is to use IMA4, which is not decompressed in hardware, but is very efficiently decoded by the CPU. The disadvantage is that you probably only get about a 50% reduction in file size.
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #11
Quote:You have to completely release each track as you are finished with it

you mean release AVPPlayer object, I guess? Then I would not be able to resume at the last position, I guess? I had to reload the complete file... this I will investigate.

Good news: It is not a NSTimer problem, because these calls slow down by a factor of 20 times:

glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];

rendering to the screen is slow due to hardware/cpu load... ? all the prerendering /logic stuff was not slowed down.

I found another workaround. I will use uncompressed WAV, but 16 Bit Mono ... then my MIDI/WAV file is less than 2 MB... and no slowdown. The sound is OK... it is a MIDI tune, so no problem here for this game.... IMA4 I would have to test next time.


many thanks, learned a lot, especially that the IPhone is not a PC with vast resources :-)
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #12
Newbrof Wrote:you mean release AVPPlayer object, I guess? Then I would not be able to resume at the last position, I guess?

Yes, release the AVAudioPlayer object (or GBMusicTrack object, if you're using that). I know it's not very convenient, but there's no way around it that I am aware of.

I don't recall if AVAudioPlayer allowed seek. I know you can with GBMusicTrack though, if you add a little extra code to it. The GBMusicTrack thread has some code that I think Bence contributed for seeking, sorry I don't have a link handy to the specific post.
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #13
ok, I did some more tests (always 1 file loaded, complete unloading and switching to other file)

.aif compressed file -> 40 FPS -> 40 FPS
.wav uncompressed -> 180 FPS -> 60 FPS
.mp3 -> 180..40 FPS -> 40 FPS

interesting, that the wav file is running with 180 FPS (timer ticks), when changing going down to 60 FPS.. but since the Ipod does not draw faster than 60 FPS, this is OK for me ( with 40 FPS you see niticable slow down and stuttering of fast moving sprites).

I don't know what to make of these numbers... I suspect, that the compressed files do not use hardware decoding... and why is WAV going down to 60...? 60 is sufficient, but what does it mean?

btw: I will go the WAV route, so that I can release Beethoven Jumpan or Avoid Hitting Piano soon :-)
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.08
Post: #14
In an attempt to have overlapping sounds, and music, I have extensively played around with sound for a while, and found this resource from Apple Dev Con:

Preferred Audio Formats in iPhone OS

For uncompressed (highest quality) audio, use 16-bit, little endian, linear PCM audio data packaged in a CAF file. You can convert an audio file to this format in Mac OS X using the afconvert command-line tool.

/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT}

The afconvert tool lets you convert to a wide range of audio data formats and file types. See the afconvert man page, and enter afconvert -h at a shell prompt, for more information.

For compressed audio when playing one sound at a time, and when you don’t need to play audio simultaneously with the iPod application, use the AAC format packaged in a CAF or m4a file.

For less memory usage when you need to play multiple sounds simultaneously, use IMA4 (IMA/ADPCM) compression. This reduces file size but entails minimal CPU impact during decompression. As with linear PCM data, package IMA4 data in a CAF file.
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #15
Hello AnotherJake, I have a promo code for my recent game Avoid Hitting Piano. Since you helped me a lot, I would like to give it to you, but I don't know where to put it.

ok, this one goes to you

XMX4ALKJYAM9

hey folks, here are other codes to use... please leave above code for Mr. Jake :-)

( dammit, itunes connect does not work... I will provide other codes later)

HYP7WT74Y9Y4
ER6RAE3MJFNY
WXNKY44LEX4T
7NYX6AJLR3R7
KK96WMF3A4JA
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  XML too slow, best way to go binary markhula 9 8,399 Jan 14, 2011 06:58 AM
Last Post: markhula
  Why is my iPad game so slow? Applewood 53 29,059 Jan 3, 2011 04:54 PM
Last Post: Applewood
  Simple 3D Game Engine(well not that simple) geoface 9 9,005 Apr 14, 2010 04:08 AM
Last Post: sio2interactive
  emulator slow when rendering from a second thread captainfreedom 1 2,928 Jan 30, 2010 05:05 PM
Last Post: ChrisD
  Chipmunk is too slow? jaguard 14 8,242 Jan 22, 2009 04:49 AM
Last Post: jaguard