UpdateRgbSurface()
static void UpdateRgbSurface(SdlResources* sdl_resources,
unsigned char* rgb_buffer) {
unsigned char const kAlpha = 255U;
int y = 0;
int x = 0;
SDL_assert(sdl_resources->surface != NULL);
SDL_assert(sdl_resources->window_surface != NULL);
SDL_LockSurface(sdl_resources->surface);
/* Copy pixels from the source buffer (rgb_buffer) into SDL's RGBA surface. */
for (y = 0; y < kSurfaceHeight; ++y) {
for (x = 0; x < kSurfaceWidth; ++x) {
Uint32* pixel = NULL;
int const kBytesPerPixel = sdl_resources->surface->format->BytesPerPixel;
int const kRowSize = sdl_resources->surface->pitch;
int const kOffset = x * kBytesPerPixel + y * kRowSize;
Uint8* const kPixels = (Uint8*)sdl_resources->surface->pixels;
SDL_PixelFormat const* kPixelFormat = sdl_resources->surface->format;
int const kSourceOffset = kSourceColorChannels * (x + kSurfaceWidth * y);
unsigned char const kRed = rgb_buffer[kSourceOffset];
unsigned char const kGreen = rgb_buffer[kSourceOffset + 1];
unsigned char const kBlue = rgb_buffer[kSourceOffset + 2];
Uint32 const kColor =
SDL_MapRGBA(kPixelFormat, kRed, kGreen, kBlue, kAlpha);
pixel = (Uint32*)(kPixels + kOffset);
*pixel = kColor;
}
}
SDL_UnlockSurface(sdl_resources->surface);
}
If I get callgrind right, this is the slowest function I have.
What can I do to speed it up?
Note: struct PixelBuffer
is just an array of RGB pixels (24-bit, 3 unsigned chars).
I use C89 and SDL 2.0.