From b2ea5eae5e24f10ed6d0aacc456b46f14650cbd4 Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 8 May 2026 22:59:55 +0200 Subject: [PATCH] REVIEWED: `long long` usage in all raylib, minimize REVIEWED: Using `long long` format instead of `long long int`, more consistent with the `short` alternative for smaller `int`, instead of `short int` --- src/external/win32_clipboard.h | 12 +++++------ src/platforms/rcore_android.c | 4 ++-- src/platforms/rcore_desktop_glfw.c | 7 ++++--- src/platforms/rcore_desktop_rgfw.c | 10 +++++----- src/platforms/rcore_drm.c | 4 ++-- src/platforms/rcore_memory.c | 12 ++++++----- src/platforms/rcore_template.c | 6 ++++-- src/raudio.c | 8 ++++---- src/rcore.c | 32 +++++++++++++++--------------- src/rgestures.h | 27 ++++++++++++------------- src/rlgl.h | 2 +- 11 files changed, 64 insertions(+), 60 deletions(-) diff --git a/src/external/win32_clipboard.h b/src/external/win32_clipboard.h index 5c30fbd2e..ea0a34bd3 100644 --- a/src/external/win32_clipboard.h +++ b/src/external/win32_clipboard.h @@ -4,14 +4,14 @@ #ifndef WIN32_CLIPBOARD_ #define WIN32_CLIPBOARD_ -unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned long long int *dataSize); +unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned int *dataSize); #endif // WIN32_CLIPBOARD_ #ifdef WIN32_CLIPBOARD_IMPLEMENTATION #include #include #include -#include +#include // NOTE: These search for architecture is taken from "windows.h", and it's necessary to avoid including windows.h // and still make it compile on msvc, because import indirectly importing "winnt.h" (e.g. ) can cause problems is these are not defined @@ -213,7 +213,7 @@ static int GetPixelDataOffset(BITMAPINFOHEADER bih); // Get pixel data offset fr //---------------------------------------------------------------------------------- // Module Functions Definition //---------------------------------------------------------------------------------- -unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned long long int *dataSize) +unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned int *dataSize) { unsigned char *bmpData = NULL; @@ -228,7 +228,7 @@ unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned long *width = bmpInfoHeader->biWidth; *height = bmpInfoHeader->biHeight; SIZE_T clipDataSize = GlobalSize(clipHandle); - if (clipDataSize >= sizeof(BITMAPINFOHEADER)) + if ((clipDataSize >= sizeof(BITMAPINFOHEADER)) && (clipDataSize < INT_MAX)) { int pixelOffset = GetPixelDataOffset(*bmpInfoHeader); @@ -236,7 +236,7 @@ unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned long //------------------------------------------------------------------------ BITMAPFILEHEADER bmpFileHeader = { 0 }; SIZE_T bmpFileSize = sizeof(bmpFileHeader) + clipDataSize; - *dataSize = bmpFileSize; + *dataSize = (unsigned int)bmpFileSize; bmpFileHeader.bfType = 0x4D42; // BMP fil type constant bmpFileHeader.bfSize = (DWORD)bmpFileSize; // Up to 4GB works fine @@ -254,7 +254,7 @@ unsigned char *Win32GetClipboardImageData(int *width, int *height, unsigned long } else { - TRACELOG(LOG_WARNING, "Clipboard data is malformed"); + TRACELOG(LOG_WARNING, "Clipboard data is not supported (>2GB?)"); GlobalUnlock(clipHandle); CloseClipboard(); } diff --git a/src/platforms/rcore_android.c b/src/platforms/rcore_android.c index c427d7a58..ca0de7239 100644 --- a/src/platforms/rcore_android.c +++ b/src/platforms/rcore_android.c @@ -656,9 +656,9 @@ double GetTime(void) double time = 0.0; struct timespec ts = { 0 }; clock_gettime(CLOCK_MONOTONIC, &ts); - unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec; + unsigned long long nanoSeconds = (unsigned long long)ts.tv_sec*1000000000LLU + (unsigned long long)ts.tv_nsec; - time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() + time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() return time; } diff --git a/src/platforms/rcore_desktop_glfw.c b/src/platforms/rcore_desktop_glfw.c index 191a3130e..b1e100cf4 100644 --- a/src/platforms/rcore_desktop_glfw.c +++ b/src/platforms/rcore_desktop_glfw.c @@ -1054,12 +1054,13 @@ Image GetClipboardImage(void) #if SUPPORT_CLIPBOARD_IMAGE && SUPPORT_MODULE_RTEXTURES #if defined(_WIN32) - unsigned long long int dataSize = 0; + + unsigned int dataSize = 0; void *bmpData = NULL; int width = 0; int height = 0; - bmpData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize); + bmpData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize); if (bmpData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data."); else image = LoadImageFromMemory(".bmp", (const unsigned char *)bmpData, (int)dataSize); @@ -1112,7 +1113,7 @@ Image GetClipboardImage(void) XCloseDisplay(dpy); #else TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform"); -#endif // defined(_WIN32) +#endif // _WIN32 #else TRACELOG(LOG_WARNING, "Clipboard image: SUPPORT_CLIPBOARD_IMAGE requires SUPPORT_MODULE_RTEXTURES to work properly"); #endif // SUPPORT_CLIPBOARD_IMAGE diff --git a/src/platforms/rcore_desktop_rgfw.c b/src/platforms/rcore_desktop_rgfw.c index e9263cb04..a71dbb15e 100755 --- a/src/platforms/rcore_desktop_rgfw.c +++ b/src/platforms/rcore_desktop_rgfw.c @@ -1024,15 +1024,15 @@ Image GetClipboardImage(void) #if SUPPORT_CLIPBOARD_IMAGE && SUPPORT_MODULE_RTEXTURES #if defined(_WIN32) - unsigned long long int dataSize = 0; // moved into _WIN32 scope until other platforms gain support - void *fileData = NULL; // moved into _WIN32 scope until other platforms gain support + unsigned int dataSize = 0; + void *fileData = NULL; int width = 0; int height = 0; - fileData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize); + fileData = (void *)Win32GetClipboardImageData(&width, &height, &dataSize); if (fileData == NULL) TRACELOG(LOG_WARNING, "Clipboard image: Couldn't get clipboard data"); - else image = LoadImageFromMemory(".bmp", (const unsigned char *)fileData, dataSize); + else image = LoadImageFromMemory(".bmp", (const unsigned char *)fileData, (int)dataSize); #elif defined(__linux__) && defined(DRGFW_X11) @@ -1082,7 +1082,7 @@ Image GetClipboardImage(void) XCloseDisplay(dpy); #else TRACELOG(LOG_WARNING, "Clipboard image: PLATFORM_DESKTOP_RGFW doesn't implement GetClipboardImage() for this OS"); -#endif // defined(_WIN32) +#endif // _WIN32 #else TRACELOG(LOG_WARNING, "Clipboard image: SUPPORT_CLIPBOARD_IMAGE requires SUPPORT_MODULE_RTEXTURES to work properly"); #endif // SUPPORT_CLIPBOARD_IMAGE diff --git a/src/platforms/rcore_drm.c b/src/platforms/rcore_drm.c index 748b8aab8..659c80011 100644 --- a/src/platforms/rcore_drm.c +++ b/src/platforms/rcore_drm.c @@ -1006,9 +1006,9 @@ double GetTime(void) double time = 0.0; struct timespec ts = { 0 }; clock_gettime(CLOCK_MONOTONIC, &ts); - unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec; + unsigned long long nanoSeconds = (unsigned long long)ts.tv_sec*1000000000LLU + (unsigned long long)ts.tv_nsec; - time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() + time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() return time; } diff --git a/src/platforms/rcore_memory.c b/src/platforms/rcore_memory.c index b06187018..6926d8f4e 100644 --- a/src/platforms/rcore_memory.c +++ b/src/platforms/rcore_memory.c @@ -364,14 +364,16 @@ double GetTime(void) { double time = 0.0; #if defined(_WIN32) - LARGE_INTEGER now = { 0 }; - QueryPerformanceCounter(&now); - return (double)(now.QuadPart - CORE.Time.base)/(double)platform.timerFrequency.QuadPart; + LARGE_INTEGER currentTicks = { 0 }; + QueryPerformanceCounter(¤tTicks); + + time = (double)(currentTicks.QuadPart - CORE.Time.base)/(double)platform.timerFrequency.QuadPart; #elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__) struct timespec ts = { 0 }; clock_gettime(CLOCK_MONOTONIC, &ts); - unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec; - time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() + unsigned long long nanoSeconds = (unsigned long long)ts.tv_sec*1000000000LLU + (unsigned long long)ts.tv_nsec; + + time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() #endif return time; } diff --git a/src/platforms/rcore_template.c b/src/platforms/rcore_template.c index ee766867a..353d32d11 100644 --- a/src/platforms/rcore_template.c +++ b/src/platforms/rcore_template.c @@ -341,10 +341,12 @@ void SwapScreenBuffer(void) double GetTime(void) { double time = 0.0; + struct timespec ts = { 0 }; clock_gettime(CLOCK_MONOTONIC, &ts); - unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec; - time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() + unsigned long long nanoSeconds = (unsigned long long)ts.tv_sec*1000000000LLU + (unsigned long long)ts.tv_nsec; + + time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer() return time; } diff --git a/src/raudio.c b/src/raudio.c index a007a1092..e4d36611c 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -858,7 +858,7 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int else if ((strcmp(fileType, ".mp3") == 0) || (strcmp(fileType, ".MP3") == 0)) { drmp3_config config = { 0 }; - unsigned long long int totalFrameCount = 0; + unsigned long long totalFrameCount = 0; // NOTE: Forcing conversion to 32bit float sample size on reading wave.data = drmp3_open_memory_and_read_pcm_frames_f32(fileData, dataSize, &config, &totalFrameCount, NULL); @@ -868,7 +868,7 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int { wave.channels = config.channels; wave.sampleRate = config.sampleRate; - wave.frameCount = (int)totalFrameCount; + wave.frameCount = (unsigned int)totalFrameCount; // WARNING: Potential loss of data } else TRACELOG(LOG_WARNING, "WAVE: Failed to load MP3 data"); @@ -896,13 +896,13 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int #if SUPPORT_FILEFORMAT_FLAC else if ((strcmp(fileType, ".flac") == 0) || (strcmp(fileType, ".FLAC") == 0)) { - unsigned long long int totalFrameCount = 0; + unsigned long long totalFrameCount = 0; // NOTE: Forcing conversion to 16bit sample size on reading wave.data = drflac_open_memory_and_read_pcm_frames_s16(fileData, dataSize, &wave.channels, &wave.sampleRate, &totalFrameCount, NULL); wave.sampleSize = 16; - if (wave.data != NULL) wave.frameCount = (unsigned int)totalFrameCount; + if (wave.data != NULL) wave.frameCount = (unsigned int)totalFrameCount; // WARNING: Potential loss of data else TRACELOG(LOG_WARNING, "WAVE: Failed to load FLAC data"); } #endif diff --git a/src/rcore.c b/src/rcore.c index 6672302dd..0dad0cd96 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -385,7 +385,7 @@ typedef struct CoreData { double draw; // Time measure for frame draw (seconds) double frame; // Time measure for one frame (seconds) double target; // Desired time for one frame, if 0 not applied (seconds) - unsigned long long int base; // Base time measure for hi-res timer (ticks or nanoseconds) + unsigned long long base; // Base time measure for hi-res timer (ticks or nanoseconds) unsigned int frameCounter; // Frame counter (frames) } Time; @@ -1970,7 +1970,7 @@ unsigned char *LoadFileData(const char *fileName, int *dataSize) size_t count = fread(data, sizeof(unsigned char), size, file); // WARNING: fread() returns a size_t value, usually 'unsigned int' (32bit compilation) and 'unsigned long long' (64bit compilation) - // dataSize is unified along raylib as a 'int' type, so, for file-sizes > INT_MAX (2147483647 bytes) there is a limitation + // dataSize is unified along raylib as a 'int' type, so, for file-sizes >INT_MAX (2147483647 bytes) there is a limitation if (count > 2147483647) { TRACELOG(LOG_WARNING, "FILEIO: [%s] File is bigger than 2147483647 bytes, avoid using LoadFileData()", fileName); @@ -3336,15 +3336,15 @@ unsigned int *ComputeSHA1(const unsigned char *data, int dataSize) memcpy(msg, data, dataSize); msg[dataSize] = 128; // Write the '1' bit - unsigned long long bitsLen = 8ULL * dataSize; - msg[newDataSize-1] = (unsigned char)(bitsLen); - msg[newDataSize-2] = (unsigned char)(bitsLen >> 8); - msg[newDataSize-3] = (unsigned char)(bitsLen >> 16); - msg[newDataSize-4] = (unsigned char)(bitsLen >> 24); - msg[newDataSize-5] = (unsigned char)(bitsLen >> 32); - msg[newDataSize-6] = (unsigned char)(bitsLen >> 40); - msg[newDataSize-7] = (unsigned char)(bitsLen >> 48); - msg[newDataSize-8] = (unsigned char)(bitsLen >> 56); + unsigned long long bitsLen = 8ULL*dataSize; + msg[newDataSize - 1] = (unsigned char)(bitsLen); + msg[newDataSize - 2] = (unsigned char)(bitsLen >> 8); + msg[newDataSize - 3] = (unsigned char)(bitsLen >> 16); + msg[newDataSize - 4] = (unsigned char)(bitsLen >> 24); + msg[newDataSize - 5] = (unsigned char)(bitsLen >> 32); + msg[newDataSize - 6] = (unsigned char)(bitsLen >> 40); + msg[newDataSize - 7] = (unsigned char)(bitsLen >> 48); + msg[newDataSize - 8] = (unsigned char)(bitsLen >> 56); // Process the message in successive 512-bit chunks for (int offset = 0; offset < newDataSize; offset += (512/8)) @@ -3453,8 +3453,8 @@ unsigned int *ComputeSHA256(const unsigned char *data, int dataSize) hash[6] = 0x1f83d9ab; hash[7] = 0x5be0cd19; - const unsigned long long int bitLen = ((unsigned long long int)dataSize)*8; - unsigned long long int paddedSize = dataSize + sizeof(dataSize); + const unsigned long long bitLen = 8ULL*dataSize; + unsigned long long paddedSize = dataSize + sizeof(dataSize); paddedSize += (64 - (paddedSize%64)); unsigned char *buffer = (unsigned char *)RL_CALLOC(paddedSize, sizeof(unsigned char)); @@ -3465,7 +3465,7 @@ unsigned int *ComputeSHA256(const unsigned char *data, int dataSize) buffer[(paddedSize - sizeof(bitLen)) + (i - 1)] = (bitLen >> (8*(sizeof(bitLen) - i))) & 0xFF; } - for (unsigned long long int blockN = 0; blockN < paddedSize/64; blockN++) + for (unsigned long long blockN = 0; blockN < paddedSize/64; blockN++) { unsigned int a = hash[0]; unsigned int b = hash[1]; @@ -3487,7 +3487,7 @@ unsigned int *ComputeSHA256(const unsigned char *data, int dataSize) } for (int t = 16; t < 64; t++) w[t] = SHA256_A1(w[t - 2]) + w[t - 7] + SHA256_A0(w[t - 15]) + w[t - 16]; - for (unsigned long long int t = 0; t < 64; t++) + for (int t = 0; t < 64; t++) { unsigned int e1 = (SHA256_ROTATE_RIGHT(e, 6) ^ SHA256_ROTATE_RIGHT(e, 11) ^ SHA256_ROTATE_RIGHT(e, 25)); unsigned int ch = ((e & f) ^ (~e & g)); @@ -4219,7 +4219,7 @@ void InitTimer(void) if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) // Success { - CORE.Time.base = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec; + CORE.Time.base = (unsigned long long)now.tv_sec*1000000000LLU + (unsigned long long)now.tv_nsec; } else TRACELOG(LOG_WARNING, "TIMER: Hi-resolution timer not available"); #endif diff --git a/src/rgestures.h b/src/rgestures.h index 734d33f0f..248e1f71a 100644 --- a/src/rgestures.h +++ b/src/rgestures.h @@ -157,8 +157,8 @@ float GetGesturePinchAngle(void); // Get gesture pinch ang extern "C" { // Prevents name mangling of functions #endif // Functions required to query time on Windows - int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount); - int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency); + int __stdcall QueryPerformanceCounter(unsigned long long *lpPerformanceCount); + int __stdcall QueryPerformanceFrequency(unsigned long long *lpFrequency); #if defined(__cplusplus) } #endif @@ -518,37 +518,36 @@ static double rgGetCurrentTime(void) time = GetTime(); #else #if defined(_WIN32) - unsigned long long int clockFrequency, currentTime; + unsigned long long clockFrequency = 0; + unsigned long long currentClockTicks = 0; QueryPerformanceFrequency(&clockFrequency); // BE CAREFUL: Costly operation! - QueryPerformanceCounter(¤tTime); + QueryPerformanceCounter(¤tClockTicks); - time = (double)currentTime/clockFrequency; // Time in seconds + time = (double)currentClockTicks/clockFrequency; // Time in seconds #endif - #if defined(__linux__) // NOTE: Only for Linux-based systems - struct timespec now; + struct timespec now = { 0 }; clock_gettime(CLOCK_MONOTONIC, &now); - unsigned long long int nowTime = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec; // Time in nanoseconds + unsigned long long nanoSeconds = (unsigned long long)now.tv_sec*1000000000LLU + (unsigned long long)now.tv_nsec; - time = ((double)nowTime*1e-9); // Time in seconds + time = ((double)nanoSeconds*1e-9); // Time in seconds #endif - #if defined(__APPLE__) //#define CLOCK_REALTIME CALENDAR_CLOCK // returns UTC time since 1970-01-01 //#define CLOCK_MONOTONIC SYSTEM_CLOCK // returns the time since boot time - clock_serv_t cclock; - mach_timespec_t now; + clock_serv_t cclock = { 0 }; + mach_timespec_t now = { 0 }; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); // NOTE: OS X does not have clock_gettime(), using clock_get_time() clock_get_time(cclock, &now); mach_port_deallocate(mach_task_self(), cclock); - unsigned long long int nowTime = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec; // Time in nanoseconds + unsigned long long nanoSeconds = (unsigned long long)now.tv_sec*1000000000LLU + (unsigned long long)now.tv_nsec; - time = ((double)nowTime*1e-9); // Time in seconds + time = ((double)nanoSeconds*1e-9); // Time in seconds #endif #endif diff --git a/src/rlgl.h b/src/rlgl.h index 76b765e2e..ea5673397 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -3647,7 +3647,7 @@ void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_HALF_FLOAT_ARB; break; case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT_ARB; break; case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT_ARB; break; - #else // defined(GRAPHICS_API_OPENGL_ES2) + #else // GRAPHICS_API_OPENGL_ES2 case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float