You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Hot reloading
  4. *
  5. * NOTE: This example requires raylib OpenGL 3.3 for shaders support and only #version 330
  6. * is currently supported. OpenGL ES 2.0 platforms are not supported at the moment.
  7. *
  8. * Example originally created with raylib 3.0, last time updated with raylib 3.5
  9. *
  10. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  11. * BSD-like license that allows static linking with closed source software
  12. *
  13. * Copyright (c) 2020-2024 Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #include "rlgl.h"
  18. #include <time.h> // Required for: localtime(), asctime()
  19. #if defined(PLATFORM_DESKTOP)
  20. #define GLSL_VERSION 330
  21. #else // PLATFORM_ANDROID, PLATFORM_WEB
  22. #define GLSL_VERSION 100
  23. #endif
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - hot reloading");
  34. const char *fragShaderFileName = "resources/shaders/glsl%i/reload.fs";
  35. time_t fragShaderFileModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION));
  36. // Load raymarching shader
  37. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  38. Shader shader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION));
  39. // Get shader locations for required uniforms
  40. int resolutionLoc = GetShaderLocation(shader, "resolution");
  41. int mouseLoc = GetShaderLocation(shader, "mouse");
  42. int timeLoc = GetShaderLocation(shader, "time");
  43. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  44. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  45. float totalTime = 0.0f;
  46. bool shaderAutoReloading = false;
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. totalTime += GetFrameTime();
  55. Vector2 mouse = GetMousePosition();
  56. float mousePos[2] = { mouse.x, mouse.y };
  57. // Set shader required uniform values
  58. SetShaderValue(shader, timeLoc, &totalTime, SHADER_UNIFORM_FLOAT);
  59. SetShaderValue(shader, mouseLoc, mousePos, SHADER_UNIFORM_VEC2);
  60. // Hot shader reloading
  61. if (shaderAutoReloading || (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)))
  62. {
  63. long currentFragShaderModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION));
  64. // Check if shader file has been modified
  65. if (currentFragShaderModTime != fragShaderFileModTime)
  66. {
  67. // Try reloading updated shader
  68. Shader updatedShader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION));
  69. if (updatedShader.id != rlGetShaderIdDefault()) // It was correctly loaded
  70. {
  71. UnloadShader(shader);
  72. shader = updatedShader;
  73. // Get shader locations for required uniforms
  74. resolutionLoc = GetShaderLocation(shader, "resolution");
  75. mouseLoc = GetShaderLocation(shader, "mouse");
  76. timeLoc = GetShaderLocation(shader, "time");
  77. // Reset required uniforms
  78. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  79. }
  80. fragShaderFileModTime = currentFragShaderModTime;
  81. }
  82. }
  83. if (IsKeyPressed(KEY_A)) shaderAutoReloading = !shaderAutoReloading;
  84. //----------------------------------------------------------------------------------
  85. // Draw
  86. //----------------------------------------------------------------------------------
  87. BeginDrawing();
  88. ClearBackground(RAYWHITE);
  89. // We only draw a white full-screen rectangle, frame is generated in shader
  90. BeginShaderMode(shader);
  91. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  92. EndShaderMode();
  93. DrawText(TextFormat("PRESS [A] to TOGGLE SHADER AUTOLOADING: %s",
  94. shaderAutoReloading? "AUTO" : "MANUAL"), 10, 10, 10, shaderAutoReloading? RED : BLACK);
  95. if (!shaderAutoReloading) DrawText("MOUSE CLICK to SHADER RE-LOADING", 10, 30, 10, BLACK);
  96. DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, 430, 10, BLACK);
  97. EndDrawing();
  98. //----------------------------------------------------------------------------------
  99. }
  100. // De-Initialization
  101. //--------------------------------------------------------------------------------------
  102. UnloadShader(shader); // Unload shader
  103. CloseWindow(); // Close window and OpenGL context
  104. //--------------------------------------------------------------------------------------
  105. return 0;
  106. }