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