Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SDL冻结(黑屏)_C++_Sdl - Fatal编程技术网

C++ SDL冻结(黑屏)

C++ SDL冻结(黑屏),c++,sdl,C++,Sdl,这是我的全部代码。 当我右键单击几次(动画正常工作)时,屏幕冻结。我认为if(SDL\u GetTicks()-start\u timeformat,0,0xFF,0xFF)); } } //返回优化的曲面 返回优化年龄; } void apply_surface(int x,int y,SDL_surface*源,SDL_surface*目标,SDL_Rect*剪辑=NULL) { //保留偏移量 垂直偏移量; //得到补偿 偏移量x=x; 偏移量y=y; //布利特 SDL_BlitSurf

这是我的全部代码。 当我右键单击几次(动画正常工作)时,屏幕冻结。我认为
if(SDL\u GetTicks()-start\u time<1000)
on中的行是问题所在(因为执行太多?)

const int SCREEN\u WIDTH=600;
屏幕内的常数_高度=500;
屏幕常数(BPP=32);
//表面
SDL_表面*背景=空;
SDL_Surface*upMessage=NULL;
SDL_Surface*downMessage=NULL;
SDL_Surface*leftMessage=NULL;
SDL_表面*rightMessage=NULL;
SDL_表面*信息=NULL;
SDL_表面*屏幕=空;
SDL_表面*光洁度=零;
SDL_表面*startStop=NULL;
SDL_曲面*one=NULL;
SDL_曲面*two=NULL;
SDL_表面*3=零;
SDL_曲面*four=零;
SDL_表面*5=零;
//事件结构
SDL_事件;
SDL_表面*加载_图像(标准::字符串文件名){
//加载的图像
SDL_表面*LoadeImage=NULL;
//将使用的优化曲面
SDL_曲面*optimizedImage=NULL;
//加载图像
LoadeImage=IMG_Load(filename.c_str());
//如果图像已加载
如果(LoadeImage!=NULL)
{
//创建优化的曲面
optimizedImage=SDL_显示格式(LoadeImage);
//释放旧表面
SDL_自由曲面(加载图像);
//如果曲面经过优化
如果(optimizedImage!=NULL)
{
//彩色键表面
SDL_SetColorKey(optimizedImage、SDL_SRCCOLORKEY、SDL_MapRGB(optimizedImage->format,0,0xFF,0xFF));
}
}
//返回优化的曲面
返回优化年龄;
}
void apply_surface(int x,int y,SDL_surface*源,SDL_surface*目标,SDL_Rect*剪辑=NULL)
{
//保留偏移量
垂直偏移量;
//得到补偿
偏移量x=x;
偏移量y=y;
//布利特
SDL_BlitSurface(源、剪辑、目标和偏移);
}
boolinit()
{
//初始化所有SDL子系统
if(SDL_Init(SDL_Init_EVERYTHING)=-1)
{
返回false;
}
//设置屏幕
屏幕=SDL_设置视频模式(屏幕宽度、屏幕高度、屏幕BPP、SDL_表面);
//如果设置屏幕时出错
如果(屏幕==NULL)
{
返回false;
}
//设置窗口标题
SDL_WM_SetCaption(“按箭头键”,NULL);
//如果一切都好的话
返回true;
}
bool load_文件()
{
//加载背景图像
背景=加载图片(“background.png”);
message=load_图像(“guy.png”);
finish=load_图像(“finish.png”);
//如果加载后台时出现问题
如果(背景==NULL)
{
返回false;
}
//如果一切顺利的话
返回true;
}
无效清除()
{
//释放表面
SDL_自由曲面(背景);
SDL_自由曲面(upMessage);
SDL_自由曲面(向下消息);
SDL_自由曲面(leftMessage);
SDL_自由曲面(右消息);
SDL_自由曲面(startStop);
SDL_自由曲面(一个);
SDL_自由曲面(两个);
SDL_自由曲面(三个);
SDL_自由曲面(四个);
SDL_自由曲面(五个);
//退出SDL
SDL_退出();
}
无效动画(SDL_表面*编号,标准::字符串名称[],整数n){
数字=空;
数字=加载_图像(名称[n]+“.png”);
应用_表面(0,0,数字,屏幕);
}
int main(int argc,char*args[]{
bool-quit=false;
bool start=true;
bool-cilj_检验=真;
int x=35,y=10;
布尔动画=假;
Uint32启动时间=0;
//计时器启动/停止标志
bool running=false;
//初始化
if(init()==false)
{
返回1;
}
//加载文件
如果(加载_文件()==false)
{
返回1;
}
//应用背景
应用_表面(0,0,背景,屏幕);
//而用户还没有退出
while(quit==false){
//如果有事件要处理
if(SDL_事件和事件)){
//如果按了一个键
if(event.type==SDL\u KEYDOWN){
//设置正确的消息面
开关(event.key.keysym.sym){
案例SDLK\U UP:
打破
案例SDLK_向下:
开始=错误;
如果(y!=410&&animation==false){
y+=100;
SDL_FillRect(屏幕,0,0);
背景=空;
消息=NULL;
背景=加载图片(“background.png”);
应用_表面(0,0,背景,屏幕);
finish=load_图像(“finish.png”);
应用于表面(506、420、表面处理、筛网);
message=load_图像(“guy.png”);
应用_表面(x、y、信息、屏幕);
//if()
}
打破
案例SDLK_左:
打破
案例SDLK_右:
开始=错误;
如果(x!=535&&animation==false){
x+=100;
SDL_FillRect(屏幕,0,0);
背景=空;
消息=NULL;
背景=加载图片(“background.png”);
应用_表面(0,0,背景,屏幕);
finish=load_图像(“finish.png”);
应用于表面(506、420、表面处理、筛网);
message=load_图像(“guy.png”);
应用_表面(x、y、信息、屏幕);
如果(x==535&&y==410){
start_time=SDL_GetTicks();
动画=真;
x=35,y=10;
}
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 500;
const int SCREEN_BPP = 32;

//The surfaces
SDL_Surface *background = NULL;
SDL_Surface *upMessage = NULL;
SDL_Surface *downMessage = NULL;
SDL_Surface *leftMessage = NULL;
SDL_Surface *rightMessage = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *finish = NULL;
SDL_Surface *startStop = NULL;
SDL_Surface *one = NULL;
SDL_Surface *two = NULL;
SDL_Surface *three = NULL;
SDL_Surface *four = NULL;
SDL_Surface *five = NULL;
//The event structure
SDL_Event event;

SDL_Surface *load_image( std::string filename ){
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Press an Arrow Key", NULL );

    //If everything initialized fine
    return true;
}

bool load_files()
{
    //Load the background image
    background = load_image( "background .png" );
    message = load_image( "guy.png" );
    finish = load_image( "finish .png" );


    //If there was a problem in loading the background
    if( background == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surfaces
    SDL_FreeSurface( background );
    SDL_FreeSurface( upMessage );
    SDL_FreeSurface( downMessage );
    SDL_FreeSurface( leftMessage );
    SDL_FreeSurface( rightMessage );
    SDL_FreeSurface( startStop );
    SDL_FreeSurface( one );
    SDL_FreeSurface( two );
    SDL_FreeSurface( three );
    SDL_FreeSurface( four );
    SDL_FreeSurface( five );
    //Quit SDL
    SDL_Quit();
}
void anim(SDL_Surface *number,std::string name[],int n){
                        number = NULL;
                        number = load_image(name[n] +".png" );
                        apply_surface( 0, 0, number, screen );
}
int main( int argc, char* args[] ){
    bool quit = false;
    bool start = true;
    bool cilj_test = true;
    int x=35,y=10;
    bool animation = false;
    Uint32 start_time = 0;
    //The timer start/stop flag
    bool running = false;
    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

    //Apply the background
    apply_surface( 0, 0, background, screen );

    //While the user hasn't quit
    while( quit == false ){
        //If there's an event to handle
        if( SDL_PollEvent( &event ) ){

            //If a key was pressed
            if( event.type == SDL_KEYDOWN ){
                //Set the proper message surface
                switch( event.key.keysym.sym ){
                    case SDLK_UP:
                        break;
                    case SDLK_DOWN:
                        start=false;
                        if(y!=410 && animation==false){
                        y+=100;
                        SDL_FillRect( screen, 0, 0 );
                        background = NULL;
                        message = NULL;
                        background = load_image( "background .png" );
                        apply_surface( 0, 0, background, screen );
                        finish = load_image( "finish.png" );
                        apply_surface(506, 420, finish, screen );
                        message = load_image( "guy.png" );
                        apply_surface( x, y, message, screen );
                    //    if()
                        }
                         break;
                    case SDLK_LEFT:
                    break;
                    case SDLK_RIGHT:
                        start=false;
                        if(x!=535 && animation==false){
                        x+=100;
                        SDL_FillRect( screen, 0, 0 );
                        background = NULL;
                        message = NULL;
                        background = load_image( "background.png" );
                        apply_surface( 0, 0, background, screen );
                        finish = load_image( "finish.png" );
                        apply_surface(506, 420, finish, screen );
                        message = load_image( "guy.png" );
                        apply_surface( x, y, message, screen );
                        if(x==535 && y==410){
                            start_time=SDL_GetTicks();
                            animation=true;
                            x=35,y=10;
                        }

                        }
                        break;
                }
            }
            //If the user has Xed out the window
            else if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }

        if(animation){
            std::string name[5]={"one","two","three","four","five"};
          if(SDL_GetTicks() -  start_time < 1000){
               anim(one,name,4);
            } else  if(SDL_GetTicks() -  start_time > 1000 && SDL_GetTicks() -  start_time < 2000){
               anim(two,name,3);
            } else  if(SDL_GetTicks() -  start_time > 2000 && SDL_GetTicks() -  start_time < 3000){
               anim(three,name,2);
            } else  if(SDL_GetTicks() -  start_time > 3000 && SDL_GetTicks() -  start_time < 4000){
               anim(four,name,1);
            } else if(SDL_GetTicks() -  start_time > 4000 && SDL_GetTicks() -  start_time < 5000){
               anim(five,name,0);
            } else if(SDL_GetTicks() -  start_time > 5000){
                        SDL_FillRect( screen, 0, 0 );
                        background = NULL;
                        message = NULL;
                        background = load_image( "background.png" );
                        apply_surface( 0, 0, background, screen );
                        finish = load_image( "finish.png" );
                        apply_surface(506, 420, finish, screen );
                        message = load_image( "guy.png" );
                        apply_surface( x, y, message, screen );
                        animation=false;
                        start_time=0;
            }
        }

        if( message != NULL ){
            if(start){
            apply_surface(35, 10, message, screen );
            apply_surface(506, 420, finish, screen );
            }

            message = NULL;
        }

        //Update the screen
        if( SDL_Flip( screen ) == -1 ){
            return 1;
        }
    }

    //Clean up
    clean_up();

    return 0;
}