4
\$\begingroup\$

I know about SDL_RenderSetScale, but I need also horizontal/vertical shift of the renderer's origin. At the moment, I manually modify positions of all screen objects with the shift. Is there a way to set this shift/offset globally?

\$\endgroup\$
3
\$\begingroup\$

No. You might try tinkering around with SDL_RenderSetViewport, but that might not even work, and if it did, you wouldn't be using it for what it is supposed to be used.

The best solution here is to add your own wrapper layer, perhaps inside your own custom renderer class. You would proxy all rendering calls trough a specific method, and that method would automatically offset the rendering for you.

So, manually modifying the positions is the best and most reasonably way to go here.

\$\endgroup\$
1
\$\begingroup\$

I like to use a Camera class, which contains logic to smoothly track the player. When I want to render, I project my SDL_Rects through the camera with some convenience functions like this:

    void Camera::project(SDL_Rect& rect) {
        rect.x += -floorToInt(position.x) + floorToInt(width * 0.5f);
        rect.y += -floorToInt(position.y) + floorToInt(height * 0.5f);
    }```    
SDL_Rect Camera::getProjected(const SDL_Rect& inRect) {
    SDL_Rect rect = inRect;
    rect.x += -floorToInt(position.x) + floorToInt(width * 0.5f);
    rect.y += -floorToInt(position.y) + floorToInt(height * 0.5f);
    return rect;
}```

The offset is the inverse of the camera's position, plus an additional offset to place the camera focal point at the center of the screen.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.