Failed Camera Movement

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.

Can you please recreate the repo, I’m on vacations now so I’ll study your code.

I’m not sure if I still have the code, but I’ll check.

If I find it and reupload it to my github, you should just download the whole repository and view/edit/use it offline, since it’ll eventually be deleted some day again.

Sorry for making you go through the trouble. I’ll definitely download it this time. I didn’t know that repos self-destructed.

No worries.
I apparently hadn’t deleted it yet, only set it to private. You can go ahead and download the repository from the link above.

And no, repositories doesn’t get automatically deleted. What I mean is that I usually delete repositories that I don’t need and have no use for, and that’s the reason why it will be removed soon.

And like I’ve written before; there’s still some issues with the code that needs fixing, especially the handling of the tile textures, but the code should be good enough to get you started on making a simple tile-based game.

oh okay I’ll download the code.

I’ll at least have a simple working game. That’s enough for me to feel like a developer.

When I try to run your code I’m greeted with these errors:

Everything compiled fine last time I built it.

Make sure that all the *.h and *.cpp files are added to the project, in this case Game.h and Game.cpp seems to be missing.

I don’t really know what’s wrong. You should remove the files from the project and re-add them, then try rebuilding and so on.

If you can’t make it compile properly, upload the whole Visual Studio project to your github and I’ll take a look at it. That means all the project files, like *.sln, *.vcproj etc, and not just the *.h and *.cpp files.

I’ve uploaded everything on github here’s the link.

There is nothing wrong with your C++ code. I have zero experience with Visual Studio but seeing all those red minus symbols before each file except main.cpp makes me think that maybe they are not part of the project or are excluded in some way. When I look in the .vcxproj I only find main.cpp there.

So what I think is happening is that it only compiles main.cpp. This is consistent with the linker error messages that you got above. I.e. it doesn’t find the implementation of the constructor and other member functions of the Game class because Game.cpp is not being compiled (and linked).