3

我遇到了 SDL 库的问题,并且分辨率与 1920x1080 不同。

我想在分辨率为 1080x1920(纵向)的屏幕中央复制显示尺寸为 1080x608 的图像。

我只有一个插入的监视器屏幕。

我使用以下命令将屏幕从1920x1080切换到1080x1920

xrandr --output DP-1 --mode 1920x1080 --rotate left --primary

我正在使用以下代码来初始化 SDL 渲染器:

/**
 * initialize everything so we are ready to display
 */
int SdlHandler::initialize(
    unsigned int positionX,
    unsigned int positionY,
    unsigned int width,
    unsigned int height,
    bool showWindow,
    std::string name) {
    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;

            return -1;
    }

    // Size if the window
    this->width = width;
    this->height = height;
    this->positionX = positionX;
    this->positionY = positionY;

    // Create the SDL window
    // 0 and 0 are the position in X and Y
    unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS;

    if (showWindow) {
            flags |= SDL_WINDOW_SHOWN;
    } else {
            flags |= SDL_WINDOW_HIDDEN;
    }

    this->window = SDL_CreateWindow(name.c_str(), this->positionX, this->positionY, this->width, this->height, flags);

    // If there had been a problem, leave
    if (!this->window) {
            return -1;
    }

    // Create a new renderer
    this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    // If there is an error creating it, just leave
    if (!this->renderer) {
            return -1;
    }

    // Setup the best for the SDL render quality
    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");

    return 0;
}

然后,我调用该SDL_RenderCopy函数来显示图像。我将使用上述代码创建的renderer创建传递给它:SDL_CreateRenderer

// Create a window at 0,0 of dimension 1080x1920
this->initialize(0, 0, 1080, 1920, true, SDL_BASE_DISPLAY_WINDOW);

// Create the SDL Rectangle that will contain the image, at the center of the window
SDL_Rect *howToDraw = new SDL_Rect();
howToDraw->x = this->positionX + floor((this->width - this->imageWidth) / 2);
howToDraw->y = this->positionY + floor((this->height - this->imageHeight) / 2);
howToDraw->w = this->imageWidth;
howToDraw->h = this->imageHeight;

SDL_RenderCopy(this->renderer, this->texture, NULL, howToDraw);

但是轴似乎在错误的位置,得到以下结果: 在此处输入图像描述

 编辑和解决方案

这是一个与窗口管理器 Compton 相关的错误,没有 Compton 一切正常...

4

1 回答 1

2

由于您正在使用 旋转显示器xrandr,我们可以认为这是一个后处理步骤,它将在每个帧缓冲区渲染后旋转所有内容。

由于此后期处理步骤将 1920x1080 图像分辨率作为输入,因此您应该使用此分辨率的 SDL。

如果您将代码更改为:

// Create a window at 0,0 of dimension 1920x1080
this->initialize(0, 0, 1920, 1080, true, SDL_BASE_DISPLAY_WINDOW);

编辑:我也知道您希望图像从窗口的中心开始,但是您将图像的中间放置在窗口的中心。

您还应该尝试以下方法:

howToDraw->x = this->positionX + this->imageWidth / 2;
于 2018-11-28T11:30:46.397 回答