SDL_SetPaletteColors崩溃程序

时间:2018-04-04 17:28:46

标签: c++ sdl-2

该程序假定从单维数组中获取像素数据并显示它。假设像素数据是每个像素1个字节,这应该导致灰度图像。

结果应该是这样的:

GRADIENT PATTERN

我遇到的问题是“SDL_SetPaletteColors”命令使程序崩溃。

我在这里做错了什么?

以下是代码:

#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;
}

0 个答案:

没有答案