Camera Not Working

Hello people. I’ve been trying to implement a camera that follows my player where it goes. But, I’m unable grasp the exact concept of what happens. What I believe happens is objects in the range of camera move out/in with same value as offsetX i.e. by the amount the player moves past the center of window.
Here’s the github link.
What happens in my code is that the tiles move by too fast and I don’t understand why?

It looks like you offset your tile based on the position of the player, which is not really what you want. What you want to do instead is positioning your camera in the game world and then offset your game objects, like tiles, based on that.

You already have a rect for your camera so offset your tile position by that.

If you want, you can pass in the camera rect to your drawTile function in the Tile class. Then, when it’s time to draw your tile, you draw it like this:

void Tile::drawTile(const SDL_Rect& cameraView)
{
	const SDL_Rect tileRect = {tile.x - cameraView.x, tile.y - cameraView.y, tile.w, tile.h};

	SDL_RenderCopy(gRenderer, texture, &src, &tileRect);
}

By doing it this way, you don’t need that shift_tile variable that you’re currently calculating and using.

So whenever your camera move to the right, your tile should then move to the left, giving the illusion that the camera scrolls through the game world.