Search Tools Links Login

Increase your game's FPS tips


Increase your game's FPS, coding tips, game tips, mesh tips, etc, might help you out a bit, i know they all greatly affected the way i code VB games, vote if you want, hope it helps

Original Author: George E.

Code


Version .01a - update #1


 


A few tips to speed up your games frame rate a bit, it's
amazing that some of this makes so much of a difference.style='mso-spacerun:yes'>  Just off the top of my head or taken from
sources, maybe you agree maybe you don't, they all work for me, and I've run into
a lot of posts here on PSC that say "my frame rate is so low" etc so
here's my two cents (that's right no spell check :) )


 


Thanks psc and everyone who has
ever posted here, persistantrealities.com, vbspeed.com, tv3d, unreal class=SpellE>sdk, ogre sdk, and everyone else
in the world - Gandolf the Gui


if you don't fit into this category
and I've forgotten to mention you than tuff luck


 


vote or don't it's just here to
help people out


 


this is not actual source code so
don't say “it doesn't compile”


_______________________________________________________________________


Do Loops:


 


They are easy enough to understand and much easier and
faster than timers.


i.e.


do while 'the escape key is not pressed' or 'the level is
still running'


''


loop


very simple


and always in your loop you need to have a do events, one
and only one do events


If you are making a single player game than you might want
to think about capturing the user input and only executing your loop when input
has fired, this method is death (bad thing) on multiplayer games


 


At the beginning of each loop reset your TimeElapsed
Variable, with that you have an exact count and are able to do things like
physics, motion of any kind, and many other things.style='mso-spacerun:yes'>  Truly much easier than a timer once you get
the hang of it.


_______________________________________________________________________


In-line vs. modularized code


 


Crazy enough, but (as per vbspeed.com, and
persistantrealities.com) inline code is faster than modularized code


 


_______________________________________________________________________


Variables


 


always use option explicit and
declare all variables and never use global


always declare your constants (but
never as 16-bit integers,varient,object or anything
non-32 bit)


 


Never...never...never use 
'as object' or 'as variant' or 'as any', amazingly slow


 


Most of us have 32-bit processors a few might have the 64's
let alone the $$$ for one


A longs and singles are 32 bits, integers are not, wee, use
longs and singles


 


i.e.


dim i as integer


for i = 0 to 1000


next i


 


should read:


dim i as long


for i = 0 to 1000&


next i


(no, the i after next .. "next i" doesn't change
the speed of anything)


 


**and**


 


VB has to treat all numbers as individual entities so
declare your numbers!!!


(yes, you read me right, declare
your numbers!)


 


   Typestyle='mso-spacerun:yes'>      Character


   Integerstyle='mso-spacerun:yes'>   No character


   Longstyle='mso-spacerun:yes'>      &


   Singlestyle='mso-spacerun:yes'>    !


   Doublestyle='mso-spacerun:yes'>    #


   Stringstyle='mso-spacerun:yes'>    $


//


A=5


B=5


C=(A+B) * 2


 


vs.


 


dim A as long


dim B as long


dim C as long


 


A=5&


B=5&


C=(A+B)*2&


 


  With Variant (no Declare)
: 3.5 secs


  Without Variant
(declared as Long) : 1.9 secs


//


 


 


_______________________________________________________________________


dynamic array fallout:


should you use dynamic arrays, of course you should, should
you keep appending to the uboundstyle='mso-spacerun:yes'>  (ubound(class=SpellE>myArray)) +1 for things such as projectiles and the like,
the answer is no, iterate through your array, if an object in your array is
past the state of usefulness i.e. has hit something, reuse it, remember, the
larger an array the slower it is, if your character just spent 20 rounds and
they are all flying through the air and he/she keeps firing, than yes, append
to the ubound, iterating is slow when compared to
appending constantly, but when you finally do have to iterate the array and now
you've appended to it 100,000 times and poof your game just halts you'll
understand what I'm talking about.


_______________________________________________________________________


 


Redundant coding: the last nail in the coffin


 


Private declare function iLoveSocks( HowMany as long ) as
long


iLoveSocks = HowMany ^ 45&


End function


 


Private sub blah()


Dim A as long


Dim B as long


Dim C as long


 


a = iLoveSocks(1000)


B = iLoveSocks(1000)


C = iLoveSocks(1000)


End sub


 


is utterly and completely wrong and will figuratively make a
60fps game run at 5


It should read


 


Private sub blah()


Dim A as long


Dim B as long


Dim C as long


Dim TempVal as long


 


TempVal = iLoveSocks(1000&)


 


A = TempVal


