Brutal slowdown with for loop..

Blitz3D Forums/Blitz3D Beginners Area/Brutal slowdown with for loop..

ErikT(Posted 2003) [#1]
I need to create a healthbar for my game that decreases along with a player's health, so I came up with this piece of code which is called once every game loop. It works as it should, but slows the game down horribly. Does anyone know what's wrong with this method?

The 'HealthDisplay_Handle' is a 1x8 res image which makes up the healthbar. 236 is the total possible length in pixels of the healthbar.

[CODE]
For x = 1 To 236
For player1.entity = Each entity
P1_HealthUnit# = 236/player1\MaxHealth
P1_TranslatedHealth# = player1\health# * P1_HealthUnit#

If x <= P1_TranslatedHealth#
DrawImageBB HealthDisplay_Handle,(P1_PicX+36)+x,(P1_PicY-8)+ScreenLimitTop
EndIf
Next
Next
[/CODE]


fredborg(Posted 2003) [#2]
That's a hopeless way of doing it :) Try something like this:
;
; Assuming you are drawing your healthbars after drawing a background or clearing the screen
;
P1_HealthUnit# = 236/player1\MaxHealth
P1_TranslatedHealth# = player1\health# * P1_HealthUnit#

; Dimensions of the cut off of the health bar
ileft   = P1_TranslatedHealth
iwidth  = 236-P1_TranslatedHealth
itop	= 0
iheight = 16

; Create a temporary image to hold the cut off
tmpimg = CreateImage(iwidth,iheight)
CopyRect ileft,itop,iwidth,iheight,0,0,BackBuffer(),ImageBuffer(tmpimg)

; Draw the healthbar
DrawImage EntireHealthBar,0,0

; Draw the background on top of the healthbar
DrawBlock tmpimg,ileft,itop

; Delete the temporary image
FreeImage tmpimg
Haven't tested it, so it might need some adjustments!


ErikT(Posted 2003) [#3]
And here I thought I was being clever. I'm getting all nervous about the rest of my code now :)

The code works fine except for some coordinate weirdness, nothing I can't figure out tho'. Thanks.


Ross C(Posted 2003) [#4]
You could also use the drawimageblock command.


Curtastic(Posted 2003) [#5]
DrawBlockRect


fredborg(Posted 2003) [#6]
Hey, that's even better :)