Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rcore] GetFrameTime() counts time since application was minimized #4588

Open
Volcanic-Penguin opened this issue Dec 9, 2024 · 5 comments · May be fixed by #4598
Open

[rcore] GetFrameTime() counts time since application was minimized #4588

Volcanic-Penguin opened this issue Dec 9, 2024 · 5 comments · May be fixed by #4598

Comments

@Volcanic-Penguin
Copy link

Volcanic-Penguin commented Dec 9, 2024

Issue description

I'm using Raylib 5.0 with Rust but I'm presuming this is not a Rust specific bug. If it is then I'm sorry for wasting your time. If you minimize a Raylib application, when you unminimize the app, GetFrameTime() will return the total amount of time that has passed since you minimized the app. I feel like this probably shouldn't happen, anything that's moving based on frame time will move way too far when opening a minimized application, so you could probably clip through walls and stuff.

Environment

Raylib 5.0 with Rust (raylib-rs)
Windows 10
Platform backend: DESKTOP (GLFW)
Renderer: Intel(R) HD Graphics 530

Code Example

Just print GetFrameTime() to the console if it's more than one second, so something like:

if GetFrameTime() > 1.0 {
  printf(GetFrameTime());
}
@asdqwe
Copy link
Contributor

asdqwe commented Dec 9, 2024

I think this is an (upstream) GLFW issue. When iconifying (minimizing) a window, processing stops on GLFW for some reason. That doesn't happen with the SDL backend.

All GetFrameTime() does is return CORE.Time.frame (ref), which is filled at the end of every frame by EndDrawing() (ref). Note that this part of raylib code is shared between all platforms.

Maybe this problem could be related to this glfw/glfw#2640 issue, since, before calculating CORE.Time.frame, raylib calls SwapScreenBuffer() (ref) which on PLATFORM_DESKTOP_GLFW calls glfwSwapBuffers() (ref).

