当VSYNC打开时,鼠标在Linux中处理SDL2的速度非常慢且缓慢

问题描述

在SDL2的事件处理循环中,调用方法SDL_GetMouseState(&x,&y);或将event.motion.xevent.motion.y用作鼠标的相对坐标会使SDL2的响应性非常差。奇怪的是SDL_GetMouseState()event.motion.x and y快很多,但是它们都不好受。还有其他获取鼠标位置的方法吗?您甚至可以尝试一下。我在SDL2中设置了一个简单的文本程序来测试响应性的内容,例如滚动,在此偏移y值。尝试使用和不使用vsync以及使用和不使用鼠标pos的方式。我目前正在使用Linux Mint。

代码:(如果使用像我这样的代码块,则在项目所在的文件夹中需要arial.ttf)


#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <string>
using std::string;
using std::to_string;





int main(int argc,char* argv[])
{


    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();

    SDL_Window *window = SDL_CreateWindow("Test Program",30,1280,720,SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);// | SDL_WINDOW_SHOWN);
    SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );


    SDL_Event event;

    SDL_Point mousePos = {0,0};



    ///Fps vars
    int fpsCounter,fpsstart,fpsEnd;
    fpsstart = SDL_GetTicks();
    fpsEnd = SDL_GetTicks();
    fpsCounter = 0;
    TTF_Font *fpsFont = TTF_OpenFont("arial.ttf",30);
    SDL_Surface *fpsSurface = TTF_RenderText_Blended(fpsFont,"FPS:  ",{0,0});
    SDL_Texture *fpsTexture = SDL_CreateTextureFromSurface(renderer,fpsSurface);
    SDL_FreeSurface(fpsSurface);
    int textW,textH,yVal;
    yVal = 50;
    SDL_QueryTexture(fpsTexture,NULL,&textW,&textH);
    SDL_Rect fpsRect = {1000,yVal,textW,textH};



    bool running = true;
    while (running)
    {




        while ( SDL_PollEvent(&event) )
        {
            if (event.type == SDL_QUIT)
            {
                running = false;
                break;
            }


            else if (event.type == SDL_MOUSEMOTION){

                int x,y;
                SDL_GetMouseState(&x,&y);
                mousePos = {x,y};
                break;
            }


            else if (event.type == SDL_MOUSEWHEEL){

                if (event.wheel.y > 0){  ///Scrolling up here
                    yVal -= 50;
                    fpsRect.y = yVal;
                    break;
                }
                if (event.wheel.y < 0){  ///Scrolling down here
                    yVal += 50;
                    fpsRect.y = yVal;
                    break;
                }
            }

        }


        SDL_SetRenderDrawColor(renderer,255,255);
        SDL_RenderClear(renderer);



        //Update every 0.5s (500ms)
        fpsEnd = SDL_GetTicks();
        fpsCounter += 2;
        if ( (fpsEnd-fpsstart) > 500 ){
            fpsstart = SDL_GetTicks();

            SDL_DestroyTexture(fpsTexture);

            ///Change text
            string newText = ("FPS:  " + to_string(fpsCounter));
            fpsSurface = TTF_RenderText_Blended(fpsFont,newText.c_str(),0});
            fpsTexture = SDL_CreateTextureFromSurface(renderer,fpsSurface);
            SDL_FreeSurface(fpsSurface);
            SDL_QueryTexture(fpsTexture,&textH);
            fpsRect = {1000,textH};

            fpsCounter = 0;
        }


        SDL_Rendercopy(renderer,fpsTexture,&fpsRect);



        SDL_RenderPresent(renderer);


    }


    SDL_DestroyTexture(fpsTexture);
    TTF_CloseFont(fpsFont);


    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    TTF_Quit();
    SDL_Quit();



    return 0;

}


我不同意关闭VSYNC是一种解决方案。无论如何,VSYNC都不应该是这样。由于某种原因,它不是在Windows中这样,它在那里可以正常工作。用SDL_Delay()将FPS限制为显示的刷新率也给我同样的问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...