B = TempVal


C = TempVal


 


end sub


 


amazingly quicker


use temporary variables everywhere
and anywhere that they can replace blocks of code


a practical example:


 


   Dim X As Single, Y
As Single, Z As Single


   X =
Cos(Deg2Rad(angle)) * 50


   Y =
Tan(Deg2Rad(angle)) * 50


   Z =
Sin(Deg2Rad(angle)) * 50


 


  can be optimized
like this :


 


   Dim X As Single, Y
As Single, Z As Single, RadAngle


   RadAngle =
Deg2Rad(angle)


   X = Cos(RadAngle) *
50!


   Y = Tan(RadAngle) *
50!


   Z = Sin(RadAngle) *
50!


 


  When it's possible,
precompute values for expansive operations and organize them in tables. This is
usually used for the trigonometric operations Cos, Sin, Tan, class=SpellE>Atn etc...


  


   Dim Angle class=GramE>As Single


   Dim class=SpellE>CosValue(720)
As Single


   Dim class=SpellE>SinValue(720)
As Single


   For Angle = 0 To
719


        class=SpellE>CosValue(Angle)
= Cos(Deg2Rad(Angle / 2))


        class=SpellE>SinValue(Angle)
= Sin(Deg2Rad(Angle / 2))


   Next Angle


 


   This table is
accurate at 0.5 degree. Then, to use it, you need to multiply the angle by 2.


   To have the
approximation of the cosine of 45°, you need to use "class=GramE>CosValue(45 * 2)" and
voila... you get 0.707....


 


amazingly enough people will
actually use 2 ^ 2 when hmm.... 2*2 and better yet, 2& * 2& is class=SpellE>soo much faster


 


 


_______________________________________________________________________


 


Strings:


 


hmm... strings in VB are really
slow, amazingly slow when compared to longs or singles


but.. if
you have to use them than just remember to use 
Mid$, Right$, Left$, Chr$


they are 3 times faster with the $


can you do realtime,
every frame, string manipulation and keep 60fps.... omg,
no, no no


what does that translate to.. class=GramE>all of you who want to do online multiplayer games...


don't use strings for anything let
alone packets, use bits for that, bit shifting and the like are amazingly
faster than strings


but........if you really have to use them......... use them
client side only and then use string tables in resources if you don't know how
to use text graphics


 


StrComp vs
'String1=String2' .......persistantrealities.com


 


Public Sub TestOneclass=GramE>()


    Dim strTest1 As
String, strTest2 As String


   


    strTest1 = class=SpellE>UCase$("all j00r b453 r b3l0nG 70 U5")


    strTest2 = class=SpellE>UCase$("ALL J00r base R Bel0NG 70 U5")


   


    '//Compare


    If strTest1 =
strTest2 Then


    End If


End Sub


 


Public Sub TestTwoclass=GramE>()


    Dim strTest1 As String,
strTest2 As String


   


    strTest1 =
"all j00r b453 r b3l0nG 70 U5"


    strTest2 =
"ALL J00r base R Bel0NG 70 U5"


   


    '//Compare


    If class=SpellE>StrComp(strTest1$,
strTest2$, vbTextCompare) = 0 Then


    End If


End Sub


 


100% faster compares weee


 


variable length strings class=SpellE>vs fixed length strings


 


Private VariableLengthString class=GramE>As String


Private FixedLengthString class=GramE>As String * 65526


 


        For I = 1 class=GramE>To lngIterations '//Run test


            '//Set an
element in the  string
to the desired value


            Mid$(class=SpellE>FixedLengthString, I) = "X"


        Next I


 


       For I = 1 class=GramE>To lngIterations '//Run test


            '//class=GramE>Concatenate a character to the variable length string


            class=SpellE>VariableLengthString = VariableLengthString
& "X"


        Next I


 


1000 times faster


 


_______________________________________________________________________


 


Compiler options.


 


 


The VB compiler has some optimizations that are somewhat
hidden to the user. Indeed they are given in the "Compilation" tab of
the Project settings, click


then on the "Advanced
optimizations" button.


There you see some unchecked boxes representing the
different optimizations that are NOT applied by default. Normally you might be
able to check them


all, without any problems in your
application. And it can really improve the speed of your application especially
for float computations.


 


  Small class=GramE>example :


  Dim class=SpellE>i As Long


  Dim A class=GramE>As Single


  For class=SpellE>i = 1& To 10000000&


     A = class=SpellE>Cos(class=SpellE>i)


  Next class=SpellE>i


 


  In VB class=GramE>IDE : 2.5 secs


  in