Test case:
#include "raylib.h"
int main(void) {
    InitWindow(800, 450, "test");
    SetTargetFPS(60);
    while (!WindowShouldClose()) {

        if (IsKeyPressed(KEY_ONE)) { MinimizeWindow(); TraceLog(LOG_INFO, "Minimized"); }

        TraceLog(LOG_INFO, "GetTime():%f. GetFrameTime():%f.", GetTime(), GetFrameTime());

        BeginDrawing();
        ClearBackground(RAYWHITE);
        EndDrawing();
     }
    CloseWindow();
    return 0;
}
GLFW log (note how iconifying the window also stops `GetTime()`):
INFO: Initializing raylib 5.6-dev
INFO: Platform backend: DESKTOP (GLFW)
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 1600 x 900
INFO:     > Screen size:  800 x 450
INFO:     > Render size:  800 x 450
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 174
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Intel
INFO:     > Renderer: Mesa Intel(R) HD Graphics 3000 (SNB GT2)
INFO:     > Version:  3.3 (Core Profile) Mesa 24.0.9-0ubuntu0.2
INFO:     > GLSL:     3.30
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: PLATFORM: DESKTOP (GLFW - X11): Initialized successfully
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: SYSTEM: Working Directory: /home/username/raylib-master/examples/core
INFO: TIMER: Target time per frame: 16.667 milliseconds
INFO: GetTime():0.082405. GetFrameTime():0.000000.
INFO: GetTime():0.092439. GetFrameTime():0.016667.
INFO: GetTime():0.106323. GetFrameTime():0.016667.
INFO: GetTime():0.122504. GetFrameTime():0.016667.
INFO: GetTime():0.139170. GetFrameTime():0.016668.
INFO: GetTime():0.155840. GetFrameTime():0.016667.
INFO: GetTime():0.172515. GetFrameTime():0.016668.
INFO: GetTime():0.189190. GetFrameTime():0.016668.
INFO: GetTime():0.205866. GetFrameTime():0.016668.
INFO: GetTime():0.222544. GetFrameTime():0.016668.
INFO: GetTime():0.239219. GetFrameTime():0.016668.
INFO: GetTime():0.255888. GetFrameTime():0.016668.
INFO: GetTime():0.272551. GetFrameTime():0.016668.
INFO: GetTime():0.289225. GetFrameTime():0.016668.
INFO: GetTime():0.305893. GetFrameTime():0.016668.
INFO: GetTime():0.322527. GetFrameTime():0.016668.
INFO: GetTime():0.339229. GetFrameTime():0.016668.
INFO: GetTime():0.355897. GetFrameTime():0.016668.
INFO: GetTime():0.372616. GetFrameTime():0.016668.
INFO: GetTime():0.389232. GetFrameTime():0.016668.
INFO: GetTime():0.405902. GetFrameTime():0.016668.
INFO: GetTime():0.422608. GetFrameTime():0.016668.
INFO: Minimized
INFO: GetTime():0.440137. GetFrameTime():0.016668.
INFO: GetTime():3.762437. GetFrameTime():0.016668.
INFO: GetTime():3.763220. GetFrameTime():3.307371.
INFO: GetTime():3.779933. GetFrameTime():0.016668.
INFO: GetTime():3.797590. GetFrameTime():0.016668.
INFO: GetTime():3.813287. GetFrameTime():0.016668.
INFO: GetTime():3.829948. GetFrameTime():0.016668.
INFO: GetTime():3.846643. GetFrameTime():0.016668.
INFO: GetTime():3.863284. GetFrameTime():0.016668.
INFO: GetTime():3.879954. GetFrameTime():0.016669.
INFO: GetTime():3.896632. GetFrameTime():0.016668.
INFO: GetTime():3.913289. GetFrameTime():0.016668.
INFO: GetTime():3.929937. GetFrameTime():0.016668.
INFO: GetTime():3.946668. GetFrameTime():0.016668.
INFO: GetTime():3.963293. GetFrameTime():0.016668.
INFO: GetTime():3.979963. GetFrameTime():0.016668.
INFO: GetTime():3.996687. GetFrameTime():0.016668.
INFO: GetTime():4.013297. GetFrameTime():0.016668.
INFO: GetTime():4.029960. GetFrameTime():0.016668.
INFO: GetTime():4.046667. GetFrameTime():0.016668.
INFO: GetTime():4.063304. GetFrameTime():0.016668.
INFO: GetTime():4.079972. GetFrameTime():0.016668.
INFO: GetTime():4.096681. GetFrameTime():0.016668.
INFO: GetTime():4.113309. GetFrameTime():0.016668.
INFO: GetTime():4.129976. GetFrameTime():0.016668.
INFO: TEXTURE: [ID 2] Unloaded texture data from VRAM (GPU)
INFO: SHADER: [ID 3] Default shader unloaded successfully
INFO: TEXTURE: [ID 1] Default texture unloaded successfully
INFO: Window closed successfully
SDL log (note how iconifying the window doesn't stop processing):
INFO: Initializing raylib 5.6-dev
INFO: Platform backend: DESKTOP (SDL)
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 1600 x 900
INFO:     > Screen size:  800 x 450
INFO:     > Render size:  800 x 450
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 174
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Intel
INFO:     > Renderer: Mesa Intel(R) HD Graphics 3000 (SNB GT2)
INFO:     > Version:  3.3 (Core Profile) Mesa 24.0.9-0ubuntu0.2
INFO:     > GLSL:     3.30
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: PLATFORM: DESKTOP (SDL2): Initialized successfully
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: SYSTEM: Working Directory: /home/username/raylib-master/examples/core
INFO: TIMER: Target time per frame: 16.667 milliseconds
INFO: GetTime():0.117000. GetFrameTime():0.000000.
INFO: GetTime():0.131000. GetFrameTime():0.017000.
INFO: GetTime():0.135000. GetFrameTime():0.017000.
INFO: GetTime():0.152000. GetFrameTime():0.017000.
INFO: GetTime():0.169000. GetFrameTime():0.017000.
INFO: GetTime():0.186000. GetFrameTime():0.017000.
INFO: GetTime():0.203000. GetFrameTime():0.017000.
INFO: GetTime():0.220000. GetFrameTime():0.017000.
INFO: GetTime():0.237000. GetFrameTime():0.017000.
INFO: GetTime():0.254000. GetFrameTime():0.017000.
INFO: GetTime():0.271000. GetFrameTime():0.017000.
INFO: GetTime():0.288000. GetFrameTime():0.017000.
INFO: GetTime():0.305000. GetFrameTime():0.017000.
INFO: GetTime():0.322000. GetFrameTime():0.017000.
INFO: GetTime():0.339000. GetFrameTime():0.017000.
INFO: GetTime():0.356000. GetFrameTime():0.017000.
INFO: GetTime():0.373000. GetFrameTime():0.017000.
INFO: Minimized
INFO: GetTime():0.390000. GetFrameTime():0.017000.
INFO: GetTime():0.425000. GetFrameTime():0.017000.
INFO: GetTime():0.425000. GetFrameTime():0.018000.
INFO: GetTime():0.442000. GetFrameTime():0.017000.
INFO: GetTime():0.459000. GetFrameTime():0.017000.
INFO: GetTime():0.476000. GetFrameTime():0.017000.
INFO: GetTime():0.493000. GetFrameTime():0.017000.
INFO: GetTime():0.510000. GetFrameTime():0.017000.
INFO: GetTime():0.527000. GetFrameTime():0.017000.
INFO: GetTime():0.544000. GetFrameTime():0.017000.
INFO: GetTime():0.561000. GetFrameTime():0.017000.
INFO: GetTime():0.578000. GetFrameTime():0.017000.
INFO: GetTime():0.595000. GetFrameTime():0.017000.
INFO: GetTime():0.612000. GetFrameTime():0.017000.
INFO: GetTime():0.629000. GetFrameTime():0.017000.
INFO: GetTime():0.646000. GetFrameTime():0.017000.
INFO: GetTime():0.663000. GetFrameTime():0.017000.
INFO: GetTime():0.680000. GetFrameTime():0.017000.
INFO: GetTime():0.697000. GetFrameTime():0.017000.
INFO: GetTime():0.714000. GetFrameTime():0.017000.
INFO: GetTime():0.731000. GetFrameTime():0.017000.
INFO: GetTime():0.748000. GetFrameTime():0.017000.
INFO: GetTime():0.765000. GetFrameTime():0.017000.
INFO: GetTime():0.782000. GetFrameTime():0.017000.
INFO: GetTime():0.799000. GetFrameTime():0.017000.
INFO: GetTime():0.816000. GetFrameTime():0.017000.
INFO: GetTime():0.833000. GetFrameTime():0.017000.
INFO: GetTime():0.850000. GetFrameTime():0.017000.
INFO: GetTime():0.867000. GetFrameTime():0.017000.
INFO: GetTime():0.884000. GetFrameTime():0.017000.
INFO: GetTime():0.901000. GetFrameTime():0.017000.
INFO: GetTime():0.918000. GetFrameTime():0.017000.
INFO: TEXTURE: [ID 2] Unloaded texture data from VRAM (GPU)
INFO: SHADER: [ID 3] Default shader unloaded successfully
INFO: TEXTURE: [ID 1] Default texture unloaded successfully
INFO: Window closed successfully

@asdqwe
Copy link
Contributor

asdqwe commented Dec 9, 2024

Out of curiosity, I tried reproducing this issue directly with GLFW and the iconifying freeze didn't happen.

GLFW test case:
//     How to replicate:
// unzip glfw-master.zip
// cd glfw-master/
//     Pick any example from examples/ and replace its code with this source.
//     Then:
// mkdir build/
// cmake -S . -B build/
// cmake --build build/
// ./build/examples/example

#include <stdio.h>
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

int main(int argc, char** argv) {

    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    GLFWwindow* window;
    window = glfwCreateWindow(640, 480, "test", NULL, NULL);
    if (!window) { glfwTerminate(); exit(EXIT_FAILURE); }

    glfwMakeContextCurrent(window);
    gladLoadGL(glfwGetProcAddress);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glfwSwapInterval(1);

    while (!glfwWindowShouldClose(window)) {
        printf("%f\n", glfwGetTime());

        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);

}

Then I rolled back raylib as far as 3.0.0 (from 2020) and, to my surprise, the issue happened even there (although I had to add MinimizeWindow()/glfwIconifyWindow() that wasn't implemented back then, HideWindow() was a different thing).

raylib 3.0.0 test case:
INFO: Initializing raylib 3.0
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 1600 x 900
INFO:     > Render size:  800 x 450
INFO:     > Screen size:  800 x 450
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: OpenGL 3.3 Core profile supported
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Intel
INFO:     > Renderer: Mesa Intel(R) HD Graphics 3000 (SNB GT2)
INFO:     > Version:  3.3 (Core Profile) Mesa 24.0.9-0ubuntu0.2
INFO:     > GLSL:     3.30
INFO: GL: Supported extensions count: 174
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: GL: Anisotropic textures filtering supported (max: 16X)
INFO: TEXTURE: [ID 1] Texture created successfully (1x1 - 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Compiled successfully
INFO: SHADER: [ID 2] Compiled successfully
INFO: SHADER: [ID 3] Program loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Internal vertex buffers initialized successfully in RAM (CPU)
INFO: RLGL: Internal vertex buffers uploaded successfully to VRAM (GPU)
INFO: RLGL: Default state initialized successfully
INFO: TEXTURE: [ID 2] Texture created successfully (128x128 - 1 mipmaps)
INFO: FONT: Default font loaded successfully
INFO: TIMER: Target time per frame: 16.667 milliseconds
INFO: GetTime():0.102898. GetFrameTime():0.000000.
INFO: GetTime():0.109237. GetFrameTime():0.016870.
INFO: GetTime():0.125903. GetFrameTime():0.016667.
INFO: GetTime():0.142571. GetFrameTime():0.016667.
INFO: GetTime():0.159238. GetFrameTime():0.016667.
INFO: GetTime():0.175905. GetFrameTime():0.016667.
INFO: GetTime():0.192572. GetFrameTime():0.016667.
INFO: GetTime():0.209239. GetFrameTime():0.016667.
INFO: GetTime():0.225906. GetFrameTime():0.016667.
INFO: GetTime():0.242574. GetFrameTime():0.016667.
INFO: GetTime():0.259241. GetFrameTime():0.016667.
INFO: GetTime():0.275907. GetFrameTime():0.016667.
INFO: GetTime():0.292575. GetFrameTime():0.016667.
INFO: GetTime():0.309243. GetFrameTime():0.016667.
INFO: Minimized
INFO: GetTime():0.326415. GetFrameTime():0.016667.
INFO: GetTime():0.342577. GetFrameTime():0.016667.
INFO: GetTime():2.574575. GetFrameTime():0.016668.
INFO: GetTime():2.575251. GetFrameTime():2.216007.
INFO: GetTime():2.591920. GetFrameTime():0.016668.
INFO: GetTime():2.608588. GetFrameTime():0.016668.
INFO: GetTime():2.625255. GetFrameTime():0.016667.
INFO: GetTime():2.641922. GetFrameTime():0.016667.
INFO: GetTime():2.658590. GetFrameTime():0.016667.
INFO: GetTime():2.675258. GetFrameTime():0.016667.
INFO: GetTime():2.691925. GetFrameTime():0.016667.
INFO: GetTime():2.708593. GetFrameTime():0.016667.
INFO: GetTime():2.725259. GetFrameTime():0.016667.
INFO: GetTime():2.741925. GetFrameTime():0.016667.
INFO: GetTime():2.758594. GetFrameTime():0.016667.
INFO: GetTime():2.775261. GetFrameTime():0.016667.
INFO: GetTime():2.791929. GetFrameTime():0.016667.
INFO: TEXTURE: [ID 2] Unloaded texture data from VRAM (GPU)
INFO: TEXTURE: [ID 1] Unloaded default texture data from VRAM (GPU)
INFO: Window closed successfully

And then I returned to the current master branch (aeb33e6) and tested again removing SwapScreenBuffer() from rcore.c (ref), which obviously broke the render, but was still able to open the window, minimize it and the issue was still there. So I guess we can rule that out.

raylib current master branch without SwapScreenBuffer test case:
INFO: Initializing raylib 5.6-dev
INFO: Platform backend: DESKTOP (GLFW)
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 1600 x 900
INFO:     > Screen size:  800 x 450
INFO:     > Render size:  800 x 450
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 174
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Intel
INFO:     > Renderer: Mesa Intel(R) HD Graphics 3000 (SNB GT2)
INFO:     > Version:  3.3 (Core Profile) Mesa 24.0.9-0ubuntu0.2
INFO:     > GLSL:     3.30
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: PLATFORM: DESKTOP (GLFW - X11): Initialized successfully
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: SYSTEM: Working Directory: /home/username/raylib-master/examples/core
INFO: TIMER: Target time per frame: 16.667 milliseconds
INFO: GetTime():0.085478. GetFrameTime():0.000000.
INFO: GetTime():0.099596. GetFrameTime():0.016667.
INFO: GetTime():0.104716. GetFrameTime():0.016667.
INFO: GetTime():0.121345. GetFrameTime():0.016667.
INFO: GetTime():0.138001. GetFrameTime():0.016667.
INFO: GetTime():0.154666. GetFrameTime():0.016667.
INFO: GetTime():0.171354. GetFrameTime():0.016667.
INFO: GetTime():0.188024. GetFrameTime():0.016667.
INFO: GetTime():0.204701. GetFrameTime():0.016667.
INFO: GetTime():0.221376. GetFrameTime():0.016667.
INFO: GetTime():0.238046. GetFrameTime():0.016668.
INFO: GetTime():0.254720. GetFrameTime():0.016668.
INFO: GetTime():0.271389. GetFrameTime():0.016668.
INFO: Minimized
INFO: GetTime():0.288958. GetFrameTime():0.016668.
INFO: GetTime():2.193007. GetFrameTime():0.016668.
INFO: GetTime():2.193109. GetFrameTime():1.888420.
INFO: GetTime():2.221997. GetFrameTime():0.016668.
INFO: GetTime():2.226468. GetFrameTime():0.016668.
INFO: GetTime():2.243483. GetFrameTime():0.016668.
INFO: GetTime():2.259815. GetFrameTime():0.016667.
INFO: GetTime():2.276477. GetFrameTime():0.016668.
INFO: GetTime():2.293133. GetFrameTime():0.016668.
INFO: GetTime():2.309814. GetFrameTime():0.016668.
INFO: GetTime():2.326483. GetFrameTime():0.016668.
INFO: GetTime():2.343145. GetFrameTime():0.016668.
INFO: GetTime():2.359864. GetFrameTime():0.016668.
INFO: GetTime():2.376531. GetFrameTime():0.016668.
INFO: GetTime():2.393207. GetFrameTime():0.016668.
INFO: GetTime():2.409882. GetFrameTime():0.016668.
INFO: TEXTURE: [ID 2] Unloaded texture data from VRAM (GPU)
INFO: SHADER: [ID 3] Default shader unloaded successfully
INFO: TEXTURE: [ID 1] Default texture unloaded successfully
INFO: Window closed successfully

So, looks like the issue is indeed with raylib (PLATFORM_DESKTOP_GLFW).

@asdqwe
Copy link
Contributor

asdqwe commented Dec 9, 2024

Ok, found the issue. Inside the PollInputEvents(), that is called in the end of EndDrawing() (ref), there's this:

// While window minimized, stop loop execution
while (IsWindowState(FLAG_WINDOW_MINIMIZED) && !IsWindowState(FLAG_WINDOW_ALWAYS_RUN)) glfwWaitEvents();

Which is stopping execution because FLAG_WINDOW_ALWAYS_RUN is false by default. So, a simple SetWindowState(FLAG_WINDOW_ALWAYS_RUN); call solves this issue.

Fix test case:
#include "raylib.h"
int main(void) {
    InitWindow(800, 450, "test");
    SetTargetFPS(60);

    SetWindowState(FLAG_WINDOW_ALWAYS_RUN); // This fixes it.

    while (!WindowShouldClose()) {

        if (IsKeyPressed(KEY_ONE)) { MinimizeWindow(); TraceLog(LOG_INFO, "Minimized"); }

        TraceLog(LOG_INFO, "GetTime():%f. GetFrameTime():%f.", GetTime(), GetFrameTime());

        BeginDrawing();
        ClearBackground(RAYWHITE);
        EndDrawing();
     }
    CloseWindow();
    return 0;
}

@raysan5 Maybe the FLAG_WINDOW_ALWAYS_RUN should be enabled by default, what do you think? Let me know if I should send a PR to add it to InitPlatform().

@Volcanic-Penguin
Copy link
Author

Wow well done, thanks for the fix!

That being said, I think that ideally you should be able to set FLAG_WINDOW_ALWAYS_RUN to false and still have GetFrameTime() work properly when the application stops and starts. I don't know how GetFrameTime() actually works but I could imagine maybe storing the time when the application stops and starts and subtracting that from the frame time or something like that.

@asdqwe
Copy link
Contributor

asdqwe commented Dec 10, 2024

The FLAG_WINDOW_ALWAYS_RUN appears to be implemented only for PLATFORM_DESKTOP_GLFW to leverage the glfwWaitEvents() to create a sort of "suspended state" (which I can totaly see the usefulness of, e.g.: tools). And glfwWaitEvents() will "puts the calling thread to sleep until at least one event is available" (doc), which is exactly what's happening. To workaround that it would be necessary to move the entire input polling to another thread, which, in my opinion, is completely unnecessary and would increase raylib code complexity considerably.

It's much easier to just default it to true (which would standardize the behavior with the other platforms) and keep the funcionality there for those that need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants