Bug with 2D experiment

Blitz3D Forums/Blitz3D Programming/Bug with 2D experiment

Chaduke(Posted 2008) [#1]
I was experimenting this morning with some simple randomized 2D rectangles. I noticed something strange and I was wondering if its my graphics card, a bug in my code, or a bug in Blitz3D.

I'm creating a custom type called "rectangle", then created a function called InitRect(), which creates a new rectangle and assigns it a random size, color, location and speed.

If you hold down the spacebar it creates new rectangles.

Here's where the strange problem occurs. If I create a certain number of rectangles before I start my main loop, it only displays a few of them, even though I created 1000 or more. You can see where I have the code commented out right before the main loop.

Let me know if the same thing happens on your system and if you know why this is happening.

PS : can someone point me to a post or instructions for the tags for this forum?

-----------------------------
; rectangles.bb

Const SW = 1024
Const SH = 768
depth = 32
fullscreen = 1

Type rectangle 
	Field x#
	Field y#
	Field r
	Field g
	Field b
	Field xv#
	Field yv#
	Field w
	Field h
End Type

Graphics SW,SH,depth,fullscreen
SetBuffer BackBuffer()

oldtime = MilliSecs()
ticks = 0
frames = 0
count = 0

; For i = 0 To 999
; 	InitRect()
; Next
	
While Not KeyDown(1)
	; fps calc
	timer = MilliSecs()
	If timer - oldtime > 999 Then 
		oldtime = timer
		ticks = ticks + 1
		fps = frames / ticks
		If frames > 9999 Then
			frames = 0
			ticks = 0
		End If	
	End If
	
	; get input
	If KeyDown(57) Then InitRect()
	
	; do calcs
	count = 0
	For r.rectangle = Each rectangle
		r\x = r\x + r\xv
		r\y = r\y + r\yv		
		If r\x > SW Then r\x = 0
		If r\x < 0 Then r\x = SW
		If r\y > SH Then r\y = 0
		If r\y < 0 Then r\y = SH	
		; draw rects
		Color r\r,r\g,r\b
		Rect r\x,r\y,r\w,r\h	
		count = count + 1
	Next
		
	; output text 
	Color 255,255,255
	Text 0,0,"fps " + fps	
	Text SW - 100,0,"count " + count
	Flip False
	Cls
	frames = frames + 1
Wend 
End

Function InitRect()
	SeedRnd MilliSecs()
	r.rectangle = New rectangle
	r\x = Rnd(0.0,SW)
	r\y = Rnd(0.0,SH)
	r\r = Rand(100,255)
	r\g = Rand(100,255)
	r\b = Rand(100,255)
	r\xv# = Rnd(-2.5,2.5)
	r\yv# = Rnd(-2.5,2.5)
	r\w = Rand(5,10)
	r\h = Rand(5,10)
End Function



Yan(Posted 2008) [#2]
You only need to seed the random number generator once so take the SeedRnd() call out of InitRect().

I shall leave it for you to work out what was happening (think about execution time and Millisecs() resolution). :o)


The forum tags can be found here...
What are the forum codes?


Chaduke(Posted 2008) [#3]
Hahah, i get it. I don't know why I was thinking I needed to seed it for every instance. So I have a bunch of them overlapping each other.

Thanks a lot man.