Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Access objects in a particular order  (Read 1785 times)

0 Members and 1 Guest are viewing this topic.

robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Access objects in a particular order
« 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?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Access objects in a particular order
« Reply #1 on: September 23, 2019, 08:39:07 pm »
whats happening thats not supposed to?

how exactly are you using this function?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

robkruger

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Access objects in a particular order
« Reply #2 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 is the complete project.


Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Access objects in a particular order
« Reply #3 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
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything