int main(int argc ,char* argv[]){
//    freopen( "CON", "wt", stdout );
//    SelfMadeBlocks(0);
    GameInitial();

    Sint32 key;
    SDL_Event gameEvent;

    MultThread = SDL_CreateThread(ThreadFunc,NULL);
    AIThread   = SDL_CreateThread(AIMain,NULL);
    while( gameOver == false ){
        //press ESC or click X to quit.
        while( SDL_PollEvent(&gameEvent) != 0 ){
            if ( gameEvent.type == SDL_QUIT ){
                gameOver = true;
            }
            if ( gameEvent.type == SDL_KEYDOWN ){
                if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
                    gameOver = true;
                    return 0;
                }
                key = gameEvent.key.keysym.sym;
                if( key==SDLK_BACKSPACE ){
                    AIMode ^= 1;
                    Operation.ClearQ();
                    if( AIMode ){
                        Search();
                    }
                }
//                if( AIMode ){
//                    continue;
//                }
                switch( key ){
                    case SDLK_DOWN: AIPlayer.Operation.AddQ(MOVEDOWN);
                                    break;
                    case SDLK_LEFT: AIPlayer.Operation.AddQ(MOVELEFT);
                                    break;
                    case SDLK_RIGHT:AIPlayer.Operation.AddQ(MOVERIGHT);
                                    break;
                    case SDLK_UP  : AIPlayer.Operation.AddQ(ROTATE);
                                    break;
                    case SDLK_TAB : AIPlayer.Operation.AddQ(TURNNEXT);
                                    break;
                }
            }
        }
//        key = SDL_GetKeyState(NULL);                          //实现长按效果的第一个方法
//        if( key[SDLK_DOWN] )  Operation.AddQ(MOVEDOWN);
//        if( key[SDLK_LEFT] )  Operation.AddQ(MOVELEFT);
//        if( key[SDLK_RIGHT] ) Operation.AddQ(MOVERIGHT);
//        if( key[SDLK_UP] )    Operation.AddQ(ROTATE);

        SDL_Delay(1);                 //减轻CPU负担
    }
    SDL_KillThread(MultThread);
    SDL_KillThread(AIThread);
	pressESCtoQuit();
    return 0;
}
int main(int argc, char* argv[])
{
    if ( SDL_Init(SDL_INIT_VIDEO) != 0 )
    {
        puts("SDL_INIT failed...");
        return 1;
    }
#if 1
    const int SCREEN_WIDTH = 0;    // 0 means use current width.
    const int SCREEN_HEIGHT = 0;    // 0 means use current height.
    const int SCREEN_BPP = 0;        // 0 means use current bpp.
#else
    const int SCREEN_WIDTH = 640;    // 0 means use current width.
    const int SCREEN_HEIGHT = 480;    // 0 means use current height.
    const int SCREEN_BPP = 32;        // 0 means use current bpp.
#endif
    const Uint32 SCREEN_FLAGS = SDL_SWSURFACE;    // SDL_SWSURFACE == 0,surface in system memory.

    SDL_Surface* pScreen = 0;
    pScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SCREEN_FLAGS);    // Creat a SDL window, and get the window's surface.
    if ( pScreen == 0 )
    {
        puts("SDL_SetVideoMode() failed!\n");
        SDL_Quit();
        return 1;
    }

    SDL_Surface* pShownBMP = 0;

    pShownBMP = IMG_Load(argv[1]); // Load a BMP file, and convert it as a surface.
    //pShownBMP = SDL_LoadBMP("picture.bmp"); // Load a BMP file, and convert it as a surface.
    if ( pShownBMP == 0 )

    {
        puts("SDL_LoadBMP() failed!\n");
        SDL_Quit();
        return -1;
    }

    SDL_Rect* pSrcRect = 0;    // If pSrcRect is NULL, the entire source surface is copied. 

    SDL_Rect* pDstRect = 0;    // If pDstRect is NULL, then the destination position (upper left corner) is (0, 0).
    if ( SDL_BlitSurface(pShownBMP, pSrcRect, pScreen, pDstRect) != 0 )    // Put the BMP's surface on the SDL window's surface.
    {
        puts("SDL_BlitSurface() failed!\n");
        SDL_Quit();
        return -1;
    }


    if ( SDL_Flip(pScreen) != 0 )    // Show the SDL window's surface.
    {
        puts("SDL_Flip() failed!\n");
        SDL_Quit();
        return -1;
    }

    pressESCtoQuit();
    SDL_Quit();

    return 0;
}