Logo

index : raylib-jai

Bindings from https://solarium.technology

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/raylib-jai.git/html/Raylib/raylib/src/platforms/rcore_memory.c blob: 1b7a55fd84a5c0583620fdabd9c4b5914fd8446e [raw] [clear marker]

        
0/**********************************************************************************************
1*
2* rcore_memory - Functions to manage window, graphics device and inputs
3*
4* PLATFORM: MEMORY (No OS)
5* - Memory framebuffer output (no os)
6*
7* LIMITATIONS:
8* - Software renderer (rlsw)
9* - No input system
10*
11* POSSIBLE IMPROVEMENTS:
12* - Improvement 01
13* - Improvement 02
14*
15* ADDITIONAL NOTES:
16* - TRACELOG() function is located in raylib [utils] module
17*
18* CONFIGURATION:
19* #define RCORE_PLATFORM_CUSTOM_FLAG
20* Custom flag for rcore on target platform -not used-
21*
22* DEPENDENCIES:
23* - rlsw: Software renderer
24* - gestures: Gestures system for touch-ready devices (or simulated from mouse inputs)
25*
26*
27* LICENSE: zlib/libpng
28*
29* Copyright (c) 2025 Ramon Santamaria (@raysan5) and contributors
30*
31* This software is provided "as-is", without any express or implied warranty. In no event
32* will the authors be held liable for any damages arising from the use of this software.
33*
34* Permission is granted to anyone to use this software for any purpose, including commercial
35* applications, and to alter it and redistribute it freely, subject to the following restrictions:
36*
37* 1. The origin of this software must not be misrepresented; you must not claim that you
38* wrote the original software. If you use this software in a product, an acknowledgment
39* in the product documentation would be appreciated but is not required.
40*
41* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
42* as being the original software.
43*
44* 3. This notice may not be removed or altered from any source distribution.
45*
46**********************************************************************************************/
47
48#if defined(_WIN32)
49 #include <conio.h> // Required for: kbhit()
50#else
51 // Provide kbhit() function in non-Windows platforms
52 #include <termios.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55#endif
56
57//----------------------------------------------------------------------------------
58// Types and Structures Definition
59//----------------------------------------------------------------------------------
60// Platform-specific required data for timming (Win32)
61#if defined(_WIN32)
62typedef struct _LARGE_INTEGER { int64_t QuadPart; } LARGE_INTEGER;
63__declspec(dllimport) int __stdcall QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);
64__declspec(dllimport) int __stdcall QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
65#endif
66
67typedef struct {
68 unsigned int *pixels; // Pointer to pixel data buffer (RGBA8888 format)
69#if defined(_WIN32)
70 LARGE_INTEGER timerFrequency;
71#endif
72} PlatformData;
73
74//----------------------------------------------------------------------------------
75// Global Variables Definition
76//----------------------------------------------------------------------------------
77extern CoreData CORE; // Global CORE state context
78
79static PlatformData platform = { 0 }; // Platform specific data
80
81//----------------------------------------------------------------------------------
82// Module Internal Functions Declaration
83//----------------------------------------------------------------------------------
84int InitPlatform(void); // Initialize platform (graphics, inputs and more)
85bool InitGraphicsDevice(void); // Initialize graphics device
86
87//----------------------------------------------------------------------------------
88// Module Functions Declaration
89//----------------------------------------------------------------------------------
90// NOTE: Functions declaration is provided by raylib.h
91
92//----------------------------------------------------------------------------------
93// Module Internal Functions Declaration
94//----------------------------------------------------------------------------------
95#if !defined(_WIN32)
96static int kbhit(void); // Check if a key has been pressed
97static char getch(void) { return getchar(); } // Get pressed character
98#endif
99
100//----------------------------------------------------------------------------------
101// Module Functions Definition: Window and Graphics Device
102//----------------------------------------------------------------------------------
103
104// Check if application should close
105bool WindowShouldClose(void)
106{
107 if (CORE.Window.ready) return CORE.Window.shouldClose;
108 else return true;
109}
110
111// Toggle fullscreen mode
112void ToggleFullscreen(void)
113{
114 TRACELOG(LOG_WARNING, "ToggleFullscreen() not available on target platform");
115}
116
117// Toggle borderless windowed mode
118void ToggleBorderlessWindowed(void)
119{
120 TRACELOG(LOG_WARNING, "ToggleBorderlessWindowed() not available on target platform");
121}
122
123// Set window state: maximized, if resizable
124void MaximizeWindow(void)
125{
126 TRACELOG(LOG_WARNING, "MaximizeWindow() not available on target platform");
127}
128
129// Set window state: minimized
130void MinimizeWindow(void)
131{
132 TRACELOG(LOG_WARNING, "MinimizeWindow() not available on target platform");
133}
134
135// Restore window from being minimized/maximized
136void RestoreWindow(void)
137{
138 TRACELOG(LOG_WARNING, "RestoreWindow() not available on target platform");
139}
140
141// Set window configuration state using flags
142void SetWindowState(unsigned int flags)
143{
144 TRACELOG(LOG_WARNING, "SetWindowState() not available on target platform");
145}
146
147// Clear window configuration state flags
148void ClearWindowState(unsigned int flags)
149{
150 TRACELOG(LOG_WARNING, "ClearWindowState() not available on target platform");
151}
152
153// Set icon for window
154void SetWindowIcon(Image image)
155{
156 TRACELOG(LOG_WARNING, "SetWindowIcon() not available on target platform");
157}
158
159// Set icon for window
160void SetWindowIcons(Image *images, int count)
161{
162 TRACELOG(LOG_WARNING, "SetWindowIcons() not available on target platform");
163}
164
165// Set title for window
166void SetWindowTitle(const char *title)
167{
168 CORE.Window.title = title;
169}
170
171// Set window position on screen (windowed mode)
172void SetWindowPosition(int x, int y)
173{
174 TRACELOG(LOG_WARNING, "SetWindowPosition() not available on target platform");
175}
176
177// Set monitor for the current window
178void SetWindowMonitor(int monitor)
179{
180 TRACELOG(LOG_WARNING, "SetWindowMonitor() not available on target platform");
181}
182
183// Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
184void SetWindowMinSize(int width, int height)
185{
186 CORE.Window.screenMin.width = width;
187 CORE.Window.screenMin.height = height;
188}
189
190// Set window maximum dimensions (FLAG_WINDOW_RESIZABLE)
191void SetWindowMaxSize(int width, int height)
192{
193 CORE.Window.screenMax.width = width;
194 CORE.Window.screenMax.height = height;
195}
196
197// Set window dimensions
198void SetWindowSize(int width, int height)
199{
200 TRACELOG(LOG_WARNING, "SetWindowSize() not available on target platform");
201}
202
203// Set window opacity, value opacity is between 0.0 and 1.0
204void SetWindowOpacity(float opacity)
205{
206 TRACELOG(LOG_WARNING, "SetWindowOpacity() not available on target platform");
207}
208
209// Set window focused
210void SetWindowFocused(void)
211{
212 TRACELOG(LOG_WARNING, "SetWindowFocused() not available on target platform");
213}
214
215// Get native window handle
216void *GetWindowHandle(void)
217{
218 TRACELOG(LOG_WARNING, "GetWindowHandle() not implemented on target platform");
219 return NULL;
220}
221
222// Get number of monitors
223int GetMonitorCount(void)
224{
225 TRACELOG(LOG_WARNING, "GetMonitorCount() not implemented on target platform");
226 return 1;
227}
228
229// Get current monitor where window is placed
230int GetCurrentMonitor(void)
231{
232 TRACELOG(LOG_WARNING, "GetCurrentMonitor() not implemented on target platform");
233 return 0;
234}
235
236// Get selected monitor position
237Vector2 GetMonitorPosition(int monitor)
238{
239 TRACELOG(LOG_WARNING, "GetMonitorPosition() not implemented on target platform");
240 return (Vector2){ 0, 0 };
241}
242
243// Get selected monitor width (currently used by monitor)
244int GetMonitorWidth(int monitor)
245{
246 TRACELOG(LOG_WARNING, "GetMonitorWidth() not implemented on target platform");
247 return 0;
248}
249
250// Get selected monitor height (currently used by monitor)
251int GetMonitorHeight(int monitor)
252{
253 TRACELOG(LOG_WARNING, "GetMonitorHeight() not implemented on target platform");
254 return 0;
255}
256
257// Get selected monitor physical width in millimetres
258int GetMonitorPhysicalWidth(int monitor)
259{
260 TRACELOG(LOG_WARNING, "GetMonitorPhysicalWidth() not implemented on target platform");
261 return 0;
262}
263
264// Get selected monitor physical height in millimetres
265int GetMonitorPhysicalHeight(int monitor)
266{
267 TRACELOG(LOG_WARNING, "GetMonitorPhysicalHeight() not implemented on target platform");
268 return 0;
269}
270
271// Get selected monitor refresh rate
272int GetMonitorRefreshRate(int monitor)
273{
274 TRACELOG(LOG_WARNING, "GetMonitorRefreshRate() not implemented on target platform");
275 return 0;
276}
277
278// Get the human-readable, UTF-8 encoded name of the selected monitor
279const char *GetMonitorName(int monitor)
280{
281 TRACELOG(LOG_WARNING, "GetMonitorName() not implemented on target platform");
282 return "";
283}
284
285// Get window position XY on monitor
286Vector2 GetWindowPosition(void)
287{
288 TRACELOG(LOG_WARNING, "GetWindowPosition() not implemented on target platform");
289 return (Vector2){ 0, 0 };
290}
291
292// Get window scale DPI factor for current monitor
293Vector2 GetWindowScaleDPI(void)
294{
295 TRACELOG(LOG_WARNING, "GetWindowScaleDPI() not implemented on target platform");
296 return (Vector2){ 1.0f, 1.0f };
297}
298
299// Set clipboard text content
300void SetClipboardText(const char *text)
301{
302 TRACELOG(LOG_WARNING, "SetClipboardText() not implemented on target platform");
303}
304
305// Get clipboard text content
306// NOTE: returned string is allocated and freed by GLFW
307const char *GetClipboardText(void)
308{
309 TRACELOG(LOG_WARNING, "GetClipboardText() not implemented on target platform");
310 return NULL;
311}
312
313// Get clipboard image
314Image GetClipboardImage(void)
315{
316 Image image = { 0 };
317
318 TRACELOG(LOG_WARNING, "GetClipboardImage() not implemented on target platform");
319
320 return image;
321}
322
323// Show mouse cursor
324void ShowCursor(void)
325{
326 CORE.Input.Mouse.cursorHidden = false;
327}
328
329// Hides mouse cursor
330void HideCursor(void)
331{
332 CORE.Input.Mouse.cursorHidden = true;
333}
334
335// Enables cursor (unlock cursor)
336void EnableCursor(void)
337{
338 // Set cursor position in the middle
339 SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
340
341 CORE.Input.Mouse.cursorHidden = false;
342}
343
344// Disables cursor (lock cursor)
345void DisableCursor(void)
346{
347 // Set cursor position in the middle
348 SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2);
349
350 CORE.Input.Mouse.cursorHidden = true;
351}
352
353// Swap back buffer with front buffer (screen drawing)
354void SwapScreenBuffer(void)
355{
356 // Update framebuffer
357 rlCopyFramebuffer(0, 0, CORE.Window.render.width, CORE.Window.render.height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, platform.pixels);
358}
359
360//----------------------------------------------------------------------------------
361// Module Functions Definition: Misc
362//----------------------------------------------------------------------------------
363
364// Get elapsed time measure in seconds since InitTimer()
365double GetTime(void)
366{
367 double time = 0.0;
368#if defined(_WIN32)
369 LARGE_INTEGER now = { 0 };
370 QueryPerformanceCounter(&now);
371 return (double)(now.QuadPart - CORE.Time.base)/(double)platform.timerFrequency.QuadPart;
372#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__EMSCRIPTEN__)
373 struct timespec ts = { 0 };
374 clock_gettime(CLOCK_MONOTONIC, &ts);
375 unsigned long long int nanoSeconds = (unsigned long long int)ts.tv_sec*1000000000LLU + (unsigned long long int)ts.tv_nsec;
376 time = (double)(nanoSeconds - CORE.Time.base)*1e-9; // Elapsed time since InitTimer()
377#endif
378 return time;
379}
380
381// Open URL with default system browser (if available)
382// NOTE: This function is only safe to use if you control the URL given.
383// A user could craft a malicious string performing another action.
384// Only call this function yourself not with user input or make sure to check the string yourself.
385// REF: https://github.com/raysan5/raylib/issues/686
386void OpenURL(const char *url)
387{
388 // Security check to (partially) avoid malicious code on target platform
389 if (strchr(url, '\'') != NULL) TRACELOG(LOG_WARNING, "SYSTEM: Provided URL could be potentially malicious, avoid [\'] character");
390 else
391 {
392 char *cmd = (char *)RL_CALLOC(strlen(url) + 32, sizeof(char));
393 sprintf(cmd, "explorer \"%s\"", url);
394 int result = system(cmd);
395 if (result == -1) TRACELOG(LOG_WARNING, "OpenURL() child process could not be created");
396 RL_FREE(cmd);
397 }
398}
399
400//----------------------------------------------------------------------------------
401// Module Functions Definition: Inputs
402//----------------------------------------------------------------------------------
403
404// Set internal gamepad mappings
405int SetGamepadMappings(const char *mappings)
406{
407 TRACELOG(LOG_WARNING, "SetGamepadMappings() not implemented on target platform");
408 return 0;
409}
410
411// Set gamepad vibration
412void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration)
413{
414 TRACELOG(LOG_WARNING, "SetGamepadVibration() not implemented on target platform");
415}
416
417// Set mouse position XY
418void SetMousePosition(int x, int y)
419{
420 CORE.Input.Mouse.currentPosition = (Vector2){ (float)x, (float)y };
421 CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
422}
423
424// Set mouse cursor
425void SetMouseCursor(int cursor)
426{
427 TRACELOG(LOG_WARNING, "SetMouseCursor() not implemented on target platform");
428}
429
430// Get physical key name
431const char *GetKeyName(int key)
432{
433 TRACELOG(LOG_WARNING, "GetKeyName() not implemented on target platform");
434 return "";
435}
436
437// Register all input events
438void PollInputEvents(void)
439{
440#if defined(SUPPORT_GESTURES_SYSTEM)
441 // NOTE: Gestures update must be called every frame to reset gestures correctly
442 // because ProcessGestureEvent() is just called on an event, not every frame
443 UpdateGestures();
444#endif
445
446 // Reset keys/chars pressed registered
447 CORE.Input.Keyboard.keyPressedQueueCount = 0;
448 CORE.Input.Keyboard.charPressedQueueCount = 0;
449
450 // Reset key repeats
451 for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
452
453 // Reset last gamepad button/axis registered state
454 CORE.Input.Gamepad.lastButtonPressed = 0; // GAMEPAD_BUTTON_UNKNOWN
455 //CORE.Input.Gamepad.axisCount = 0;
456
457 // Register previous touch states
458 for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i];
459
460 // Reset touch positions
461 // TODO: It resets on target platform the mouse position and not filled again until a move-event,
462 // so, if mouse is not moved it returns a (0, 0) position... this behaviour should be reviewed!
463 //for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.position[i] = (Vector2){ 0, 0 };
464
465 // Register previous keys states
466 // NOTE: Android supports up to 260 keys
467 for (int i = 0; i < 260; i++)
468 {
469 CORE.Input.Keyboard.previousKeyState[i] = CORE.Input.Keyboard.currentKeyState[i];
470 CORE.Input.Keyboard.keyRepeatInFrame[i] = 0;
471 }
472
473 // TODO: Poll input events for current platform
474
475 // Check for key pressed to exit
476 if (kbhit())
477 {
478 int key = getch();
479 if (key == 27) CORE.Window.shouldClose = true; // KEY_SCAPE
480 }
481}
482
483//----------------------------------------------------------------------------------
484// Module Internal Functions Definition
485//----------------------------------------------------------------------------------
486
487// Initialize platform: graphics, inputs and more
488int InitPlatform(void)
489{
490 // Memory framebuffer can only work with software renderer
491 if (rlGetVersion() != RL_OPENGL_11_SOFTWARE)
492 {
493 TRACELOG(LOG_WARNING, "DISPLAY: Memory platform requires software renderer (GRAPHICS_API_OPENGL_11_SOFTWARE)");
494 TRACELOG(LOG_FATAL, "PLATFORM: Failed to initialize graphics device");
495 return -1;
496 }
497 else
498 {
499 // Load memory framebuffer with desired screen size
500 platform.pixels = (unsigned int *)RL_CALLOC(CORE.Window.screen.width*CORE.Window.screen.height, sizeof(int));
501 }
502 //----------------------------------------------------------------------------
503
504 // If everything work as expected, we can continue
505 CORE.Window.render.width = CORE.Window.screen.width;
506 CORE.Window.render.height = CORE.Window.screen.height;
507 CORE.Window.currentFbo.width = CORE.Window.render.width;
508 CORE.Window.currentFbo.height = CORE.Window.render.height;
509
510 TRACELOG(LOG_INFO, "DISPLAY: Device initialized successfully");
511 TRACELOG(LOG_INFO, " > Display size: %i x %i", CORE.Window.display.width, CORE.Window.display.height);
512 TRACELOG(LOG_INFO, " > Screen size: %i x %i", CORE.Window.screen.width, CORE.Window.screen.height);
513 TRACELOG(LOG_INFO, " > Render size: %i x %i", CORE.Window.render.width, CORE.Window.render.height);
514 TRACELOG(LOG_INFO, " > Viewport offsets: %i, %i", CORE.Window.renderOffset.x, CORE.Window.renderOffset.y);
515
516 CORE.Window.ready = true;
517
518 // TODO: Load OpenGL extensions
519 // NOTE: GL procedures address loader is required to load extensions
520 //----------------------------------------------------------------------------
521 // ...
522 //----------------------------------------------------------------------------
523
524 // TODO: Initialize input events system
525 // It could imply keyboard, mouse, gamepad, touch...
526 // Depending on the platform libraries/SDK it could use a callback mechanism
527 // For system events and inputs evens polling on a per-frame basis, use PollInputEvents()
528 //----------------------------------------------------------------------------
529 // ...
530 //----------------------------------------------------------------------------
531
532 // Initialize timing system
533 //----------------------------------------------------------------------------
534#if defined(_WIN32)
535 LARGE_INTEGER time = { 0 };
536 QueryPerformanceCounter(&time);
537 QueryPerformanceFrequency(&platform.timerFrequency);
538 CORE.Time.base = time.QuadPart;
539#endif
540 InitTimer();
541 //----------------------------------------------------------------------------
542
543 // Initialize storage system
544 //----------------------------------------------------------------------------
545 CORE.Storage.basePath = GetWorkingDirectory();
546 //----------------------------------------------------------------------------
547
548 TRACELOG(LOG_INFO, "PLATFORM: MEMORY: Initialized successfully");
549
550 return 0;
551}
552
553// Close platform
554void ClosePlatform(void)
555{
556 RL_FREE(platform.pixels);
557}
558
559//----------------------------------------------------------------------------------
560// Module Internal Functions Definition
561//----------------------------------------------------------------------------------
562#if !defined(_WIN32)
563// Check if a key has been pressed
564static int kbhit(void)
565{
566 struct termios oldt = { 0 };
567 struct termios newt = { 0 };
568 int ch = 0;
569 int oldf = 0;
570
571 tcgetattr(STDIN_FILENO, &oldt);
572 newt = oldt;
573 newt.c_lflag &= ~(ICANON | ECHO);
574 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
575 oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
576 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
577
578 ch = getchar();
579
580 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
581 fcntl(STDIN_FILENO, F_SETFL, oldf);
582
583 if (ch != EOF)
584 {
585 ungetc(ch, stdin);
586 return 1;
587 }
588
589 return 0;
590}
591#endif
592
593// EOF
594
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit