From 716e26aa37e352f0188824bc2de7dd3035f7413c Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 23 Jun 2021 01:25:09 +0200 Subject: [PATCH] Review BeginTextureMode() usage Moved outside BeginDrawing()/EndDrawing() to illustrate drawing is happening to an external texture (not screen) --- examples/core/core_split_screen.c | 22 +++++----- examples/core/core_vr_simulator.c | 28 ++++++------- examples/core/core_window_letterbox.c | 38 ++++++++--------- examples/shaders/shaders_custom_uniform.c | 51 ++++++++++------------- examples/shaders/shaders_eratosthenes.c | 34 +++++++-------- examples/shaders/shaders_julia_set.c | 42 +++++++++---------- examples/shaders/shaders_postprocessing.c | 34 +++++---------- src/core.c | 7 +++- 8 files changed, 113 insertions(+), 143 deletions(-) diff --git a/examples/core/core_split_screen.c b/examples/core/core_split_screen.c index 0bfdb84ac..31c3fb4bf 100644 --- a/examples/core/core_split_screen.c +++ b/examples/core/core_split_screen.c @@ -89,7 +89,7 @@ int main(void) // this moves thigns at 10 world units per second, regardless of the actual FPS float offsetThisFrame = 10.0f*GetFrameTime(); - // Move player 1 forward and backwards (no turning) + // Move Player1 forward and backwards (no turning) if (IsKeyDown(KEY_W)) { cameraPlayer1.position.z += offsetThisFrame; @@ -101,7 +101,7 @@ int main(void) cameraPlayer1.target.z -= offsetThisFrame; } - // Move player 2 forward and backwards (no turning) + // Move Player2 forward and backwards (no turning) if (IsKeyDown(KEY_UP)) { cameraPlayer2.position.x += offsetThisFrame; @@ -116,7 +116,7 @@ int main(void) // Draw //---------------------------------------------------------------------------------- - // Draw player 1's view to the render texture + // Draw Player1 view to the render texture BeginTextureMode(screenPlayer1); ClearBackground(SKYBLUE); BeginMode3D(cameraPlayer1); @@ -125,7 +125,7 @@ int main(void) DrawText("PLAYER1 W/S to move", 0, 0, 20, RED); EndTextureMode(); - // Draw player 2's view to the render texture + // Draw Player2 view to the render texture BeginTextureMode(screenPlayer2); ClearBackground(SKYBLUE); BeginMode3D(cameraPlayer2); @@ -134,21 +134,21 @@ int main(void) DrawText("PLAYER2 UP/DOWN to move", 0, 0, 20, BLUE); EndTextureMode(); - // Draw both view render textures to the screen side by side + // Draw both views render textures to the screen side by side BeginDrawing(); ClearBackground(BLACK); - DrawTextureRec(screenPlayer1.texture, splitScreenRect, (Vector2) { 0, 0 }, WHITE); - DrawTextureRec(screenPlayer2.texture, splitScreenRect, (Vector2) { screenWidth/2.0f, 0 }, WHITE); + DrawTextureRec(screenPlayer1.texture, splitScreenRect, (Vector2){ 0, 0 }, WHITE); + DrawTextureRec(screenPlayer2.texture, splitScreenRect, (Vector2){ screenWidth/2.0f, 0 }, WHITE); EndDrawing(); } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadRenderTexture(screenPlayer1); - UnloadRenderTexture(screenPlayer2); - UnloadTexture(textureGrid); + UnloadRenderTexture(screenPlayer1); // Unload render texture + UnloadRenderTexture(screenPlayer2); // Unload render texture + UnloadTexture(textureGrid); // Unload texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/core/core_vr_simulator.c b/examples/core/core_vr_simulator.c index bba90b823..65f0dec65 100644 --- a/examples/core/core_vr_simulator.c +++ b/examples/core/core_vr_simulator.c @@ -105,30 +105,26 @@ int main(void) // Draw //---------------------------------------------------------------------------------- - BeginDrawing(); - + BeginTextureMode(target); ClearBackground(RAYWHITE); + BeginVrStereoMode(config); + BeginMode3D(camera); - BeginTextureMode(target); - ClearBackground(RAYWHITE); - BeginVrStereoMode(config); - BeginMode3D(camera); - - DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED); - DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON); - DrawGrid(40, 1.0f); - - EndMode3D(); - EndVrStereoMode(); - EndTextureMode(); + DrawCube(cubePosition, 2.0f, 2.0f, 2.0f, RED); + DrawCubeWires(cubePosition, 2.0f, 2.0f, 2.0f, MAROON); + DrawGrid(40, 1.0f); + EndMode3D(); + EndVrStereoMode(); + EndTextureMode(); + + BeginDrawing(); + ClearBackground(RAYWHITE); BeginShaderMode(distortion); DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE); EndShaderMode(); - DrawFPS(10, 10); - EndDrawing(); //---------------------------------------------------------------------------------- } diff --git a/examples/core/core_window_letterbox.c b/examples/core/core_window_letterbox.c index 2c3af6df6..2933ca422 100644 --- a/examples/core/core_window_letterbox.c +++ b/examples/core/core_window_letterbox.c @@ -48,11 +48,11 @@ int main(void) Color colors[10] = { 0 }; for (int i = 0; i < 10; i++) colors[i] = (Color){ GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255 }; - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -79,37 +79,33 @@ int main(void) // Draw //---------------------------------------------------------------------------------- + // Draw everything in the render texture, note this will not be rendered on screen, yet + BeginTextureMode(target); + ClearBackground(RAYWHITE); // Clear render texture background color + + for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]); + + DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE); + DrawText(TextFormat("Default Mouse: [%i , %i]", (int)mouse.x, (int)mouse.y), 350, 25, 20, GREEN); + DrawText(TextFormat("Virtual Mouse: [%i , %i]", (int)virtualMouse.x, (int)virtualMouse.y), 350, 55, 20, YELLOW); + EndTextureMode(); + BeginDrawing(); - ClearBackground(BLACK); + ClearBackground(BLACK); // Clear screen background - // Draw everything in the render texture, note this will not be rendered on screen, yet - BeginTextureMode(target); - - ClearBackground(RAYWHITE); // Clear render texture background color - - for (int i = 0; i < 10; i++) DrawRectangle(0, (gameScreenHeight/10)*i, gameScreenWidth, gameScreenHeight/10, colors[i]); - - DrawText("If executed inside a window,\nyou can resize the window,\nand see the screen scaling!", 10, 25, 20, WHITE); - - DrawText(TextFormat("Default Mouse: [%i , %i]", (int)mouse.x, (int)mouse.y), 350, 25, 20, GREEN); - DrawText(TextFormat("Virtual Mouse: [%i , %i]", (int)virtualMouse.x, (int)virtualMouse.y), 350, 55, 20, YELLOW); - - EndTextureMode(); - - // Draw RenderTexture2D to window, properly scaled + // Draw render texture to screen, properly scaled DrawTexturePro(target.texture, (Rectangle){ 0.0f, 0.0f, (float)target.texture.width, (float)-target.texture.height }, (Rectangle){ (GetScreenWidth() - ((float)gameScreenWidth*scale))*0.5f, (GetScreenHeight() - ((float)gameScreenHeight*scale))*0.5f, (float)gameScreenWidth*scale, (float)gameScreenHeight*scale }, (Vector2){ 0, 0 }, 0.0f, WHITE); - EndDrawing(); //-------------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadRenderTexture(target); // Unload render texture + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/shaders/shaders_custom_uniform.c b/examples/shaders/shaders_custom_uniform.c index 6efda727b..60516c110 100644 --- a/examples/shaders/shaders_custom_uniform.c +++ b/examples/shaders/shaders_custom_uniform.c @@ -65,11 +65,11 @@ int main(void) // Setup orbital camera SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -81,55 +81,46 @@ int main(void) // Send new value to the shader to be used on drawing SetShaderValue(shader, swirlCenterLoc, swirlCenter, SHADER_UNIFORM_VEC2); - UpdateCamera(&camera); // Update camera + UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(RAYWHITE); // Clear texture background + + BeginMode3D(camera); // Begin 3d mode drawing + DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture + DrawGrid(10, 1.0f); // Draw a grid + EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode + + DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + BeginDrawing(); + ClearBackground(RAYWHITE); // Clear screen background - ClearBackground(RAYWHITE); - - BeginTextureMode(target); // Enable drawing to texture - - ClearBackground(RAYWHITE); // Clear texture background - - BeginMode3D(camera); // Begin 3d mode drawing - - DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture - - DrawGrid(10, 1.0f); // Draw a grid - - EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode - - DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); - - EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - + // Enable shader using the custom uniform BeginShaderMode(shader); - // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE); - EndShaderMode(); // Draw some 2d text over drawn texture DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY); - DrawFPS(10, 10); - EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - UnloadTexture(texture); // Unload texture - UnloadModel(model); // Unload model - UnloadRenderTexture(target); // Unload render texture + UnloadShader(shader); // Unload shader + UnloadTexture(texture); // Unload texture + UnloadModel(model); // Unload model + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/shaders/shaders_eratosthenes.c b/examples/shaders/shaders_eratosthenes.c index d5163a7f6..65fd9f980 100644 --- a/examples/shaders/shaders_eratosthenes.c +++ b/examples/shaders/shaders_eratosthenes.c @@ -46,11 +46,11 @@ int main(void) // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/eratosthenes.fs", GLSL_VERSION)); - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -59,35 +59,33 @@ int main(void) // Draw //---------------------------------------------------------------------------------- + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(BLACK); // Clear the render texture + + // Draw a rectangle in shader mode to be used as shader canvas + // NOTE: Rectangle uses font white character texture coordinates, + // so shader can not be applied here directly because input vertexTexCoord + // do not represent full screen coordinates (space where want to apply shader) + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); + EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader) + BeginDrawing(); - - ClearBackground(RAYWHITE); - - BeginTextureMode(target); // Enable drawing to texture - ClearBackground(BLACK); // Clear the render texture - - // Draw a rectangle in shader mode to be used as shader canvas - // NOTE: Rectangle uses font white character texture coordinates, - // so shader can not be applied here directly because input vertexTexCoord - // do not represent full screen coordinates (space where want to apply shader) - DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); - EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader) + ClearBackground(RAYWHITE); // Clear screen background BeginShaderMode(shader); // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE); EndShaderMode(); - EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - UnloadRenderTexture(target); // Unload texture + UnloadShader(shader); // Unload shader + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/shaders/shaders_julia_set.c b/examples/shaders/shaders_julia_set.c index 4a12ba02e..90c44cf58 100644 --- a/examples/shaders/shaders_julia_set.c +++ b/examples/shaders/shaders_julia_set.c @@ -75,15 +75,15 @@ int main(void) SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT); SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2); - int incrementSpeed = 0; // Multiplier of speed to change c value - bool showControls = true; // Show controls - bool pause = false; // Pause animation + int incrementSpeed = 0; // Multiplier of speed to change c value + bool showControls = true; // Show controls + bool pause = false; // Pause animation - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- @@ -145,20 +145,19 @@ int main(void) // Draw //---------------------------------------------------------------------------------- + // Using a render texture to draw Julia set + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(BLACK); // Clear the render texture + + // Draw a rectangle in shader mode to be used as shader canvas + // NOTE: Rectangle uses font white character texture coordinates, + // so shader can not be applied here directly because input vertexTexCoord + // do not represent full screen coordinates (space where want to apply shader) + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); + EndTextureMode(); + BeginDrawing(); - - ClearBackground(BLACK); // Clear the screen of the previous frame. - - // Using a render texture to draw Julia set - BeginTextureMode(target); // Enable drawing to texture - ClearBackground(BLACK); // Clear the render texture - - // Draw a rectangle in shader mode to be used as shader canvas - // NOTE: Rectangle uses font white character texture coordinates, - // so shader can not be applied here directly because input vertexTexCoord - // do not represent full screen coordinates (space where want to apply shader) - DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK); - EndTextureMode(); + ClearBackground(BLACK); // Clear screen background // Draw the saved texture and rendered julia set with shader // NOTE: We do not invert texture on Y, already considered inside shader @@ -176,17 +175,16 @@ int main(void) DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, 60, 10, RAYWHITE); DrawText("Press KEY_SPACE to pause movement animation", 10, 75, 10, RAYWHITE); } - EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - UnloadShader(shader); // Unload shader - UnloadRenderTexture(target); // Unload render texture + UnloadShader(shader); // Unload shader + UnloadRenderTexture(target); // Unload render texture - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/shaders/shaders_postprocessing.c b/examples/shaders/shaders_postprocessing.c index ef815391b..ebe5fcdb4 100644 --- a/examples/shaders/shaders_postprocessing.c +++ b/examples/shaders/shaders_postprocessing.c @@ -124,50 +124,38 @@ int main(void) // Draw //---------------------------------------------------------------------------------- + BeginTextureMode(target); // Enable drawing to texture + ClearBackground(RAYWHITE); // Clear texture background + + BeginMode3D(camera); // Begin 3d mode drawing + DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture + DrawGrid(10, 1.0f); // Draw a grid + EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode + EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) + BeginDrawing(); + ClearBackground(RAYWHITE); // Clear screen background - ClearBackground(RAYWHITE); - - BeginTextureMode(target); // Enable drawing to texture - - ClearBackground(RAYWHITE); // Clear texture background - - BeginMode3D(camera); // Begin 3d mode drawing - - DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture - - DrawGrid(10, 1.0f); // Draw a grid - - EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode - - EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) - - // Render previously generated texture using selected postpro shader + // Render generated texture using selected postprocessing shader BeginShaderMode(shaders[currentShader]); - // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE); - EndShaderMode(); // Draw 2d shapes and text over drawn texture DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7f)); DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); - DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK); DrawText(postproShaderText[currentShader], 330, 15, 20, RED); DrawText("< >", 540, 10, 30, DARKBLUE); - DrawFPS(700, 15); - EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- - // Unload all postpro shaders for (int i = 0; i < MAX_POSTPRO_SHADERS; i++) UnloadShader(shaders[i]); diff --git a/src/core.c b/src/core.c index 38e6a5e7b..e70b9474c 100644 --- a/src/core.c +++ b/src/core.c @@ -1942,7 +1942,10 @@ void ClearBackground(Color color) // Setup canvas (framebuffer) to start drawing void BeginDrawing(void) { - CORE.Time.current = GetTime(); // Number of elapsed seconds since InitTimer() + // WARNING: Previously to BeginDrawing() other render textures drawing could happen, + // consequently the measure for update vs draw is not accurate (only the total frame time is accurate) + + CORE.Time.current = GetTime(); // Number of elapsed seconds since InitTimer() CORE.Time.update = CORE.Time.current - CORE.Time.previous; CORE.Time.previous = CORE.Time.current; @@ -2045,7 +2048,7 @@ void EndDrawing(void) CORE.Time.frame += waitTime; // Total frame time: update + draw + wait } - PollInputEvents(); // Poll user events + PollInputEvents(); // Poll user events (before next frame update) #endif #if defined(SUPPORT_EVENTS_AUTOMATION)