0

I am creating a program that allows you to view fractals like the Mandelbrot or Julia set. I would like to render them as quickly as possible. I would love a way to put an array of uint8_t pixel values onto the screen. The array is formatted like this...

{r0,g0,b0,r1,g1,b1,...}

(A one dimensional array or RGB color values)

I know I have the proper data because before I just set individual points and it worked...

for(int i = 0;i < height * width;++i) {
  //setStroke and point are functions that I made that together just draw a colored point
  r.setStroke(data[i*3],data[i*3+1],data[i*3+2]);
  r.point(i % r.window.w,i / r.window.w);
}

This is a pretty slow operation especially if the screen is big (which I would like it to be) Is there any faster way to just put all the data onto the screen. I tried doing something like this

  void* pixels;
  int pitch;

  SDL_Texture* img = SDL_CreateTexture(ren, 
  SDL_GetWindowPixelFormat(win),SDL_TEXTUREACCESS_STREAMING,window.w,window.h);

  SDL_LockTexture(img, NULL, &pixels, &pitch);
  memcpy(pixels, data, window.w * 3 * window.h);
  SDL_UnlockTexture(img);

  SDL_RenderCopy(ren,img,NULL,NULL);
  SDL_DestroyTexture(img);

I have no idea what I'm doing so please have mercy

Edit (thank you for comments :))

So here is what I do now

    SDL_Texture* img = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGB888,SDL_TEXTUREACCESS_STREAMING,window.w,window.h);
    SDL_UpdateTexture(img,NULL,&data[0],window.w * 3);
    SDL_RenderCopy(ren,img,NULL,NULL);
    SDL_DestroyTexture(img);

But I get this Image... which is not what it should look like Julia set

I am thinking that my data is just formatted wrong, right now it is formatted as an array of uint8_t in RGB order. Is there another way I should be formatting it (note I do not need an alpha channel)

Owen Kuhn
  • 85
  • 6
  • 1
    Hrm, at least [back in 2015](https://stackoverflow.com/a/33312056/44729) all of the SDL_Renderer backends I looked had ARGB preferred formats, not RGB. Does `SDL_GetWindowPixelFormat(win)` match any of the formats returned by `SDL_GetRendererInfo()`? – genpfault Nov 26 '19 at 04:21
  • 2
    Also, edit in a [mcve] so we know you aren't calling `SDL_CreateTexture()`/`SDL_DestroyTexture()` every frame :) – genpfault Nov 26 '19 at 04:22
  • UpdateTexture is the function I am looking for, sorry It would take me awhile to create a minimal reproducible example because of the way my code is laid out (it uses a wrapper for SDL that I made so its a lot of code). – Owen Kuhn Nov 26 '19 at 23:31
  • Thank you for your response it has been extremely helpful, however I just need to fix one thing please look at my edit above – Owen Kuhn Nov 26 '19 at 23:31
  • @genpfault I made an edit do you have any suggestions? Thank you so much! – Owen Kuhn Nov 27 '19 at 01:26
  • @OwenKuhn genpfault meant you really shouldn't create new texture each frame. Create it once at startup and update via either locking (prefered for performance reasons) or updatetexture, then rendercopy. It doesn't look like your source image and texture size matches though. Once that is fixed I'd recommend selecting single pixel and inspecting its colours as it it quite possible your source data is not what you think it should be. – keltar Nov 27 '19 at 04:31

0 Answers0