SDL_SetPaletteColors crashes program

The program is supposed take pixel data from a uni dimensional array and display it. The pixel data is supposed to be 1 byte per pixel, which is supposed to result in a gray scale image.

The result is supposed to look like this:

gradient

Th problem i run into is that the “SDL_SetPaletteColors” command crashes the program.

What am I doing wrong here?

Here is the code:

#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 100;
const int SCREEN_HEIGHT = 100;

char* pixels;
int icnt,icnt2;

//Starts up SDL and creates window
bool init();

//Frees media and shuts down SDL
void close();

//The window we’ll be rendering to
SDL_Window* gWindow = NULL;

//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//The image we will load and show on the screen
//SDL_Surface* gHelloWorld = NULL;
SDL_Surface* gHelloWorld = NULL;

bool init()
{
//Initialization flag
bool success = true;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
    printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    success = false;
}
else
{
    //Create window
    gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
    if( gWindow == NULL )
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        //Get window surface
        gScreenSurface = SDL_GetWindowSurface( gWindow );
    }
}

return success;

}

void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;

//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;

//Quit SDL subsystems
SDL_Quit();

}

int main( int argc, char* args[] )
{
pixels = new char[10000]; //pixel array
icnt2=0;
SDL_Color colors[256];

for(icnt=0;icnt<10000;icnt++)  //gradient test image generator
{
  pixels[icnt]=(char)icnt2;
  icnt2++;
  if(icnt2 == 99){icnt2 =0;}
}



//Start up SDL and create window
if( !init() )
{
    printf( "Failed to initialize!\n" );
}
else
{

        gHelloWorld = SDL_CreateRGBSurfaceFrom((void*)pixels,
        100,
        100,
        8,
        100,
        0x000000FF,
        0x0000FF00,
        0x00FF0000,
        0);

        for(icnt = 0; icnt < 255; icnt++)
        {
            colors[icnt].r = colors[icnt].g = colors[icnt].b = icnt;
        }

        //program crashes here   ------------------
        SDL_SetPaletteColors(gHelloWorld->format->palette, colors, 0, 255);   //program crashes here
        //-----------------------------------------

        //Apply the image
        SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );

        //Update the surface
        SDL_UpdateWindowSurface( gWindow );

        //Wait two seconds
        SDL_Delay( 2000 );
    //}
}

//Free resources and close SDL
close();

return 0;

}

I’m not certain, but have you null-checked the surface, format, and palette?

Hey Jonny.

That was the issue.

This solved the problem…

gHelloWorld = SDL_CreateRGBSurfaceFrom((void*)pixels,
100,
100,
8,
100,
0x00000000,
0x00000000,
0x00000000,
0);