diff --git a/src/raylib.h b/src/raylib.h index 5fa66bd03b74..5d63d4b62412 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1058,15 +1058,16 @@ RLAPI int GetRandomValue(int min, int max); // Get a rando RLAPI void SetRandomSeed(unsigned int seed); // Set the seed for the random number generator RLAPI void TakeScreenshot(const char *fileName); // Takes a screenshot of current screen (filename extension defines format) RLAPI void SetConfigFlags(unsigned int flags); // Setup init configuration flags (view FLAGS) +RLAPI void OpenURL(const char *url); // Open URL with default system browser (if available) +// NOTE: Following functions implemented in module [utils] +//------------------------------------------------------------------ RLAPI void TraceLog(int logLevel, const char *text, ...); // Show trace log messages (LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR...) RLAPI void SetTraceLogLevel(int logLevel); // Set the current threshold (minimum) log level RLAPI void *MemAlloc(unsigned int size); // Internal memory allocator RLAPI void *MemRealloc(void *ptr, unsigned int size); // Internal memory reallocator RLAPI void MemFree(void *ptr); // Internal memory free -RLAPI void OpenURL(const char *url); // Open URL with default system browser (if available) - // Set custom callbacks // WARNING: Callbacks setup is intended for advance users RLAPI void SetTraceLogCallback(TraceLogCallback callback); // Set custom trace log @@ -1083,6 +1084,9 @@ RLAPI bool ExportDataAsCode(const unsigned char *data, int dataSize, const char RLAPI char *LoadFileText(const char *fileName); // Load text data from file (read), returns a '\0' terminated string RLAPI void UnloadFileText(char *text); // Unload file text data allocated by LoadFileText() RLAPI bool SaveFileText(const char *fileName, char *text); // Save text data to file (write), string must be '\0' terminated, returns true on success +//------------------------------------------------------------------ + +// File system functions RLAPI bool FileExists(const char *fileName); // Check if file exists RLAPI bool DirectoryExists(const char *dirPath); // Check if a directory path exists RLAPI bool IsFileExtension(const char *fileName, const char *ext); // Check file extension (including point: .png, .wav) @@ -1120,9 +1124,9 @@ RLAPI bool IsKeyPressedRepeat(int key); // Check if a key RLAPI bool IsKeyDown(int key); // Check if a key is being pressed RLAPI bool IsKeyReleased(int key); // Check if a key has been released once RLAPI bool IsKeyUp(int key); // Check if a key is NOT being pressed -RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC) RLAPI int GetKeyPressed(void); // Get key pressed (keycode), call it multiple times for keys queued, returns 0 when the queue is empty RLAPI int GetCharPressed(void); // Get char pressed (unicode), call it multiple times for chars queued, returns 0 when the queue is empty +RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC) // Input-related functions: gamepads RLAPI bool IsGamepadAvailable(int gamepad); // Check if a gamepad is available diff --git a/src/rcore.c b/src/rcore.c index 29881198a834..fa8f50d868fa 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -315,14 +315,12 @@ const char *TextFormat(const char *text, ...); // Formatting of text with #endif //---------------------------------------------------------------------------------- -// Module Functions Definition - Window and OpenGL Context Functions +// Module Functions Definition: Window and Graphics Device //---------------------------------------------------------------------------------- -// NOTE: Multiple window/display/monitor management functions have been moved to platform-specific modules - -// Platform-specific functions: -//void InitWindow(int width, int height, const char *title); -//void CloseWindow(void); +// NOTE: Functions with a platform-specific implementation on rcore_.c +//void InitWindow(int width, int height, const char *title) +//void CloseWindow(void) //bool WindowShouldClose(void) //bool IsWindowHidden(void) //bool IsWindowMinimized(void) @@ -364,9 +362,6 @@ const char *TextFormat(const char *text, ...); // Formatting of text with //void HideCursor(void) //void EnableCursor(void) //void DisableCursor(void) -//double GetTime(void) -//void TakeScreenshot(const char *fileName) -//void OpenURL(const char *url) // Check if window has been initialized successfully @@ -435,6 +430,19 @@ bool IsCursorOnScreen(void) return CORE.Input.Mouse.cursorOnScreen; } +//---------------------------------------------------------------------------------- +// Module Functions Definition: Custom frame control +//---------------------------------------------------------------------------------- + +// NOTE: Functions with a platform-specific implementation on rcore_.c +//void SwapScreenBuffer(void); +//void PollInputEvents(void); +//void WaitTime(double seconds); + +//---------------------------------------------------------------------------------- +// Module Functions Definition: Screen Drawing +//---------------------------------------------------------------------------------- + // Set background color (framebuffer clear color) void ClearBackground(Color color) { @@ -741,6 +749,10 @@ void EndScissorMode(void) rlDisableScissorTest(); } +//---------------------------------------------------------------------------------- +// Module Functions Definition: VR Stereo Rendering +//---------------------------------------------------------------------------------- + // Begin VR drawing configuration void BeginVrStereoMode(VrStereoConfig config) { @@ -837,6 +849,10 @@ void UnloadVrStereoConfig(VrStereoConfig config) TRACELOG(LOG_INFO, "UnloadVrStereoConfig not implemented in rcore.c"); } +//---------------------------------------------------------------------------------- +// Module Functions Definition: Shaders Management +//---------------------------------------------------------------------------------- + // Load shader from files and bind default locations // NOTE: If shader string is NULL, using default vertex/fragment shaders Shader LoadShader(const char *vsFileName, const char *fsFileName) @@ -1002,6 +1018,10 @@ void SetShaderValueTexture(Shader shader, int locIndex, Texture2D texture) } } +//---------------------------------------------------------------------------------- +// Module Functions Definition: Screen-space Queries +//---------------------------------------------------------------------------------- + // Get a ray trace from mouse position Ray GetMouseRay(Vector2 mouse, Camera camera) { @@ -1161,6 +1181,13 @@ Vector2 GetScreenToWorld2D(Vector2 position, Camera2D camera) return (Vector2){ transform.x, transform.y }; } +//---------------------------------------------------------------------------------- +// Module Functions Definition: Timming +//---------------------------------------------------------------------------------- + +// NOTE: Functions with a platform-specific implementation on rcore_.c +//double GetTime(void) + // Set target FPS (maximum) void SetTargetFPS(int fps) { @@ -1209,6 +1236,63 @@ float GetFrameTime(void) return (float)CORE.Time.frame; } +//---------------------------------------------------------------------------------- +// Module Functions Definition: Misc +//---------------------------------------------------------------------------------- + +// NOTE: Functions with a platform-specific implementation on rcore_.c +//void OpenURL(const char *url) + +// Get a random value between min and max (both included) +// WARNING: Ranges higher than RAND_MAX will return invalid results +// More specifically, if (max - min) > INT_MAX there will be an overflow, +// and otherwise if (max - min) > RAND_MAX the random value will incorrectly never exceed a certain threshold +int GetRandomValue(int min, int max) +{ + if (min > max) + { + int tmp = max; + max = min; + min = tmp; + } + + if ((unsigned int)(max - min) > (unsigned int)RAND_MAX) + { + TRACELOG(LOG_WARNING, "Invalid GetRandomValue() arguments, range should not be higher than %i", RAND_MAX); + } + + return (rand()%(abs(max - min) + 1) + min); +} + +// Set the seed for the random number generator +void SetRandomSeed(unsigned int seed) +{ + srand(seed); +} + +// Takes a screenshot of current screen (saved a .png) +void TakeScreenshot(const char *fileName) +{ +#if defined(SUPPORT_MODULE_RTEXTURES) + // Security check to (partially) avoid malicious code + if (strchr(fileName, '\'') != NULL) { TRACELOG(LOG_WARNING, "SYSTEM: Provided fileName could be potentially malicious, avoid [\'] character"); return; } + + Vector2 scale = GetWindowScaleDPI(); + unsigned char *imgData = rlReadScreenPixels((int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y)); + Image image = { imgData, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y), 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 }; + + char path[512] = { 0 }; + strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, fileName)); + + ExportImage(image, path); // WARNING: Module required: rtextures + RL_FREE(imgData); + + TRACELOG(LOG_INFO, "SYSTEM: [%s] Screenshot taken successfully", path); +#else + TRACELOG(LOG_WARNING,"IMAGE: ExportImage() requires module: rtextures"); +#endif +} + // Setup window configuration flags (view FLAGS) // NOTE: This function is expected to be called before window creation, // because it sets up some flags for the window creation process. @@ -1221,7 +1305,7 @@ void SetConfigFlags(unsigned int flags) } //---------------------------------------------------------------------------------- -// Module Functions Definition: FileSystem +// Module Functions Definition: File system //---------------------------------------------------------------------------------- // Check if the file exists @@ -1669,36 +1753,9 @@ long GetFileModTime(const char *fileName) } //---------------------------------------------------------------------------------- -// Module Functions Definition: Misc +// Module Functions Definition: Compression and Encoding //---------------------------------------------------------------------------------- -// Get a random value between min and max (both included) -// WARNING: Ranges higher than RAND_MAX will return invalid results -// More specifically, if (max - min) > INT_MAX there will be an overflow, -// and otherwise if (max - min) > RAND_MAX the random value will incorrectly never exceed a certain threshold -int GetRandomValue(int min, int max) -{ - if (min > max) - { - int tmp = max; - max = min; - min = tmp; - } - - if ((unsigned int)(max - min) > (unsigned int)RAND_MAX) - { - TRACELOG(LOG_WARNING, "Invalid GetRandomValue() arguments, range should not be higher than %i", RAND_MAX); - } - - return (rand()%(abs(max - min) + 1) + min); -} - -// Set the seed for the random number generator -void SetRandomSeed(unsigned int seed) -{ - srand(seed); -} - // Compress data (DEFLATE algorithm) unsigned char *CompressData(const unsigned char *data, int dataSize, int *compDataSize) { @@ -1841,26 +1898,9 @@ unsigned char *DecodeDataBase64(const unsigned char *data, int *outputSize) } //---------------------------------------------------------------------------------- -// Module Functions Definition: Inputs +// Module Functions Definition: Input Handling: Keyboard //---------------------------------------------------------------------------------- -// Platform-specific functions -//void SetExitKey(int key) -//const char *GetGamepadName(int gamepad) -//int GetGamepadAxisCount(int gamepad) -//int SetGamepadMappings(const char *mappings) -//int GetMouseX(void) -//int GetMouseY(void) -//Vector2 GetMousePosition(void) -//void SetMousePosition(int x, int y) -//float GetMouseWheelMove(void) -//void SetMouseCursor(int cursor) -//int GetTouchX(void) -//int GetTouchY(void) -//Vector2 GetTouchPosition(int index) -//void SwapScreenBuffer(void) -//void PollInputEvents(void) - // Check if a key has been pressed once bool IsKeyPressed(int key) { @@ -1971,6 +2011,22 @@ int GetCharPressed(void) return value; } +// Set a custom key to exit program +// NOTE: default exitKey is set to ESCAPE +void SetExitKey(int key) +{ + CORE.Input.Keyboard.exitKey = key; +} + +//---------------------------------------------------------------------------------- +// Module Functions Definition: Input Handling: Gamepad +//---------------------------------------------------------------------------------- + +// NOTE: Functions with a platform-specific implementation on rcore_.c +//int GetGamepadAxisCount(int gamepad) ** +//const char *GetGamepadName(int gamepad) ** +//int SetGamepadMappings(const char *mappings) + // Check if a gamepad is available bool IsGamepadAvailable(int gamepad) { @@ -1981,16 +2037,11 @@ bool IsGamepadAvailable(int gamepad) return result; } -// Get axis movement vector for a gamepad -float GetGamepadAxisMovement(int gamepad, int axis) -{ - float value = 0; - - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXIS) && - (fabsf(CORE.Input.Gamepad.axisState[gamepad][axis]) > 0.1f)) value = CORE.Input.Gamepad.axisState[gamepad][axis]; // 0.1f = GAMEPAD_AXIS_MINIMUM_DRIFT/DELTA - - return value; -} +// Get gamepad internal name id +//const char *GetGamepadName(int gamepad) +//{ +// return CORE.Input.Gamepad.ready[gamepad]; +//} // Check if a gamepad button has been pressed once bool IsGamepadButtonPressed(int gamepad, int button) @@ -2042,6 +2093,35 @@ int GetGamepadButtonPressed(void) return CORE.Input.Gamepad.lastButtonPressed; } +// Get gamepad axis count +//int GetGamepadAxisCount(int gamepad) +//{ +// return CORE.Input.Gamepad.axisCount; +//} + +// Get axis movement vector for a gamepad +float GetGamepadAxisMovement(int gamepad, int axis) +{ + float value = 0; + + if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXIS) && + (fabsf(CORE.Input.Gamepad.axisState[gamepad][axis]) > 0.1f)) value = CORE.Input.Gamepad.axisState[gamepad][axis]; // 0.1f = GAMEPAD_AXIS_MINIMUM_DRIFT/DELTA + + return value; +} + +//---------------------------------------------------------------------------------- +// Module Functions Definition: Input Handling: Mouse +//---------------------------------------------------------------------------------- + +// NOTE: Functions with a platform-specific implementation on rcore_.c +//int GetMouseX(void) ** +//int GetMouseY(void) ** +//Vector2 GetMousePosition(void) ** +//void SetMousePosition(int x, int y) +//float GetMouseWheelMove(void) ** +//void SetMouseCursor(int cursor) + // Check if a mouse button has been pressed once bool IsMouseButtonPressed(int button) { @@ -2129,6 +2209,15 @@ Vector2 GetMouseWheelMoveV(void) return result; } +//---------------------------------------------------------------------------------- +// Module Functions Definition: Input Handling: Touch +//---------------------------------------------------------------------------------- + +// NOTE: Functions with a platform-specific implementation on rcore_.c +//int GetTouchX(void) +//int GetTouchY(void) +//Vector2 GetTouchPosition(int index) + // Get touch point identifier for given index int GetTouchPointId(int index) { @@ -2329,29 +2418,6 @@ void WaitTime(double seconds) #endif } -// Takes a screenshot of current screen (saved a .png) -void TakeScreenshot(const char *fileName) -{ -#if defined(SUPPORT_MODULE_RTEXTURES) - // Security check to (partially) avoid malicious code - if (strchr(fileName, '\'') != NULL) { TRACELOG(LOG_WARNING, "SYSTEM: Provided fileName could be potentially malicious, avoid [\'] character"); return; } - - Vector2 scale = GetWindowScaleDPI(); - unsigned char *imgData = rlReadScreenPixels((int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y)); - Image image = { imgData, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y), 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 }; - - char path[512] = { 0 }; - strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, fileName)); - - ExportImage(image, path); // WARNING: Module required: rtextures - RL_FREE(imgData); - - TRACELOG(LOG_INFO, "SYSTEM: [%s] Screenshot taken successfully", path); -#else - TRACELOG(LOG_WARNING,"IMAGE: ExportImage() requires module: rtextures"); -#endif -} - // Scan all files and directories in a base path // WARNING: files.paths[] must be previously allocated and // contain enough space to store all required paths diff --git a/src/rcore_android.c b/src/rcore_android.c index 6b43b78b4dd1..a9fc5fb9b927 100644 --- a/src/rcore_android.c +++ b/src/rcore_android.c @@ -635,17 +635,10 @@ void OpenURL(const char *url) // Module Functions Definition: Inputs //---------------------------------------------------------------------------------- -// Set a custom key to exit program -void SetExitKey(int key) -{ - TRACELOG(LOG_WARNING, "SetExitKey() not implemented on target platform"); -} - // Get gamepad internal name id const char *GetGamepadName(int gamepad) { - TRACELOG(LOG_WARNING, "GetGamepadName() not implemented on target platform"); - return NULL; + return CORE.Input.Gamepad.name[gamepad]; } // Get gamepad axis count diff --git a/src/rcore_custom.c b/src/rcore_custom.c index 933f222fb332..e17aa96b1084 100644 --- a/src/rcore_custom.c +++ b/src/rcore_custom.c @@ -563,17 +563,10 @@ void OpenURL(const char *url) // Module Functions Definition: Inputs //---------------------------------------------------------------------------------- -// Set a custom key to exit program -void SetExitKey(int key) -{ - TRACELOG(LOG_WARNING, "SetExitKey() not implemented on target platform"); -} - // Get gamepad internal name id const char *GetGamepadName(int gamepad) { - TRACELOG(LOG_WARNING, "GetGamepadName() not implemented on target platform"); - return NULL; + return CORE.Input.Gamepad.name[gamepad]; } // Get gamepad axis count @@ -592,19 +585,25 @@ int SetGamepadMappings(const char *mappings) // Get mouse position X int GetMouseX(void) { - return (int)CORE.Input.Touch.position[0].x; + return (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); } // Get mouse position Y int GetMouseY(void) { - return (int)CORE.Input.Touch.position[0].y; + return (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); } // Get mouse position XY Vector2 GetMousePosition(void) { - return GetTouchPosition(0); + Vector2 position = { 0 }; + + // NOTE: On canvas scaling, mouse position is proportionally returned + position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; + position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; + + return position; } // Set mouse position XY diff --git a/src/rcore_desktop.c b/src/rcore_desktop.c index 4fd86d93eef4..c410b713bcd9 100644 --- a/src/rcore_desktop.c +++ b/src/rcore_desktop.c @@ -130,6 +130,7 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int static void MouseCursorPosCallback(GLFWwindow *window, double x, double y); // GLFW3 Cursor Position Callback, runs on mouse move static void MouseScrollCallback(GLFWwindow *window, double xoffset, double yoffset); // GLFW3 Srolling Callback, runs on mouse wheel static void CursorEnterCallback(GLFWwindow *window, int enter); // GLFW3 Cursor Enter Callback, cursor enters client area +static void JoystickCallback(int jid, int event); // GLFW3 Joystick Connected/Disconnected Callback //---------------------------------------------------------------------------------- // Module Functions Declaration @@ -1221,21 +1222,10 @@ void OpenURL(const char *url) // Module Functions Definition: Inputs //---------------------------------------------------------------------------------- -// Set a custom key to exit program -// NOTE: default exitKey is ESCAPE -void SetExitKey(int key) -{ - CORE.Input.Keyboard.exitKey = key; -} - // Get gamepad internal name id const char *GetGamepadName(int gamepad) { - const char *name = NULL; - - if (CORE.Input.Gamepad.ready[gamepad]) name = glfwGetJoystickName(gamepad); - - return name; + return CORE.Input.Gamepad.name[gamepad]; } // Get gamepad axis count @@ -1731,6 +1721,7 @@ static bool InitGraphicsDevice(int width, int height) glfwSetCursorPosCallback(platform.handle, MouseCursorPosCallback); // Track mouse position changes glfwSetScrollCallback(platform.handle, MouseScrollCallback); glfwSetCursorEnterCallback(platform.handle, CursorEnterCallback); + glfwSetJoystickCallback(JoystickCallback); glfwMakeContextCurrent(platform.handle); @@ -2066,5 +2057,17 @@ static void CursorEnterCallback(GLFWwindow *window, int enter) else CORE.Input.Mouse.cursorOnScreen = false; } -// EOF +// GLFW3 Joystick Connected/Disconnected Callback +static void JoystickCallback(int jid, int event) +{ + if (event == GLFW_CONNECTED) + { + strcpy(CORE.Input.Gamepad.name[jid], glfwGetJoystickName(jid)); + } + else if (event == GLFW_DISCONNECTED) + { + memset(CORE.Input.Gamepad.name[jid], 0, 64); + } +} +// EOF diff --git a/src/rcore_drm.c b/src/rcore_drm.c index c7823f820d18..ccf44feb358b 100644 --- a/src/rcore_drm.c +++ b/src/rcore_drm.c @@ -739,13 +739,6 @@ void OpenURL(const char *url) // Module Functions Definition: Inputs //---------------------------------------------------------------------------------- -// Set a custom key to exit program -// NOTE: default exitKey is ESCAPE -void SetExitKey(int key) -{ - CORE.Input.Keyboard.exitKey = key; -} - // Get gamepad internal name id const char *GetGamepadName(int gamepad) { @@ -764,6 +757,7 @@ const char *GetGamepadName(int gamepad) int GetGamepadAxisCount(int gamepad) { int axisCount = 0; + if (CORE.Input.Gamepad.ready[gamepad]) ioctl(platform.gamepadStreamFd[gamepad], JSIOCGAXES, &axisCount); CORE.Input.Gamepad.axisCount = axisCount; diff --git a/src/rcore_web.c b/src/rcore_web.c index 83acae02f640..ed7dc2657897 100644 --- a/src/rcore_web.c +++ b/src/rcore_web.c @@ -697,21 +697,10 @@ void OpenURL(const char *url) // Module Functions Definition: Inputs //---------------------------------------------------------------------------------- -// Set a custom key to exit program -// NOTE: default exitKey is ESCAPE -void SetExitKey(int key) -{ - CORE.Input.Keyboard.exitKey = key; -} - // Get gamepad internal name id const char *GetGamepadName(int gamepad) { - const char *name = NULL; - - name = CORE.Input.Gamepad.name[gamepad]; - - return name; + return CORE.Input.Gamepad.name[gamepad]; } // Get gamepad axis count