a EXE form but without enabled optimization : 2.0 secs


  in
a EXE with all enabled optimization : 0.02 secs !!!!


 


I think that this result is amazing enough to make you try
it yourself.


ohh and
always remove all of your "debug.print"s
weird but it makes a difference in the compiled version let alone the class=SpellE>uncompiled


 


_______________________________________________________________________


True or not True


According to the findings on persistantrealities.com and
other sites True is faster than false, basically
instead of coding:


 


Dim isTheSkyRed as Boolean


 


If isTheSkyRed = false then


‘ ‘ ‘ ‘


End If


 


You would write


 


If isTheSkyRed = Not True then


‘ ‘ ‘ ‘


End If


 


Or


 


If Not isTheSkyRed then


‘ ‘ ‘ ‘


End If


 


Both examples are faster than saying False, I went “class=SpellE>hugh?!?” the first time I saw that
one too.


_______________________________________________________________________


 


DirectX vs BitBlt,
setpixel etc


 


for those of you who class=SpellE>dont know it as of yet, Direct X is just plainly much
quicker than BitBlt and the like, .... class=GramE>if used right :)


if you are going to do any kind of
picture manipulation (2D,Renders,etc) in Direct X always make sure that your
using one or more backbuffers


 


_______________________________________________________________________


 


Visual Culling


 


if your going to be doing any
rendering to the screen(which is just about anyone who is reading this, by this
point) than you need to understand


that certain things need to take
precedence over others in your game


what is more important to render?:


1. the box completely out of the
users view


2. the front of the users 3rd
person mesh


3. the actual visual aspect of the
users 3rd person mesh


 


well of course the actual visual
aspect of the users 3rd person mesh


 


make sure that your not rendering
anything that the user can not see, such an easy concept but rarely used by
beginners


 


_______________________________________________________________________


 


Packets (multi-player)


 


as I've stated earlier use bits for
packets, that's first of all


secondly, keep it simple:


 


does everyone need to know that
this particular user has a axe that looks a certain way, of course not, they
just need to know that when they get


hit with it, it takes off X
damage.  The same goes for two players
half a world away from each other, they absolutely don't need to know when the
other one opens a door or jumps or almost anything for that matter, except
global game changes, what's changing in the field of view of the users
character/camera is all that matters.


 


_______________________________________________________________________


 


in-game-loop chat


 


remember what i
said about real-time string manipulation..... don't class=SpellE>don't don't


 


_______________________________________________________________________


 


/ vs. weird stuff


apparently / is much class=SpellE>much faster than


*** update ***


Also, the
"" and "/" are completely different operations. One is
regular division, resulting in a decimal, and the other returns an integer. class=GramE>for
example: X Y = Int(X / Y)


***update***
-much thanks to href="http://www.planet-source-code.com/vb/feedback/EmailUser.asp?lngWId=1&lngToPersonId=2274451481&txtReferralPage=http%3A%2F%2Fwww%2Eplanet%2Dsource%2Dcode%2Ecom%2Fvb%2Fscripts%2Fshowcode%2Easp%3FtxtCodeId%3D57743%26lngWId%3D1">class=SpellE>Gandolf_The_GUI


and


x / 1 is much faster than class=SpellE>Cint(number)


40% faster


 


_______________________________________________________________________


 


Division vs. Multiplication:


(persistantrealities.com)


Public Sub TestOneclass=GramE>()


    Dim class=SpellE>lngReturn As Long


    Dim Action class=GramE>As Long


    Action = 0.25


    class=SpellE>lngReturn = 10 * Action


End Sub


 


Public Sub TestTwoclass=GramE>()


    Dim class=SpellE>lngReturn As Long


    Dim Action class=GramE>As Long


    Action = 4


    class=SpellE>lngReturn = 10style='mso-spacerun:yes'>  Action


End Sub


 


ohh a
mere 400% faster with multiplication


 


_______________________________________________________________________


 


IIF vs. If then


IIF is something like 70% slower


always end your if statements with
end if


i.e. none of this: if x = Z then x
= Y


always expand it


 


_______________________________________________________________________


 


process and thread priority


 


does it really matter if your full
screen and at normal priority, crazy but true, yes


grab a little process priority code
and bump it up when your loop is running makes a difference


does process priority matter when
your in windowed mode, so much that it might boost your game 10fps or more


 


_______________________________________________________________________


 


3D:


 


