3

I am facing an issue with the SDL library and different resolution than 1920x1080.

I want to copy display an image of dimension 1080x608 at the center of a screen of resolution 1080x1920 (portrait).

I have only one plugged monitor screen.

I used the following command to switch screen from 1920x1080 to 1080x1920 :

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

I am using the following code to initialize the SDL renderer :

/**
 * 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;
}

Then, i call the SDL_RenderCopy function to display the image. I pass it the created renderer created with theSDL_CreateRenderer on the above code :

// 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);

But the axis seems to be at the wrong position, igot the following result : enter image description here

 EDIT AND SOLUTION

This was a bug related to Compton, the window manager, everything is working good without Compton ...

Community
  • 1
  • 1
user7364588
  • 1,014
  • 2
  • 12
  • 32
  • What result did you *expect*? – Some programmer dude Nov 28 '18 at 11:14
  • @Someprogrammerdude i expected to have the origin 0,0 on top - left corner of the monitor, but actually it is on the top - left corner of a monitor that does not exist, being above. I made an arrow on the image "I want the origin here" – user7364588 Nov 28 '18 at 12:06

1 Answers1

2

Since you are rotating your display using xrandr, we can consider this is a post processing step that will rotate everything after each framebuffer is rendered.

Because this post processing step takes a 1920x1080 image resolution as input, you should use the SDL at this resolution.

What if you change your code for:

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

EDIT: I also understand that you want your image to start at the center of the window, but your are placing the middle of the image at the center of the window.

You should also try the following:

howToDraw->x = this->positionX + this->imageWidth / 2;
genpfault
  • 51,148
  • 11
  • 85
  • 139
Tezirg
  • 1,629
  • 1
  • 10
  • 20
  • Sorry for the missunderstanding, the xrandr command is computed at first, before running program. When the program starts running, the screen is already **1080x1920**. The image resolution is **1080x608** – user7364588 Nov 28 '18 at 12:04
  • @Alrick It doesn't matter *when* the command runs, the rotation is done after everything is drawn on a 1920x1080 display. – Some programmer dude Nov 28 '18 at 12:12
  • Yes, but the rotation will still happen after drawing the frame before displaying on the physical screen. – Tezirg Nov 28 '18 at 12:13