Strange behaviour/bug, need your help

Blitz3D Forums/Blitz3D Programming/Strange behaviour/bug, need your help

Hi everyone,

I ran into a strange behaviour that drives me crazy. I've checked my code again and again the last 4 hours and I don't understand it. Could you do me a favour, run the code on your machine and see for yourself. It's part of my surface-manager I'm currently working on - i removed everything except the parts where my problem occurs.

All this code does is creating some test-quads and storing them for later use. Everytime you press <Space> a quad will be created. And now the weird thing: The fourth quad makes the three previous disappear, the fifth makes them appear again. Same happens with 6th, 9th and 11th quad (didn't tested further). I cannot find any error in my code and this happens either with debugmode on or off. BUT if you uncomment the line with "debuglog" in the mainloop (and turn debugmode on), this bug will not show up. Here's the code:

Type SM_Entity
	Field msh%
	Field srf%
	Field NewObj.SM_Object	; points to object we process
	Field NewObjVt%			; Stores Vertex-IDs for NewObj until SM_EndObject
	Field NewObjVtPtr%		; "Pointer" to last offset
	Field NewObjTris%		; stores Tri-IDs for NewObj until SM_EndObject
	Field NewObjTrisPtr%	; "Pointer" to last offset
End Type

Type SM_Object
	Field par.SM_Entity
	Field Tris%				; Int-Bank of tris belonging to this object
End Type

Function SM_CreateEntity%(parent%=0,entity_fx%=2)
;returns new entity
	Local sm.SM_Entity=New SM_Entity
	sm\msh=CreateMesh()
	sm\srf=CreateSurface(sm\msh)

	EntityFX (sm\msh,entity_fx)
	NameEntity (sm\msh,Handle(sm))
	Return sm\msh
End Function

Function SM_BeginObject%(smid%,estimated_vt%=0,estimated_tri%=0)
;prepare a new object to add vertices/triangles
;RETURNS: handle of SM_Object
	Local sm.SM_Entity=Object.SM_Entity(EntityName(smid))
	If sm=Null Then RuntimeError ("SM_BeginObject: Invalid SM_Entity")
	If sm\NewObj<>Null Then RuntimeError ("SM_BeginObject: Can't create two objects at once. Call SM_EndObject first")

		sm\NewObj=New SM_Object
		sm\NewObj\par=sm
		sm\NewObjTris=CreateBank (estimated_tri*4) : sm\NewObjTrisPtr=0
		sm\NewObjVt=CreateBank (estimated_vt*4) : sm\NewObjVtPtr=0
		
	Return Handle(sm\NewObj)
End Function

Function SM_EndObject%(smid%)
; finalize object-creation
;RETURNS: number of vertices added
	Local sm.SM_Entity=Object.SM_Entity(EntityName(smid))
	If sm=Null Then RuntimeError ("SM_EndObject: Invalid SM_Object")
	If sm\NewObj=Null Then RuntimeError ("SM_EndObject: No SM_Object open")
	
	sm\NewObj\Tris=CreateBank(sm\NewObjTrisPtr)
	CopyBank(sm\NewObjTris,0,sm\NewObj\Tris,0,sm\NewObjTrisPtr)
	Local cnt%=sm\NewObjVtPtr/4	
	SMInt_UpdateNormals (sm\NewObj)
	
	FreeBank(sm\NewObjTris) : sm\NewObjTrisPtr=0
	FreeBank(sm\NewObjVt) : sm\NewObjVtPtr=0
	sm\NewObj=Null
	Return (cnt%)
End Function

Function SMInt_UpdateNormals (obj.SM_Object)
;maybe optimize this later and calculate just the new object	
	If obj=Null Then RuntimeError ("SMInt_UpdateNormals: Invalid SM_Object - ERROR IN LIB, THIS SHOULD NOT HAPPEN")
	
	UpdateNormals (obj\par\msh)
End Function

Function SM_AddVertex% (smid%,x#=0,y#=0,z#=0,u#=0,v#=0)
;adds a vertex to current object
;RETURNS: Vertex-ID
	Local sm.SM_Entity=Object.SM_Entity(EntityName(smid))
	If sm=Null Then RuntimeError ("SM_AddVertex: Invalid SM_Object")
	
	Local size%=BankSize(sm\NewObjVt)
	Local vt%=AddVertex(sm\srf,x,y,z,u,v)
	
	sm\NewObjVtPtr=sm\NewObjVtPtr+4
	If sm\NewObjVtPtr > size Then ResizeBank(sm\NewObjVt,sm\NewObjVtPtr)
	PokeInt (sm\NewObjVt,sm\NewObjVtPtr-4,vt%)
	
	Return (vt%)
End Function

Function SM_AddTriangle% (smid%,v0%,v1%,v2%)
;adds a triangle to current object
;RETURNS: Triangle-ID
	Local sm.SM_Entity=Object.SM_Entity(EntityName(smid))
	If sm=Null Then RuntimeError ("SM_AddTriangle: Invalid SM_Object")
	
	Local size%=BankSize(sm\NewObjTris)
	Local t%=AddTriangle(sm\srf,v0,v1,v2)
	
	sm\NewObjTrisPtr=sm\NewObjTrisPtr+4
	If sm\NewObjTrisPtr > size Then ResizeBank(sm\NewObjTris,sm\NewObjTrisPtr)
	PokeInt (sm\NewObjTris,sm\NewObjTrisPtr-4,t)
	
	Return (t%)
End Function

;================================================================

Graphics3D 1024,768,32,2
SetBuffer BackBuffer()

cam=CreateCamera()
PositionEntity cam,0,0,-20
AmbientLight(255,255,255)

Local ent=SM_CreateEntity()
Local v0,v1,v2,v3,px,py
; Hit <Space> to create a quad at random position. If you create the 4th quad, the previous three disappear.
; Create the 5th, and they appear again. The 6th makes all previous quads disappear again, then the 9th, the 11th => Stopped cheching
While Not KeyDown(1)
		If KeyHit(57)
			SM_BeginObject (ent)
				px=Rnd(-8,8) : py=Rnd(-8,8)
				v0=SM_AddVertex (ent,px-1,py-1,0)
				v1=SM_AddVertex (ent,px-1,py+1,0)
				v2=SM_AddVertex (ent,px+1,py+1,0)
				v3=SM_AddVertex (ent,px+1,py-1,0)
				SM_AddTriangle (ent,v0,v1,v2)
				SM_AddTriangle (ent,v0,v2,v3)
			SM_EndObject (ent)
			; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			; !!!! Uncommenting this debuglog will prevent this strange behaviour - if you run in debugmode 
			; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			; DebugLog "WHAT THE HELL!!!!"
			; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		EndIf
	RenderWorld
	Flip False
Wend
End


Any hints or ideas to hunt this evil bug could save an old man's day...

Sincerly

Jake