mirror of
https://github.com/raysan5/raylib.git
synced 2026-11-28 02:21:47 -05:00
Compare commits
11 Commits
985ac298be
...
5181a2260d
| Author | SHA1 | Date | |
|---|---|---|---|
| 5181a2260d | |||
| 16343404dd | |||
| 5824b6461d | |||
| 84ea59ed9d | |||
| 2b7e9f24ee | |||
| b28643a42e | |||
| fe9ace1e03 | |||
| 7639166019 | |||
| 55c9651d74 | |||
| bca8233e4b | |||
| be6b676840 |
@ -13,6 +13,16 @@ Do you enjoy raylib and want to contribute? Nice! You can help with the followin
|
||||
This document contains a set of guidelines to contribute to the project. These are mostly guidelines, not rules.
|
||||
Use your best judgment, and feel free to propose changes to this document in a pull request.
|
||||
|
||||
### About AI Usage
|
||||
|
||||
**Please, if you never used raylib, avoid sending PRs to the project.**
|
||||
|
||||
raylib project accepts PRs independently of the tools used by the experienced developers contributing to the project but the contributor must be knowledgeable about the addressed issue and the whole context involving that issue.
|
||||
|
||||
Please, do not use AI to generate the issue description, try to avoid excessive detail and verbosity. Screenshots of the issue and results are always preferred.
|
||||
|
||||
Remember that raylib is maintained by humans, that means a person is reading every issue/PR and taking required decision. **It takes time**, so try to shape your issues/PRs accordingly to minimize the burden for the maintainers.
|
||||
|
||||
### raylib philosophy
|
||||
|
||||
- raylib is a tool to **ENJOY** videogames programming, every function in raylib is designed as a mini-tutorial on itself.
|
||||
|
||||
18
src/external/rlsw.h
vendored
18
src/external/rlsw.h
vendored
@ -2405,8 +2405,10 @@ static void sw_texture_sample_linear(float *SW_RESTRICT color, const sw_texture_
|
||||
if (tex->sWrap == SW_REPEAT)
|
||||
{
|
||||
#if RLSW_SUPPORT_NPOT_TEXTURE
|
||||
x0 = (x0%tex->width + tex->width)%tex->width;
|
||||
x1 = (x1%tex->width + tex->width)%tex->width;
|
||||
x0 %= tex->width;
|
||||
if (x0 < 0) x0 += tex->width; // The modulo result may be negative. If that's the case, shift x0 back to the range [0, width)
|
||||
x1 = x0 + 1;
|
||||
if (x1 == tex->width) x1 = 0; // If x1 goes past the right column, wrap back to 0
|
||||
#else
|
||||
x0 = x0 & tex->wMinus1;
|
||||
x1 = x1 & tex->wMinus1;
|
||||
@ -2421,8 +2423,10 @@ static void sw_texture_sample_linear(float *SW_RESTRICT color, const sw_texture_
|
||||
if (tex->tWrap == SW_REPEAT)
|
||||
{
|
||||
#if RLSW_SUPPORT_NPOT_TEXTURE
|
||||
y0 = (y0%tex->height + tex->height)%tex->height;
|
||||
y1 = (y1%tex->height + tex->height)%tex->height;
|
||||
y0 %= tex->height;
|
||||
if (y0 < 0) y0 += tex->height; // The modulo result may be negative. If that's the case, shift y0 back to the range [0, height)
|
||||
y1 = y0 + 1;
|
||||
if (y1 == tex->height) y1 = 0; // If y1 goes past the bottom row, wrap back to 0
|
||||
#else
|
||||
y0 = y0 & tex->hMinus1;
|
||||
y1 = y1 & tex->hMinus1;
|
||||
@ -4359,7 +4363,7 @@ void swPopMatrix(void)
|
||||
{
|
||||
case SW_PROJECTION:
|
||||
{
|
||||
if (RLSW.stackProjectionCounter <= 0)
|
||||
if (RLSW.stackProjectionCounter <= 1)
|
||||
{
|
||||
RLSW.errCode = SW_STACK_UNDERFLOW;
|
||||
return;
|
||||
@ -4370,7 +4374,7 @@ void swPopMatrix(void)
|
||||
} break;
|
||||
case SW_MODELVIEW:
|
||||
{
|
||||
if (RLSW.stackModelviewCounter <= 0)
|
||||
if (RLSW.stackModelviewCounter <= 1)
|
||||
{
|
||||
RLSW.errCode = SW_STACK_UNDERFLOW;
|
||||
return;
|
||||
@ -4381,7 +4385,7 @@ void swPopMatrix(void)
|
||||
} break;
|
||||
case SW_TEXTURE:
|
||||
{
|
||||
if (RLSW.stackTextureCounter <= 0)
|
||||
if (RLSW.stackTextureCounter <= 1)
|
||||
{
|
||||
RLSW.errCode = SW_STACK_UNDERFLOW;
|
||||
return;
|
||||
|
||||
@ -66,8 +66,24 @@
|
||||
#undef DrawTextExW
|
||||
|
||||
#include <windowsx.h>
|
||||
#include <shellscalingapi.h>
|
||||
#include <versionhelpers.h>
|
||||
|
||||
// Avoid including shellscalingapi.h by defining these two enums directly
|
||||
#ifndef DPI_ENUMS_DECLARED
|
||||
typedef enum PROCESS_DPI_AWARENESS {
|
||||
PROCESS_DPI_UNAWARE = 0,
|
||||
PROCESS_SYSTEM_DPI_AWARE = 1,
|
||||
PROCESS_PER_MONITOR_DPI_AWARE = 2
|
||||
} PROCESS_DPI_AWARENESS;
|
||||
|
||||
typedef enum MONITOR_DPI_TYPE {
|
||||
MDT_EFFECTIVE_DPI = 0,
|
||||
MDT_ANGULAR_DPI = 1,
|
||||
MDT_RAW_DPI = 2,
|
||||
MDT_DEFAULT = MDT_EFFECTIVE_DPI
|
||||
} MONITOR_DPI_TYPE;
|
||||
|
||||
#define DPI_ENUMS_DECLARED
|
||||
#endif
|
||||
|
||||
#include <malloc.h> // Required for alloca()
|
||||
|
||||
@ -101,6 +117,8 @@ typedef struct {
|
||||
int appScreenHeight;
|
||||
unsigned int desiredFlags;
|
||||
|
||||
bool perMonitorDPIAwareV2; // Flag to check DPI-awareness level. This is used mainly to distinguish between per-monitor DPI awareness v1 and v2
|
||||
|
||||
LARGE_INTEGER timerFrequency;
|
||||
|
||||
char *clipboardText;
|
||||
@ -115,6 +133,12 @@ typedef const char *(WINAPI *PFNWGLGETEXTENSIONSSTRINGARBPROC)(HDC hdc);
|
||||
// Define Win32 function pointer types
|
||||
typedef UINT (WINAPI *GetDpiForWindowProc)(HWND hwnd);
|
||||
typedef BOOL (WINAPI *AdjustWindowRectExForDpiProc)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
|
||||
typedef BOOL (WINAPI *SetProcessDpiAwarenessContextProc)(HANDLE value);
|
||||
typedef HRESULT (WINAPI *SetProcessDpiAwarenessProc)(PROCESS_DPI_AWARENESS value);
|
||||
typedef BOOL (WINAPI *SetProcessDPIAwareProc)(void);
|
||||
typedef HRESULT (WINAPI *GetDpiForMonitorProc)(HMONITOR monitor, MONITOR_DPI_TYPE dpiType, UINT *dpiX, UINT *dpiY);
|
||||
typedef HANDLE (WINAPI *GetThreadDpiAwarenessContextProc)(void);
|
||||
typedef BOOL (WINAPI *AreDpiAwarenessContextsEqualProc)(HANDLE a, HANDLE b);
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
@ -130,8 +154,17 @@ static PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
|
||||
static PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = NULL;
|
||||
|
||||
// Win32 functions that need to be dynamically loaded to support older versions of Windows
|
||||
// Before calling any of these functions, they should be null-checked
|
||||
static GetDpiForWindowProc pGetDpiForWindow = NULL;
|
||||
static AdjustWindowRectExForDpiProc pAdjustWindowRectExForDpi = NULL;
|
||||
static SetProcessDpiAwarenessContextProc pSetProcessDpiAwarenessContext = NULL;
|
||||
static SetProcessDpiAwarenessProc pSetProcessDpiAwareness = NULL;
|
||||
static SetProcessDPIAwareProc pSetProcessDPIAware = NULL;
|
||||
static GetDpiForMonitorProc pGetDpiForMonitor = NULL;
|
||||
static GetThreadDpiAwarenessContextProc pGetThreadDpiAwarenessContext = NULL;
|
||||
static AreDpiAwarenessContextsEqualProc pAreDpiAwarenessContextsEqual = NULL;
|
||||
|
||||
static HMODULE shcore = NULL; // shcore.dll (Windows 8.1 and early versions of Windows 10): loaded once, NULL on other versions
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// This part of the file contains pure functions that never access global state
|
||||
@ -213,8 +246,7 @@ static AdjustWindowRectExForDpiProc pAdjustWindowRectExForDpi = NULL;
|
||||
#define WGL_CONTEXT_ES_PROFILE_BIT_EXT 0x00000004
|
||||
#define WGL_CONTEXT_ES2_PROFILE_BIT_EXT 0x00000004
|
||||
|
||||
// Older versions of Windows don't support these events
|
||||
// These events won't be triggered, but this allows them to compile
|
||||
// Polyfill macro definitions and enums for older Windows SDKs
|
||||
#ifndef WM_DPICHANGED
|
||||
#define WM_DPICHANGED 0x02E0
|
||||
#endif
|
||||
@ -227,6 +259,13 @@ static AdjustWindowRectExForDpiProc pAdjustWindowRectExForDpi = NULL;
|
||||
#define WM_MOUSEHWHEEL 0x020E
|
||||
#endif
|
||||
|
||||
#ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE
|
||||
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((HANDLE)-3)
|
||||
#endif
|
||||
#ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
|
||||
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((HANDLE)-4)
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
@ -399,8 +438,26 @@ static void AToWCopy(const char *ascii, wchar_t *outPtr, size_t outLen)
|
||||
static void LoadWin32FunctionPointers(void)
|
||||
{
|
||||
HMODULE user32 = GetModuleHandleW(L"user32.dll");
|
||||
|
||||
pGetDpiForWindow = (GetDpiForWindowProc)GetProcAddress(user32, "GetDpiForWindow");
|
||||
pAdjustWindowRectExForDpi = (AdjustWindowRectExForDpiProc)GetProcAddress(user32, "AdjustWindowRectExForDpi");
|
||||
pSetProcessDpiAwarenessContext = (SetProcessDpiAwarenessContextProc)GetProcAddress(user32, "SetProcessDpiAwarenessContext");
|
||||
|
||||
pSetProcessDPIAware = (SetProcessDPIAwareProc)GetProcAddress(user32, "SetProcessDPIAware");
|
||||
pGetThreadDpiAwarenessContext = (GetThreadDpiAwarenessContextProc)GetProcAddress(user32, "GetThreadDpiAwarenessContext");
|
||||
pAreDpiAwarenessContextsEqual = (AreDpiAwarenessContextsEqualProc)GetProcAddress(user32, "AreDpiAwarenessContextsEqual");
|
||||
|
||||
// SetProcessDpiAwarenessContext (Windows 10 v1703) supersedes SetProcessDpiAwareness (Windows 8.1)
|
||||
// We can avoid loading the shcore.dll if the newer DPI functions are available
|
||||
if (pSetProcessDpiAwarenessContext == NULL)
|
||||
{
|
||||
shcore = LoadLibraryW(L"shcore.dll");
|
||||
if (shcore != NULL)
|
||||
{
|
||||
pSetProcessDpiAwareness = (SetProcessDpiAwarenessProc)GetProcAddress(shcore, "SetProcessDpiAwareness");
|
||||
pGetDpiForMonitor = (GetDpiForMonitorProc)GetProcAddress(shcore, "GetDpiForMonitor");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetDpiForWindow with a fallback for older versions of Windows
|
||||
@ -411,15 +468,25 @@ static UINT GetDpiForWindowCompat(HWND hwnd)
|
||||
if (pGetDpiForWindow != NULL)
|
||||
{
|
||||
// This API requires Windows 10 v1607 or newer
|
||||
// Returns 0 on failure
|
||||
result = pGetDpiForWindow(hwnd);
|
||||
}
|
||||
else
|
||||
else if (pGetDpiForMonitor != NULL)
|
||||
{
|
||||
// Window uses CS_OWNDC, so we don't need to call ReleaseDC
|
||||
HDC dc = GetDC(hwnd);
|
||||
// Windows 8.1 and early versions of Windows 10
|
||||
// Get the DPI based on which monitor the window is on
|
||||
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
|
||||
UINT dpiX = 0;
|
||||
UINT dpiY = 0;
|
||||
HRESULT hr = pGetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
|
||||
if (hr == S_OK) result = dpiX;
|
||||
}
|
||||
|
||||
// Before Windows 10 v1607: fallback to system-wide DPI (96 for DPI-unaware processes)
|
||||
result = GetDeviceCaps(dc, LOGPIXELSX);
|
||||
if (result == 0)
|
||||
{
|
||||
// If running on older versions of Windows or if the modern API calls fail: fallback to system-wide DPI
|
||||
// We can call GetDC without a corresponding ReleaseDC because the window was created with CS_OWNDC
|
||||
result = GetDeviceCaps(GetDC(hwnd), LOGPIXELSX);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -429,7 +496,7 @@ static UINT GetDpiForWindowCompat(HWND hwnd)
|
||||
static BOOL AdjustWindowRectExForDpiCompat(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi)
|
||||
{
|
||||
BOOL result = FALSE;
|
||||
if (pAdjustWindowRectExForDpi != NULL)
|
||||
if ((pAdjustWindowRectExForDpi != NULL) && platform.perMonitorDPIAwareV2)
|
||||
{
|
||||
result = pAdjustWindowRectExForDpi(lpRect, dwStyle, bMenu, dwExStyle, dpi);
|
||||
}
|
||||
@ -440,6 +507,44 @@ static BOOL AdjustWindowRectExForDpiCompat(LPRECT lpRect, DWORD dwStyle, BOOL bM
|
||||
return result;
|
||||
}
|
||||
|
||||
// Set process to highest level of dpi awareness on this version of Windows
|
||||
// DPI awareness can only be set once. This function will fail on subsequent calls (which is harmless)
|
||||
// Windows 10 v1703+ - per-monitor awareness v2
|
||||
// Windows 8.1/10 - per-monitor awareness v1
|
||||
// Windows Vista/7/8 - system-aware
|
||||
// Windows XP - No DPI awareness
|
||||
static void SetProcessDpiAwareCompat(void)
|
||||
{
|
||||
if (pSetProcessDpiAwarenessContext != NULL)
|
||||
{
|
||||
// Windows 10 v1703+
|
||||
BOOL succeeded = pSetProcessDpiAwarenessContext((HANDLE)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
||||
|
||||
if (!succeeded)
|
||||
{
|
||||
// If monitor aware v2 is not available, try v1
|
||||
pSetProcessDpiAwarenessContext((HANDLE)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
|
||||
}
|
||||
}
|
||||
else if (pSetProcessDpiAwareness != NULL)
|
||||
{
|
||||
// Windows 8.1+
|
||||
pSetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
|
||||
}
|
||||
else if (pSetProcessDPIAware != NULL)
|
||||
{
|
||||
// Windows Vista+
|
||||
pSetProcessDPIAware();
|
||||
}
|
||||
|
||||
platform.perMonitorDPIAwareV2 = false;
|
||||
if ((pGetThreadDpiAwarenessContext != NULL) && (pAreDpiAwarenessContextsEqual != NULL))
|
||||
{
|
||||
// There are subtle differences between per-monitor v1 and v2
|
||||
platform.perMonitorDPIAwareV2 = pAreDpiAwarenessContextsEqual(pGetThreadDpiAwarenessContext(), (HANDLE)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
||||
}
|
||||
}
|
||||
|
||||
static bool DecoratedFromStyle(DWORD style)
|
||||
{
|
||||
if (style & STYLE_FLAGS_UNDECORATED_ON)
|
||||
@ -449,7 +554,7 @@ static bool DecoratedFromStyle(DWORD style)
|
||||
}
|
||||
|
||||
DWORD masked = (style & STYLE_FLAGS_UNDECORATED_OFF);
|
||||
if (STYLE_FLAGS_UNDECORATED_OFF != masked)
|
||||
if (STYLE_FLAGS_UNDECORATED_OFF != masked)
|
||||
{
|
||||
TRACELOG(LOG_ERROR, "WIN32: FLAGS: Style 0x%x is missing flags 0x%x", masked, masked ^ STYLE_FLAGS_UNDECORATED_OFF);
|
||||
}
|
||||
@ -586,8 +691,8 @@ static bool UpdateWindowSize(int mode, HWND hwnd, int width, int height, unsigne
|
||||
float dpiScale = ((float)dpi)/96.0f;
|
||||
bool dpiScaling = flags & FLAG_WINDOW_HIGHDPI;
|
||||
SIZE desiredSize = {
|
||||
.cx = dpiScaling? (int)((float)width*dpiScale) : width,
|
||||
.cy = dpiScaling? (int)((float)height*dpiScale) : height
|
||||
.cx = dpiScaling? (int)roundf((float)width*dpiScale) : width,
|
||||
.cy = dpiScaling? (int)roundf((float)height*dpiScale) : height
|
||||
};
|
||||
|
||||
// Get client size (framebuffer inside the window)
|
||||
@ -632,33 +737,6 @@ static bool UpdateWindowSize(int mode, HWND hwnd, int width, int height, unsigne
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if running in Windows 10 version 1703 (Creators Update)
|
||||
static BOOL IsWindows10Version1703OrGreaterWin32(void)
|
||||
{
|
||||
HMODULE ntdll = LoadLibraryW(L"ntdll.dll");
|
||||
|
||||
DWORD (*Verify)(RTL_OSVERSIONINFOEXW*, ULONG, ULONGLONG) =
|
||||
(DWORD (*)(RTL_OSVERSIONINFOEXW*, ULONG, ULONGLONG))GetProcAddress(ntdll, "RtlVerifyVersionInfo");
|
||||
if (!Verify)
|
||||
{
|
||||
TRACELOG(LOG_ERROR, "WIN32: Failed to verify Windows version [ERROR: %lu]", GetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
RTL_OSVERSIONINFOEXW osvi = { 0 };
|
||||
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
|
||||
osvi.dwMajorVersion = 10;
|
||||
osvi.dwMinorVersion = 0;
|
||||
osvi.dwBuildNumber = 15063; // Build 15063 corresponds to Windows 10 version 1703 (Creators Update)
|
||||
|
||||
DWORDLONG cond = 0;
|
||||
VER_SET_CONDITION(cond, VER_MAJORVERSION, VER_GREATER_EQUAL);
|
||||
VER_SET_CONDITION(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
|
||||
VER_SET_CONDITION(cond, VER_BUILDNUMBER, VER_GREATER_EQUAL);
|
||||
|
||||
return 0 == (*Verify)(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, cond);
|
||||
}
|
||||
|
||||
// Get OpenGL function pointers
|
||||
static void *WglGetProcAddress(const char *procname)
|
||||
{
|
||||
@ -908,7 +986,7 @@ static void HandleRawInput(LPARAM lparam);
|
||||
static void HandleWindowResize(HWND hwnd, int *width, int *height);
|
||||
|
||||
static void UpdateWindowStyle(HWND hwnd, unsigned desiredFlags);
|
||||
static unsigned SanitizeFlags(int mode, unsigned flags);
|
||||
static unsigned SanitizeFlags(unsigned flags);
|
||||
static void UpdateFlags(HWND hwnd, unsigned desiredFlags, int width, int height); // Update window flags
|
||||
|
||||
// Check if OpenGL extension is available
|
||||
@ -965,14 +1043,20 @@ void RestoreWindow(void)
|
||||
// Set window configuration state using flags
|
||||
void SetWindowState(unsigned int flags)
|
||||
{
|
||||
platform.desiredFlags = SanitizeFlags(1 /*SANITIZE_FLAGS_NORMAL*/, CORE.Window.flags | flags);
|
||||
if (flags & FLAG_MSAA_4X_HINT) TRACELOG(LOG_WARNING, "WINDOW: MSAA can only be configured before window initialization");
|
||||
if (flags & FLAG_WINDOW_HIGHDPI) TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
|
||||
|
||||
platform.desiredFlags = SanitizeFlags(CORE.Window.flags | flags);
|
||||
UpdateFlags(platform.hwnd, platform.desiredFlags, platform.appScreenWidth, platform.appScreenHeight);
|
||||
}
|
||||
|
||||
// Clear window configuration state flags
|
||||
void ClearWindowState(unsigned int flags)
|
||||
{
|
||||
platform.desiredFlags = SanitizeFlags(1 /*SANITIZE_FLAGS_NORMAL*/, CORE.Window.flags & ~flags);
|
||||
if (flags & FLAG_MSAA_4X_HINT) TRACELOG(LOG_WARNING, "WINDOW: MSAA can only be configured before window initialization");
|
||||
if (flags & FLAG_WINDOW_HIGHDPI) TRACELOG(LOG_WARNING, "WINDOW: High DPI can only be configured before window initialization");
|
||||
|
||||
platform.desiredFlags = SanitizeFlags(CORE.Window.flags & ~flags);
|
||||
UpdateFlags(platform.hwnd, platform.desiredFlags, platform.appScreenWidth, platform.appScreenHeight);
|
||||
}
|
||||
|
||||
@ -1532,9 +1616,12 @@ void SetMousePosition(int x, int y)
|
||||
// Skip if window is minimized or if ClientToScreen calculation fails
|
||||
if (!IsIconic(platform.hwnd) && ClientToScreen(platform.hwnd, &topLeft))
|
||||
{
|
||||
if (SetCursorPos(topLeft.x + x, topLeft.y + y))
|
||||
Vector2 scale = CORE.Input.Mouse.scale;
|
||||
int xScaled = (int)roundf(x/scale.x);
|
||||
int yScaled = (int)roundf(y/scale.y);
|
||||
if (SetCursorPos(topLeft.x + xScaled, topLeft.y + yScaled))
|
||||
{
|
||||
CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };
|
||||
CORE.Input.Mouse.currentPosition = (Vector2){ (float)xScaled, (float)yScaled };
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1834,7 +1921,7 @@ int InitPlatform(void)
|
||||
|
||||
platform.appScreenWidth = CORE.Window.screen.width;
|
||||
platform.appScreenHeight = CORE.Window.screen.height;
|
||||
platform.desiredFlags = SanitizeFlags(0 /*SANITIZE_FLAGS_FIRST*/, CORE.Window.flags);
|
||||
platform.desiredFlags = SanitizeFlags(CORE.Window.flags);
|
||||
|
||||
// NOTE: From this point CORE.Window.flags should always reflect the actual state of the window
|
||||
CORE.Window.flags = FLAG_WINDOW_HIDDEN | (platform.desiredFlags & FLAG_MASK_NO_UPDATE);
|
||||
@ -1842,22 +1929,9 @@ int InitPlatform(void)
|
||||
CORE.Window.screenMax.height = 9999;
|
||||
|
||||
LoadWin32FunctionPointers();
|
||||
/*
|
||||
// TODO: Review SetProcessDpiAwarenessContext()
|
||||
// NOTE: SetProcessDpiAwarenessContext() requires Windows 10, version 1703 and shcore.lib linkage
|
||||
if (IsWindows10Version1703OrGreaterWin32())
|
||||
{
|
||||
TRACELOG(LOG_INFO, "DpiAware: >=Win10Creators");
|
||||
if (!SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
|
||||
TRACELOG(LOG_ERROR, "%s failed, error %u", "SetProcessDpiAwarenessContext", GetLastError());
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACELOG(LOG_INFO, "DpiAware: <Win10Creators");
|
||||
HRESULT hr = SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
|
||||
if (hr < 0) TRACELOG(LOG_ERROR, "%s failed, hresult=0x%lx", "SetProcessDpiAwareness", (DWORD)hr);
|
||||
}
|
||||
*/
|
||||
|
||||
SetProcessDpiAwareCompat();
|
||||
|
||||
HINSTANCE hInstance = GetModuleHandleW(0);
|
||||
|
||||
// Define window class
|
||||
@ -1898,16 +1972,6 @@ int InitPlatform(void)
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "WIN32: DISPLAY: Failed to get primary monitor from point [ERROR: %u]", GetLastError());
|
||||
|
||||
// Adjust the window rectangle so the *client area* matches desired size
|
||||
// NOTE: Window width/height includes borders and title-bar
|
||||
DWORD style = WS_OVERLAPPEDWINDOW;
|
||||
RECT rect = { 0, 0, platform.appScreenWidth, platform.appScreenHeight };
|
||||
AdjustWindowRect(&rect, style, FALSE);
|
||||
//AdjustWindowRectEx(&rect, WS_OVERLAPPEDWINDOW, FALSE, WINDOW_STYLE_EX);
|
||||
//AdjustWindowRectExForDpi(&rect, style, FALSE, WINDOW_STYLE_EX, dpi);
|
||||
int windowWidth = rect.right - rect.left;
|
||||
int windowHeight = rect.bottom - rect.top;
|
||||
|
||||
// Create window
|
||||
// NOTE: Title string needs to be converted to WCHAR
|
||||
WCHAR *titleWide = NULL;
|
||||
@ -1918,9 +1982,9 @@ int InitPlatform(void)
|
||||
WINDOW_STYLE_EX,
|
||||
CLASS_NAME,
|
||||
titleWide,
|
||||
MakeWindowStyle(CORE.Window.flags), // WS_OVERLAPPEDWINDOW | WS_VISIBLE
|
||||
MakeWindowStyle(CORE.Window.flags | FLAG_WINDOW_HIDDEN),
|
||||
CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
windowWidth, windowHeight, // TODO: Window size [width, height], needs to be updated?
|
||||
CW_USEDEFAULT, CW_USEDEFAULT, // Window size/position will be overridden after creation
|
||||
NULL, NULL,
|
||||
GetModuleHandleW(NULL), NULL);
|
||||
|
||||
@ -1934,6 +1998,10 @@ int InitPlatform(void)
|
||||
// NOTE: Windows GDI object that represents a drawing surface
|
||||
platform.hdc = GetDC(platform.hwnd);
|
||||
|
||||
bool highDPIWindowFlag = platform.desiredFlags & FLAG_WINDOW_HIGHDPI;
|
||||
UINT dpi = GetDpiForWindowCompat(platform.hwnd);
|
||||
float dpiScale = dpi/96.0f;
|
||||
|
||||
if (rlGetVersion() != RL_OPENGL_SOFTWARE)
|
||||
{
|
||||
// Init hardware-accelerated OpenGL modern context
|
||||
@ -1942,6 +2010,41 @@ int InitPlatform(void)
|
||||
|
||||
CORE.Window.ready = true;
|
||||
|
||||
RECT rect = { 0, 0, platform.appScreenWidth, platform.appScreenHeight };
|
||||
if (highDPIWindowFlag)
|
||||
{
|
||||
rect.right = (LONG)roundf(platform.appScreenWidth*dpiScale);
|
||||
rect.bottom = (LONG)roundf(platform.appScreenHeight*dpiScale);
|
||||
}
|
||||
AdjustWindowRectExForDpiCompat(
|
||||
&rect, MakeWindowStyle(platform.desiredFlags),
|
||||
FALSE, WINDOW_STYLE_EX, dpi
|
||||
);
|
||||
|
||||
int windowWidth = rect.right - rect.left;
|
||||
int windowHeight = rect.bottom - rect.top;
|
||||
|
||||
HMONITOR currentMonitor = MonitorFromWindow(platform.hwnd, MONITOR_DEFAULTTONEAREST);
|
||||
MONITORINFO monitorInfo = { 0 };
|
||||
monitorInfo.cbSize = sizeof(monitorInfo);
|
||||
if (GetMonitorInfoW(currentMonitor, &monitorInfo))
|
||||
{
|
||||
// rcWork is the monitor area, not including the taskbar
|
||||
RECT rcWork = monitorInfo.rcWork;
|
||||
int monitorWidth = rcWork.right - rcWork.left;
|
||||
int monitorHeight = rcWork.bottom - rcWork.top;
|
||||
int x = rcWork.left + (monitorWidth - windowWidth)/2;
|
||||
int y = rcWork.top + (monitorHeight - windowHeight)/2;
|
||||
|
||||
// Ensure the window doesn't go off-screen; otherwise, the user might not be able to move it
|
||||
if (x < rcWork.left) x = rcWork.left;
|
||||
if (y < rcWork.top) y = rcWork.top;
|
||||
|
||||
// Center the window and set it to the correct DPI-adjusted size
|
||||
SetWindowPos(platform.hwnd, NULL, x, y, windowWidth, windowHeight, SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "DISPLAY: Unable to retrieve information about monitor");
|
||||
|
||||
if ((platform.desiredFlags & FLAG_WINDOW_HIDDEN) == 0)
|
||||
{
|
||||
// Show window and taskbar icon, activate unless FLAG_WINDOW_UNFOCUSED is set
|
||||
@ -2038,6 +2141,13 @@ void ClosePlatform(void)
|
||||
platform.clipboardText = NULL;
|
||||
}
|
||||
|
||||
// Unload shcore.dll
|
||||
if (shcore != NULL)
|
||||
{
|
||||
FreeLibrary(shcore);
|
||||
shcore = NULL;
|
||||
}
|
||||
|
||||
#if SUPPORT_WINMM_HIGHRES_TIMER && !SUPPORT_BUSY_WAIT_LOOP
|
||||
timeEndPeriod(1); // Restore time period
|
||||
#endif
|
||||
@ -2136,11 +2246,13 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpara
|
||||
} break;
|
||||
case WM_GETMINMAXINFO:
|
||||
{
|
||||
UINT dpi = GetDpiForWindowCompat(hwnd);
|
||||
DWORD style = MakeWindowStyle(platform.desiredFlags);
|
||||
SIZE maxClientSize = { CORE.Window.screenMax.width, CORE.Window.screenMax.height };
|
||||
SIZE maxWindowSize = CalcWindowSize(96, maxClientSize, style);
|
||||
SIZE minClientSize = { CORE.Window.screenMin.width, CORE.Window.screenMin.height };
|
||||
SIZE minWindowSize = CalcWindowSize(96, minClientSize, style);
|
||||
float clientScale = (CORE.Window.flags & FLAG_WINDOW_HIGHDPI)? ((float)dpi)/96.0f : 1.0f;
|
||||
SIZE maxClientSize = { (LONG)roundf(CORE.Window.screenMax.width*clientScale), (LONG)roundf(CORE.Window.screenMax.height*clientScale) };
|
||||
SIZE maxWindowSize = CalcWindowSize(dpi, maxClientSize, style);
|
||||
SIZE minClientSize = { (LONG)roundf(CORE.Window.screenMin.width*clientScale), (LONG)roundf(CORE.Window.screenMin.height*clientScale) };
|
||||
SIZE minWindowSize = CalcWindowSize(dpi, minClientSize, style);
|
||||
|
||||
LPMINMAXINFO lpmmi = (LPMINMAXINFO) lparam;
|
||||
lpmmi->ptMaxSize.x = maxWindowSize.cx;
|
||||
@ -2246,42 +2358,65 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lpara
|
||||
UINT newDpi = (UINT)wparam; // TODO: WARNING: Converting from WPARAM = UINT_PTR
|
||||
|
||||
// For the following flag changes, a window resize event should be posted,
|
||||
// TODO: Should it be done after dpi changes?
|
||||
// Keep the pending size when minimized, maximized, or borderless
|
||||
if (CORE.Window.flags & FLAG_WINDOW_MINIMIZED) return TRUE;
|
||||
if (CORE.Window.flags & FLAG_WINDOW_MAXIMIZED) return TRUE;
|
||||
if (CORE.Window.flags & FLAG_BORDERLESS_WINDOWED_MODE) return TRUE;
|
||||
|
||||
float dpiScale = ((float)newDpi)/96.0f;
|
||||
bool dpiScaling = CORE.Window.flags & FLAG_WINDOW_HIGHDPI;
|
||||
// Get size in pixels from points
|
||||
// Calculate client area in pixels from points
|
||||
SIZE desired = {
|
||||
.cx = dpiScaling? (int)((float)platform.appScreenWidth*dpiScale) : platform.appScreenWidth,
|
||||
.cy = dpiScaling? (int)((float)platform.appScreenHeight*dpiScale) : platform.appScreenHeight
|
||||
.cx = dpiScaling? (int)roundf((float)platform.appScreenWidth*dpiScale) : platform.appScreenWidth,
|
||||
.cy = dpiScaling? (int)roundf((float)platform.appScreenHeight*dpiScale) : platform.appScreenHeight
|
||||
};
|
||||
inoutSize->cx = desired.cx;
|
||||
inoutSize->cy = desired.cy;
|
||||
// Calculate and set window size, including title and window border
|
||||
*inoutSize = CalcWindowSize(newDpi, desired, MakeWindowStyle(CORE.Window.flags));
|
||||
|
||||
result = TRUE;
|
||||
} break;
|
||||
case WM_DPICHANGED:
|
||||
{
|
||||
// Get current dpi scale factor
|
||||
//float scalex = HIWORD(wparam)/96.0f;
|
||||
//float scaley = LOWORD(wparam)/96.0f;
|
||||
//float scalex = LOWORD(wparam)/96.0f;
|
||||
//float scaley = HIWORD(wparam)/96.0f;
|
||||
|
||||
RECT *suggestedRect = (RECT *)lparam;
|
||||
|
||||
// Never set the window size to anything other than the suggested rect here
|
||||
// Doing so can cause a window to stutter between monitors when transitioning between them
|
||||
int result = (int)SetWindowPos(hwnd, NULL,
|
||||
suggestedRect->left, suggestedRect->top,
|
||||
suggestedRect->right - suggestedRect->left,
|
||||
suggestedRect->bottom - suggestedRect->top,
|
||||
SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
if (platform.perMonitorDPIAwareV2)
|
||||
{
|
||||
BOOL succeeded = SetWindowPos(hwnd, NULL,
|
||||
suggestedRect->left, suggestedRect->top,
|
||||
suggestedRect->right - suggestedRect->left,
|
||||
suggestedRect->bottom - suggestedRect->top,
|
||||
SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
|
||||
if (result == 0) TRACELOG(LOG_ERROR, "Failed to set window position [ERROR: %lu]", GetLastError());
|
||||
if (succeeded == FALSE) TRACELOG(LOG_ERROR, "Failed to set window position [ERROR: %lu]", GetLastError());
|
||||
}
|
||||
else if ((CORE.Window.flags & FLAG_WINDOW_HIGHDPI) && !(CORE.Window.flags & (FLAG_WINDOW_MINIMIZED | FLAG_WINDOW_MAXIMIZED | FLAG_BORDERLESS_WINDOWED_MODE)))
|
||||
{
|
||||
// This code only runs when per-monitor dpi awareness v1 is enabled. v1 doesn't send WM_GETDPISCALEDSIZE and the suggested RECT is subtly wrong (doesn't properly account for the title bar size)
|
||||
// The solution is to manually build a properly sized RECT (and ignore the advice of not using the suggested RECT). This makes a per-monitor v1 process match the behavior of per-monitor v2
|
||||
// To prevent the potential stutter when dragging the window between monitors, the manually sized rect keeps the same center as the suggested RECT
|
||||
RECT windowRect = { 0 };
|
||||
RECT clientRect = { 0 };
|
||||
GetWindowRect(hwnd, &windowRect);
|
||||
GetClientRect(hwnd, &clientRect);
|
||||
|
||||
// TODO: Update screen data, render size, screen scaling, viewport...
|
||||
float dpiScale = ((float)LOWORD(wparam))/96.0f;
|
||||
int width = (int)roundf(platform.appScreenWidth*dpiScale) + (windowRect.right - windowRect.left) - clientRect.right;
|
||||
int height = (int)roundf(platform.appScreenHeight*dpiScale) + (windowRect.bottom - windowRect.top) - clientRect.bottom;
|
||||
int x = suggestedRect->left + ((suggestedRect->right - suggestedRect->left) - width)/2;
|
||||
int y = suggestedRect->top + ((suggestedRect->bottom - suggestedRect->top) - height)/2;
|
||||
|
||||
BOOL succeeded = SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
|
||||
if (succeeded == FALSE) TRACELOG(LOG_ERROR, "Failed to set window position [ERROR: %lu]", GetLastError());
|
||||
}
|
||||
|
||||
HandleWindowResize(hwnd, &platform.appScreenWidth, &platform.appScreenHeight);
|
||||
|
||||
} break;
|
||||
case WM_SETCURSOR:
|
||||
@ -2433,7 +2568,7 @@ static void HandleKey(WPARAM wparam, LPARAM lparam, char state)
|
||||
bool isNextExtendedKey = ((HIWORD(next.lParam) & KF_EXTENDED) != 0);
|
||||
if ((next.wParam == VK_MENU) && ((LONG)next.time == time) && isNextExtendedKey)
|
||||
{
|
||||
if ((next.message == WM_KEYDOWN) || (next.message == WM_SYSKEYDOWN) ||
|
||||
if ((next.message == WM_KEYDOWN) || (next.message == WM_SYSKEYDOWN) ||
|
||||
(next.message == WM_KEYUP) || (next.message == WM_SYSKEYUP)) ignoreKey = true;
|
||||
}
|
||||
}
|
||||
@ -2532,16 +2667,16 @@ static void HandleWindowResize(HWND hwnd, int *width, int *height)
|
||||
GetClientRect(hwnd, &rect);
|
||||
SIZE clientSize = { rect.right, rect.bottom };
|
||||
|
||||
if ((CORE.Window.render.width != clientSize.cx) || (CORE.Window.render.height != clientSize.cy)) CORE.Window.resizedLastFrame = true;
|
||||
CORE.Window.render.width = (int)clientSize.cx;
|
||||
CORE.Window.render.height = (int)clientSize.cy;
|
||||
CORE.Window.currentFbo.width = (int)clientSize.cx;
|
||||
CORE.Window.currentFbo.height = (int)clientSize.cy;
|
||||
|
||||
CORE.Window.resizedLastFrame = true;
|
||||
float dpiScale = ((float)GetDpiForWindowCompat(hwnd))/96.0f;
|
||||
bool highdpi = !!(CORE.Window.flags & FLAG_WINDOW_HIGHDPI);
|
||||
unsigned int screenWidth = highdpi? (unsigned int)(((float)clientSize.cx)/dpiScale) : clientSize.cx;
|
||||
unsigned int screenHeight = highdpi? (unsigned int)(((float)clientSize.cy)/dpiScale) : clientSize.cy;
|
||||
unsigned int screenWidth = highdpi? (unsigned int)roundf(((float)clientSize.cx)/dpiScale) : clientSize.cx;
|
||||
unsigned int screenHeight = highdpi? (unsigned int)roundf(((float)clientSize.cy)/dpiScale) : clientSize.cy;
|
||||
CORE.Window.screen.width = screenWidth;
|
||||
CORE.Window.screen.height = screenHeight;
|
||||
|
||||
@ -2554,6 +2689,12 @@ static void HandleWindowResize(HWND hwnd, int *width, int *height)
|
||||
|
||||
CORE.Window.screenScale = MatrixScale( (float)CORE.Window.render.width/CORE.Window.screen.width,
|
||||
(float)CORE.Window.render.height/CORE.Window.screen.height, 1.0f);
|
||||
|
||||
if (CORE.Window.flags & FLAG_WINDOW_HIGHDPI)
|
||||
{
|
||||
float mouseScale = 1.0f/dpiScale;
|
||||
CORE.Input.Mouse.scale = (Vector2){ mouseScale, mouseScale };
|
||||
}
|
||||
}
|
||||
|
||||
// Update window style
|
||||
@ -2595,7 +2736,7 @@ static void UpdateWindowStyle(HWND hwnd, unsigned desiredFlags)
|
||||
}
|
||||
|
||||
// Sanitize flags
|
||||
static unsigned SanitizeFlags(int mode, unsigned flags)
|
||||
static unsigned SanitizeFlags(unsigned flags)
|
||||
{
|
||||
if (flags & FLAG_WINDOW_MAXIMIZED)
|
||||
{
|
||||
@ -2634,14 +2775,8 @@ static unsigned SanitizeFlags(int mode, unsigned flags)
|
||||
}
|
||||
}
|
||||
|
||||
if (mode == 1)
|
||||
{
|
||||
if ((flags & FLAG_MSAA_4X_HINT) && (!(CORE.Window.flags & FLAG_MSAA_4X_HINT)))
|
||||
{
|
||||
TRACELOG(LOG_WARNING, "WIN32: WINDOW: MSAA can only be configured before window initialization");
|
||||
flags &= ~FLAG_MSAA_4X_HINT;
|
||||
}
|
||||
}
|
||||
// Don't change flags that can't be updated after window initialization
|
||||
flags = (flags & ~FLAG_MASK_NO_UPDATE) | (CORE.Window.flags & FLAG_MASK_NO_UPDATE);
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
94
src/raudio.c
94
src/raudio.c
@ -157,6 +157,7 @@ typedef struct tagBITMAPINFOHEADER {
|
||||
#endif
|
||||
|
||||
#define MA_MALLOC RL_MALLOC
|
||||
#define MA_REALLOC RL_REALLOC
|
||||
#define MA_FREE RL_FREE
|
||||
|
||||
#define MA_NO_JACK
|
||||
@ -577,12 +578,6 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
|
||||
{
|
||||
AudioBuffer *audioBuffer = (AudioBuffer *)RL_CALLOC(1, sizeof(AudioBuffer));
|
||||
|
||||
if (audioBuffer == NULL)
|
||||
{
|
||||
TRACELOG(LOG_WARNING, "AUDIO: Failed to allocate memory for buffer");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (sizeInFrames > 0) audioBuffer->data = (unsigned char *)RL_CALLOC(sizeInFrames*channels*ma_get_bytes_per_sample(format), 1);
|
||||
|
||||
// Audio data runs through a format converter
|
||||
@ -1266,6 +1261,7 @@ void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels)
|
||||
void *data = RL_CALLOC(frameCount*channels*(sampleSize/8), 1);
|
||||
|
||||
frameCount = (ma_uint32)ma_convert_frames(data, frameCount, formatOut, channels, sampleRate, wave->data, frameCountIn, formatIn, wave->channels, wave->sampleRate);
|
||||
|
||||
if (frameCount == 0)
|
||||
{
|
||||
RL_FREE(data);
|
||||
@ -1285,22 +1281,16 @@ void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels)
|
||||
// Copy a wave to a new wave
|
||||
Wave WaveCopy(Wave wave)
|
||||
{
|
||||
Wave newWave = { 0 };
|
||||
Wave copy = { 0 };
|
||||
|
||||
newWave.data = RL_MALLOC(wave.frameCount*wave.channels*wave.sampleSize/8);
|
||||
copy.data = RL_CALLOC(wave.frameCount*wave.channels*wave.sampleSize/8, 1);
|
||||
memcpy(copy.data, wave.data, wave.frameCount*wave.channels*wave.sampleSize/8);
|
||||
copy.frameCount = wave.frameCount;
|
||||
copy.sampleRate = wave.sampleRate;
|
||||
copy.sampleSize = wave.sampleSize;
|
||||
copy.channels = wave.channels;
|
||||
|
||||
if (newWave.data != NULL)
|
||||
{
|
||||
// NOTE: Size must be provided in bytes
|
||||
memcpy(newWave.data, wave.data, wave.frameCount*wave.channels*wave.sampleSize/8);
|
||||
|
||||
newWave.frameCount = wave.frameCount;
|
||||
newWave.sampleRate = wave.sampleRate;
|
||||
newWave.sampleSize = wave.sampleSize;
|
||||
newWave.channels = wave.channels;
|
||||
}
|
||||
|
||||
return newWave;
|
||||
return copy;
|
||||
}
|
||||
|
||||
// Crop a wave to defined frames range
|
||||
@ -1311,7 +1301,7 @@ void WaveCrop(Wave *wave, int initFrame, int finalFrame)
|
||||
{
|
||||
int frameCount = finalFrame - initFrame;
|
||||
|
||||
void *data = RL_MALLOC(frameCount*wave->channels*wave->sampleSize/8);
|
||||
void *data = RL_CALLOC(frameCount*wave->channels*wave->sampleSize/8, 1);
|
||||
|
||||
memcpy(data, (unsigned char *)wave->data + (initFrame*wave->channels*wave->sampleSize/8), frameCount*wave->channels*wave->sampleSize/8);
|
||||
|
||||
@ -1327,7 +1317,7 @@ void WaveCrop(Wave *wave, int initFrame, int finalFrame)
|
||||
// NOTE 2: Sample data allocated should be freed with UnloadWaveSamples()
|
||||
float *LoadWaveSamples(Wave wave)
|
||||
{
|
||||
float *samples = (float *)RL_MALLOC(wave.frameCount*wave.channels*sizeof(float));
|
||||
float *samples = (float *)RL_CALLOC(wave.frameCount*wave.channels*sizeof(float), 1);
|
||||
|
||||
// NOTE: sampleCount is the total number of interlaced samples (including channels)
|
||||
|
||||
@ -1558,7 +1548,6 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data,
|
||||
else if ((strcmp(fileType, ".wav") == 0) || (strcmp(fileType, ".WAV") == 0))
|
||||
{
|
||||
drwav *ctxWav = (drwav *)RL_CALLOC(1, sizeof(drwav));
|
||||
|
||||
bool success = drwav_init_memory(ctxWav, (const void *)data, dataSize, NULL);
|
||||
|
||||
if (success)
|
||||
@ -1706,13 +1695,13 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data,
|
||||
#if SUPPORT_FILEFORMAT_MOD
|
||||
else if ((strcmp(fileType, ".mod") == 0) || (strcmp(fileType, ".MOD") == 0))
|
||||
{
|
||||
jar_mod_context_t *ctxMod = (jar_mod_context_t *)RL_MALLOC(sizeof(jar_mod_context_t));
|
||||
jar_mod_context_t *ctxMod = (jar_mod_context_t *)RL_CALLOC(1, sizeof(jar_mod_context_t));
|
||||
int result = 0;
|
||||
|
||||
jar_mod_init(ctxMod);
|
||||
|
||||
// Copy data to allocated memory for default UnloadMusicStream
|
||||
unsigned char *newData = (unsigned char *)RL_MALLOC(dataSize);
|
||||
unsigned char *newData = (unsigned char *)RL_CALLOC(dataSize, 1);
|
||||
for (int i = 0; i < dataSize; i++) newData[i] = data[i];
|
||||
|
||||
// Memory loaded version for jar_mod_load_file()
|
||||
@ -2099,31 +2088,18 @@ float GetMusicTimePlayed(Music music)
|
||||
float secondsPlayed = 0.0f;
|
||||
if (music.stream.buffer != NULL)
|
||||
{
|
||||
#if SUPPORT_FILEFORMAT_XM
|
||||
if (music.ctxType == MUSIC_MODULE_XM)
|
||||
{
|
||||
uint64_t framesPlayed = 0;
|
||||
|
||||
jar_xm_get_position((jar_xm_context_t *)music.ctxData, NULL, NULL, NULL, &framesPlayed);
|
||||
secondsPlayed = (float)framesPlayed/music.stream.sampleRate;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
//ma_uint32 frameSizeInBytes = ma_get_bytes_per_sample(music.stream.buffer->dsp.formatConverterIn.config.formatIn)*music.stream.buffer->dsp.formatConverterIn.config.channels;
|
||||
int framesProcessed = (int)music.stream.buffer->framesProcessed;
|
||||
int subBufferSize = (int)music.stream.buffer->sizeInFrames/2;
|
||||
int framesInFirstBuffer = music.stream.buffer->isSubBufferProcessed[0]? 0 : subBufferSize;
|
||||
int framesInSecondBuffer = music.stream.buffer->isSubBufferProcessed[1]? 0 : subBufferSize;
|
||||
int framesInBuffers = framesInFirstBuffer + framesInSecondBuffer;
|
||||
if (((unsigned int)framesInBuffers > music.frameCount) && !music.looping) framesInBuffers = music.frameCount;
|
||||
int framesSentToMix = music.stream.buffer->frameCursorPos%subBufferSize;
|
||||
int framesPlayed = (framesProcessed - framesInBuffers + framesSentToMix)%(int)music.frameCount;
|
||||
if (framesPlayed < 0) framesPlayed += music.frameCount;
|
||||
secondsPlayed = (float)framesPlayed/music.stream.sampleRate;
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
ma_mutex_lock(&AUDIO.System.lock);
|
||||
int framesProcessed = (int)music.stream.buffer->framesProcessed;
|
||||
int subBufferSize = (int)music.stream.buffer->sizeInFrames/2;
|
||||
int framesInFirstBuffer = music.stream.buffer->isSubBufferProcessed[0]? 0 : subBufferSize;
|
||||
int framesInSecondBuffer = music.stream.buffer->isSubBufferProcessed[1]? 0 : subBufferSize;
|
||||
int framesInBuffers = framesInFirstBuffer + framesInSecondBuffer;
|
||||
if (((unsigned int)framesInBuffers > music.frameCount) && !music.looping) framesInBuffers = music.frameCount;
|
||||
int framesSentToMix = music.stream.buffer->frameCursorPos%subBufferSize;
|
||||
int framesPlayed = (framesProcessed - framesInBuffers + framesSentToMix)%(int)music.frameCount;
|
||||
if (framesPlayed < 0) framesPlayed += music.frameCount;
|
||||
secondsPlayed = (float)framesPlayed/music.stream.sampleRate;
|
||||
ma_mutex_unlock(&AUDIO.System.lock);
|
||||
}
|
||||
|
||||
return secondsPlayed;
|
||||
@ -2284,10 +2260,8 @@ void AttachAudioStreamProcessor(AudioStream stream, AudioCallback process)
|
||||
|
||||
rAudioProcessor *last = stream.buffer->processor;
|
||||
|
||||
while (last && last->next)
|
||||
{
|
||||
last = last->next;
|
||||
}
|
||||
while (last && last->next) last = last->next;
|
||||
|
||||
if (last)
|
||||
{
|
||||
processor->prev = last;
|
||||
@ -2337,10 +2311,8 @@ void AttachAudioMixedProcessor(AudioCallback process)
|
||||
|
||||
rAudioProcessor *last = AUDIO.mixedProcessor;
|
||||
|
||||
while (last && last->next)
|
||||
{
|
||||
last = last->next;
|
||||
}
|
||||
while (last && last->next) last = last->next;
|
||||
|
||||
if (last)
|
||||
{
|
||||
processor->prev = last;
|
||||
@ -2596,9 +2568,9 @@ static void OnSendAudioDataToDevice(ma_device *pDevice, void *pFramesOut, const
|
||||
float tempBuffer[1024] = { 0 }; // Frames for stereo
|
||||
|
||||
ma_uint32 framesToReadRightNow = framesToRead;
|
||||
if (framesToReadRightNow > sizeof(tempBuffer)/sizeof(tempBuffer[0])/AUDIO_DEVICE_CHANNELS)
|
||||
if (framesToReadRightNow > (1024/AUDIO_DEVICE_CHANNELS))
|
||||
{
|
||||
framesToReadRightNow = sizeof(tempBuffer)/sizeof(tempBuffer[0])/AUDIO_DEVICE_CHANNELS;
|
||||
framesToReadRightNow = (1024/AUDIO_DEVICE_CHANNELS);
|
||||
}
|
||||
|
||||
ma_uint32 framesJustRead = ReadAudioBufferFramesInMixingFormat(audioBuffer, tempBuffer, framesToReadRightNow);
|
||||
@ -2875,7 +2847,7 @@ static unsigned char *LoadFileData(const char *fileName, int *dataSize)
|
||||
|
||||
if (size > 0)
|
||||
{
|
||||
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
|
||||
data = (unsigned char *)RL_CALLOC(size, 1);
|
||||
|
||||
// NOTE: fread() returns number of read elements instead of bytes, so reading [1 byte, size elements]
|
||||
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
|
||||
|
||||
@ -3129,7 +3129,6 @@ char *EncodeDataBase64(const unsigned char *data, int dataSize, int *outputSize)
|
||||
|
||||
// Load some memory to store encoded string
|
||||
char *encodedData = (char *)RL_CALLOC(estimatedOutputSize, 1);
|
||||
if (encodedData == NULL) return NULL;
|
||||
|
||||
int outputCount = 0;
|
||||
for (int i = 0; i < dataSize;)
|
||||
@ -3197,7 +3196,6 @@ unsigned char *DecodeDataBase64(const char *text, int *outputSize)
|
||||
// Load some memory to store decoded data
|
||||
// NOTE: Allocated enough size to include padding
|
||||
unsigned char *decodedData = (unsigned char *)RL_CALLOC(maxOutputSize, 1);
|
||||
if (decodedData == NULL) return NULL;
|
||||
|
||||
int outputCount = 0;
|
||||
for (int i = 0; i < dataSize;)
|
||||
|
||||
@ -3756,7 +3756,7 @@ void *rlReadTexturePixels(unsigned int id, int width, int height, int format)
|
||||
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||
#else
|
||||
// Reading data as original texture format, in some platforms (RPI, Wasm) it works
|
||||
pixels = (unsigned char *)RL_MALLOC(rlGetPixelDataSize(width, height, format));
|
||||
pixels = (unsigned char *)RL_CALLOC(rlGetPixelDataSize(width, height, format), 1);
|
||||
unsigned int glInternalFormat = 0, glFormat = 0, glType = 0;
|
||||
rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType);
|
||||
glReadPixels(0, 0, width, height, glFormat, glType, pixels);
|
||||
|
||||
73
src/rtext.c
73
src/rtext.c
@ -508,8 +508,8 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
||||
|
||||
// Populate tempCharValues and tempCharsRecs with glyphs data
|
||||
// Move temp data to sized charValues and charRecs arrays
|
||||
font.glyphs = (GlyphInfo *)RL_MALLOC(font.glyphCount*sizeof(GlyphInfo));
|
||||
font.recs = (Rectangle *)RL_MALLOC(font.glyphCount*sizeof(Rectangle));
|
||||
font.glyphs = (GlyphInfo *)RL_CALLOC(font.glyphCount, sizeof(GlyphInfo));
|
||||
font.recs = (Rectangle *)RL_CALLOC(font.glyphCount, sizeof(Rectangle));
|
||||
|
||||
for (int i = 0; i < font.glyphCount; i++)
|
||||
{
|
||||
@ -659,7 +659,7 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
|
||||
// NOTE: By default filling glyphCount consecutively, starting at 32 (Space)
|
||||
if (requiredCodepoints == NULL)
|
||||
{
|
||||
requiredCodepoints = (int *)RL_MALLOC(codepointCount*sizeof(int));
|
||||
requiredCodepoints = (int *)RL_CALLOC(codepointCount, sizeof(int));
|
||||
for (int i = 0; i < codepointCount; i++) requiredCodepoints[i] = i + 32;
|
||||
genFontChars = true;
|
||||
}
|
||||
@ -823,7 +823,7 @@ Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyp
|
||||
glyphCount = (glyphCount > 0)? glyphCount : 95;
|
||||
|
||||
// NOTE: Rectangles memory is loaded here!
|
||||
Rectangle *recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle));
|
||||
Rectangle *recs = (Rectangle *)RL_CALLOC(glyphCount, sizeof(Rectangle));
|
||||
|
||||
// Calculate image size based on total glyph width and glyph row count
|
||||
int totalWidth = 0;
|
||||
@ -927,11 +927,11 @@ Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyp
|
||||
}
|
||||
else if (packMethod == 1) // Use Skyline rect packing algorithm (stb_pack_rect)
|
||||
{
|
||||
stbrp_context *context = (stbrp_context *)RL_MALLOC(sizeof(*context));
|
||||
stbrp_node *nodes = (stbrp_node *)RL_MALLOC(glyphCount*sizeof(*nodes));
|
||||
stbrp_context *context = (stbrp_context *)RL_CALLOC(1, sizeof(stbrp_context));
|
||||
stbrp_node *nodes = (stbrp_node *)RL_CALLOC(glyphCount, sizeof(stbrp_node));
|
||||
|
||||
stbrp_init_target(context, atlas.width, atlas.height, nodes, glyphCount);
|
||||
stbrp_rect *rects = (stbrp_rect *)RL_MALLOC(glyphCount*sizeof(stbrp_rect));
|
||||
stbrp_rect *rects = (stbrp_rect *)RL_CALLOC(glyphCount, sizeof(stbrp_rect));
|
||||
|
||||
// Fill rectangles for packaging
|
||||
for (int i = 0; i < glyphCount; i++)
|
||||
@ -994,7 +994,7 @@ Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyp
|
||||
}
|
||||
|
||||
// Convert image data from GRAYSCALE to GRAY_ALPHA
|
||||
unsigned char *dataGrayAlpha = (unsigned char *)RL_MALLOC(atlas.width*atlas.height*sizeof(unsigned char)*2); // Two channels
|
||||
unsigned char *dataGrayAlpha = (unsigned char *)RL_CALLOC(atlas.width*atlas.height*2, sizeof(unsigned char)); // Two channels
|
||||
|
||||
for (int i = 0, k = 0; i < atlas.width*atlas.height; i++, k += 2)
|
||||
{
|
||||
@ -1876,35 +1876,32 @@ char *TextReplaceAlloc(const char *text, const char *search, const char *replace
|
||||
int tempLen = textLen + (replaceLen - searchLen)*count + 1;
|
||||
temp = result = (char *)RL_CALLOC(tempLen, sizeof(char));
|
||||
|
||||
if (result != NULL) // Memory was allocated
|
||||
// First time through the loop, all the variable are set correctly from here on,
|
||||
// - 'temp' points to the end of the result string
|
||||
// - 'insertPoint' points to the next occurrence of replace in text
|
||||
// - 'text' points to the remainder of text after "end of replace"
|
||||
while (count > 0)
|
||||
{
|
||||
// First time through the loop, all the variable are set correctly from here on,
|
||||
// - 'temp' points to the end of the result string
|
||||
// - 'insertPoint' points to the next occurrence of replace in text
|
||||
// - 'text' points to the remainder of text after "end of replace"
|
||||
while (count > 0)
|
||||
insertPoint = strstr(text, search);
|
||||
lastReplacePos = (int)(insertPoint - text);
|
||||
|
||||
memcpy(temp, text, lastReplacePos);
|
||||
temp += lastReplacePos;
|
||||
|
||||
if (replaceLen > 0)
|
||||
{
|
||||
insertPoint = strstr(text, search);
|
||||
lastReplacePos = (int)(insertPoint - text);
|
||||
|
||||
memcpy(temp, text, lastReplacePos);
|
||||
temp += lastReplacePos;
|
||||
|
||||
if (replaceLen > 0)
|
||||
{
|
||||
memcpy(temp, replacement, replaceLen);
|
||||
temp += replaceLen;
|
||||
}
|
||||
|
||||
text += (lastReplacePos + searchLen); // Move to next "end of replace"
|
||||
|
||||
count--;
|
||||
memcpy(temp, replacement, replaceLen);
|
||||
temp += replaceLen;
|
||||
}
|
||||
|
||||
// Copy remaind text part after replacement to result (pointed by moving temp)
|
||||
// NOTE: Text pointer internal copy has been updated along the process
|
||||
memcpy(temp, text, TextLength(text));
|
||||
text += (lastReplacePos + searchLen); // Move to next "end of replace"
|
||||
|
||||
count--;
|
||||
}
|
||||
|
||||
// Copy remaind text part after replacement to result (pointed by moving temp)
|
||||
// NOTE: Text pointer internal copy has been updated along the process
|
||||
memcpy(temp, text, TextLength(text));
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -2024,11 +2021,11 @@ char *TextInsertAlloc(const char *text, const char *insert, int position)
|
||||
if (position > textLen) position = textLen; // End of text string
|
||||
int insertLen = TextLength(insert);
|
||||
|
||||
result = (char *)RL_MALLOC(textLen + insertLen + 1);
|
||||
result = (char *)RL_CALLOC(textLen + insertLen + 1, 1);
|
||||
|
||||
for (int i = 0; i < position; i++) result[i] = text[i];
|
||||
for (int i = 0; i < insertLen; i++) result[i+position] = insert[i];
|
||||
for (int i = position; i < textLen; i++) result[i+insertLen] = text[i];
|
||||
for (int i = 0; i < insertLen; i++) result[i + position] = insert[i];
|
||||
for (int i = position; i < textLen; i++) result[i + insertLen] = text[i];
|
||||
|
||||
result[textLen + insertLen] = '\0'; // Add EOL
|
||||
}
|
||||
@ -2736,8 +2733,8 @@ static Font LoadBMFont(const char *fileName)
|
||||
font.baseSize = fontSize;
|
||||
font.glyphCount = glyphCount;
|
||||
font.glyphPadding = 0;
|
||||
font.glyphs = (GlyphInfo *)RL_MALLOC(glyphCount*sizeof(GlyphInfo));
|
||||
font.recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle));
|
||||
font.glyphs = (GlyphInfo *)RL_CALLOC(glyphCount, sizeof(GlyphInfo));
|
||||
font.recs = (Rectangle *)RL_CALLOC(glyphCount, sizeof(Rectangle));
|
||||
|
||||
int charId = 0;
|
||||
int charX = 0;
|
||||
@ -2844,7 +2841,7 @@ static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, c
|
||||
|
||||
// In case no chars count provided, default to 95
|
||||
codepointCount = (codepointCount > 0)? codepointCount : 95;
|
||||
int *requiredCodepoints = (int *)RL_MALLOC(codepointCount*sizeof(int));
|
||||
int *requiredCodepoints = (int *)RL_CALLOC(codepointCount, sizeof(int));
|
||||
|
||||
if (codepoints == NULL)
|
||||
{
|
||||
|
||||
@ -320,7 +320,7 @@ Image LoadImageRaw(const char *fileName, int width, int height, int format, int
|
||||
// Offset file data to expected raw image by header size
|
||||
if ((headerSize > 0) && ((headerSize + size) <= dataSize)) dataPtr += headerSize;
|
||||
|
||||
image.data = RL_MALLOC(size); // Allocate required memory in bytes
|
||||
image.data = RL_CALLOC(size, 1); // Allocate required memory in bytes
|
||||
memcpy(image.data, dataPtr, size); // Copy required data to image
|
||||
image.width = width;
|
||||
image.height = height;
|
||||
@ -859,7 +859,7 @@ Image GenImageColor(int width, int height, Color color)
|
||||
// The gradient effectively rotates counter-clockwise by the specified amount
|
||||
Image GenImageGradientLinear(int width, int height, int direction, Color start, Color end)
|
||||
{
|
||||
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
|
||||
|
||||
float radianDirection = (float)(90 - direction)/180.f*3.14159f;
|
||||
float cosDir = cosf(radianDirection);
|
||||
@ -907,7 +907,7 @@ Image GenImageGradientLinear(int width, int height, int direction, Color start,
|
||||
// Generate image: radial gradient
|
||||
Image GenImageGradientRadial(int width, int height, float density, Color inner, Color outer)
|
||||
{
|
||||
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
|
||||
float radius = (width < height)? (float)width/2.0f : (float)height/2.0f;
|
||||
|
||||
float centerX = (float)width/2.0f;
|
||||
@ -944,7 +944,7 @@ Image GenImageGradientRadial(int width, int height, float density, Color inner,
|
||||
// Generate image: square gradient
|
||||
Image GenImageGradientSquare(int width, int height, float density, Color inner, Color outer)
|
||||
{
|
||||
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
|
||||
|
||||
float centerX = (float)width/2.0f;
|
||||
float centerY = (float)height/2.0f;
|
||||
@ -993,7 +993,7 @@ Image GenImageGradientSquare(int width, int height, float density, Color inner,
|
||||
// Generate image: checked
|
||||
Image GenImageChecked(int width, int height, int checksX, int checksY, Color col1, Color col2)
|
||||
{
|
||||
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
@ -1019,7 +1019,7 @@ Image GenImageChecked(int width, int height, int checksX, int checksY, Color col
|
||||
// NOTE: It requires GetRandomValue(), defined in [rcore]
|
||||
Image GenImageWhiteNoise(int width, int height, float factor)
|
||||
{
|
||||
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
|
||||
|
||||
for (int i = 0; i < width*height; i++)
|
||||
{
|
||||
@ -1041,7 +1041,7 @@ Image GenImageWhiteNoise(int width, int height, float factor)
|
||||
// Generate image: perlin noise
|
||||
Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float scale)
|
||||
{
|
||||
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
|
||||
|
||||
float aspectRatio = (float)width/(float)height;
|
||||
|
||||
@ -1092,13 +1092,13 @@ Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float
|
||||
// Generate image: cellular algorithm. Bigger tileSize means bigger cells
|
||||
Image GenImageCellular(int width, int height, int tileSize)
|
||||
{
|
||||
Color *pixels = (Color *)RL_MALLOC(width*height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
|
||||
|
||||
int seedsPerRow = width/tileSize;
|
||||
int seedsPerCol = height/tileSize;
|
||||
int seedCount = seedsPerRow*seedsPerCol;
|
||||
|
||||
Vector2 *seeds = (Vector2 *)RL_MALLOC(seedCount*sizeof(Vector2));
|
||||
Vector2 *seeds = (Vector2 *)RL_CALLOC(seedCount, sizeof(Vector2));
|
||||
|
||||
for (int i = 0; i < seedCount; i++)
|
||||
{
|
||||
@ -1288,7 +1288,7 @@ void ImageCrop(Image *image, Rectangle crop)
|
||||
{
|
||||
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||
|
||||
unsigned char *croppedData = (unsigned char *)RL_MALLOC(cropWidth*cropHeight*bytesPerPixel);
|
||||
unsigned char *croppedData = (unsigned char *)RL_CALLOC(cropWidth*cropHeight*bytesPerPixel, 1);
|
||||
|
||||
// OPTION 1: Move cropped data line-by-line
|
||||
for (int y = cropY, offsetSize = 0; y < cropY + cropHeight; y++)
|
||||
@ -1336,7 +1336,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
{
|
||||
case PIXELFORMAT_UNCOMPRESSED_GRAYSCALE:
|
||||
{
|
||||
image->data = (unsigned char *)RL_MALLOC(image->width*image->height*sizeof(unsigned char));
|
||||
image->data = (unsigned char *)RL_CALLOC(image->width*image->height, sizeof(unsigned char));
|
||||
|
||||
for (int i = 0; i < image->width*image->height; i++)
|
||||
{
|
||||
@ -1346,7 +1346,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA:
|
||||
{
|
||||
image->data = (unsigned char *)RL_MALLOC(image->width*image->height*2*sizeof(unsigned char));
|
||||
image->data = (unsigned char *)RL_CALLOC(image->width*image->height*2, sizeof(unsigned char));
|
||||
|
||||
for (int i = 0, k = 0; i < image->width*image->height*2; i += 2, k++)
|
||||
{
|
||||
@ -1357,7 +1357,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R5G6B5:
|
||||
{
|
||||
image->data = (unsigned short *)RL_MALLOC(image->width*image->height*sizeof(unsigned short));
|
||||
image->data = (unsigned short *)RL_CALLOC(image->width*image->height, sizeof(unsigned short));
|
||||
|
||||
unsigned char r = 0;
|
||||
unsigned char g = 0;
|
||||
@ -1375,7 +1375,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R8G8B8:
|
||||
{
|
||||
image->data = (unsigned char *)RL_MALLOC(image->width*image->height*3*sizeof(unsigned char));
|
||||
image->data = (unsigned char *)RL_CALLOC(image->width*image->height*3, sizeof(unsigned char));
|
||||
|
||||
for (int i = 0, k = 0; i < image->width*image->height*3; i += 3, k++)
|
||||
{
|
||||
@ -1386,7 +1386,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R5G5B5A1:
|
||||
{
|
||||
image->data = (unsigned short *)RL_MALLOC(image->width*image->height*sizeof(unsigned short));
|
||||
image->data = (unsigned short *)RL_CALLOC(image->width*image->height, sizeof(unsigned short));
|
||||
|
||||
unsigned char r = 0;
|
||||
unsigned char g = 0;
|
||||
@ -1406,7 +1406,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R4G4B4A4:
|
||||
{
|
||||
image->data = (unsigned short *)RL_MALLOC(image->width*image->height*sizeof(unsigned short));
|
||||
image->data = (unsigned short *)RL_CALLOC(image->width*image->height, sizeof(unsigned short));
|
||||
|
||||
unsigned char r = 0;
|
||||
unsigned char g = 0;
|
||||
@ -1426,7 +1426,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R8G8B8A8:
|
||||
{
|
||||
image->data = (unsigned char *)RL_MALLOC(image->width*image->height*4*sizeof(unsigned char));
|
||||
image->data = (unsigned char *)RL_CALLOC(image->width*image->height*4, sizeof(unsigned char));
|
||||
|
||||
for (int i = 0, k = 0; i < image->width*image->height*4; i += 4, k++)
|
||||
{
|
||||
@ -1440,7 +1440,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
{
|
||||
// WARNING: Image is converted to GRAYSCALE equivalent 32bit
|
||||
|
||||
image->data = (float *)RL_MALLOC(image->width*image->height*sizeof(float));
|
||||
image->data = (float *)RL_CALLOC(image->width*image->height, sizeof(float));
|
||||
|
||||
for (int i = 0; i < image->width*image->height; i++)
|
||||
{
|
||||
@ -1449,7 +1449,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R32G32B32:
|
||||
{
|
||||
image->data = (float *)RL_MALLOC(image->width*image->height*3*sizeof(float));
|
||||
image->data = (float *)RL_CALLOC(image->width*image->height*3, sizeof(float));
|
||||
|
||||
for (int i = 0, k = 0; i < image->width*image->height*3; i += 3, k++)
|
||||
{
|
||||
@ -1460,7 +1460,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R32G32B32A32:
|
||||
{
|
||||
image->data = (float *)RL_MALLOC(image->width*image->height*4*sizeof(float));
|
||||
image->data = (float *)RL_CALLOC(image->width*image->height*4, sizeof(float));
|
||||
|
||||
for (int i = 0, k = 0; i < image->width*image->height*4; i += 4, k++)
|
||||
{
|
||||
@ -1474,7 +1474,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
{
|
||||
// WARNING: Image is converted to GRAYSCALE equivalent 16bit
|
||||
|
||||
image->data = (unsigned short *)RL_MALLOC(image->width*image->height*sizeof(unsigned short));
|
||||
image->data = (unsigned short *)RL_CALLOC(image->width*image->height, sizeof(unsigned short));
|
||||
|
||||
for (int i = 0; i < image->width*image->height; i++)
|
||||
{
|
||||
@ -1483,7 +1483,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R16G16B16:
|
||||
{
|
||||
image->data = (unsigned short *)RL_MALLOC(image->width*image->height*3*sizeof(unsigned short));
|
||||
image->data = (unsigned short *)RL_CALLOC(image->width*image->height*3, sizeof(unsigned short));
|
||||
|
||||
for (int i = 0, k = 0; i < image->width*image->height*3; i += 3, k++)
|
||||
{
|
||||
@ -1494,7 +1494,7 @@ void ImageFormat(Image *image, int newFormat)
|
||||
} break;
|
||||
case PIXELFORMAT_UNCOMPRESSED_R16G16B16A16:
|
||||
{
|
||||
image->data = (unsigned short *)RL_MALLOC(image->width*image->height*4*sizeof(unsigned short));
|
||||
image->data = (unsigned short *)RL_CALLOC(image->width*image->height*4, sizeof(unsigned short));
|
||||
|
||||
for (int i = 0, k = 0; i < image->width*image->height*4; i += 4, k++)
|
||||
{
|
||||
@ -1790,7 +1790,7 @@ void ImageResizeNN(Image *image, int newWidth, int newHeight)
|
||||
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
|
||||
|
||||
Color *pixels = LoadImageColors(*image);
|
||||
Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color));
|
||||
Color *output = (Color *)RL_CALLOC(newWidth*newHeight, sizeof(Color));
|
||||
|
||||
// EDIT: added +1 to account for an early rounding problem
|
||||
int xRatio = ((image->width << 16)/newWidth) + 1;
|
||||
@ -1840,7 +1840,7 @@ void ImageResize(Image *image, int newWidth, int newHeight)
|
||||
(image->format == PIXELFORMAT_UNCOMPRESSED_R8G8B8A8))
|
||||
{
|
||||
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||
unsigned char *output = (unsigned char *)RL_MALLOC(newWidth*newHeight*bytesPerPixel);
|
||||
unsigned char *output = (unsigned char *)RL_CALLOC(newWidth*newHeight*bytesPerPixel, 1);
|
||||
|
||||
switch (image->format)
|
||||
{
|
||||
@ -1860,7 +1860,7 @@ void ImageResize(Image *image, int newWidth, int newHeight)
|
||||
{
|
||||
// Get data as Color pixels array to work with it
|
||||
Color *pixels = LoadImageColors(*image);
|
||||
Color *output = (Color *)RL_MALLOC(newWidth*newHeight*sizeof(Color));
|
||||
Color *output = (Color *)RL_CALLOC(newWidth*newHeight, sizeof(Color));
|
||||
|
||||
// NOTE: Color data is cast to (unsigned char *), there shouldn't been any problem...
|
||||
stbir_resize_uint8_linear((unsigned char *)pixels, image->width, image->height, 0, (unsigned char *)output, newWidth, newHeight, 0, (stbir_pixel_layout)4);
|
||||
@ -2101,7 +2101,7 @@ void ImageAlphaMask(Image *image, Image alphaMask)
|
||||
// In case image is only grayscale, add alpha channel
|
||||
if (image->format == PIXELFORMAT_UNCOMPRESSED_GRAYSCALE)
|
||||
{
|
||||
unsigned char *data = (unsigned char *)RL_MALLOC(image->width*image->height*2);
|
||||
unsigned char *data = (unsigned char *)RL_CALLOC(image->width*image->height*2, 1);
|
||||
|
||||
// Apply alpha mask to alpha channel
|
||||
for (int i = 0, k = 0; (i < mask.width*mask.height) || (i < image->width*image->height); i++, k += 2)
|
||||
@ -2176,8 +2176,8 @@ void ImageBlurGaussian(Image *image, int blurSize)
|
||||
Color *pixels = LoadImageColors(*image);
|
||||
|
||||
// Loop switches between pixelsCopy1 and pixelsCopy2
|
||||
Vector4 *pixelsCopy1 = (Vector4 *)RL_MALLOC((image->height)*(image->width)*sizeof(Vector4));
|
||||
Vector4 *pixelsCopy2 = (Vector4 *)RL_MALLOC((image->height)*(image->width)*sizeof(Vector4));
|
||||
Vector4 *pixelsCopy1 = (Vector4 *)RL_CALLOC(image->height*image->width, sizeof(Vector4));
|
||||
Vector4 *pixelsCopy2 = (Vector4 *)RL_CALLOC(image->height*image->width, sizeof(Vector4));
|
||||
|
||||
for (int i = 0; i < (image->height*image->width); i++)
|
||||
{
|
||||
@ -2324,7 +2324,7 @@ void ImageKernelConvolution(Image *image, const float *kernel, int kernelSize)
|
||||
}
|
||||
|
||||
Color *pixels = LoadImageColors(*image);
|
||||
Color *result = (Color *)RL_MALLOC((image->height)*(image->width)*sizeof(Color));
|
||||
Color *result = (Color *)RL_CALLOC((image->height)*(image->width), sizeof(Color));
|
||||
|
||||
int startRange = 0, endRange = 0;
|
||||
|
||||
@ -2511,7 +2511,7 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp)
|
||||
}
|
||||
|
||||
// NOTE: Storing the dithered data as unsigned short (16bpp)
|
||||
image->data = (unsigned short *)RL_MALLOC(image->width*image->height*sizeof(unsigned short));
|
||||
image->data = (unsigned short *)RL_CALLOC(image->width*image->height, sizeof(unsigned short));
|
||||
|
||||
Color oldPixel = WHITE;
|
||||
Color newPixel = WHITE;
|
||||
@ -2599,7 +2599,7 @@ void ImageFlipVertical(Image *image)
|
||||
else
|
||||
{
|
||||
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||
unsigned char *flippedData = (unsigned char *)RL_MALLOC(image->width*image->height*bytesPerPixel);
|
||||
unsigned char *flippedData = (unsigned char *)RL_CALLOC(image->width*image->height*bytesPerPixel, 1);
|
||||
|
||||
for (int i = (image->height - 1), offsetSize = 0; i >= 0; i--)
|
||||
{
|
||||
@ -2623,7 +2623,7 @@ void ImageFlipHorizontal(Image *image)
|
||||
else
|
||||
{
|
||||
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||
unsigned char *flippedData = (unsigned char *)RL_MALLOC(image->width*image->height*bytesPerPixel);
|
||||
unsigned char *flippedData = (unsigned char *)RL_CALLOC(image->width*image->height*bytesPerPixel, 1);
|
||||
|
||||
for (int y = 0; y < image->height; y++)
|
||||
{
|
||||
@ -2684,18 +2684,20 @@ void ImageRotate(Image *image, int degrees)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
float oldX = ((x - width/2.0f)*cosRadius + (y - height/2.0f)*sinRadius) + image->width/2.0f;
|
||||
float oldY = ((y - height/2.0f)*cosRadius - (x - width/2.0f)*sinRadius) + image->height/2.0f;
|
||||
float oldX = ((x + 0.5f - width/2.0f)*cosRadius + (y + 0.5f - height/2.0f)*sinRadius) + image->width/2.0f - 0.5f;
|
||||
float oldY = ((y + 0.5f - height/2.0f)*cosRadius - (x + 0.5f - width/2.0f)*sinRadius) + image->height/2.0f - 0.5f;
|
||||
|
||||
if ((oldX >= 0) && (oldX < image->width) && (oldY >= 0) && (oldY < image->height))
|
||||
if ((oldX >= -0.5f) && (oldX < image->width - 0.5f) && (oldY >= -0.5f) && (oldY < image->height - 0.5f))
|
||||
{
|
||||
int x1 = (int)floorf(oldX);
|
||||
int y1 = (int)floorf(oldY);
|
||||
float sampleX = (oldX < 0)? 0 : oldX;
|
||||
float sampleY = (oldY < 0)? 0 : oldY;
|
||||
int x1 = (int)floorf(sampleX);
|
||||
int y1 = (int)floorf(sampleY);
|
||||
int x2 = MIN(x1 + 1, image->width - 1);
|
||||
int y2 = MIN(y1 + 1, image->height - 1);
|
||||
|
||||
float px = oldX - x1;
|
||||
float py = oldY - y1;
|
||||
float px = sampleX - x1;
|
||||
float py = sampleY - y1;
|
||||
|
||||
for (int i = 0; i < bytesPerPixel; i++)
|
||||
{
|
||||
@ -2706,7 +2708,7 @@ void ImageRotate(Image *image, int degrees)
|
||||
|
||||
float val = f1*(1 - px)*(1 - py) + f2*px*(1 - py) + f3*(1 - px)*py + f4*px*py;
|
||||
|
||||
rotatedData[(y*width + x)*bytesPerPixel + i] = (unsigned char)val;
|
||||
rotatedData[(y*width + x)*bytesPerPixel + i] = (unsigned char)(val + 0.5f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2731,7 +2733,7 @@ void ImageRotateCW(Image *image)
|
||||
else
|
||||
{
|
||||
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||
unsigned char *rotatedData = (unsigned char *)RL_MALLOC(image->width*image->height*bytesPerPixel);
|
||||
unsigned char *rotatedData = (unsigned char *)RL_CALLOC(image->width*image->height*bytesPerPixel, 1);
|
||||
|
||||
for (int y = 0; y < image->height; y++)
|
||||
{
|
||||
@ -2763,7 +2765,7 @@ void ImageRotateCCW(Image *image)
|
||||
else
|
||||
{
|
||||
int bytesPerPixel = GetPixelDataSize(1, 1, image->format);
|
||||
unsigned char *rotatedData = (unsigned char *)RL_MALLOC(image->width*image->height*bytesPerPixel);
|
||||
unsigned char *rotatedData = (unsigned char *)RL_CALLOC(image->width*image->height*bytesPerPixel, 1);
|
||||
|
||||
for (int y = 0; y < image->height; y++)
|
||||
{
|
||||
@ -2986,7 +2988,7 @@ Color *LoadImageColors(Image image)
|
||||
{
|
||||
if ((image.width == 0) || (image.height == 0)) return NULL; // Security check
|
||||
|
||||
Color *pixels = (Color *)RL_MALLOC(image.width*image.height*sizeof(Color));
|
||||
Color *pixels = (Color *)RL_CALLOC(image.width*image.height, sizeof(Color));
|
||||
|
||||
if (image.format >= PIXELFORMAT_COMPRESSED_DXT1_RGB) TRACELOG(LOG_WARNING, "IMAGE: Pixel data retrieval not supported for compressed image formats");
|
||||
else
|
||||
@ -3142,7 +3144,7 @@ Color *LoadImagePalette(Image image, int maxPaletteSize, int *colorCount)
|
||||
|
||||
if (pixels != NULL)
|
||||
{
|
||||
palette = (Color *)RL_MALLOC(maxPaletteSize*sizeof(Color));
|
||||
palette = (Color *)RL_CALLOC(maxPaletteSize, sizeof(Color));
|
||||
|
||||
for (int i = 0; i < maxPaletteSize; i++) palette[i] = BLANK; // Set all colors to BLANK
|
||||
|
||||
@ -5830,7 +5832,7 @@ static unsigned short FloatToHalf(float x)
|
||||
// Get pixel data from image as Vector4 array (float normalized)
|
||||
static Vector4 *LoadImageDataNormalized(Image image)
|
||||
{
|
||||
Vector4 *pixels = (Vector4 *)RL_MALLOC(image.width*image.height*sizeof(Vector4));
|
||||
Vector4 *pixels = (Vector4 *)RL_CALLOC(image.width*image.height, sizeof(Vector4));
|
||||
|
||||
if (image.format >= PIXELFORMAT_COMPRESSED_DXT1_RGB) TRACELOG(LOG_WARNING, "IMAGE: Pixel data retrieval not supported for compressed image formats");
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user