0

I am trying to implement boundary fill algorithm in sdl2 but it requires to know the color value of a particular pixel. In turbo c, I have successfully used the getpixel() function to do so. How to achieve the same using SDL2? So far I have created a window and a renderer and draw a rectangle using SDL_RenderDrawRect() function.

This is my main method:

int main(int argc, char const *argv[])
{
  SDL_Rect rect;
  rect.x = 100;
  rect.y = 100;
  rect.w = 300;
  rect.h = 200;
  bool isRunning = true;
  if (SDL_Init(SDL_INIT_EVERYTHING) >= 0)
  {
    window = SDL_CreateWindow("Boundary Fill", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    if (window != 0)
    {
      surface = SDL_GetWindowSurface(window);
      renderer = SDL_CreateRenderer(window, -1, 0);
    }
  }
  else
  {
    return 1;
  }

  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  SDL_RenderClear(renderer);
  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 1);
  SDL_RenderDrawRect(renderer, &rect);
  SDL_RenderPresent(renderer);

  while (isRunning)
  {
    while (SDL_PollEvent(&event))
    {
      switch (event.type)
      {
      case SDL_QUIT:
        isRunning = false;
        break;

      default:
        break;
      }
    }
  }
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

Now I want to fill this rectangle using various polygon filling algorithms.

  • Not using SDL at all but peeking individual GPU pixels on CPU side is very slow on OpenGL/DX which is used under the hood of SDL so if you want true performance you should rethink what you are doing. If speed is not a problem then just look in the SDL docs/headers for corresponding function to `glReadPixels` or its counterpart from Dx (not sure which you use) so somehing like Pixels, GetPixel, ... Also boundary fill on higher resolutions needs to be coded properly using Priority que to avoid stack overflows ... or use [line fill instead of pixels](https://stackoverflow.com/a/37810355/2521214) – Spektre Dec 15 '21 at 09:48

0 Answers0