spriteAPI.h 5.5 KB

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