effects.d 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. module graphics.effects;
  2. import raylib;
  3. import std.stdio;
  4. import variables;
  5. import std.string;
  6. import system.abstraction;
  7. import system.config;
  8. import std.algorithm;
  9. import system.abstraction;
  10. int screenWidth;
  11. int screenHeight;
  12. Texture2D[] loadAnimationFramesUI(const string archivePath, const string animationName)
  13. {
  14. screenWidth = GetScreenWidth();
  15. screenHeight = GetScreenHeight();
  16. Texture2D[] frames;
  17. uint frameIndex = 1;
  18. while (true)
  19. {
  20. string frameFileName = format("%s-%03d.png", animationName, frameIndex);
  21. uint image_size;
  22. debug debugWriteln(frameFileName);
  23. char* image_data = get_file_data_from_archive(toStringz(archivePath),
  24. toStringz(frameFileName), &image_size);
  25. if (image_data == null) {
  26. debug debugWriteln("exiting from load anim UI");
  27. break;
  28. }
  29. Image image = LoadImageFromMemory(".PNG", cast(const(ubyte)*) image_data, image_size);
  30. Texture2D texture = LoadTextureFromImage(image);
  31. UnloadImage(image);
  32. frames ~= texture;
  33. debug debugWriteln("Loaded frame for UI ", frameIndex, " - ", frameFileName);
  34. frameIndex++;
  35. }
  36. debug debugWriteln("Frames for ui animations length: ", frames.length);
  37. return frames;
  38. }
  39. void playUIAnimation(Texture2D[] frames)
  40. {
  41. static float frameTime = 0.0f;
  42. if (playAnimation) {
  43. frameTime += GetFrameTime();
  44. while (frameTime >= frameDuration && frameDuration > 0) {
  45. frameTime -= frameDuration;
  46. currentFrame = cast(int)((currentFrame + 1) % frames.length);
  47. }
  48. int frameWidth = frames[currentFrame].width;
  49. int frameHeight = frames[currentFrame].height;
  50. DrawTexturePro(
  51. frames[currentFrame],
  52. Rectangle(0, 0, frameWidth, frameHeight),
  53. Rectangle(0, 0, screenWidth, screenHeight),
  54. Vector2(0, 0),
  55. 0,
  56. Color(255, 255, 255, 127)
  57. );
  58. } else {
  59. frameTime = 0.0f;
  60. currentFrame = 0;
  61. }
  62. }
  63. Sound sfx;
  64. void playSfx(string filename) {
  65. debug debugWriteln("Loading & playing SFX");
  66. sfx = LoadSound(filename.toStringz());
  67. PlaySound(sfx);
  68. }