VoxelFarm.h
1 /************************************************************
2 * (C) Voxel Farm Inc. 2015
3 */
4 
5 #pragma once
6 
7 #ifndef VOXELFARM_VOXELFARM_H
8 #define VOXELFARM_VOXELFARM_H
9 
10 #include "VoxelFarmConfig.h"
11 #include <fstream>
12 
14 namespace VoxelFarm
15 {
16  const int ERROR_UNKNOWN = 1;
17  const int ERROR_INVALID_SIZE = 2;
18  const int ERROR_FILENOTFOUND = 3;
19 
21  typedef void OnError(int code, const char* message, const char* data, bool& terminate);
23  extern OnError* onError;
25  void notifyError(int code, const char* message, const char* data, bool terminate);
26 
27  extern std::ofstream logstream;
28 
29 #if VOXELFARM_USE_DEFAULT_ALLOC
30  static inline void* MallocDefault(size_t nBytes)
31  {
32  void* result = malloc(nBytes);
33  if (result == nullptr)
34  {
35  throw std::bad_alloc();
36  }
37  return result;
38  }
39 
40  static inline void* CallocDefault(size_t nObjects, size_t nSize)
41  {
42  void* result = calloc(nObjects, nSize);
43  if (result == nullptr)
44  {
45  throw std::bad_alloc();
46  }
47  return result;
48  }
49 
50  static inline void* ReallocDefault(void* ptr, size_t nBytes)
51  {
52  void* result = realloc(ptr, nBytes);
53  if (result == nullptr)
54  {
55  throw std::bad_alloc();
56  }
57  return result;
58  }
59 #endif
60 }
61 
409 /*
410 (http://www.websequencediagrams.com)
411 ClipmapView sequence
412 
413 Host->ClipmapView: position change
414 opt Scene Loop
415 Host->+ClipmapView: sceneLoop()
416 ClipmapView->+Scener: generate new scene
417 Scener->-ClipmapView:
418 ClipmapView->+Generator:start jobs
419 Generator->-ClipmapView:
420 ClipmapView->ClipmapView: queue cells in scene
421 ClipmapView->-Host:
422 end
423 opt Contour Loop
424 Host->+ClipmapView:contourLoop()
425 ClipmapView->ClipmapView: get next cell in queue
426 ClipmapView->+Generator: generate voxels for cell
427 Generator->-ClipmapView:
428 
429 ClipmapView->+Host: create mesh container
430 Host->-ClipmapView:
431 ClipmapView->+Contourer:build mesh from voxel data
432 Contourer->-ClipmapView:
433 ClipmapView->ClipmapView:queue mesh
434 ClipmapView->-Host:
435 end
436 opt Bake loop
437 Host-->+Host: bake
438 Host->+ClipmapView: getNextCellToBake()
439 ClipmapView->-Host:mesh
440 Host->+ClipmapView: notifyCellBaked()
441 ClipmapView->ClipmapView: cache mesh
442 ClipmapView->-Host:
443 
444 Edition sequence
445 
446 Host->ClipmapView: edition request
447 ClipmapView->+BlockData: perform changes
448 BlockData->+Host: new cell data
449 Host-->-Host: persist changes
450 BlockData->-ClipmapView: changed cells
451 ClipmapView->ClipmapView: queue cell
452 opt Contour Loop
453 Host->+ClipmapView:contourLoop()
454 ClipmapView->ClipmapView: get next cell in queue
455 ClipmapView->+Generator: generate voxels for cell
456 Generator->-ClipmapView:
457 
458 ClipmapView->+Host: create mesh container
459 Host->-ClipmapView:
460 ClipmapView->+Contourer:build mesh from voxel data
461 Contourer->-ClipmapView:
462 ClipmapView->ClipmapView:queue mesh
463 ClipmapView->-Host:
464 end
465 opt For each queued mesh
466 Host->+ClipmapView: get next mesh in queue
467 ClipmapView->-Host:mesh
468 Host->Host: process mesh
469 Host->ClipmapView: done with mesh
470 ClipmapView->ClipmapView: cache mesh
471 
472 
473 Generator layers
474 
475 Host->+Generator: generate cell
476 Generator->+VoxelLayer0: generate cell
477 VoxelLayer0->-Generator:
478 Generator->+VoxelLayer1: generate cell
479 VoxelLayer1->-Generator:
480 Generator->+VoxelLayerN: generate cell
481 VoxelLayerN->-Generator:
482 Generator->-Host:
483 
484 Simple contouring:
485 
486 Host->+Generator: generate cell
487 Generator->+VoxelLayer0: generate cell
488 VoxelLayer0->-Generator: voxel data
489 Generator->+VoxelLayerN: generate cell
490 VoxelLayerN->-Generator: voxel data
491 Generator->-Host:voxel data
492 Host->+Contourer: generate mesh
493 Contourer->-Host: mesh
494 
495 
496 Terrain generation:
497 
498 Generator->+Terrain VoxelLayer: generate cell
499 opt for each (X,Z) coordinate in cell
500 Terrain VoxelLayer->+Heightmap:find dominant biome
501 Heightmap->-Terrain VoxelLayer:
502 Terrain VoxelLayer->+Biome:compute height
503 Biome->+Noise: get noise
504 Noise->-Biome:
505 Biome->+Tileset: get sampled height
506 Tileset->-Biome:
507 Biome->+Material Library: get material displacement
508 Material Library->-Biome:
509 Biome->-Terrain VoxelLayer:
510 Terrain VoxelLayer->+Biome:compute cave profile
511 Biome->+Noise: get cave noise
512 Noise->-Biome:
513 Biome->+Tileset: get cave profile
514 Tileset->-Biome:
515 Biome->-Terrain VoxelLayer:
516 opt for each (Y) coordinate in current (X,Z)
517 Terrain VoxelLayer->Terrain VoxelLayer: compute voxel
518 end
519 Terrain VoxelLayer->-Generator:
520 
521 Class diagrams (http://yuml.me/diagram/scruffy/class/draw)
522 --------------
523 
524 // CClipmapView
525 [CClipmapView]-->[CGenerator]
526 [CGenerator]-->[CMaterialLibrary]
527 [CClipmapView]-->[CMaterialLibrary]
528 [CClipmapView]-->[CBlockData]
529 [CClipmapView]--0..*>[CCellData]
530 [CClipmapView]<>->[CBillboardCache]
531 [CClipmapView]<>-3>[Scene]
532 [CGenerator]--0..*>[<<IVoxelLayer>>]
533 [CMaterialLibrary]<>-0..*>[CMaterial]
534 [<<IVoxelLayer>>]^-.-[CBlockData]
535 [<<IVoxelLayer>>]^-.-[CHeightmapTerrain]
536 [<<IVoxelLayer>>]^-.-[CInstanceManager]
537 [<<IVoxelLayer>>]^-.-[CArchitectureManager]
538 [CHeightmapTerrain]-->[CHeightmap]
539 [CHeightmap]--0..*>[CBiome]
540 [CBiome]-->[CHeightLayer]
541 [CHeightLayer]-->[CCornerTileset]
542 [CHeightLayer]-->[<<ITileSet>>]
543 [<<ITileSet>>]^-.-[CTileset]
544 [<<ITileSet>>]^-.-[CPeriodicTileset]
545 [CInstanceManager]<>-0..*>[ClassData]
546 
547 
548 */
549 
550 #endif // VOXELFARM_VOXELFARM_H
Contains all classes and functions for the VoxelFarm engine.
void notifyError(int code, const char *message, const char *data, bool terminate)
Notifies there was an error.
OnError * onError
Error event handler.
void OnError(int code, const char *message, const char *data, bool &terminate)
Prototype for error event.
Definition: VoxelFarm.h:21