diff --git a/src/core.c b/src/core.c index 6ea54862..a3b5f486 100644 --- a/src/core.c +++ b/src/core.c @@ -88,6 +88,8 @@ #if defined __linux || defined(PLATFORM_WEB) #include // Required for: timespec, nanosleep(), select() - POSIX +#elif defined __APPLE__ + #include // Required for: usleep() #endif #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) @@ -287,7 +289,7 @@ static void InitGraphicsDevice(int width, int height); // Initialize graphics d static void SetupFramebufferSize(int displayWidth, int displayHeight); static void InitTimer(void); // Initialize timer static double GetTime(void); // Returns time since InitTimer() was run -static void Wait(int ms); // Wait for some milliseconds (stop program execution) +static void Wait(float ms); // Wait for some milliseconds (stop program execution) static bool GetKeyStatus(int key); // Returns if a key has been pressed static bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed static void PollInputEvents(void); // Register user events @@ -339,7 +341,7 @@ static void *GamepadThread(void *arg); // Mouse reading thread #if defined(_WIN32) // NOTE: We include Sleep() function signature here to avoid windows.h inclusion - void __stdcall Sleep(unsigned long msTimeout); // Required for Delay() + void __stdcall Sleep(unsigned long msTimeout); // Required for Wait() #endif //---------------------------------------------------------------------------------- @@ -703,19 +705,19 @@ void EndDrawing(void) currentTime = GetTime(); drawTime = currentTime - previousTime; previousTime = currentTime; - + frameTime = updateTime + drawTime; // Wait for some milliseconds... if (frameTime < targetTime) { - Wait((int)((targetTime - frameTime)*1000)); + Wait((targetTime - frameTime)*1000.0f); currentTime = GetTime(); double extraTime = currentTime - previousTime; previousTime = currentTime; - frameTime = updateTime + drawTime + extraTime; + frameTime += extraTime; } } @@ -851,14 +853,14 @@ void SetTargetFPS(int fps) // Returns current FPS int GetFPS(void) { - return (int)floorf(1.0f/GetFrameTime()); + return (int)(1.0f/GetFrameTime()); } // Returns time in seconds for one frame float GetFrameTime(void) { // NOTE: We round value to milliseconds - return (roundf(frameTime*1000.0)/1000.0f); + return (float)frameTime; } // Converts Color to float array and normalizes @@ -1688,7 +1690,10 @@ static void InitGraphicsDevice(int width, int height) #endif glfwMakeContextCurrent(window); - glfwSwapInterval(0); // Disable VSync by default + + // Try to disable GPU V-Sync by default, set framerate using SetTargetFPS() + // NOTE: V-Sync can be enabled by graphic driver configuration + glfwSwapInterval(0); #if defined(PLATFORM_DESKTOP) // Load OpenGL 3.3 extensions @@ -1696,9 +1701,8 @@ static void InitGraphicsDevice(int width, int height) rlglLoadExtensions(glfwGetProcAddress); #endif - // Enables GPU v-sync, so frames are not limited to screen refresh rate (60Hz -> 60 FPS) - // If not set, swap interval uses GPU v-sync configuration - // Framerate can be setup using SetTargetFPS() + // Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS) + // NOTE: V-Sync can be enabled by graphic driver configuration if (configFlags & FLAG_VSYNC_HINT) { glfwSwapInterval(1); @@ -2001,27 +2005,30 @@ static double GetTime(void) } // Wait for some milliseconds (stop program execution) -static void Wait(int ms) -{ -#if defined _WIN32 - Sleep(ms); -#elif defined __linux || defined(PLATFORM_WEB) - struct timespec req = { 0 }; - time_t sec = (int)(ms/1000); - ms -= (sec*1000); - req.tv_sec=sec; - req.tv_nsec=ms*1000000L; - - // NOTE: Use nanosleep() on Unix platforms... usleep() it's deprecated. - while (nanosleep(&req,&req) == -1) continue; -//#elif defined __APPLE__ - // TODO: -#else +static void Wait(float ms) +{ +#define SUPPORT_BUSY_WAIT_LOOP +#if defined(SUPPORT_BUSY_WAIT_LOOP) double prevTime = GetTime(); double nextTime = 0.0; // Busy wait loop - while ((nextTime - prevTime) < (double)ms/1000.0) nextTime = GetTime(); + while ((nextTime - prevTime) < ms/1000.0f) nextTime = GetTime(); +#else + #if defined _WIN32 + Sleep(ms); + #elif defined __linux || defined(PLATFORM_WEB) + struct timespec req = { 0 }; + time_t sec = (int)(ms/1000.0f); + ms -= (sec*1000); + req.tv_sec = sec; + req.tv_nsec = ms*1000000L; + + // NOTE: Use nanosleep() on Unix platforms... usleep() it's deprecated. + while (nanosleep(&req, &req) == -1) continue; + #elif defined __APPLE__ + usleep(ms*1000.0f); + #endif #endif } diff --git a/src/raylib.h b/src/raylib.h index ab7d7027..beda833c 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -98,14 +98,13 @@ #define RAD2DEG (180.0f/PI) // raylib Config Flags -#define FLAG_SHOW_LOGO 1 -#define FLAG_SHOW_MOUSE_CURSOR 2 -#define FLAG_FULLSCREEN_MODE 4 -#define FLAG_WINDOW_RESIZABLE 8 -#define FLAG_WINDOW_DECORATED 16 -#define FLAG_WINDOW_TRANSPARENT 32 -#define FLAG_MSAA_4X_HINT 64 -#define FLAG_VSYNC_HINT 128 +#define FLAG_SHOW_LOGO 1 // Set this flag to show raylib logo at startup +#define FLAG_FULLSCREEN_MODE 2 // Set this flag to run program in fullscreen +#define FLAG_WINDOW_RESIZABLE 4 // Set this flag to allow resizable window +#define FLAG_WINDOW_DECORATED 8 // Set this flag to show window decoration (frame and buttons) +#define FLAG_WINDOW_TRANSPARENT 16 // Set this flag to allow transparent window +#define FLAG_MSAA_4X_HINT 32 // Set this flag to try enabling MSAA 4X +#define FLAG_VSYNC_HINT 64 // Set this flag to try enabling V-Sync on GPU // Keyboard Function Keys #define KEY_SPACE 32 @@ -674,8 +673,8 @@ RLAPI Vector2 GetWorldToScreen(Vector3 position, Camera camera); // Returns the RLAPI Matrix GetCameraMatrix(Camera camera); // Returns camera transform matrix (view matrix) RLAPI void SetTargetFPS(int fps); // Set target FPS (maximum) -RLAPI int GetFPS(void); // Returns current FPS (rounded value) -RLAPI float GetFrameTime(void); // Returns time in seconds for one frame (rounded value) +RLAPI int GetFPS(void); // Returns current FPS +RLAPI float GetFrameTime(void); // Returns time in seconds for one frame RLAPI Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value RLAPI int GetHexValue(Color color); // Returns hexadecimal value for a Color diff --git a/src/text.c b/src/text.c index 6f18b391..18ebf482 100644 --- a/src/text.c +++ b/src/text.c @@ -531,8 +531,22 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i // NOTE: Uses default font void DrawFPS(int posX, int posY) { + // NOTE: We are rendering fps every second for better viewing on high framerates + + static int fps = 0; + static int counter = 0; + static int refreshRate = 20; + + if (counter < refreshRate) counter++; + else + { + fps = GetFPS(); + refreshRate = fps; + counter = 0; + } + // NOTE: We have rounding errors every frame, so it oscillates a lot - DrawText(FormatText("%2i FPS", GetFPS()), posX, posY, 20, LIME); + DrawText(FormatText("%2i FPS", fps), posX, posY, 20, LIME); } //----------------------------------------------------------------------------------