Blue blocks

Blitz3D Forums/Blitz3D Beginners Area/Blue blocks

Marcell(Posted 2009) [#1]
Hello.

My program fills rectangle with blue blocks.

The area is 15 x 30 = 450 in blocks.

I'm trying to make a delay after the rectangle
is finished.

Everyting works fine, until I add Delay-command
to make a delay (see the code, Delay 4000). What happens is
that delay seems to take place when blocks-value
is 450 (waiting for 451) while at the same time the rectangle
misses one block. After a delay also the last block appears
-- what should happen is that delay should takes place _after_
the last block.


In short: Programs stops at 450 (waiting for 451) --> delay = 4000
--> one block missing --> delay --> "Ok." (see code) --> delay --> the last block appears.

; Sorry, no comments. The code is from 2007. I'm not sure my self how things works here... :-)

Graphics 1024,768,32,1

Const blockSize = 12
Const BankWidth = 15
Const BankHeight = 30

Global gfxPoolInside = CreateImage(BankWidth*blockSize,BankHeight*blockSize)
Global gfxActionBuffer = CreateImage(BankWidth*blockSize,BankHeight*blockSize)


Global poolBankBlocks
Global poolBankUE
Global poolBank
Global stateEffect_fadeIn
Global timerUE				
Global arpa
Global counter

Global timerUE_fadeIn

Global blocks = 0

Global frameTimer

frameTimer = MilliSecs()


poolBankUE = CreateBank(BankWidth * BankHeight)


Type poolBankBlock
	Field x,y
	Field block
End Type

SetBuffer BackBuffer()

blocks = 0


Gosub initEffectUE_fadeIn
stateEffect_fadeIn = True


.main
While Not KeyHit(1)
		
		
	Gosub updateScreen
	
	
	Flip
	
Wend
End





.updateScreen
SetBuffer BackBuffer()

Cls

DrawImage gfxPoolInside,0,0 

	
Color 0,0,255


If stateEffect_fadeIn = True And MilliSecs() - timerUE_fadeIn > 15 Then
	Gosub handleEffectUE_fadeIn
	
	blocks = blocks + 1

	
EndIf

SetBuffer BackBuffer()

Color 255,255,255
Text 300,0,blocks


If blocks = 451 Then

	Color 255,255,255
	Text 500,0,"OK."

	Delay 4000
	
EndIf




Return



.initEffectUE_fadeIn
For pb.poolBankBlock = Each poolBankBlock
	Delete pb
Next


poolBankBlocks = 0

For y = 0 To BankHeight-1
	For x = 0 To BankWidth-1
		If PeekByte(poolBankUE, x + y * BankWidth) = 0 Then
			pb.poolBankBlock = New poolBankBlock
			pb\x = x
			pb\y = y
			poolBankBlocks = poolBankBlocks + 1
		EndIf
	Next
Next
		
timerUE_fadeIn = MilliSecs()	
		
Return




.initEffectUE_fadeIn_forNextRound

counter = 0
arpa = Rand(0,poolBankBlocks - 1)

For pb.poolBankBlock = Each poolBankBlock

	If counter = arpa Then
		PokeByte poolBankUE, pb\x + pb\y * BankWidth, 1
		Delete pb
		poolBankBlocks = poolBankBlocks - 1
	EndIf
	
	counter = counter + 1
Next


If counter = 0 Then stateEffect_fadeIn = False

timerUE_fadeIn = MilliSecs()

Return



.DrawBlck

For y = 0 To BankHeight-1
	For x = 0 To BankWidth-1
		tempvalue = PeekByte(poolBankUE, x + y * BankWidth)
		If tempvalue = 1 Then
			Color 0,0,255
			Rect x*blockSize,y*blockSize,blockSize,blockSize,1
		End If
	Next
Next

Return


.handleEffectUE_fadeIN

	SetBuffer ImageBuffer(gfxActionBuffer)
	Cls
	Gosub DrawBlck
	GrabImage gfxPoolInside,0,0

		
	Gosub initEffectUE_fadeIn_forNextRound

Return








Warner(Posted 2009) [#2]
It is just a guess: have you called Flip before using Delay? Else, the screen is not updated.


Marcell(Posted 2009) [#3]
Flip and Delay are called ok.


Warner(Posted 2009) [#4]
If blocks = 451 Then

	Color 255,255,255
	Text 500,0,"OK."

	Delay 4000
	
EndIf

But then why is there no 'Flip' in this part of the code? The message will not show.
Anyway, it would be a good idea to reorganise the code first, so that you have a clear idea what is going on at which place. That would make it easier to solve the problem and expand this code later on.


Marcell(Posted 2009) [#5]
I tried Flip there, didn't help.

The code can be copy-pasted to Blitz.

The last block appears after delay.


Marcell(Posted 2009) [#6]
I got the last block done with this code:

If blocks = 451 Then

...  ...

	GrabImage gfxPoolInside,0,0 
	
	SetBuffer BackBuffer()
	DrawImage gfxPoolInside,0,0
	
	Flip
	Delay 5000




_PJ_(Posted 2009) [#7]
Might I suggest, that unless at any point you are drawing to another buffer, that you place SetBuffer right under the Graphics command instead of within your subroutine?

Essentially when using the BackBuffer, all the drawing commands are drawn 'invisibly' onto the back buffer, and will stay there until the flip command swaps the visible screen display (front buffer) for the drawn-on back buffer.

Each time you flip the buffers in this way, the backbuffer is cleared, so all the drawing needs to be re-done.

When you want a delayed appearance of something drawn to the screen, you can opt for using Delay and redrawing the entire screen again then adding the delayed-appearance-block, which works, but would prevent any other activity for that duraton....

OR

You can repeatedly update the screen (which includes re-drawing all blocks) with a check against the time (Millisecss()) passed since the scene was beghun. If this time passed is longer than whatever duration, then add in the last block.

Currently, your code is something of a combination of the two, which is why it wasn't working too well. Using just one method or the other would be a lot 'better' :)