peekevent + waittimer = ???

BlitzPlus Forums/BlitzPlus Programming/peekevent + waittimer = ???

Idiot(Posted 2004) [#1]
Problem:

window=CreateWindow("WFT",300,300,300,300)

timer=CreateTimer(60)

Repeat
	e=PeekEvent()
	WaitTimer(timer)
Until KeyDown(1) Or e=$803


You can quit with escape, but the close button for the window is extremely flakey. I want to use a timer to guarantee maximum gamespeed, but I also need to know if the user is trying to close the window. Any ideas?


Eikon(Posted 2004) [#2]
Try:
window = CreateWindow("WFT", 300, 300, 300, 300)

timer = CreateTimer(60)

Repeat
	e = PeekEvent()
	WaitTimer timer
	
Until KeyDown(1) Or e = $4001



soja(Posted 2004) [#3]
I wouldn't use WaitTimer in an event loop. I would check for event $4001 and use WaitEvent. Personal preference, I guess.


Hotcakes(Posted 2004) [#4]
Yeh, what they all said. Keep your code consistant. WaitTimer in B+ grabs events, so there's every likelihood that it steals some of the wrong ones. WaitTimer is not programmed to be friendly, the timertick event is (if there was a friendly way of doing WaitTimer Mark would have included it in B+'s original release!)


Idiot(Posted 2004) [#5]
First, I want a normal loop, for a normal game running on a canvas in a window that has a quit button and a quit prompt. And I want it to run no faster than 60fps. Waitevent waits a specific number of milliseconds regardless of how long the loop has been executing. How can I guarantee no faster than 60fps without a timer?

Second, Eikon, that solution seems to work in that example, but in a game-like environment it starts randomly quitting.

window = CreateWindow("WFT", 300, 300, 300, 300)
canvas = CreateCanvas(0,0,300,300,window)
timer = CreateTimer(60)

SetBuffer CanvasBuffer(canvas)

Repeat
	
	Color Rand(100,255),Rand(100,255),Rand(100,255)
	Line Rand(0,300),Rand(0,300),Rand(0,300),Rand(0,300)
	
	If MouseHit(1) Then Cls
	
	FlipCanvas canvas
	
	e = PeekEvent()
	WaitTimer timer
	
	If e=$4001
		If Confirm("Are you sure?  "+quitString$)
			quit=True
		EndIf
	EndIf

Until KeyDown(1) Or quit=True


You can click the mouse to clear the screen, I don't know if that's really effecting it or not.


soja(Posted 2004) [#6]
I do something like this:
CreateTimer(60)
InitGui()
InitGame()

Repeat
	Select WaitEvent()
		Case $803 : End
		; other cases here (input, etc)
		Case $4001
			Logic()
			Update()
			Render()
	End Select
Forever

You can even enclose the Select WaitEvent() block in a Select GameState block, along with more Select WaitEvent() blocks for other states. Unless your logic/rendering takes more than 1/60th of a second, it will run smooth, and no faster than 60 hertz.


Idiot(Posted 2004) [#7]
Fantastic. Works perfectly, thanks, soja!