Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong SDL origin axis in 1080x1920 resolution

Tags:

c++

sdl

xrandr

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 ...

like image 413
user7364588 Avatar asked Dec 31 '25 14:12

user7364588


1 Answers

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;
like image 175
Tezirg Avatar answered Jan 03 '26 02:01

Tezirg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!