GLBasic forum

Main forum => GLBasic - en => Topic started by: yaKC on 2018-May-16

Title: Faster alternatve to GetPixel
Post by: yaKC on 2018-May-16
I've been working on a game for a few years using GetPixel, however I'm stuck at around 60 checks max per frame as it's really slow, I read that it transfers the framebuffer everytime you use it. Is there anyway to use Lockbits, or Getpixels (multiple) so that i only need to transfer once? Or perhaps a way using OpenGL?
I can use a 1 pixel sprite collsion as analternative with an image but this game really needs live info from the screen. not the whole screen just around a 32x32 area around the player.
Thanks in advance if anyone has a solution :D
Title: Re: Faster alternatve to GetPixel
Post by: bigsofty on 2018-May-16
You could use GRABSPRITE() to grab the 32x32 area around the player and if your not changing the background each frame, just reuse the array data?

EDIT: Oops missed out turning the sprite into a pixel array stage ( SPRITE2MEM() ), thanks for the reminder Dreamerman ;)
Title: Re: Faster alternatve to GetPixel
Post by: dreamerman on 2018-May-16
It all depends on your game, for what and how you are using this. But as Bigsofty mentioned you can use GrabSprite (don't know how fast it would be), Mem2Sprite and of course OpenGL either in GLB or inline C++. Don't remember now if any one made fast sprite2mem but as you can see in this topic about Mem2Sprite ( using OpenGL can give you much better results. You can combine this with some other tricks - draw player and part of background to offscreen sprite then use Mem2Sprite and so on...
Title: Re: Faster alternatve to GetPixel
Post by: MrPlow on 2018-May-17
I have an idea that might be of use - I did something like this for an android app.
Not sure how effective this would be for your use case, it could work for small resolutions or subset of the display

Create a function that...
1. draw the objects items on screen
2. along with the function send the main color of the the object as parameter
3. create an array or type storing x,y,w,h,color of object

Second function to fetch from type and array by x,y coords

I used a cruder version for a color picker routine...
It might not suit your problem but maybe it might give you ideas