spriteAPI.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 
  2. #ifndef DFPSR_SPRITE_ENGINE
  3. #define DFPSR_SPRITE_ENGINE
  4. #include "../../DFPSR/includeFramework.h"
  5. #include "orthoAPI.h"
  6. #include "lightAPI.h"
  7. #include <assert.h>
  8. #include <limits>
  9. namespace dsr {
  10. // TODO: Make into a constructor for each vector type
  11. inline FVector3D parseFVector3D(const ReadableString& content) {
  12. List<String> args = string_split(content, U',');
  13. if (args.length() != 3) {
  14. printText("Expected a vector of three decimal values.\n");
  15. return FVector3D();
  16. } else {
  17. return FVector3D(string_toDouble(args[0]), string_toDouble(args[1]), string_toDouble(args[2]));
  18. }
  19. }
  20. // A 2D image with depth and normal images for deferred light
  21. // To be rendered into images in advance for maximum detail level
  22. struct SpriteInstance {
  23. public:
  24. int typeIndex;
  25. Direction direction;
  26. IVector3D location; // Mini-tile coordinates
  27. bool shadowCasting;
  28. public:
  29. SpriteInstance(int typeIndex, Direction direction, const IVector3D& location, bool shadowCasting)
  30. : typeIndex(typeIndex), direction(direction), location(location), shadowCasting(shadowCasting) {}
  31. };
  32. struct DenseModelImpl;
  33. using DenseModel = std::shared_ptr<struct DenseModelImpl>;
  34. DenseModel DenseModel_create(const Model& original);
  35. // A 3D model that can be rotated freely
  36. // To be rendered during game-play to allow free rotation
  37. struct ModelInstance {
  38. public:
  39. int typeIndex;
  40. Transform3D location; // 3D tile coordinates with translation and 3-axis rotation allowed
  41. public:
  42. ModelInstance(int typeIndex, const Transform3D& location)
  43. : typeIndex(typeIndex), location(location) {}
  44. };
  45. class SpriteWorldImpl;
  46. using SpriteWorld = std::shared_ptr<SpriteWorldImpl>;
  47. // Sprite types
  48. int spriteWorld_loadSpriteTypeFromFile(const String& folderPath, const String& spriteName);
  49. int spriteWorld_getSpriteTypeCount();
  50. // Model types
  51. int spriteWorld_loadModelTypeFromFile(const String& folderPath, const String& visibleModelName, const String& shadowModelName);
  52. int spriteWorld_getModelTypeCount();
  53. SpriteWorld spriteWorld_create(OrthoSystem ortho, int shadowResolution);
  54. void spriteWorld_addBackgroundSprite(SpriteWorld& world, const SpriteInstance& sprite);
  55. void spriteWorld_addBackgroundModel(SpriteWorld& world, const ModelInstance& instance);
  56. void spriteWorld_addTemporarySprite(SpriteWorld& world, const SpriteInstance& sprite);
  57. void spriteWorld_addTemporaryModel(SpriteWorld& world, const ModelInstance& instance);
  58. // Create a point light that only exists until the next call to spriteWorld_clearTemporary.
  59. // position is in tile unit world-space.
  60. void spriteWorld_createTemporary_pointLight(SpriteWorld& world, const FVector3D position, float radius, float intensity, ColorRgbI32 color, bool shadowCasting);
  61. void spriteWorld_createTemporary_directedLight(SpriteWorld& world, const FVector3D direction, float intensity, ColorRgbI32 color);
  62. void spriteWorld_clearTemporary(SpriteWorld& world);
  63. // Draw the world using the current camera at the center of colorTarget
  64. void spriteWorld_draw(SpriteWorld& world, AlignedImageRgbaU8& colorTarget);
  65. // Draw debug information
  66. void spriteWorld_debug_octrees(SpriteWorld& world, AlignedImageRgbaU8& colorTarget);
  67. // The result is an approximation in mini-tile units.
  68. // The 3D system does not align with screen pixels for less than whole tile units.
  69. IVector3D spriteWorld_findGroundAtPixel(SpriteWorld& world, const AlignedImageRgbaU8& colorBuffer, const IVector2D& pixelLocation);
  70. // Set the camera's location directly
  71. void spriteWorld_setCameraLocation(SpriteWorld& world, const IVector3D miniTileLocation);
  72. // Approximates a mini-tile offset along the ground from the given pixel offset and moves the camera accordingly
  73. // If the offset is too small, the camera might not move at all
  74. void spriteWorld_moveCameraInPixels(SpriteWorld& world, const IVector2D& pixelOffset);
  75. // Get internal buffers after rendering.
  76. // Reading before having drawn the world for the first time will return null because the world does not yet know the target resolution.
  77. // By not being a part of rendering itself, it cannot go back in time and speed up rendering, so only use for debugging.
  78. // TODO: Make another feature for actually disabling dynamic light on low-end machines.
  79. AlignedImageRgbaU8 spriteWorld_getDiffuseBuffer(SpriteWorld& world);
  80. OrderedImageRgbaU8 spriteWorld_getNormalBuffer(SpriteWorld& world);
  81. OrderedImageRgbaU8 spriteWorld_getLightBuffer(SpriteWorld& world);
  82. AlignedImageF32 spriteWorld_getHeightBuffer(SpriteWorld& world);
  83. // Access the index of the camera's fixed direction
  84. // This is not an index selecting the camera itself, only selecting the viewing angle
  85. // TODO: Implement bound checks or a system that's easier to understand.
  86. int spriteWorld_getCameraDirectionIndex(SpriteWorld& world);
  87. void spriteWorld_setCameraDirectionIndex(SpriteWorld& world, int index);
  88. // Pre-conditions:
  89. // The model should be pre-transformed so that it can be rendered at the world origin
  90. // Textures must be converted into vertex colors or else they will simply be ignored
  91. // Enabling debug will save another file using a *Debug.png prefix with additional information
  92. // Use it to find flaws in generated shadow shapes that are hard to see in raw data
  93. // TODO: Hide OrthoSystem or expose it safely
  94. void sprite_generateFromModel(const Model& visibleModel, const Model& shadowModel, const OrthoSystem& ortho, const String& targetPath, int cameraAngles, bool debug = false);
  95. // A simpler version writing the result to an image and a string instead of saving to files.
  96. void sprite_generateFromModel(ImageRgbaU8& targetAtlas, String& targetConfigText, const Model& visibleModel, const Model& shadowModel, const OrthoSystem& ortho, const String& targetPath, int cameraAngles);
  97. }
  98. #endif