when making a mesh realize what part of the mesh is going to
be seen most and capitalize on that, what does low poly really mean, it means
however many polys you can use and still keep 60fps class=SpellE>vsynched with everything else in the scene, don't let
anyone tell you that it means 100 poly characters or anything else for that
matter, balance it, if you really want to be cool about it either use 3
different complexity models for items shown dependant on camera distance (DOF
depth of field), but the best way is to incorporate a LOD, Level of Detail,
component to your render queue, look it up


 


can your projectile mesh be
replaced with a fixed graphic billboard, if so, do it


(billboard = a 1x1 plane with the
normal always facing a reference point with a graphic texture on it)


_______________________________________________________________________


 


1 single texture map vs. multimaps:


in very many 3d programs, maya,
3dsmax, milkshape you can use something called class=SpellE>multimaps and they end up making it much easier in the 3d
program, but when you transfer your mesh over as mdl,x,3ds,
etc the materials start to pile up, basically if your program has to load 10
one MB tex files versus 1 two MB file obviously the
latter is preferable, UNWRAP all your meshes and bake them out!


_______________________________________________________________________


 


Again, you can use strings to identify just about anything
such as the mesh title, name, texture name, file name, etc, if you've read
through this whole thing than you know you'll get the shame sign from me if you
do, let alone a low frame rate.  If you
need to know who “owns” this particular projectile or whatever, just use a class=GramE>number(long) to do so, it might make your coding a bit
harder but it will save a frame or 10 in the long run.


_______________________________________________________________________


 


Walls, Ceiling, Floors:


If I could ask the god of BSPs why
you have to use boxes instead of planes for your walls, ceiling, and floors
than I would, it is for this reason only that I don't use bsps
(there are others but I digress) .style='mso-spacerun:yes'>  Use planes whenever possible it might be a
little tricky for you to line them up correctly but in the long run, when you
take a 6 sided box versus a 1 sided plane and multiply that by an entire level
that might come out to 1000 or more polys (actually
displayed at one time (culling, fov, class=SpellE>dof) ) saved.


_______________________________________________________________________


 


Foliage: a game maker and killer, everyone loves a lush
scene except when it kills frame rates. 
Use multiple 1x1 planes with scripted animated textures, rotated around,
for plants, trees, etc


_______________________________________________________________________


 


Lights:


Does your mesh ever really need direct dynamic illumination,
probably not, pre-compute your light maps for everything, and use a shadow plane
for your character, 
for
we all know a 3d game is nothing without shadows, if you
really must use dynamic lighting, remember that its extremely costly and only
use it on objects visible to the user. 
At one time I had the thought that I could use one dynamic light to
constantly be on the user character and to project the shadow for it as
well.  It worked, but at the cost of a
base 5 frames per second, so I had to take that back from somewhere else, it
ended up being the sound, and that ended up being bad so I went to the A.I. and
lowered that, it is a perpetual balancing act, remember that.


_______________________________________________________________________


 


Reflections:


If you don't know, reflections are done with multiple scene
cameras, and multiple cameras means multiple rendering, but there is help,
rather than halving your frame rate, only have the reflection camera render
every 3rd frame or so, depending on your tastes, buffer it out and drop
the resolution on the buffer and flip it back, you lose the perfect
reflections, but you keep your player playing.


_______________________________________________________________________


 


Ragdoll:


Oou ragdoll, soo cool, right, well
if you didn't know, any kind of physics, solid, sliding, cloth, softbody, and
especially ragdoll, take an immense amount of processing power, the work around
to this is absolutely not to use the actual mesh, yep, use invisible extremely
basic meshes, or even better than that pure variables and math to do your
interactions.


_______________________________________________________________________


 


Keyframe vs. Predefined bone
animation:


Keyframe animation is at this
point basically very slow to use by just about any engine let alone one
optimized for it.  Use predefined bone
animation and with most 3d character editors you can even assign what
animations are blended together, easy as pie


_______________________________________________________________________


 


Bad Fog vs. Good Fog


Can there be bad fog, yep, but only when it's used wrong,
fog is basically there to either set an atmosphere for a game or to exclude
objects from the render, hint, hint, “boy its foggy,
but wow I'm getting a constant 60 frames per second” is what you balance
against.  And really, and I can't express
this enough, don't forget to exclude the completely fogged in objects from the
render.  Exponential vs. Linear, like
anything, exponential is prettier but much more costly… balance.


_______________________________________________________________________


 


Scripts:


Wow, I can script actions and A.I. and my game can just read
it from external files, super cool, and uuber slow,
scripts=strings=death, hardcode it unless the point of your game is to let the
user mess with it, don't be lazy.


_______________________________________________________________________


 


Water:


