Increase your game's FPS tips
Posted: 2003-06-01
By: ArchiveBot
Viewed: 80
Filed Under:
No attachments for this post
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 Thanks psc and everyone who has if you don't fit into this category vote or don't it's just here to this is not actual source code so _______________________________________________________________________ Do Loops: They are easy enough to understand and much easier and i.e. do while 'the escape key is not pressed' or 'the level is '' loop very simple and always in your loop you need to have a do events, one If you are making a single player game than you might want At the beginning of each loop reset your TimeElapsed _______________________________________________________________________ In-line vs. modularized code Crazy enough, but (as per vbspeed.com, and _______________________________________________________________________ Variables always use option explicit and always declare your constants (but Never...never...never use Most of us have 32-bit processors a few might have the 64's A longs and singles are 32 bits, integers are not, wee, use 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 **and** VB has to treat all numbers as individual entities so (yes, you read me right, declare 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) Without Variant // _______________________________________________________________________ dynamic array fallout: should you use dynamic arrays, of course you should, should _______________________________________________________________________ Redundant coding: the last nail in the coffin Private declare function iLoveSocks( HowMany as long ) as 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 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 a practical example: Dim X As Single, Y X = Y = Z = can be optimized Dim X As Single, Y RadAngle = X = Cos(RadAngle) * Y = Tan(RadAngle) * Z = Sin(RadAngle) * When it's possible, Dim Angle class=GramE>As Single Dim class=SpellE>CosValue(720) Dim class=SpellE>SinValue(720) For Angle = 0 To class=SpellE>CosValue(Angle) class=SpellE>SinValue(Angle) Next Angle This table is To have the amazingly enough people will _______________________________________________________________________ Strings: hmm... strings in VB are really but.. if they are 3 times faster with the $ can you do realtime, what does that translate to.. class=GramE>all of you who want to do online multiplayer games... don't use strings for anything let but........if you really have to use them......... use them StrComp vs Public Sub TestOneclass=GramE>() Dim strTest1 As 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 = End If End Sub Public Sub TestTwoclass=GramE>() Dim strTest1 As String, strTest1 = strTest2 = '//Compare If class=SpellE>StrComp(strTest1$, 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 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 Next I 1000 times faster _______________________________________________________________________ Compiler options. The VB compiler has some optimizations that are somewhat then on the "Advanced There you see some unchecked boxes representing the all, without any problems in your 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 in I think that this result is amazing enough to make you try ohh and _______________________________________________________________________ True or not True According to the findings on persistantrealities.com and 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 _______________________________________________________________________ DirectX vs BitBlt, for those of you who class=SpellE>dont know it as of yet, Direct X is just plainly much if you are going to do any kind of _______________________________________________________________________ Visual Culling if your going to be doing any that certain things need to take what is more important to render?: 1. the box completely out of the 2. the front of the users 3rd 3. the actual visual aspect of the well of course the actual visual make sure that your not rendering _______________________________________________________________________ Packets (multi-player) as I've stated earlier use bits for secondly, keep it simple: does everyone need to know that hit with it, it takes off X _______________________________________________________________________ in-game-loop chat remember what i _______________________________________________________________________ / vs. weird stuff apparently / is much class=SpellE>much faster than *** update *** Also, the ***update*** 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 _______________________________________________________________________ IIF vs. If then IIF is something like 70% slower always end your if statements with i.e. none of this: if x = Z then x always expand it _______________________________________________________________________ process and thread priority does it really matter if your full grab a little process priority code does process priority matter when _______________________________________________________________________ 3D: when making a mesh realize what part of the mesh is going to can your projectile mesh be (billboard = a 1x1 plane with the _______________________________________________________________________ 1 single texture map vs. multimaps: in very many 3d programs, maya, _______________________________________________________________________ Again, you can use strings to identify just about anything _______________________________________________________________________ Walls, Ceiling, Floors: If I could ask the god of BSPs why _______________________________________________________________________ Foliage: a game maker and killer, everyone loves a lush _______________________________________________________________________ Lights: Does your mesh ever really need direct dynamic illumination, _______________________________________________________________________ Reflections: If you don't know, reflections are done with multiple scene _______________________________________________________________________ Ragdoll: Oou ragdoll, soo cool, right, well _______________________________________________________________________ Keyframe vs. Predefined bone Keyframe animation is at this _______________________________________________________________________ Bad Fog vs. Good Fog Can there be bad fog, yep, but only when it's used wrong, _______________________________________________________________________ Scripts: Wow, I can script actions and A.I. and my game can just read _______________________________________________________________________ Water: Dynamic water, a big fat no, non-dynamic but visually _______________________________________________________________________ Tiles: A wonderful thing if done right in 2D or pseuto-2D games, if _______________________________________________________________________ 3D sound: No game needs CD quality sound, really, while 50 projectiles Windows media player vs. direct sound vs Windows media player = don't use it Direct sound = easiest winMCI = _______________________________________________________________________ A.I. One of the most costly things about games, even more than _______________________________________________________________________ Questions that I've been asked: Q: Can I do my math / A: Can you get VB to Q: What is the main way A: If you only do one Q: Should I use A: From what I've Q: Is there an easy A: Not really, they Q: Should I use vSync or not? A: Develop your game Q: Why can't I even A: Do you think any Q: Can VB really make A: They both get Q: If I use inline class=SpellE>asm in vb will it make a A: If you can write Q: Can I apply a class=SpellE>photoshop like filter to my render backbuffer A: Yes and no, yes in Q: Witch is better, A: Single 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 Thanks,. G P.S. Just about
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 :) )
ever posted here, persistantrealities.com, vbspeed.com, tv3d, unreal class=SpellE>sdk, ogre sdk, and everyone else
in the world - Gandolf the Gui
and I've forgotten to mention you than tuff luck
help people out
don't say “it doesn't compile”
faster than timers.
still running'
and only one do events
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
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.
persistantrealities.com) inline code is faster than modularized code
declare all variables and never use global
never as 16-bit integers,varient,object or anything
non-32 bit)
'as object' or 'as variant' or 'as any', amazingly slow
let alone the $$$ for one
longs and singles
the speed of anything)
declare your numbers!!!
your numbers!)
: 3.5 secs
(declared as Long) : 1.9 secs
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.
long
60fps game run at 5
and anywhere that they can replace blocks of code
As Single, Z As Single
Cos(Deg2Rad(angle)) * 50
Tan(Deg2Rad(angle)) * 50
Sin(Deg2Rad(angle)) * 50
like this :
As Single, Z As Single, RadAngle
Deg2Rad(angle)
50!
50!
50!
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...
As Single
As Single
719
= Cos(Deg2Rad(Angle / 2))
= Sin(Deg2Rad(Angle / 2))
accurate at 0.5 degree. Then, to use it, you need to multiply the angle by 2.
approximation of the cosine of 45°, you need to use "class=GramE>CosValue(45 * 2)" and
voila... you get 0.707....
actually use 2 ^ 2 when hmm.... 2*2 and better yet, 2& * 2& is class=SpellE>soo much faster
slow, amazingly slow when compared to longs or singles
you have to use them than just remember to use
Mid$, Right$, Left$, Chr$
every frame, string manipulation and keep 60fps.... omg,
no, no no
alone packets, use bits for that, bit shifting and the like are amazingly
faster than strings
client side only and then use string tables in resources if you don't know how
to use text graphics
'String1=String2' .......persistantrealities.com
String, strTest2 As String
strTest2 Then
strTest2 As String
"all j00r b453 r b3l0nG 70 U5"
"ALL J00r base R Bel0NG 70 U5"
strTest2$, vbTextCompare) = 0 Then
element in the string
to the desired value
& "X"
hidden to the user. Indeed they are given in the "Compilation" tab of
the Project settings, click
optimizations" button.
different optimizations that are NOT applied by default. Normally you might be
able to check them
application. And it can really improve the speed of your application especially
for float computations.
a EXE form but without enabled optimization : 2.0 secs
a EXE with all enabled optimization : 0.02 secs !!!!
it yourself.
always remove all of your "debug.print"s
weird but it makes a difference in the compiled version let alone the class=SpellE>uncompiled
other sites True is faster than false, basically
instead of coding:
one too.
setpixel etc
quicker than BitBlt and the like, .... class=GramE>if used right :)
picture manipulation (2D,Renders,etc) in Direct X always make sure that your
using one or more backbuffers
rendering to the screen(which is just about anyone who is reading this, by this
point) than you need to understand
precedence over others in your game
users view
person mesh
users 3rd person mesh
aspect of the users 3rd person mesh
anything that the user can not see, such an easy concept but rarely used by
beginners
packets, that's first of all
this particular user has a axe that looks a certain way, of course not, they
just need to know that when they get
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.
said about real-time string manipulation..... don't class=SpellE>don't don't
"" 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)
-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
mere 400% faster with multiplication
end if
= Y
screen and at normal priority, crazy but true, yes
and bump it up when your loop is running makes a difference
your in windowed mode, so much that it might boost your game 10fps or more
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
replaced with a fixed graphic billboard, if so, do it
normal always facing a reference point with a graphic texture on it)
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!
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.
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.
scene except when it kills frame rates.
Use multiple 1x1 planes with scripted animated textures, rotated around,
for plants, trees, etc
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.
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.
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.
animation:
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
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.
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.
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.
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.
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.
winMCI
best but very complex to implement
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.
sound / etc in a separate thread to increase my frame rate?
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
to increase my frame rate?
thing from what I've written here, it should be to eliminate redundant coding.
module level variables (private), sub level variables (dim), or public
variables?
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.
way to make animated X files in my favorite 3d animation program?
all have the X exporter, some better than others.
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.
use the standard teapot primitive in my BSP project, I get errors.
of the high-end games of today use BSPs, if you do
your wrong..
games as fast as C++
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).
difference?
your own ASM than why are you writing this game in VB in the first place?
each render pass to get whatever effect?
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.
user types or 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.
completely welcome you to write your own article on it, in fact that would be
great, let alone leave your opinions here.
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.
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.