The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Slow rendering
thyagobr


Joined: 15 Apr 2015
Posts: 2
Hi, everyone.

I'm a beginner in both C++ and SDL, and I'm slowly writing a small game to learn some concepts.
I'm having a problem, though: my rendering seems to be *really* slow.
Calculating with PerformanceCounters, I'm getting +65ish milliseconds per frame.
Could someone tell me what is wrong with my rendering function?
Thanks a million Razz

Code:

SDL_Texture *texture;

...

void render(int x_offset, int y_offset)
{
  if (texture)
  {
    SDL_DestroyTexture(texture);
  }

  texture = SDL_CreateTexture(renderer,
      SDL_PIXELFORMAT_ARGB8888,
      SDL_TEXTUREACCESS_STREAMING,
      texture_w,
      texture_h);

  if (SDL_LockTexture(texture, NULL, &pixel_memory, &pitch) < 0) {
    printf("Oops! %s\n", SDL_GetError());
  }

  Uint32 *pixel;
  Uint8 *row = (Uint8 *) pixel_memory;

  for (int j = 0; j < texture_h; ++j) {
    pixel = (Uint32 *)((Uint8 *) pixel_memory + j * pitch);
    for (int i = 0; i < texture_w; ++i) {

      Uint8 alpha = 255;
      Uint8 red = 172;
      Uint8 green = 0;
      Uint8 blue = 255;

     *pixel++ = ((alpha << 24) | (red << 16) | (green << 8) | (blue));

    }
  }
  SDL_UnlockTexture(texture);
}
[/b]
Naith


Joined: 03 Jul 2014
Posts: 158
Pixel manipulation on a texture is always a expensive process (as I understand) so that might be the reason.
Also, you're creating- and destroying a texture each time the render function is called, so each frame. If you have the opportunity, create the texture 1 time, at the start of the program, and destroy it at program shutdown.

I'm not really sure what you're doing, or trying to do, in your code with the pixel manipulation thing so the only advice I can give at the moment is to, like I said, create the texture only 1 time, at program startup, and leave the pixel manipulation code in the render function.
thyagobr


Joined: 15 Apr 2015
Posts: 2
Okay, so maybe I should explain why I'm doing that.

I wasn't before. But I ran into a very strange bug: when I added SDL_INIT_GAMECONTROLLER to the Init function, my game would break as soon as I'd resize it on the Height only. I mean, I could resize it's Width with no problem, but if the Height would be resized up - more precisely, above it's initial Height set on SDL_CreateWindow() - I'd get a segmentation fault.

After some (a lot of) hacking and slashing through the code, I found out that the problem was happening on a SDL_GetWindowSize(window, &texture_w, &texture_h) call I was making inside the event loop, on a SDL_WINDOWEVENT_SIZE_CHANGED capture. I removed it, all was fine (but with no resizing, of course). I found out that if I'd try to set the value of texture_h (yes, I had no problems messing with texture_w) directly, the program crashed with a segfault.

My next step was to re-allocate the texture with every loop, effectively creating a new texture with the correct sizes from the get-go. It worked.

My code was probably broken (you can see the actual commit that fixed my issue here), but what really puzzled me was "why only when I load SDL_INIT_GAMECONTROLLER? =]

Anyways, this is why I'm re-creating the texture every time.