Thursday, August 18, 2011

Frustrations Per Second

(This is a long post, and mainly code & theory related.  Just FYI)

So I mentioned I wanted the game to run faster.  There were two more things I wanted to try.  The first is optimizing the backgrounds.  The default graphic type is vector art, and flipping it to a bitmap is supposed to make the game run faster.  It's a pretty simple change, you just add this code to the 1st frame of the clip:

this.cacheAsBitmap = true

I don't quite understand why this works BTW.  It has something to do with vector art requiring more resources when it needs to update (Vector art can also do more, like spin or zoom easily, so vector art is also the safer choice most times).  What I do understand is that the first time I tried it the results were just crazy good, possibly better than the quality button.  Unfortunately, subsequent applications were not so incredible, and often I couldn't see any improvement at all.

So I decided to try & be a bit more scientific about this.  I built a shitty little FPS counter by placing this code in the main loop:

newtime = getTimer()
_root.xxx = _Math.round(1000 / (newtime - oldtime))
oldtime = newtime

There's some variable declarations elsewhere of course, and _root.xxx is just the name of a global variable I use when I'm chasing errors.  With this 'tool', I then altered the background in small steps.  I also reset horde mode to spawn exactly 10 monsters just off from the hero.  And here's what I found:   (I should note that the FPS output was an integer, so the decimal point is my interpretation of how often it was flipping around.)

                                               FPS
Original Background:                    3.4
Bitmap, built from movie clips:       3.9
Bitmap, drawn from shapes:          4.0
No Background:                          5.1

Yeah, that's some shitty FPS there eh?  It's supposed to be 20.  But it does show an improvement with this bitmap idea, so at least it's a step in the right direction. 

The other thing I wanted to play with was optimizing the symbols that make up the bad guys.  I think I've mentioned this before, but there's a simplify tool that will substantially reduce the line count of your symbols (The command is in the main toolbar, under Modify, Shape, Optimize).  I played with this in the past & wasn't impressed, so this time around I cranked it to max settings.  Not as a permanent change, more to see if it would do anything at all.  In my mind, this should be very similar to what the quality toggle is doing, so I was hoping to perhaps find a good middle ground between graphics & performance.  And here's what I got:


                                          Normal Monsters            Optimized Monsters          
Original Background:                    3.4                                   4.0
Bitmap, built from movie clips:       3.9                                   4.2
Bitmap, drawn from shapes:          4.0                                   3.7
No Background:                          5.1                                   4.6

Now call me a pessimist, but the FPS numbers between the normal & optimized are virtually the same in my opinion.  There's no clear indicator that optimizing the shapes improved performance at all.  (This also tells me there's a lot of error in my data gathering ability, but that's nothing new. hahah).  In my mind, it does show a small improvement with the bitmap idea though, so I'm going to keep doing that for backgrounds.  And it looks like it doesn't matter how the backgrounds are built either, which is nice because it means I don't have to change a lot.  hehe. 

So, that's my conclusions from this.  What do y'all think?  If I toggle the quality button to low, the FPS jumps up to anywhere from 12 to 20.  So clearly the quality button is doing more than the optimize shape command.  In my opinion, that also means the bulk of the processing time is being spent on the graphics, not the code.  I reckon that's a good thing, but I'm not sure there's much I can do to fix it, short of 'draw less'. 

DirtyC101



8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Try that again: Here is a little article that may help with optimization: http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html

    Been trying to make games myself, looking through tutorials when I stumbled upon this.

    ReplyDelete
  3. Since the slowdown seems to be caused by the graphics rather than the coding, would it be feasible to simply reduce the number of enemies on screen at once? 10 enemies at once cripples the framerate, so maybe find out the maximum number of enemies you can have at once while still keeping the FPS reasonable. While this might work against the whole "horde" aspect of the game, the game will at least be more playable and (hopefully) you can still have a reasonable number of baddies on screen at the same time.

    For instance, if there're supposed to be ten guys at the same time for the area, maybe you could spawn in five to start with, and every time you beat one another guy spawns in just offscreen and makes its way towards you, and so on until all ten baddies are beaten. This could keep up the pressure on the player while keeping the pressure down on your CPU.

    Just a thought.

    ReplyDelete
  4. I'd also suggest to reduce tthe number of enemies to a reasonable amount. While the horde mode is fun in some way, I personally don't really like it. it's just too much, you cannot really fight that much enemies at once an you can't really enjoy the action because it either lags or there is just too much mobs standing around blocking the view etc.

    Maybe make less enemies, but those a bit more challenging. It's good for the FPS AND the enjoyability of the overall game :D

    ReplyDelete
  5. So some one on LOK forums had this to say.

    "It seems anonymous people can't comment on his blog. Tell this guy to look into more mainstream flash games like the ones found on Armor Games, Max Games, Kongregate, etc. They have all kinds of fancy sprites moving around all over the place, but for some reason they don't suffer any performance losses. Why is that? They must be doing something with their logic to optimize performance. Dirtyc101 will need to figure out what they're doing, and copy their techniques. Hopefully one of these developers will have some open source material to show as an example, or at least be nice enough to share performance tips. Or maybe there's a community where these guys started out, where this kind of knowledge is commonplace. In the year 2011, given how advanced the internet is, there's no excuse for anyone to not know something about modern industry or technology - you just have to know where to look"

    ReplyDelete
  6. JDS, TK241, that's almost exactly what I plan on doing in the main game. Hell, there's already a dumbed down version of it in the demo (there's exactly 10 zombies, but never more than 5 on any one screen). So I'm not expecting any problems in the main game. Horde mode is just a bonus thing I can throw in, as I have everything handy to make it already. But if I'm going to include it, I'd like it to run well too.

    Bluelight, thanks for reposting that. Yeah, I'm not happy with the way comments work right now, and I'd like to do a LARGE overhaul to the whole thing. However, I have a game to make first, and I don't feel much like policing anonymous posts at the moment. It is on my plate to fix in the future however.

    As for game speed in other Flash games, the more I look around the more I realize my sprites are too big & too complex. That's a trade I'm willing to accept in a sex game, particularly one that's nearly half done. I will dig around through Kongregate & the others again & see though. If this kind of knowledge is commonplace, I have yet to find it. Bits & pieces sure, but anytime I find it together where it could be useful, it's written using names/theory I don't understand.

    ReplyDelete
  7. Well I will dare to say it's the size, yes, the size of the sahpes your using, try to fit everything on the 100% scale and work from there, I have noticed many games that, the bigger the content (for example the size of the canvas) tend to play slower, so go for a little change and see if it works, another think I realized while making my game, is that if you have a movieclip consisting of 10 frames in whicn every frame has a shape (just a shape, meaning its not in your library), it is heavier if you have the exact movieclip but with shapes made from graphics (F8>convert to symbol), my point is, if you will use a shape at least twice, make it a symbol or a movieclip, that way flash will just have to copy that symbol instead of re-drawing the sme shape over and over. Hope that helps, keep the good work.

    ReplyDelete
  8. (On a completely unrelated note, I know it's a beta but hurr still porn. Invincibility cheat/enemy invincibility would not be frowned upon)

    ReplyDelete