Implemented Wait()
Now program is halted (OS signal call) for required amount of time every frame, so CPU usage drops to zero, instead of using a busy wait loop.
This commit is contained in:
parent
37a64df7b9
commit
b681e8c277
3 changed files with 53 additions and 38 deletions
66
src/core.c
66
src/core.c
|
@ -67,9 +67,13 @@
|
||||||
#include <string.h> // Required for: strcmp()
|
#include <string.h> // Required for: strcmp()
|
||||||
//#include <errno.h> // Macros for reporting and retrieving error conditions through error codes
|
//#include <errno.h> // Macros for reporting and retrieving error conditions through error codes
|
||||||
|
|
||||||
|
#if defined __linux || defined(PLATFORM_WEB)
|
||||||
|
#include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||||
//#define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3
|
//#define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3
|
||||||
#include <GLFW/glfw3.h> // GLFW3 library: Windows, OpenGL context and Input management
|
#include <GLFW/glfw3.h> // GLFW3 library: Windows, OpenGL context and Input management
|
||||||
|
|
||||||
#ifdef __linux
|
#ifdef __linux
|
||||||
#define GLFW_EXPOSE_NATIVE_X11 // Linux specific definitions for getting
|
#define GLFW_EXPOSE_NATIVE_X11 // Linux specific definitions for getting
|
||||||
|
@ -243,7 +247,7 @@ static int dropFilesCount = 0; // Count stored strings
|
||||||
|
|
||||||
static double currentTime, previousTime; // Used to track timmings
|
static double currentTime, previousTime; // Used to track timmings
|
||||||
static double updateTime, drawTime; // Time measures for update and draw
|
static double updateTime, drawTime; // Time measures for update and draw
|
||||||
static double frameTime; // Time measure for one frame
|
static double frameTime = 0.0; // Time measure for one frame
|
||||||
static double targetTime = 0.0; // Desired time for one frame, if 0 not applied
|
static double targetTime = 0.0; // Desired time for one frame, if 0 not applied
|
||||||
|
|
||||||
static char configFlags = 0; // Configuration flags (bit based)
|
static char configFlags = 0; // Configuration flags (bit based)
|
||||||
|
@ -264,6 +268,7 @@ static void InitGraphicsDevice(int width, int height); // Initialize graphics d
|
||||||
static void SetupFramebufferSize(int displayWidth, int displayHeight);
|
static void SetupFramebufferSize(int displayWidth, int displayHeight);
|
||||||
static void InitTimer(void); // Initialize timer
|
static void InitTimer(void); // Initialize timer
|
||||||
static double GetTime(void); // Returns time since InitTimer() was run
|
static double GetTime(void); // Returns time since InitTimer() was run
|
||||||
|
static void Wait(int ms); // Wait for some milliseconds (stop program execution)
|
||||||
static bool GetKeyStatus(int key); // Returns if a key has been pressed
|
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 bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed
|
||||||
static void PollInputEvents(void); // Register user events
|
static void PollInputEvents(void); // Register user events
|
||||||
|
@ -313,6 +318,11 @@ static void InitGamepad(void); // Init raw gamepad inpu
|
||||||
static void *GamepadThread(void *arg); // Mouse reading thread
|
static void *GamepadThread(void *arg); // Mouse reading thread
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
// NOTE: We include Sleep() function signature here to avoid windows.h inclusion
|
||||||
|
void __stdcall Sleep(unsigned long msTimeout); // Required for Delay()
|
||||||
|
#endif
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module Functions Definition - Window and OpenGL Context Functions
|
// Module Functions Definition - Window and OpenGL Context Functions
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
@ -638,15 +648,16 @@ void EndDrawing(void)
|
||||||
|
|
||||||
frameTime = updateTime + drawTime;
|
frameTime = updateTime + drawTime;
|
||||||
|
|
||||||
double extraTime = 0.0;
|
// Wait for some milliseconds...
|
||||||
|
if (frameTime < targetTime)
|
||||||
while (frameTime < targetTime)
|
|
||||||
{
|
{
|
||||||
// Implement a delay
|
Wait((int)((targetTime - frameTime)*1000));
|
||||||
|
|
||||||
currentTime = GetTime();
|
currentTime = GetTime();
|
||||||
extraTime = currentTime - previousTime;
|
double extraTime = currentTime - previousTime;
|
||||||
previousTime = currentTime;
|
previousTime = currentTime;
|
||||||
frameTime += extraTime;
|
|
||||||
|
frameTime = updateTime + drawTime + extraTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -780,20 +791,16 @@ void SetTargetFPS(int fps)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns current FPS
|
// Returns current FPS
|
||||||
float GetFPS(void)
|
int GetFPS(void)
|
||||||
{
|
{
|
||||||
return (float)(1.0/frameTime);
|
return (int)floorf(1.0f/GetFrameTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns time in seconds for one frame
|
// Returns time in seconds for one frame
|
||||||
float GetFrameTime(void)
|
float GetFrameTime(void)
|
||||||
{
|
{
|
||||||
// As we are operate quite a lot with frameTime,
|
// NOTE: We round value to milliseconds
|
||||||
// it could be no stable, so we round it before passing it around
|
return (roundf(frameTime*1000.0)/1000.0f);
|
||||||
// NOTE: There are still problems with high framerates (>500fps)
|
|
||||||
double roundedFrameTime = round(frameTime*10000)/10000.0;
|
|
||||||
|
|
||||||
return (float)roundedFrameTime; // Time in seconds to run a frame
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts Color to float array and normalizes
|
// Converts Color to float array and normalizes
|
||||||
|
@ -1931,6 +1938,31 @@ static double GetTime(void)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
double prevTime = GetTime();
|
||||||
|
double nextTime = 0.0;
|
||||||
|
|
||||||
|
// Busy wait loop
|
||||||
|
while ((nextTime - prevTime) < (double)ms/1000.0) nextTime = GetTime();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Get one key state
|
// Get one key state
|
||||||
static bool GetKeyStatus(int key)
|
static bool GetKeyStatus(int key)
|
||||||
{
|
{
|
||||||
|
|
|
@ -659,8 +659,8 @@ RLAPI Vector2 GetWorldToScreen(Vector3 position, Camera camera); // Returns the
|
||||||
RLAPI Matrix GetCameraMatrix(Camera camera); // Returns camera transform matrix (view matrix)
|
RLAPI Matrix GetCameraMatrix(Camera camera); // Returns camera transform matrix (view matrix)
|
||||||
|
|
||||||
RLAPI void SetTargetFPS(int fps); // Set target FPS (maximum)
|
RLAPI void SetTargetFPS(int fps); // Set target FPS (maximum)
|
||||||
RLAPI float GetFPS(void); // Returns current FPS
|
RLAPI int GetFPS(void); // Returns current FPS (rounded value)
|
||||||
RLAPI float GetFrameTime(void); // Returns time in seconds for one frame
|
RLAPI float GetFrameTime(void); // Returns time in seconds for one frame (rounded value)
|
||||||
|
|
||||||
RLAPI Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value
|
RLAPI Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value
|
||||||
RLAPI int GetHexValue(Color color); // Returns hexadecimal value for a Color
|
RLAPI int GetHexValue(Color color); // Returns hexadecimal value for a Color
|
||||||
|
|
21
src/text.c
21
src/text.c
|
@ -504,25 +504,8 @@ Vector2 MeasureTextEx(SpriteFont spriteFont, const char *text, float fontSize, i
|
||||||
// NOTE: Uses default font
|
// NOTE: Uses default font
|
||||||
void DrawFPS(int posX, int posY)
|
void DrawFPS(int posX, int posY)
|
||||||
{
|
{
|
||||||
// NOTE: We are rendering fps every second for better viewing on high framerates
|
// NOTE: We have rounding errors every frame, so it oscillates a lot
|
||||||
// TODO: Not working properly on ANDROID and RPI (for high framerates)
|
DrawText(FormatText("%2i FPS", GetFPS()), posX, posY, 20, LIME);
|
||||||
|
|
||||||
static float fps = 0.0f;
|
|
||||||
static int counter = 0;
|
|
||||||
static int refreshRate = 20;
|
|
||||||
|
|
||||||
if (counter < refreshRate)
|
|
||||||
{
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fps = GetFPS();
|
|
||||||
refreshRate = (int)fps;
|
|
||||||
counter = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
DrawText(FormatText("%2.0f FPS", fps), posX, posY, 20, LIME);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue