1

I am working on a simple project using pygame and I need to get and set pixel colors. Using pygame.get_at(()) and pygame.set_at(()) is VERY slow.

So I tried using PixelArray to get and set the colors manually. But this still is VERY slow

Is there a much faster way to do this?

Is it possible to make a funtion to get and set a pixel in c++ so it would be faster or is there a way in python/pygame to do it?

For example:

a c++ function that returns the pixel color at a given coordinate and another function that sets the pixel at a given coordinate to a given color.

Then I could call these functions in pygame somehow.

Thank you very much.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
arny berns
  • 33
  • 4

1 Answers1

0

C++ SetPixel and GetPixel function

Part of window.h library

Set Pixel at coords with color

COLORREF SetPixel(
  _In_  HDC hdc,
  _In_  int X,
  _In_  int Y,
  _In_  COLORREF crColor
);

Return color of pixel at specified coords

COLORREF GetPixel(
  _In_  HDC hdc,
  _In_  int nXPos,
  _In_  int nYPos
);

SDL:

Get Color

Uint32 rawpixel = getpixel(surface, x, y);
Uint8 red, green, blue;

SDL_GetRGB(rawpixel, surface->format, &red, &green, &blue);

SetPixel at coords

void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
    Uint32 *pixmem32;
    Uint32 colour;  

    colour = SDL_MapRGB( screen->format, r, g, b );

    pixmem32 = (Uint32*) screen->pixels  + y + x;
    *pixmem32 = colour;
}

Link