commit
c05701253e
4 changed files with 67 additions and 47 deletions
61
src/core.c
61
src/core.c
|
@ -88,6 +88,8 @@
|
||||||
|
|
||||||
#if defined __linux || defined(PLATFORM_WEB)
|
#if defined __linux || defined(PLATFORM_WEB)
|
||||||
#include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
|
#include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
|
||||||
|
#elif defined __APPLE__
|
||||||
|
#include <unistd.h> // Required for: usleep()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
#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 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 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 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
|
||||||
|
@ -339,7 +341,7 @@ static void *GamepadThread(void *arg); // Mouse reading thread
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
// NOTE: We include Sleep() function signature here to avoid windows.h inclusion
|
// 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
|
#endif
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
@ -703,19 +705,19 @@ void EndDrawing(void)
|
||||||
currentTime = GetTime();
|
currentTime = GetTime();
|
||||||
drawTime = currentTime - previousTime;
|
drawTime = currentTime - previousTime;
|
||||||
previousTime = currentTime;
|
previousTime = currentTime;
|
||||||
|
|
||||||
frameTime = updateTime + drawTime;
|
frameTime = updateTime + drawTime;
|
||||||
|
|
||||||
// Wait for some milliseconds...
|
// Wait for some milliseconds...
|
||||||
if (frameTime < targetTime)
|
if (frameTime < targetTime)
|
||||||
{
|
{
|
||||||
Wait((int)((targetTime - frameTime)*1000));
|
Wait((targetTime - frameTime)*1000.0f);
|
||||||
|
|
||||||
currentTime = GetTime();
|
currentTime = GetTime();
|
||||||
double extraTime = currentTime - previousTime;
|
double extraTime = currentTime - previousTime;
|
||||||
previousTime = currentTime;
|
previousTime = currentTime;
|
||||||
|
|
||||||
frameTime = updateTime + drawTime + extraTime;
|
frameTime += extraTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -851,14 +853,14 @@ void SetTargetFPS(int fps)
|
||||||
// Returns current FPS
|
// Returns current FPS
|
||||||
int GetFPS(void)
|
int GetFPS(void)
|
||||||
{
|
{
|
||||||
return (int)floorf(1.0f/GetFrameTime());
|
return (int)(1.0f/GetFrameTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns time in seconds for one frame
|
// Returns time in seconds for one frame
|
||||||
float GetFrameTime(void)
|
float GetFrameTime(void)
|
||||||
{
|
{
|
||||||
// NOTE: We round value to milliseconds
|
// NOTE: We round value to milliseconds
|
||||||
return (roundf(frameTime*1000.0)/1000.0f);
|
return (float)frameTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts Color to float array and normalizes
|
// Converts Color to float array and normalizes
|
||||||
|
@ -1688,7 +1690,10 @@ static void InitGraphicsDevice(int width, int height)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
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)
|
#if defined(PLATFORM_DESKTOP)
|
||||||
// Load OpenGL 3.3 extensions
|
// Load OpenGL 3.3 extensions
|
||||||
|
@ -1696,9 +1701,8 @@ static void InitGraphicsDevice(int width, int height)
|
||||||
rlglLoadExtensions(glfwGetProcAddress);
|
rlglLoadExtensions(glfwGetProcAddress);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Enables GPU v-sync, so frames are not limited to screen refresh rate (60Hz -> 60 FPS)
|
// Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
|
||||||
// If not set, swap interval uses GPU v-sync configuration
|
// NOTE: V-Sync can be enabled by graphic driver configuration
|
||||||
// Framerate can be setup using SetTargetFPS()
|
|
||||||
if (configFlags & FLAG_VSYNC_HINT)
|
if (configFlags & FLAG_VSYNC_HINT)
|
||||||
{
|
{
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
@ -2001,27 +2005,30 @@ static double GetTime(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for some milliseconds (stop program execution)
|
// Wait for some milliseconds (stop program execution)
|
||||||
static void Wait(int ms)
|
static void Wait(float ms)
|
||||||
{
|
{
|
||||||
#if defined _WIN32
|
#define SUPPORT_BUSY_WAIT_LOOP
|
||||||
Sleep(ms);
|
#if defined(SUPPORT_BUSY_WAIT_LOOP)
|
||||||
#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 prevTime = GetTime();
|
||||||
double nextTime = 0.0;
|
double nextTime = 0.0;
|
||||||
|
|
||||||
// Busy wait loop
|
// 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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
src/raylib.h
19
src/raylib.h
|
@ -98,14 +98,13 @@
|
||||||
#define RAD2DEG (180.0f/PI)
|
#define RAD2DEG (180.0f/PI)
|
||||||
|
|
||||||
// raylib Config Flags
|
// raylib Config Flags
|
||||||
#define FLAG_SHOW_LOGO 1
|
#define FLAG_SHOW_LOGO 1 // Set this flag to show raylib logo at startup
|
||||||
#define FLAG_SHOW_MOUSE_CURSOR 2
|
#define FLAG_FULLSCREEN_MODE 2 // Set this flag to run program in fullscreen
|
||||||
#define FLAG_FULLSCREEN_MODE 4
|
#define FLAG_WINDOW_RESIZABLE 4 // Set this flag to allow resizable window
|
||||||
#define FLAG_WINDOW_RESIZABLE 8
|
#define FLAG_WINDOW_DECORATED 8 // Set this flag to show window decoration (frame and buttons)
|
||||||
#define FLAG_WINDOW_DECORATED 16
|
#define FLAG_WINDOW_TRANSPARENT 16 // Set this flag to allow transparent window
|
||||||
#define FLAG_WINDOW_TRANSPARENT 32
|
#define FLAG_MSAA_4X_HINT 32 // Set this flag to try enabling MSAA 4X
|
||||||
#define FLAG_MSAA_4X_HINT 64
|
#define FLAG_VSYNC_HINT 64 // Set this flag to try enabling V-Sync on GPU
|
||||||
#define FLAG_VSYNC_HINT 128
|
|
||||||
|
|
||||||
// Keyboard Function Keys
|
// Keyboard Function Keys
|
||||||
#define KEY_SPACE 32
|
#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 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 int GetFPS(void); // Returns current FPS (rounded value)
|
RLAPI int GetFPS(void); // Returns current FPS
|
||||||
RLAPI float GetFrameTime(void); // Returns time in seconds for one frame (rounded value)
|
RLAPI float GetFrameTime(void); // Returns time in seconds for one frame
|
||||||
|
|
||||||
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
|
||||||
|
|
16
src/text.c
16
src/text.c
|
@ -531,8 +531,22 @@ 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
|
||||||
|
|
||||||
|
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
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
* This game has been created using raylib v1.2 (www.raylib.com)
|
* This game has been created using raylib v1.2 (www.raylib.com)
|
||||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2014 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
|
@ -28,17 +28,17 @@ int main()
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
const int screenWidth = 800;
|
const int screenWidth = 800;
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
const char windowTitle[30] = "<game name goes here>";
|
const char windowTitle[30] = "<game name goes here>";
|
||||||
|
|
||||||
GameScreen currentScreen = LOGO;
|
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, windowTitle);
|
InitWindow(screenWidth, screenHeight, windowTitle);
|
||||||
|
|
||||||
|
GameScreen currentScreen = LOGO;
|
||||||
|
|
||||||
// TODO: Initialize all required variables and load all required data here!
|
// TODO: Initialize all required variables and load all required data here!
|
||||||
|
|
||||||
int framesCounter = 0; // Used to count frames
|
int framesCounter = 0; // Useful to count frames
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60); // Set desired framerate (frames-per-second)
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Main game loop
|
// Main game loop
|
||||||
|
@ -149,4 +149,4 @@ int main()
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue