Author Topic: Faster alternatve to GetPixel  (Read 1164 times)

Offline yaKC

  • Mc. Print
  • *
  • Posts: 17
    • View Profile
Faster alternatve to GetPixel
« 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

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2660
    • View Profile
Re: Faster alternatve to GetPixel
« Reply #1 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 ;)
« Last Edit: 2018-May-17 by bigsofty »


“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline dreamerman

  • Global Moderator
  • Dr. Type
  • *******
  • Posts: 288
    • View Profile
    • my personal website
Re: Faster alternatve to GetPixel
« Reply #2 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...
Check my source code editor for GLBasic - link

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1662
    • View Profile
Re: Faster alternatve to GetPixel
« Reply #3 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
« Last Edit: 2018-May-17 by MrPlow »
Speccy-48k, Speccy-128k, Amigas, PCs