Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Tilemap rendering 2D RPG

$
0
0
Good evening all, so it has been a while since I've posted but I am back to working on some stuff now that I have time. I'm working out some issues with a game I'm working on which is an old school 2D top down style JRPG. I'm working out the code for rendering the map to the screen which I know is something that has been done a million times and believe me I have read through close to hundreds of articles and tried so many changes to my code to find out what is going on and what is causing the error I'm having and I still can't track down what I'm doing wrong so I have decided that it is time for a fresh set of eyes to look at my code. Let me set the stage for what I'm doing I have my map setup so that I can have multiple layers to the map, to give the world some depth, and I am using a Vector to hold my array of int values that are used to decide the tiles to render to the screen. The issue I am having is somewhere between my rendering and my update code if I try to move the map at all on the screen I immediately get a Vector index out of bounds error and I can't track down what I'm doing wrong. I know it is probably something simple but I can't see it. Here is my update method: void Layer::UpdateLayer(float deltaTime) { // update the draw position based on scroll speed, velocity and the change in time from frame to frame m_currentDrawPosition.x += (m_scrollSpeed.x * m_velocity.x) * deltaTime; m_currentDrawPosition.y += (m_scrollSpeed.y * m_velocity.y) * deltaTime; // check to see that the draw position isn't less than 0 if (m_currentDrawPosition.x < 0) { m_currentDrawPosition.x = 0; } // check to see that the draw position isn't greater than the size of the map if ( (m_mapDimensions.x >= RenderManager::GetInstance().GetGameWidth()) && m_currentDrawPosition.x > m_mapDimensions.x - RenderManager::GetInstance().GetGameWidth()) { m_currentDrawPosition.x = m_mapDimensions.x - RenderManager::GetInstance().GetGameWidth(); } // check to see that the draw position isn't less than 0 if (m_currentDrawPosition.y < 0) { m_currentDrawPosition.y = 0; } // check to see that the draw position isn't greater than the size of the map if ( (m_mapDimensions.y >= RenderManager::GetInstance().GetGameHeight()) && m_currentDrawPosition.y > m_mapDimensions.y - RenderManager::GetInstance().GetGameHeight()) { m_currentDrawPosition.y = m_mapDimensions.y - RenderManager::GetInstance().GetGameHeight(); } } Here is my Render Method: void Layer::RenderLayer() { int screenRows = 0; // int that represents the number of rows on the screen int screenColumns = 0; // int that represents the number of columns on the screen int tileX = 0; // int that represents x position on screen for a tile int tileY = 0; // int that represents y position on screen for a tile int tileValue = 0; // value of tile in tilemap at the position int textureColumns = 0; // how many columns in the texture // only calculate and do rendering if this is a graphical layer if (m_layerType == graphicLayer) { // calculate certain values for rendering screenRows = RenderManager::GetInstance().GetGameHeight() / (int)(m_tileDimensions.y); screenColumns = RenderManager::GetInstance().GetGameWidth() / (int)(m_tileDimensions.x); if (screenColumns > m_layerColumns) { screenColumns = m_layerColumns; } if (screenRows > m_layerRows) { screenRows = m_layerRows; } tileX = (int)(m_currentDrawPosition.x / m_tileDimensions.x); tileY = (int)(m_currentDrawPosition.y / m_tileDimensions.y); textureColumns = (int)(RenderManager::GetInstance().GetTextureDesc(m_textureName).Width / m_tileDimensions.x); // traverse through the number of rows for (int row = 0; row < m_layerRows; row++) { // traverse through the number of columns for (int column = 0; column < m_layerColumns; column++) { // grab value from tilemap tileValue = m_tileMap[((tileY + row) * m_layerColumns + (tileX + column))]; // calculate the source rectangle m_sourceRectangle.left = (long)((tileValue % textureColumns) * m_tileDimensions.x); m_sourceRectangle.top = (long)((tileValue / textureColumns) * m_tileDimensions.y); m_sourceRectangle.right = (long)(m_sourceRectangle.left + m_tileDimensions.x); m_sourceRectangle.bottom = (long)(m_sourceRectangle.top + m_tileDimensions.y); // calculate the destination rectangle m_destinationRectangle.left = (long)(column * m_tileDimensions.x); m_destinationRectangle.top = (long)(row * m_tileDimensions.y); m_destinationRectangle.right = (long)(m_destinationRectangle.left + m_tileDimensions.x); m_destinationRectangle.bottom = (long)(m_destinationRectangle.top + m_tileDimensions.y); // draw the tile to the screen at the correct position RenderManager::GetInstance().RenderObject(m_textureName, m_sourceRectangle, m_destinationRectangle); } } } } So although I'm not looking for somebody to write my code for me I'm looking for some help in figuring out what I'm doing wrong that is causing the error's I'm getting. Please help me with this if you can. Also let me know if you need any more info on how I'm doing things.

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>