Lock/Unlock

Post your coding questions such as 'how to' here.
Post Reply
Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

Lock/Unlock

Post by Buckethead »

On this example the dots on the background looks smooth in windowed mode but they're totally jerky in Fullscreen mode.
I didn't save it, but I did other codes and it was jerky even in a window.

I'd to 'force' Unlock instruction to display every effects in a frame. I'm pretty sure that's why I got the problem.

What is the good method to print fast dots with text and other draw commands ?

Code: Select all


' Replace "If 0" by If "1" to permute between fullscreen/windowed mode
If 0 Then SCREEN 800,600,0,2 : SetWindow "COOLBASIC",3 Else SCREEN 800,600,16,0

' How do you change the size of a font without a load ? (freeze)  
font = LoadFont("arial",16)
fontLOGO = LoadFont("impact",210)
SetFont fontLOGO


Dim star(100)
For i = 1 To 100
star(i) = Rnd(800)
Next i


'--- Main routine ---

Repeat
Lock

'1/ 2D starfield dots and texts
SetFont font
For i = 1 To 100
c = int(Abs(5*Sin(i^4)))
vit = 1 + c
star(i) = star(i) + vit
If star(i) > 870 Then star(i) = 0
Color 0,9*vit,100+17*vit

PutPixel 8*i,star(i), 50+ vit * 30 Shl 16 + 50+ vit * 30 Shl 8
Text star(i)-70,i * 6,"COOLBASIC"

Next i


'2/ Big text

j = j + 5
Color 100,100,245
SetFont fontLOGO
Text 15,300-Abs(200*Sin(j)), "COOLBASIC"



Color 0,20,170
SetFont font
Text 0,0,"("+ FPS() +" FPS)"


' Wihtout a second unlock, it only display the vertical starfield on the background
Unlock
Unlock


DrawScreen 1,1 : Forever
User avatar
CCE
Artist
Artist
Posts: 650
Joined: Mon Aug 27, 2007 9:53 pm

Re: Lock/Unlock

Post by CCE »

I'm not sure is this what you were looking for, but here it goes.

It's just a small simple example, with your nice PutPixel2 row in it.
You asked "What is the good method to print fast dots with text and other draw commands ?"
I think its the PutPixel2, and I have an idea why it didnt work on your code.

For some strange reason when you use command PutPixel2, with an array or some other mathematical operators than basic numbers in it like this

Code: Select all

PutPixel2 8*i,star(i), 50+ vit * 30 Shl 16 + 50+ vit * 30 Shl 8
it won't work.

But if you use it like this it works without MAV.

Code: Select all

PutPixel2 i,i+2, 50+ vit * 30 Shl 16 + 50+ vit * 30 Shl 8
Ofcourse it won't give the same results, but I think you got the idea.

Oh, and the example of PutPixel2 without GetPixel2

Code: Select all

SCREEN 48,32,16,2	// a pretty small window
FrameLimit 40

Repeat

Lock SCREEN()	// Lock the SCREEN
	For y=0 To 32
	For x=0 To 48
		vit = Rand(0,4)	// Randomize Dot Color
		// fast locked pixel commands
		PutPixel2 x,y,50+ vit * 30 Shl 16 + 50+ vit * 30 Shl 8
	Next x
	Next y
Unlock	// Unlock the SCREEN buffer

Text 0,0,"Text"
DrawScreen
Forever
Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

Re: Lock/Unlock

Post by Buckethead »

Thanks for your reply.
I see what you tried to show me, I get it but I forgot to say that I've a MAV with PutPixel2/GetPixel2. This is why I use simple PutPixel instruction.
The example I gave works on my machine. I can't explain the bugg on your machine, perhaps I'd set the var bad or something.
Now that you point out the problem with PutPixel2 and parameters, I'll test this, but my first think was that I forced Unlock.

My real problem is how to use Lock/Unlock with a huge loop of PutPixel combined with other 2D effects that use other draw commands after this loop. On such routine I needed to put Unlock instruction 2 times. Btw the display wasn't correct in fullscreen.

It seems I got the problem only when I use PutPixel. I did a little test with Copybox & scrolltext routine (sinus scroll), which is 2 ""effects"" combined. One Unlock was enough.

Also, do you use some of kind of IDE ? Your code It's much clear than mine ;)
User avatar
CCE
Artist
Artist
Posts: 650
Joined: Mon Aug 27, 2007 9:53 pm

Re: Lock/Unlock

Post by CCE »

My real problem is how to use Lock/Unlock with a huge loop of PutPixel combined with other 2D effects that use other draw commands after this loop. On such routine I needed to put Unlock instruction 2 times. Btw the display wasn't correct in fullscreen.
The double Unlock can be corrected easily, just change the place of the Lock command, and place the Unlock just after the pixel commands, like this

Code: Select all

Lock SCREEN()
	PutPixel 8*i,star(i), 50+ vit * 30 Shl 16 + 50+ vit * 30 Shl 8
Unlock
And I don't have any problems with the fullscreen mode, some random slowdowns, but thats all.

Btw, I'm not using any third-party IDE's, I'm running on the basic CoolBasic Editor.
Some tabs here and there makes wonders :D

Edit: Oh, I tested your code on my other computer, and discovered that it runs great with PutPixel2, with the code same as above

Code: Select all

Lock SCREEN()
	PutPixel2 8*i,star(i), 50+ vit * 30 Shl 16 + 50+ vit * 30 Shl 8
Unlock
... but there's only a 4 fps speedup :(
Buckethead
Newcomer
Posts: 16
Joined: Thu Sep 20, 2007 9:53 am

Re: Lock/Unlock

Post by Buckethead »

Ok, now I think I get it. Lock/unlock should be used only with PutPixel/GetPixel (fast or slow version of it)
It was in the help but I wanted to lock everything ;)
On my example it's too bad because I can't use the same loop and calculation to draw the dots and text (dots are x,y and text y,x)

Hehe nice tabs :D

Duh, this exemple is very slow. I thought it should be smooth enough even with an old machine, my bad. I wanted to point out the problem and did it the most easy way. (somehow I had better framerate with a scripting langage with this kind of routine under a 386 DX and Excel version of 1994, hehe)
Btw I'm under XP and any Coolbasic executable takes 50% of resources.
Post Reply