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