WorldGenerator.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "WorldGenerator.h"
  2. #include "StaticRegistry.h"
  3. #include "Game.h"
  4. #include "PerlinNoise.h"
  5. using namespace Framework;
  6. WorldGenerator::WorldGenerator( int seed, Game *zGame )
  7. : Thread(),
  8. zGame( zGame ),
  9. noise( new PerlinNoise( seed ) ),
  10. exit( 0 )
  11. {
  12. start();
  13. }
  14. WorldGenerator::~WorldGenerator()
  15. {
  16. noise->release();
  17. }
  18. void WorldGenerator::thread()
  19. {
  20. while( !exit )
  21. {
  22. cs.Enter();
  23. GenerationRequest next;
  24. bool hasNext = false;
  25. if( requestQueue.getEintragAnzahl() > 0 )
  26. {
  27. next = requestQueue.get( 0 );
  28. requestQueue.remove( 0 );
  29. }
  30. cs.Leave();
  31. if( !hasNext )
  32. {
  33. sleep( 1 );
  34. continue;
  35. }
  36. Punkt start = zGame->getChunkCenter( next.startX, next.startY );
  37. Punkt end = zGame->getChunkCenter( next.endX, next.endY );
  38. int xDir = start.x > end.x ? -1 : ( start.x < end.x ? 1 : 0 );
  39. int yDir = start.y > end.y ? -1 : ( start.y < end.y ? 1 : 0 );
  40. for( int x = start.x; x != end.x; x += CHUNK_SIZE * xDir )
  41. {
  42. for( int y = start.y; y != end.y; y += CHUNK_SIZE * yDir )
  43. {
  44. Chunk *generatedChunk = StaticRegistry<DimensionGenerator>::INSTANCE.zElement( next.dimensionId )->generateChunk( noise, zGame, x, y );
  45. zGame->addChunck( generatedChunk, next.dimensionId );
  46. }
  47. }
  48. }
  49. }
  50. void WorldGenerator::requestGeneration( GenerationRequest request )
  51. {
  52. cs.Enter();
  53. requestQueue.add( request );
  54. cs.Leave();
  55. }
  56. void WorldGenerator::exitAndWait()
  57. {
  58. exit = 1;
  59. warteAufThread( 10000 );
  60. ende();
  61. }