Jump to content
  • Advertisement
Sign in to follow this  
isko

Loading chunks in 2D tilemap

This topic is 783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

I am currently working on an 2D game. I have already implemented an entity component system and I use tilemaps.

To increase performance, I decided to divide the map in chunks and load/update only those chunks that are actually seen by the player's view.

Well, the first chunk (0, 0) is rendered but when I go into another chunk the chunk is not rendered.

Even if I try to print the position of the veritces of the other chunks, the program aborts.

 

I think the code of the RenderMapSystem is not wrong because it detects which chunks have to be rendered.

 

Rendering the map

if(map.layer != KindsOfMap::HIGHER && map.vecChunks.size() > 0 && map.mapActivated){
            sf::Vector2i chunk(map.mapSize.x/CHUNK_SIZE.x, map.mapSize.y/CHUNK_SIZE.y);
            int left = 0, top = 0, right = 0, bottom = 0;
            //get the chunk indices into which top, left and bottom, right points of view fall
            sf::Vector2f temp = target.getView().getCenter() - (target.getView().getSize()/2.f); //get the top and left point
            left = static_cast<int>(temp.x/(CHUNK_SIZE.x*map.tileSize.x));
            top = static_cast<int>(temp.y/(CHUNK_SIZE.y*map.tileSize.y));
     
            temp += target.getView().getSize(); //bottom and right
            right = 1 + static_cast<int>(temp.x/(CHUNK_SIZE.x*map.tileSize.x));
            bottom = 1 + static_cast<int>(temp.y/(CHUNK_SIZE.y*map.tileSize.y));

            left = std::max(0, std::min(left, chunk.x));
            top = std::max(0, std::min(top, chunk.y));
            right = std::max(0, std::min(right, chunk.x));
            bottom = std::max(0, std::min(bottom, chunk.y));

            sf::RenderStates states;

            states.texture = &(*map.texture);
            std::cout << sizeof(map.vecChunks[1][0]) << std::endl;

            for(int i = left; i < right; ++i){
                for(int j = top; j < bottom; ++j){
                    for(int h = 0; h < 4; ++h){
                        std::cout << i << " " << j << " - " << map.vecChunks.size() << " " << map.vecChunks[j].size() << std::endl;
                        std::cout << h << " - " << map.vecChunks[i][j].operator[](h).position.x << " " << map.vecChunks[i][j].operator[](h).position.y << ": " << map.vecChunks[i][j].operator[](h).texCoords.x << " "
                                  << map.vecChunks[i][j].operator[](h).texCoords.y << std::endl;
                    }
                    std::cout << left << " " << right << " " << top << " " << bottom << std::endl;
                    target.draw(map.vecChunks[i][j], states);
                }
            }
        }

Filling out the chunks

    map->chunks = sf::Vector2i((width/CHUNK_SIZE.x)+1, (height/CHUNK_SIZE.y)+1);
    m_chunks.assign(map->chunks.x, std::vector<sf::VertexArray>(map->chunks.y, sf::VertexArray(sf::Quads)));
    std::cout << m_chunks.size() << " " << m_chunks[0].size() << std::endl;

    for(int iy = 0; iy < height; ++iy){
        for(int ix = 0; ix < width; ++ix){
            appendTile(ix, iy, m_chunks[ix/CHUNK_SIZE.x][iy/CHUNK_SIZE.y], wholeMap);
        }
    }

    map->vecChunks = m_chunks;
    map->mapSize = std::move(sf::Vector2i(width, height));

appendTile-function

void MapLoader::appendTile(int gx, int gy, sf::VertexArray& garr, std::vector<std::vector<sf::Vector2i>>& wholeMap){
    if(wholeMap[gx][gy].x != -1 && wholeMap[gx][gy].y != -1){
    sf::Vertex ver;
    ver.position=sf::Vector2f(gy * TILES_SIZE.y + TILES_SIZE.y, gx * TILES_SIZE.x);
    ver.texCoords=sf::Vector2f(wholeMap[gx][gy].x * TILES_SIZE.x + TILES_SIZE.x, wholeMap[gx][gy].y * TILES_SIZE.y);
    garr.append(ver);

    ver.position=sf::Vector2f(gy*TILES_SIZE.y, gx*TILES_SIZE.x);
    ver.texCoords=sf::Vector2f(wholeMap[gx][gy].x * TILES_SIZE.x, wholeMap[gx][gy].y * TILES_SIZE.y);
    garr.append(ver);

    ver.position=sf::Vector2f(gy*TILES_SIZE.y, gx*TILES_SIZE.x+TILES_SIZE.x);
    ver.texCoords=sf::Vector2f(wholeMap[gx][gy].x * TILES_SIZE.x, wholeMap[gx][gy].y * TILES_SIZE.y + TILES_SIZE.y);
    garr.append(ver);

    ver.position=sf::Vector2f(gy*TILES_SIZE.y+TILES_SIZE.y, gx*TILES_SIZE.x+TILES_SIZE.x);
    ver.texCoords=sf::Vector2f(wholeMap[gx][gy].x * TILES_SIZE.x + TILES_SIZE.x, wholeMap[gx][gy].y * TILES_SIZE.y + TILES_SIZE.y);
    garr.append(ver);
    }
}

Share this post


Link to post
Share on other sites
Advertisement

why don't you try stepping through and seeing exactly where it aborts? Likely somewhere where you are indexing your arrays - use your debugger

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!