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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - stubbie

Pages: [1]
1
General / Using std::futures for creating chunks
« on: July 21, 2024, 06:50:54 pm »
I am currently working on creating an isometric terrain generation program and was wondering if it would be possible to optimize my code by using futures when creating chunks. The chunks get created and destroyed pretty often, so allowing this to run in parallel would optimize my code by a ton. I also modify the blocks in the individual chunks, so being able to split this process up into multiple threads would render be big performance gains. I just cannot figure out how to actually put this into code. What I have been trying to do is using these data structures, where chunks to load are the chunks created asynchronously to be merged with chunks, chunks is the map of all the chunks currently visible, and futures are just a collection of futures I used to create the chunks:
    std::map<std::pair<int,int>, Chunk> chunks;
    std::map<std::pair<int,int>, Chunk> chunksToLoad; //used threads
    std::vector<std::future<void>> futures;
Then I attempted to create the chunks and update them in these two functions:
 static std::mutex mtx;
static void generateChunk(int chunkX, int chunkY, std::map<std::pair<int,int>, Chunk> *chunkToLoad,
    const siv::PerlinNoise &perlin, int octaves, float persistence, float frequency) {
    sf::Vector2i blockCoords(chunkX * Chunks::size, chunkY * Chunks::size);
    Chunk chunk;
    chunk.setVisibleBlocks(blockCoords, perlin, octaves, persistence, frequency);
    std::lock_guard<std::mutex> lock(mtx);
    chunkToLoad->insert({{blockCoords.x, blockCoords.y}, chunk});
}

void chunkManager::updateChunks() {
  for(int i = -Manage::renderDistance; i <= Manage::renderDistance; ++i){
    for(int j = -Manage::renderDistance; j <= Manage::renderDistance; ++j){
      futures.push_back(std::async(std::launch::async, generateChunk, chunkPosition.x + i, chunkPosition.y + j,
          &chunkToLoad, perlin, octaves, persistence, frequency));
    }
  }
  for(auto &pair : chunkToLoad){
    if(chunks.find(pair.first) == chunks.end()){
      chunks[pair.first] = pair.second;
    }
  }
  chunkToLoad.clear();
  futures.clear();
}
 
This whole process proves to be super slow for creating the world, much slower than when I run just on one thread. Does anyone have any advice for how I could take a better approach at creating these chunks in parallel? Is it even possible in this case? Thank you very much to anyone who can help.

2
General / Recommended to use smart pointer here?
« on: July 13, 2024, 01:37:03 am »
I am currently working on an isometric terrain generation engine and was wondering if it would be best to use smart pointers for storing chunk objects. Currently my chunks are stored like so and are loaded and removed in the following functions. Within my chunks map if I changed the chunk to std::unique_ptr<Chunk> would this be a good use case of smart pointers? I don't have much experience using them so I am struggling to determine if this is a good idea or not.

   std::map<std::pair<int,int>, Chunk> chunks;

void chunkManager::loadChunk(int chunkX, int chunkY){  
  sf::Vector2i blockCoords(chunkX * Chunks::size, chunkY * Chunks::size); //converted from chunk space to blocks in game space

  //chunk doesnt exist in map
  if(chunks.find({chunkX, chunkY}) == chunks.end()){
    auto chunk = Chunk();
    chunk.setBlocks(blockCoords, perlin);
    chunks[{chunkX, chunkY}] = chunk;
  }
  window.draw(chunks[{chunkX, chunkY}]);
}

void chunkManager::unloadChunk(){
  for(auto it = chunks.begin(); it != chunks.end();){
    if(std::abs(chunkPosition.x - it->first.first) > Manage::renderDistance ||
       std::abs(chunkPosition.y - it->first.second) > Manage::renderDistance){
      it = chunks.erase(it);
    }else{
      it++;
    }
  }
}

3
General / Building SFML project error with sf::VideoMode
« on: October 03, 2023, 05:50:39 pm »
I am trying to compile a SFML project, but when i try to compile main.cpp it throws errors. The code I am using is exactly what is in the sfml-linux documentation. I am using endavouros if that may have a part in the issue. I have been able to run sfml projects up until today when I create a new project everything broke. I have also tried building from source, but no fix. It seems to be an issue with linking, however when I include all the libraries needed to compile my code I am still faced with errors. I followed the sfml-linux documentation to create the project and this is the error I am faced with when I try to build:

/usr/bin/ld: main.o: in function `main':
main.cpp:(.text+0xaf): undefined reference to `sf::VideoMode::VideoMode(sf::Vector2<unsigned int> const&, unsigned int)'
collect2: error: ld returned 1 exit status

Thank you to anyone who can help me.


Pages: [1]