diff options
Diffstat (limited to 'examples/web/shaders/shaders_hot_reloading.c')
| -rw-r--r-- | examples/web/shaders/shaders_hot_reloading.c | 185 |
1 files changed, 114 insertions, 71 deletions
diff --git a/examples/web/shaders/shaders_hot_reloading.c b/examples/web/shaders/shaders_hot_reloading.c index ef101b4..f0ed6f4 100644 --- a/examples/web/shaders/shaders_hot_reloading.c +++ b/examples/web/shaders/shaders_hot_reloading.c @@ -16,12 +16,45 @@ #include <time.h> // Required for: localtime(), asctime() +#if defined(PLATFORM_WEB) + #include <emscripten/emscripten.h> +#endif + #if defined(PLATFORM_DESKTOP) #define GLSL_VERSION 330 #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB #define GLSL_VERSION 100 #endif +//---------------------------------------------------------------------------------- +// Global Variables Definition +//---------------------------------------------------------------------------------- +static const int screenWidth = 800; +static const int screenHeight = 450; + +static char fragShaderFileName[256] = { 0 }; +static long fragShaderFileModTime = 0; + +static Shader shader = { 0 }; + +// Shader locations for required uniforms +static int resolutionLoc = 0; +static int mouseLoc = 0; +static int timeLoc = 0; + +static float resolution[2] = { 0 }; + +static float totalTime = 0.0f; +static bool shaderAutoReloading = false; + +//---------------------------------------------------------------------------------- +// Module Functions Declaration +//---------------------------------------------------------------------------------- +void UpdateDrawFrame(void); // Update and Draw one frame + +//---------------------------------------------------------------------------------- +// Program Main Entry Point +//---------------------------------------------------------------------------------- int main(void) { // Initialization @@ -31,99 +64,109 @@ int main(void) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - hot reloading"); - const char *fragShaderFileName = "resources/shaders/glsl%i/reload.fs"; - long fragShaderFileModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION)); + TextCopy(fragShaderFileName, TextFormat("resources/shaders/glsl%i/reload.fs", GLSL_VERSION)); + fragShaderFileModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION)); - // Load raymarching shader + // Load shader // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader - Shader shader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION)); + shader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION)); // Get shader locations for required uniforms - int resolutionLoc = GetShaderLocation(shader, "resolution"); - int mouseLoc = GetShaderLocation(shader, "mouse"); - int timeLoc = GetShaderLocation(shader, "time"); + resolutionLoc = GetShaderLocation(shader, "resolution"); + mouseLoc = GetShaderLocation(shader, "mouse"); + timeLoc = GetShaderLocation(shader, "time"); - float resolution[2] = { (float)screenWidth, (float)screenHeight }; + resolution[0] = (float)screenWidth; + resolution[1] = (float)screenHeight; SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2); + +#if defined(PLATFORM_WEB) + emscripten_set_main_loop(UpdateDrawFrame, 60, 1); +#else + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- - float totalTime = 0.0f; - bool shaderAutoReloading = false; + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + UpdateDrawFrame(); + } +#endif + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadShader(shader); // Unload shader - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- - // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + return 0; +} + +//---------------------------------------------------------------------------------- +// Module Functions Definitions +//---------------------------------------------------------------------------------- +void UpdateDrawFrame(void) +{ + // Update + //---------------------------------------------------------------------------------- + totalTime += GetFrameTime(); + Vector2 mouse = GetMousePosition(); + float mousePos[2] = { mouse.x, mouse.y }; + + // Set shader required uniform values + SetShaderValue(shader, timeLoc, &totalTime, SHADER_UNIFORM_FLOAT); + SetShaderValue(shader, mouseLoc, mousePos, SHADER_UNIFORM_VEC2); + + // Hot shader reloading + if (shaderAutoReloading || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) { - // Update - //---------------------------------------------------------------------------------- - totalTime += GetFrameTime(); - Vector2 mouse = GetMousePosition(); - float mousePos[2] = { mouse.x, mouse.y }; - - // Set shader required uniform values - SetShaderValue(shader, timeLoc, &totalTime, SHADER_UNIFORM_FLOAT); - SetShaderValue(shader, mouseLoc, mousePos, SHADER_UNIFORM_VEC2); + long currentFragShaderModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION)); - // Hot shader reloading - if (shaderAutoReloading || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) + // Check if shader file has been modified + if (currentFragShaderModTime != fragShaderFileModTime) { - long currentFragShaderModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION)); + // Try reloading updated shader + Shader updatedShader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION)); - // Check if shader file has been modified - if (currentFragShaderModTime != fragShaderFileModTime) + if (updatedShader.id != GetShaderDefault().id) // It was correctly loaded { - // Try reloading updated shader - Shader updatedShader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION)); + UnloadShader(shader); + shader = updatedShader; - if (updatedShader.id != GetShaderDefault().id) // It was correctly loaded - { - UnloadShader(shader); - shader = updatedShader; - - // Get shader locations for required uniforms - resolutionLoc = GetShaderLocation(shader, "resolution"); - mouseLoc = GetShaderLocation(shader, "mouse"); - timeLoc = GetShaderLocation(shader, "time"); - - // Reset required uniforms - SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2); - } + // Get shader locations for required uniforms + resolutionLoc = GetShaderLocation(shader, "resolution"); + mouseLoc = GetShaderLocation(shader, "mouse"); + timeLoc = GetShaderLocation(shader, "time"); - fragShaderFileModTime = currentFragShaderModTime; + // Reset required uniforms + SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2); } + + fragShaderFileModTime = currentFragShaderModTime; } - - if (IsKeyPressed(KEY_A)) shaderAutoReloading = !shaderAutoReloading; - //---------------------------------------------------------------------------------- + } + + if (IsKeyPressed(KEY_A)) shaderAutoReloading = !shaderAutoReloading; + //---------------------------------------------------------------------------------- - // Draw - //---------------------------------------------------------------------------------- - BeginDrawing(); + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); - ClearBackground(RAYWHITE); + ClearBackground(RAYWHITE); - // We only draw a white full-screen rectangle, frame is generated in shader - BeginShaderMode(shader); - DrawRectangle(0, 0, screenWidth, screenHeight, WHITE); - EndShaderMode(); + // We only draw a white full-screen rectangle, frame is generated in shader + BeginShaderMode(shader); + DrawRectangle(0, 0, screenWidth, screenHeight, WHITE); + EndShaderMode(); - DrawText(TextFormat("PRESS [A] to TOGGLE SHADER AUTOLOADING: %s", - shaderAutoReloading? "AUTO" : "MANUAL"), 10, 10, 10, shaderAutoReloading? RED : BLACK); - if (!shaderAutoReloading) DrawText("MOUSE CLICK to SHADER RE-LOADING", 10, 30, 10, BLACK); - - DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, 430, 10, BLACK); - - EndDrawing(); - //---------------------------------------------------------------------------------- - } - - // De-Initialization - //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - - CloseWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- + DrawText(TextFormat("PRESS [A] to TOGGLE SHADER AUTOLOADING: %s", + shaderAutoReloading? "AUTO" : "MANUAL"), 10, 10, 10, shaderAutoReloading? RED : BLACK); + if (!shaderAutoReloading) DrawText("MOUSE CLICK to SHADER RE-LOADING", 10, 30, 10, BLACK); + + DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, 430, 10, BLACK); - return 0; + EndDrawing(); + //---------------------------------------------------------------------------------- } |
