Using Grabimage to make a Mirror

BlitzMax Forums/BlitzMax Programming/Using Grabimage to make a Mirror

Rico(Posted 2011) [#1]
Hi i made a mirror in my 2D platform game by using grabimage to grab the area in front of the mirror and then using DrawImage and flipping the image to superimpose this image over the mirror sprite. This works fine but seems to slow the program a bit. i presume it's because it is doing grabimage every frame. is there a faster way of doing this at all? Thank you :)


GfK(Posted 2011) [#2]
To draw everything upside down:
SetScale 1,-1



Rico(Posted 2011) [#3]
thank you, yes i was doing that to flip the image though i was doing it horizontally (its a side on mirror) . i realise now though that i have to flip any of the graphics drawn by the mirror and draw and confine them to the mirror rectangle. its a bit annoying because i have to do it for any sprites than move near it, whereas before with grabimage it was so easy! oh well lol


Rico(Posted 2011) [#4]
btw is there a command to copy a section of the screen to another place on the screen?


Rico(Posted 2011) [#5]
ok well i tried it using grabpixmap but that slowed things as much as grabimage :(


Rico(Posted 2011) [#6]
I did a speed test and it takes about 40 milliseconds to do grabimage or grabpixmap. but only 1 millisec to draw the image. should it be this slow?


Kryzon(Posted 2011) [#7]
If there's that much difference then you should stick to rendering everything twice but in a flipped fashion the second time. Then you can mask this content or frame it so it looks like a reflection from a mirror, draw a border around it etc.


Rico(Posted 2011) [#8]
thanks Kryzon, yes that is what i was planning to do. i was going to clip the images using set viewport so it stays within the mirror. its a lot more work though, writing code to do it for every enemy and the main character and also the background. . i wish there was a fast grabimage command. think it would be very useful!


Grey Alien(Posted 2011) [#9]
The reason it's slow is because it's getting data from the video card and video cards are optimized for SENDING data to them obviously, not the other way round. Back in BlitzPlus days a whole screen grab image could take 300ms or more.


Rico(Posted 2011) [#10]
ah ok thanks Grey Alien, i think i just worked out that there are 16.6 millisconds in one frame (my game runs at 60 fps). so you can see why grabimage is far from usable, i was only grabbing a 72 by 143 pixels rectangle as well . it seems far too slow still even taking into account what you said. (considering the power of modern day PCs)

i used to have an Amiga (a very slow computer compared to a modern PC) and you could use the blitter to moves sections of the screen about for scrolling etc. im sure most PC graphics cards have blitters in them. can i use the blitter in my gfx card somehow (via Blitz)?

Last edited 2011


slenkar(Posted 2011) [#11]
i realise now though that i have to flip any of the graphics drawn by the mirror and draw and confine them to the mirror rectangle


What about the SetViewPort command?

If that doesnt help my GLmax2d module has fast grab image
http://www.blitzbasic.com/codearcs/codearcs.php?code=2612


ima747(Posted 2011) [#12]
Actually moderns graphics systems don't use a blitter as such. The whole concept of what is displayed is actually wildly different than back in the day. Blitz does a wonderful job of disguising it all so you can think about things in the "classic" way, but hardware acceleration is where it all happens and you just can't apply that tech to a straight blitter... It used be back in the day there was memory, and the display buffer, and the back buffer. you got to play with all of them. Now there's memory and a troll under a bridge known as a GPU... you feed it things it likes and close your eyes :0)

With the power of modern systems, combined with their very refined data paths (basically OUT and that's it for graphics it's usually much faster to repeat yourself a lot (draw things a large number of times) rather than try to change the flow (get anything back from the graphics system).


Rico(Posted 2011) [#13]
thanks slenkar yes i decided to use set viewport. i got my mirror working fine, though i havent done it for all the bad guys yet, just the main characater and the background so far. i like the sound of your faster grabimage, what do i have to do to get it running in my game? do i just have to copy your function code into it?

Last edited 2011


Rico(Posted 2011) [#14]
thanks for the info ima747. i just remember in the old blitz you used to be able to find out if the blitter was available in some of the graphics modes, so i assumed it was still active. but yes brute force output will have to be the order of the day :)


slenkar(Posted 2011) [#15]
yw

EDIT- you could try just copying the function over :)
Function GrabImage:TImage(x,y,width,height)
Local i:TImage=CreateImage(width,height)
glBindTexture GL_TEXTURE_2D,i.name
glCopyTexSubImage2D GL_TEXTURE_2D,0,0,0,x,GraphicsHeight()-i.height-y,width,height
Return i
End Function


or if that doesnt work:

to use the module I made just copy the source code to
mods/keef/gldraw/gldraw.bmx

theres collision and bitmap font code in the same code archive too

the commands are the same as the normal drawing commands
its a total replacement for the graphics driver so you dont have to use
SetGraphicsDriver

you will have to use the FrameWork command so my functions dont clash with the official glmax2d

Last edited 2011

Last edited 2011

Last edited 2011

Last edited 2011


Rico(Posted 2011) [#16]
Thanks Slenkar, i will give it a go. im not very good with that framework stuff haha so i hope it works. but i have doen it before with Box2D. thanks for the help! :D


Sledge(Posted 2011) [#17]
Here, just rustled this up as a possible approach for you to consider:



I don't know why you're grabbing images instead of pixmaps. :D

EDIT: Added highlights and tint. Why not!

EDIT 2: Oh, pretty sure that the last time I did something like this I grabbed one big pixmap and rendered it in rows using DrawSubImageRect(), which *should* be faster than grabbing individual rows. But there you go, I was supposed to get my head down half an hour ago :D

Last edited 2011


Rico(Posted 2011) [#18]
Thanks very much for that Sledge *thumbs up*, sorrry i thought the thread was finished so i didnt check back until now

i did try using Grab Pixmap but it was exatcly the same speed as GrabImage as far as i remmeber so i gave up. i didnt go any further looking into that.

thats a very nice program but it does run quite slowly on my computer. so i dont think i could use that approach in my game. does it run fast on yours?