2014-09-17 00:51:31 +04:00
|
|
|
/**********************************************************************************************
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* raylib.core - Basic functions to manage windows, OpenGL context and input on multiple platforms
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
2017-12-05 16:01:35 +03:00
|
|
|
* PLATFORMS SUPPORTED:
|
|
|
|
* PLATFORM_DESKTOP: Windows (Win32, Win64)
|
|
|
|
* PLATFORM_DESKTOP: Linux (32 and 64 bit)
|
|
|
|
* PLATFORM_DESKTOP: OSX/macOS
|
|
|
|
* PLATFORM_DESKTOP: FreeBSD
|
|
|
|
* PLATFORM_ANDROID: Android (ARM, ARM64)
|
|
|
|
* PLATFORM_RPI: Raspberry Pi (Raspbian)
|
|
|
|
* PLATFORM_WEB: HTML5 (Chrome, Firefox)
|
|
|
|
* PLATFORM_UWP: Universal Windows Platform
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* CONFIGURATION:
|
|
|
|
*
|
|
|
|
* #define PLATFORM_DESKTOP
|
2017-12-05 16:01:35 +03:00
|
|
|
* Windowing and input system configured for desktop platforms: Windows, Linux, OSX, FreeBSD
|
2017-02-16 02:50:02 +03:00
|
|
|
* NOTE: Oculus Rift CV1 requires PLATFORM_DESKTOP for mirror rendering - View [rlgl] module to enable it
|
|
|
|
*
|
|
|
|
* #define PLATFORM_ANDROID
|
|
|
|
* Windowing and input system configured for Android device, app activity managed internally in this module.
|
|
|
|
* NOTE: OpenGL ES 2.0 is required and graphic device is managed by EGL
|
|
|
|
*
|
|
|
|
* #define PLATFORM_RPI
|
2017-12-05 16:01:35 +03:00
|
|
|
* Windowing and input system configured for Raspberry Pi i native mode (no X.org required, tested on Raspbian),
|
|
|
|
* graphic device is managed by EGL and inputs are processed is raw mode, reading from /dev/input/
|
2017-02-16 02:50:02 +03:00
|
|
|
*
|
|
|
|
* #define PLATFORM_WEB
|
|
|
|
* Windowing and input system configured for HTML5 (run on browser), code converted from C to asm.js
|
|
|
|
* using emscripten compiler. OpenGL ES 2.0 required for direct translation to WebGL equivalent code.
|
|
|
|
*
|
2017-03-21 15:22:59 +03:00
|
|
|
* #define SUPPORT_DEFAULT_FONT (default)
|
2017-02-16 02:50:02 +03:00
|
|
|
* Default font is loaded on window initialization to be available for the user to render simple text.
|
|
|
|
* NOTE: If enabled, uses external module functions to load default raylib font (module: text)
|
|
|
|
*
|
2017-03-21 15:22:59 +03:00
|
|
|
* #define SUPPORT_CAMERA_SYSTEM
|
|
|
|
* Camera module is included (camera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital
|
2017-02-16 02:50:02 +03:00
|
|
|
*
|
2017-03-21 15:22:59 +03:00
|
|
|
* #define SUPPORT_GESTURES_SYSTEM
|
|
|
|
* Gestures module is included (gestures.h) to support gestures detection: tap, hold, swipe, drag
|
2017-02-16 02:50:02 +03:00
|
|
|
*
|
|
|
|
* #define SUPPORT_MOUSE_GESTURES
|
|
|
|
* Mouse gestures are directly mapped like touches and processed by gestures system.
|
|
|
|
*
|
2017-05-08 03:37:37 +03:00
|
|
|
* #define SUPPORT_BUSY_WAIT_LOOP
|
|
|
|
* Use busy wait loop for timming sync, if not defined, a high-resolution timer is setup and used
|
|
|
|
*
|
2017-05-18 19:57:11 +03:00
|
|
|
* #define SUPPORT_GIF_RECORDING
|
|
|
|
* Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback()
|
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* DEPENDENCIES:
|
2017-12-05 16:01:35 +03:00
|
|
|
* rglfw - Manage graphic device, OpenGL context and inputs on PLATFORM_DESKTOP (Windows, Linux, OSX. FreeBSD)
|
|
|
|
* raymath - 3D math functionality (Vector2, Vector3, Matrix, Quaternion)
|
2017-01-29 01:02:30 +03:00
|
|
|
* camera - Multiple 3D camera modes (free, orbital, 1st person, 3rd person)
|
2016-11-16 20:46:13 +03:00
|
|
|
* gestures - Gestures system for touch-ready devices (or simulated from mouse inputs)
|
2014-09-17 00:51:31 +04:00
|
|
|
*
|
2016-11-16 20:46:13 +03:00
|
|
|
*
|
2017-02-16 02:50:02 +03:00
|
|
|
* LICENSE: zlib/libpng
|
2016-11-16 20:46:13 +03:00
|
|
|
*
|
2017-12-05 16:01:35 +03:00
|
|
|
* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
|
2014-09-03 18:51:28 +04:00
|
|
|
*
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
2013-11-23 16:30:54 +04:00
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
2013-11-23 16:30:54 +04:00
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2014-09-03 18:51:28 +04:00
|
|
|
* 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
|
2013-11-23 16:30:54 +04:00
|
|
|
* in the product documentation would be appreciated but is not required.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
2013-11-23 16:30:54 +04:00
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
2013-11-19 02:38:44 +04:00
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
2017-03-25 14:01:01 +03:00
|
|
|
// Default configuration flags (supported features)
|
|
|
|
//-------------------------------------------------
|
2017-03-21 15:22:59 +03:00
|
|
|
#define SUPPORT_DEFAULT_FONT
|
|
|
|
#define SUPPORT_MOUSE_GESTURES
|
|
|
|
#define SUPPORT_CAMERA_SYSTEM
|
2017-11-03 14:41:03 +03:00
|
|
|
#define SUPPORT_GESTURES_SYSTEM
|
2017-05-08 03:37:37 +03:00
|
|
|
#define SUPPORT_BUSY_WAIT_LOOP
|
2017-05-18 19:57:11 +03:00
|
|
|
#define SUPPORT_GIF_RECORDING
|
2017-03-25 14:01:01 +03:00
|
|
|
//-------------------------------------------------
|
2017-03-21 15:22:59 +03:00
|
|
|
|
2016-12-17 21:05:40 +03:00
|
|
|
#include "raylib.h"
|
|
|
|
|
2017-12-10 23:11:04 +03:00
|
|
|
#if (defined(__linux__) || defined(PLATFORM_WEB)) && _POSIX_C_SOURCE < 199309L
|
2017-11-23 00:47:57 +03:00
|
|
|
#undef _POSIX_C_SOURCE
|
|
|
|
#define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext.
|
|
|
|
#endif
|
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2
|
2017-04-21 01:08:00 +03:00
|
|
|
#include "utils.h" // Required for: fopen() Android mapping
|
2016-08-06 17:32:46 +03:00
|
|
|
|
2016-01-20 20:20:05 +03:00
|
|
|
#define RAYMATH_IMPLEMENTATION // Use raymath as a header-only library (includes implementation)
|
|
|
|
#define RAYMATH_EXTERN_INLINE // Compile raymath functions as static inline (remember, it's a compiler hint)
|
2016-12-17 21:05:40 +03:00
|
|
|
#include "raymath.h" // Required for: Vector3 and Matrix functions
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_GESTURES_SYSTEM)
|
|
|
|
#define GESTURES_IMPLEMENTATION
|
|
|
|
#include "gestures.h" // Gestures detection functionality
|
|
|
|
#endif
|
2016-08-06 17:32:46 +03:00
|
|
|
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_CAMERA_SYSTEM) && !defined(PLATFORM_ANDROID)
|
2016-08-26 20:40:37 +03:00
|
|
|
#define CAMERA_IMPLEMENTATION
|
2017-05-18 19:57:11 +03:00
|
|
|
#include "camera.h" // Camera system functionality
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SUPPORT_GIF_RECORDING)
|
2017-10-02 14:06:04 +03:00
|
|
|
#define RGIF_IMPLEMENTATION
|
|
|
|
#include "external/rgif.h" // Support GIF recording
|
2016-08-26 20:40:37 +03:00
|
|
|
#endif
|
2016-08-10 13:55:54 +03:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
#include <stdio.h> // Standard input / output lib
|
2016-11-01 16:39:57 +03:00
|
|
|
#include <stdlib.h> // Required for: malloc(), free(), rand(), atexit()
|
|
|
|
#include <stdint.h> // Required for: typedef unsigned long long int uint64_t, used by hi-res timer
|
|
|
|
#include <time.h> // Required for: time() - Android/RPI hi-res timer (NOTE: Linux only!)
|
|
|
|
#include <math.h> // Required for: tan() [Used in Begin3dMode() to set perspective]
|
2017-03-29 01:35:42 +03:00
|
|
|
#include <string.h> // Required for: strrchr(), strcmp()
|
2016-11-01 16:39:57 +03:00
|
|
|
//#include <errno.h> // Macros for reporting and retrieving error conditions through error codes
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2017-05-11 17:24:40 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <direct.h> // Required for: _getch(), _chdir()
|
|
|
|
#define GETCWD _getcwd // NOTE: MSDN recommends not to use getcwd(), chdir()
|
|
|
|
#define CHDIR _chdir
|
|
|
|
#else
|
|
|
|
#include "unistd.h" // Required for: getch(), chdir() (POSIX)
|
|
|
|
#define GETCWD getcwd
|
|
|
|
#define CHDIR chdir
|
|
|
|
#endif
|
|
|
|
|
2017-05-08 03:37:37 +03:00
|
|
|
#if defined(__linux__) || defined(PLATFORM_WEB)
|
2017-01-28 02:56:45 +03:00
|
|
|
#include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
|
2017-05-08 03:37:37 +03:00
|
|
|
#elif defined(__APPLE__)
|
2017-03-05 21:17:00 +03:00
|
|
|
#include <unistd.h> // Required for: usleep()
|
2017-01-28 02:56:45 +03:00
|
|
|
#endif
|
|
|
|
|
2016-02-03 21:00:56 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2017-01-28 02:56:45 +03:00
|
|
|
//#define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3
|
|
|
|
#include <GLFW/glfw3.h> // GLFW3 library: Windows, OpenGL context and Input management
|
2016-01-21 14:24:35 +03:00
|
|
|
|
2017-05-08 03:47:44 +03:00
|
|
|
#if defined(__linux__)
|
2016-02-03 21:00:56 +03:00
|
|
|
#define GLFW_EXPOSE_NATIVE_X11 // Linux specific definitions for getting
|
|
|
|
#define GLFW_EXPOSE_NATIVE_GLX // native functions like glfwGetX11Window
|
|
|
|
#include <GLFW/glfw3native.h> // which are required for hiding mouse
|
2015-02-03 05:47:28 +03:00
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
//#include <GL/gl.h> // OpenGL functions (GLFW3 already includes gl.h)
|
|
|
|
//#define GLFW_DLL // Using GLFW DLL on Windows -> No, we use static version!
|
2017-05-08 03:37:37 +03:00
|
|
|
|
|
|
|
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32)
|
2017-12-15 15:44:31 +03:00
|
|
|
__stdcall unsigned int timeBeginPeriod(unsigned int uPeriod);
|
|
|
|
__stdcall unsigned int timeEndPeriod(unsigned int uPeriod);
|
2017-05-08 03:37:37 +03:00
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
2016-10-18 01:15:23 +03:00
|
|
|
//#include <android/sensor.h> // Android sensors functions (accelerometer, gyroscope, light...)
|
2014-09-17 00:51:31 +04:00
|
|
|
#include <android/window.h> // Defines AWINDOW_FLAG_FULLSCREEN and others
|
2016-01-03 15:01:21 +03:00
|
|
|
#include <android_native_app_glue.h> // Defines basic app state struct and manages activity
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#include <EGL/egl.h> // Khronos EGL library - Native platform display device control functions
|
|
|
|
#include <GLES2/gl2.h> // Khronos OpenGL ES 2.0 library
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
#include <fcntl.h> // POSIX file control definitions - open(), creat(), fcntl()
|
|
|
|
#include <unistd.h> // POSIX standard function definitions - read(), close(), STDIN_FILENO
|
|
|
|
#include <termios.h> // POSIX terminal control definitions - tcgetattr(), tcsetattr()
|
|
|
|
#include <pthread.h> // POSIX threads management (mouse input)
|
|
|
|
|
|
|
|
#include <sys/ioctl.h> // UNIX System call for device-specific input/output operations - ioctl()
|
|
|
|
#include <linux/kd.h> // Linux: KDSKBMODE, K_MEDIUMRAM constants definition
|
|
|
|
#include <linux/input.h> // Linux: Keycodes constants definition (KEY_A, ...)
|
2016-11-02 15:39:48 +03:00
|
|
|
#include <linux/joystick.h> // Linux: Joystick support library
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#include "bcm_host.h" // Raspberry Pi VideoCore IV access functions
|
|
|
|
|
|
|
|
#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
|
|
|
|
#endif
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2015-10-30 13:30:32 +03:00
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#include <emscripten/html5.h>
|
|
|
|
#endif
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-02-19 02:16:16 +03:00
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
// Old device inputs system
|
2016-03-17 14:54:36 +03:00
|
|
|
#define DEFAULT_KEYBOARD_DEV STDIN_FILENO // Standard input
|
|
|
|
#define DEFAULT_MOUSE_DEV "/dev/input/mouse0" // Mouse input
|
2017-01-06 00:36:40 +03:00
|
|
|
#define DEFAULT_TOUCH_DEV "/dev/input/event4" // Touch input virtual device (created by ts_uinput)
|
2016-03-17 14:54:36 +03:00
|
|
|
#define DEFAULT_GAMEPAD_DEV "/dev/input/js" // Gamepad input (base dev for all gamepads: js0, js1, ...)
|
2016-02-19 02:16:16 +03:00
|
|
|
|
|
|
|
// New device input events (evdev) (must be detected)
|
|
|
|
//#define DEFAULT_KEYBOARD_DEV "/dev/input/eventN"
|
|
|
|
//#define DEFAULT_MOUSE_DEV "/dev/input/eventN"
|
|
|
|
//#define DEFAULT_GAMEPAD_DEV "/dev/input/eventN"
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
#define MOUSE_SENSITIVITY 0.8f
|
2016-02-19 02:16:16 +03:00
|
|
|
#endif
|
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
#define MAX_GAMEPADS 4 // Max number of gamepads supported
|
2016-10-17 19:18:13 +03:00
|
|
|
#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad)
|
2016-10-14 12:14:41 +03:00
|
|
|
#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad)
|
|
|
|
|
2017-03-21 15:22:59 +03:00
|
|
|
#define STORAGE_FILENAME "storage.data"
|
2016-10-09 14:09:08 +03:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-06-14 18:16:20 +03:00
|
|
|
// ...
|
2013-11-19 02:38:44 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2014-09-17 00:51:31 +04:00
|
|
|
static GLFWwindow *window; // Native window (graphic device)
|
2015-01-21 02:13:17 +03:00
|
|
|
static bool windowMinimized = false;
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
2014-09-17 00:51:31 +04:00
|
|
|
static struct android_app *app; // Android activity
|
|
|
|
static struct android_poll_source *source; // Android events polling source
|
2016-03-21 22:15:11 +03:00
|
|
|
static int ident, events; // Android ALooper_pollAll() variables
|
2016-05-31 01:01:19 +03:00
|
|
|
static const char *internalDataPath; // Android internal data path to write data (/data/data/<package>/files)
|
2016-03-21 22:15:11 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
static bool windowReady = false; // Used to detect display initialization
|
2016-01-03 15:01:21 +03:00
|
|
|
static bool appEnabled = true; // Used to detec if app is active
|
|
|
|
static bool contextRebindRequired = false; // Used to know context rebind required
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_RPI)
|
2014-09-17 00:51:31 +04:00
|
|
|
static EGL_DISPMANX_WINDOW_T nativeWindow; // Native window (graphic device)
|
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Keyboard input variables
|
2014-09-17 00:51:31 +04:00
|
|
|
// NOTE: For keyboard we will use the standard input (but reconfigured...)
|
2016-02-18 16:05:48 +03:00
|
|
|
static struct termios defaultKeyboardSettings; // Used to store default keyboard settings
|
2014-09-17 00:51:31 +04:00
|
|
|
static int defaultKeyboardMode; // Used to store default keyboard mode
|
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Mouse input variables
|
|
|
|
static int mouseStream = -1; // Mouse device file descriptor
|
|
|
|
static bool mouseReady = false; // Flag to know if mouse is ready
|
2016-10-14 12:14:41 +03:00
|
|
|
static pthread_t mouseThreadId; // Mouse reading thread id
|
2016-02-18 16:05:48 +03:00
|
|
|
|
2017-01-06 00:36:40 +03:00
|
|
|
// Touch input variables
|
|
|
|
static int touchStream = -1; // Touch device file descriptor
|
|
|
|
static bool touchReady = false; // Flag to know if touch interface is ready
|
|
|
|
static pthread_t touchThreadId; // Touch reading thread id
|
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Gamepad input variables
|
2016-10-14 12:14:41 +03:00
|
|
|
static int gamepadStream[MAX_GAMEPADS] = { -1 };// Gamepad device file descriptor
|
|
|
|
static pthread_t gamepadThreadId; // Gamepad reading thread id
|
2016-11-02 15:39:48 +03:00
|
|
|
static char gamepadName[64]; // Gamepad name holder
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2017-12-18 02:06:57 +03:00
|
|
|
static EGLDisplay display; // Native display device (physical screen connection)
|
|
|
|
static EGLSurface surface; // Surface to draw on, framebuffers (connected to context)
|
|
|
|
static EGLContext context; // Graphic context, mode in which drawing can be done
|
|
|
|
static EGLConfig config; // Graphic config
|
|
|
|
static uint64_t baseTime; // Base time measure for hi-res timer
|
|
|
|
static bool windowShouldClose = false; // Flag to set window for closing
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
|
|
|
|
2016-07-13 21:05:00 +03:00
|
|
|
// Display size-related data
|
|
|
|
static unsigned int displayWidth, displayHeight; // Display width and height (monitor, device-screen, LCD, ...)
|
2014-09-17 00:51:31 +04:00
|
|
|
static int screenWidth, screenHeight; // Screen width and height (used render area)
|
2016-07-13 21:05:00 +03:00
|
|
|
static int renderWidth, renderHeight; // Framebuffer width and height (render area, including black bars if required)
|
2014-09-17 00:51:31 +04:00
|
|
|
static int renderOffsetX = 0; // Offset X from render area (must be divided by 2)
|
|
|
|
static int renderOffsetY = 0; // Offset Y from render area (must be divided by 2)
|
|
|
|
static bool fullscreen = false; // Fullscreen mode (useful only for PLATFORM_DESKTOP)
|
|
|
|
static Matrix downscaleView; // Matrix to downscale view (in case screen size bigger than display size)
|
|
|
|
|
2017-11-12 13:45:35 +03:00
|
|
|
static bool cursorHidden = false; // Track if cursor is hidden
|
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
|
2017-11-12 13:45:35 +03:00
|
|
|
static const char *windowTitle = NULL; // Window text title...
|
2014-12-09 15:21:55 +03:00
|
|
|
static bool cursorOnScreen = false; // Tracks if cursor is inside client area
|
2017-05-11 17:24:40 +03:00
|
|
|
static int screenshotCounter = 0; // Screenshots counter
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
// Register mouse states
|
|
|
|
static char previousMouseState[3] = { 0 }; // Registers previous mouse button state
|
|
|
|
static char currentMouseState[3] = { 0 }; // Registers current mouse button state
|
|
|
|
static int previousMouseWheelY = 0; // Registers previous mouse wheel variation
|
|
|
|
static int currentMouseWheelY = 0; // Registers current mouse wheel variation
|
2013-11-28 22:59:56 +04:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
// Register gamepads states
|
|
|
|
static bool gamepadReady[MAX_GAMEPADS] = { false }; // Flag to know if gamepad is ready
|
|
|
|
static float gamepadAxisState[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis state
|
2016-10-29 23:16:54 +03:00
|
|
|
static char previousGamepadState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Previous gamepad buttons state
|
|
|
|
static char currentGamepadState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Current gamepad buttons state
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
// Keyboard configuration
|
2014-09-17 00:51:31 +04:00
|
|
|
static int exitKey = KEY_ESCAPE; // Default exit key (ESC)
|
2016-02-19 21:57:25 +03:00
|
|
|
#endif
|
2015-07-29 22:44:27 +03:00
|
|
|
|
2016-10-17 19:18:13 +03:00
|
|
|
// Register keyboard states
|
|
|
|
static char previousKeyState[512] = { 0 }; // Registers previous frame key state
|
|
|
|
static char currentKeyState[512] = { 0 }; // Registers current frame key state
|
|
|
|
|
|
|
|
static int lastKeyPressed = -1; // Register last key pressed
|
2016-10-27 14:41:43 +03:00
|
|
|
static int lastGamepadButtonPressed = -1; // Register last gamepad button pressed
|
2016-11-01 16:39:57 +03:00
|
|
|
static int gamepadAxisCount = 0; // Register number of available gamepad axis
|
2016-10-17 19:18:13 +03:00
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
static Vector2 mousePosition; // Mouse position on screen
|
2017-03-21 15:22:59 +03:00
|
|
|
|
2017-05-08 22:03:48 +03:00
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
static bool toggleCursorLock = false; // Ask for cursor pointer lock on next click
|
|
|
|
#endif
|
|
|
|
|
2016-07-13 21:05:00 +03:00
|
|
|
static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen
|
2016-04-17 12:19:32 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2015-07-29 22:44:27 +03:00
|
|
|
static char **dropFilesPath; // Store dropped files paths as strings
|
|
|
|
static int dropFilesCount = 0; // Count stored strings
|
2015-05-11 01:15:46 +03:00
|
|
|
#endif
|
2015-05-05 00:46:31 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
static double currentTime, previousTime; // Used to track timmings
|
|
|
|
static double updateTime, drawTime; // Time measures for update and draw
|
2017-01-28 02:56:45 +03:00
|
|
|
static double frameTime = 0.0; // Time measure for one frame
|
2014-09-17 00:51:31 +04:00
|
|
|
static double targetTime = 0.0; // Desired time for one frame, if 0 not applied
|
2014-01-29 00:21:29 +04:00
|
|
|
|
2016-07-13 21:05:00 +03:00
|
|
|
static char configFlags = 0; // Configuration flags (bit based)
|
2015-07-29 22:44:27 +03:00
|
|
|
static bool showLogo = false; // Track if showing logo at init is enabled
|
2014-07-23 02:06:24 +04:00
|
|
|
|
2017-05-18 19:57:11 +03:00
|
|
|
#if defined(SUPPORT_GIF_RECORDING)
|
|
|
|
static int gifFramesCounter = 0;
|
|
|
|
static bool gifRecording = false;
|
|
|
|
#endif
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Other Modules Functions Declaration (required by core)
|
|
|
|
//----------------------------------------------------------------------------------
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
2016-06-11 15:08:39 +03:00
|
|
|
extern void LoadDefaultFont(void); // [Module: text] Loads default font on InitWindow()
|
|
|
|
extern void UnloadDefaultFont(void); // [Module: text] Unloads default font from GPU memory
|
2016-10-09 14:09:08 +03:00
|
|
|
#endif
|
2016-08-06 17:32:46 +03:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
2016-06-25 22:28:50 +03:00
|
|
|
static void InitGraphicsDevice(int width, int height); // Initialize graphics device
|
2016-05-02 15:11:57 +03:00
|
|
|
static void SetupFramebufferSize(int displayWidth, int displayHeight);
|
2014-09-17 00:51:31 +04:00
|
|
|
static void InitTimer(void); // Initialize timer
|
2017-12-15 15:44:31 +03:00
|
|
|
static double GetTime(void); // Returns time since InitTimer() was run
|
2017-03-05 21:17:00 +03:00
|
|
|
static void Wait(float ms); // Wait for some milliseconds (stop program execution)
|
2014-09-17 00:51:31 +04:00
|
|
|
static bool GetKeyStatus(int key); // Returns if a key has been pressed
|
2017-03-09 18:25:15 +03:00
|
|
|
static bool GetMouseButtonStatus(int button); // Returns if a mouse button has been pressed
|
2014-09-17 00:51:31 +04:00
|
|
|
static void PollInputEvents(void); // Register user events
|
2016-05-02 15:11:57 +03:00
|
|
|
static void SwapBuffers(void); // Copy back buffer to front buffers
|
2014-09-17 00:51:31 +04:00
|
|
|
static void LogoAnimation(void); // Plays raylib logo appearing animation
|
2016-12-25 22:42:22 +03:00
|
|
|
static void SetupViewport(void); // Set viewport parameters
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2013-11-23 16:30:54 +04:00
|
|
|
static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
|
2014-09-17 00:51:31 +04:00
|
|
|
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); // GLFW3 Keyboard Callback, runs on key pressed
|
2014-12-31 20:03:32 +03:00
|
|
|
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods); // GLFW3 Mouse Button Callback, runs on mouse button pressed
|
2016-02-02 18:43:42 +03:00
|
|
|
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y); // GLFW3 Cursor Position Callback, runs on mouse move
|
2014-12-31 20:03:32 +03:00
|
|
|
static void CharCallback(GLFWwindow *window, unsigned int key); // GLFW3 Char Key Callback, runs on key pressed (get char value)
|
2014-09-17 00:51:31 +04:00
|
|
|
static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset); // GLFW3 Srolling Callback, runs on mouse wheel
|
|
|
|
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
|
2015-07-29 22:44:27 +03:00
|
|
|
static void WindowIconifyCallback(GLFWwindow *window, int iconified); // GLFW3 WindowIconify Callback, runs when window is minimized/restored
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths); // GLFW3 Window Drop Callback, runs when drop files into window
|
2014-12-15 03:08:30 +03:00
|
|
|
#endif
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2015-04-22 18:34:42 +03:00
|
|
|
static void AndroidCommandCallback(struct android_app *app, int32_t cmd); // Process Android activity lifecycle commands
|
2016-01-03 15:01:21 +03:00
|
|
|
static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event); // Process Android inputs
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2015-10-30 13:30:32 +03:00
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *e, void *userData);
|
2017-04-08 01:16:03 +03:00
|
|
|
static EM_BOOL EmscriptenKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData);
|
|
|
|
static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
|
|
|
|
static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData);
|
2016-12-25 22:42:22 +03:00
|
|
|
static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData);
|
2015-10-30 13:30:32 +03:00
|
|
|
#endif
|
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
static void InitKeyboard(void); // Init raw keyboard system (standard input reading)
|
|
|
|
static void ProcessKeyboard(void); // Process keyboard events
|
|
|
|
static void RestoreKeyboard(void); // Restore keyboard system
|
|
|
|
static void InitMouse(void); // Mouse initialization (including mouse thread)
|
|
|
|
static void *MouseThread(void *arg); // Mouse reading thread
|
2017-01-06 00:36:40 +03:00
|
|
|
static void InitTouch(void); // Touch device initialization (including touch thread)
|
|
|
|
static void *TouchThread(void *arg); // Touch device reading thread
|
2016-05-02 15:11:57 +03:00
|
|
|
static void InitGamepad(void); // Init raw gamepad input
|
|
|
|
static void *GamepadThread(void *arg); // Mouse reading thread
|
|
|
|
#endif
|
|
|
|
|
2017-01-28 02:56:45 +03:00
|
|
|
#if defined(_WIN32)
|
|
|
|
// NOTE: We include Sleep() function signature here to avoid windows.h inclusion
|
2017-03-05 21:17:00 +03:00
|
|
|
void __stdcall Sleep(unsigned long msTimeout); // Required for Wait()
|
2017-01-28 02:56:45 +03:00
|
|
|
#endif
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition - Window and OpenGL Context Functions
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI) || defined(PLATFORM_WEB)
|
2017-05-09 23:03:46 +03:00
|
|
|
// Initialize window and OpenGL context
|
2017-11-12 13:45:35 +03:00
|
|
|
// NOTE: data parameter could be used to pass any kind of required data to the initialization
|
2017-12-15 15:44:31 +03:00
|
|
|
void InitWindow(int width, int height, void *data)
|
2013-12-19 15:08:06 +04:00
|
|
|
{
|
2017-12-05 02:05:05 +03:00
|
|
|
TraceLog(LOG_INFO, "Initializing raylib (v1.9-dev)");
|
2015-02-02 02:57:08 +03:00
|
|
|
|
2017-11-12 13:45:35 +03:00
|
|
|
// Input data is window title char data
|
|
|
|
windowTitle = (char *)data;
|
2013-12-19 15:08:06 +04:00
|
|
|
|
2016-06-25 22:28:50 +03:00
|
|
|
// Init graphics device (display device and OpenGL context)
|
|
|
|
InitGraphicsDevice(width, height);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
2016-10-09 14:09:08 +03:00
|
|
|
// Load default font
|
2014-09-17 00:51:31 +04:00
|
|
|
// NOTE: External function (defined in module: text)
|
|
|
|
LoadDefaultFont();
|
2016-10-09 14:09:08 +03:00
|
|
|
#endif
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Init hi-res timer
|
|
|
|
InitTimer();
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
// Init raw input system
|
|
|
|
InitMouse(); // Mouse init
|
2017-01-06 00:36:40 +03:00
|
|
|
InitTouch(); // Touch init
|
2014-09-17 00:51:31 +04:00
|
|
|
InitKeyboard(); // Keyboard init
|
|
|
|
InitGamepad(); // Gamepad init
|
2014-03-25 15:40:35 +04:00
|
|
|
#endif
|
2015-04-22 18:34:42 +03:00
|
|
|
|
2015-10-30 13:30:32 +03:00
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
emscripten_set_fullscreenchange_callback(0, 0, 1, EmscriptenFullscreenChangeCallback);
|
2017-04-08 01:16:03 +03:00
|
|
|
|
|
|
|
// Support keyboard events
|
|
|
|
emscripten_set_keypress_callback("#canvas", NULL, 1, EmscriptenKeyboardCallback);
|
|
|
|
|
|
|
|
// Support mouse events
|
|
|
|
emscripten_set_click_callback("#canvas", NULL, 1, EmscriptenMouseCallback);
|
|
|
|
|
|
|
|
// Support touch events
|
|
|
|
emscripten_set_touchstart_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
|
|
|
emscripten_set_touchend_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
|
|
|
emscripten_set_touchmove_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
|
|
|
emscripten_set_touchcancel_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
2016-01-03 15:01:21 +03:00
|
|
|
//emscripten_set_touchstart_callback(0, NULL, 1, Emscripten_HandleTouch);
|
|
|
|
//emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-04-08 01:16:03 +03:00
|
|
|
// Support gamepad events (not provided by GLFW3 on emscripten)
|
2016-12-25 22:42:22 +03:00
|
|
|
emscripten_set_gamepadconnected_callback(NULL, 1, EmscriptenGamepadCallback);
|
|
|
|
emscripten_set_gamepaddisconnected_callback(NULL, 1, EmscriptenGamepadCallback);
|
2015-10-30 13:30:32 +03:00
|
|
|
#endif
|
|
|
|
|
2016-01-13 21:30:35 +03:00
|
|
|
mousePosition.x = (float)screenWidth/2.0f;
|
|
|
|
mousePosition.y = (float)screenHeight/2.0f;
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// raylib logo appearing animation (if enabled)
|
|
|
|
if (showLogo)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
SetTargetFPS(60);
|
|
|
|
LogoAnimation();
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-06-03 01:53:51 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2017-11-12 13:45:35 +03:00
|
|
|
// Initialize window and OpenGL context (and Android activity)
|
|
|
|
// NOTE: data parameter could be used to pass any kind of required data to the initialization
|
2017-12-15 15:44:31 +03:00
|
|
|
void InitWindow(int width, int height, void *data)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2017-12-05 02:05:05 +03:00
|
|
|
TraceLog(LOG_INFO, "Initializing raylib (v1.9-dev)");
|
2015-02-02 02:57:08 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
screenWidth = width;
|
|
|
|
screenHeight = height;
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2017-11-12 13:45:35 +03:00
|
|
|
// Input data is android app pointer
|
|
|
|
app = (struct android_app *)data;
|
2016-05-31 01:01:19 +03:00
|
|
|
internalDataPath = app->activity->internalDataPath;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Set desired windows flags before initializing anything
|
|
|
|
ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_FULLSCREEN, 0); //AWINDOW_FLAG_SCALED, AWINDOW_FLAG_DITHER
|
|
|
|
//ANativeActivity_setWindowFlags(app->activity, AWINDOW_FLAG_FORCE_NOT_FULLSCREEN, AWINDOW_FLAG_FULLSCREEN);
|
2014-03-25 15:40:35 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
int orientation = AConfiguration_getOrientation(app->config);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (orientation == ACONFIGURATION_ORIENTATION_PORT) TraceLog(LOG_INFO, "PORTRAIT window orientation");
|
|
|
|
else if (orientation == ACONFIGURATION_ORIENTATION_LAND) TraceLog(LOG_INFO, "LANDSCAPE window orientation");
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2016-02-12 14:22:56 +03:00
|
|
|
// TODO: Automatic orientation doesn't seem to work
|
2014-09-17 00:51:31 +04:00
|
|
|
if (width <= height)
|
|
|
|
{
|
|
|
|
AConfiguration_setOrientation(app->config, ACONFIGURATION_ORIENTATION_PORT);
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Window set to portraid mode");
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AConfiguration_setOrientation(app->config, ACONFIGURATION_ORIENTATION_LAND);
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Window set to landscape mode");
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
//AConfiguration_getDensity(app->config);
|
|
|
|
//AConfiguration_getKeyboard(app->config);
|
|
|
|
//AConfiguration_getScreenSize(app->config);
|
|
|
|
//AConfiguration_getScreenLong(app->config);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2015-04-22 18:34:42 +03:00
|
|
|
app->onAppCmd = AndroidCommandCallback;
|
2016-01-03 15:01:21 +03:00
|
|
|
app->onInputEvent = AndroidInputCallback;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
InitAssetManager(app->activity->assetManager);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Android app initialized successfully");
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Wait for window to be initialized (display and context)
|
2014-09-17 00:51:31 +04:00
|
|
|
while (!windowReady)
|
2014-07-23 02:06:24 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
// Process events loop
|
|
|
|
while ((ident = ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0)
|
|
|
|
{
|
|
|
|
// Process this event
|
|
|
|
if (source != NULL) source->process(app, source);
|
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// NOTE: Never close window, native activity is controlled by the system!
|
|
|
|
//if (app->destroyRequested != 0) windowShouldClose = true;
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2014-07-23 02:06:24 +04:00
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Close window and unload OpenGL context
|
2017-12-15 15:44:31 +03:00
|
|
|
void CloseWindow(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2017-05-18 19:57:11 +03:00
|
|
|
#if defined(SUPPORT_GIF_RECORDING)
|
|
|
|
if (gifRecording)
|
|
|
|
{
|
2017-05-27 15:40:05 +03:00
|
|
|
GifEnd();
|
2017-05-18 19:57:11 +03:00
|
|
|
gifRecording = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
2013-11-23 16:30:54 +04:00
|
|
|
UnloadDefaultFont();
|
2016-10-09 14:09:08 +03:00
|
|
|
#endif
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
rlglClose(); // De-init rlgl
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2013-11-19 02:38:44 +04:00
|
|
|
glfwDestroyWindow(window);
|
2013-11-23 16:30:54 +04:00
|
|
|
glfwTerminate();
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
|
|
|
|
2017-05-08 03:37:37 +03:00
|
|
|
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32)
|
|
|
|
timeEndPeriod(1); // Restore time period
|
|
|
|
#endif
|
|
|
|
|
2016-06-03 01:53:51 +03:00
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2014-09-17 00:51:31 +04:00
|
|
|
// Close surface, context and display
|
|
|
|
if (display != EGL_NO_DISPLAY)
|
|
|
|
{
|
|
|
|
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
|
|
|
|
if (surface != EGL_NO_SURFACE)
|
|
|
|
{
|
|
|
|
eglDestroySurface(display, surface);
|
|
|
|
surface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context != EGL_NO_CONTEXT)
|
|
|
|
{
|
|
|
|
eglDestroyContext(display, context);
|
|
|
|
context = EGL_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
eglTerminate(display);
|
|
|
|
display = EGL_NO_DISPLAY;
|
2016-08-16 12:09:55 +03:00
|
|
|
}
|
2016-06-24 20:34:47 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
// Wait for mouse and gamepad threads to finish before closing
|
|
|
|
// NOTE: Those threads should already have finished at this point
|
|
|
|
// because they are controlled by windowShouldClose variable
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-24 20:08:23 +03:00
|
|
|
windowShouldClose = true; // Added to force threads to exit when the close window is called
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-06-24 20:34:47 +03:00
|
|
|
pthread_join(mouseThreadId, NULL);
|
2017-01-06 00:36:40 +03:00
|
|
|
pthread_join(touchThreadId, NULL);
|
2016-06-24 20:34:47 +03:00
|
|
|
pthread_join(gamepadThreadId, NULL);
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Window closed successfully");
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Check if KEY_ESCAPE pressed or Close icon pressed
|
2014-09-17 00:51:31 +04:00
|
|
|
bool WindowShouldClose(void)
|
2015-02-02 02:57:08 +03:00
|
|
|
{
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2015-01-21 02:13:17 +03:00
|
|
|
// While window minimized, stop loop execution
|
2017-01-25 13:38:15 +03:00
|
|
|
while (windowMinimized) glfwWaitEvents();
|
2015-01-21 02:13:17 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
return (glfwWindowShouldClose(window));
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2014-09-17 00:51:31 +04:00
|
|
|
return windowShouldClose;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Check if window has been minimized (or lost focus)
|
2015-07-29 22:44:27 +03:00
|
|
|
bool IsWindowMinimized(void)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
|
|
|
return windowMinimized;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Toggle fullscreen mode (only PLATFORM_DESKTOP)
|
2014-09-17 00:51:31 +04:00
|
|
|
void ToggleFullscreen(void)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
fullscreen = !fullscreen; // Toggle fullscreen flag
|
|
|
|
|
2016-06-26 02:36:06 +03:00
|
|
|
// NOTE: glfwSetWindowMonitor() doesn't work properly (bugs)
|
|
|
|
if (fullscreen) glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE);
|
|
|
|
else glfwSetWindowMonitor(window, NULL, 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE);
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Could not toggle to windowed mode");
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-05 05:00:35 +03:00
|
|
|
// Set icon for window (only PLATFORM_DESKTOP)
|
|
|
|
void SetWindowIcon(Image image)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
ImageFormat(&image, UNCOMPRESSED_R8G8B8A8);
|
|
|
|
|
|
|
|
GLFWimage icon[1];
|
|
|
|
|
|
|
|
icon[0].width = image.width;
|
|
|
|
icon[0].height = image.height;
|
|
|
|
icon[0].pixels = (unsigned char *)image.data;
|
|
|
|
|
|
|
|
// NOTE: We only support one image icon
|
|
|
|
glfwSetWindowIcon(window, 1, icon);
|
|
|
|
|
|
|
|
// TODO: Support multi-image icons --> image.mipmaps
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:35:54 +03:00
|
|
|
// Set title for window (only PLATFORM_DESKTOP)
|
|
|
|
void SetWindowTitle(const char *title)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
glfwSetWindowTitle(window, title);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-05 12:55:29 +03:00
|
|
|
// Set window position on screen (windowed mode)
|
|
|
|
void SetWindowPosition(int x, int y)
|
|
|
|
{
|
2017-03-09 15:13:13 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2017-03-05 12:55:29 +03:00
|
|
|
glfwSetWindowPos(window, x, y);
|
2017-03-09 15:13:13 +03:00
|
|
|
#endif
|
2017-03-05 12:55:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set monitor for the current window (fullscreen mode)
|
|
|
|
void SetWindowMonitor(int monitor)
|
|
|
|
{
|
2017-03-09 15:13:13 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2017-03-05 12:55:29 +03:00
|
|
|
int monitorCount;
|
|
|
|
GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
|
|
|
|
|
|
|
|
if ((monitor >= 0) && (monitor < monitorCount))
|
|
|
|
{
|
|
|
|
glfwSetWindowMonitor(window, monitors[monitor], 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE);
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Selected fullscreen monitor: [%i] %s", monitor, glfwGetMonitorName(monitors[monitor]));
|
2017-03-05 12:55:29 +03:00
|
|
|
}
|
2017-07-02 13:35:13 +03:00
|
|
|
else TraceLog(LOG_WARNING, "Selected monitor not found");
|
2017-03-09 15:13:13 +03:00
|
|
|
#endif
|
2017-03-05 12:55:29 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Set window minimum dimensions (FLAG_WINDOW_RESIZABLE)
|
2017-05-02 16:04:32 +03:00
|
|
|
void SetWindowMinSize(int width, int height)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
const GLFWvidmode *mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
|
|
|
glfwSetWindowSizeLimits(window, width, height, mode->width, mode->height);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Get current screen width
|
|
|
|
int GetScreenWidth(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
return screenWidth;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Get current screen height
|
|
|
|
int GetScreenHeight(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
return screenHeight;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2016-10-17 19:18:13 +03:00
|
|
|
// Show mouse cursor
|
2017-12-15 15:44:31 +03:00
|
|
|
void ShowCursor()
|
2016-10-17 19:18:13 +03:00
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2017-05-08 03:47:44 +03:00
|
|
|
#if defined(__linux__)
|
2016-10-17 19:18:13 +03:00
|
|
|
XUndefineCursor(glfwGetX11Display(), glfwGetX11Window(window));
|
|
|
|
#else
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
cursorHidden = false;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Hides mouse cursor
|
2017-12-15 15:44:31 +03:00
|
|
|
void HideCursor()
|
2016-10-17 19:18:13 +03:00
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2017-05-08 03:47:44 +03:00
|
|
|
#if defined(__linux__)
|
2017-01-15 03:10:34 +03:00
|
|
|
XColor col;
|
|
|
|
const char nil[] = {0};
|
2016-10-17 19:18:13 +03:00
|
|
|
|
2017-01-15 03:10:34 +03:00
|
|
|
Pixmap pix = XCreateBitmapFromData(glfwGetX11Display(), glfwGetX11Window(window), nil, 1, 1);
|
|
|
|
Cursor cur = XCreatePixmapCursor(glfwGetX11Display(), pix, pix, &col, &col, 0, 0);
|
2016-10-17 19:18:13 +03:00
|
|
|
|
2017-01-15 03:10:34 +03:00
|
|
|
XDefineCursor(glfwGetX11Display(), glfwGetX11Window(window), cur);
|
|
|
|
XFreeCursor(glfwGetX11Display(), cur);
|
2016-10-17 19:18:13 +03:00
|
|
|
#else
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
cursorHidden = true;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Check if cursor is not visible
|
2016-10-17 19:18:13 +03:00
|
|
|
bool IsCursorHidden()
|
|
|
|
{
|
|
|
|
return cursorHidden;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Enables cursor (unlock cursor)
|
2016-10-17 19:18:13 +03:00
|
|
|
void EnableCursor()
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
2017-05-08 22:03:48 +03:00
|
|
|
#endif
|
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
toggleCursorLock = true;
|
2016-10-17 19:18:13 +03:00
|
|
|
#endif
|
|
|
|
cursorHidden = false;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Disables cursor (lock cursor)
|
2016-10-17 19:18:13 +03:00
|
|
|
void DisableCursor()
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
2017-05-08 22:03:48 +03:00
|
|
|
#endif
|
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
toggleCursorLock = true;
|
2016-10-17 19:18:13 +03:00
|
|
|
#endif
|
|
|
|
cursorHidden = true;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Set background color (framebuffer clear color)
|
2013-11-19 02:38:44 +04:00
|
|
|
void ClearBackground(Color color)
|
|
|
|
{
|
2015-07-13 19:20:16 +03:00
|
|
|
// Clear full framebuffer (not only render area) to color
|
2014-09-17 00:51:31 +04:00
|
|
|
rlClearColor(color.r, color.g, color.b, color.a);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Setup canvas (framebuffer) to start drawing
|
2014-09-03 19:06:10 +04:00
|
|
|
void BeginDrawing(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
currentTime = GetTime(); // Number of elapsed seconds since InitTimer() was called
|
2013-11-23 16:30:54 +04:00
|
|
|
updateTime = currentTime - previousTime;
|
|
|
|
previousTime = currentTime;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-03-30 21:09:16 +03:00
|
|
|
rlClearScreenBuffers(); // Clear current framebuffers
|
2014-03-25 15:40:35 +04:00
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
2016-01-11 15:29:55 +03:00
|
|
|
rlMultMatrixf(MatrixToFloat(downscaleView)); // If downscale required, apply it here
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2015-02-02 02:57:08 +03:00
|
|
|
//rlTranslatef(0.375, 0.375, 0); // HACK to have 2D pixel-perfect drawing on OpenGL 1.1
|
2014-03-25 15:40:35 +04:00
|
|
|
// NOTE: Not required with OpenGL 3.3+
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// End canvas drawing and swap buffers (double buffering)
|
2014-09-03 19:06:10 +04:00
|
|
|
void EndDrawing(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-06-14 18:16:20 +03:00
|
|
|
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
|
2016-06-03 01:53:51 +03:00
|
|
|
|
2017-05-18 19:57:11 +03:00
|
|
|
#if defined(SUPPORT_GIF_RECORDING)
|
|
|
|
|
|
|
|
#define GIF_RECORD_FRAMERATE 10
|
|
|
|
|
|
|
|
if (gifRecording)
|
|
|
|
{
|
|
|
|
gifFramesCounter++;
|
|
|
|
|
|
|
|
// NOTE: We record one gif frame every 10 game frames
|
|
|
|
if ((gifFramesCounter%GIF_RECORD_FRAMERATE) == 0)
|
|
|
|
{
|
|
|
|
// Get image data for the current frame (from backbuffer)
|
|
|
|
// NOTE: This process is very slow... :(
|
2017-07-17 01:33:40 +03:00
|
|
|
unsigned char *screenData = rlReadScreenPixels(screenWidth, screenHeight);
|
2017-05-27 15:40:05 +03:00
|
|
|
GifWriteFrame(screenData, screenWidth, screenHeight, 10, 8, false);
|
2017-05-18 19:57:11 +03:00
|
|
|
|
|
|
|
free(screenData); // Free image data
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((gifFramesCounter/15)%2) == 1)
|
|
|
|
{
|
|
|
|
DrawCircle(30, screenHeight - 20, 10, RED);
|
|
|
|
DrawText("RECORDING", 50, screenHeight - 25, 10, MAROON);
|
|
|
|
}
|
|
|
|
|
|
|
|
rlglDraw(); // Draw RECORDING message
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-03 01:53:51 +03:00
|
|
|
SwapBuffers(); // Copy back buffer to front buffer
|
2014-09-17 00:51:31 +04:00
|
|
|
PollInputEvents(); // Poll user events
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-04-01 11:39:33 +03:00
|
|
|
// Frame time control system
|
2014-09-17 00:51:31 +04:00
|
|
|
currentTime = GetTime();
|
2013-11-23 16:30:54 +04:00
|
|
|
drawTime = currentTime - previousTime;
|
|
|
|
previousTime = currentTime;
|
2017-03-05 21:17:00 +03:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
frameTime = updateTime + drawTime;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-01-28 02:56:45 +03:00
|
|
|
// Wait for some milliseconds...
|
2017-01-29 01:02:30 +03:00
|
|
|
if (frameTime < targetTime)
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
2017-03-05 21:17:00 +03:00
|
|
|
Wait((targetTime - frameTime)*1000.0f);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
currentTime = GetTime();
|
2017-01-28 02:56:45 +03:00
|
|
|
double extraTime = currentTime - previousTime;
|
2013-11-23 16:30:54 +04:00
|
|
|
previousTime = currentTime;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2017-03-05 21:17:00 +03:00
|
|
|
frameTime += extraTime;
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Initialize 2D mode with custom camera (2D)
|
2016-05-02 01:37:33 +03:00
|
|
|
void Begin2dMode(Camera2D camera)
|
|
|
|
{
|
|
|
|
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
|
|
|
|
|
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
|
|
|
|
|
|
|
// Camera rotation and scaling is always relative to target
|
|
|
|
Matrix matOrigin = MatrixTranslate(-camera.target.x, -camera.target.y, 0.0f);
|
|
|
|
Matrix matRotation = MatrixRotate((Vector3){ 0.0f, 0.0f, 1.0f }, camera.rotation*DEG2RAD);
|
|
|
|
Matrix matScale = MatrixScale(camera.zoom, camera.zoom, 1.0f);
|
|
|
|
Matrix matTranslation = MatrixTranslate(camera.offset.x + camera.target.x, camera.offset.y + camera.target.y, 0.0f);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 01:37:33 +03:00
|
|
|
Matrix matTransform = MatrixMultiply(MatrixMultiply(matOrigin, MatrixMultiply(matScale, matRotation)), matTranslation);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 01:37:33 +03:00
|
|
|
rlMultMatrixf(MatrixToFloat(matTransform));
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Ends 2D mode with custom camera
|
2016-05-02 01:37:33 +03:00
|
|
|
void End2dMode(void)
|
|
|
|
{
|
|
|
|
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
|
|
|
|
|
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Initializes 3D mode with custom camera (3D)
|
2013-11-19 02:38:44 +04:00
|
|
|
void Begin3dMode(Camera camera)
|
|
|
|
{
|
2015-02-02 02:57:08 +03:00
|
|
|
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
|
2017-03-14 03:05:22 +03:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
|
|
|
|
rlPushMatrix(); // Save previous matrix, which contains the settings for the 2d ortho projection
|
|
|
|
rlLoadIdentity(); // Reset current matrix (PROJECTION)
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2014-03-16 23:59:02 +04:00
|
|
|
// Setup perspective projection
|
2015-10-06 18:13:40 +03:00
|
|
|
float aspect = (float)screenWidth/(float)screenHeight;
|
2017-07-22 11:35:41 +03:00
|
|
|
double top = 0.01*tan(camera.fovy*0.5*DEG2RAD);
|
2014-03-16 23:59:02 +04:00
|
|
|
double right = top*aspect;
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2015-08-27 17:10:15 +03:00
|
|
|
// NOTE: zNear and zFar values are important when computing depth buffer values
|
2016-03-02 19:13:31 +03:00
|
|
|
rlFrustum(-right, right, -top, top, 0.01, 1000.0);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
// Setup Camera view
|
2017-07-21 18:19:28 +03:00
|
|
|
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
|
|
|
|
rlMultMatrixf(MatrixToFloat(matView)); // Multiply MODELVIEW matrix by view matrix (camera)
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-03-17 15:51:48 +03:00
|
|
|
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ends 3D mode and returns to default 2D orthographic mode
|
2014-09-03 19:06:10 +04:00
|
|
|
void End3dMode(void)
|
2016-08-16 12:09:55 +03:00
|
|
|
{
|
2016-06-21 14:49:13 +03:00
|
|
|
rlglDraw(); // Process internal buffers (update + draw)
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
rlMatrixMode(RL_PROJECTION); // Switch to projection matrix
|
|
|
|
rlPopMatrix(); // Restore previous matrix (PROJECTION) from matrix stack
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-03-25 15:40:35 +04:00
|
|
|
rlMatrixMode(RL_MODELVIEW); // Get back to modelview matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-03-17 15:51:48 +03:00
|
|
|
rlDisableDepthTest(); // Disable DEPTH_TEST for 2D
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2016-03-30 21:09:16 +03:00
|
|
|
// Initializes render texture for drawing
|
|
|
|
void BeginTextureMode(RenderTexture2D target)
|
|
|
|
{
|
|
|
|
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
|
|
|
|
|
2016-05-29 12:49:13 +03:00
|
|
|
rlEnableRenderTexture(target.id); // Enable render target
|
|
|
|
|
2016-03-30 21:09:16 +03:00
|
|
|
rlClearScreenBuffers(); // Clear render texture buffers
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-29 12:49:13 +03:00
|
|
|
// Set viewport to framebuffer size
|
2016-08-16 12:09:55 +03:00
|
|
|
rlViewport(0, 0, target.texture.width, target.texture.height);
|
|
|
|
|
2016-05-29 12:49:13 +03:00
|
|
|
rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (PROJECTION)
|
2016-03-30 21:09:16 +03:00
|
|
|
|
2016-05-29 12:49:13 +03:00
|
|
|
// Set orthographic projection to current framebuffer size
|
|
|
|
// NOTE: Configured top-left corner as (0, 0)
|
2016-08-16 12:09:55 +03:00
|
|
|
rlOrtho(0, target.texture.width, target.texture.height, 0, 0.0f, 1.0f);
|
2016-05-29 12:49:13 +03:00
|
|
|
|
|
|
|
rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
|
2016-03-30 21:09:16 +03:00
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
2016-05-29 12:49:13 +03:00
|
|
|
|
|
|
|
//rlScalef(0.0f, -1.0f, 0.0f); // Flip Y-drawing (?)
|
2016-03-30 21:09:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ends drawing to render texture
|
|
|
|
void EndTextureMode(void)
|
|
|
|
{
|
|
|
|
rlglDraw(); // Draw Buffers (Only OpenGL 3+ and ES2)
|
|
|
|
|
2016-05-29 12:49:13 +03:00
|
|
|
rlDisableRenderTexture(); // Disable render target
|
|
|
|
|
|
|
|
// Set viewport to default framebuffer size (screen size)
|
2016-12-15 10:58:15 +03:00
|
|
|
SetupViewport();
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-29 12:49:13 +03:00
|
|
|
rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (PROJECTION)
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-29 12:49:13 +03:00
|
|
|
// Set orthographic projection to current framebuffer size
|
|
|
|
// NOTE: Configured top-left corner as (0, 0)
|
|
|
|
rlOrtho(0, GetScreenWidth(), GetScreenHeight(), 0, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
2016-03-30 21:09:16 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Returns a ray trace from mouse position
|
|
|
|
Ray GetMouseRay(Vector2 mousePosition, Camera camera)
|
|
|
|
{
|
|
|
|
Ray ray;
|
|
|
|
|
|
|
|
// Calculate normalized device coordinates
|
|
|
|
// NOTE: y value is negative
|
|
|
|
float x = (2.0f*mousePosition.x)/(float)GetScreenWidth() - 1.0f;
|
|
|
|
float y = 1.0f - (2.0f*mousePosition.y)/(float)GetScreenHeight();
|
|
|
|
float z = 1.0f;
|
|
|
|
|
|
|
|
// Store values in a vector
|
|
|
|
Vector3 deviceCoords = { x, y, z };
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Device coordinates: (%f, %f, %f)", deviceCoords.x, deviceCoords.y, deviceCoords.z);
|
2017-05-09 23:03:46 +03:00
|
|
|
|
2017-07-22 12:02:40 +03:00
|
|
|
// Calculate projection matrix from perspective
|
2017-07-21 11:42:41 +03:00
|
|
|
Matrix matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)GetScreenWidth()/(double)GetScreenHeight()), 0.01, 1000.0);
|
2017-05-09 23:03:46 +03:00
|
|
|
|
|
|
|
// Calculate view matrix from camera look at
|
|
|
|
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
|
|
|
|
|
2017-07-22 12:02:40 +03:00
|
|
|
// Unproject far/near points
|
2017-07-17 01:33:40 +03:00
|
|
|
Vector3 nearPoint = rlUnproject((Vector3){ deviceCoords.x, deviceCoords.y, 0.0f }, matProj, matView);
|
|
|
|
Vector3 farPoint = rlUnproject((Vector3){ deviceCoords.x, deviceCoords.y, 1.0f }, matProj, matView);
|
2017-05-09 23:03:46 +03:00
|
|
|
|
|
|
|
// Calculate normalized direction vector
|
2017-07-22 23:15:50 +03:00
|
|
|
Vector3 direction = Vector3Subtract(farPoint, nearPoint);
|
|
|
|
Vector3Normalize(&direction);
|
2017-05-09 23:03:46 +03:00
|
|
|
|
|
|
|
// Apply calculated vectors to ray
|
|
|
|
ray.position = camera.position;
|
|
|
|
ray.direction = direction;
|
|
|
|
|
|
|
|
return ray;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the screen space position from a 3d world space position
|
|
|
|
Vector2 GetWorldToScreen(Vector3 position, Camera camera)
|
|
|
|
{
|
|
|
|
// Calculate projection matrix (from perspective instead of frustum
|
2017-07-21 11:42:41 +03:00
|
|
|
Matrix matProj = MatrixPerspective(camera.fovy*DEG2RAD, (double)GetScreenWidth()/(double)GetScreenHeight(), 0.01, 1000.0);
|
2017-05-09 23:03:46 +03:00
|
|
|
|
|
|
|
// Calculate view matrix from camera look at (and transpose it)
|
|
|
|
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
|
|
|
|
|
|
|
|
// Convert world position vector to quaternion
|
|
|
|
Quaternion worldPos = { position.x, position.y, position.z, 1.0f };
|
|
|
|
|
|
|
|
// Transform world position to view
|
|
|
|
QuaternionTransform(&worldPos, matView);
|
|
|
|
|
|
|
|
// Transform result to projection (clip space position)
|
|
|
|
QuaternionTransform(&worldPos, matProj);
|
|
|
|
|
|
|
|
// Calculate normalized device coordinates (inverted y)
|
|
|
|
Vector3 ndcPos = { worldPos.x/worldPos.w, -worldPos.y/worldPos.w, worldPos.z/worldPos.w };
|
|
|
|
|
|
|
|
// Calculate 2d screen position vector
|
|
|
|
Vector2 screenPosition = { (ndcPos.x + 1.0f)/2.0f*(float)GetScreenWidth(), (ndcPos.y + 1.0f)/2.0f*(float)GetScreenHeight() };
|
|
|
|
|
|
|
|
return screenPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get transform matrix for camera
|
|
|
|
Matrix GetCameraMatrix(Camera camera)
|
|
|
|
{
|
|
|
|
return MatrixLookAt(camera.position, camera.target, camera.up);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set target FPS (maximum)
|
2013-11-19 02:38:44 +04:00
|
|
|
void SetTargetFPS(int fps)
|
|
|
|
{
|
2016-09-24 00:25:13 +03:00
|
|
|
if (fps < 1) targetTime = 0.0;
|
|
|
|
else targetTime = 1.0/(double)fps;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Target time per frame: %02.03f milliseconds", (float)targetTime*1000);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns current FPS
|
2017-01-28 02:56:45 +03:00
|
|
|
int GetFPS(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2017-03-05 21:17:00 +03:00
|
|
|
return (int)(1.0f/GetFrameTime());
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Returns time in seconds for last frame drawn
|
2014-09-03 19:06:10 +04:00
|
|
|
float GetFrameTime(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2017-01-28 02:56:45 +03:00
|
|
|
// NOTE: We round value to milliseconds
|
2017-03-05 21:17:00 +03:00
|
|
|
return (float)frameTime;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2016-01-11 15:29:55 +03:00
|
|
|
// Converts Color to float array and normalizes
|
|
|
|
float *ColorToFloat(Color color)
|
|
|
|
{
|
|
|
|
static float buffer[4];
|
|
|
|
|
|
|
|
buffer[0] = (float)color.r/255;
|
|
|
|
buffer[1] = (float)color.g/255;
|
|
|
|
buffer[2] = (float)color.b/255;
|
|
|
|
buffer[3] = (float)color.a/255;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Returns a Color struct from hexadecimal value
|
|
|
|
Color GetColor(int hexValue)
|
|
|
|
{
|
2013-11-23 16:30:54 +04:00
|
|
|
Color color;
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
color.r = (unsigned char)(hexValue >> 24) & 0xFF;
|
|
|
|
color.g = (unsigned char)(hexValue >> 16) & 0xFF;
|
|
|
|
color.b = (unsigned char)(hexValue >> 8) & 0xFF;
|
|
|
|
color.a = (unsigned char)hexValue & 0xFF;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2013-11-23 16:30:54 +04:00
|
|
|
return color;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns hexadecimal value for a Color
|
|
|
|
int GetHexValue(Color color)
|
|
|
|
{
|
2015-07-05 19:19:49 +03:00
|
|
|
return (((int)color.r << 24) | ((int)color.g << 16) | ((int)color.b << 8) | (int)color.a);
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 15:08:06 +04:00
|
|
|
// Returns a random value between min and max (both included)
|
|
|
|
int GetRandomValue(int min, int max)
|
|
|
|
{
|
2014-01-07 19:53:57 +04:00
|
|
|
if (min > max)
|
|
|
|
{
|
|
|
|
int tmp = max;
|
|
|
|
max = min;
|
|
|
|
min = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rand()%(abs(max-min)+1) + min);
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Color fade-in or fade-out, alpha goes from 0.0f to 1.0f
|
2014-01-23 15:36:18 +04:00
|
|
|
Color Fade(Color color, float alpha)
|
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
if (alpha < 0.0f) alpha = 0.0f;
|
|
|
|
else if (alpha > 1.0f) alpha = 1.0f;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-11-03 14:41:03 +03:00
|
|
|
return (Color){color.r, color.g, color.b, (unsigned char)(255.0f*alpha)};
|
2014-01-23 15:36:18 +04:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Activate raylib logo at startup (can be done with flags)
|
2017-03-29 01:35:42 +03:00
|
|
|
void ShowLogo(void)
|
|
|
|
{
|
|
|
|
showLogo = true;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Setup window configuration flags (view FLAGS)
|
2015-02-04 22:23:43 +03:00
|
|
|
void SetConfigFlags(char flags)
|
2014-10-17 23:11:58 +04:00
|
|
|
{
|
|
|
|
configFlags = flags;
|
|
|
|
|
|
|
|
if (configFlags & FLAG_SHOW_LOGO) showLogo = true;
|
|
|
|
if (configFlags & FLAG_FULLSCREEN_MODE) fullscreen = true;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// NOTE TraceLog() function is located in [utils.h]
|
|
|
|
|
2017-05-11 17:24:40 +03:00
|
|
|
// Takes a screenshot of current screen (saved a .png)
|
|
|
|
void TakeScreenshot(const char *fileName)
|
2017-04-16 14:47:49 +03:00
|
|
|
{
|
2017-12-15 15:44:31 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
|
2017-07-17 01:33:40 +03:00
|
|
|
unsigned char *imgData = rlReadScreenPixels(renderWidth, renderHeight);
|
2017-05-11 17:24:40 +03:00
|
|
|
SavePNG(fileName, imgData, renderWidth, renderHeight, 4); // Save image as PNG
|
2017-04-16 14:47:49 +03:00
|
|
|
free(imgData);
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Screenshot taken: %s", fileName);
|
2017-04-16 14:47:49 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-29 01:35:42 +03:00
|
|
|
// Check file extension
|
|
|
|
bool IsFileExtension(const char *fileName, const char *ext)
|
2014-07-23 02:06:24 +04:00
|
|
|
{
|
2017-03-29 01:35:42 +03:00
|
|
|
bool result = false;
|
|
|
|
const char *fileExt;
|
|
|
|
|
|
|
|
if ((fileExt = strrchr(fileName, '.')) != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp(fileExt, ext) == 0) result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-07-23 02:06:24 +04:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:35:54 +03:00
|
|
|
// Get the extension for a filename
|
|
|
|
const char *GetExtension(const char *fileName)
|
|
|
|
{
|
|
|
|
const char *dot = strrchr(fileName, '.');
|
|
|
|
|
|
|
|
if (!dot || dot == fileName) return "";
|
|
|
|
|
|
|
|
return (dot + 1);
|
|
|
|
}
|
|
|
|
|
2017-05-11 17:24:40 +03:00
|
|
|
// Get directory for a given fileName (with path)
|
|
|
|
const char *GetDirectoryPath(const char *fileName)
|
|
|
|
{
|
|
|
|
char *lastSlash = NULL;
|
|
|
|
static char filePath[256]; // MAX_DIRECTORY_PATH_SIZE = 256
|
|
|
|
memset(filePath, 0, 256);
|
|
|
|
|
|
|
|
lastSlash = strrchr(fileName, '\\');
|
|
|
|
strncpy(filePath, fileName, strlen(fileName) - (strlen(lastSlash) - 1));
|
|
|
|
filePath[strlen(fileName) - strlen(lastSlash)] = '\0';
|
|
|
|
|
|
|
|
return filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current working directory
|
|
|
|
const char *GetWorkingDirectory(void)
|
|
|
|
{
|
|
|
|
static char currentDir[256]; // MAX_DIRECTORY_PATH_SIZE = 256
|
|
|
|
memset(currentDir, 0, 256);
|
|
|
|
|
|
|
|
GETCWD(currentDir, 256 - 1);
|
|
|
|
|
|
|
|
return currentDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change working directory, returns true if success
|
|
|
|
bool ChangeDirectory(const char *dir)
|
|
|
|
{
|
|
|
|
return (CHDIR(dir) == 0);
|
|
|
|
}
|
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2017-05-09 23:03:46 +03:00
|
|
|
// Check if a file has been dropped into window
|
2015-07-29 22:44:27 +03:00
|
|
|
bool IsFileDropped(void)
|
|
|
|
{
|
|
|
|
if (dropFilesCount > 0) return true;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Get dropped files names
|
2015-07-29 22:44:27 +03:00
|
|
|
char **GetDroppedFiles(int *count)
|
|
|
|
{
|
|
|
|
*count = dropFilesCount;
|
|
|
|
return dropFilesPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear dropped files paths buffer
|
|
|
|
void ClearDroppedFiles(void)
|
|
|
|
{
|
|
|
|
if (dropFilesCount > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < dropFilesCount; i++) free(dropFilesPath[i]);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-07-29 22:44:27 +03:00
|
|
|
free(dropFilesPath);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-07-29 22:44:27 +03:00
|
|
|
dropFilesCount = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 21:20:28 +03:00
|
|
|
#endif
|
2015-07-29 22:44:27 +03:00
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Save integer value to storage file (to defined position)
|
2016-01-04 17:12:34 +03:00
|
|
|
// NOTE: Storage positions is directly related to file memory layout (4 bytes each integer)
|
|
|
|
void StorageSaveValue(int position, int value)
|
|
|
|
{
|
|
|
|
FILE *storageFile = NULL;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-31 01:01:19 +03:00
|
|
|
char path[128];
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
strcpy(path, internalDataPath);
|
|
|
|
strcat(path, "/");
|
|
|
|
strcat(path, STORAGE_FILENAME);
|
|
|
|
#else
|
|
|
|
strcpy(path, STORAGE_FILENAME);
|
|
|
|
#endif
|
2016-01-04 17:12:34 +03:00
|
|
|
|
|
|
|
// Try open existing file to append data
|
2016-08-16 12:09:55 +03:00
|
|
|
storageFile = fopen(path, "rb+");
|
2016-01-04 17:12:34 +03:00
|
|
|
|
|
|
|
// If file doesn't exist, create a new storage data file
|
2016-05-31 01:01:19 +03:00
|
|
|
if (!storageFile) storageFile = fopen(path, "wb");
|
2016-01-04 17:12:34 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (!storageFile) TraceLog(LOG_WARNING, "Storage data file could not be created");
|
2016-01-04 17:12:34 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get file size
|
|
|
|
fseek(storageFile, 0, SEEK_END);
|
|
|
|
int fileSize = ftell(storageFile); // Size in bytes
|
|
|
|
fseek(storageFile, 0, SEEK_SET);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (fileSize < (position*4)) TraceLog(LOG_WARNING, "Storage position could not be found");
|
2016-01-04 17:12:34 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fseek(storageFile, (position*4), SEEK_SET);
|
|
|
|
fwrite(&value, 1, 4, storageFile);
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-04 17:12:34 +03:00
|
|
|
fclose(storageFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Load integer value from storage file (from defined position)
|
2016-01-04 17:12:34 +03:00
|
|
|
// NOTE: If requested position could not be found, value 0 is returned
|
|
|
|
int StorageLoadValue(int position)
|
|
|
|
{
|
|
|
|
int value = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-31 01:01:19 +03:00
|
|
|
char path[128];
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
strcpy(path, internalDataPath);
|
|
|
|
strcat(path, "/");
|
|
|
|
strcat(path, STORAGE_FILENAME);
|
|
|
|
#else
|
|
|
|
strcpy(path, STORAGE_FILENAME);
|
|
|
|
#endif
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-04 17:12:34 +03:00
|
|
|
// Try open existing file to append data
|
2016-08-16 12:09:55 +03:00
|
|
|
FILE *storageFile = fopen(path, "rb");
|
2016-01-04 17:12:34 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (!storageFile) TraceLog(LOG_WARNING, "Storage data file could not be found");
|
2016-01-04 17:12:34 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get file size
|
|
|
|
fseek(storageFile, 0, SEEK_END);
|
2017-02-12 01:17:56 +03:00
|
|
|
int fileSize = ftell(storageFile); // Size in bytes
|
2016-01-04 17:12:34 +03:00
|
|
|
rewind(storageFile);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (fileSize < (position*4)) TraceLog(LOG_WARNING, "Storage position could not be found");
|
2016-01-04 17:12:34 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fseek(storageFile, (position*4), SEEK_SET);
|
2017-02-12 01:17:56 +03:00
|
|
|
fread(&value, 4, 1, storageFile); // Read 1 element of 4 bytes size
|
2016-01-04 17:12:34 +03:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-04 17:12:34 +03:00
|
|
|
fclose(storageFile);
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-04 17:12:34 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition - Input (Keyboard, Mouse, Gamepad) Functions
|
|
|
|
//----------------------------------------------------------------------------------
|
2013-11-28 22:59:56 +04:00
|
|
|
// Detect if a key has been pressed once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsKeyPressed(int key)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2014-04-09 22:25:26 +04:00
|
|
|
bool pressed = false;
|
2013-11-28 22:59:56 +04:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
if ((currentKeyState[key] != previousKeyState[key]) && (currentKeyState[key] == 1)) pressed = true;
|
2014-04-09 22:25:26 +04:00
|
|
|
else pressed = false;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
return pressed;
|
2013-11-28 22:59:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a key is being pressed (key held down)
|
|
|
|
bool IsKeyDown(int key)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
if (GetKeyStatus(key) == 1) return true;
|
2013-11-23 16:30:54 +04:00
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2013-11-28 22:59:56 +04:00
|
|
|
// Detect if a key has been released once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsKeyReleased(int key)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2014-04-09 22:25:26 +04:00
|
|
|
bool released = false;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
if ((currentKeyState[key] != previousKeyState[key]) && (currentKeyState[key] == 0)) released = true;
|
2014-04-09 22:25:26 +04:00
|
|
|
else released = false;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
return released;
|
2013-11-28 22:59:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a key is NOT being pressed (key not held down)
|
|
|
|
bool IsKeyUp(int key)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
if (GetKeyStatus(key) == 0) return true;
|
2013-11-23 16:30:54 +04:00
|
|
|
else return false;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2015-01-04 20:05:50 +03:00
|
|
|
// Get the last key pressed
|
|
|
|
int GetKeyPressed(void)
|
|
|
|
{
|
|
|
|
return lastKeyPressed;
|
|
|
|
}
|
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
// Set a custom key to exit program
|
|
|
|
// NOTE: default exitKey is ESCAPE
|
|
|
|
void SetExitKey(int key)
|
2014-01-29 00:21:29 +04:00
|
|
|
{
|
2016-10-17 19:18:13 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2016-04-17 12:19:32 +03:00
|
|
|
exitKey = key;
|
2015-02-03 05:47:28 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// NOTE: Gamepad support not implemented in emscripten GLFW3 (PLATFORM_WEB)
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Detect if a gamepad is available
|
|
|
|
bool IsGamepadAvailable(int gamepad)
|
|
|
|
{
|
2016-02-18 16:05:48 +03:00
|
|
|
bool result = false;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-31 15:56:57 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2016-03-17 14:54:36 +03:00
|
|
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad]) result = true;
|
2016-10-31 15:56:57 +03:00
|
|
|
#endif
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
return result;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2016-11-01 16:39:57 +03:00
|
|
|
// Check gamepad name (if available)
|
|
|
|
bool IsGamepadName(int gamepad, const char *name)
|
|
|
|
{
|
|
|
|
bool result = false;
|
2016-11-17 15:50:56 +03:00
|
|
|
|
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2017-01-29 01:02:30 +03:00
|
|
|
const char *gamepadName = NULL;
|
2016-11-18 15:39:57 +03:00
|
|
|
|
2016-11-01 16:39:57 +03:00
|
|
|
if (gamepadReady[gamepad]) gamepadName = GetGamepadName(gamepad);
|
|
|
|
if ((name != NULL) && (gamepadName != NULL)) result = (strcmp(name, gamepadName) == 0);
|
2016-11-17 15:50:56 +03:00
|
|
|
#endif
|
|
|
|
|
2016-11-01 16:39:57 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:47:43 +03:00
|
|
|
// Return gamepad internal name id
|
|
|
|
const char *GetGamepadName(int gamepad)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2016-11-01 16:39:57 +03:00
|
|
|
if (gamepadReady[gamepad]) return glfwGetJoystickName(gamepad);
|
2016-10-14 01:47:43 +03:00
|
|
|
else return NULL;
|
2016-11-02 15:39:48 +03:00
|
|
|
#elif defined(PLATFORM_RPI)
|
2016-12-25 04:01:13 +03:00
|
|
|
if (gamepadReady[gamepad]) ioctl(gamepadStream[gamepad], JSIOCGNAME(64), &gamepadName);
|
2016-11-02 15:39:48 +03:00
|
|
|
|
|
|
|
return gamepadName;
|
2016-10-14 01:47:43 +03:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:39:57 +03:00
|
|
|
// Return gamepad axis count
|
|
|
|
int GetGamepadAxisCount(int gamepad)
|
|
|
|
{
|
2016-11-02 15:39:48 +03:00
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
int axisCount = 0;
|
|
|
|
if (gamepadReady[gamepad]) ioctl(gamepadStream[gamepad], JSIOCGAXES, &axisCount);
|
|
|
|
gamepadAxisCount = axisCount;
|
|
|
|
#endif
|
2016-11-01 16:39:57 +03:00
|
|
|
return gamepadAxisCount;
|
|
|
|
}
|
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
// Return axis movement vector for a gamepad
|
2016-03-16 19:52:09 +03:00
|
|
|
float GetGamepadAxisMovement(int gamepad, int axis)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-03-16 19:52:09 +03:00
|
|
|
float value = 0;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-31 15:56:57 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
|
|
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (axis < MAX_GAMEPAD_AXIS)) value = gamepadAxisState[gamepad][axis];
|
|
|
|
#endif
|
2013-11-23 16:30:54 +04:00
|
|
|
|
2016-03-16 19:52:09 +03:00
|
|
|
return value;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Detect if a gamepad button has been pressed once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsGamepadButtonPressed(int gamepad, int button)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
2014-04-09 22:25:26 +04:00
|
|
|
bool pressed = false;
|
2016-10-31 15:56:57 +03:00
|
|
|
|
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2017-01-29 01:02:30 +03:00
|
|
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
|
|
|
|
(currentGamepadState[gamepad][button] != previousGamepadState[gamepad][button]) &&
|
2016-10-14 12:14:41 +03:00
|
|
|
(currentGamepadState[gamepad][button] == 1)) pressed = true;
|
2016-10-31 15:56:57 +03:00
|
|
|
#endif
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
return pressed;
|
2013-11-28 22:59:56 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Detect if a gamepad button is being pressed
|
2013-11-28 22:59:56 +04:00
|
|
|
bool IsGamepadButtonDown(int gamepad, int button)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-02-18 16:05:48 +03:00
|
|
|
bool result = false;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-10-31 15:56:57 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2016-10-17 19:18:13 +03:00
|
|
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
|
2016-10-14 12:14:41 +03:00
|
|
|
(currentGamepadState[gamepad][button] == 1)) result = true;
|
2016-10-31 15:56:57 +03:00
|
|
|
#endif
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
return result;
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Detect if a gamepad button has NOT been pressed once
|
2013-11-19 02:38:44 +04:00
|
|
|
bool IsGamepadButtonReleased(int gamepad, int button)
|
2013-11-28 22:59:56 +04:00
|
|
|
{
|
2014-04-09 22:25:26 +04:00
|
|
|
bool released = false;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-31 15:56:57 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2017-01-29 01:02:30 +03:00
|
|
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
|
|
|
|
(currentGamepadState[gamepad][button] != previousGamepadState[gamepad][button]) &&
|
2016-10-14 12:14:41 +03:00
|
|
|
(currentGamepadState[gamepad][button] == 0)) released = true;
|
2016-10-31 15:56:57 +03:00
|
|
|
#endif
|
2013-11-28 22:59:56 +04:00
|
|
|
|
2014-04-09 22:25:26 +04:00
|
|
|
return released;
|
2013-11-28 22:59:56 +04:00
|
|
|
}
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Detect if a mouse button is NOT being pressed
|
2013-11-28 22:59:56 +04:00
|
|
|
bool IsGamepadButtonUp(int gamepad, int button)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-02-18 16:05:48 +03:00
|
|
|
bool result = false;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-10-31 15:56:57 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
2017-01-29 01:02:30 +03:00
|
|
|
if ((gamepad < MAX_GAMEPADS) && gamepadReady[gamepad] && (button < MAX_GAMEPAD_BUTTONS) &&
|
2016-10-14 12:14:41 +03:00
|
|
|
(currentGamepadState[gamepad][button] == 0)) result = true;
|
2016-10-31 15:56:57 +03:00
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
return result;
|
|
|
|
}
|
2016-10-27 14:41:43 +03:00
|
|
|
|
|
|
|
// Get the last gamepad button pressed
|
|
|
|
int GetGamepadButtonPressed(void)
|
|
|
|
{
|
|
|
|
return lastGamepadButtonPressed;
|
|
|
|
}
|
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
// Detect if a mouse button has been pressed once
|
|
|
|
bool IsMouseButtonPressed(int button)
|
|
|
|
{
|
|
|
|
bool pressed = false;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-09-29 14:56:37 +03:00
|
|
|
// TODO: Review, gestures could be not supported despite being on Android platform!
|
2016-04-17 12:19:32 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2016-04-17 12:36:40 +03:00
|
|
|
if (IsGestureDetected(GESTURE_TAP)) pressed = true;
|
2016-04-17 12:19:32 +03:00
|
|
|
#else
|
|
|
|
if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 1)) pressed = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return pressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a mouse button is being pressed
|
|
|
|
bool IsMouseButtonDown(int button)
|
|
|
|
{
|
|
|
|
bool down = false;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2016-04-17 12:36:40 +03:00
|
|
|
if (IsGestureDetected(GESTURE_HOLD)) down = true;
|
2016-04-17 12:19:32 +03:00
|
|
|
#else
|
|
|
|
if (GetMouseButtonStatus(button) == 1) down = true;
|
|
|
|
#endif
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
return down;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a mouse button has been released once
|
|
|
|
bool IsMouseButtonReleased(int button)
|
|
|
|
{
|
|
|
|
bool released = false;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
|
|
|
if ((currentMouseState[button] != previousMouseState[button]) && (currentMouseState[button] == 0)) released = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return released;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect if a mouse button is NOT being pressed
|
|
|
|
bool IsMouseButtonUp(int button)
|
|
|
|
{
|
|
|
|
bool up = false;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
#if !defined(PLATFORM_ANDROID)
|
|
|
|
if (GetMouseButtonStatus(button) == 0) up = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns mouse position X
|
|
|
|
int GetMouseX(void)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
return (int)touchPosition[0].x;
|
|
|
|
#else
|
|
|
|
return (int)mousePosition.x;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns mouse position Y
|
|
|
|
int GetMouseY(void)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
return (int)touchPosition[0].x;
|
|
|
|
#else
|
|
|
|
return (int)mousePosition.y;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns mouse position XY
|
|
|
|
Vector2 GetMousePosition(void)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
return GetTouchPosition(0);
|
|
|
|
#else
|
|
|
|
return mousePosition;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set mouse position XY
|
|
|
|
void SetMousePosition(Vector2 position)
|
|
|
|
{
|
|
|
|
mousePosition = position;
|
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
|
|
|
// NOTE: emscripten not implemented
|
|
|
|
glfwSetCursorPos(window, position.x, position.y);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns mouse wheel movement Y
|
|
|
|
int GetMouseWheelMove(void)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
return 0;
|
|
|
|
#elif defined(PLATFORM_WEB)
|
|
|
|
return previousMouseWheelY/100;
|
|
|
|
#else
|
|
|
|
return previousMouseWheelY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Returns touch position X for touch point 0 (relative to screen size)
|
2015-10-30 13:30:32 +03:00
|
|
|
int GetTouchX(void)
|
|
|
|
{
|
2016-01-24 21:17:08 +03:00
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
2016-02-10 12:31:06 +03:00
|
|
|
return (int)touchPosition[0].x;
|
2016-01-24 21:17:08 +03:00
|
|
|
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
|
|
|
return GetMouseX();
|
|
|
|
#endif
|
2015-10-30 13:30:32 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Returns touch position Y for touch point 0 (relative to screen size)
|
2015-10-30 13:30:32 +03:00
|
|
|
int GetTouchY(void)
|
|
|
|
{
|
2016-01-24 21:17:08 +03:00
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
2016-02-10 12:31:06 +03:00
|
|
|
return (int)touchPosition[0].y;
|
2016-01-24 21:17:08 +03:00
|
|
|
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
|
|
|
return GetMouseY();
|
|
|
|
#endif
|
2015-10-30 13:30:32 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 23:03:46 +03:00
|
|
|
// Returns touch position XY for a touch point index (relative to screen size)
|
2016-02-12 14:22:56 +03:00
|
|
|
// TODO: Touch position should be scaled depending on display size and render size
|
2016-02-10 12:31:06 +03:00
|
|
|
Vector2 GetTouchPosition(int index)
|
2015-10-30 13:30:32 +03:00
|
|
|
{
|
2016-02-10 12:31:06 +03:00
|
|
|
Vector2 position = { -1.0f, -1.0f };
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-24 21:17:08 +03:00
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
|
2016-02-10 12:31:06 +03:00
|
|
|
if (index < MAX_TOUCH_POINTS) position = touchPosition[index];
|
2017-07-02 13:35:13 +03:00
|
|
|
else TraceLog(LOG_WARNING, "Required touch point out of range (Max touch points: %i)", MAX_TOUCH_POINTS);
|
2015-10-30 13:30:32 +03:00
|
|
|
|
|
|
|
if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
|
|
|
|
{
|
2016-02-12 14:22:56 +03:00
|
|
|
// TODO: Review touch position scaling for screenSize vs displaySize
|
2015-10-30 13:30:32 +03:00
|
|
|
position.x = position.x*((float)screenWidth/(float)(displayWidth - renderOffsetX)) - renderOffsetX/2;
|
|
|
|
position.y = position.y*((float)screenHeight/(float)(displayHeight - renderOffsetY)) - renderOffsetY/2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
position.x = position.x*((float)renderWidth/(float)displayWidth) - renderOffsetX/2;
|
|
|
|
position.y = position.y*((float)renderHeight/(float)displayHeight) - renderOffsetY/2;
|
|
|
|
}
|
2016-01-24 21:17:08 +03:00
|
|
|
#else // PLATFORM_DESKTOP, PLATFORM_RPI
|
2016-02-10 12:31:06 +03:00
|
|
|
if (index == 0) position = GetMousePosition();
|
2016-01-24 21:17:08 +03:00
|
|
|
#endif
|
2015-10-30 13:30:32 +03:00
|
|
|
|
|
|
|
return position;
|
|
|
|
}
|
2016-01-04 23:00:20 +03:00
|
|
|
|
2013-11-19 02:38:44 +04:00
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Initialize display device and framebuffer
|
|
|
|
// NOTE: width and height represent the screen (framebuffer) desired size, not actual display size
|
|
|
|
// If width or height are 0, default display size will be used for framebuffer size
|
2016-06-25 22:28:50 +03:00
|
|
|
static void InitGraphicsDevice(int width, int height)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
|
|
|
screenWidth = width; // User desired width
|
|
|
|
screenHeight = height; // User desired height
|
|
|
|
|
|
|
|
// NOTE: Framebuffer (render area - renderWidth, renderHeight) could include black bars...
|
|
|
|
// ...in top-down or left-right to match display aspect ratio (no weird scalings)
|
|
|
|
|
|
|
|
// Downscale matrix is required in case desired screen area is bigger than display area
|
|
|
|
downscaleView = MatrixIdentity();
|
2016-06-03 01:53:51 +03:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2014-09-17 00:51:31 +04:00
|
|
|
glfwSetErrorCallback(ErrorCallback);
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (!glfwInit()) TraceLog(LOG_ERROR, "Failed to initialize GLFW");
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2014-12-15 03:08:30 +03:00
|
|
|
// NOTE: Getting video modes is not implemented in emscripten GLFW3 version
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2014-09-17 00:51:31 +04:00
|
|
|
// Find monitor resolution
|
|
|
|
const GLFWvidmode *mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
|
|
|
|
|
|
|
displayWidth = mode->width;
|
|
|
|
displayHeight = mode->height;
|
|
|
|
|
|
|
|
// Screen size security check
|
|
|
|
if (screenWidth <= 0) screenWidth = displayWidth;
|
|
|
|
if (screenHeight <= 0) screenHeight = displayHeight;
|
2016-04-17 12:19:32 +03:00
|
|
|
#endif // defined(PLATFORM_DESKTOP)
|
|
|
|
|
|
|
|
#if defined(PLATFORM_WEB)
|
2014-12-15 03:08:30 +03:00
|
|
|
displayWidth = screenWidth;
|
|
|
|
displayHeight = screenHeight;
|
2016-04-17 12:19:32 +03:00
|
|
|
#endif // defined(PLATFORM_WEB)
|
2015-02-02 02:57:08 +03:00
|
|
|
|
2016-10-05 01:48:44 +03:00
|
|
|
glfwDefaultWindowHints(); // Set default windows hints
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2017-03-05 12:55:29 +03:00
|
|
|
// Check some Window creation flags
|
|
|
|
if (configFlags & FLAG_WINDOW_RESIZABLE) glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // Resizable window
|
|
|
|
else glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Avoid window being resizable
|
|
|
|
|
|
|
|
if (configFlags & FLAG_WINDOW_DECORATED) glfwWindowHint(GLFW_DECORATED, GL_TRUE); // Border and buttons on Window
|
|
|
|
|
|
|
|
if (configFlags & FLAG_WINDOW_TRANSPARENT)
|
2016-10-05 01:48:44 +03:00
|
|
|
{
|
2017-03-05 12:55:29 +03:00
|
|
|
// TODO: Enable transparent window (not ready yet on GLFW 3.2)
|
2016-10-05 01:48:44 +03:00
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2017-03-05 12:55:29 +03:00
|
|
|
if (configFlags & FLAG_MSAA_4X_HINT)
|
|
|
|
{
|
|
|
|
glfwWindowHint(GLFW_SAMPLES, 4); // Enables multisampling x4 (MSAA), default is 0
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Trying to enable MSAA x4");
|
2017-03-05 12:55:29 +03:00
|
|
|
}
|
|
|
|
|
2016-10-05 01:48:44 +03:00
|
|
|
//glfwWindowHint(GLFW_RED_BITS, 8); // Framebuffer red color component bits
|
|
|
|
//glfwWindowHint(GLFW_DEPTH_BITS, 16); // Depthbuffer bits (24 by default)
|
|
|
|
//glfwWindowHint(GLFW_REFRESH_RATE, 0); // Refresh rate for fullscreen window
|
2014-09-17 00:51:31 +04:00
|
|
|
//glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); // Default OpenGL API to use. Alternative: GLFW_OPENGL_ES_API
|
2016-10-05 01:48:44 +03:00
|
|
|
//glfwWindowHint(GLFW_AUX_BUFFERS, 0); // Number of auxiliar buffers
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2015-02-02 02:57:08 +03:00
|
|
|
// NOTE: When asking for an OpenGL context version, most drivers provide highest supported version
|
2014-09-17 00:51:31 +04:00
|
|
|
// with forward compatibility to older OpenGL versions.
|
2017-03-05 12:55:29 +03:00
|
|
|
// For example, if using OpenGL 1.1, driver can provide a 4.3 context forward compatible.
|
2015-02-02 02:57:08 +03:00
|
|
|
|
2016-06-16 21:25:50 +03:00
|
|
|
// Check selection OpenGL version
|
|
|
|
if (rlGetVersion() == OPENGL_21)
|
|
|
|
{
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // Choose OpenGL major version (just hint)
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // Choose OpenGL minor version (just hint)
|
|
|
|
}
|
|
|
|
else if (rlGetVersion() == OPENGL_33)
|
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Choose OpenGL major version (just hint)
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint)
|
2015-10-06 18:13:40 +03:00
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Profiles Hint: Only 3.3 and above!
|
2014-09-17 00:51:31 +04:00
|
|
|
// Other values: GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_COMPAT_PROFILE
|
2017-05-08 03:47:44 +03:00
|
|
|
#if defined(__APPLE__)
|
2016-08-16 12:09:55 +03:00
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // OSX Requires
|
2016-05-14 03:10:05 +03:00
|
|
|
#else
|
2015-10-06 18:13:40 +03:00
|
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE); // Fordward Compatibility Hint: Only 3.3 and above!
|
2016-05-14 03:10:05 +03:00
|
|
|
#endif
|
2016-05-07 19:04:22 +03:00
|
|
|
//glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fullscreen)
|
|
|
|
{
|
2016-06-26 02:36:06 +03:00
|
|
|
// Obtain recommended displayWidth/displayHeight from a valid videomode for the monitor
|
2016-08-16 12:09:55 +03:00
|
|
|
int count;
|
2016-02-13 19:39:38 +03:00
|
|
|
const GLFWvidmode *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-06-26 02:36:06 +03:00
|
|
|
// Get closest videomode to desired screenWidth/screenHeight
|
2016-02-13 19:39:38 +03:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2016-06-26 02:36:06 +03:00
|
|
|
if (modes[i].width >= screenWidth)
|
|
|
|
{
|
|
|
|
if (modes[i].height >= screenHeight)
|
|
|
|
{
|
|
|
|
displayWidth = modes[i].width;
|
|
|
|
displayHeight = modes[i].height;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 19:39:38 +03:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Closest fullscreen videomode: %i x %i", displayWidth, displayHeight);
|
2016-06-26 02:36:06 +03:00
|
|
|
|
|
|
|
// NOTE: ISSUE: Closest videomode could not match monitor aspect-ratio, for example,
|
|
|
|
// for a desired screen size of 800x450 (16:9), closest supported videomode is 800x600 (4:3),
|
|
|
|
// framebuffer is rendered correctly but once displayed on a 16:9 monitor, it gets stretched
|
|
|
|
// by the sides to fit all monitor space...
|
|
|
|
|
|
|
|
// At this point we need to manage render size vs screen size
|
2016-08-16 12:09:55 +03:00
|
|
|
// NOTE: This function uses and modifies global module variables:
|
2016-06-26 02:36:06 +03:00
|
|
|
// screenWidth/screenHeight - renderWidth/renderHeight - downscaleView
|
|
|
|
SetupFramebufferSize(displayWidth, displayHeight);
|
|
|
|
|
|
|
|
window = glfwCreateWindow(displayWidth, displayHeight, windowTitle, glfwGetPrimaryMonitor(), NULL);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-06-26 02:36:06 +03:00
|
|
|
// NOTE: Full-screen change, not working properly...
|
|
|
|
//glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No-fullscreen window creation
|
|
|
|
window = glfwCreateWindow(screenWidth, screenHeight, windowTitle, NULL, NULL);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-10-21 19:23:49 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
// Center window on screen
|
2016-02-13 19:39:38 +03:00
|
|
|
int windowPosX = displayWidth/2 - screenWidth/2;
|
|
|
|
int windowPosY = displayHeight/2 - screenHeight/2;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-10-21 19:23:49 +03:00
|
|
|
if (windowPosX < 0) windowPosX = 0;
|
|
|
|
if (windowPosY < 0) windowPosY = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-10-21 19:23:49 +03:00
|
|
|
glfwSetWindowPos(window, windowPosX, windowPosY);
|
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
renderWidth = screenWidth;
|
|
|
|
renderHeight = screenHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_ERROR, "GLFW Failed to initialize Window");
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Display device initialized successfully");
|
2014-12-15 03:08:30 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Display size: %i x %i", displayWidth, displayHeight);
|
2014-12-15 03:08:30 +03:00
|
|
|
#endif
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Render size: %i x %i", renderWidth, renderHeight);
|
|
|
|
TraceLog(LOG_INFO, "Screen size: %i x %i", screenWidth, screenHeight);
|
|
|
|
TraceLog(LOG_INFO, "Viewport offsets: %i, %i", renderOffsetX, renderOffsetY);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 16:20:42 +03:00
|
|
|
glfwSetWindowSizeCallback(window, WindowSizeCallback); // NOTE: Resizing not allowed by default!
|
2014-09-17 00:51:31 +04:00
|
|
|
glfwSetCursorEnterCallback(window, CursorEnterCallback);
|
|
|
|
glfwSetKeyCallback(window, KeyCallback);
|
2014-12-31 20:03:32 +03:00
|
|
|
glfwSetMouseButtonCallback(window, MouseButtonCallback);
|
2016-03-20 16:20:42 +03:00
|
|
|
glfwSetCursorPosCallback(window, MouseCursorPosCallback); // Track mouse position changes
|
2014-12-31 20:03:32 +03:00
|
|
|
glfwSetCharCallback(window, CharCallback);
|
2014-09-17 00:51:31 +04:00
|
|
|
glfwSetScrollCallback(window, ScrollCallback);
|
2015-01-21 02:13:17 +03:00
|
|
|
glfwSetWindowIconifyCallback(window, WindowIconifyCallback);
|
2015-07-29 22:44:27 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
glfwSetDropCallback(window, WindowDropCallback);
|
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
2017-03-05 21:17:00 +03:00
|
|
|
|
|
|
|
// Try to disable GPU V-Sync by default, set framerate using SetTargetFPS()
|
|
|
|
// NOTE: V-Sync can be enabled by graphic driver configuration
|
|
|
|
glfwSwapInterval(0);
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-07 13:35:36 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2016-06-14 16:42:04 +03:00
|
|
|
// Load OpenGL 3.3 extensions
|
|
|
|
// NOTE: GLFW loader function is passed as parameter
|
2017-07-19 11:09:34 +03:00
|
|
|
rlLoadExtensions(glfwGetProcAddress);
|
2016-02-07 13:35:36 +03:00
|
|
|
#endif
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-03-05 21:17:00 +03:00
|
|
|
// Try to enable GPU V-Sync, so frames are limited to screen refresh rate (60Hz -> 60 FPS)
|
|
|
|
// NOTE: V-Sync can be enabled by graphic driver configuration
|
2015-01-21 02:13:17 +03:00
|
|
|
if (configFlags & FLAG_VSYNC_HINT)
|
|
|
|
{
|
|
|
|
glfwSwapInterval(1);
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Trying to enable VSYNC");
|
2016-08-16 12:09:55 +03:00
|
|
|
}
|
2016-04-17 12:19:32 +03:00
|
|
|
#endif // defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2014-09-17 00:51:31 +04:00
|
|
|
fullscreen = true;
|
|
|
|
|
|
|
|
// Screen size security check
|
|
|
|
if (screenWidth <= 0) screenWidth = displayWidth;
|
|
|
|
if (screenHeight <= 0) screenHeight = displayHeight;
|
|
|
|
|
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
bcm_host_init();
|
|
|
|
|
|
|
|
DISPMANX_ELEMENT_HANDLE_T dispmanElement;
|
|
|
|
DISPMANX_DISPLAY_HANDLE_T dispmanDisplay;
|
|
|
|
DISPMANX_UPDATE_HANDLE_T dispmanUpdate;
|
2014-09-30 20:22:21 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
VC_RECT_T dstRect;
|
|
|
|
VC_RECT_T srcRect;
|
|
|
|
#endif
|
|
|
|
|
2015-08-07 18:25:05 +03:00
|
|
|
EGLint samples = 0;
|
|
|
|
EGLint sampleBuffer = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
if (configFlags & FLAG_MSAA_4X_HINT)
|
2015-08-07 18:25:05 +03:00
|
|
|
{
|
|
|
|
samples = 4;
|
|
|
|
sampleBuffer = 1;
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Trying to enable MSAA x4");
|
2015-08-07 18:25:05 +03:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
const EGLint framebufferAttribs[] =
|
|
|
|
{
|
2017-05-08 22:03:48 +03:00
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, // Type of context support -> Required on RPI?
|
|
|
|
//EGL_SURFACE_TYPE, EGL_WINDOW_BIT, // Don't use it on Android!
|
2014-09-19 14:34:25 +04:00
|
|
|
EGL_RED_SIZE, 8, // RED color bit depth (alternative: 5)
|
|
|
|
EGL_GREEN_SIZE, 8, // GREEN color bit depth (alternative: 6)
|
|
|
|
EGL_BLUE_SIZE, 8, // BLUE color bit depth (alternative: 5)
|
2017-05-09 19:11:02 +03:00
|
|
|
//EGL_ALPHA_SIZE, 8, // ALPHA bit depth (required for transparent framebuffer)
|
|
|
|
//EGL_TRANSPARENT_TYPE, EGL_NONE, // Request transparent framebuffer (EGL_TRANSPARENT_RGB does not work on RPI)
|
2015-08-10 11:20:53 +03:00
|
|
|
EGL_DEPTH_SIZE, 16, // Depth buffer size (Required to use Depth testing!)
|
2014-09-19 14:34:25 +04:00
|
|
|
//EGL_STENCIL_SIZE, 8, // Stencil buffer size
|
2015-08-07 18:25:05 +03:00
|
|
|
EGL_SAMPLE_BUFFERS, sampleBuffer, // Activate MSAA
|
2015-08-10 11:20:53 +03:00
|
|
|
EGL_SAMPLES, samples, // 4x Antialiasing if activated (Free on MALI GPUs)
|
2014-09-17 00:51:31 +04:00
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint contextAttribs[] =
|
|
|
|
{
|
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint numConfigs;
|
|
|
|
|
|
|
|
// Get an EGL display connection
|
|
|
|
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
|
|
|
|
|
|
|
// Initialize the EGL display connection
|
|
|
|
eglInitialize(display, NULL, NULL);
|
|
|
|
|
|
|
|
// Get an appropriate EGL framebuffer configuration
|
|
|
|
eglChooseConfig(display, framebufferAttribs, &config, 1, &numConfigs);
|
|
|
|
|
|
|
|
// Set rendering API
|
|
|
|
eglBindAPI(EGL_OPENGL_ES_API);
|
|
|
|
|
|
|
|
// Create an EGL rendering context
|
|
|
|
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
|
|
|
|
|
|
|
|
// Create an EGL window surface
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
EGLint displayFormat;
|
|
|
|
|
|
|
|
displayWidth = ANativeWindow_getWidth(app->window);
|
|
|
|
displayHeight = ANativeWindow_getHeight(app->window);
|
|
|
|
|
|
|
|
// EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is guaranteed to be accepted by ANativeWindow_setBuffersGeometry()
|
|
|
|
// As soon as we picked a EGLConfig, we can safely reconfigure the ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID
|
|
|
|
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &displayFormat);
|
|
|
|
|
|
|
|
// At this point we need to manage render size vs screen size
|
|
|
|
// NOTE: This function use and modify global module variables: screenWidth/screenHeight and renderWidth/renderHeight and downscaleView
|
|
|
|
SetupFramebufferSize(displayWidth, displayHeight);
|
|
|
|
|
|
|
|
ANativeWindow_setBuffersGeometry(app->window, renderWidth, renderHeight, displayFormat);
|
|
|
|
//ANativeWindow_setBuffersGeometry(app->window, 0, 0, displayFormat); // Force use of native display size
|
|
|
|
|
|
|
|
surface = eglCreateWindowSurface(display, config, app->window, NULL);
|
2016-04-17 12:19:32 +03:00
|
|
|
#endif // defined(PLATFORM_ANDROID)
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-04-17 12:19:32 +03:00
|
|
|
#if defined(PLATFORM_RPI)
|
2014-09-17 00:51:31 +04:00
|
|
|
graphics_get_display_size(0, &displayWidth, &displayHeight);
|
|
|
|
|
|
|
|
// At this point we need to manage render size vs screen size
|
|
|
|
// NOTE: This function use and modify global module variables: screenWidth/screenHeight and renderWidth/renderHeight and downscaleView
|
|
|
|
SetupFramebufferSize(displayWidth, displayHeight);
|
|
|
|
|
|
|
|
dstRect.x = 0;
|
|
|
|
dstRect.y = 0;
|
|
|
|
dstRect.width = displayWidth;
|
|
|
|
dstRect.height = displayHeight;
|
|
|
|
|
|
|
|
srcRect.x = 0;
|
|
|
|
srcRect.y = 0;
|
|
|
|
srcRect.width = renderWidth << 16;
|
|
|
|
srcRect.height = renderHeight << 16;
|
|
|
|
|
|
|
|
// NOTE: RPI dispmanx windowing system takes care of srcRec scaling to dstRec by hardware (no cost)
|
|
|
|
// Take care that renderWidth/renderHeight fit on displayWidth/displayHeight aspect ratio
|
|
|
|
|
2014-09-30 20:22:21 +04:00
|
|
|
VC_DISPMANX_ALPHA_T alpha;
|
|
|
|
alpha.flags = DISPMANX_FLAGS_ALPHA_FIXED_ALL_PIXELS;
|
2017-05-09 19:11:02 +03:00
|
|
|
alpha.opacity = 255; // Set transparency level for framebuffer, requires EGLAttrib: EGL_TRANSPARENT_TYPE
|
2014-09-30 20:22:21 +04:00
|
|
|
alpha.mask = 0;
|
|
|
|
|
|
|
|
dispmanDisplay = vc_dispmanx_display_open(0); // LCD
|
2014-09-17 00:51:31 +04:00
|
|
|
dispmanUpdate = vc_dispmanx_update_start(0);
|
|
|
|
|
|
|
|
dispmanElement = vc_dispmanx_element_add(dispmanUpdate, dispmanDisplay, 0/*layer*/, &dstRect, 0/*src*/,
|
2014-09-30 20:22:21 +04:00
|
|
|
&srcRect, DISPMANX_PROTECTION_NONE, &alpha, 0/*clamp*/, DISPMANX_NO_ROTATE);
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
nativeWindow.element = dispmanElement;
|
|
|
|
nativeWindow.width = renderWidth;
|
|
|
|
nativeWindow.height = renderHeight;
|
|
|
|
vc_dispmanx_update_submit_sync(dispmanUpdate);
|
|
|
|
|
|
|
|
surface = eglCreateWindowSurface(display, config, &nativeWindow, NULL);
|
|
|
|
//---------------------------------------------------------------------------------
|
2016-04-17 12:19:32 +03:00
|
|
|
#endif // defined(PLATFORM_RPI)
|
2014-09-17 00:51:31 +04:00
|
|
|
// There must be at least one frame displayed before the buffers are swapped
|
|
|
|
//eglSwapInterval(display, 1);
|
|
|
|
|
|
|
|
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_ERROR, "Unable to attach EGL rendering context to EGL surface");
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Grab the width and height of the surface
|
|
|
|
//eglQuerySurface(display, surface, EGL_WIDTH, &renderWidth);
|
|
|
|
//eglQuerySurface(display, surface, EGL_HEIGHT, &renderHeight);
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Display device initialized successfully");
|
|
|
|
TraceLog(LOG_INFO, "Display size: %i x %i", displayWidth, displayHeight);
|
|
|
|
TraceLog(LOG_INFO, "Render size: %i x %i", renderWidth, renderHeight);
|
|
|
|
TraceLog(LOG_INFO, "Screen size: %i x %i", screenWidth, screenHeight);
|
|
|
|
TraceLog(LOG_INFO, "Viewport offsets: %i, %i", renderOffsetX, renderOffsetY);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2016-04-17 12:19:32 +03:00
|
|
|
#endif // defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-06-25 22:28:50 +03:00
|
|
|
// Initialize OpenGL context (states and resources)
|
2016-07-26 14:02:25 +03:00
|
|
|
// NOTE: screenWidth and screenHeight not used, just stored as globals
|
2016-06-26 15:13:11 +03:00
|
|
|
rlglInit(screenWidth, screenHeight);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-12-15 10:58:15 +03:00
|
|
|
// Setup default viewport
|
|
|
|
SetupViewport();
|
2016-06-26 00:28:50 +03:00
|
|
|
|
|
|
|
// Initialize internal projection and modelview matrices
|
|
|
|
// NOTE: Default to orthographic projection mode with top-left corner at (0,0)
|
|
|
|
rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (PROJECTION)
|
2016-08-16 12:09:55 +03:00
|
|
|
rlOrtho(0, renderWidth - renderOffsetX, renderHeight - renderOffsetY, 0, 0.0f, 1.0f);
|
2016-06-26 00:28:50 +03:00
|
|
|
rlMatrixMode(RL_MODELVIEW); // Switch back to MODELVIEW matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
|
2016-06-03 01:53:51 +03:00
|
|
|
|
2015-02-26 15:52:03 +03:00
|
|
|
ClearBackground(RAYWHITE); // Default background color for raylib games :P
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
2016-06-24 20:34:47 +03:00
|
|
|
windowReady = true; // IMPORTANT!
|
2015-02-26 15:52:03 +03:00
|
|
|
#endif
|
|
|
|
}
|
2015-03-01 18:00:52 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
// Set viewport parameters
|
2016-12-15 10:58:15 +03:00
|
|
|
static void SetupViewport(void)
|
|
|
|
{
|
2017-05-08 03:47:44 +03:00
|
|
|
#if defined(__APPLE__)
|
2016-12-15 10:58:15 +03:00
|
|
|
// Get framebuffer size of current window
|
|
|
|
// NOTE: Required to handle HighDPI display correctly on OSX because framebuffer
|
|
|
|
// is automatically reasized to adapt to new DPI.
|
|
|
|
// When OS does that, it can be detected using GLFW3 callback: glfwSetFramebufferSizeCallback()
|
|
|
|
int fbWidth, fbHeight;
|
|
|
|
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
|
|
|
|
rlViewport(renderOffsetX/2, renderOffsetY/2, fbWidth - renderOffsetX, fbHeight - renderOffsetY);
|
|
|
|
#else
|
|
|
|
// Initialize screen viewport (area of the screen that you will actually draw to)
|
|
|
|
// NOTE: Viewport must be recalculated if screen is resized
|
|
|
|
rlViewport(renderOffsetX/2, renderOffsetY/2, renderWidth - renderOffsetX, renderHeight - renderOffsetY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Compute framebuffer size relative to screen size and display size
|
2016-06-24 20:34:47 +03:00
|
|
|
// NOTE: Global variables renderWidth/renderHeight and renderOffsetX/renderOffsetY can be modified
|
2016-05-02 15:11:57 +03:00
|
|
|
static void SetupFramebufferSize(int displayWidth, int displayHeight)
|
2016-08-16 12:09:55 +03:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
// Calculate renderWidth and renderHeight, we have the display size (input params) and the desired screen size (global var)
|
|
|
|
if ((screenWidth > displayWidth) || (screenHeight > displayHeight))
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "DOWNSCALING: Required screen size (%ix%i) is bigger than display size (%ix%i)", screenWidth, screenHeight, displayWidth, displayHeight);
|
2013-11-19 02:38:44 +04:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Downscaling to fit display with border-bars
|
|
|
|
float widthRatio = (float)displayWidth/(float)screenWidth;
|
|
|
|
float heightRatio = (float)displayHeight/(float)screenHeight;
|
2014-01-29 00:21:29 +04:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
if (widthRatio <= heightRatio)
|
|
|
|
{
|
|
|
|
renderWidth = displayWidth;
|
|
|
|
renderHeight = (int)round((float)screenHeight*widthRatio);
|
|
|
|
renderOffsetX = 0;
|
|
|
|
renderOffsetY = (displayHeight - renderHeight);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
renderWidth = (int)round((float)screenWidth*heightRatio);
|
|
|
|
renderHeight = displayHeight;
|
|
|
|
renderOffsetX = (displayWidth - renderWidth);
|
|
|
|
renderOffsetY = 0;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// NOTE: downscale matrix required!
|
|
|
|
float scaleRatio = (float)renderWidth/(float)screenWidth;
|
|
|
|
|
|
|
|
downscaleView = MatrixScale(scaleRatio, scaleRatio, scaleRatio);
|
|
|
|
|
|
|
|
// NOTE: We render to full display resolution!
|
|
|
|
// We just need to calculate above parameters for downscale matrix and offsets
|
|
|
|
renderWidth = displayWidth;
|
|
|
|
renderHeight = displayHeight;
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Downscale matrix generated, content will be rendered at: %i x %i", renderWidth, renderHeight);
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2016-05-02 15:11:57 +03:00
|
|
|
else if ((screenWidth < displayWidth) || (screenHeight < displayHeight))
|
2013-11-23 16:30:54 +04:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
// Required screen size is smaller than display size
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "UPSCALING: Required screen size: %i x %i -> Display size: %i x %i", screenWidth, screenHeight, displayWidth, displayHeight);
|
2016-05-02 15:11:57 +03:00
|
|
|
|
|
|
|
// Upscaling to fit display with border-bars
|
|
|
|
float displayRatio = (float)displayWidth/(float)displayHeight;
|
|
|
|
float screenRatio = (float)screenWidth/(float)screenHeight;
|
|
|
|
|
|
|
|
if (displayRatio <= screenRatio)
|
|
|
|
{
|
|
|
|
renderWidth = screenWidth;
|
|
|
|
renderHeight = (int)round((float)screenWidth/displayRatio);
|
|
|
|
renderOffsetX = 0;
|
|
|
|
renderOffsetY = (renderHeight - screenHeight);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
renderWidth = (int)round((float)screenHeight*displayRatio);
|
|
|
|
renderHeight = screenHeight;
|
|
|
|
renderOffsetX = (renderWidth - screenWidth);
|
|
|
|
renderOffsetY = 0;
|
|
|
|
}
|
2013-11-23 16:30:54 +04:00
|
|
|
}
|
2016-05-02 15:11:57 +03:00
|
|
|
else // screen == display
|
2016-03-06 21:30:16 +03:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
renderWidth = screenWidth;
|
|
|
|
renderHeight = screenHeight;
|
|
|
|
renderOffsetX = 0;
|
|
|
|
renderOffsetY = 0;
|
2016-03-06 21:30:16 +03:00
|
|
|
}
|
2014-12-31 20:03:32 +03:00
|
|
|
}
|
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Initialize hi-resolution timer
|
|
|
|
static void InitTimer(void)
|
2014-12-31 20:03:32 +03:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
srand(time(NULL)); // Initialize random seed
|
2017-05-08 03:37:37 +03:00
|
|
|
|
|
|
|
#if !defined(SUPPORT_BUSY_WAIT_LOOP) && defined(_WIN32)
|
|
|
|
timeBeginPeriod(1); // Setup high-resolution timer to 1ms (granularity of 1-2 ms)
|
|
|
|
#endif
|
2016-05-02 15:11:57 +03:00
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
|
|
|
struct timespec now;
|
|
|
|
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) // Success
|
|
|
|
{
|
|
|
|
baseTime = (uint64_t)now.tv_sec*1000000000LLU + (uint64_t)now.tv_nsec;
|
|
|
|
}
|
2017-07-02 13:35:13 +03:00
|
|
|
else TraceLog(LOG_WARNING, "No hi-resolution timer available");
|
2016-02-02 18:43:42 +03:00
|
|
|
#endif
|
2016-05-02 15:11:57 +03:00
|
|
|
|
|
|
|
previousTime = GetTime(); // Get time as double
|
2016-02-02 18:43:42 +03:00
|
|
|
}
|
|
|
|
|
2017-12-18 02:06:57 +03:00
|
|
|
// Get elapsed time measure (in seconds)
|
2017-12-15 15:44:31 +03:00
|
|
|
static double GetTime(void)
|
2016-02-02 18:43:42 +03:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
2017-12-18 02:06:57 +03:00
|
|
|
return glfwGetTime(); // Elapsed time since glfwInit()
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2016-05-02 15:11:57 +03:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
uint64_t time = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec;
|
2016-02-02 18:43:42 +03:00
|
|
|
|
2017-12-18 02:06:57 +03:00
|
|
|
return (double)(time - baseTime)*1e-9; // Elapsed time since InitTimer()
|
2016-01-24 21:17:08 +03:00
|
|
|
#endif
|
2014-12-31 20:03:32 +03:00
|
|
|
}
|
|
|
|
|
2017-01-28 02:56:45 +03:00
|
|
|
// Wait for some milliseconds (stop program execution)
|
2017-03-28 20:15:27 +03:00
|
|
|
// NOTE: Sleep() granularity could be around 10 ms, it means, Sleep() could
|
|
|
|
// take longer than expected... for that reason we use the busy wait loop
|
|
|
|
// http://stackoverflow.com/questions/43057578/c-programming-win32-games-sleep-taking-longer-than-expected
|
2017-03-05 21:17:00 +03:00
|
|
|
static void Wait(float ms)
|
|
|
|
{
|
|
|
|
#if defined(SUPPORT_BUSY_WAIT_LOOP)
|
2017-01-28 02:56:45 +03:00
|
|
|
double prevTime = GetTime();
|
|
|
|
double nextTime = 0.0;
|
|
|
|
|
|
|
|
// Busy wait loop
|
2017-03-05 21:17:00 +03:00
|
|
|
while ((nextTime - prevTime) < ms/1000.0f) nextTime = GetTime();
|
|
|
|
#else
|
2017-05-08 03:37:37 +03:00
|
|
|
#if defined(_WIN32)
|
2017-03-28 20:15:27 +03:00
|
|
|
Sleep((unsigned int)ms);
|
2017-05-08 03:37:37 +03:00
|
|
|
#elif defined(__linux__) || defined(PLATFORM_WEB)
|
2017-03-05 21:17:00 +03:00
|
|
|
struct timespec req = { 0 };
|
|
|
|
time_t sec = (int)(ms/1000.0f);
|
|
|
|
ms -= (sec*1000);
|
|
|
|
req.tv_sec = sec;
|
|
|
|
req.tv_nsec = ms*1000000L;
|
|
|
|
|
|
|
|
// NOTE: Use nanosleep() on Unix platforms... usleep() it's deprecated.
|
|
|
|
while (nanosleep(&req, &req) == -1) continue;
|
2017-05-08 03:37:37 +03:00
|
|
|
#elif defined(__APPLE__)
|
2017-03-05 21:17:00 +03:00
|
|
|
usleep(ms*1000.0f);
|
|
|
|
#endif
|
2017-01-28 02:56:45 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Get one key state
|
|
|
|
static bool GetKeyStatus(int key)
|
2014-12-31 20:03:32 +03:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
|
|
|
return glfwGetKey(window, key);
|
|
|
|
#elif defined(PLATFORM_ANDROID)
|
2016-10-17 19:18:13 +03:00
|
|
|
// NOTE: Android supports up to 260 keys
|
|
|
|
if (key < 0 || key > 260) return false;
|
|
|
|
else return currentKeyState[key];
|
2016-05-02 15:11:57 +03:00
|
|
|
#elif defined(PLATFORM_RPI)
|
|
|
|
// NOTE: Keys states are filled in PollInputEvents()
|
|
|
|
if (key < 0 || key > 511) return false;
|
|
|
|
else return currentKeyState[key];
|
|
|
|
#endif
|
2013-11-19 02:38:44 +04:00
|
|
|
}
|
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Get one mouse button state
|
|
|
|
static bool GetMouseButtonStatus(int button)
|
2013-12-19 15:08:06 +04:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
|
|
|
return glfwGetMouseButton(window, button);
|
|
|
|
#elif defined(PLATFORM_ANDROID)
|
2016-12-25 22:42:22 +03:00
|
|
|
// TODO: Check for virtual mouse?
|
2016-05-02 15:11:57 +03:00
|
|
|
return false;
|
|
|
|
#elif defined(PLATFORM_RPI)
|
|
|
|
// NOTE: Mouse buttons states are filled in PollInputEvents()
|
|
|
|
return currentMouseState[button];
|
|
|
|
#endif
|
2013-12-19 15:08:06 +04:00
|
|
|
}
|
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Poll (store) all input events
|
|
|
|
static void PollInputEvents(void)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_GESTURES_SYSTEM)
|
2016-05-02 15:11:57 +03:00
|
|
|
// NOTE: Gestures update must be called every frame to reset gestures correctly
|
|
|
|
// because ProcessGestureEvent() is just called on an event, not every frame
|
|
|
|
UpdateGestures();
|
2017-03-21 15:22:59 +03:00
|
|
|
#endif
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-17 19:18:13 +03:00
|
|
|
// Reset last key pressed registered
|
|
|
|
lastKeyPressed = -1;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-11-02 15:39:48 +03:00
|
|
|
#if !defined(PLATFORM_RPI)
|
|
|
|
// Reset last gamepad button/axis registered state
|
2016-10-27 14:41:43 +03:00
|
|
|
lastGamepadButtonPressed = -1;
|
2016-11-01 16:39:57 +03:00
|
|
|
gamepadAxisCount = 0;
|
2016-11-02 15:39:48 +03:00
|
|
|
#endif
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
|
|
|
// Mouse input polling
|
|
|
|
double mouseX;
|
|
|
|
double mouseY;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
glfwGetCursorPos(window, &mouseX, &mouseY);
|
2015-01-21 02:13:17 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
mousePosition.x = (float)mouseX;
|
|
|
|
mousePosition.y = (float)mouseY;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Keyboard input polling (automatically managed by GLFW3 through callback)
|
|
|
|
|
|
|
|
// Register previous keys states
|
|
|
|
for (int i = 0; i < 512; i++) previousKeyState[i] = currentKeyState[i];
|
|
|
|
|
|
|
|
// Register previous mouse states
|
|
|
|
for (int i = 0; i < 3; i++) previousMouseState[i] = currentMouseState[i];
|
|
|
|
|
|
|
|
previousMouseWheelY = currentMouseWheelY;
|
|
|
|
currentMouseWheelY = 0;
|
2016-11-18 15:39:57 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2016-11-01 16:39:57 +03:00
|
|
|
// Check if gamepads are ready
|
|
|
|
// NOTE: We do it here in case of disconection
|
|
|
|
for (int i = 0; i < MAX_GAMEPADS; i++)
|
|
|
|
{
|
|
|
|
if (glfwJoystickPresent(i)) gamepadReady[i] = true;
|
|
|
|
else gamepadReady[i] = false;
|
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
// Register gamepads buttons events
|
|
|
|
for (int i = 0; i < MAX_GAMEPADS; i++)
|
2016-10-14 01:47:43 +03:00
|
|
|
{
|
2016-11-01 16:39:57 +03:00
|
|
|
if (gamepadReady[i]) // Check if gamepad is available
|
2016-10-14 01:47:43 +03:00
|
|
|
{
|
2016-10-14 12:14:41 +03:00
|
|
|
// Register previous gamepad states
|
|
|
|
for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) previousGamepadState[i][k] = currentGamepadState[i][k];
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
// Get current gamepad state
|
|
|
|
// NOTE: There is no callback available, so we get it manually
|
|
|
|
const unsigned char *buttons;
|
|
|
|
int buttonsCount;
|
|
|
|
|
|
|
|
buttons = glfwGetJoystickButtons(i, &buttonsCount);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
for (int k = 0; (buttons != NULL) && (k < buttonsCount) && (buttonsCount < MAX_GAMEPAD_BUTTONS); k++)
|
|
|
|
{
|
2016-10-27 14:41:43 +03:00
|
|
|
if (buttons[k] == GLFW_PRESS)
|
|
|
|
{
|
|
|
|
currentGamepadState[i][k] = 1;
|
|
|
|
lastGamepadButtonPressed = k;
|
|
|
|
}
|
2016-10-14 12:14:41 +03:00
|
|
|
else currentGamepadState[i][k] = 0;
|
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
// Get current axis state
|
|
|
|
const float *axes;
|
|
|
|
int axisCount = 0;
|
|
|
|
|
|
|
|
axes = glfwGetJoystickAxes(i, &axisCount);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-14 12:14:41 +03:00
|
|
|
for (int k = 0; (axes != NULL) && (k < axisCount) && (k < MAX_GAMEPAD_AXIS); k++)
|
|
|
|
{
|
|
|
|
gamepadAxisState[i][k] = axes[k];
|
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-11-01 16:39:57 +03:00
|
|
|
gamepadAxisCount = axisCount;
|
2016-10-14 01:47:43 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-02 15:11:57 +03:00
|
|
|
|
2016-10-14 01:47:43 +03:00
|
|
|
glfwPollEvents(); // Register keyboard/mouse events (callbacks)... and window events!
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
2016-05-02 15:11:57 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
// Gamepad support using emscripten API
|
|
|
|
// NOTE: GLFW3 joystick functionality not available in web
|
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
// Get number of gamepads connected
|
|
|
|
int numGamepads = emscripten_get_num_gamepads();
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
for (int i = 0; (i < numGamepads) && (i < MAX_GAMEPADS); i++)
|
|
|
|
{
|
|
|
|
// Register previous gamepad button states
|
|
|
|
for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) previousGamepadState[i][k] = currentGamepadState[i][k];
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
EmscriptenGamepadEvent gamepadState;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
int result = emscripten_get_gamepad_status(i, &gamepadState);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
if (result == EMSCRIPTEN_RESULT_SUCCESS)
|
|
|
|
{
|
|
|
|
// Register buttons data for every connected gamepad
|
|
|
|
for (int j = 0; (j < gamepadState.numButtons) && (j < MAX_GAMEPAD_BUTTONS); j++)
|
|
|
|
{
|
|
|
|
if (gamepadState.digitalButton[j] == 1)
|
|
|
|
{
|
|
|
|
currentGamepadState[i][j] = 1;
|
|
|
|
lastGamepadButtonPressed = j;
|
|
|
|
}
|
|
|
|
else currentGamepadState[i][j] = 0;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
//printf("Gamepad %d, button %d: Digital: %d, Analog: %g\n", gamepadState.index, j, gamepadState.digitalButton[j], gamepadState.analogButton[j]);
|
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
// Register axis data for every connected gamepad
|
|
|
|
for (int j = 0; (j < gamepadState.numAxes) && (j < MAX_GAMEPAD_AXIS); j++)
|
|
|
|
{
|
|
|
|
gamepadAxisState[i][j] = gamepadState.axis[j];
|
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
gamepadAxisCount = gamepadState.numAxes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-03 01:53:51 +03:00
|
|
|
#if defined(PLATFORM_ANDROID)
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register previous keys states
|
2016-10-17 19:18:13 +03:00
|
|
|
// NOTE: Android supports up to 260 keys
|
|
|
|
for (int i = 0; i < 260; i++) previousKeyState[i] = currentKeyState[i];
|
2016-05-02 15:11:57 +03:00
|
|
|
|
|
|
|
// Poll Events (registered events)
|
|
|
|
// NOTE: Activity is paused if not enabled (appEnabled)
|
|
|
|
while ((ident = ALooper_pollAll(appEnabled ? 0 : -1, NULL, &events,(void**)&source)) >= 0)
|
|
|
|
{
|
|
|
|
// Process this event
|
|
|
|
if (source != NULL) source->process(app, source);
|
|
|
|
|
|
|
|
// NOTE: Never close window, native activity is controlled by the system!
|
|
|
|
if (app->destroyRequested != 0)
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
//TraceLog(LOG_INFO, "Closing Window...");
|
2016-05-02 15:11:57 +03:00
|
|
|
//windowShouldClose = true;
|
|
|
|
//ANativeActivity_finish(app->activity);
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
2016-05-02 15:11:57 +03:00
|
|
|
|
2016-06-03 01:53:51 +03:00
|
|
|
#if defined(PLATFORM_RPI)
|
2016-05-02 15:11:57 +03:00
|
|
|
// NOTE: Mouse input events polling is done asynchonously in another pthread - MouseThread()
|
|
|
|
|
|
|
|
// NOTE: Keyboard reading could be done using input_event(s) reading or just read from stdin,
|
|
|
|
// we use method 2 (stdin) but maybe in a future we should change to method 1...
|
|
|
|
ProcessKeyboard();
|
|
|
|
|
|
|
|
// NOTE: Gamepad (Joystick) input events polling is done asynchonously in another pthread - GamepadThread()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy back buffer to front buffers
|
|
|
|
static void SwapBuffers(void)
|
|
|
|
{
|
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
|
|
|
glfwSwapBuffers(window);
|
2016-06-03 01:53:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI)
|
2016-05-02 15:11:57 +03:00
|
|
|
eglSwapBuffers(display, surface);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
|
|
|
// GLFW3 Error Callback, runs on GLFW3 error
|
|
|
|
static void ErrorCallback(int error, const char *description)
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "[GLFW3 Error] Code: %i Decription: %s", error, description);
|
2016-05-02 15:11:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 Srolling Callback, runs on mouse wheel
|
|
|
|
static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
|
|
|
|
{
|
|
|
|
currentMouseWheelY = (int)yoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 Keyboard Callback, runs on key pressed
|
|
|
|
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
|
|
|
{
|
|
|
|
if (key == exitKey && action == GLFW_PRESS)
|
|
|
|
{
|
|
|
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
|
|
|
|
|
|
|
// NOTE: Before closing window, while loop must be left!
|
|
|
|
}
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
2017-05-11 17:24:40 +03:00
|
|
|
else if (key == GLFW_KEY_F12 && action == GLFW_PRESS)
|
|
|
|
{
|
2017-05-18 19:57:11 +03:00
|
|
|
#if defined(SUPPORT_GIF_RECORDING)
|
|
|
|
if (mods == GLFW_MOD_CONTROL)
|
|
|
|
{
|
|
|
|
if (gifRecording)
|
|
|
|
{
|
2017-05-27 15:40:05 +03:00
|
|
|
GifEnd();
|
2017-05-18 19:57:11 +03:00
|
|
|
gifRecording = false;
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "End animated GIF recording");
|
2017-05-18 19:57:11 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gifRecording = true;
|
|
|
|
gifFramesCounter = 0;
|
|
|
|
|
|
|
|
// 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.
|
2017-05-27 15:40:05 +03:00
|
|
|
GifBegin(FormatText("screenrec%03i.gif", screenshotCounter), screenWidth, screenHeight, (int)(GetFrameTime()*10.0f), 8, false);
|
2017-05-18 19:57:11 +03:00
|
|
|
screenshotCounter++;
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Begin animated GIF recording: %s", FormatText("screenrec%03i.gif", screenshotCounter));
|
2017-05-18 19:57:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // SUPPORT_GIF_RECORDING
|
|
|
|
{
|
|
|
|
TakeScreenshot(FormatText("screenshot%03i.png", screenshotCounter));
|
|
|
|
screenshotCounter++;
|
|
|
|
}
|
2017-05-11 17:24:40 +03:00
|
|
|
}
|
2017-05-18 19:57:11 +03:00
|
|
|
#endif // PLATFORM_DESKTOP
|
2016-08-16 12:09:55 +03:00
|
|
|
else
|
2016-05-02 15:11:57 +03:00
|
|
|
{
|
|
|
|
currentKeyState[key] = action;
|
|
|
|
if (action == GLFW_PRESS) lastKeyPressed = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 Mouse Button Callback, runs on mouse button pressed
|
|
|
|
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
|
|
|
|
{
|
|
|
|
currentMouseState[button] = action;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
|
2016-05-02 15:11:57 +03:00
|
|
|
// Process mouse events as touches to be able to use mouse-gestures
|
|
|
|
GestureEvent gestureEvent;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch actions
|
|
|
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_DOWN;
|
|
|
|
else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) gestureEvent.touchAction = TOUCH_UP;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// NOTE: TOUCH_MOVE event is registered in MouseCursorPosCallback()
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Assign a pointer ID
|
|
|
|
gestureEvent.pointerId[0] = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch points count
|
|
|
|
gestureEvent.pointCount = 1;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch points position, only one point registered
|
|
|
|
gestureEvent.position[0] = GetMousePosition();
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Normalize gestureEvent.position[0] for screenWidth and screenHeight
|
2016-05-31 20:12:37 +03:00
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
2016-05-02 15:11:57 +03:00
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
2016-05-31 20:12:37 +03:00
|
|
|
|
|
|
|
// Gesture data is sent to gestures system for processing
|
2016-05-02 15:11:57 +03:00
|
|
|
ProcessGestureEvent(gestureEvent);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 Cursor Position Callback, runs on mouse move
|
|
|
|
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
|
|
|
|
{
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
|
2016-05-02 15:11:57 +03:00
|
|
|
// Process mouse events as touches to be able to use mouse-gestures
|
|
|
|
GestureEvent gestureEvent;
|
|
|
|
|
|
|
|
gestureEvent.touchAction = TOUCH_MOVE;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Assign a pointer ID
|
|
|
|
gestureEvent.pointerId[0] = 0;
|
|
|
|
|
|
|
|
// Register touch points count
|
|
|
|
gestureEvent.pointCount = 1;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch points position, only one point registered
|
|
|
|
gestureEvent.position[0] = (Vector2){ (float)x, (float)y };
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
touchPosition[0] = gestureEvent.position[0];
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Normalize gestureEvent.position[0] for screenWidth and screenHeight
|
2016-08-16 12:09:55 +03:00
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
2016-05-02 15:11:57 +03:00
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
|
|
|
|
|
|
|
// Gesture data is sent to gestures system for processing
|
|
|
|
ProcessGestureEvent(gestureEvent);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 Char Key Callback, runs on key pressed (get char value)
|
|
|
|
static void CharCallback(GLFWwindow *window, unsigned int key)
|
|
|
|
{
|
|
|
|
lastKeyPressed = key;
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
//TraceLog(LOG_INFO, "Char Callback Key pressed: %i\n", key);
|
2016-05-02 15:11:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 CursorEnter Callback, when cursor enters the window
|
|
|
|
static void CursorEnterCallback(GLFWwindow *window, int enter)
|
|
|
|
{
|
|
|
|
if (enter == true) cursorOnScreen = true;
|
|
|
|
else cursorOnScreen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 WindowSize Callback, runs when window is resized
|
|
|
|
// NOTE: Window resizing not allowed by default
|
|
|
|
static void WindowSizeCallback(GLFWwindow *window, int width, int height)
|
|
|
|
{
|
2016-06-26 00:28:50 +03:00
|
|
|
// If window is resized, viewport and projection matrix needs to be re-calculated
|
|
|
|
rlViewport(0, 0, width, height); // Set viewport width and height
|
|
|
|
rlMatrixMode(RL_PROJECTION); // Switch to PROJECTION matrix
|
|
|
|
rlLoadIdentity(); // Reset current matrix (PROJECTION)
|
|
|
|
rlOrtho(0, width, 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)
|
2016-05-02 15:11:57 +03:00
|
|
|
|
|
|
|
// Window size must be updated to be used on 3D mode to get new aspect ratio (Begin3dMode())
|
2017-11-02 22:08:52 +03:00
|
|
|
// 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)
|
2016-05-02 15:11:57 +03:00
|
|
|
screenWidth = width;
|
|
|
|
screenHeight = height;
|
|
|
|
renderWidth = width;
|
|
|
|
renderHeight = height;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// NOTE: Postprocessing texture is not scaled to new size
|
|
|
|
}
|
|
|
|
|
|
|
|
// GLFW3 WindowIconify Callback, runs when window is minimized/restored
|
|
|
|
static void WindowIconifyCallback(GLFWwindow* window, int iconified)
|
|
|
|
{
|
|
|
|
if (iconified) windowMinimized = true; // The window was iconified
|
2015-07-29 22:44:27 +03:00
|
|
|
else windowMinimized = false; // The window was restored
|
|
|
|
}
|
|
|
|
#endif
|
2015-02-02 02:57:08 +03:00
|
|
|
|
2015-07-29 22:44:27 +03:00
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
// GLFW3 Window Drop Callback, runs when drop files into window
|
|
|
|
// NOTE: Paths are stored in dinamic memory for further retrieval
|
|
|
|
// Everytime new files are dropped, old ones are discarded
|
|
|
|
static void WindowDropCallback(GLFWwindow *window, int count, const char **paths)
|
|
|
|
{
|
|
|
|
ClearDroppedFiles();
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-07-29 22:44:27 +03:00
|
|
|
dropFilesPath = (char **)malloc(sizeof(char *)*count);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2015-07-29 22:44:27 +03:00
|
|
|
for (int i = 0; i < count; i++)
|
2015-01-21 02:13:17 +03:00
|
|
|
{
|
2015-07-29 22:44:27 +03:00
|
|
|
dropFilesPath[i] = (char *)malloc(sizeof(char)*256); // Max path length set to 256 char
|
|
|
|
strcpy(dropFilesPath[i], paths[i]);
|
2015-01-21 02:13:17 +03:00
|
|
|
}
|
2015-07-29 22:44:27 +03:00
|
|
|
|
|
|
|
dropFilesCount = count;
|
2015-01-21 02:13:17 +03:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PLATFORM_ANDROID)
|
|
|
|
// Android: Process activity lifecycle commands
|
2015-04-22 18:34:42 +03:00
|
|
|
static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case APP_CMD_START:
|
|
|
|
{
|
|
|
|
//rendering = true;
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_START");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_RESUME:
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_RESUME");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_INIT_WINDOW:
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_INIT_WINDOW");
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
if (app->window != NULL)
|
|
|
|
{
|
2016-01-03 15:01:21 +03:00
|
|
|
if (contextRebindRequired)
|
|
|
|
{
|
|
|
|
// Reset screen scaling to full display size
|
|
|
|
EGLint displayFormat;
|
|
|
|
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &displayFormat);
|
|
|
|
ANativeWindow_setBuffersGeometry(app->window, renderWidth, renderHeight, displayFormat);
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Recreate display surface and re-attach OpenGL context
|
|
|
|
surface = eglCreateWindowSurface(display, config, app->window, NULL);
|
|
|
|
eglMakeCurrent(display, surface, surface, context);
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
contextRebindRequired = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-25 22:28:50 +03:00
|
|
|
// Init graphics device (display device and OpenGL context)
|
|
|
|
InitGraphicsDevice(screenWidth, screenHeight);
|
2016-01-03 15:01:21 +03:00
|
|
|
|
2017-03-21 15:22:59 +03:00
|
|
|
#if defined(SUPPORT_DEFAULT_FONT)
|
2016-10-09 14:09:08 +03:00
|
|
|
// Load default font
|
2016-01-03 15:01:21 +03:00
|
|
|
// NOTE: External function (defined in module: text)
|
|
|
|
LoadDefaultFont();
|
2016-10-09 14:09:08 +03:00
|
|
|
#endif
|
2016-05-31 20:12:37 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// TODO: GPU assets reload in case of lost focus (lost context)
|
|
|
|
// NOTE: This problem has been solved just unbinding and rebinding context from display
|
2016-05-31 20:12:37 +03:00
|
|
|
/*
|
2016-01-03 15:01:21 +03:00
|
|
|
if (assetsReloadRequired)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < assetsCount; i++)
|
|
|
|
{
|
|
|
|
// TODO: Unload old asset if required
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Load texture again to pointed texture
|
|
|
|
(*textureAsset + i) = LoadTexture(assetPath[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Init hi-res timer
|
|
|
|
InitTimer();
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// raylib logo appearing animation (if enabled)
|
|
|
|
if (showLogo)
|
|
|
|
{
|
|
|
|
SetTargetFPS(60); // Not required on Android
|
|
|
|
LogoAnimation();
|
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case APP_CMD_GAINED_FOCUS:
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_GAINED_FOCUS");
|
2016-01-03 15:01:21 +03:00
|
|
|
appEnabled = true;
|
2015-07-29 22:44:27 +03:00
|
|
|
//ResumeMusicStream();
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_PAUSE:
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_PAUSE");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_LOST_FOCUS:
|
|
|
|
{
|
|
|
|
//DrawFrame();
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_LOST_FOCUS");
|
2016-01-03 15:01:21 +03:00
|
|
|
appEnabled = false;
|
2015-07-29 22:44:27 +03:00
|
|
|
//PauseMusicStream();
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_TERM_WINDOW:
|
|
|
|
{
|
2016-01-03 15:01:21 +03:00
|
|
|
// Dettach OpenGL context and destroy display surface
|
|
|
|
// NOTE 1: Detaching context before destroying display surface avoids losing our resources (textures, shaders, VBOs...)
|
|
|
|
// NOTE 2: In some cases (too many context loaded), OS could unload context automatically... :(
|
|
|
|
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
eglDestroySurface(display, surface);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
contextRebindRequired = true;
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_TERM_WINDOW");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_SAVE_STATE:
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_SAVE_STATE");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_STOP:
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_STOP");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_DESTROY:
|
|
|
|
{
|
|
|
|
// TODO: Finish activity?
|
|
|
|
//ANativeActivity_finish(app->activity);
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_DESTROY");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
case APP_CMD_CONFIG_CHANGED:
|
|
|
|
{
|
|
|
|
//AConfiguration_fromAssetManager(app->config, app->activity->assetManager);
|
|
|
|
//print_cur_config(app);
|
|
|
|
|
|
|
|
// Check screen orientation here!
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "APP_CMD_CONFIG_CHANGED");
|
2014-09-17 00:51:31 +04:00
|
|
|
} break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2016-01-03 15:01:21 +03:00
|
|
|
|
|
|
|
// Android: Get input events
|
|
|
|
static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
|
|
|
|
{
|
|
|
|
//http://developer.android.com/ndk/reference/index.html
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
int type = AInputEvent_getType(event);
|
|
|
|
|
|
|
|
if (type == AINPUT_EVENT_TYPE_MOTION)
|
|
|
|
{
|
2016-02-10 12:31:06 +03:00
|
|
|
// Get first touch position
|
|
|
|
touchPosition[0].x = AMotionEvent_getX(event, 0);
|
|
|
|
touchPosition[0].y = AMotionEvent_getY(event, 0);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-10 12:31:06 +03:00
|
|
|
// Get second touch position
|
|
|
|
touchPosition[1].x = AMotionEvent_getX(event, 1);
|
|
|
|
touchPosition[1].y = AMotionEvent_getY(event, 1);
|
2016-01-03 15:01:21 +03:00
|
|
|
}
|
|
|
|
else if (type == AINPUT_EVENT_TYPE_KEY)
|
|
|
|
{
|
|
|
|
int32_t keycode = AKeyEvent_getKeyCode(event);
|
|
|
|
//int32_t AKeyEvent_getMetaState(event);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-05 14:21:40 +03:00
|
|
|
// Save current button and its state
|
2016-10-17 19:18:13 +03:00
|
|
|
// NOTE: Android key action is 0 for down and 1 for up
|
|
|
|
if (AKeyEvent_getAction(event) == 0)
|
|
|
|
{
|
|
|
|
currentKeyState[keycode] = 1; // Key down
|
|
|
|
lastKeyPressed = keycode;
|
|
|
|
}
|
|
|
|
else currentKeyState[keycode] = 0; // Key up
|
2016-01-03 15:01:21 +03:00
|
|
|
|
2016-01-05 14:21:40 +03:00
|
|
|
if (keycode == AKEYCODE_POWER)
|
|
|
|
{
|
|
|
|
// Let the OS handle input to avoid app stuck. Behaviour: CMD_PAUSE -> CMD_SAVE_STATE -> CMD_STOP -> CMD_CONFIG_CHANGED -> CMD_LOST_FOCUS
|
|
|
|
// Resuming Behaviour: CMD_START -> CMD_RESUME -> CMD_CONFIG_CHANGED -> CMD_CONFIG_CHANGED -> CMD_GAINED_FOCUS
|
|
|
|
// It seems like locking mobile, screen size (CMD_CONFIG_CHANGED) is affected.
|
|
|
|
// NOTE: AndroidManifest.xml must have <activity android:configChanges="orientation|keyboardHidden|screenSize" >
|
|
|
|
// Before that change, activity was calling CMD_TERM_WINDOW and CMD_DESTROY when locking mobile, so that was not a normal behaviour
|
|
|
|
return 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
}
|
2016-01-05 14:21:40 +03:00
|
|
|
else if ((keycode == AKEYCODE_BACK) || (keycode == AKEYCODE_MENU))
|
2016-01-03 15:01:21 +03:00
|
|
|
{
|
2016-01-04 23:00:20 +03:00
|
|
|
// Eat BACK_BUTTON and AKEYCODE_MENU, just do nothing... and don't let to be handled by OS!
|
2016-01-03 15:01:21 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ((keycode == AKEYCODE_VOLUME_UP) || (keycode == AKEYCODE_VOLUME_DOWN))
|
|
|
|
{
|
|
|
|
// Set default OS behaviour
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
int32_t action = AMotionEvent_getAction(event);
|
|
|
|
unsigned int flags = action & AMOTION_EVENT_ACTION_MASK;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-09-29 14:56:37 +03:00
|
|
|
#if defined(SUPPORT_GESTURES_SYSTEM)
|
2016-01-03 15:01:21 +03:00
|
|
|
GestureEvent gestureEvent;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Register touch actions
|
|
|
|
if (flags == AMOTION_EVENT_ACTION_DOWN) gestureEvent.touchAction = TOUCH_DOWN;
|
|
|
|
else if (flags == AMOTION_EVENT_ACTION_UP) gestureEvent.touchAction = TOUCH_UP;
|
|
|
|
else if (flags == AMOTION_EVENT_ACTION_MOVE) gestureEvent.touchAction = TOUCH_MOVE;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Register touch points count
|
|
|
|
gestureEvent.pointCount = AMotionEvent_getPointerCount(event);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-02 18:43:42 +03:00
|
|
|
// Register touch points id
|
2016-01-28 12:03:37 +03:00
|
|
|
gestureEvent.pointerId[0] = AMotionEvent_getPointerId(event, 0);
|
|
|
|
gestureEvent.pointerId[1] = AMotionEvent_getPointerId(event, 1);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Register touch points position
|
|
|
|
// NOTE: Only two points registered
|
|
|
|
gestureEvent.position[0] = (Vector2){ AMotionEvent_getX(event, 0), AMotionEvent_getY(event, 0) };
|
|
|
|
gestureEvent.position[1] = (Vector2){ AMotionEvent_getX(event, 1), AMotionEvent_getY(event, 1) };
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-17 15:00:48 +03:00
|
|
|
// Normalize gestureEvent.position[x] for screenWidth and screenHeight
|
2016-08-16 12:09:55 +03:00
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
2016-02-17 15:00:48 +03:00
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
2016-08-16 12:09:55 +03:00
|
|
|
|
|
|
|
gestureEvent.position[1].x /= (float)GetScreenWidth();
|
2016-02-17 15:00:48 +03:00
|
|
|
gestureEvent.position[1].y /= (float)GetScreenHeight();
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-01-03 15:01:21 +03:00
|
|
|
// Gesture data is sent to gestures system for processing
|
|
|
|
ProcessGestureEvent(gestureEvent);
|
2017-09-29 14:56:37 +03:00
|
|
|
#else
|
|
|
|
|
2017-11-02 22:08:52 +03:00
|
|
|
// Support only simple touch position
|
|
|
|
if (flags == AMOTION_EVENT_ACTION_DOWN)
|
|
|
|
{
|
|
|
|
// Get first touch position
|
|
|
|
touchPosition[0].x = AMotionEvent_getX(event, 0);
|
|
|
|
touchPosition[0].y = AMotionEvent_getY(event, 0);
|
|
|
|
|
|
|
|
touchPosition[0].x /= (float)GetScreenWidth();
|
|
|
|
touchPosition[0].y /= (float)GetScreenHeight();
|
|
|
|
}
|
2017-09-29 14:56:37 +03:00
|
|
|
#endif
|
2016-01-03 15:01:21 +03:00
|
|
|
|
2017-09-29 14:56:37 +03:00
|
|
|
return 0;
|
2016-01-03 15:01:21 +03:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
#endif
|
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
#if defined(PLATFORM_WEB)
|
2016-12-25 22:42:22 +03:00
|
|
|
|
|
|
|
// Register fullscreen change events
|
2016-05-02 15:11:57 +03:00
|
|
|
static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *e, void *userData)
|
2013-11-19 02:38:44 +04:00
|
|
|
{
|
2016-08-16 12:09:55 +03:00
|
|
|
//isFullscreen: int e->isFullscreen
|
|
|
|
//fullscreenEnabled: int e->fullscreenEnabled
|
2016-05-02 15:11:57 +03:00
|
|
|
//fs element nodeName: (char *) e->nodeName
|
|
|
|
//fs element id: (char *) e->id
|
|
|
|
//Current element size: (int) e->elementWidth, (int) e->elementHeight
|
|
|
|
//Screen size:(int) e->screenWidth, (int) e->screenHeight
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
if (e->isFullscreen)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Canvas scaled to fullscreen. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight);
|
2016-05-02 15:11:57 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_INFO, "Canvas scaled to windowed. ElementSize: (%ix%i), ScreenSize(%ix%i)", e->elementWidth, e->elementHeight, e->screenWidth, e->screenHeight);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// TODO: Depending on scaling factor (screen vs element), calculate factor to scale mouse/touch input
|
2015-02-02 02:57:08 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-02 02:57:08 +03:00
|
|
|
|
2017-04-08 01:16:03 +03:00
|
|
|
// 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))
|
|
|
|
{
|
|
|
|
emscripten_exit_pointerlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register mouse input events
|
|
|
|
static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
|
|
|
|
{
|
2017-04-28 01:29:50 +03:00
|
|
|
// Lock mouse pointer when click on screen
|
2017-05-08 22:03:48 +03:00
|
|
|
if ((eventType == EMSCRIPTEN_EVENT_CLICK) && toggleCursorLock)
|
2017-04-08 01:16:03 +03:00
|
|
|
{
|
|
|
|
EmscriptenPointerlockChangeEvent plce;
|
|
|
|
emscripten_get_pointerlock_status(&plce);
|
|
|
|
|
|
|
|
if (!plce.isActive) emscripten_request_pointerlock(0, 1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emscripten_exit_pointerlock();
|
|
|
|
emscripten_get_pointerlock_status(&plce);
|
2017-07-02 13:35:13 +03:00
|
|
|
//if (plce.isActive) TraceLog(LOG_WARNING, "Pointer lock exit did not work!");
|
2017-04-08 01:16:03 +03:00
|
|
|
}
|
2017-05-08 22:03:48 +03:00
|
|
|
|
|
|
|
toggleCursorLock = false;
|
2017-04-08 01:16:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
// Register touch input events
|
2017-04-08 01:16:03 +03:00
|
|
|
static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
|
2016-05-02 15:11:57 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
for (int i = 0; i < touchEvent->numTouches; i++)
|
|
|
|
{
|
|
|
|
long x, y, id;
|
2015-04-06 15:02:29 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
if (!touchEvent->touches[i].isChanged) continue;
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
id = touchEvent->touches[i].identifier;
|
|
|
|
x = touchEvent->touches[i].canvasX;
|
|
|
|
y = touchEvent->touches[i].canvasY;
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
printf("%s, numTouches: %d %s%s%s%s\n", emscripten_event_type_to_string(eventType), event->numTouches,
|
|
|
|
event->ctrlKey ? " CTRL" : "", event->shiftKey ? " SHIFT" : "", event->altKey ? " ALT" : "", event->metaKey ? " META" : "");
|
2016-01-04 23:00:20 +03:00
|
|
|
|
2016-08-31 11:27:29 +03:00
|
|
|
for (int i = 0; i < event->numTouches; ++i)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-05-02 15:11:57 +03:00
|
|
|
const EmscriptenTouchPoint *t = &event->touches[i];
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
printf(" %ld: screen: (%ld,%ld), client: (%ld,%ld), page: (%ld,%ld), isChanged: %d, onTarget: %d, canvas: (%ld, %ld)\n",
|
|
|
|
t->identifier, t->screenX, t->screenY, t->clientX, t->clientY, t->pageX, t->pageY, t->isChanged, t->onTarget, t->canvasX, t->canvasY);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2016-05-02 15:11:57 +03:00
|
|
|
*/
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
GestureEvent gestureEvent;
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch actions
|
|
|
|
if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART) gestureEvent.touchAction = TOUCH_DOWN;
|
|
|
|
else if (eventType == EMSCRIPTEN_EVENT_TOUCHEND) gestureEvent.touchAction = TOUCH_UP;
|
|
|
|
else if (eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) gestureEvent.touchAction = TOUCH_MOVE;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch points count
|
|
|
|
gestureEvent.pointCount = touchEvent->numTouches;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch points id
|
|
|
|
gestureEvent.pointerId[0] = touchEvent->touches[0].identifier;
|
|
|
|
gestureEvent.pointerId[1] = touchEvent->touches[1].identifier;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Register touch points position
|
|
|
|
// NOTE: Only two points registered
|
|
|
|
// TODO: Touch data should be scaled accordingly!
|
|
|
|
//gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].canvasX, touchEvent->touches[0].canvasY };
|
|
|
|
//gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].canvasX, touchEvent->touches[1].canvasY };
|
|
|
|
gestureEvent.position[0] = (Vector2){ touchEvent->touches[0].targetX, touchEvent->touches[0].targetY };
|
|
|
|
gestureEvent.position[1] = (Vector2){ touchEvent->touches[1].targetX, touchEvent->touches[1].targetY };
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
touchPosition[0] = gestureEvent.position[0];
|
|
|
|
touchPosition[1] = gestureEvent.position[1];
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Normalize gestureEvent.position[x] for screenWidth and screenHeight
|
2016-08-16 12:09:55 +03:00
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
2016-05-02 15:11:57 +03:00
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
2016-08-16 12:09:55 +03:00
|
|
|
|
|
|
|
gestureEvent.position[1].x /= (float)GetScreenWidth();
|
2016-05-02 15:11:57 +03:00
|
|
|
gestureEvent.position[1].y /= (float)GetScreenHeight();
|
2016-02-18 16:05:48 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
// Gesture data is sent to gestures system for processing
|
2016-06-03 19:51:19 +03:00
|
|
|
ProcessGestureEvent(gestureEvent);
|
2016-02-18 16:05:48 +03:00
|
|
|
|
2016-05-02 15:11:57 +03:00
|
|
|
return 1;
|
2016-02-18 16:05:48 +03:00
|
|
|
}
|
2016-12-25 22:42:22 +03:00
|
|
|
|
|
|
|
// Register connected/disconnected gamepads events
|
|
|
|
static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
printf("%s: timeStamp: %g, connected: %d, index: %ld, numAxes: %d, numButtons: %d, id: \"%s\", mapping: \"%s\"\n",
|
2017-01-29 01:02:30 +03:00
|
|
|
eventType != 0 ? emscripten_event_type_to_string(eventType) : "Gamepad state",
|
2016-12-25 22:42:22 +03:00
|
|
|
gamepadEvent->timestamp, gamepadEvent->connected, gamepadEvent->index, gamepadEvent->numAxes, gamepadEvent->numButtons, gamepadEvent->id, gamepadEvent->mapping);
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
for(int i = 0; i < gamepadEvent->numAxes; ++i) printf("Axis %d: %g\n", i, gamepadEvent->axis[i]);
|
|
|
|
for(int i = 0; i < gamepadEvent->numButtons; ++i) printf("Button %d: Digital: %d, Analog: %g\n", i, gamepadEvent->digitalButton[i], gamepadEvent->analogButton[i]);
|
|
|
|
*/
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
if ((gamepadEvent->connected) && (gamepadEvent->index < MAX_GAMEPADS)) gamepadReady[gamepadEvent->index] = true;
|
|
|
|
else gamepadReady[gamepadEvent->index] = false;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-12-25 22:42:22 +03:00
|
|
|
// TODO: Test gamepadEvent->index
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-02 15:11:57 +03:00
|
|
|
#endif
|
2016-02-18 16:05:48 +03:00
|
|
|
|
|
|
|
#if defined(PLATFORM_RPI)
|
|
|
|
// Initialize Keyboard system (using standard input)
|
|
|
|
static void InitKeyboard(void)
|
|
|
|
{
|
|
|
|
// NOTE: We read directly from Standard Input (stdin) - STDIN_FILENO file descriptor
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Make stdin non-blocking (not enough, need to configure to non-canonical mode)
|
|
|
|
int flags = fcntl(STDIN_FILENO, F_GETFL, 0); // F_GETFL: Get the file access mode and the file status flags
|
|
|
|
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); // F_SETFL: Set the file status flags to the value specified
|
|
|
|
|
|
|
|
// Save terminal keyboard settings and reconfigure terminal with new settings
|
|
|
|
struct termios keyboardNewSettings;
|
|
|
|
tcgetattr(STDIN_FILENO, &defaultKeyboardSettings); // Get current keyboard settings
|
|
|
|
keyboardNewSettings = defaultKeyboardSettings;
|
|
|
|
|
|
|
|
// New terminal settings for keyboard: turn off buffering (non-canonical mode), echo and key processing
|
|
|
|
// NOTE: ISIG controls if ^C and ^Z generate break signals or not
|
|
|
|
keyboardNewSettings.c_lflag &= ~(ICANON | ECHO | ISIG);
|
|
|
|
//keyboardNewSettings.c_iflag &= ~(ISTRIP | INLCR | ICRNL | IGNCR | IXON | IXOFF);
|
|
|
|
keyboardNewSettings.c_cc[VMIN] = 1;
|
|
|
|
keyboardNewSettings.c_cc[VTIME] = 0;
|
|
|
|
|
|
|
|
// Set new keyboard settings (change occurs immediately)
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &keyboardNewSettings);
|
|
|
|
|
|
|
|
// NOTE: Reading directly from stdin will give chars already key-mapped by kernel to ASCII or UNICODE, we change that! -> WHY???
|
|
|
|
|
|
|
|
// Save old keyboard mode to restore it at the end
|
|
|
|
if (ioctl(STDIN_FILENO, KDGKBMODE, &defaultKeyboardMode) < 0)
|
|
|
|
{
|
|
|
|
// NOTE: It could mean we are using a remote keyboard through ssh!
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Could not change keyboard mode (SSH keyboard?)");
|
2016-02-18 16:05:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We reconfigure keyboard mode to get:
|
2016-08-16 12:09:55 +03:00
|
|
|
// - scancodes (K_RAW)
|
2016-02-18 16:05:48 +03:00
|
|
|
// - keycodes (K_MEDIUMRAW)
|
|
|
|
// - ASCII chars (K_XLATE)
|
|
|
|
// - UNICODE chars (K_UNICODE)
|
|
|
|
ioctl(STDIN_FILENO, KDSKBMODE, K_XLATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register keyboard restore when program finishes
|
|
|
|
atexit(RestoreKeyboard);
|
|
|
|
}
|
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
// Process keyboard inputs
|
|
|
|
// TODO: Most probably input reading and processing should be in a separate thread
|
2016-02-18 16:05:48 +03:00
|
|
|
static void ProcessKeyboard(void)
|
|
|
|
{
|
2016-02-19 21:57:25 +03:00
|
|
|
#define MAX_KEYBUFFER_SIZE 32 // Max size in bytes to read
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Keyboard input polling (fill keys[256] array with status)
|
2016-02-19 21:57:25 +03:00
|
|
|
int bufferByteCount = 0; // Bytes available on the buffer
|
|
|
|
char keysBuffer[MAX_KEYBUFFER_SIZE]; // Max keys to be read at a time
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
// Reset pressed keys array
|
|
|
|
for (int i = 0; i < 512; i++) currentKeyState[i] = 0;
|
|
|
|
|
|
|
|
// Read availables keycodes from stdin
|
2016-02-19 21:57:25 +03:00
|
|
|
bufferByteCount = read(STDIN_FILENO, keysBuffer, MAX_KEYBUFFER_SIZE); // POSIX system call
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
// Fill all read bytes (looking for keys)
|
|
|
|
for (int i = 0; i < bufferByteCount; i++)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Bytes on keysBuffer: %i", bufferByteCount);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
//printf("Key(s) bytes: ");
|
|
|
|
//for (int i = 0; i < bufferByteCount; i++) printf("0x%02x ", keysBuffer[i]);
|
|
|
|
//printf("\n");
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
// NOTE: If (key == 0x1b), depending on next key, it could be a special keymap code!
|
|
|
|
// Up -> 1b 5b 41 / Left -> 1b 5b 44 / Right -> 1b 5b 43 / Down -> 1b 5b 42
|
|
|
|
if (keysBuffer[i] == 0x1b)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-02-19 21:57:25 +03:00
|
|
|
// Detect ESC to stop program
|
|
|
|
if (bufferByteCount == 1) currentKeyState[256] = 1; // raylib key: KEY_ESCAPE
|
|
|
|
else
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-02-19 21:57:25 +03:00
|
|
|
if (keysBuffer[i + 1] == 0x5b) // Special function key
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-02-19 21:57:25 +03:00
|
|
|
if ((keysBuffer[i + 2] == 0x5b) || (keysBuffer[i + 2] == 0x31) || (keysBuffer[i + 2] == 0x32))
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-02-19 21:57:25 +03:00
|
|
|
// Process special function keys (F1 - F12)
|
|
|
|
switch (keysBuffer[i + 3])
|
|
|
|
{
|
|
|
|
case 0x41: currentKeyState[290] = 1; break; // raylib KEY_F1
|
|
|
|
case 0x42: currentKeyState[291] = 1; break; // raylib KEY_F2
|
|
|
|
case 0x43: currentKeyState[292] = 1; break; // raylib KEY_F3
|
|
|
|
case 0x44: currentKeyState[293] = 1; break; // raylib KEY_F4
|
|
|
|
case 0x45: currentKeyState[294] = 1; break; // raylib KEY_F5
|
|
|
|
case 0x37: currentKeyState[295] = 1; break; // raylib KEY_F6
|
|
|
|
case 0x38: currentKeyState[296] = 1; break; // raylib KEY_F7
|
|
|
|
case 0x39: currentKeyState[297] = 1; break; // raylib KEY_F8
|
|
|
|
case 0x30: currentKeyState[298] = 1; break; // raylib KEY_F9
|
|
|
|
case 0x31: currentKeyState[299] = 1; break; // raylib KEY_F10
|
|
|
|
case 0x33: currentKeyState[300] = 1; break; // raylib KEY_F11
|
|
|
|
case 0x34: currentKeyState[301] = 1; break; // raylib KEY_F12
|
|
|
|
default: break;
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
if (keysBuffer[i + 2] == 0x5b) i += 4;
|
|
|
|
else if ((keysBuffer[i + 2] == 0x31) || (keysBuffer[i + 2] == 0x32)) i += 5;
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2016-02-19 21:57:25 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (keysBuffer[i + 2])
|
|
|
|
{
|
|
|
|
case 0x41: currentKeyState[265] = 1; break; // raylib KEY_UP
|
|
|
|
case 0x42: currentKeyState[264] = 1; break; // raylib KEY_DOWN
|
|
|
|
case 0x43: currentKeyState[262] = 1; break; // raylib KEY_RIGHT
|
|
|
|
case 0x44: currentKeyState[263] = 1; break; // raylib KEY_LEFT
|
|
|
|
default: break;
|
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
i += 3; // Jump to next key
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
// NOTE: Some keys are not directly keymapped (CTRL, ALT, SHIFT)
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 21:57:25 +03:00
|
|
|
else if (keysBuffer[i] == 0x0a) currentKeyState[257] = 1; // raylib KEY_ENTER (don't mix with <linux/input.h> KEY_*)
|
|
|
|
else if (keysBuffer[i] == 0x7f) currentKeyState[259] = 1; // raylib KEY_BACKSPACE
|
|
|
|
else
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Pressed key (ASCII): 0x%02x", keysBuffer[i]);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
// Translate lowercase a-z letters to A-Z
|
|
|
|
if ((keysBuffer[i] >= 97) && (keysBuffer[i] <= 122))
|
|
|
|
{
|
|
|
|
currentKeyState[(int)keysBuffer[i] - 32] = 1;
|
|
|
|
}
|
|
|
|
else currentKeyState[(int)keysBuffer[i]] = 1;
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
// Check exit key (same functionality as GLFW3 KeyCallback())
|
|
|
|
if (currentKeyState[exitKey] == 1) windowShouldClose = true;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2017-05-11 17:24:40 +03:00
|
|
|
// Check screen capture key (raylib key: KEY_F12)
|
|
|
|
if (currentKeyState[301] == 1)
|
|
|
|
{
|
|
|
|
TakeScreenshot(FormatText("screenshot%03i.png", screenshotCounter));
|
|
|
|
screenshotCounter++;
|
|
|
|
}
|
2016-02-18 16:05:48 +03:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Restore default keyboard input
|
|
|
|
static void RestoreKeyboard(void)
|
|
|
|
{
|
|
|
|
// Reset to default keyboard settings
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &defaultKeyboardSettings);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Reconfigure keyboard to default mode
|
|
|
|
ioctl(STDIN_FILENO, KDSKBMODE, defaultKeyboardMode);
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2014-07-23 02:06:24 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
// Mouse initialization (including mouse thread)
|
|
|
|
static void InitMouse(void)
|
2014-07-23 02:06:24 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
// NOTE: We can use /dev/input/mice to read from all available mice
|
2014-12-31 20:03:32 +03:00
|
|
|
if ((mouseStream = open(DEFAULT_MOUSE_DEV, O_RDONLY|O_NONBLOCK)) < 0)
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Mouse device could not be opened, no mouse available");
|
2014-12-31 20:03:32 +03:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mouseReady = true;
|
|
|
|
|
2014-12-31 20:03:32 +03:00
|
|
|
int error = pthread_create(&mouseThreadId, NULL, &MouseThread, NULL);
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (error != 0) TraceLog(LOG_WARNING, "Error creating mouse input event thread");
|
|
|
|
else TraceLog(LOG_INFO, "Mouse device initialized successfully");
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mouse reading thread
|
|
|
|
// NOTE: We need a separate thread to avoid loosing mouse events,
|
2015-09-02 21:34:14 +03:00
|
|
|
// if too much time passes between reads, queue gets full and new events override older ones...
|
2014-09-17 00:51:31 +04:00
|
|
|
static void *MouseThread(void *arg)
|
|
|
|
{
|
2016-12-27 19:37:35 +03:00
|
|
|
const unsigned char XSIGN = (1 << 4);
|
|
|
|
const unsigned char YSIGN = (1 << 5);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
|
|
|
typedef struct {
|
2016-02-18 16:05:48 +03:00
|
|
|
char buttons;
|
2016-08-16 12:09:55 +03:00
|
|
|
char dx, dy;
|
2016-02-18 16:05:48 +03:00
|
|
|
} MouseEvent;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
MouseEvent mouse;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
int mouseRelX = 0;
|
|
|
|
int mouseRelY = 0;
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-07-04 19:35:50 +03:00
|
|
|
while (!windowShouldClose)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-02-18 16:05:48 +03:00
|
|
|
if (read(mouseStream, &mouse, sizeof(MouseEvent)) == (int)sizeof(MouseEvent))
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-02-18 16:05:48 +03:00
|
|
|
if ((mouse.buttons & 0x08) == 0) break; // This bit should always be set
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Check Left button pressed
|
|
|
|
if ((mouse.buttons & 0x01) > 0) currentMouseState[0] = 1;
|
|
|
|
else currentMouseState[0] = 0;
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Check Right button pressed
|
|
|
|
if ((mouse.buttons & 0x02) > 0) currentMouseState[1] = 1;
|
|
|
|
else currentMouseState[1] = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Check Middle button pressed
|
|
|
|
if ((mouse.buttons & 0x04) > 0) currentMouseState[2] = 1;
|
|
|
|
else currentMouseState[2] = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
mouseRelX = (int)mouse.dx;
|
|
|
|
mouseRelY = (int)mouse.dy;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
if ((mouse.buttons & XSIGN) > 0) mouseRelX = -1*(255 - mouseRelX);
|
|
|
|
if ((mouse.buttons & YSIGN) > 0) mouseRelY = -1*(255 - mouseRelY);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
// NOTE: Mouse movement is normalized to not be screen resolution dependant
|
|
|
|
// We suppose 2*255 (max relative movement) is equivalent to screenWidth (max pixels width)
|
|
|
|
// Result after normalization is multiplied by MOUSE_SENSITIVITY factor
|
|
|
|
|
|
|
|
mousePosition.x += (float)mouseRelX*((float)screenWidth/(2*255))*MOUSE_SENSITIVITY;
|
|
|
|
mousePosition.y -= (float)mouseRelY*((float)screenHeight/(2*255))*MOUSE_SENSITIVITY;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
if (mousePosition.x < 0) mousePosition.x = 0;
|
|
|
|
if (mousePosition.y < 0) mousePosition.y = 0;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
if (mousePosition.x > screenWidth) mousePosition.x = screenWidth;
|
|
|
|
if (mousePosition.y > screenHeight) mousePosition.y = screenHeight;
|
|
|
|
}
|
|
|
|
//else read(mouseStream, &mouse, 1); // Try to sync up again
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-06 00:36:40 +03:00
|
|
|
// Touch initialization (including touch thread)
|
|
|
|
static void InitTouch(void)
|
|
|
|
{
|
|
|
|
if ((touchStream = open(DEFAULT_TOUCH_DEV, O_RDONLY|O_NONBLOCK)) < 0)
|
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_WARNING, "Touch device could not be opened, no touchscreen available");
|
2017-01-06 00:36:40 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
touchReady = true;
|
|
|
|
|
|
|
|
int error = pthread_create(&touchThreadId, NULL, &TouchThread, NULL);
|
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (error != 0) TraceLog(LOG_WARNING, "Error creating touch input event thread");
|
|
|
|
else TraceLog(LOG_INFO, "Touch device initialized successfully");
|
2017-01-06 00:36:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Touch reading thread.
|
2017-01-29 01:02:30 +03:00
|
|
|
// This reads from a Virtual Input Event /dev/input/event4 which is
|
2017-01-06 00:36:40 +03:00
|
|
|
// created by the ts_uinput daemon. This takes, filters and scales
|
|
|
|
// raw input from the Touchscreen (which appears in /dev/input/event3)
|
|
|
|
// based on the Calibration data referenced by tslib.
|
|
|
|
static void *TouchThread(void *arg)
|
|
|
|
{
|
|
|
|
struct input_event ev;
|
2017-01-14 19:18:06 +03:00
|
|
|
GestureEvent gestureEvent;
|
2017-01-06 00:36:40 +03:00
|
|
|
|
|
|
|
while (!windowShouldClose)
|
|
|
|
{
|
|
|
|
if (read(touchStream, &ev, sizeof(ev)) == (int)sizeof(ev))
|
|
|
|
{
|
|
|
|
// if pressure > 0 then simulate left mouse button click
|
2017-01-29 01:02:30 +03:00
|
|
|
if (ev.type == EV_ABS && ev.code == 24 && ev.value == 0 && currentMouseState[0] == 1)
|
2017-01-14 19:18:06 +03:00
|
|
|
{
|
|
|
|
currentMouseState[0] = 0;
|
|
|
|
gestureEvent.touchAction = TOUCH_UP;
|
|
|
|
gestureEvent.pointCount = 1;
|
|
|
|
gestureEvent.pointerId[0] = 0;
|
|
|
|
gestureEvent.pointerId[1] = 1;
|
|
|
|
gestureEvent.position[0] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[1] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
|
|
|
gestureEvent.position[1].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[1].y /= (float)GetScreenHeight();
|
|
|
|
ProcessGestureEvent(gestureEvent);
|
2017-01-29 01:02:30 +03:00
|
|
|
}
|
|
|
|
if (ev.type == EV_ABS && ev.code == 24 && ev.value > 0 && currentMouseState[0] == 0)
|
2017-01-14 19:18:06 +03:00
|
|
|
{
|
2017-01-29 01:02:30 +03:00
|
|
|
currentMouseState[0] = 1;
|
2017-01-14 19:18:06 +03:00
|
|
|
gestureEvent.touchAction = TOUCH_DOWN;
|
|
|
|
gestureEvent.pointCount = 1;
|
|
|
|
gestureEvent.pointerId[0] = 0;
|
|
|
|
gestureEvent.pointerId[1] = 1;
|
|
|
|
gestureEvent.position[0] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[1] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
|
|
|
gestureEvent.position[1].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[1].y /= (float)GetScreenHeight();
|
|
|
|
ProcessGestureEvent(gestureEvent);
|
2017-01-29 01:02:30 +03:00
|
|
|
}
|
2017-01-06 00:36:40 +03:00
|
|
|
// x & y values supplied by event4 have been scaled & de-jittered using tslib calibration data
|
2017-01-29 01:02:30 +03:00
|
|
|
if (ev.type == EV_ABS && ev.code == 0)
|
2017-01-14 19:18:06 +03:00
|
|
|
{
|
|
|
|
mousePosition.x = ev.value;
|
|
|
|
if (mousePosition.x < 0) mousePosition.x = 0;
|
|
|
|
if (mousePosition.x > screenWidth) mousePosition.x = screenWidth;
|
|
|
|
gestureEvent.touchAction = TOUCH_MOVE;
|
|
|
|
gestureEvent.pointCount = 1;
|
|
|
|
gestureEvent.pointerId[0] = 0;
|
|
|
|
gestureEvent.pointerId[1] = 1;
|
|
|
|
gestureEvent.position[0] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[1] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
|
|
|
gestureEvent.position[1].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[1].y /= (float)GetScreenHeight();
|
|
|
|
ProcessGestureEvent(gestureEvent);
|
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
if (ev.type == EV_ABS && ev.code == 1)
|
2017-01-14 19:18:06 +03:00
|
|
|
{
|
|
|
|
mousePosition.y = ev.value;
|
|
|
|
if (mousePosition.y < 0) mousePosition.y = 0;
|
|
|
|
if (mousePosition.y > screenHeight) mousePosition.y = screenHeight;
|
|
|
|
gestureEvent.touchAction = TOUCH_MOVE;
|
|
|
|
gestureEvent.pointCount = 1;
|
|
|
|
gestureEvent.pointerId[0] = 0;
|
|
|
|
gestureEvent.pointerId[1] = 1;
|
|
|
|
gestureEvent.position[0] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[1] = (Vector2){ mousePosition.x, mousePosition.y };
|
|
|
|
gestureEvent.position[0].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[0].y /= (float)GetScreenHeight();
|
|
|
|
gestureEvent.position[1].x /= (float)GetScreenWidth();
|
|
|
|
gestureEvent.position[1].y /= (float)GetScreenHeight();
|
|
|
|
ProcessGestureEvent(gestureEvent);
|
|
|
|
}
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2017-01-06 00:36:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Init gamepad system
|
|
|
|
static void InitGamepad(void)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-03-17 14:54:36 +03:00
|
|
|
char gamepadDev[128] = "";
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-03-17 14:54:36 +03:00
|
|
|
for (int i = 0; i < MAX_GAMEPADS; i++)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-03-17 14:54:36 +03:00
|
|
|
sprintf(gamepadDev, "%s%i", DEFAULT_GAMEPAD_DEV, i);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
|
|
|
if ((gamepadStream[i] = open(gamepadDev, O_RDONLY|O_NONBLOCK)) < 0)
|
2016-03-17 14:54:36 +03:00
|
|
|
{
|
|
|
|
// NOTE: Only show message for first gamepad
|
2017-07-02 13:35:13 +03:00
|
|
|
if (i == 0) TraceLog(LOG_WARNING, "Gamepad device could not be opened, no gamepad available");
|
2016-03-17 14:54:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gamepadReady[i] = true;
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2016-03-17 14:54:36 +03:00
|
|
|
// NOTE: Only create one thread
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
int error = pthread_create(&gamepadThreadId, NULL, &GamepadThread, NULL);
|
2014-09-17 00:51:31 +04:00
|
|
|
|
2017-07-02 13:35:13 +03:00
|
|
|
if (error != 0) TraceLog(LOG_WARNING, "Error creating gamepad input event thread");
|
|
|
|
else TraceLog(LOG_INFO, "Gamepad device initialized successfully");
|
2016-03-17 14:54:36 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
}
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
|
|
|
|
2016-02-18 16:05:48 +03:00
|
|
|
// Process Gamepad (/dev/input/js0)
|
|
|
|
static void *GamepadThread(void *arg)
|
2014-09-17 00:51:31 +04:00
|
|
|
{
|
2016-02-18 16:05:48 +03:00
|
|
|
#define JS_EVENT_BUTTON 0x01 // Button pressed/released
|
|
|
|
#define JS_EVENT_AXIS 0x02 // Joystick axis moved
|
|
|
|
#define JS_EVENT_INIT 0x80 // Initial state of device
|
|
|
|
|
|
|
|
struct js_event {
|
|
|
|
unsigned int time; // event timestamp in milliseconds
|
|
|
|
short value; // event value
|
|
|
|
unsigned char type; // event type
|
|
|
|
unsigned char number; // event axis/button number
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read gamepad event
|
2016-05-31 20:12:37 +03:00
|
|
|
struct js_event gamepadEvent;
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-06-24 20:34:47 +03:00
|
|
|
while (!windowShouldClose)
|
2016-02-18 16:05:48 +03:00
|
|
|
{
|
2016-03-17 14:54:36 +03:00
|
|
|
for (int i = 0; i < MAX_GAMEPADS; i++)
|
2016-02-18 16:05:48 +03:00
|
|
|
{
|
2016-03-17 14:54:36 +03:00
|
|
|
if (read(gamepadStream[i], &gamepadEvent, sizeof(struct js_event)) == (int)sizeof(struct js_event))
|
2016-02-18 16:05:48 +03:00
|
|
|
{
|
2016-03-17 14:54:36 +03:00
|
|
|
gamepadEvent.type &= ~JS_EVENT_INIT; // Ignore synthetic events
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-03-17 14:54:36 +03:00
|
|
|
// Process gamepad events by type
|
2016-08-16 12:09:55 +03:00
|
|
|
if (gamepadEvent.type == JS_EVENT_BUTTON)
|
2016-02-18 16:05:48 +03:00
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Gamepad button: %i, value: %i", gamepadEvent.number, gamepadEvent.value);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
|
|
|
if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS)
|
2016-03-17 14:54:36 +03:00
|
|
|
{
|
|
|
|
// 1 - button pressed, 0 - button released
|
2016-10-14 12:14:41 +03:00
|
|
|
currentGamepadState[i][gamepadEvent.number] = (int)gamepadEvent.value;
|
2017-01-29 01:02:30 +03:00
|
|
|
|
2016-10-27 14:41:43 +03:00
|
|
|
if ((int)gamepadEvent.value == 1) lastGamepadButtonPressed = gamepadEvent.number;
|
2016-11-02 15:39:48 +03:00
|
|
|
else lastGamepadButtonPressed = -1;
|
2016-03-17 14:54:36 +03:00
|
|
|
}
|
2016-02-18 16:05:48 +03:00
|
|
|
}
|
2016-08-16 12:09:55 +03:00
|
|
|
else if (gamepadEvent.type == JS_EVENT_AXIS)
|
2016-02-18 16:05:48 +03:00
|
|
|
{
|
2017-07-02 13:35:13 +03:00
|
|
|
TraceLog(LOG_DEBUG, "Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value);
|
2016-08-16 12:09:55 +03:00
|
|
|
|
2016-03-17 14:54:36 +03:00
|
|
|
if (gamepadEvent.number < MAX_GAMEPAD_AXIS)
|
|
|
|
{
|
|
|
|
// NOTE: Scaling of gamepadEvent.value to get values between -1..1
|
2016-10-14 12:14:41 +03:00
|
|
|
gamepadAxisState[i][gamepadEvent.number] = (float)gamepadEvent.value/32768;
|
2016-03-17 14:54:36 +03:00
|
|
|
}
|
2016-02-18 16:05:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-31 20:12:37 +03:00
|
|
|
}
|
|
|
|
|
2016-02-19 21:57:25 +03:00
|
|
|
return NULL;
|
2014-09-17 00:51:31 +04:00
|
|
|
}
|
2016-06-24 20:34:47 +03:00
|
|
|
#endif // PLATFORM_RPI
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
// Plays raylib logo appearing animation
|
|
|
|
static void LogoAnimation(void)
|
|
|
|
{
|
2017-05-08 03:47:44 +03:00
|
|
|
#if !defined(PLATFORM_WEB)
|
2014-09-17 00:51:31 +04:00
|
|
|
int logoPositionX = screenWidth/2 - 128;
|
|
|
|
int logoPositionY = screenHeight/2 - 128;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
int framesCounter = 0;
|
|
|
|
int lettersCount = 0;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
int topSideRecWidth = 16;
|
|
|
|
int leftSideRecHeight = 16;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
int bottomSideRecWidth = 16;
|
|
|
|
int rightSideRecHeight = 16;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
int state = 0; // Tracking animation states (State Machine)
|
2014-09-17 00:51:31 +04:00
|
|
|
float alpha = 1.0f; // Useful for fading
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
while (!WindowShouldClose() && (state != 4)) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
if (state == 0) // State 0: Small box blinking
|
|
|
|
{
|
|
|
|
framesCounter++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
if (framesCounter == 84)
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2014-07-23 02:06:24 +04:00
|
|
|
state = 1;
|
|
|
|
framesCounter = 0; // Reset counter... will be used later...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == 1) // State 1: Top and left bars growing
|
|
|
|
{
|
|
|
|
topSideRecWidth += 4;
|
|
|
|
leftSideRecHeight += 4;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
if (topSideRecWidth == 256) state = 2;
|
|
|
|
}
|
|
|
|
else if (state == 2) // State 2: Bottom and right bars growing
|
|
|
|
{
|
|
|
|
bottomSideRecWidth += 4;
|
|
|
|
rightSideRecHeight += 4;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
if (bottomSideRecWidth == 256) state = 3;
|
|
|
|
}
|
|
|
|
else if (state == 3) // State 3: Letters appearing (one by one)
|
|
|
|
{
|
|
|
|
framesCounter++;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
if (framesCounter/12) // Every 12 frames, one more letter!
|
2014-09-03 18:51:28 +04:00
|
|
|
{
|
2014-07-23 02:06:24 +04:00
|
|
|
lettersCount++;
|
|
|
|
framesCounter = 0;
|
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
if (lettersCount >= 10) // When all letters have appeared, just fade out everything
|
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
alpha -= 0.02f;
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
if (alpha <= 0.0f)
|
2014-07-23 02:06:24 +04:00
|
|
|
{
|
2014-09-17 00:51:31 +04:00
|
|
|
alpha = 0.0f;
|
2014-07-23 02:06:24 +04:00
|
|
|
state = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//----------------------------------------------------------------------------------
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
if (state == 0)
|
|
|
|
{
|
|
|
|
if ((framesCounter/12)%2) DrawRectangle(logoPositionX, logoPositionY, 16, 16, BLACK);
|
|
|
|
}
|
|
|
|
else if (state == 1)
|
|
|
|
{
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
|
|
|
|
}
|
|
|
|
else if (state == 2)
|
|
|
|
{
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, BLACK);
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY, 16, leftSideRecHeight, BLACK);
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
DrawRectangle(logoPositionX + 240, logoPositionY, 16, rightSideRecHeight, BLACK);
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, BLACK);
|
|
|
|
}
|
|
|
|
else if (state == 3)
|
|
|
|
{
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY, topSideRecWidth, 16, Fade(BLACK, alpha));
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY + 16, 16, leftSideRecHeight - 32, Fade(BLACK, alpha));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
DrawRectangle(logoPositionX + 240, logoPositionY + 16, 16, rightSideRecHeight - 32, Fade(BLACK, alpha));
|
|
|
|
DrawRectangle(logoPositionX, logoPositionY + 240, bottomSideRecWidth, 16, Fade(BLACK, alpha));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-09-17 00:51:31 +04:00
|
|
|
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2016-02-13 21:14:22 +03:00
|
|
|
DrawText(SubText("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
|
2014-07-23 02:06:24 +04:00
|
|
|
}
|
2014-09-03 18:51:28 +04:00
|
|
|
|
2014-07-23 02:06:24 +04:00
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
2014-12-31 20:03:32 +03:00
|
|
|
#endif
|
2014-09-17 00:51:31 +04:00
|
|
|
|
|
|
|
showLogo = false; // Prevent for repeating when reloading window (Android)
|
|
|
|
}
|