Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
26003f3cb7
11 changed files with 4922 additions and 37420 deletions
569
src/core.c
569
src/core.c
|
|
@ -255,6 +255,7 @@
|
|||
#include "EGL/egl.h" // Khronos EGL library - Native platform display device control functions
|
||||
#include "EGL/eglext.h" // Khronos EGL library - Extensions
|
||||
#include "GLES2/gl2.h" // Khronos OpenGL ES 2.0 library
|
||||
#include "uwp_events.h" // UWP bootstrapping functions
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
|
|
@ -328,10 +329,6 @@ typedef struct {
|
|||
typedef struct { int x; int y; } Point;
|
||||
typedef struct { unsigned int width; unsigned int height; } Size;
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
extern EGLNativeWindowType handle; // Native window handler for UWP (external, defined in UWP App)
|
||||
#endif
|
||||
|
||||
// Core global state context data
|
||||
typedef struct CoreData {
|
||||
struct {
|
||||
|
|
@ -352,6 +349,7 @@ typedef struct CoreData {
|
|||
const char *title; // Window text title const pointer
|
||||
bool ready; // Flag to check if window has been initialized successfully
|
||||
bool minimized; // Flag to check if window has been minimized
|
||||
bool focused; // Flag to check if window has been focused
|
||||
bool resized; // Flag to check if window has been resized
|
||||
bool fullscreen; // Flag to check if fullscreen mode required
|
||||
bool alwaysRun; // Flag to keep window update/draw running on minimized
|
||||
|
|
@ -377,6 +375,11 @@ typedef struct CoreData {
|
|||
const char *internalDataPath; // Android internal data path to write data (/data/data/<package>/files)
|
||||
bool contextRebindRequired; // Used to know context rebind required
|
||||
} Android;
|
||||
#endif
|
||||
#if defined(PLATFORM_UWP)
|
||||
struct {
|
||||
const char* internalDataPath; // UWP App data path
|
||||
} UWP;
|
||||
#endif
|
||||
struct {
|
||||
#if defined(PLATFORM_RPI)
|
||||
|
|
@ -498,6 +501,7 @@ static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset);
|
|||
static void CursorEnterCallback(GLFWwindow *window, int enter); // GLFW3 Cursor Enter Callback, cursor enters client area
|
||||
static void WindowSizeCallback(GLFWwindow *window, int width, int height); // GLFW3 WindowSize Callback, runs when window is resized
|
||||
static void WindowIconifyCallback(GLFWwindow *window, int iconified); // GLFW3 WindowIconify Callback, runs when window is minimized/restored
|
||||
static void WindowFocusCallback(GLFWwindow *window, int focused); // GLFW3 WindowFocus Callback, runs when window get/lose focus
|
||||
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths); // GLFW3 Window Drop Callback, runs when drop files into window
|
||||
#endif
|
||||
|
||||
|
|
@ -608,6 +612,14 @@ static void RestoreTerminal(void)
|
|||
// NOTE: data parameter could be used to pass any kind of required data to the initialization
|
||||
void InitWindow(int width, int height, const char *title)
|
||||
{
|
||||
#if defined(PLATFORM_UWP)
|
||||
if (!UWPIsConfigured())
|
||||
{
|
||||
TRACELOG(LOG_ERROR, "UWP Functions have not been set yet, please set these before initializing raylib!");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
TRACELOG(LOG_INFO, "Initializing raylib %s", RAYLIB_VERSION);
|
||||
|
||||
CORE.Window.title = title;
|
||||
|
|
@ -617,6 +629,11 @@ void InitWindow(int width, int height, const char *title)
|
|||
CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f };
|
||||
CORE.Input.Gamepad.lastButtonPressed = -1;
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
// The axis count is 6 (2 thumbsticks and left and right trigger)
|
||||
CORE.Input.Gamepad.axisCount = 6;
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
CORE.Window.screen.width = width;
|
||||
CORE.Window.screen.height = height;
|
||||
|
|
@ -679,7 +696,9 @@ void InitWindow(int width, int height, const char *title)
|
|||
// Init graphics device (display device and OpenGL context)
|
||||
// NOTE: returns true if window and graphic device has been initialized successfully
|
||||
CORE.Window.ready = InitGraphicsDevice(width, height);
|
||||
|
||||
if (!CORE.Window.ready) return;
|
||||
else CORE.Window.focused = true;
|
||||
|
||||
// Init hi-res timer
|
||||
InitTimer();
|
||||
|
|
@ -756,7 +775,7 @@ void CloseWindow(void)
|
|||
glfwTerminate();
|
||||
#endif
|
||||
|
||||
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32)
|
||||
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32) && !defined(PLATFORM_UWP)
|
||||
timeEndPeriod(1); // Restore time period
|
||||
#endif
|
||||
|
||||
|
|
@ -842,7 +861,7 @@ bool WindowShouldClose(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
// Check if window has been minimized (or lost focus)
|
||||
// Check if window has been minimized
|
||||
bool IsWindowMinimized(void)
|
||||
{
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
|
||||
|
|
@ -852,6 +871,16 @@ bool IsWindowMinimized(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
// Check if window has the focus
|
||||
bool IsWindowFocused(void)
|
||||
{
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
|
||||
return CORE.Window.focused;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Check if window has been resized
|
||||
bool IsWindowResized(void)
|
||||
{
|
||||
|
|
@ -1138,6 +1167,21 @@ Vector2 GetWindowPosition(void)
|
|||
return (Vector2){ (float)x, (float)y };
|
||||
}
|
||||
|
||||
// Get window scale DPI factor
|
||||
Vector2 GetWindowScaleDPI(void)
|
||||
{
|
||||
Vector2 scale = { 1.0f, 1.0f };
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
|
||||
|
||||
if (monitor != NULL) glfwGetMonitorContentScale(monitor, &scale.x, &scale.y);
|
||||
else TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor");
|
||||
#endif
|
||||
|
||||
return scale;
|
||||
}
|
||||
|
||||
// Get the human-readable, UTF-8 encoded name of the primary monitor
|
||||
const char *GetMonitorName(int monitor)
|
||||
{
|
||||
|
|
@ -1180,9 +1224,7 @@ void ShowCursor(void)
|
|||
glfwSetInputMode(CORE.Window.handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
#endif
|
||||
#if defined(PLATFORM_UWP)
|
||||
UWPMessage *msg = CreateUWPMessage();
|
||||
msg->type = UWP_MSG_SHOW_MOUSE;
|
||||
SendMessageToUWP(msg);
|
||||
UWPGetMouseShowFunc()();
|
||||
#endif
|
||||
CORE.Input.Mouse.cursorHidden = false;
|
||||
}
|
||||
|
|
@ -1194,9 +1236,7 @@ void HideCursor(void)
|
|||
glfwSetInputMode(CORE.Window.handle, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
||||
#endif
|
||||
#if defined(PLATFORM_UWP)
|
||||
UWPMessage *msg = CreateUWPMessage();
|
||||
msg->type = UWP_MSG_HIDE_MOUSE;
|
||||
SendMessageToUWP(msg);
|
||||
UWPGetMouseHideFunc()();
|
||||
#endif
|
||||
CORE.Input.Mouse.cursorHidden = true;
|
||||
}
|
||||
|
|
@ -1217,9 +1257,7 @@ void EnableCursor(void)
|
|||
CORE.Input.Mouse.cursorLockRequired = true;
|
||||
#endif
|
||||
#if defined(PLATFORM_UWP)
|
||||
UWPMessage *msg = CreateUWPMessage();
|
||||
msg->type = UWP_MSG_LOCK_MOUSE;
|
||||
SendMessageToUWP(msg);
|
||||
UWPGetMouseUnlockFunc()();
|
||||
#endif
|
||||
CORE.Input.Mouse.cursorHidden = false;
|
||||
}
|
||||
|
|
@ -1234,9 +1272,7 @@ void DisableCursor(void)
|
|||
CORE.Input.Mouse.cursorLockRequired = true;
|
||||
#endif
|
||||
#if defined(PLATFORM_UWP)
|
||||
UWPMessage *msg = CreateUWPMessage();
|
||||
msg->type = UWP_MSG_UNLOCK_MOUSE;
|
||||
SendMessageToUWP(msg);
|
||||
UWPGetMouseLockFunc()();
|
||||
#endif
|
||||
CORE.Input.Mouse.cursorHidden = true;
|
||||
}
|
||||
|
|
@ -1321,10 +1357,6 @@ void EndDrawing(void)
|
|||
CORE.Time.previous = CORE.Time.current;
|
||||
|
||||
CORE.Time.frame += waitTime; // Total frame time: update + draw + wait
|
||||
|
||||
//SetWindowTitle(FormatText("Update: %f, Draw: %f, Req.Wait: %f, Real.Wait: %f, Total: %f, Target: %f\n",
|
||||
// (float)CORE.Time.update, (float)CORE.Time.draw, (float)(CORE.Time.target - (CORE.Time.update + CORE.Time.draw)),
|
||||
// (float)waitTime, (float)CORE.Time.frame, (float)CORE.Time.target));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1684,8 +1716,7 @@ double GetTime(void)
|
|||
#endif
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
// Updated through messages
|
||||
return CORE.Time.current;
|
||||
return UWPGetQueryTimeFunc()();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
@ -1867,6 +1898,10 @@ void TakeScreenshot(const char *fileName)
|
|||
strcpy(path, CORE.Android.internalDataPath);
|
||||
strcat(path, "/");
|
||||
strcat(path, fileName);
|
||||
#elif defined(PLATFORM_UWP)
|
||||
strcpy(path, CORE.UWP.internalDataPath);
|
||||
strcat(path, "/");
|
||||
strcat(path, fileName);
|
||||
#else
|
||||
strcpy(path, fileName);
|
||||
#endif
|
||||
|
|
@ -2200,6 +2235,10 @@ void SaveStorageValue(unsigned int position, int value)
|
|||
strcpy(path, CORE.Android.internalDataPath);
|
||||
strcat(path, "/");
|
||||
strcat(path, STORAGE_DATA_FILE);
|
||||
#elif defined(PLATFORM_UWP)
|
||||
strcpy(path, CORE.UWP.internalDataPath);
|
||||
strcat(path, "/");
|
||||
strcat(path, STORAGE_DATA_FILE);
|
||||
#else
|
||||
strcpy(path, STORAGE_DATA_FILE);
|
||||
#endif
|
||||
|
|
@ -2273,6 +2312,10 @@ int LoadStorageValue(unsigned int position)
|
|||
strcpy(path, CORE.Android.internalDataPath);
|
||||
strcat(path, "/");
|
||||
strcat(path, STORAGE_DATA_FILE);
|
||||
#elif defined(PLATFORM_UWP)
|
||||
strcpy(path, CORE.UWP.internalDataPath);
|
||||
strcat(path, "/");
|
||||
strcat(path, STORAGE_DATA_FILE);
|
||||
#else
|
||||
strcpy(path, STORAGE_DATA_FILE);
|
||||
#endif
|
||||
|
|
@ -2613,11 +2656,7 @@ void SetMousePosition(int x, int y)
|
|||
glfwSetCursorPos(CORE.Window.handle, CORE.Input.Mouse.position.x, CORE.Input.Mouse.position.y);
|
||||
#endif
|
||||
#if defined(PLATFORM_UWP)
|
||||
UWPMessage *msg = CreateUWPMessage();
|
||||
msg->type = UWP_MSG_SET_MOUSE_LOCATION;
|
||||
msg->paramVector0.x = CORE.Input.Mouse.position.x;
|
||||
msg->paramVector0.y = CORE.Input.Mouse.position.y;
|
||||
SendMessageToUWP(msg);
|
||||
UWPGetMouseSetPosFunc()(x, y);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
@ -2650,7 +2689,7 @@ int GetMouseWheelMove(void)
|
|||
// Returns touch position X for touch point 0 (relative to screen size)
|
||||
int GetTouchX(void)
|
||||
{
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
|
||||
return (int)CORE.Input.Touch.position[0].x;
|
||||
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
||||
return GetMouseX();
|
||||
|
|
@ -2660,7 +2699,7 @@ int GetTouchX(void)
|
|||
// Returns touch position Y for touch point 0 (relative to screen size)
|
||||
int GetTouchY(void)
|
||||
{
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) || defined(PLATFORM_UWP)
|
||||
return (int)CORE.Input.Touch.position[0].y;
|
||||
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
||||
return GetMouseY();
|
||||
|
|
@ -2673,7 +2712,7 @@ Vector2 GetTouchPosition(int index)
|
|||
{
|
||||
Vector2 position = { -1.0f, -1.0f };
|
||||
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) || defined(PLATFORM_RPI)
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB) || defined(PLATFORM_RPI) || defined(PLATFORM_UWP)
|
||||
if (index < MAX_TOUCH_POINTS) position = CORE.Input.Touch.position[index];
|
||||
else TRACELOG(LOG_WARNING, "INPUT: Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS);
|
||||
|
||||
|
|
@ -2923,6 +2962,7 @@ static bool InitGraphicsDevice(int width, int height)
|
|||
glfwSetCharCallback(CORE.Window.handle, CharCallback);
|
||||
glfwSetScrollCallback(CORE.Window.handle, ScrollCallback);
|
||||
glfwSetWindowIconifyCallback(CORE.Window.handle, WindowIconifyCallback);
|
||||
glfwSetWindowFocusCallback(CORE.Window.handle, WindowFocusCallback);
|
||||
glfwSetDropCallback(CORE.Window.handle, WindowDropCallback);
|
||||
|
||||
glfwMakeContextCurrent(CORE.Window.handle);
|
||||
|
|
@ -3134,7 +3174,7 @@ static bool InitGraphicsDevice(int width, int height)
|
|||
//https://stackoverflow.com/questions/46550182/how-to-create-eglsurface-using-c-winrt-and-angle
|
||||
|
||||
//CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, reinterpret_cast<IInspectable*>(surfaceCreationProperties), surfaceAttributes);
|
||||
CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, handle, surfaceAttributes);
|
||||
CORE.Window.surface = eglCreateWindowSurface(CORE.Window.device, CORE.Window.config, (EGLNativeWindowType) UWPGetCoreWindowPtr(), surfaceAttributes);
|
||||
if (CORE.Window.surface == EGL_NO_SURFACE)
|
||||
{
|
||||
TRACELOG(LOG_WARNING, "DISPLAY: Failed to create EGL fullscreen surface");
|
||||
|
|
@ -3152,6 +3192,9 @@ static bool InitGraphicsDevice(int width, int height)
|
|||
eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_WIDTH, &CORE.Window.screen.width);
|
||||
eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_HEIGHT, &CORE.Window.screen.height);
|
||||
|
||||
// Get display size
|
||||
UWPGetDisplaySizeFunc()(&CORE.Window.display.width, &CORE.Window.display.height);
|
||||
|
||||
#endif // PLATFORM_UWP
|
||||
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
||||
|
|
@ -3263,8 +3306,13 @@ static bool InitGraphicsDevice(int width, int height)
|
|||
else
|
||||
{
|
||||
// Grab the width and height of the surface
|
||||
//eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_WIDTH, &CORE.Window.render.width);
|
||||
//eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_HEIGHT, &CORE.Window.render.height);
|
||||
#if defined(PLATFORM_UWP)
|
||||
CORE.Window.render.width = CORE.Window.screen.width;
|
||||
CORE.Window.render.height = CORE.Window.screen.height;
|
||||
#else
|
||||
CORE.Window.render.width = CORE.Window.display.width;
|
||||
CORE.Window.render.height = CORE.Window.display.height;
|
||||
#endif
|
||||
|
||||
TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully");
|
||||
TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
|
||||
|
|
@ -3299,7 +3347,7 @@ static bool InitGraphicsDevice(int width, int height)
|
|||
|
||||
ClearBackground(RAYWHITE); // Default background color for raylib games :P
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_UWP)
|
||||
CORE.Window.ready = true;
|
||||
#endif
|
||||
return true;
|
||||
|
|
@ -3404,7 +3452,7 @@ static void InitTimer(void)
|
|||
{
|
||||
srand((unsigned int)time(NULL)); // Initialize random seed
|
||||
|
||||
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32)
|
||||
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32) && !defined(PLATFORM_UWP)
|
||||
timeBeginPeriod(1); // Setup high-resolution timer to 1ms (granularity of 1-2 ms)
|
||||
#endif
|
||||
|
||||
|
|
@ -3428,7 +3476,9 @@ static void InitTimer(void)
|
|||
// Ref: http://www.geisswerks.com/ryan/FAQS/timing.html --> All about timming on Win32!
|
||||
static void Wait(float ms)
|
||||
{
|
||||
#if defined(SUPPORT_BUSY_WAIT_LOOP) && !defined(PLATFORM_UWP)
|
||||
#if defined(PLATFORM_UWP)
|
||||
UWPGetSleepFunc()(ms / 1000);
|
||||
#elif defined(SUPPORT_BUSY_WAIT_LOOP)
|
||||
double prevTime = GetTime();
|
||||
double nextTime = 0.0;
|
||||
|
||||
|
|
@ -3456,7 +3506,7 @@ static void Wait(float ms)
|
|||
usleep(ms*1000.0f);
|
||||
#endif
|
||||
|
||||
#if defined(SUPPORT_HALFBUSY_WAIT_LOOP)
|
||||
#if defined(SUPPORT_HALFBUSY_WAIT_LOOP)// && !defined(PLATFORM_UWP)
|
||||
while (GetTime() < destTime) { }
|
||||
#endif
|
||||
#endif
|
||||
|
|
@ -3613,142 +3663,6 @@ static void PollInputEvents(void)
|
|||
CORE.Input.Mouse.currentWheelMove = 0;
|
||||
|
||||
for (int i = 0; i < 3; i++) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i];
|
||||
|
||||
// Loop over pending messages
|
||||
while (HasMessageFromUWP())
|
||||
{
|
||||
UWPMessage *msg = GetMessageFromUWP();
|
||||
|
||||
switch (msg->type)
|
||||
{
|
||||
case UWP_MSG_REGISTER_KEY:
|
||||
{
|
||||
// Convert from virtualKey
|
||||
int actualKey = -1;
|
||||
|
||||
switch (msg->paramInt0)
|
||||
{
|
||||
case 0x08: actualKey = KEY_BACKSPACE; break;
|
||||
case 0x20: actualKey = KEY_SPACE; break;
|
||||
case 0x1B: actualKey = KEY_ESCAPE; break;
|
||||
case 0x0D: actualKey = KEY_ENTER; break;
|
||||
case 0x2E: actualKey = KEY_DELETE; break;
|
||||
case 0x27: actualKey = KEY_RIGHT; break;
|
||||
case 0x25: actualKey = KEY_LEFT; break;
|
||||
case 0x28: actualKey = KEY_DOWN; break;
|
||||
case 0x26: actualKey = KEY_UP; break;
|
||||
case 0x70: actualKey = KEY_F1; break;
|
||||
case 0x71: actualKey = KEY_F2; break;
|
||||
case 0x72: actualKey = KEY_F3; break;
|
||||
case 0x73: actualKey = KEY_F4; break;
|
||||
case 0x74: actualKey = KEY_F5; break;
|
||||
case 0x75: actualKey = KEY_F6; break;
|
||||
case 0x76: actualKey = KEY_F7; break;
|
||||
case 0x77: actualKey = KEY_F8; break;
|
||||
case 0x78: actualKey = KEY_F9; break;
|
||||
case 0x79: actualKey = KEY_F10; break;
|
||||
case 0x7A: actualKey = KEY_F11; break;
|
||||
case 0x7B: actualKey = KEY_F12; break;
|
||||
case 0xA0: actualKey = KEY_LEFT_SHIFT; break;
|
||||
case 0xA2: actualKey = KEY_LEFT_CONTROL; break;
|
||||
case 0xA4: actualKey = KEY_LEFT_ALT; break;
|
||||
case 0xA1: actualKey = KEY_RIGHT_SHIFT; break;
|
||||
case 0xA3: actualKey = KEY_RIGHT_CONTROL; break;
|
||||
case 0xA5: actualKey = KEY_RIGHT_ALT; break;
|
||||
case 0x30: actualKey = KEY_ZERO; break;
|
||||
case 0x31: actualKey = KEY_ONE; break;
|
||||
case 0x32: actualKey = KEY_TWO; break;
|
||||
case 0x33: actualKey = KEY_THREE; break;
|
||||
case 0x34: actualKey = KEY_FOUR; break;
|
||||
case 0x35: actualKey = KEY_FIVE; break;
|
||||
case 0x36: actualKey = KEY_SIX; break;
|
||||
case 0x37: actualKey = KEY_SEVEN; break;
|
||||
case 0x38: actualKey = KEY_EIGHT; break;
|
||||
case 0x39: actualKey = KEY_NINE; break;
|
||||
case 0x41: actualKey = KEY_A; break;
|
||||
case 0x42: actualKey = KEY_B; break;
|
||||
case 0x43: actualKey = KEY_C; break;
|
||||
case 0x44: actualKey = KEY_D; break;
|
||||
case 0x45: actualKey = KEY_E; break;
|
||||
case 0x46: actualKey = KEY_F; break;
|
||||
case 0x47: actualKey = KEY_G; break;
|
||||
case 0x48: actualKey = KEY_H; break;
|
||||
case 0x49: actualKey = KEY_I; break;
|
||||
case 0x4A: actualKey = KEY_J; break;
|
||||
case 0x4B: actualKey = KEY_K; break;
|
||||
case 0x4C: actualKey = KEY_L; break;
|
||||
case 0x4D: actualKey = KEY_M; break;
|
||||
case 0x4E: actualKey = KEY_N; break;
|
||||
case 0x4F: actualKey = KEY_O; break;
|
||||
case 0x50: actualKey = KEY_P; break;
|
||||
case 0x51: actualKey = KEY_Q; break;
|
||||
case 0x52: actualKey = KEY_R; break;
|
||||
case 0x53: actualKey = KEY_S; break;
|
||||
case 0x54: actualKey = KEY_T; break;
|
||||
case 0x55: actualKey = KEY_U; break;
|
||||
case 0x56: actualKey = KEY_V; break;
|
||||
case 0x57: actualKey = KEY_W; break;
|
||||
case 0x58: actualKey = KEY_X; break;
|
||||
case 0x59: actualKey = KEY_Y; break;
|
||||
case 0x5A: actualKey = KEY_Z; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
if (actualKey > -1) CORE.Input.Keyboard.currentKeyState[actualKey] = msg->paramChar0;
|
||||
|
||||
} break;
|
||||
case UWP_MSG_REGISTER_CLICK: CORE.Input.Mouse.currentButtonState[msg->paramInt0] = msg->paramChar0; break;
|
||||
case UWP_MSG_SCROLL_WHEEL_UPDATE: CORE.Input.Mouse.currentWheelMove += msg->paramInt0; break;
|
||||
case UWP_MSG_UPDATE_MOUSE_LOCATION: CORE.Input.Mouse.position = msg->paramVector0; break;
|
||||
case UWP_MSG_SET_GAMEPAD_ACTIVE: if (msg->paramInt0 < MAX_GAMEPADS) CORE.Input.Gamepad.ready[msg->paramInt0] = msg->paramBool0; break;
|
||||
case UWP_MSG_SET_GAMEPAD_BUTTON:
|
||||
{
|
||||
if ((msg->paramInt0 < MAX_GAMEPADS) && (msg->paramInt1 < MAX_GAMEPAD_BUTTONS)) CORE.Input.Gamepad.currentState[msg->paramInt0][msg->paramInt1] = msg->paramChar0;
|
||||
} break;
|
||||
case UWP_MSG_SET_GAMEPAD_AXIS:
|
||||
{
|
||||
if ((msg->paramInt0 < MAX_GAMEPADS) && (msg->paramInt1 < MAX_GAMEPAD_AXIS)) CORE.Input.Gamepad.axisState[msg->paramInt0][msg->paramInt1] = msg->paramFloat0;
|
||||
|
||||
// Register buttons for 2nd triggers
|
||||
CORE.Input.Gamepad.currentState[msg->paramInt0][GAMEPAD_BUTTON_LEFT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[msg->paramInt0][GAMEPAD_AXIS_LEFT_TRIGGER] > 0.1);
|
||||
CORE.Input.Gamepad.currentState[msg->paramInt0][GAMEPAD_BUTTON_RIGHT_TRIGGER_2] = (char)(CORE.Input.Gamepad.axisState[msg->paramInt0][GAMEPAD_AXIS_RIGHT_TRIGGER] > 0.1);
|
||||
} break;
|
||||
case UWP_MSG_SET_DISPLAY_DIMS:
|
||||
{
|
||||
CORE.Window.display.width = msg->paramVector0.x;
|
||||
CORE.Window.display.height = msg->paramVector0.y;
|
||||
} break;
|
||||
case UWP_MSG_HANDLE_RESIZE:
|
||||
{
|
||||
eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_WIDTH, &CORE.Window.screen.width);
|
||||
eglQuerySurface(CORE.Window.device, CORE.Window.surface, EGL_HEIGHT, &CORE.Window.screen.height);
|
||||
|
||||
// If window is resized, viewport and projection matrix needs to be re-calculated
|
||||
rlViewport(0, 0, CORE.Window.screen.width, CORE.Window.screen.height); // Set viewport width and height
|
||||
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
|
||||
rlLoadIdentity(); // Reset current matrix (projection)
|
||||
rlOrtho(0, CORE.Window.screen.width, CORE.Window.screen.height, 0, 0.0f, 1.0f); // Orthographic projection mode with top-left corner at (0,0)
|
||||
rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
|
||||
rlLoadIdentity(); // Reset current matrix (modelview)
|
||||
rlClearScreenBuffers(); // Clear screen buffers (color and depth)
|
||||
|
||||
// Window size must be updated to be used on 3D mode to get new aspect ratio (BeginMode3D())
|
||||
// NOTE: Be careful! GLFW3 will choose the closest fullscreen resolution supported by current monitor,
|
||||
// for example, if reescaling back to 800x450 (desired), it could set 720x480 (closest fullscreen supported)
|
||||
CORE.Window.currentFbo.width = CORE.Window.screen.width;
|
||||
CORE.Window.currentFbo.height = CORE.Window.screen.height;
|
||||
|
||||
// NOTE: Postprocessing texture is not scaled to new size
|
||||
|
||||
CORE.Window.resized = true;
|
||||
|
||||
} break;
|
||||
case UWP_MSG_SET_GAME_TIME: CORE.Time.current = msg->paramDouble0; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
DeleteUWPMessage(msg); //Delete, we are done
|
||||
}
|
||||
#endif // PLATFORM_UWP
|
||||
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||
|
|
@ -4114,6 +4028,13 @@ static void WindowIconifyCallback(GLFWwindow *window, int iconified)
|
|||
else CORE.Window.minimized = false; // The window was restored
|
||||
}
|
||||
|
||||
// GLFW3 WindowFocus Callback, runs when window get/lose focus
|
||||
static void WindowFocusCallback(GLFWwindow *window, int focused)
|
||||
{
|
||||
if (focused) CORE.Window.focused = true; // The window was focused
|
||||
else CORE.Window.focused = false; // The window lost focus
|
||||
}
|
||||
|
||||
// GLFW3 Window Drop Callback, runs when drop files into window
|
||||
// NOTE: Paths are stored in dynamic memory for further retrieval
|
||||
// Everytime new files are dropped, old ones are discarded
|
||||
|
|
@ -4249,7 +4170,8 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
|
|||
|
||||
if (type == AINPUT_EVENT_TYPE_MOTION)
|
||||
{
|
||||
if ((source & AINPUT_SOURCE_JOYSTICK) == AINPUT_SOURCE_JOYSTICK || (source & AINPUT_SOURCE_GAMEPAD) == AINPUT_SOURCE_GAMEPAD)
|
||||
if (((source & AINPUT_SOURCE_JOYSTICK) == AINPUT_SOURCE_JOYSTICK) ||
|
||||
((source & AINPUT_SOURCE_GAMEPAD) == AINPUT_SOURCE_GAMEPAD))
|
||||
{
|
||||
// Get first touch position
|
||||
CORE.Input.Touch.position[0].x = AMotionEvent_getX(event, 0);
|
||||
|
|
@ -4399,7 +4321,7 @@ static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const Emscripte
|
|||
// Register keyboard input events
|
||||
static EM_BOOL EmscriptenKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
|
||||
{
|
||||
if ((eventType == EMSCRIPTEN_EVENT_KEYPRESS) && (strcmp(keyEvent->code, "Escape") == 0))
|
||||
if ((eventType == EMSCRIPTEN_EVENT_KEYPRESS) && (keyEvent->key == 27)) // ESCAPE key
|
||||
{
|
||||
emscripten_exit_pointerlock();
|
||||
}
|
||||
|
|
@ -5064,20 +4986,11 @@ static void *EventThread(void *arg)
|
|||
// Make sure we got a valid keycode
|
||||
if ((keycode > 0) && (keycode < sizeof(CORE.Input.Keyboard.currentKeyState)))
|
||||
{
|
||||
/* Disabled buffer !!
|
||||
// Store the key information for raylib to later use
|
||||
CORE.Input.Keyboard.currentKeyState[keycode] = event.value;
|
||||
if (event.value > 0)
|
||||
{
|
||||
// Add the key int the fifo
|
||||
CORE.Input.Keyboard.lastKeyPressed.contents[CORE.Input.Keyboard.lastKeyPressed.head] = keycode; // Put the data at the front of the fifo snake
|
||||
CORE.Input.Keyboard.lastKeyPressed.head = (CORE.Input.Keyboard.lastKeyPressed.head + 1) & 0x07; // Increment the head pointer forwards and binary wraparound after 7 (fifo is 8 elements long)
|
||||
// TODO: This fifo is not fully threadsafe with multiple writers, so multiple keyboards hitting a key at the exact same time could miss a key (double write to head before it was incremented)
|
||||
}
|
||||
*/
|
||||
|
||||
CORE.Input.Keyboard.currentKeyState[keycode] = event.value;
|
||||
if (event.value == 1)
|
||||
// WARNING: https://www.kernel.org/doc/Documentation/input/input.txt
|
||||
// Event interface: 'value' is the value the event carries. Either a relative change for EV_REL,
|
||||
// absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat
|
||||
CORE.Input.Keyboard.currentKeyState[keycode] = (event.value >= 1)? 1 : 0;
|
||||
if (event.value >= 1)
|
||||
{
|
||||
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = keycode; // Register last key pressed
|
||||
CORE.Input.Keyboard.keyPressedQueueCount++;
|
||||
|
|
@ -5235,3 +5148,277 @@ static void *GamepadThread(void *arg)
|
|||
return NULL;
|
||||
}
|
||||
#endif // PLATFORM_RPI
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
// UWP function pointers
|
||||
// NOTE: Those pointers are set by UWP App
|
||||
static UWPQueryTimeFunc uwpQueryTimeFunc = NULL;
|
||||
static UWPSleepFunc uwpSleepFunc = NULL;
|
||||
static UWPDisplaySizeFunc uwpDisplaySizeFunc = NULL;
|
||||
static UWPMouseFunc uwpMouseLockFunc = NULL;
|
||||
static UWPMouseFunc uwpMouseUnlockFunc = NULL;
|
||||
static UWPMouseFunc uwpMouseShowFunc = NULL;
|
||||
static UWPMouseFunc uwpMouseHideFunc = NULL;
|
||||
static UWPMouseSetPosFunc uwpMouseSetPosFunc = NULL;
|
||||
static void *uwpCoreWindow = NULL;
|
||||
|
||||
// Check all required UWP function pointers have been set
|
||||
bool UWPIsConfigured()
|
||||
{
|
||||
bool pass = true;
|
||||
|
||||
if (uwpQueryTimeFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetQueryTimeFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpSleepFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetSleepFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpDisplaySizeFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetDisplaySizeFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpMouseLockFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseLockFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpMouseUnlockFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseUnlockFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpMouseShowFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseShowFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpMouseHideFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseHideFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpMouseSetPosFunc == NULL) { TRACELOG(LOG_ERROR, "UWP: UWPSetMouseSetPosFunc() must be called with a valid function before InitWindow()"); pass = false; }
|
||||
if (uwpCoreWindow == NULL) { TRACELOG(LOG_ERROR, "UWP: A pointer to the UWP core window must be set before InitWindow()"); pass = false; }
|
||||
|
||||
return pass;
|
||||
}
|
||||
|
||||
// UWP function handlers get/set
|
||||
void UWPSetDataPath(const char* path) { CORE.UWP.internalDataPath = path; }
|
||||
UWPQueryTimeFunc UWPGetQueryTimeFunc(void) { return uwpQueryTimeFunc; }
|
||||
void UWPSetQueryTimeFunc(UWPQueryTimeFunc func) { uwpQueryTimeFunc = func; }
|
||||
UWPSleepFunc UWPGetSleepFunc(void) { return uwpSleepFunc; }
|
||||
void UWPSetSleepFunc(UWPSleepFunc func) { uwpSleepFunc = func; }
|
||||
UWPDisplaySizeFunc UWPGetDisplaySizeFunc(void) { return uwpDisplaySizeFunc; }
|
||||
void UWPSetDisplaySizeFunc(UWPDisplaySizeFunc func) { uwpDisplaySizeFunc = func; }
|
||||
UWPMouseFunc UWPGetMouseLockFunc() { return uwpMouseLockFunc; }
|
||||
void UWPSetMouseLockFunc(UWPMouseFunc func) { uwpMouseLockFunc = func; }
|
||||
UWPMouseFunc UWPGetMouseUnlockFunc() { return uwpMouseUnlockFunc; }
|
||||
void UWPSetMouseUnlockFunc(UWPMouseFunc func) { uwpMouseUnlockFunc = func; }
|
||||
UWPMouseFunc UWPGetMouseShowFunc() { return uwpMouseShowFunc; }
|
||||
void UWPSetMouseShowFunc(UWPMouseFunc func) { uwpMouseShowFunc = func; }
|
||||
UWPMouseFunc UWPGetMouseHideFunc() { return uwpMouseHideFunc; }
|
||||
void UWPSetMouseHideFunc(UWPMouseFunc func) { uwpMouseHideFunc = func; }
|
||||
UWPMouseSetPosFunc UWPGetMouseSetPosFunc() { return uwpMouseSetPosFunc; }
|
||||
void UWPSetMouseSetPosFunc(UWPMouseSetPosFunc func) { uwpMouseSetPosFunc = func; }
|
||||
|
||||
void *UWPGetCoreWindowPtr() { return uwpCoreWindow; }
|
||||
void UWPSetCoreWindowPtr(void* ptr) { uwpCoreWindow = ptr; }
|
||||
void UWPMouseWheelEvent(int deltaY) { CORE.Input.Mouse.currentWheelMove = (int)deltaY; }
|
||||
|
||||
void UWPKeyDownEvent(int key, bool down, bool controlKey)
|
||||
{
|
||||
if (key == CORE.Input.Keyboard.exitKey && down)
|
||||
{
|
||||
// Time to close the window.
|
||||
CORE.Window.shouldClose = true;
|
||||
}
|
||||
else if (key == KEY_F12 && down)
|
||||
{
|
||||
#if defined(SUPPORT_GIF_RECORDING)
|
||||
if (controlKey)
|
||||
{
|
||||
if (gifRecording)
|
||||
{
|
||||
GifEnd();
|
||||
gifRecording = false;
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
// Download file from MEMFS (emscripten memory filesystem)
|
||||
// saveFileFromMEMFSToDisk() function is defined in raylib/templates/web_shel/shell.html
|
||||
emscripten_run_script(TextFormat("saveFileFromMEMFSToDisk('%s','%s')", TextFormat("screenrec%03i.gif", screenshotCounter - 1), TextFormat("screenrec%03i.gif", screenshotCounter - 1)));
|
||||
#endif
|
||||
TRACELOG(LOG_INFO, "SYSTEM: Finish animated GIF recording");
|
||||
}
|
||||
else
|
||||
{
|
||||
gifRecording = true;
|
||||
gifFramesCounter = 0;
|
||||
|
||||
char path[512] = { 0 };
|
||||
strcpy(path, CORE.UWP.internalDataPath);
|
||||
strcat(path, TextFormat("./screenrec%03i.gif", screenshotCounter));
|
||||
|
||||
// NOTE: delay represents the time between frames in the gif, if we capture a gif frame every
|
||||
// 10 game frames and each frame trakes 16.6ms (60fps), delay between gif frames should be ~16.6*10.
|
||||
GifBegin(path, CORE.Window.screen.width, CORE.Window.screen.height, (int)(GetFrameTime() * 10.0f), 8, false);
|
||||
screenshotCounter++;
|
||||
|
||||
TRACELOG(LOG_INFO, "SYSTEM: Start animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif // SUPPORT_GIF_RECORDING
|
||||
#if defined(SUPPORT_SCREEN_CAPTURE)
|
||||
{
|
||||
TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
|
||||
screenshotCounter++;
|
||||
}
|
||||
#endif // SUPPORT_SCREEN_CAPTURE
|
||||
}
|
||||
else
|
||||
{
|
||||
CORE.Input.Keyboard.currentKeyState[key] = down;
|
||||
}
|
||||
}
|
||||
|
||||
void UWPKeyCharEvent(int key)
|
||||
{
|
||||
if (CORE.Input.Keyboard.keyPressedQueueCount < MAX_CHARS_QUEUE)
|
||||
{
|
||||
// Add character to the queue
|
||||
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount] = key;
|
||||
CORE.Input.Keyboard.keyPressedQueueCount++;
|
||||
}
|
||||
}
|
||||
|
||||
void UWPMouseButtonEvent(int button, bool down)
|
||||
{
|
||||
CORE.Input.Mouse.currentButtonState[button] = down;
|
||||
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
|
||||
// Process mouse events as touches to be able to use mouse-gestures
|
||||
GestureEvent gestureEvent = { 0 };
|
||||
|
||||
// Register touch actions
|
||||
if ((CORE.Input.Mouse.currentButtonState[button] == 1) && (CORE.Input.Mouse.previousButtonState[button] == 0)) gestureEvent.touchAction = TOUCH_DOWN;
|
||||
else if ((CORE.Input.Mouse.currentButtonState[button] == 0) && (CORE.Input.Mouse.previousButtonState[button] == 1)) gestureEvent.touchAction = TOUCH_UP;
|
||||
|
||||
// NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback()
|
||||
|
||||
// Assign a pointer ID
|
||||
gestureEvent.pointerId[0] = 0;
|
||||
|
||||
// Register touch points count
|
||||
gestureEvent.pointCount = 1;
|
||||
|
||||
// Register touch points position, only one point registered
|
||||
gestureEvent.position[0] = GetMousePosition();
|
||||
|
||||
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
|
||||
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
||||
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
||||
|
||||
// Gesture data is sent to gestures system for processing
|
||||
ProcessGestureEvent(gestureEvent);
|
||||
#endif
|
||||
}
|
||||
|
||||
void UWPMousePosEvent(double x, double y)
|
||||
{
|
||||
CORE.Input.Mouse.position.x = (float)x;
|
||||
CORE.Input.Mouse.position.y = (float)y;
|
||||
CORE.Input.Touch.position[0] = CORE.Input.Mouse.position;
|
||||
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
|
||||
// Process mouse events as touches to be able to use mouse-gestures
|
||||
GestureEvent gestureEvent = { 0 };
|
||||
|
||||
gestureEvent.touchAction = TOUCH_MOVE;
|
||||
|
||||
// Assign a pointer ID
|
||||
gestureEvent.pointerId[0] = 0;
|
||||
|
||||
// Register touch points count
|
||||
gestureEvent.pointCount = 1;
|
||||
|
||||
// Register touch points position, only one point registered
|
||||
gestureEvent.position[0] = CORE.Input.Mouse.position;
|
||||
|
||||
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
|
||||
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
||||
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
||||
|
||||
// Gesture data is sent to gestures system for processing
|
||||
ProcessGestureEvent(gestureEvent);
|
||||
#endif
|
||||
}
|
||||
|
||||
void UWPResizeEvent(int width, int height)
|
||||
{
|
||||
SetupViewport(width, height); // Reset viewport and projection matrix for new size
|
||||
|
||||
// Set current screen size
|
||||
CORE.Window.screen.width = width;
|
||||
CORE.Window.screen.height = height;
|
||||
CORE.Window.currentFbo.width = width;
|
||||
CORE.Window.currentFbo.height = height;
|
||||
|
||||
// NOTE: Postprocessing texture is not scaled to new size
|
||||
|
||||
CORE.Window.resized = true;
|
||||
}
|
||||
|
||||
void UWPActivateGamepadEvent(int gamepad, bool active)
|
||||
{
|
||||
if (gamepad < MAX_GAMEPADS) CORE.Input.Gamepad.ready[gamepad] = active;
|
||||
}
|
||||
|
||||
void UWPRegisterGamepadButton(int gamepad, int button, bool down)
|
||||
{
|
||||
if (gamepad < MAX_GAMEPADS)
|
||||
{
|
||||
if (button < MAX_GAMEPAD_BUTTONS)
|
||||
{
|
||||
CORE.Input.Gamepad.currentState[gamepad][button] = down;
|
||||
CORE.Input.Gamepad.lastButtonPressed = button;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UWPRegisterGamepadAxis(int gamepad, int axis, float value)
|
||||
{
|
||||
if (gamepad < MAX_GAMEPADS)
|
||||
{
|
||||
if (axis < MAX_GAMEPAD_AXIS) CORE.Input.Gamepad.axisState[gamepad][axis] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void UWPGestureMove(int pointer, float x, float y)
|
||||
{
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM)
|
||||
GestureEvent gestureEvent = { 0 };
|
||||
|
||||
// Assign the pointer ID and touch action
|
||||
gestureEvent.pointerId[0] = pointer;
|
||||
gestureEvent.touchAction = TOUCH_MOVE;
|
||||
|
||||
// Register touch points count
|
||||
gestureEvent.pointCount = 1;
|
||||
|
||||
// Register touch points position, only one point registered
|
||||
gestureEvent.position[0].x = x;
|
||||
gestureEvent.position[0].y = y;
|
||||
|
||||
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
|
||||
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
||||
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
||||
|
||||
// Gesture data is sent to gestures system for processing
|
||||
ProcessGestureEvent(gestureEvent);
|
||||
#endif
|
||||
}
|
||||
|
||||
void UWPGestureTouch(int pointer, float x, float y, bool touch)
|
||||
{
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM)
|
||||
GestureEvent gestureEvent = { 0 };
|
||||
|
||||
// Assign the pointer ID and touch action
|
||||
gestureEvent.pointerId[0] = pointer;
|
||||
gestureEvent.touchAction = touch ? TOUCH_DOWN : TOUCH_UP;
|
||||
|
||||
// Register touch points count
|
||||
gestureEvent.pointCount = 1;
|
||||
|
||||
// Register touch points position, only one point registered
|
||||
gestureEvent.position[0].x = x;
|
||||
gestureEvent.position[0].y = y;
|
||||
|
||||
// Normalize gestureEvent.position[0] for CORE.Window.screen.width and CORE.Window.screen.height
|
||||
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
||||
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
||||
|
||||
// Gesture data is sent to gestures system for processing
|
||||
ProcessGestureEvent(gestureEvent);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // PLATFORM_UWP
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@
|
|||
#define EASEDEF extern
|
||||
#endif
|
||||
|
||||
#include <math.h> // Required for: sinf(), cosf(), sqrt(), pow()
|
||||
#include <math.h> // Required for: sinf(), cosf(), sqrtf(), powf()
|
||||
|
||||
#ifndef PI
|
||||
#define PI 3.14159265358979323846f //Required as PI is not always defined in math.h
|
||||
|
|
@ -112,12 +112,12 @@ EASEDEF float EaseSineOut(float t, float b, float c, float d) { return (c*sinf(t
|
|||
EASEDEF float EaseSineInOut(float t, float b, float c, float d) { return (-c/2.0f*(cosf(PI*t/d) - 1.0f) + b); }
|
||||
|
||||
// Circular Easing functions
|
||||
EASEDEF float EaseCircIn(float t, float b, float c, float d) { t /= d; return (-c*(sqrt(1.0f - t*t) - 1.0f) + b); }
|
||||
EASEDEF float EaseCircOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*sqrt(1.0f - t*t) + b); }
|
||||
EASEDEF float EaseCircIn(float t, float b, float c, float d) { t /= d; return (-c*(sqrtf(1.0f - t*t) - 1.0f) + b); }
|
||||
EASEDEF float EaseCircOut(float t, float b, float c, float d) { t = t/d - 1.0f; return (c*sqrtf(1.0f - t*t) + b); }
|
||||
EASEDEF float EaseCircInOut(float t, float b, float c, float d)
|
||||
{
|
||||
if ((t/=d/2.0f) < 1.0f) return (-c/2.0f*(sqrt(1.0f - t*t) - 1.0f) + b);
|
||||
t -= 2.0f; return (c/2.0f*(sqrt(1.0f - t*t) + 1.0f) + b);
|
||||
if ((t/=d/2.0f) < 1.0f) return (-c/2.0f*(sqrtf(1.0f - t*t) - 1.0f) + b);
|
||||
t -= 2.0f; return (c/2.0f*(sqrtf(1.0f - t*t) + 1.0f) + b);
|
||||
}
|
||||
|
||||
// Cubic Easing functions
|
||||
|
|
@ -139,15 +139,15 @@ EASEDEF float EaseQuadInOut(float t, float b, float c, float d)
|
|||
}
|
||||
|
||||
// Exponential Easing functions
|
||||
EASEDEF float EaseExpoIn(float t, float b, float c, float d) { return (t == 0.0f) ? b : (c*pow(2.0f, 10.0f*(t/d - 1.0f)) + b); }
|
||||
EASEDEF float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-pow(2.0f, -10.0f*t/d) + 1.0f) + b); }
|
||||
EASEDEF float EaseExpoIn(float t, float b, float c, float d) { return (t == 0.0f) ? b : (c*powf(2.0f, 10.0f*(t/d - 1.0f)) + b); }
|
||||
EASEDEF float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-powf(2.0f, -10.0f*t/d) + 1.0f) + b); }
|
||||
EASEDEF float EaseExpoInOut(float t, float b, float c, float d)
|
||||
{
|
||||
if (t == 0.0f) return b;
|
||||
if (t == d) return (b + c);
|
||||
if ((t/=d/2.0f) < 1.0f) return (c/2.0f*pow(2.0f, 10.0f*(t - 1.0f)) + b);
|
||||
if ((t/=d/2.0f) < 1.0f) return (c/2.0f*powf(2.0f, 10.0f*(t - 1.0f)) + b);
|
||||
|
||||
return (c/2.0f*(-pow(2.0f, -10.0f*(t - 1.0f)) + 2.0f) + b);
|
||||
return (c/2.0f*(-powf(2.0f, -10.0f*(t - 1.0f)) + 2.0f) + b);
|
||||
}
|
||||
|
||||
// Back Easing functions
|
||||
|
|
@ -219,7 +219,7 @@ EASEDEF float EaseElasticIn(float t, float b, float c, float d)
|
|||
float p = d*0.3f;
|
||||
float a = c;
|
||||
float s = p/4.0f;
|
||||
float postFix = a*pow(2.0f, 10.0f*(t-=1.0f));
|
||||
float postFix = a*powf(2.0f, 10.0f*(t-=1.0f));
|
||||
|
||||
return (-(postFix*sinf((t*d-s)*(2.0f*PI)/p )) + b);
|
||||
}
|
||||
|
|
@ -233,7 +233,7 @@ EASEDEF float EaseElasticOut(float t, float b, float c, float d)
|
|||
float a = c;
|
||||
float s = p/4.0f;
|
||||
|
||||
return (a*pow(2.0f,-10.0f*t)*sinf((t*d-s)*(2.0f*PI)/p) + c + b);
|
||||
return (a*powf(2.0f,-10.0f*t)*sinf((t*d-s)*(2.0f*PI)/p) + c + b);
|
||||
}
|
||||
|
||||
EASEDEF float EaseElasticInOut(float t, float b, float c, float d)
|
||||
|
|
@ -247,11 +247,11 @@ EASEDEF float EaseElasticInOut(float t, float b, float c, float d)
|
|||
|
||||
if (t < 1.0f)
|
||||
{
|
||||
float postFix = a*pow(2.0f, 10.0f*(t-=1.0f));
|
||||
float postFix = a*powf(2.0f, 10.0f*(t-=1.0f));
|
||||
return -0.5f*(postFix*sinf((t*d-s)*(2.0f*PI)/p)) + b;
|
||||
}
|
||||
|
||||
float postFix = a*pow(2.0f, -10.0f*(t-=1.0f));
|
||||
float postFix = a*powf(2.0f, -10.0f*(t-=1.0f));
|
||||
|
||||
return (postFix*sinf((t*d-s)*(2.0f*PI)/p)*0.5f + c + b);
|
||||
}
|
||||
|
|
|
|||
22
src/models.c
22
src/models.c
|
|
@ -3521,27 +3521,15 @@ static Model LoadGLTF(const char *fileName)
|
|||
Model model = { 0 };
|
||||
|
||||
// glTF file loading
|
||||
FILE *gltfFile = fopen(fileName, "rb");
|
||||
unsigned int dataSize = 0;
|
||||
unsigned char *fileData = LoadFileData(fileName, &dataSize);
|
||||
|
||||
if (gltfFile == NULL)
|
||||
{
|
||||
TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open glTF file", fileName);
|
||||
return model;
|
||||
}
|
||||
|
||||
fseek(gltfFile, 0, SEEK_END);
|
||||
int size = ftell(gltfFile);
|
||||
fseek(gltfFile, 0, SEEK_SET);
|
||||
|
||||
void *buffer = RL_MALLOC(size);
|
||||
fread(buffer, size, 1, gltfFile);
|
||||
|
||||
fclose(gltfFile);
|
||||
if (fileData == NULL) return model;
|
||||
|
||||
// glTF data loading
|
||||
cgltf_options options = { 0 };
|
||||
cgltf_data *data = NULL;
|
||||
cgltf_result result = cgltf_parse(&options, buffer, size, &data);
|
||||
cgltf_result result = cgltf_parse(&options, fileData, dataSize, &data);
|
||||
|
||||
if (result == cgltf_result_success)
|
||||
{
|
||||
|
|
@ -3713,7 +3701,7 @@ static Model LoadGLTF(const char *fileName)
|
|||
}
|
||||
else TRACELOG(LOG_WARNING, "MODEL: [%s] Failed to load glTF data", fileName);
|
||||
|
||||
RL_FREE(buffer);
|
||||
RL_FREE(fileData);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -881,7 +881,8 @@ RLAPI void InitWindow(int width, int height, const char *title); // Initialize
|
|||
RLAPI bool WindowShouldClose(void); // Check if KEY_ESCAPE pressed or Close icon pressed
|
||||
RLAPI void CloseWindow(void); // Close window and unload OpenGL context
|
||||
RLAPI bool IsWindowReady(void); // Check if window has been initialized successfully
|
||||
RLAPI bool IsWindowMinimized(void); // Check if window has been minimized (or lost focus)
|
||||
RLAPI bool IsWindowMinimized(void); // Check if window has been minimized
|
||||
RLAPI bool IsWindowFocused(void); // Check if window has been focused
|
||||
RLAPI bool IsWindowResized(void); // Check if window has been resized
|
||||
RLAPI bool IsWindowHidden(void); // Check if window is currently hidden
|
||||
RLAPI bool IsWindowFullscreen(void); // Check if window is currently fullscreen
|
||||
|
|
@ -903,6 +904,7 @@ RLAPI int GetMonitorHeight(int monitor); // Get primary
|
|||
RLAPI int GetMonitorPhysicalWidth(int monitor); // Get primary monitor physical width in millimetres
|
||||
RLAPI int GetMonitorPhysicalHeight(int monitor); // Get primary monitor physical height in millimetres
|
||||
RLAPI Vector2 GetWindowPosition(void); // Get window position XY on monitor
|
||||
RLAPI Vector2 GetWindowScaleDPI(void); // Get window scale DPI factor
|
||||
RLAPI const char *GetMonitorName(int monitor); // Get the human-readable, UTF-8 encoded name of the primary monitor
|
||||
RLAPI const char *GetClipboardText(void); // Get clipboard text content
|
||||
RLAPI void SetClipboardText(const char *text); // Set clipboard text content
|
||||
|
|
|
|||
100
src/raymath.h
100
src/raymath.h
|
|
@ -179,6 +179,13 @@ RMDEF Vector2 Vector2Add(Vector2 v1, Vector2 v2)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Add vector and float value
|
||||
RMDEF Vector2 Vector2AddValue(Vector2 v, float add)
|
||||
{
|
||||
Vector2 result = { v.x + add, v.y + add };
|
||||
return result;
|
||||
}
|
||||
|
||||
// Subtract two vectors (v1 - v2)
|
||||
RMDEF Vector2 Vector2Subtract(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
|
|
@ -186,6 +193,13 @@ RMDEF Vector2 Vector2Subtract(Vector2 v1, Vector2 v2)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Subtract vector by float value
|
||||
RMDEF Vector2 Vector2SubtractValue(Vector2 v, float sub)
|
||||
{
|
||||
Vector2 result = { v.x - sub, v.y - sub };
|
||||
return result;
|
||||
}
|
||||
|
||||
// Calculate vector length
|
||||
RMDEF float Vector2Length(Vector2 v)
|
||||
{
|
||||
|
|
@ -223,7 +237,7 @@ RMDEF Vector2 Vector2Scale(Vector2 v, float scale)
|
|||
}
|
||||
|
||||
// Multiply vector by vector
|
||||
RMDEF Vector2 Vector2MultiplyV(Vector2 v1, Vector2 v2)
|
||||
RMDEF Vector2 Vector2Multiply(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
Vector2 result = { v1.x*v2.x, v1.y*v2.y };
|
||||
return result;
|
||||
|
|
@ -236,15 +250,8 @@ RMDEF Vector2 Vector2Negate(Vector2 v)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Divide vector by a float value
|
||||
RMDEF Vector2 Vector2Divide(Vector2 v, float div)
|
||||
{
|
||||
Vector2 result = { v.x/div, v.y/div };
|
||||
return result;
|
||||
}
|
||||
|
||||
// Divide vector by vector
|
||||
RMDEF Vector2 Vector2DivideV(Vector2 v1, Vector2 v2)
|
||||
RMDEF Vector2 Vector2Divide(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
Vector2 result = { v1.x/v2.x, v1.y/v2.y };
|
||||
return result;
|
||||
|
|
@ -253,7 +260,7 @@ RMDEF Vector2 Vector2DivideV(Vector2 v1, Vector2 v2)
|
|||
// Normalize provided vector
|
||||
RMDEF Vector2 Vector2Normalize(Vector2 v)
|
||||
{
|
||||
Vector2 result = Vector2Divide(v, Vector2Length(v));
|
||||
Vector2 result = Vector2Scale(v, 1/Vector2Length(v));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -301,6 +308,13 @@ RMDEF Vector3 Vector3Add(Vector3 v1, Vector3 v2)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Add vector and float value
|
||||
RMDEF Vector3 Vector3AddValue(Vector3 v, float add)
|
||||
{
|
||||
Vector3 result = { v.x + add, v.y + add, v.z + add };
|
||||
return result;
|
||||
}
|
||||
|
||||
// Subtract two vectors
|
||||
RMDEF Vector3 Vector3Subtract(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
|
|
@ -308,6 +322,13 @@ RMDEF Vector3 Vector3Subtract(Vector3 v1, Vector3 v2)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Subtract vector by float value
|
||||
RMDEF Vector3 Vector3SubtractValue(Vector3 v, float sub)
|
||||
{
|
||||
Vector3 result = { v.x - sub, v.y - sub, v.z - sub };
|
||||
return result;
|
||||
}
|
||||
|
||||
// Multiply vector by scalar
|
||||
RMDEF Vector3 Vector3Scale(Vector3 v, float scalar)
|
||||
{
|
||||
|
|
@ -386,15 +407,8 @@ RMDEF Vector3 Vector3Negate(Vector3 v)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Divide vector by a float value
|
||||
RMDEF Vector3 Vector3Divide(Vector3 v, float div)
|
||||
{
|
||||
Vector3 result = { v.x / div, v.y / div, v.z / div };
|
||||
return result;
|
||||
}
|
||||
|
||||
// Divide vector by vector
|
||||
RMDEF Vector3 Vector3DivideV(Vector3 v1, Vector3 v2)
|
||||
RMDEF Vector3 Vector3Divide(Vector3 v1, Vector3 v2)
|
||||
{
|
||||
Vector3 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z };
|
||||
return result;
|
||||
|
|
@ -1040,6 +1054,34 @@ RMDEF float16 MatrixToFloatV(Matrix mat)
|
|||
// Module Functions Definition - Quaternion math
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Add two quaternions
|
||||
RMDEF Quaternion QuaternionAdd(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
Quaternion result = {q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Add quaternion and float value
|
||||
RMDEF Quaternion QuaternionAddValue(Quaternion q, float add)
|
||||
{
|
||||
Quaternion result = {q.x + add, q.y + add, q.z + add, q.w + add};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Subtract two quaternions
|
||||
RMDEF Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
Quaternion result = {q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Subtract quaternion and float value
|
||||
RMDEF Quaternion QuaternionSubtractValue(Quaternion q, float sub)
|
||||
{
|
||||
Quaternion result = {q.x - sub, q.y - sub, q.z - sub, q.w - sub};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Returns identity quaternion
|
||||
RMDEF Quaternion QuaternionIdentity(void)
|
||||
{
|
||||
|
|
@ -1108,6 +1150,28 @@ RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2)
|
|||
return result;
|
||||
}
|
||||
|
||||
// Scale quaternion by float value
|
||||
RMDEF Quaternion QuaternionScale(Quaternion q, float mul)
|
||||
{
|
||||
Quaternion result = { 0 };
|
||||
|
||||
float qax = q.x, qay = q.y, qaz = q.z, qaw = q.w;
|
||||
|
||||
result.x = qax * mul + qaw * mul + qay * mul - qaz * mul;
|
||||
result.y = qay * mul + qaw * mul + qaz * mul - qax * mul;
|
||||
result.z = qaz * mul + qaw * mul + qax * mul - qay * mul;
|
||||
result.w = qaw * mul - qax * mul - qay * mul - qaz * mul;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Divide two quaternions
|
||||
RMDEF Quaternion QuaternionDivide(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
Quaternion result = {q1.x / q2.x, q1.y / q2.y, q1.z / q2.z, q1.w / q2.w};
|
||||
return result;
|
||||
}
|
||||
|
||||
// Calculate linear interpolation between two quaternions
|
||||
RMDEF Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -663,6 +663,7 @@ RLAPI int GetPixelDataSize(int width, int height, int format);// Get pixel data
|
|||
#endif
|
||||
|
||||
#if defined(GRAPHICS_API_OPENGL_ES2)
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <EGL/egl.h> // EGL library
|
||||
#include <GLES2/gl2.h> // OpenGL ES 2.0 library
|
||||
#include <GLES2/gl2ext.h> // OpenGL ES 2.0 extensions library
|
||||
|
|
|
|||
736
src/rmem.h
736
src/rmem.h
|
|
@ -2,7 +2,7 @@
|
|||
*
|
||||
* rmem - raylib memory pool and objects pool
|
||||
*
|
||||
* A quick, efficient, and minimal free list and stack-based allocator
|
||||
* A quick, efficient, and minimal free list and arena-based allocator
|
||||
*
|
||||
* PURPOSE:
|
||||
* - A quicker, efficient memory allocator alternative to 'malloc' and friends.
|
||||
|
|
@ -55,6 +55,8 @@
|
|||
#define RMEMAPI // We are building or using library as a static library (or Linux shared library)
|
||||
#endif
|
||||
|
||||
#define RMEM_VERSION "v1.3" // changelog at bottom of header.
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
@ -66,39 +68,45 @@ struct MemNode {
|
|||
MemNode *next, *prev;
|
||||
};
|
||||
|
||||
// Freelist implementation
|
||||
typedef struct AllocList {
|
||||
MemNode *head, *tail;
|
||||
size_t len, maxNodes;
|
||||
bool autoDefrag : 1;
|
||||
size_t len;
|
||||
} AllocList;
|
||||
|
||||
typedef struct Stack {
|
||||
uint8_t *mem, *base;
|
||||
// Arena allocator.
|
||||
typedef struct Arena {
|
||||
uintptr_t mem, offs;
|
||||
size_t size;
|
||||
} Stack;
|
||||
} Arena;
|
||||
|
||||
#define MEMPOOL_BUCKET_SIZE 8
|
||||
#define MEMPOOL_BUCKET_BITS 3
|
||||
|
||||
enum {
|
||||
MEMPOOL_BUCKET_SIZE = 8,
|
||||
MEMPOOL_BUCKET_BITS = (sizeof(uintptr_t) >> 1) + 1,
|
||||
MEM_SPLIT_THRESHOLD = sizeof(uintptr_t) * 4
|
||||
};
|
||||
|
||||
typedef struct MemPool {
|
||||
AllocList freeList;
|
||||
Stack stack;
|
||||
MemNode *buckets[MEMPOOL_BUCKET_SIZE];
|
||||
AllocList large, buckets[MEMPOOL_BUCKET_SIZE];
|
||||
Arena arena;
|
||||
} MemPool;
|
||||
|
||||
|
||||
// Object Pool
|
||||
typedef struct ObjPool {
|
||||
Stack stack;
|
||||
size_t objSize, freeBlocks;
|
||||
uintptr_t mem, offs;
|
||||
size_t objSize, freeBlocks, memSize;
|
||||
} ObjPool;
|
||||
|
||||
|
||||
// Double-Ended Stack aka Deque
|
||||
typedef struct BiStack {
|
||||
uint8_t *mem, *front, *back;
|
||||
uintptr_t mem, front, back;
|
||||
size_t size;
|
||||
} BiStack;
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" { // Prevents name mangling of functions
|
||||
#endif
|
||||
|
|
@ -115,10 +123,7 @@ RMEMAPI void *MemPoolRealloc(MemPool *mempool, void *ptr, size_t bytes);
|
|||
RMEMAPI void MemPoolFree(MemPool *mempool, void *ptr);
|
||||
RMEMAPI void MemPoolCleanUp(MemPool *mempool, void **ptrref);
|
||||
RMEMAPI void MemPoolReset(MemPool *mempool);
|
||||
RMEMAPI bool MemPoolDefrag(MemPool *mempool);
|
||||
|
||||
RMEMAPI size_t GetMemPoolFreeMemory(const MemPool mempool);
|
||||
RMEMAPI void ToggleMemPoolAutoDefrag(MemPool *mempool);
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Functions Declaration - Object Pool
|
||||
|
|
@ -161,7 +166,9 @@ RMEMAPI intptr_t BiStackMargins(BiStack destack);
|
|||
|
||||
#if defined(RMEM_IMPLEMENTATION)
|
||||
|
||||
#include <stdio.h> // Required for: malloc(), calloc(), free()
|
||||
#include <stdio.h> // Required for:
|
||||
#include <stdlib.h> // Required for:
|
||||
#include <string.h> // Required for:
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
|
|
@ -188,6 +195,145 @@ static inline size_t __AlignSize(const size_t size, const size_t align)
|
|||
return (size + (align - 1)) & -align;
|
||||
}
|
||||
|
||||
static MemNode *__SplitMemNode(MemNode *const node, const size_t bytes)
|
||||
{
|
||||
uintptr_t n = ( uintptr_t )node;
|
||||
MemNode *const r = ( MemNode* )(n + (node->size - bytes));
|
||||
node->size -= bytes;
|
||||
r->size = bytes;
|
||||
return r;
|
||||
}
|
||||
|
||||
static void __InsertMemNodeBefore(AllocList *const list, MemNode *const insert, MemNode *const curr)
|
||||
{
|
||||
insert->next = curr;
|
||||
if (curr->prev==NULL) list->head = insert;
|
||||
else
|
||||
{
|
||||
insert->prev = curr->prev;
|
||||
curr->prev->next = insert;
|
||||
}
|
||||
curr->prev = insert;
|
||||
}
|
||||
|
||||
static void __ReplaceMemNode(MemNode *const old, MemNode *const replace)
|
||||
{
|
||||
replace->prev = old->prev;
|
||||
replace->next = old->next;
|
||||
if( old->prev != NULL )
|
||||
old->prev->next = replace;
|
||||
if( old->next != NULL )
|
||||
old->next->prev = replace;
|
||||
}
|
||||
|
||||
|
||||
static MemNode *__RemoveMemNode(AllocList *const list, MemNode *const node)
|
||||
{
|
||||
if (node->prev != NULL) node->prev->next = node->next;
|
||||
else
|
||||
{
|
||||
list->head = node->next;
|
||||
if (list->head != NULL) list->head->prev = NULL;
|
||||
else list->tail = NULL;
|
||||
}
|
||||
|
||||
if (node->next != NULL) node->next->prev = node->prev;
|
||||
else
|
||||
{
|
||||
list->tail = node->prev;
|
||||
if (list->tail != NULL) list->tail->next = NULL;
|
||||
else list->head = NULL;
|
||||
}
|
||||
list->len--;
|
||||
return node;
|
||||
}
|
||||
|
||||
static MemNode *__FindMemNode(AllocList *const list, const size_t bytes)
|
||||
{
|
||||
for (MemNode *node = list->head; node != NULL; node = node->next)
|
||||
{
|
||||
if (node->size < bytes) continue;
|
||||
// close in size - reduce fragmentation by not splitting.
|
||||
else if (node->size <= bytes + MEM_SPLIT_THRESHOLD) return __RemoveMemNode(list, node);
|
||||
else return __SplitMemNode(node, bytes);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void __InsertMemNode(MemPool *const mempool, AllocList *const list, MemNode *const node, const bool is_bucket)
|
||||
{
|
||||
if (list->head == NULL)
|
||||
{
|
||||
list->head = node;
|
||||
list->len++;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (MemNode *iter = list->head; iter != NULL; iter = iter->next)
|
||||
{
|
||||
if (( uintptr_t )iter == mempool->arena.offs)
|
||||
{
|
||||
mempool->arena.offs += iter->size;
|
||||
__RemoveMemNode(list, iter);
|
||||
iter = list->head;
|
||||
}
|
||||
const uintptr_t inode = ( uintptr_t )node;
|
||||
const uintptr_t iiter = ( uintptr_t )iter;
|
||||
const uintptr_t iter_end = iiter + iter->size;
|
||||
const uintptr_t node_end = inode + node->size;
|
||||
if (iter==node) return;
|
||||
else if (iter < node)
|
||||
{
|
||||
// node was coalesced prior.
|
||||
if (iter_end > inode) return;
|
||||
else if (iter_end==inode && !is_bucket)
|
||||
{
|
||||
// if we can coalesce, do so.
|
||||
iter->size += node->size;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (iter > node)
|
||||
{
|
||||
// Address sort, lowest to highest aka ascending order.
|
||||
if (iiter < node_end) return;
|
||||
else if (iter==list->head && !is_bucket)
|
||||
{
|
||||
if (iter_end==inode) iter->size += node->size;
|
||||
else if (node_end==iiter)
|
||||
{
|
||||
node->size += list->head->size;
|
||||
node->next = list->head->next;
|
||||
node->prev = NULL;
|
||||
list->head = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
node->next = iter;
|
||||
node->prev = NULL;
|
||||
iter->prev = node;
|
||||
list->head = node;
|
||||
list->len++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (iter_end==inode && !is_bucket)
|
||||
{
|
||||
// if we can coalesce, do so.
|
||||
iter->size += node->size;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
__InsertMemNodeBefore(list, iter, node);
|
||||
list->len++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition - Memory Pool
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
@ -196,114 +342,77 @@ MemPool CreateMemPool(const size_t size)
|
|||
{
|
||||
MemPool mempool = { 0 };
|
||||
|
||||
if (size == 0UL) return mempool;
|
||||
if (size == 0) return mempool;
|
||||
else
|
||||
{
|
||||
// Align the mempool size to at least the size of an alloc node.
|
||||
mempool.stack.size = size;
|
||||
mempool.stack.mem = malloc(mempool.stack.size*sizeof *mempool.stack.mem);
|
||||
|
||||
if (mempool.stack.mem == NULL)
|
||||
{
|
||||
mempool.stack.size = 0UL;
|
||||
return mempool;
|
||||
}
|
||||
uint8_t *const restrict buf = malloc(size*sizeof *buf);
|
||||
if (buf==NULL) return mempool;
|
||||
else
|
||||
{
|
||||
mempool.stack.base = mempool.stack.mem + mempool.stack.size;
|
||||
mempool.arena.size = size;
|
||||
mempool.arena.mem = ( uintptr_t )buf;
|
||||
mempool.arena.offs = mempool.arena.mem + mempool.arena.size;
|
||||
return mempool;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MemPool CreateMemPoolFromBuffer(void *buf, const size_t size)
|
||||
MemPool CreateMemPoolFromBuffer(void *const restrict buf, const size_t size)
|
||||
{
|
||||
MemPool mempool = { 0 };
|
||||
|
||||
if ((size == 0UL) || (buf == NULL) || (size <= sizeof(MemNode))) return mempool;
|
||||
if ((size == 0) || (buf == NULL) || (size <= sizeof(MemNode))) return mempool;
|
||||
else
|
||||
{
|
||||
mempool.stack.size = size;
|
||||
mempool.stack.mem = buf;
|
||||
mempool.stack.base = mempool.stack.mem + mempool.stack.size;
|
||||
mempool.arena.size = size;
|
||||
mempool.arena.mem = ( uintptr_t )buf;
|
||||
mempool.arena.offs = mempool.arena.mem + mempool.arena.size;
|
||||
return mempool;
|
||||
}
|
||||
}
|
||||
|
||||
void DestroyMemPool(MemPool *const mempool)
|
||||
void DestroyMemPool(MemPool *const restrict mempool)
|
||||
{
|
||||
if ((mempool == NULL) || (mempool->stack.mem == NULL)) return;
|
||||
if (mempool->arena.mem == 0) return;
|
||||
else
|
||||
{
|
||||
free(mempool->stack.mem);
|
||||
void *const restrict ptr = ( void* )mempool->arena.mem;
|
||||
free(ptr);
|
||||
*mempool = (MemPool){ 0 };
|
||||
}
|
||||
}
|
||||
|
||||
void *MemPoolAlloc(MemPool *const mempool, const size_t size)
|
||||
{
|
||||
if ((mempool == NULL) || (size == 0UL) || (size > mempool->stack.size)) return NULL;
|
||||
if ((size == 0) || (size > mempool->arena.size)) return NULL;
|
||||
else
|
||||
{
|
||||
MemNode *new_mem = NULL;
|
||||
const size_t ALLOC_SIZE = __AlignSize(size + sizeof *new_mem, sizeof(intptr_t));
|
||||
const size_t BUCKET_INDEX = (ALLOC_SIZE >> MEMPOOL_BUCKET_BITS) - 1;
|
||||
const size_t BUCKET_SLOT = (ALLOC_SIZE >> MEMPOOL_BUCKET_BITS) - 1;
|
||||
|
||||
// If the size is small enough, let's check if our buckets has a fitting memory block.
|
||||
if ((BUCKET_INDEX < MEMPOOL_BUCKET_SIZE) &&
|
||||
(mempool->buckets[BUCKET_INDEX] != NULL) &&
|
||||
(mempool->buckets[BUCKET_INDEX]->size >= ALLOC_SIZE))
|
||||
if (BUCKET_SLOT < MEMPOOL_BUCKET_SIZE)
|
||||
{
|
||||
new_mem = mempool->buckets[BUCKET_INDEX];
|
||||
mempool->buckets[BUCKET_INDEX] = mempool->buckets[BUCKET_INDEX]->next;
|
||||
if( mempool->buckets[BUCKET_INDEX] != NULL )
|
||||
mempool->buckets[BUCKET_INDEX]->prev = NULL;
|
||||
new_mem = __FindMemNode(&mempool->buckets[BUCKET_SLOT], ALLOC_SIZE);
|
||||
}
|
||||
else if (mempool->freeList.head != NULL)
|
||||
else if (mempool->large.head != NULL)
|
||||
{
|
||||
const size_t MEM_SPLIT_THRESHOLD = 16;
|
||||
|
||||
// If the freelist is valid, let's allocate FROM the freelist then!
|
||||
for (MemNode *inode = mempool->freeList.head; inode != NULL; inode = inode->next)
|
||||
{
|
||||
if (inode->size < ALLOC_SIZE) continue;
|
||||
else if (inode->size <= (ALLOC_SIZE + MEM_SPLIT_THRESHOLD))
|
||||
{
|
||||
// Close in size - reduce fragmentation by not splitting.
|
||||
new_mem = inode;
|
||||
(inode->prev != NULL)? (inode->prev->next = inode->next) : (mempool->freeList.head = inode->next);
|
||||
(inode->next != NULL)? (inode->next->prev = inode->prev) : (mempool->freeList.tail = inode->prev);
|
||||
|
||||
if (mempool->freeList.head != NULL) mempool->freeList.head->prev = NULL;
|
||||
else mempool->freeList.tail = NULL;
|
||||
|
||||
if (mempool->freeList.tail != NULL) mempool->freeList.tail->next = NULL;
|
||||
mempool->freeList.len--;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Split the memory chunk.
|
||||
new_mem = (MemNode *)((uint8_t *)inode + (inode->size - ALLOC_SIZE));
|
||||
inode->size -= ALLOC_SIZE;
|
||||
new_mem->size = ALLOC_SIZE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
new_mem = __FindMemNode(&mempool->large, ALLOC_SIZE);
|
||||
}
|
||||
|
||||
if (new_mem == NULL)
|
||||
{
|
||||
// not enough memory to support the size!
|
||||
if ((mempool->stack.base - ALLOC_SIZE) < mempool->stack.mem) return NULL;
|
||||
if ((mempool->arena.offs - ALLOC_SIZE) < mempool->arena.mem) return NULL;
|
||||
else
|
||||
{
|
||||
// Couldn't allocate from a freelist, allocate from available mempool.
|
||||
// Subtract allocation size from the mempool.
|
||||
mempool->stack.base -= ALLOC_SIZE;
|
||||
mempool->arena.offs -= ALLOC_SIZE;
|
||||
|
||||
// Use the available mempool space as the new node.
|
||||
new_mem = (MemNode *)mempool->stack.base;
|
||||
new_mem = ( MemNode* )mempool->arena.offs;
|
||||
new_mem->size = ALLOC_SIZE;
|
||||
}
|
||||
}
|
||||
|
|
@ -313,33 +422,32 @@ void *MemPoolAlloc(MemPool *const mempool, const size_t size)
|
|||
// | mem size | lowest addr of block
|
||||
// | next node | 12 byte (32-bit) header
|
||||
// | prev node | 24 byte (64-bit) header
|
||||
// --------------
|
||||
// |------------|
|
||||
// | alloc'd |
|
||||
// | memory |
|
||||
// | space | highest addr of block
|
||||
// --------------
|
||||
new_mem->next = new_mem->prev = NULL;
|
||||
uint8_t *const final_mem = (uint8_t *)new_mem + sizeof *new_mem;
|
||||
uint8_t *const restrict final_mem = ( uint8_t* )new_mem + sizeof *new_mem;
|
||||
return memset(final_mem, 0, new_mem->size - sizeof *new_mem);
|
||||
}
|
||||
}
|
||||
|
||||
void *MemPoolRealloc(MemPool *const restrict mempool, void *ptr, const size_t size)
|
||||
void *MemPoolRealloc(MemPool *const restrict mempool, void *const ptr, const size_t size)
|
||||
{
|
||||
if ((mempool == NULL) || (size > mempool->stack.size)) return NULL;
|
||||
if (size > mempool->arena.size) return NULL;
|
||||
// NULL ptr should make this work like regular Allocation.
|
||||
else if (ptr == NULL) return MemPoolAlloc(mempool, size);
|
||||
else if ((uintptr_t)ptr - sizeof(MemNode) < (uintptr_t)mempool->stack.mem) return NULL;
|
||||
else if ((uintptr_t)ptr - sizeof(MemNode) < mempool->arena.mem) return NULL;
|
||||
else
|
||||
{
|
||||
MemNode *const node = (MemNode *)((uint8_t *)ptr - sizeof *node);
|
||||
MemNode *const node = ( MemNode* )(( uint8_t* )ptr - sizeof *node);
|
||||
const size_t NODE_SIZE = sizeof *node;
|
||||
uint8_t *const resized_block = MemPoolAlloc(mempool, size);
|
||||
|
||||
if (resized_block == NULL) return NULL;
|
||||
else
|
||||
{
|
||||
MemNode *const resized = (MemNode *)(resized_block - sizeof *resized);
|
||||
MemNode *const resized = ( MemNode* )(resized_block - sizeof *resized);
|
||||
memmove(resized_block, ptr, (node->size > resized->size)? (resized->size - NODE_SIZE) : (node->size - NODE_SIZE));
|
||||
MemPoolFree(mempool, ptr);
|
||||
return resized_block;
|
||||
|
|
@ -347,72 +455,39 @@ void *MemPoolRealloc(MemPool *const restrict mempool, void *ptr, const size_t si
|
|||
}
|
||||
}
|
||||
|
||||
void MemPoolFree(MemPool *const restrict mempool, void *ptr)
|
||||
void MemPoolFree(MemPool *const restrict mempool, void *const ptr)
|
||||
{
|
||||
if ((mempool == NULL) || (ptr == NULL) || ((uintptr_t)ptr - sizeof(MemNode) < (uintptr_t)mempool->stack.mem)) return;
|
||||
const uintptr_t p = ( uintptr_t )ptr;
|
||||
if ((ptr == NULL) || (p - sizeof(MemNode) < mempool->arena.mem)) return;
|
||||
else
|
||||
{
|
||||
// Behind the actual pointer data is the allocation info.
|
||||
MemNode *const mem_node = (MemNode *)((uint8_t *)ptr - sizeof *mem_node);
|
||||
const size_t BUCKET_INDEX = (mem_node->size >> MEMPOOL_BUCKET_BITS) - 1;
|
||||
const uintptr_t block = p - sizeof(MemNode);
|
||||
MemNode *const mem_node = ( MemNode* )block;
|
||||
const size_t BUCKET_SLOT = (mem_node->size >> MEMPOOL_BUCKET_BITS) - 1;
|
||||
|
||||
// Make sure the pointer data is valid.
|
||||
if (((uintptr_t)mem_node < (uintptr_t)mempool->stack.base) ||
|
||||
(((uintptr_t)mem_node - (uintptr_t)mempool->stack.mem) > mempool->stack.size) ||
|
||||
(mem_node->size == 0UL) ||
|
||||
(mem_node->size > mempool->stack.size)) return;
|
||||
// If the mem_node is right at the stack base ptr, then add it to the stack.
|
||||
else if ((uintptr_t)mem_node == (uintptr_t)mempool->stack.base)
|
||||
if ((block < mempool->arena.offs) ||
|
||||
((block - mempool->arena.mem) > mempool->arena.size) ||
|
||||
(mem_node->size == 0) ||
|
||||
(mem_node->size > mempool->arena.size)) return;
|
||||
// If the mem_node is right at the arena offs, then merge it back to the arena.
|
||||
else if (block == mempool->arena.offs)
|
||||
{
|
||||
mempool->stack.base += mem_node->size;
|
||||
mempool->arena.offs += mem_node->size;
|
||||
}
|
||||
// attempted stack merge failed, try to place it into the memnode buckets
|
||||
else if (BUCKET_INDEX < MEMPOOL_BUCKET_SIZE)
|
||||
else
|
||||
{
|
||||
if (mempool->buckets[BUCKET_INDEX] == NULL) mempool->buckets[BUCKET_INDEX] = mem_node;
|
||||
else
|
||||
{
|
||||
for (MemNode *n = mempool->buckets[BUCKET_INDEX]; n != NULL; n = n->next) if( n==mem_node ) return;
|
||||
mempool->buckets[BUCKET_INDEX]->prev = mem_node;
|
||||
mem_node->next = mempool->buckets[BUCKET_INDEX];
|
||||
mempool->buckets[BUCKET_INDEX] = mem_node;
|
||||
}
|
||||
}
|
||||
// Otherwise, we add it to the free list.
|
||||
// We also check if the freelist already has the pointer so we can prevent double frees.
|
||||
else /*if ((mempool->freeList.len == 0UL) || ((uintptr_t)mempool->freeList.head >= (uintptr_t)mempool->stack.mem && (uintptr_t)mempool->freeList.head - (uintptr_t)mempool->stack.mem < mempool->stack.size))*/
|
||||
{
|
||||
for (MemNode *n = mempool->freeList.head; n != NULL; n = n->next) if (n == mem_node) return;
|
||||
|
||||
// This code insertion sorts where largest size is last.
|
||||
if (mempool->freeList.head == NULL)
|
||||
{
|
||||
mempool->freeList.head = mempool->freeList.tail = mem_node;
|
||||
mempool->freeList.len++;
|
||||
}
|
||||
else if (mempool->freeList.head->size >= mem_node->size)
|
||||
{
|
||||
mem_node->next = mempool->freeList.head;
|
||||
mem_node->next->prev = mem_node;
|
||||
mempool->freeList.head = mem_node;
|
||||
mempool->freeList.len++;
|
||||
}
|
||||
else //if (mempool->freeList.tail->size <= mem_node->size)
|
||||
{
|
||||
mem_node->prev = mempool->freeList.tail;
|
||||
mempool->freeList.tail->next = mem_node;
|
||||
mempool->freeList.tail = mem_node;
|
||||
mempool->freeList.len++;
|
||||
}
|
||||
|
||||
if (mempool->freeList.autoDefrag && (mempool->freeList.maxNodes != 0UL) && (mempool->freeList.len > mempool->freeList.maxNodes)) MemPoolDefrag(mempool);
|
||||
// try to place it into bucket or large freelist.
|
||||
struct AllocList *const l = (BUCKET_SLOT < MEMPOOL_BUCKET_SIZE) ? &mempool->buckets[BUCKET_SLOT] : &mempool->large;
|
||||
__InsertMemNode(mempool, l, mem_node, (BUCKET_SLOT < MEMPOOL_BUCKET_SIZE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MemPoolCleanUp(MemPool *const restrict mempool, void **ptrref)
|
||||
void MemPoolCleanUp(MemPool *const restrict mempool, void **const ptrref)
|
||||
{
|
||||
if ((mempool == NULL) || (ptrref == NULL) || (*ptrref == NULL)) return;
|
||||
if ((ptrref == NULL) || (*ptrref == NULL)) return;
|
||||
else
|
||||
{
|
||||
MemPoolFree(mempool, *ptrref);
|
||||
|
|
@ -422,264 +497,127 @@ void MemPoolCleanUp(MemPool *const restrict mempool, void **ptrref)
|
|||
|
||||
size_t GetMemPoolFreeMemory(const MemPool mempool)
|
||||
{
|
||||
size_t total_remaining = (uintptr_t)mempool.stack.base - (uintptr_t)mempool.stack.mem;
|
||||
size_t total_remaining = mempool.arena.offs - mempool.arena.mem;
|
||||
|
||||
for (MemNode *n = mempool.freeList.head; n != NULL; n = n->next) total_remaining += n->size;
|
||||
for (MemNode *n=mempool.large.head; n != NULL; n = n->next) total_remaining += n->size;
|
||||
|
||||
for (int i = 0; i < MEMPOOL_BUCKET_SIZE; i++) for (MemNode *n = mempool.buckets[i]; n != NULL; n = n->next) total_remaining += n->size;
|
||||
for (size_t i=0; i<MEMPOOL_BUCKET_SIZE; i++) for (MemNode *n = mempool.buckets[i].head; n != NULL; n = n->next) total_remaining += n->size;
|
||||
|
||||
return total_remaining;
|
||||
}
|
||||
|
||||
void MemPoolReset(MemPool *const mempool)
|
||||
{
|
||||
if (mempool == NULL) return;
|
||||
mempool->freeList.head = mempool->freeList.tail = NULL;
|
||||
mempool->freeList.len = 0;
|
||||
for (int i = 0; i < MEMPOOL_BUCKET_SIZE; i++) mempool->buckets[i] = NULL;
|
||||
mempool->stack.base = mempool->stack.mem + mempool->stack.size;
|
||||
}
|
||||
|
||||
bool MemPoolDefrag(MemPool *const mempool)
|
||||
{
|
||||
if (mempool == NULL) return false;
|
||||
else
|
||||
mempool->large.head = mempool->large.tail = NULL;
|
||||
mempool->large.len = 0;
|
||||
for (size_t i = 0; i < MEMPOOL_BUCKET_SIZE; i++)
|
||||
{
|
||||
// If the memory pool has been entirely released, fully defrag it.
|
||||
if (mempool->stack.size == GetMemPoolFreeMemory(*mempool))
|
||||
{
|
||||
MemPoolReset(mempool);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < MEMPOOL_BUCKET_SIZE; i++)
|
||||
{
|
||||
while (mempool->buckets[i] != NULL)
|
||||
{
|
||||
if ((uintptr_t)mempool->buckets[i] == (uintptr_t)mempool->stack.base)
|
||||
{
|
||||
mempool->stack.base += mempool->buckets[i]->size;
|
||||
mempool->buckets[i]->size = 0;
|
||||
mempool->buckets[i] = mempool->buckets[i]->next;
|
||||
if (mempool->buckets[i] != NULL) mempool->buckets[i]->prev = NULL;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
}
|
||||
|
||||
const size_t PRE_DEFRAG_LEN = mempool->freeList.len;
|
||||
MemNode **node = &mempool->freeList.head;
|
||||
|
||||
while (*node != NULL)
|
||||
{
|
||||
if ((uintptr_t)*node == (uintptr_t)mempool->stack.base)
|
||||
{
|
||||
// If node is right at the stack, merge it back into the stack.
|
||||
mempool->stack.base += (*node)->size;
|
||||
(*node)->size = 0UL;
|
||||
((*node)->prev != NULL)? ((*node)->prev->next = (*node)->next) : (mempool->freeList.head = (*node)->next);
|
||||
((*node)->next != NULL)? ((*node)->next->prev = (*node)->prev) : (mempool->freeList.tail = (*node)->prev);
|
||||
|
||||
if (mempool->freeList.head != NULL) mempool->freeList.head->prev = NULL;
|
||||
else mempool->freeList.tail = NULL;
|
||||
|
||||
if (mempool->freeList.tail != NULL) mempool->freeList.tail->next = NULL;
|
||||
mempool->freeList.len--;
|
||||
node = &mempool->freeList.head;
|
||||
}
|
||||
else if (((uintptr_t)*node + (*node)->size) == (uintptr_t)(*node)->next)
|
||||
{
|
||||
// Next node is at a higher address.
|
||||
(*node)->size += (*node)->next->size;
|
||||
(*node)->next->size = 0UL;
|
||||
|
||||
// <-[P Curr N]-> <-[P Next N]-> <-[P NextNext N]->
|
||||
//
|
||||
// |--------------------|
|
||||
// <-[P Curr N]-> <-[P Next N]-> [P NextNext N]->
|
||||
if ((*node)->next->next != NULL) (*node)->next->next->prev = *node;
|
||||
|
||||
// <-[P Curr N]-> <-[P NextNext N]->
|
||||
(*node)->next = (*node)->next->next;
|
||||
|
||||
mempool->freeList.len--;
|
||||
node = &mempool->freeList.head;
|
||||
}
|
||||
else if ((((uintptr_t)*node + (*node)->size) == (uintptr_t)(*node)->prev) && ((*node)->prev->prev != NULL))
|
||||
{
|
||||
// Prev node is at a higher address.
|
||||
(*node)->size += (*node)->prev->size;
|
||||
(*node)->prev->size = 0UL;
|
||||
|
||||
// <-[P PrevPrev N]-> <-[P Prev N]-> <-[P Curr N]->
|
||||
//
|
||||
// |--------------------|
|
||||
// <-[P PrevPrev N] <-[P Prev N]-> <-[P Curr N]->
|
||||
(*node)->prev->prev->next = *node;
|
||||
|
||||
// <-[P PrevPrev N]-> <-[P Curr N]->
|
||||
(*node)->prev = (*node)->prev->prev;
|
||||
|
||||
mempool->freeList.len--;
|
||||
node = &mempool->freeList.head;
|
||||
}
|
||||
else if ((*node)->prev != NULL && (*node)->next != NULL && (uintptr_t)*node - (*node)->next->size == (uintptr_t)(*node)->next)
|
||||
{
|
||||
// Next node is at a lower address.
|
||||
(*node)->next->size += (*node)->size;
|
||||
|
||||
(*node)->size = 0UL;
|
||||
(*node)->next->prev = (*node)->prev;
|
||||
(*node)->prev->next = (*node)->next;
|
||||
*node = (*node)->next;
|
||||
|
||||
mempool->freeList.len--;
|
||||
node = &mempool->freeList.head;
|
||||
}
|
||||
else if ((*node)->prev != NULL && (*node)->next != NULL && (uintptr_t)*node - (*node)->prev->size == (uintptr_t)(*node)->prev)
|
||||
{
|
||||
// Prev node is at a lower address.
|
||||
(*node)->prev->size += (*node)->size;
|
||||
|
||||
(*node)->size = 0UL;
|
||||
(*node)->next->prev = (*node)->prev;
|
||||
(*node)->prev->next = (*node)->next;
|
||||
*node = (*node)->prev;
|
||||
|
||||
mempool->freeList.len--;
|
||||
node = &mempool->freeList.head;
|
||||
}
|
||||
else
|
||||
{
|
||||
node = &(*node)->next;
|
||||
}
|
||||
}
|
||||
|
||||
return PRE_DEFRAG_LEN > mempool->freeList.len;
|
||||
}
|
||||
mempool->buckets[i].head = mempool->buckets[i].tail = NULL;
|
||||
mempool->buckets[i].len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ToggleMemPoolAutoDefrag(MemPool *const mempool)
|
||||
{
|
||||
if (mempool == NULL) return;
|
||||
else mempool->freeList.autoDefrag ^= true;
|
||||
mempool->arena.offs = mempool->arena.mem + mempool->arena.size;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition - Object Pool
|
||||
//----------------------------------------------------------------------------------
|
||||
union ObjInfo {
|
||||
uint8_t *const byte;
|
||||
size_t *const index;
|
||||
};
|
||||
|
||||
ObjPool CreateObjPool(const size_t objsize, const size_t len)
|
||||
{
|
||||
ObjPool objpool = { 0 };
|
||||
|
||||
if ((len == 0UL) || (objsize == 0UL)) return objpool;
|
||||
if ((len == 0) || (objsize == 0)) return objpool;
|
||||
else
|
||||
{
|
||||
objpool.objSize = __AlignSize(objsize, sizeof(size_t));
|
||||
objpool.stack.size = objpool.freeBlocks = len;
|
||||
objpool.stack.mem = calloc(objpool.stack.size, objpool.objSize);
|
||||
|
||||
if (objpool.stack.mem == NULL)
|
||||
const size_t aligned_size = __AlignSize(objsize, sizeof(size_t));
|
||||
uint8_t *const restrict buf = calloc(len, aligned_size);
|
||||
if (buf == NULL) return objpool;
|
||||
objpool.objSize = aligned_size;
|
||||
objpool.memSize = objpool.freeBlocks = len;
|
||||
objpool.mem = ( uintptr_t )buf;
|
||||
|
||||
for (size_t i=0; i<objpool.freeBlocks; i++)
|
||||
{
|
||||
objpool.stack.size = 0UL;
|
||||
return objpool;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < objpool.freeBlocks; i++)
|
||||
{
|
||||
union ObjInfo block = { .byte = &objpool.stack.mem[i*objpool.objSize] };
|
||||
*block.index = i + 1;
|
||||
}
|
||||
|
||||
objpool.stack.base = objpool.stack.mem;
|
||||
return objpool;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ObjPool CreateObjPoolFromBuffer(void *const buf, const size_t objsize, const size_t len)
|
||||
{
|
||||
ObjPool objpool = { 0 };
|
||||
|
||||
// If the object size isn't large enough to align to a size_t, then we can't use it.
|
||||
if ((buf == NULL) || (len == 0UL) || (objsize < sizeof(size_t)) || (objsize*len != __AlignSize(objsize, sizeof(size_t))*len)) return objpool;
|
||||
else
|
||||
{
|
||||
objpool.objSize = __AlignSize(objsize, sizeof(size_t));
|
||||
objpool.stack.size = objpool.freeBlocks = len;
|
||||
objpool.stack.mem = buf;
|
||||
|
||||
for (int i = 0; i < objpool.freeBlocks; i++)
|
||||
{
|
||||
union ObjInfo block = { .byte = &objpool.stack.mem[i*objpool.objSize] };
|
||||
*block.index = i + 1;
|
||||
size_t *const restrict index = ( size_t* )(objpool.mem + (i*aligned_size));
|
||||
*index = i + 1;
|
||||
}
|
||||
|
||||
objpool.stack.base = objpool.stack.mem;
|
||||
objpool.offs = objpool.mem;
|
||||
return objpool;
|
||||
}
|
||||
}
|
||||
|
||||
void DestroyObjPool(ObjPool *const objpool)
|
||||
ObjPool CreateObjPoolFromBuffer(void *const restrict buf, const size_t objsize, const size_t len)
|
||||
{
|
||||
if ((objpool == NULL) || (objpool->stack.mem == NULL)) return;
|
||||
ObjPool objpool = { 0 };
|
||||
|
||||
// If the object size isn't large enough to align to a size_t, then we can't use it.
|
||||
const size_t aligned_size = __AlignSize(objsize, sizeof(size_t));
|
||||
if ((buf == NULL) || (len == 0) || (objsize < sizeof(size_t)) || (objsize*len != aligned_size*len)) return objpool;
|
||||
else
|
||||
{
|
||||
free(objpool->stack.mem);
|
||||
objpool.objSize = aligned_size;
|
||||
objpool.memSize = objpool.freeBlocks = len;
|
||||
objpool.mem = (uintptr_t)buf;
|
||||
|
||||
for (size_t i=0; i<objpool.freeBlocks; i++)
|
||||
{
|
||||
size_t *const restrict index = ( size_t* )(objpool.mem + (i*aligned_size));
|
||||
*index = i + 1;
|
||||
}
|
||||
|
||||
objpool.offs = objpool.mem;
|
||||
return objpool;
|
||||
}
|
||||
}
|
||||
|
||||
void DestroyObjPool(ObjPool *const restrict objpool)
|
||||
{
|
||||
if (objpool->mem == 0) return;
|
||||
else
|
||||
{
|
||||
void *const restrict ptr = ( void* )objpool->mem;
|
||||
free(ptr);
|
||||
*objpool = (ObjPool){0};
|
||||
}
|
||||
}
|
||||
|
||||
void *ObjPoolAlloc(ObjPool *const objpool)
|
||||
{
|
||||
if (objpool == NULL) return NULL;
|
||||
else
|
||||
if (objpool->freeBlocks > 0)
|
||||
{
|
||||
if (objpool->freeBlocks > 0UL)
|
||||
{
|
||||
// For first allocation, head points to the very first index.
|
||||
// Head = &pool[0];
|
||||
// ret = Head == ret = &pool[0];
|
||||
union ObjInfo ret = { .byte = objpool->stack.base };
|
||||
objpool->freeBlocks--;
|
||||
// For first allocation, head points to the very first index.
|
||||
// Head = &pool[0];
|
||||
// ret = Head == ret = &pool[0];
|
||||
size_t *const restrict block = ( size_t* )objpool->offs;
|
||||
objpool->freeBlocks--;
|
||||
|
||||
// after allocating, we set head to the address of the index that *Head holds.
|
||||
// Head = &pool[*Head * pool.objsize];
|
||||
objpool->stack.base = (objpool->freeBlocks != 0UL)? objpool->stack.mem + (*ret.index*objpool->objSize) : NULL;
|
||||
memset(ret.byte, 0, objpool->objSize);
|
||||
|
||||
return ret.byte;
|
||||
}
|
||||
else return NULL;
|
||||
// after allocating, we set head to the address of the index that *Head holds.
|
||||
// Head = &pool[*Head * pool.objsize];
|
||||
objpool->offs = (objpool->freeBlocks != 0)? objpool->mem + (*block*objpool->objSize) : 0;
|
||||
return memset(block, 0, objpool->objSize);
|
||||
}
|
||||
else return NULL;
|
||||
}
|
||||
|
||||
void ObjPoolFree(ObjPool *const restrict objpool, void *ptr)
|
||||
void ObjPoolFree(ObjPool *const restrict objpool, void *const ptr)
|
||||
{
|
||||
union ObjInfo p = { .byte = ptr };
|
||||
if ((objpool == NULL) || (ptr == NULL) || (p.byte < objpool->stack.mem) || (p.byte > objpool->stack.mem + objpool->stack.size*objpool->objSize)) return;
|
||||
uintptr_t block = (uintptr_t)ptr;
|
||||
if ((ptr == NULL) || (block < objpool->mem) || (block > objpool->mem + objpool->memSize*objpool->objSize)) return;
|
||||
else
|
||||
{
|
||||
// When we free our pointer, we recycle the pointer space to store the previous index and then we push it as our new head.
|
||||
// *p = index of Head in relation to the buffer;
|
||||
// Head = p;
|
||||
*p.index = (objpool->stack.base != NULL)? (objpool->stack.base - objpool->stack.mem)/objpool->objSize : objpool->stack.size;
|
||||
objpool->stack.base = p.byte;
|
||||
size_t *const restrict index = ( size_t* )block;
|
||||
*index = (objpool->offs != 0)? (objpool->offs - objpool->mem)/objpool->objSize : objpool->memSize;
|
||||
objpool->offs = block;
|
||||
objpool->freeBlocks++;
|
||||
}
|
||||
}
|
||||
|
||||
void ObjPoolCleanUp(ObjPool *const restrict objpool, void **ptrref)
|
||||
void ObjPoolCleanUp(ObjPool *const restrict objpool, void **const restrict ptrref)
|
||||
{
|
||||
if ((objpool == NULL) || (ptrref == NULL) || (*ptrref == NULL)) return;
|
||||
if (ptrref == NULL) return;
|
||||
else
|
||||
{
|
||||
ObjPoolFree(objpool, *ptrref);
|
||||
|
|
@ -694,71 +632,85 @@ void ObjPoolCleanUp(ObjPool *const restrict objpool, void **ptrref)
|
|||
BiStack CreateBiStack(const size_t len)
|
||||
{
|
||||
BiStack destack = { 0 };
|
||||
if (len == 0UL) return destack;
|
||||
|
||||
if (len == 0) return destack;
|
||||
|
||||
uint8_t *const buf = malloc(len*sizeof *buf);
|
||||
if (buf==NULL) return destack;
|
||||
destack.size = len;
|
||||
destack.mem = malloc(len*sizeof *destack.mem);
|
||||
if (destack.mem==NULL) destack.size = 0UL;
|
||||
else
|
||||
{
|
||||
destack.front = destack.mem;
|
||||
destack.back = destack.mem + len;
|
||||
}
|
||||
destack.mem = ( uintptr_t )buf;
|
||||
destack.front = destack.mem;
|
||||
destack.back = destack.mem + len;
|
||||
return destack;
|
||||
}
|
||||
|
||||
BiStack CreateBiStackFromBuffer(void *const buf, const size_t len)
|
||||
{
|
||||
BiStack destack = { 0 };
|
||||
if (len == 0UL || buf == NULL) return destack;
|
||||
destack.size = len;
|
||||
destack.mem = destack.front = buf;
|
||||
destack.back = destack.mem + len;
|
||||
return destack;
|
||||
if (len == 0 || buf == NULL) return destack;
|
||||
else
|
||||
{
|
||||
destack.size = len;
|
||||
destack.mem = destack.front = ( uintptr_t )buf;
|
||||
destack.back = destack.mem + len;
|
||||
return destack;
|
||||
}
|
||||
}
|
||||
|
||||
void DestroyBiStack(BiStack *const destack)
|
||||
void DestroyBiStack(BiStack *const restrict destack)
|
||||
{
|
||||
if ((destack == NULL) || (destack->mem == NULL)) return;
|
||||
free(destack->mem);
|
||||
*destack = (BiStack){0};
|
||||
if (destack->mem == 0) return;
|
||||
else
|
||||
{
|
||||
uint8_t *const restrict buf = ( uint8_t* )destack->mem;
|
||||
free(buf);
|
||||
*destack = (BiStack){0};
|
||||
}
|
||||
}
|
||||
|
||||
void *BiStackAllocFront(BiStack *const destack, const size_t len)
|
||||
void *BiStackAllocFront(BiStack *const restrict destack, const size_t len)
|
||||
{
|
||||
if ((destack == NULL) || (destack->mem == NULL)) return NULL;
|
||||
|
||||
const size_t ALIGNED_LEN = __AlignSize(len, sizeof(uintptr_t));
|
||||
// front end stack is too high!
|
||||
if (destack->front + ALIGNED_LEN >= destack->back) return NULL;
|
||||
|
||||
uint8_t *ptr = destack->front;
|
||||
destack->front += ALIGNED_LEN;
|
||||
return ptr;
|
||||
if (destack->mem == 0) return NULL;
|
||||
else
|
||||
{
|
||||
const size_t ALIGNED_LEN = __AlignSize(len, sizeof(uintptr_t));
|
||||
// front end arena is too high!
|
||||
if (destack->front + ALIGNED_LEN >= destack->back) return NULL;
|
||||
else
|
||||
{
|
||||
uint8_t *const restrict ptr = ( uint8_t* )destack->front;
|
||||
destack->front += ALIGNED_LEN;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void *BiStackAllocBack(BiStack *const destack, const size_t len)
|
||||
void *BiStackAllocBack(BiStack *const restrict destack, const size_t len)
|
||||
{
|
||||
if ((destack == NULL) || (destack->mem == NULL)) return NULL;
|
||||
|
||||
const size_t ALIGNED_LEN = __AlignSize(len, sizeof(uintptr_t));
|
||||
// back end stack is too low
|
||||
if (destack->back - ALIGNED_LEN <= destack->front) return NULL;
|
||||
|
||||
destack->back -= ALIGNED_LEN;
|
||||
return destack->back;
|
||||
if (destack->mem == 0) return NULL;
|
||||
else
|
||||
{
|
||||
const size_t ALIGNED_LEN = __AlignSize(len, sizeof(uintptr_t));
|
||||
// back end arena is too low
|
||||
if (destack->back - ALIGNED_LEN <= destack->front) return NULL;
|
||||
else
|
||||
{
|
||||
destack->back -= ALIGNED_LEN;
|
||||
uint8_t *const restrict ptr = ( uint8_t* )destack->back;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BiStackResetFront(BiStack *const destack)
|
||||
{
|
||||
if ((destack == NULL) || (destack->mem == NULL)) return;
|
||||
destack->front = destack->mem;
|
||||
if (destack->mem == 0) return;
|
||||
else destack->front = destack->mem;
|
||||
}
|
||||
|
||||
void BiStackResetBack(BiStack *const destack)
|
||||
{
|
||||
if ((destack == NULL) || (destack->mem == NULL)) return;
|
||||
destack->back = destack->mem + destack->size;
|
||||
if (destack->mem == 0) return;
|
||||
else destack->back = destack->mem + destack->size;
|
||||
}
|
||||
|
||||
void BiStackResetAll(BiStack *const destack)
|
||||
|
|
@ -767,9 +719,21 @@ void BiStackResetAll(BiStack *const destack)
|
|||
BiStackResetFront(destack);
|
||||
}
|
||||
|
||||
intptr_t BiStackMargins(const BiStack destack)
|
||||
inline intptr_t BiStackMargins(const BiStack destack)
|
||||
{
|
||||
return destack.back - destack.front;
|
||||
}
|
||||
|
||||
#endif // RMEM_IMPLEMENTATION
|
||||
|
||||
/*******
|
||||
* Changelog
|
||||
* v1.0: First Creation.
|
||||
* v1.1: bug patches for the mempool and addition of object pool.
|
||||
* v1.2: addition of bidirectional arena.
|
||||
* v1.3:
|
||||
* optimizations of allocators.
|
||||
* renamed 'Stack' to 'Arena'.
|
||||
* replaced certain define constants with an anonymous enum.
|
||||
* refactored MemPool to no longer require active or deferred defragging.
|
||||
********/
|
||||
|
|
|
|||
|
|
@ -1298,7 +1298,7 @@ void DrawTriangleStrip(Vector2 *points, int pointsCount, Color color)
|
|||
{
|
||||
if (pointsCount >= 3)
|
||||
{
|
||||
if (rlCheckBufferLimit(pointsCount)) rlglDraw();
|
||||
if (rlCheckBufferLimit(3*(pointsCount - 2))) rlglDraw();
|
||||
|
||||
rlBegin(RL_TRIANGLES);
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
|
|
|||
73
src/utils.c
73
src/utils.c
|
|
@ -71,13 +71,6 @@ static AAssetManager *assetManager = NULL; // Android assets manage
|
|||
static const char *internalDataPath = NULL; // Android internal data path
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
static int UWPOutMessageId = -1; // Last index of output message
|
||||
static UWPMessage *UWPOutMessages[MAX_UWP_MESSAGES]; // Messages out to UWP
|
||||
static int UWPInMessageId = -1; // Last index of input message
|
||||
static UWPMessage *UWPInMessages[MAX_UWP_MESSAGES]; // Messages in from UWP
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
@ -394,69 +387,3 @@ static int android_close(void *cookie)
|
|||
return 0;
|
||||
}
|
||||
#endif // PLATFORM_ANDROID
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
UWPMessage *CreateUWPMessage(void)
|
||||
{
|
||||
UWPMessage *msg = (UWPMessage *)RL_MALLOC(sizeof(UWPMessage));
|
||||
msg->type = UWP_MSG_NONE;
|
||||
Vector2 v0 = { 0, 0 };
|
||||
msg->paramVector0 = v0;
|
||||
msg->paramInt0 = 0;
|
||||
msg->paramInt1 = 0;
|
||||
msg->paramChar0 = 0;
|
||||
msg->paramFloat0 = 0;
|
||||
msg->paramDouble0 = 0;
|
||||
msg->paramBool0 = false;
|
||||
return msg;
|
||||
}
|
||||
|
||||
void DeleteUWPMessage(UWPMessage *msg)
|
||||
{
|
||||
RL_FREE(msg);
|
||||
}
|
||||
|
||||
bool UWPHasMessages(void)
|
||||
{
|
||||
return (UWPOutMessageId > -1);
|
||||
}
|
||||
|
||||
UWPMessage *UWPGetMessage(void)
|
||||
{
|
||||
if (UWPHasMessages()) return UWPOutMessages[UWPOutMessageId--];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void UWPSendMessage(UWPMessage *msg)
|
||||
{
|
||||
if ((UWPInMessageId + 1) < MAX_UWP_MESSAGES)
|
||||
{
|
||||
UWPInMessageId++;
|
||||
UWPInMessages[UWPInMessageId] = msg;
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "UWP: Not enough array space to register new inbound message");
|
||||
}
|
||||
|
||||
void SendMessageToUWP(UWPMessage *msg)
|
||||
{
|
||||
if ((UWPOutMessageId + 1) < MAX_UWP_MESSAGES)
|
||||
{
|
||||
UWPOutMessageId++;
|
||||
UWPOutMessages[UWPOutMessageId] = msg;
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "UWP: Not enough array space to register new outward message");
|
||||
}
|
||||
|
||||
bool HasMessageFromUWP(void)
|
||||
{
|
||||
return UWPInMessageId > -1;
|
||||
}
|
||||
|
||||
UWPMessage *GetMessageFromUWP(void)
|
||||
{
|
||||
if (HasMessageFromUWP()) return UWPInMessages[UWPInMessageId--];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
#endif // PLATFORM_UWP
|
||||
|
|
|
|||
59
src/utils.h
59
src/utils.h
|
|
@ -72,65 +72,6 @@ void InitAssetManager(AAssetManager *manager, const char *dataPath); // Initia
|
|||
FILE *android_fopen(const char *fileName, const char *mode); // Replacement for fopen() -> Read-only!
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
// UWP Messages System
|
||||
typedef enum {
|
||||
UWP_MSG_NONE = 0,
|
||||
|
||||
// Send
|
||||
UWP_MSG_SHOW_MOUSE,
|
||||
UWP_MSG_HIDE_MOUSE,
|
||||
UWP_MSG_LOCK_MOUSE,
|
||||
UWP_MSG_UNLOCK_MOUSE,
|
||||
UWP_MSG_SET_MOUSE_LOCATION, // paramVector0 (pos)
|
||||
|
||||
// Receive (Into C)
|
||||
UWP_MSG_REGISTER_KEY, // paramInt0 (key), paramChar0 (status)
|
||||
UWP_MSG_REGISTER_CLICK, // paramInt0 (button), paramChar0 (status)
|
||||
UWP_MSG_SCROLL_WHEEL_UPDATE, // paramInt0 (delta)
|
||||
UWP_MSG_UPDATE_MOUSE_LOCATION, // paramVector0 (pos)
|
||||
UWP_MSG_SET_GAMEPAD_ACTIVE, // paramInt0 (gamepad), paramBool0 (active or not)
|
||||
UWP_MSG_SET_GAMEPAD_BUTTON, // paramInt0 (gamepad), paramInt1 (button), paramChar0 (status)
|
||||
UWP_MSG_SET_GAMEPAD_AXIS, // paramInt0 (gamepad), int1 (axis), paramFloat0 (value)
|
||||
UWP_MSG_SET_DISPLAY_DIMS, // paramVector0 (display dimensions)
|
||||
UWP_MSG_HANDLE_RESIZE, // paramVector0 (new dimensions) - Onresized event
|
||||
UWP_MSG_SET_GAME_TIME, // paramInt0
|
||||
} UWPMessageType;
|
||||
|
||||
typedef struct UWPMessage {
|
||||
UWPMessageType type; // Message type
|
||||
|
||||
Vector2 paramVector0; // Vector parameters
|
||||
int paramInt0; // Int parameter
|
||||
int paramInt1; // Int parameter
|
||||
char paramChar0; // Char parameters
|
||||
float paramFloat0; // Float parameters
|
||||
double paramDouble0; // Double parameters
|
||||
bool paramBool0; // Bool parameters
|
||||
|
||||
// More parameters can be added and fed to functions
|
||||
} UWPMessage;
|
||||
|
||||
// Allocate UWP Message
|
||||
RLAPI UWPMessage* CreateUWPMessage(void);
|
||||
|
||||
// Free UWP Message
|
||||
RLAPI void DeleteUWPMessage(UWPMessage* msg);
|
||||
|
||||
// Get messages into C++
|
||||
RLAPI bool UWPHasMessages(void);
|
||||
RLAPI UWPMessage* UWPGetMessage(void);
|
||||
RLAPI void UWPSendMessage(UWPMessage* msg);
|
||||
|
||||
// For C to call
|
||||
#ifndef __cplusplus // Hide from C++ code
|
||||
void SendMessageToUWP(UWPMessage* msg);
|
||||
bool HasMessageFromUWP(void);
|
||||
UWPMessage* GetMessageFromUWP(void);
|
||||
#endif
|
||||
|
||||
#endif //defined(PLATFORM_UWP)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
119
src/uwp_events.h
Normal file
119
src/uwp_events.h
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/**********************************************************************************************
|
||||
*
|
||||
* raylib.uwp_events - Functions for bootstrapping UWP functionality within raylib's core.
|
||||
*
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2020-2020 Reece Mackie (@Rover656)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
||||
* wrote the original software. If you use this software in a product, an acknowledgment
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
#ifndef UWP_EVENTS_H
|
||||
#define UWP_EVENTS_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_UWP)
|
||||
|
||||
// Determine if UWP functions are set and ready for raylib's use.
|
||||
bool UWPIsConfigured();
|
||||
|
||||
// Call this to set the UWP data path you wish for saving and loading.
|
||||
void UWPSetDataPath(const char* path);
|
||||
|
||||
// Function for getting program time.
|
||||
typedef double(*UWPQueryTimeFunc)();
|
||||
UWPQueryTimeFunc UWPGetQueryTimeFunc(void);
|
||||
void UWPSetQueryTimeFunc(UWPQueryTimeFunc func);
|
||||
|
||||
// Function for sleeping the current thread
|
||||
typedef void (*UWPSleepFunc)(double sleepUntil);
|
||||
UWPSleepFunc UWPGetSleepFunc(void);
|
||||
void UWPSetSleepFunc(UWPSleepFunc func);
|
||||
|
||||
// Function for querying the display size
|
||||
typedef void(*UWPDisplaySizeFunc)(int* width, int* height);
|
||||
UWPDisplaySizeFunc UWPGetDisplaySizeFunc(void);
|
||||
void UWPSetDisplaySizeFunc(UWPDisplaySizeFunc func);
|
||||
|
||||
// Functions for mouse cursor control
|
||||
typedef void(*UWPMouseFunc)(void);
|
||||
UWPMouseFunc UWPGetMouseLockFunc();
|
||||
void UWPSetMouseLockFunc(UWPMouseFunc func);
|
||||
UWPMouseFunc UWPGetMouseUnlockFunc();
|
||||
void UWPSetMouseUnlockFunc(UWPMouseFunc func);
|
||||
UWPMouseFunc UWPGetMouseShowFunc();
|
||||
void UWPSetMouseShowFunc(UWPMouseFunc func);
|
||||
UWPMouseFunc UWPGetMouseHideFunc();
|
||||
void UWPSetMouseHideFunc(UWPMouseFunc func);
|
||||
|
||||
// Function for setting mouse cursor position.
|
||||
typedef void (*UWPMouseSetPosFunc)(int x, int y);
|
||||
UWPMouseSetPosFunc UWPGetMouseSetPosFunc();
|
||||
void UWPSetMouseSetPosFunc(UWPMouseSetPosFunc func);
|
||||
|
||||
// The below functions are implemented in core.c but are placed here so they can be called by user code.
|
||||
// This choice is made as platform-specific code is preferred to be kept away from raylib.h
|
||||
|
||||
// Call this when a Key is pressed or released.
|
||||
void UWPKeyDownEvent(int key, bool down, bool controlKey);
|
||||
|
||||
// Call this on the CoreWindow::CharacterRecieved event
|
||||
void UWPKeyCharEvent(int key);
|
||||
|
||||
// Call when a mouse button state changes
|
||||
void UWPMouseButtonEvent(int button, bool down);
|
||||
|
||||
// Call when the mouse cursor moves
|
||||
void UWPMousePosEvent(double x, double y);
|
||||
|
||||
// Call when the mouse wheel moves
|
||||
void UWPMouseWheelEvent(int deltaY);
|
||||
|
||||
// Call when the window resizes
|
||||
void UWPResizeEvent(int width, int height);
|
||||
|
||||
// Call when a gamepad is made active
|
||||
void UWPActivateGamepadEvent(int gamepad, bool active);
|
||||
|
||||
// Call when a gamepad button state changes
|
||||
void UWPRegisterGamepadButton(int gamepad, int button, bool down);
|
||||
|
||||
// Call when a gamepad axis state changes
|
||||
void UWPRegisterGamepadAxis(int gamepad, int axis, float value);
|
||||
|
||||
// Call when the touch point moves
|
||||
void UWPGestureMove(int pointer, float x, float y);
|
||||
|
||||
// Call when there is a touch down or up
|
||||
void UWPGestureTouch(int pointer, float x, float y, bool touch);
|
||||
|
||||
// Set the core window pointer so that we can pass it to EGL.
|
||||
void* UWPGetCoreWindowPtr();
|
||||
void UWPSetCoreWindowPtr(void* ptr);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // PLATFORM_UWP
|
||||
|
||||
#endif // UWP_EVENTS_H
|
||||
Loading…
Reference in a new issue