Dynamic water, a big fat no, non-dynamic but visually
appealing water, of 
course
.  Does your water
need to ebb and flow, if so remember that the complexity of the “land” affects
how good the water looks when it ebbs and flows, not the complexity of the class=GramE>water.  Pretty
waterfalls, yep, they're great, but only if you use pre-rendered scripted
animated textures with alpha channels on layered low poly planes, believe me,
it looks just like you really used the crazy 3000 particles to do it, and no
you can not do 3000 particles and still well, do anything.


_______________________________________________________________________


 


Tiles:


A wonderful thing if done right in 2D or pseuto-2D games, if
you don't know what a matrix is read for a week until you dream about them,
because if your reading your tile levels from the registry let alone class=SpellE>ini files than I bet your scratching your head and
wondering why your level loads so slowly.


_______________________________________________________________________


 


3D sound:


No game needs CD quality sound, really, while 50 projectiles
are coming at your character they wont really notice
the difference between a bit rate of 256 or 92 or 32bit audio vs. 8 or 16
bit.  As a general rule, more low quality
3d sounds are always preferable to a very few very high quality 3d sounds.style='mso-spacerun:yes'>  Music is exactly the same, keep it low
quality unless you actually have to. 


Windows media player vs. direct sound vs
winMCI


Windows media player = don't use it


Direct sound = easiest


winMCI =
best but very complex to implement


_______________________________________________________________________


 


A.I.


One of the most costly things about games, even more than
polygon count, yep, if your going to use neural networks, genetic class=GramE>algorithms , A* path finding, or any of the multitude of
A.I. out there, you just can not be liberal with it.style='mso-spacerun:yes'>  If a computer controlled character will only
end up dying after 10 seconds of game time does it really need to be able to
die well and do college level calc 3 hmm I don't believe so.style='mso-spacerun:yes'>  Use it where it is necessary.


_______________________________________________________________________


 


Questions that I've been asked:


 


Q:  Can I do my math /
sound / etc in a separate thread to increase my frame rate?


A:  Can you get VB to
run stable with more than one thread, if you can go for it, if you can't don't,
will it make a difference, nope


 


Q:  What is the main way
to increase my frame rate?


A:  If you only do one
thing from what I've written here, it should be to eliminate redundant coding.


 


Q:  Should I use
module level variables (private), sub level variables (dim), or public
variables?


A:  From what I've
seen if you can put everything into one module (bas) with a sub main, and
create your form at runtime(windowed) or just do full screen it is really going
to help speed things along, it will also make your coding a headache though.style='mso-spacerun:yes'>  Remember, inline is faster.


 


Q:  Is there an easy
way to make animated X files in my favorite 3d animation program?


A:  Not really, they
all have the X exporter, some better than others.


 


Q: Should I use vSync or not?


A:  Develop your game
without it, and if you can get your frame rate higher than the standard 60 than
go ahead and implement it, just keep in mind that some people out there use a
refresh rate of 110 or above, can your game pull that off, why even ask,
implement code to switch the users refresh rate to 60 when your game starts,
use vsync, and set it back when they exit, they'll
never know, and your game will run super smooth on just about anyone's decent
or better system.


 


Q:  Why can't I even
use the standard teapot primitive in my BSP project, I get errors.


A:  Do you think any
of the high-end games of today use BSPs, if you do
your wrong..


 


Q:  Can VB really make
games as fast as C++


A:  They both get
compiled into ASM, but VB f'ks around a bit too much
to make it tight ASM, with some of the things that I've previously stated you
can tighten it up a bit (declared numbers, etc).


 


Q:  If I use inline class=SpellE>asm in vb will it make a
difference?


A:  If you can write
your own ASM than why are you writing this game in VB in the first place?


 


Q:  Can I apply a class=SpellE>photoshop like filter to my render backbuffer
each render pass to get whatever effect?


A:  Yes and no, yes in
the fact that if your using a DX9 script to do it pre-flip than yes, if your
using just about anything else, nope, not even the super cool motion blur,
script or bust.


 


Q:  Witch is better,
user types or single variables


A:  Single variables
are quicker, but unless you want your game to be 10 years in the making at the
gain of 1/100 a frame a second than just use user types, just remember to type
them as 32 bit variables.


 


_______________________________________________________________________


 


Like I've said before, this is all just opinion and class=GramE>conjecture, it works for me, believe it or not.


 


If you think that you have better ideas on any of this than I
completely welcome you to write your own article on it, in fact that would be
great, let alone leave your opinions here.


 


Thanks,.


G


 


P.S.  Just about
everything I know about all of this I learned or started off on the, well one
of the feet, here on PSC, yep, I'm addicted.

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 100 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.