| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | #if defined(_WIN32) |
| 47 | #include <conio.h> |
| 48 | #else |
| 49 | |
| 50 | #include <termios.h> |
| 51 | #include <unistd.h> |
| 52 | #include <fcntl.h> |
| 53 | #endif |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | #if defined(_WIN32) |
| 60 | typedef 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 | |
| 65 | typedef struct { |
| 66 | unsigned int *pixels; |
| 67 | #if defined(_WIN32) |
| 68 | LARGE_INTEGER timerFrequency; |
| 69 | #endif |
| 70 | } PlatformData; |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | extern CoreData CORE; |
| 76 | |
| 77 | static PlatformData platform = { 0 }; |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | int InitPlatform(void); |
| 83 | bool InitGraphicsDevice(void); |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | #if !defined(_WIN32) |
| 94 | static int kbhit(void); |
| 95 | static char getch(void) { return getchar(); } |
| 96 | #endif |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | bool WindowShouldClose(void) |
| 104 | { |
| 105 | if (CORE.Window.ready) return CORE.Window.shouldClose; |
| 106 | else return true; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void ToggleFullscreen(void) |
| 111 | { |
| 112 | TRACELOG(LOG_WARNING, "ToggleFullscreen() not available on target platform"); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void ToggleBorderlessWindowed(void) |
| 117 | { |
| 118 | TRACELOG(LOG_WARNING, "ToggleBorderlessWindowed() not available on target platform"); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | void MaximizeWindow(void) |
| 123 | { |
| 124 | TRACELOG(LOG_WARNING, "MaximizeWindow() not available on target platform"); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | void MinimizeWindow(void) |
| 129 | { |
| 130 | TRACELOG(LOG_WARNING, "MinimizeWindow() not available on target platform"); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void RestoreWindow(void) |
| 135 | { |
| 136 | TRACELOG(LOG_WARNING, "RestoreWindow() not available on target platform"); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void SetWindowState(unsigned int flags) |
| 141 | { |
| 142 | TRACELOG(LOG_WARNING, "SetWindowState() not available on target platform"); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void ClearWindowState(unsigned int flags) |
| 147 | { |
| 148 | TRACELOG(LOG_WARNING, "ClearWindowState() not available on target platform"); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void SetWindowIcon(Image image) |
| 153 | { |
| 154 | TRACELOG(LOG_WARNING, "SetWindowIcon() not available on target platform"); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | void SetWindowIcons(Image *images, int count) |
| 159 | { |
| 160 | TRACELOG(LOG_WARNING, "SetWindowIcons() not available on target platform"); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | void SetWindowTitle(const char *title) |
| 165 | { |
| 166 | CORE.Window.title = title; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void SetWindowPosition(int x, int y) |
| 171 | { |
| 172 | TRACELOG(LOG_WARNING, "SetWindowPosition() not available on target platform"); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | void SetWindowMonitor(int monitor) |
| 177 | { |
| 178 | TRACELOG(LOG_WARNING, "SetWindowMonitor() not available on target platform"); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | void SetWindowMinSize(int width, int height) |
| 183 | { |
| 184 | CORE.Window.screenMin.width = width; |
| 185 | CORE.Window.screenMin.height = height; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void SetWindowMaxSize(int width, int height) |
| 190 | { |
| 191 | CORE.Window.screenMax.width = width; |
| 192 | CORE.Window.screenMax.height = height; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void SetWindowSize(int width, int height) |
| 197 | { |
| 198 | TRACELOG(LOG_WARNING, "SetWindowSize() not available on target platform"); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | void SetWindowOpacity(float opacity) |
| 203 | { |
| 204 | TRACELOG(LOG_WARNING, "SetWindowOpacity() not available on target platform"); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | void SetWindowFocused(void) |
| 209 | { |
| 210 | TRACELOG(LOG_WARNING, "SetWindowFocused() not available on target platform"); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | void *GetWindowHandle(void) |
| 215 | { |
| 216 | TRACELOG(LOG_WARNING, "GetWindowHandle() not implemented on target platform"); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | int GetMonitorCount(void) |
| 222 | { |
| 223 | TRACELOG(LOG_WARNING, "GetMonitorCount() not implemented on target platform"); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | int GetCurrentMonitor(void) |
| 229 | { |
| 230 | TRACELOG(LOG_WARNING, "GetCurrentMonitor() not implemented on target platform"); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | Vector2 GetMonitorPosition(int monitor) |
| 236 | { |
| 237 | TRACELOG(LOG_WARNING, "GetMonitorPosition() not implemented on target platform"); |
| 238 | return (Vector2){ 0, 0 }; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | int GetMonitorWidth(int monitor) |
| 243 | { |
| 244 | TRACELOG(LOG_WARNING, "GetMonitorWidth() not implemented on target platform"); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | int GetMonitorHeight(int monitor) |
| 250 | { |
| 251 | TRACELOG(LOG_WARNING, "GetMonitorHeight() not implemented on target platform"); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | int GetMonitorPhysicalWidth(int monitor) |
| 257 | { |
| 258 | TRACELOG(LOG_WARNING, "GetMonitorPhysicalWidth() not implemented on target platform"); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | int GetMonitorPhysicalHeight(int monitor) |
| 264 | { |
| 265 | TRACELOG(LOG_WARNING, "GetMonitorPhysicalHeight() not implemented on target platform"); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | int GetMonitorRefreshRate(int monitor) |
| 271 | { |
| 272 | TRACELOG(LOG_WARNING, "GetMonitorRefreshRate() not implemented on target platform"); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | const char *GetMonitorName(int monitor) |
| 278 | { |
| 279 | TRACELOG(LOG_WARNING, "GetMonitorName() not implemented on target platform"); |
| 280 | return ""; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | Vector2 GetWindowPosition(void) |
| 285 | { |
| 286 | TRACELOG(LOG_WARNING, "GetWindowPosition() not implemented on target platform"); |
| 287 | return (Vector2){ 0, 0 }; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | Vector2 GetWindowScaleDPI(void) |
| 292 | { |
| 293 | TRACELOG(LOG_WARNING, "GetWindowScaleDPI() not implemented on target platform"); |
| 294 | return (Vector2){ 1.0f, 1.0f }; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | void SetClipboardText(const char *text) |
| 299 | { |
| 300 | TRACELOG(LOG_WARNING, "SetClipboardText() not implemented on target platform"); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | const char *GetClipboardText(void) |
| 306 | { |
| 307 | TRACELOG(LOG_WARNING, "GetClipboardText() not implemented on target platform"); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | Image 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 | |
| 322 | void ShowCursor(void) |
| 323 | { |
| 324 | CORE.Input.Mouse.cursorHidden = false; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | void HideCursor(void) |
| 329 | { |
| 330 | CORE.Input.Mouse.cursorHidden = true; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | void EnableCursor(void) |
| 335 | { |
| 336 | |
| 337 | SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); |
| 338 | |
| 339 | CORE.Input.Mouse.cursorHidden = false; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | void DisableCursor(void) |
| 344 | { |
| 345 | |
| 346 | SetMousePosition(CORE.Window.screen.width/2, CORE.Window.screen.height/2); |
| 347 | |
| 348 | CORE.Input.Mouse.cursorHidden = true; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | void SwapScreenBuffer(void) |
| 353 | { |
| 354 | |
| 355 | rlCopyFramebuffer(0, 0, CORE.Window.render.width, CORE.Window.render.height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, platform.pixels); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | double 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; |
| 375 | #endif |
| 376 | return time; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | void OpenURL(const char *url) |
| 385 | { |
| 386 | |
| 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 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | int SetGamepadMappings(const char *mappings) |
| 404 | { |
| 405 | TRACELOG(LOG_WARNING, "SetGamepadMappings() not implemented on target platform"); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | void SetGamepadVibration(int gamepad, float leftMotor, float rightMotor, float duration) |
| 411 | { |
| 412 | TRACELOG(LOG_WARNING, "SetGamepadVibration() not implemented on target platform"); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | void 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 | |
| 423 | void SetMouseCursor(int cursor) |
| 424 | { |
| 425 | TRACELOG(LOG_WARNING, "SetMouseCursor() not implemented on target platform"); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | const char *GetKeyName(int key) |
| 430 | { |
| 431 | TRACELOG(LOG_WARNING, "GetKeyName() not implemented on target platform"); |
| 432 | return ""; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | void PollInputEvents(void) |
| 437 | { |
| 438 | #if SUPPORT_GESTURES_SYSTEM |
| 439 | |
| 440 | |
| 441 | UpdateGestures(); |
| 442 | #endif |
| 443 | |
| 444 | |
| 445 | CORE.Input.Keyboard.keyPressedQueueCount = 0; |
| 446 | CORE.Input.Keyboard.charPressedQueueCount = 0; |
| 447 | |
| 448 | |
| 449 | for (int i = 0; i < MAX_KEYBOARD_KEYS; i++) CORE.Input.Keyboard.keyRepeatInFrame[i] = 0; |
| 450 | |
| 451 | |
| 452 | CORE.Input.Gamepad.lastButtonPressed = 0; |
| 453 | |
| 454 | |
| 455 | |
| 456 | for (int i = 0; i < MAX_TOUCH_POINTS; i++) CORE.Input.Touch.previousTouchState[i] = CORE.Input.Touch.currentTouchState[i]; |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 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 | |
| 472 | |
| 473 | |
| 474 | if (kbhit()) |
| 475 | { |
| 476 | int key = getch(); |
| 477 | if (key == 27) CORE.Window.shouldClose = true; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | int InitPlatform(void) |
| 487 | { |
| 488 | |
| 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 | |
| 498 | platform.pixels = (unsigned int *)RL_CALLOC(CORE.Window.screen.width*CORE.Window.screen.height, sizeof(int)); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | |
| 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 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 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 | |
| 542 | |
| 543 | CORE.Storage.basePath = GetWorkingDirectory(); |
| 544 | |
| 545 | |
| 546 | TRACELOG(LOG_INFO, "PLATFORM: MEMORY: Initialized successfully"); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | void ClosePlatform(void) |
| 553 | { |
| 554 | RL_FREE(platform.pixels); |
| 555 | } |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | #if !defined(_WIN32) |
| 561 | |
| 562 | static 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 | |