Quick tips

BlitzMax Forums/BlitzMax Beginners Area/Quick tips

Flip an image by using negative values in SetScale:
SetScale -1,1  ' next drawn image is flipped horizontally
DrawImage character,23,40
Your turn ..

Access parts of strings with slices. Beats using Left(),Right() and Mid():
t:String = "Hello"
Print Mid(t,4,2)
Print t[3..5]
Don't know how big your array is? No worries, you can step through it using length or with EachIn:
Local yo:Int[Rand(100)]

For j = 0 To yo.length-1
	yo[j] = j
Next

For j:Int = EachIn yo
	Print j
next


For those new to Max coming from previous Blitz's, "Global"s are now much slower than "Local"s.

Technically speaking though, the reverse is true - Global's are slightly faster than in older versions of Blitz, but Locals are lightning fast now.

Locals get pushed onto the CPU registers, while globals are always stored in system ram. You can still use globals, but I think you should design tight loops that use all-locals.

Another tip: render your objects in order of image for a mild speed boost (ie. all image A then all image B). Although this isn't essential at all.

LoadAnimImage currently splits the image strip into different textures: I think that it should use a single texture -if the programmer specifies- because sometimes people will be using large anim images.

some more info to do with globals, and consts

a global in a function.
function keeptrackofsomething()
   global tracker:int
  traker:+1
  print tracker
end function

is global to that function, ie if you call the function 5 times you will get
1
2
3
4
5
printed out.
but any code outside the function can't change that varible.

function coolfeature()
 const coolfactor = 1000
 print coolfactor
end function

coolfeature()
print coolfactor


this will result in the
1000 and 0 being printed.

this lets you have vars that are only constant to a function, not the whole code.

cool eh?

Where possible, replace SetRotation() and SetScale() with SetTransform() - will compile to less code and execute a tiny amount faster!

Do shorthand coding by using ':'
from   MyRedAlien.XPos=MyRedAlien.XPos+225
to     MyRedAlien.XPos:+225

For line continuation use '..'
DrawImageRect MyBigAlien.image,MyBigAlien.X,MyBigAlien.Y,..
              MyBigAlien.Width,MyBigAlien.Height

To reduce the headache of syncing your mods create a *.bat file in your main BlitMax folder which looks like this:
Credits: BotBuilder
cd bin
bmk syncmods -u MyUserName -p MYPASSWORD


Turn frame limting on/off by using bglsetswapinterval:
Credits: Dreamora
bglSetSwapInterval 0  'OFF
bglSetSwapInterval 1  'ON


Use 'Framework' and UPX for small *.exe sizes.
This example comes out at 16.5K (Win32) after the above treatments:
framework BRL.StandardIO
Print "Hello world"


EDIT: Added to BlitzWiki - Performance Tips


Faster plotting ..

This method will speed up rendering of starfields etc ...

Instead of:
For s:star=Eachin starlist
 Plot s.x,s.y
Next

Try this:
glDisable GL_TEXTURE_2D
glBegin GL_POINTS
For s:star=EachIn starlist
  glVertex2f s.x+0.5,s.y+0.5 
Next
glEnd
glEnable GL_TEXTURE_2D
(I managed to gain an extra 7000 plots per frame for instance!)

Give it a whirl:
Framework BRL.GLMax2D
Import BRL.Random
Import BRL.System


Const TESTTIME = 1000   ' (1000 = one second)

Const width=640 , height=480
Graphics width,height,0


Cls
DrawText "Normal / Fast plot test.   PRESS ANY KEY ..." , 10,10
Flip
WaitKey ; Cls ; Flip

' normal plot
Cls ; SetColor 80,50,50
t=MilliSecs() ; ncount=0
Repeat
	Plot Rand(width),Rand(height)
	ncount:+1
Until MilliSecs()>=t+TESTTIME
Flip

' fast plot
Cls ; SetColor 50,50,80
t=MilliSecs() ; fcount=0
glDisable GL_TEXTURE_2D	
glBegin GL_POINTS
Repeat
	glVertex2f Rand(width),Rand(height)
	fcount:+1
Until MilliSecs()>=t+TESTTIME
glEnd
glEnable GL_TEXTURE_2D
Flip

' results
SetColor $ff,$ff,$ff
DrawText "Number of plots achieved in "+TESTTIME+" m/secs:" , 10,40
DrawText "Normal = "+ncount ,20,70
DrawText "Faster = "+fcount ,20,90
DrawText "Gain   = "+(fcount-ncount) ,20,110
DrawText "Any key to end ..." ,10,200
Flip
FlushMem
WaitKey

End


Ahh, never mind then...When jb changes his code I remove mine :D

(blunder corrected- thanks fb)

Not tested but I suspect DrawLine, DrawRect, etc can benefit from the same technique.

JB: That certainly makes a difference!

On my computer:

Normal: 1944455
Faster: 2543586
Gain: 599131

That's a difference of 9985 plots per frame.

I was trying to do the setscale trick and it doesn't work for me am i doing something wrong its being put in just like you had it. Also this image is a created image if that makes any diff