在Android中恢复后SDL-2速度变慢

时间:2019-07-14 09:13:03

标签: android sdl-2

我们在Android项目的UI中使用SDL-2。将应用程序发送到后台后恢复运行后,我们将面临速度下降的问题。由于我们的应用程序有点复杂,因此我在此分享一个小的工作片段,以再现同样的问题。

在代码段中,我只是在几毫秒后在不同位置绘制一个矩形。由于x坐标保持不变,矩形将似乎从左向右移动,矩形移动的速度取决于显示循环中的usleep()。一旦我们运行了应用程序,请单击主页按钮,然后从最近的应用程序列表中恢复该应用程序。恢复应用程序后,与启动应用程序时矩形移动的速度相比,矩形将开始缓慢移动。

当睡眠1秒钟时,速度不会有任何明显的延迟。也就是说,如果我每隔一秒钟移动一次矩形。

SDL_Window* window = NULL;

SDL_Renderer* renderer = 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
    {
        //Set texture filtering to linear
        if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
        {
            printf( "Warning: Linear texture filtering not enabled!" );
        }

        //Create window
        window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( window == NULL )
        {
            printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
            success = false;
        }
        else
        {
            //Create renderer for window
            renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
            if( renderer == NULL )
            {
                printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
                success = false;
            }
            else
            {
                //Initialize renderer color
                SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
            }
        }
    }

    return success;
}

int main( int argc, char* args[] )
{
    int i = 0;
    //Start up SDL and create window
    if( !init() )
    {
        printf( "Failed to initialize!\n" );
    }
    else {
        //Main loop flag
        bool quit = false;

        //Event handler
        SDL_Event e;

        //While application is running
        while (!quit) {
            //Handle events on queue
            while (SDL_PollEvent(&e) != 0) {
                //User requests quit
                if (e.type == SDL_QUIT) {
                    quit = true;
                }
            }

            SDL_Rect rect;
            SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);

            //Clear screen
            SDL_RenderClear(renderer);

            i += 10;
            SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
            rect.x = i % 1000;
            rect.y = 10;
            rect.w = 30;
            rect.h = 30;
            SDL_RenderFillRect(renderer, &rect);

            //Update screen
            SDL_RenderPresent(renderer);

            usleep(10);
        }
    }

    //Free resources and close SDL
    close();

    return 0;
}

在此先感谢您的帮助:)

0 个答案:

没有答案