SFML community forums

Help => General => Topic started by: robkruger on September 23, 2019, 08:03:54 pm

Title: Access objects in a particular order
Post by: robkruger on September 23, 2019, 08:03:54 pm
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?
Title: Re: Access objects in a particular order
Post by: Stauricus on September 23, 2019, 08:39:07 pm
whats happening thats not supposed to?

how exactly are you using this function?
Title: Re: Access objects in a particular order
Post by: robkruger on September 25, 2019, 05:04:06 pm
Some don't get loaded, some do.

I call the function everytime the player moves a complete chunk
if(((static_cast<int>(fabs(m_camera.x) + 0.5) % 512 >= -5
  && static_cast<int>(fabs(m_camera.x) + 0.5) % 512 <= 5)
  || (static_cast<int>(fabs(m_camera.y) + 0.5) % 512 >= -5
  && static_cast<int>(fabs(m_camera.y) + 0.5) % 512 <= 5))
  && m_last_load != m_camera.get_location()){
    loaded_chunks = load_chunks();
  }

If you want, here (https://github.com/robkruger/sfml_project) is the complete project.

Title: Re: Access objects in a particular order
Post by: Nexus on October 02, 2019, 10:40:38 am
You assume that we have a lot of insights about your project, or the time to dig into it. Since this is not the case, try to describe your problem in a more precise way.

Try to follow this post very carefully, this will ensure you get the most meaningful responses:
https://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368