Slowdown problem with a TYPE

Blitz3D Forums/Blitz3D Programming/Slowdown problem with a TYPE

Newbie31(Posted 2014) [#1]
Heyo,

I'm having a problem with types again. It's probably something simple but I can't see it. The problem is when I press [esc] to exit it takes a long time to exit the compiler. I've look through my code and I found it is caused when this type is active. Could somebody please have look and see where I went wrong?




Midimaster(Posted 2014) [#2]
Inside the main loop every tick you create tons of new ammo_indicators.

That is wrong. You should create them only once before the loop and then when add a new one, you should also only create one single new one.

Inside the loop you should only draw the ammo_indicators.

symbolic code:
Global tempshell = LoadImage( "_Sprites\ammo\04.bmp" )
CreateAmmoCounter( tempshell , weapon_magsize% )

While Not KeyHit(1)
	Cls
	
	If KeyHit( 8 ) And current_weapon% < 4
		current_weapon% = current_weapon% + 1
		CreateAmmo( tempshell) 
	ElseIf KeyHit( 7 ) And current_weapon% > 1
		current_weapon% = current_weapon% - 1
	EndIf
	
	DrawAmmoCounter( tempshell , weapon_magsize% )
	
	Text 0 , 180 , "Press [6] or [7] to change current weapon."
	Text 0 , 200 , "Current weapon : " + current_weapon%
	
	Flip	
Wend
End

;Function To Create Some Bullets In Magazine
Function CreateAmmocounter( image , magsize )
	For x = 1 To magsize
		CreateAmmo( image )
	Next
End Function


;Function To Create Single Bullets In Magazine
Function CreateAmmo( image)
		ammopic.ammo_indicator = New ammo_indicator
		ammopic\bullet_img = CopyImage ( image )
End Function


;Function To Draw Bullets In Magazine
Function DrawAmmoCounter( image , magsize )
	For x = 1 To magsize
		DrawImage ammopic\bullet_img , ImageWidth( image ) * (x-1) , 0
	Next
End Function




Floyd(Posted 2014) [#3]
You probably don't need CopyImage at all.

;Function To Create Single Bullets In Magazine
Function CreateAmmo( image)
		ammopic.ammo_indicator = New ammo_indicator
		ammopic\bullet_img = image
End Function


Blitz used to be very slow to exit when there were thousands of types allocated. That got fixed about ten years ago, with version 1.87 ( I think ).

An archive of versions is here


Newbie31(Posted 2014) [#4]
Hello,

I tried as you suggested midimaster and that work , thanks.

I'm not very good with types, but my aim is to use this in a 3d game to display the bullets remaining in a clip, and as it is a 2d function(draws onto the screen/HUD) I thought it would have to go after the renderworld and update world commands.

Full Code : FPS_Main_V_0.0.3.bb




Newbie31(Posted 2014) [#5]
Me again,

I updated my code but now the images of the shells are drawing over one another when I change ammo types.

example images
http://postimg.org/image/psjqx22o7/
http://postimg.org/image/u2yesn7rb/

I suspect this is due to where I place the "Function DrawAmmoCounter( image , magsize )" code. But I'm not good at that either.


Midimaster(Posted 2014) [#6]
I don't understand, what you are talking about.... The code you sent has no relation to the problem you descripe.

I cannot find the code part, where bullets are in a clip. I cannot find any code with bullets in HUD display.

I cannot see any code line related to the word "shell". I cannot find the function DrawAmmoCounter() nor the ammo system.

Wrong sample code?


Newbie31(Posted 2014) [#7]
Sorry, The code I posted was the environment I want to put the ammo indicator into. This is the updated code that you suggested, I've added a little bit of other stuff since. But the problem is the pictures of the diffirent ammo types are being drawn over the top of one another, like in the images I posted. I didn't mean to be confusing.



Also I had another look at my original code and had quick try without coping the image like Floyd suggested, I think that it ran faster without copyimage.... but I'll have better look later tonight.


Newbie31(Posted 2014) [#8]
I thought I might as well post the my starting code again, since I've modified it. So you could take a look and see what you think?




Midimaster(Posted 2014) [#9]
You have a CLS at the beginning of the main loop. So no image can be drawn over another.

So this only can happen, if you call DrawAmmoCounter() twice inside the main loop. Your problem are the two FOR/NEXT

Function DrawAmmoCounter( image , magsize )
	For x = 1 To magsize
		For ammopic.ammo_indicator = Each ammo_indicator


What do you want? Show all bullets of all weapons or show only bullets of the current weapon?


Type Weapon
	Field BulletImg
	Field ID
	Field CurrentMag%, MaxMag%
End


If you add a weapon you should also define its typical MaxMag% and a unique Id%.

If you change the weapon you do it like this:


Global CurrentWeapon.Weapon
.....
	If KeyHit( 2 )
		ChangeWeapon(1)
......


Function ChangeWeapon(NewID%)
	For loc.Weapon =EachIn Weapon
		If local loc\Id=NewID
			CurrentWeapon=loc
		Endif
	Next
End Function


For drawing you only have to call the DrawWeaponMag()

Function DrawWeaponMag()
        local width%=ImageWidth( CurrentWeapon\BulletImg ) 
	If CurrentWeapon\CurrentMag=0
		DrawImage reload , 0 , 0
	Else
		For local x% = 0 To CurrentWeapon\CurrentMag-1
			DrawImage CurrentWeapon\BulletImg , width* x , 0
		Next
	Endif
End Function



With this it is very easy to reload the weapon:

If MouseHit( 1 ) And CurrentWeapon\CurrentMag > 0
	CurrentWeapon\CurrentMag  = CurrentWeapon\CurrentMag  - 1
Elseif MouseHit( 2 )
	CurrentWeapon\CurrentMag = CurrentWeapon\MaxMag
EndIf



To define a new weapon:
Global.....

AddNewWeapon 1, "_Sprites\ammo\01.bmp", 6
AddNewWeapon 2, "_Sprites\ammo\02.bmp", 8
.....
ChangeWeapon(1) 
.....
While Not KeyHit( 1 )
     ....
Wend
....
Function AddNewWeapon(Id%, Path$, MaxMag%)
     local loc.Weapon= New Weapon
     loc\Id=Id
     loc\BulletImg=LoadImage(Path)
     MaskImage loc\BulletImg , 255 , 255 , 255

     loc\MaxMag=MaxMag
     loc\CurrentMag=MaxMag
End