ReadPixels/WritePixels on iPad

Monkey Forums/Monkey Beginners/ReadPixels/WritePixels on iPad

TVCruelty(Posted 2015) [#1]
Hi,

I'm completely baffled. The following code runs and works fine on desktop (PC and Mac) and HTML5. On the iPad, however, I never see the screen-grabbed image, just the new image on a black background. Any help would be much appreciated as my game really needs this.

(By the way, I don't know how to properly embed code here so apologies if it looks a bit naff.)

<code>

Method OnUpdate()
If MouseHit()
state = Not state
If Not state
gotGrab = False
EndIf
EndIf
End

Method OnRender()

If state And Not gotGrab
grab = ScreenGrab(0, 0, DeviceWidth(), DeviceHeight())
gotGrab = True
EndIf

Cls()

If Not state
DrawImage(card, 100, 100)
DrawImage(card, 400, 100)
Else
SetAlpha 0.5
DrawImage(grab, 0, 0)
SetAlpha 1
DrawImage(card, 300, 100)
EndIf

End

Function ScreenGrab:Image(X:Int, Y:Int, Width:Int, Height:Int)

If X < 0 Error "Screen Grab Error"
If Y<0 Error "Screen Grab Error"
If (X+Width)>DeviceWidth() Error "Screen Grab Error"
If (Y + Height) > DeviceHeight() Error "Screen Grab Error"

Local ScreenShot:Image
ScreenShot = CreateImage(Width,Height)
Local pixels:Int[] =New Int[Width*Height]
ReadPixels(pixels, X, Y, Width,Height)
ScreenShot.WritePixels(pixels, 0, 0, Width,Height)
Return ScreenShot

End

</code>

Obviously, in this case the "grabbed" screen is the same every time but in the game proper it'll vary.

Thanks!


ordigdug(Posted 2015) [#2]
For Forum Codes:
-- Click "forum codes" link above and to the right of post window
-- < > needs to be replaced with [ ]

As for your issue:
MouseHit() does not work on touch screens, use TouchHit()


TVCruelty(Posted 2015) [#3]
Ah, thanks. Didn't notice the forum codes link!

The MouseHit() definitely isn't the problem - it does work on iOS, in fact, and reduces my platform-specific code.

So, that apart, any idea why my screen-grabbed image isn't showing up? Like I said, this all works fine on other platforms.


Pakz(Posted 2015) [#4]
Maybe post it in the bugs forum? If it works on other targets and not on the other then I would think it is a bug. I had the same thing not so long ago with the flash target which turned out to be a bug.


TVCruelty(Posted 2015) [#5]
Thanks @Pakz, I'll do that.

(If it is a bug I'd be surprised that no-one else has ever come across it - but I guess stranger things have happened!)


Pakz(Posted 2015) [#6]
Monkey does not have that many users. So bugs tend to stay undiscovered.

edit:

You need a mac right to compile for ipad? I have a ipad but no mac.


TVCruelty(Posted 2015) [#7]
Yes, you need a Mac. You compile on the Mac to the iOS target, then load the resulting project into Xcode. After that it's a "simple" matter of subscribing to the dev programme and registering your iPad as a development machine...

The next step, to actually submit to the App Store, is another whole world of pain that I haven't got clear in my head yet!


TVCruelty(Posted 2015) [#8]
By the way, if anyone is able to test my code on an iOS device I'd appreciate your feedback.


skid(Posted 2015) [#9]

Monkey does not have that many users. So bugs tend to stay undiscovered



Also, most Monkey users I expect understand that using readpixels / writepixels in a hardware accelerated environment is a very poor solution to most problems so the commands themselves are left on the shelf.

In regards to modern iPad, allocating retina display sized images in itself is likely to produce more performance hick-ups and memory thrashing than simply compositing the display by drawing all the component images every frame.


TVCruelty(Posted 2015) [#10]
Well OK but my intention is to use this technique only occasionally - it's not so unusual to want to, for example, grab what's on screen, alpha it down and place a dialog on top, is it? My game is a strategy game so it's not going to be happening at pace.

Like Pakz suggested, I'll probably raise this as a bug as soon as I've had chance to do some more tests, because, whether you approve of it or not, it should work.


skid(Posted 2015) [#11]
whether you approve of it or not, it should work


I totally agree.

Just be aware that older iPads especially have very little memory and that drawing the scene plus a solid color full screen rectangle with alpha plus your dialog is likely to be a lot less taxing on such a platform than the use of display sized image grabs.


Gerry Quinn(Posted 2015) [#12]
ReadPixels / WritePixels were one of the chief demands of Monkey users from the start. The purpose is generally pre-level graphic composition, for which speed and efficiemcy are not irrelevant, Sure, people do try to use it for things it's not suited to, but that doesn't alter the fact that it's very important for the things that need it.


Pakz(Posted 2015) [#13]
I needed readpixels and writepixels for a space invaders game. Where you and the aliens destroy the blocks on the bottom of the screen. From what I tried with the feature it is fast enough for things like that. Worms (original blitz game) had destructable terrain. Modifying images is needed for that to. I had only had a speed problem with monkey in html5 with the setcolor command. Luckely someone told me About that.