crash on readpixels

Monkey Targets Forums/Desktop/crash on readpixels

Capn Lee(Posted 2013) [#1]
On GLFW I get an app crash using readpixels that doesn't seem to happen on any other target, I'm not sure if it's a monkey/glfw bug or (more likely) due to my misunderstanding

If anyone could advise a better way to do the following or advise how I can chnage this to not cause a crash, it would be really helpful

Method grabforfade:Int()
	scrshot = CreateImage(SCREEN_WIDTH, SCREEN_HEIGHT)
	Local pixels:Int[] =[SCREEN_WIDTH * SCREEN_HEIGHT]
	ReadPixels(pixels, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
	scrshot.WritePixels(pixels, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
	fadeamount = 1.0
	imagetake = False
	Return 0
End


All this is used for is fading between different states, after writing this image, it pastes it over the top of the screen render with an opacity of 'fadeamount'


Shinkiro1(Posted 2013) [#2]
Afaik you can only read pixels from OnRender(). If you are using it from OnUpdate() it will cause a crash.

A much easier method is to draw a black rectangle over the whole screen and use SetAlpha to fade in/out.


Capn Lee(Posted 2013) [#3]
Sorry, I should have mentioned, this method is called from within OnRender()
It does the same if moved directly into the OnRender() method also.


Capn Lee(Posted 2013) [#4]
could someone attempt to add the above code to the OnRender() method and see if they get the same issue running as GLFW?

It would be a good start if I knew whether the problem was due to my setup or a general bug, thanks


slenkar(Posted 2013) [#5]
im not promising anything but try this


Method grabforfade:Int()
scrshot = CreateImage(SCREEN_WIDTH+1, SCREEN_HEIGHT+1)
Local pixels:Int[] =[(SCREEN_WIDTH+1) * (SCREEN_HEIGHT+1)]
ReadPixels(pixels, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
scrshot.WritePixels(pixels, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
fadeamount = 1.0
imagetake = False
Return 0
End



if that doesnt work can you create a small sample program?


Capn Lee(Posted 2013) [#6]
That was good thinking, I was ready to be ashamed if that worked but unfortunately it didn't. I've written a short example that shows off my problem, here is a test program which works under html5 but not under glfw.

It is also pasted below:

Import mojo

Function Main:Int()
	Local t:testprogram = New testprogram
	Return 0
End

Global SCREEN_WIDTH:Int
Global SCREEN_HEIGHT:Int

Class testprogram Extends App

	Field boxLoc:Int[]
	
	Field screenShot:Image


	Method OnCreate()
		SetUpdateRate(60)
		SCREEN_WIDTH = DeviceWidth()
		SCREEN_HEIGHT = DeviceHeight()
		boxLoc =[SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2]
	End
	
	Method OnUpdate()
		If KeyDown(KEY_UP) Then boxLoc[1] -= 3
		If KeyDown(KEY_DOWN) Then boxLoc[1] += 3
		If KeyDown(KEY_LEFT) Then boxLoc[0] -= 3
		If KeyDown(KEY_RIGHT) Then boxLoc[0] += 3
		If boxLoc[0] < 32 Then boxLoc[0] = 32
		If boxLoc[0] > SCREEN_WIDTH - 32 Then boxLoc[0] = SCREEN_WIDTH - 32
		If boxLoc[1] < 32 Then boxLoc[1] = 32
		If boxLoc[1] > SCREEN_HEIGHT - 32 Then boxLoc[1] = SCREEN_HEIGHT - 32
	End
	
	Method OnRender()
		Cls(5, 5, 25)
		SetColor(220, 220, 190)
		DrawCircle(boxLoc[0], boxLoc[1], 16)
		ScreenGrab()
		DrawRect(0, 0, (SCREEN_WIDTH / 2) + 2, (SCREEN_HEIGHT / 2) + 2)
		PushMatrix()
			Scale(0.5, 0.5)
			DrawImage(screenShot, 1, 1)
		PopMatrix()
		
	End
	
	Method ScreenGrab()
		screenShot = CreateImage(SCREEN_WIDTH, SCREEN_HEIGHT)
		Local pixels:Int[] =[SCREEN_WIDTH * SCREEN_HEIGHT]
		ReadPixels(pixels, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
		screenShot.WritePixels(pixels, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
	End
End



DGuy(Posted 2013) [#7]
Instead of:
Local pixels:Int[] = [SCREEN_WIDTH * SCREEN_HEIGHT]

Try:
Local pixels:Int[] = New Int[SCREEN_WIDTH * SCREEN_HEIGHT]
                     ^^^^^^^

The original code is creating an array of length=1; with pixels[0] = (SCREEN_WIDTH * SCREEN_HEIGHT)


Capn Lee(Posted 2013) [#8]
you're completely right, thank you.

I am now confused as to why my code worked fine in html5/flash :S


Nobuyuki(Posted 2013) [#9]
type checking's a little different on those platforms.