Why does this fail so miserably?

BlitzMax Forums/BlitzMax Programming/Why does this fail so miserably?

Grey Alien(Posted 2006) [#1]
WARNING: If you run this code you'll have to "End Task" one of BlitzMax Applications that are made.

OK, the aim behind this code is to start in full-screen 800x600 32-bit, and if that fails, try in the same res in 16-bit, and if that fails, start in windowed mode.

What I have done is deliberately added + 1 to ScreenWidth when CreateGraphics is called the first two times to make a ridiculous size of 801 pixels that should fail on graphics cards thus resulting in GR being null. I know that GR is null because I added a debug stop after if GR=null.

However, this is the weird thing, the code seems to make 3 "BlitzMax Application"s that can only be seen in Task Manager. The final windowed mode is never shown properly and the repeat until loop is never entered thus you can't press escape, or run any code in fact. You have to End Task on the BlitzMax Application to get out of it.

I thought my logic was sound, make a graphics object, if it fails to create the mode, gr will be null so you can try to make another etc. I know I could check all available graphics modes and automatically make a selection (maybe I should just do this) but I don't get why this code fails so badly.

Any ideas?

Strict

Local fullscreen=1
Local screenwidth = 800
Local screenheight = 600
Local forcewindowedmode = 0

		Local gr:TGraphics
		If FullScreen Then
			'Try 32 bit, failing that try 16 bit, failing that use windowed mode
			gr = CreateGraphics (ScreenWidth+1,ScreenHeight,32,60,GRAPHICS_BACKBUFFER)	
			If gr = Null Then
				gr = CreateGraphics (ScreenWidth+1,ScreenHeight,16,60,GRAPHICS_BACKBUFFER)	
				If gr = Null Then
					ForceWindowedMode = 1
					FullScreen = 0
					'Code will continue after EndIf and call Graphics in windowed mode
				EndIf
			EndIf
		EndIf
		'No else on purpose
		If FullScreen = 0 Then
			gr = CreateGraphics (ScreenWidth,ScreenHeight,0,60,GRAPHICS_BACKBUFFER)	
		EndIf
		SetGraphics gr

Repeat

Until KeyHit(KEY_ESCAPE)



Azathoth(Posted 2006) [#2]
Though if you remove the repeat until loop it does exit.


Robert Cummings(Posted 2006) [#3]
Repeat Until is sucking up all the cpu time. Put a WaitEvent in there.


Grey Alien(Posted 2006) [#4]
sure OK, but that's not the real problem. The real problem is why does it make 3 blitzmax application windows if gr is null the first two times!

OK just did some testing and this:
Strict

Local screenwidth = 800
Local screenheight = 600

Graphics (ScreenWidth,ScreenHeight,0,60,GRAPHICS_BACKBUFFER)	
Repeat

Until KeyHit(KEY_ESCAPE)


does work, you can press escape to exit.

but this doesn't work escape does nothing! What's the difference?

Strict

Local screenwidth = 800
Local screenheight = 600

Local gr:TGraphics
gr = CreateGraphics (ScreenWidth,ScreenHeight,0,60,GRAPHICS_BACKBUFFER)	
SetGraphics(gr)

Repeat

Until KeyHit(KEY_ESCAPE)



Yan(Posted 2006) [#5]
but this doesn't work escape does nothing! What's the difference?
Polled input isn't running. The Graphics() command starts this as well as setting the graphics object.


Why not just do it like so?...



Grey Alien(Posted 2006) [#6]
I just did, thanks. Just wondered why the windows are being made even though creategraphics is returning null.