The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.
SDL
Simple Directmedia Layer Forums
Can't get my viewport to work properly
BiiXteR
Joined: 12 May 2016
Posts: 2
Posted: Fri May 13, 2016 11:49 am
I've been trying to add a viewport to my game using SDL2, however it just won't work.
What I want my viewport to do is to always follow the player, and how I do this is by assigning the players x and y to my viewport rect's x and y.
But, it doesn't work out as I expected it to, instead the viewport always "falls behind" or the player is faster than the viewport.
Here's a GIF on what my problem is :
As you can see, the viewport doesn't follow the player like it should.
This is my Camera class, where the viewport is initialized, drawn and so on :
Code:
Camera::Camera(Uint16 x, Uint16 y, Uint16 w, Uint16 h)
{
// Sets up the viewport positions and sizes
viewport.x = x;
viewport.y = y;
viewport.w = w;
viewport.h = h;
// Sets the viewport so that SDL uses it
SDL_RenderSetViewport(Window::GetRenderer(), &viewport);