Image is All White

I have an image stored in memory as an array where the values are between 0 and 255. I would like to display this image to the screen using SDL. I am writing code in C.

I have written the following code based on tutorials that I found on the internet. My impression is that this should display an image. But instead, no matter how I change the data array, I always get a white window.

Can anyone tell me how to fix this? Any help is very appreciated.

void main( void ){
  SDL_Window* window;
  SDL_Surface* imgSurf;
  SDL_Surface* winSurf;
  SDL_Event sdlEvent;
  unsigned char* data;
  size_t i, j;
  bool running;
  size_t h, w;

  window = NULL;
  imgSurf = NULL;
  winSurf = NULL;

  h = 480;
  w = 640;

    (int) img->w, (int) img->h, SDL_WINDOW_ALLOW_HIGHDPI );

  winSurf = SDL_GetWindowSurface( window );

  // Make an image that's black on left and increasing to white on right
  data = malloc( sizeof( uint8_t ) * (h*w) );
  for( j=0; j < img->h; ++j ){
    for( i=0; i < img->w; ++i ){
      data[ i + j * img->w ] = (uint8_t) i;

  imgSurf = SDL_CreateRGBSurfaceFrom( (void*) data, (int) img->w, (int) img->h,
    8, w, 0, 0, 0, 0 );

  running = true;
  while( running ){

    if( SDL_WaitEvent( &sdlEvent ) ){
      // WaitEvent is appropriate if window responds to user events
      // If things happen without user events, use SDL_PollEvent

      switch( sdlEvent.type ){
        case SDL_QUIT:
          running = false;

    SDL_BlitSurface( imgSurf, NULL, winSurf, NULL );
    SDL_UpdateWindowSurface( window );
  SDL_FreeSurface( imgSurf );  imgSurf = NULL;
  SDL_FreeSurface( winSurf );  winSurf = NULL;

  free( data );

  SDL_DestroyWindow( window );

  return 0;

I think your problem is you’ve used SDL_WaitEvent which just sits in that function until an event happens. So your image is never drawn until you hit the quit button, and then the window is destroyed anyway.

Try changing the SDL_WaitEvent to SDL_PollEvent and then the code will carry on being executed and will loop around your while loop until you exit the app.

I use SDL_WaitEvent in my games but I set up a timer and then draw things every time the timer is called.

If you just want to test things you could alternatively do your Blit and Update before the while loop begins so things are drawn and then the app just sits in the SDL_WaitEvent until you hit the window’s exit button.