To load the chunks which have to be loaded related to where the camera is in my game, I have written this function, but I can't get it to load the correct chunks. This is my code:
std::vector<chunk> game::load_chunks(){
std::vector<chunk> loaded_chunks;
int x = (m_camera.x + 960) / 512;
int y = (m_camera.y + 540) / 512;
int width = static_cast<int>(m_width / 512 + 0.99);
int height = static_cast<int>(m_height / 512 + 0.99);
int amount = width * height;
int i;
std::cout << m_camera.x << " " << m_camera.y << std::endl;
std::cout << "x " << x << " y " << y << std::endl;
std::cout << width << " " << height << std::endl;
std::cout << -(amount / 2) << " " << (amount / 2) << std::endl;
for(i = -(amount / 2); i < (amount / 2); i++){
std::cout << x + (i % width) << " " << y + (i / height) << std::endl;
auto find = m_chunks.find(std::make_pair(x + (i % width), y + (i / height)));
if(find != m_chunks.end()) loaded_chunks.push_back(find->second);
}
m_last_load.x = m_camera.x;
m_last_load.y = m_camera.y;
return loaded_chunks;
}
Can anybody help?