Render map from .map file


#1

I’m making a simple platforming game in SDL 2.0 and C++.
I’m having a few problems, one of them is getting a map loader working, currently I have something mashed together from some tutorials and some of my own stuff.

Here are all the files in question (Since I can only post 2 links, I’ll just post the link to the github repo)
The Github Repo
The files that I’m talking about are:

  • Map.h
  • Map.cpp
  • Tile.h
  • Tile.cpp
  • Window.h
  • Window.cpp
  • .Map file
  • The tileset.png

So I use Map::OnLoad() to load the map from the .map file, then while it’s loading, all the tiles are being pushed to my Tile list “TileList”. Then I use Map::OnRender() to render it tile by tile.

Now I’m not quite sure where my error roots from or why it’s happening, but I think it’s in my Map::OnRender() where I actually render it using
SDL_Rect _SrcTile = { TilesetX, TilesetY, TILE_SIZE, TILE_SIZE }; SDL_Rect _DstRect = { MapX, MapY, tY, tX }; SDL_RenderCopy(_renderer, _tileset, &_SrcTile, &_DstRect);
When I run the game, this is what I end up with:


I’m not getting any errors, but it’s not at all what I expect to happen, how can I fix this?


#2

Before someone here start going through your code base to find any error(s), what kind of output do you expect to get? What in your screenshot is incorrect etc?
The more information you include in your post, the better.


#3

Looking at your _DstRect, perheps you meant { MapX+tX, MapY+tY, TILE_SIZE, TILE_SIZE }?

Also you might have tx and ty swapped in the _DstRect you posted.

Make sure to also read up on how SDL_RenderCopy works. The wiki is a great resource!


#4

I was expecting a full map to be rendered, like blue, pink and white, not just one huge pink tile


#5

That seems to have fixed it somewhat, but now this is what I’m getting:


Thanks for the help though!


#6

Ok I messed with the values a bit, and got it to work:


Thanks very much for your help!