Failed Camera Movement

Hey all, I’ve been trying to implement a simple 2D camera for my game and it doesn’t work as expected. When I start my game the screen is all black with only my player visible and all the tiles disappeared.
Here’s a link to my code.

Have you tried what I suggested to you here: Camera Not Working?
That post has a link to the same github so I assume you’re the same person as that user.

Yes I did try that, If I understand correctly I’m doing what you suggested. I can’t understand what the camera really does, I tried lazy foo’s camera tut too.

So why did you create a new account and started this thread instead of asking in the original post I linked to? Oh well…

I checked the code in the drawTile function inside the Tile class and you’re not doing what I told you to do.

void Tile::drawTile() {
	tile.x -= cam->getX();
	tile.y -= cam->getY();
	SDL_RenderCopy(gRenderer, texture, &src, &tile);

Each time the drawTile function is called by your Game class, each created tile’s X- and Y position is being moved away from their original position, by the amount that cam->getX() and cam->getY() contains.
So let’s say that getX() equals to -10. That means that, everytime drawTile is called, the tile will move 10 pixels to the left, and that will continue as long as drawTile is called.

What you need to do instead is creating a temporary SDL_Rect in the drawTile function, holding the tile’s original position and then subtract the camera’s offset from that.

void Tile::drawTile() {
	const SDL_Rect tileRect = {tile.x - (int)cam->getX(), tile.y - (int)cam->getY(), tile.w, tile.h};

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

Notice that I don’t alter the tile’s original position in any way. I leave it as is and use that original position to render the tile, with the camera’s position acting as an offset.
Notice also that I pass in the tileRect to the SDL_RenderCopy function, instead of the tile rect.

After you’ve done exactly what I wrote above, try move the camera to the right with some key on the keyboard (use the setPos function in the Camera class). If you’ve done everything correctly, you should see the tile(s) moving to the left.
Then if you try moving the camera to the left (and, again, if you’ve done everything correctly), you should see the tile(s) moving to the right.

1 Like

I logged out and forgot the password.

This time I think I did exactly what you told me, so the camera works fine but the collision’s messed up.
I can dm you the video of what’s on the screen cause it won’t let me post any attachments.

I saw the message that you completely fixed my code but I couldn’t go through it cause of work.

I accidently removed the github repository. I still have the code on my computer though. Did you manage to download the code before it was deleted?

No, I had your code opened in github itself.

I re-created the github repository containing the code.
You can check it out and/or download it here:

There’s still some code in that repository that needs some fixing up and refacturing, but that will have to do for now.

The fact that each tile is loading its own SDL_Texture* is one thing that really needs fixing. Instead of each tile creating a texture, each tile texture should be loaded somewhere else and be reused/shared for every tile in the tilemap. An even better solution is to have one(1) SDL_Texture containing ALL the tiles for the game and share that texture with every tile.

Thank you I’ll work on that.