mirror of
https://github.com/captain-amygdala/pistorm.git
synced 2026-04-17 00:35:28 +00:00
Fix compilation on Raspberry Pi OS Bullseye
Requires commenting/uncommenting the applicable lines in the Makefile. Not quite sure how to handle this yet.
This commit is contained in:
6
Makefile
6
Makefile
@@ -52,9 +52,11 @@ CFLAGS = $(WARNINGS) -I. -I./raylib -I/opt/vc/include/ -march=armv8-a -mfloat
|
||||
# Old SDL2 stuff
|
||||
#LFLAGS = $(WARNINGS) `sdl2-config --libs`
|
||||
|
||||
# Pi3 standard raylib stuff
|
||||
# Pi3 standard raylib linker flags
|
||||
LFLAGS = $(WARNINGS) -L/opt/vc/lib -L./raylib -lraylib -lbrcmGLESv2 -lbrcmEGL -lbcm_host -lstdc++ -lvcos -lvchiq_arm -lasound
|
||||
# Pi4 experimental crap
|
||||
# Pi 3 Debian Bullseye DRM linker flags
|
||||
#LFLAGS = $(WARNINGS) -L/usr/local/lib -L/opt/vc/lib -L./raylib_drm -lraylib -lGLESv2 -lEGL -lgbm -ldrm -ldl -lstdc++ -lvcos -lvchiq_arm -lvchostif -lasound
|
||||
# Pi4 experimental (non-working) linker flags
|
||||
#LFLAGS = $(WARNINGS) -L/usr/local/lib -L/opt/vc/lib -L./raylib_pi4_test -lraylib -lGLESv2 -lEGL -lgbm -ldrm -ldl -lstdc++ -lvcos -lvchiq_arm -lvchostif -lasound
|
||||
|
||||
TARGET = $(EXENAME)$(EXE)
|
||||
|
||||
@@ -49,6 +49,9 @@ platform amiga
|
||||
#setvar rtg
|
||||
# Uncomment to enable DPMS (monitor turns off) when RTG sleeps
|
||||
#setvar rtg-dpms
|
||||
# Uncomment the following lines to set the RTG native screen resolution width and height. Default is 1280x720.
|
||||
#setvar rtg-width 1280
|
||||
#setvar rtg-height 720
|
||||
|
||||
# Uncommenting the lines below here sets the specified sample rate for the Pistorm AHI device and enables the PiStorm AHI
|
||||
# device respectively. The PiStorm AHI device does not yet work properly, please read the information in the readme file
|
||||
|
||||
@@ -486,6 +486,20 @@ void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
|
||||
rtg_dpms = 1;
|
||||
printf("[AMIGA] DPMS enabled for RTG.\n");
|
||||
}
|
||||
if (CHKVAR("rtg-width")) {
|
||||
if (val && strlen(val) != 0) {
|
||||
uint32_t rtg_width = get_int(val);
|
||||
rtg_set_screen_width(rtg_width);
|
||||
printf("[AMIGA] RTG screen width set to %d.\n", rtg_width);
|
||||
}
|
||||
}
|
||||
if (CHKVAR("rtg-height")) {
|
||||
if (val && strlen(val) != 0) {
|
||||
uint32_t rtg_height = get_int(val);
|
||||
rtg_set_screen_height(rtg_height);
|
||||
printf("[AMIGA] RTG screen height set to %d.\n", rtg_height);
|
||||
}
|
||||
}
|
||||
if CHKVAR("kick13") {
|
||||
printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
|
||||
kick13_mode = 1;
|
||||
|
||||
@@ -227,14 +227,12 @@ void *rtgThread(void *args) {
|
||||
Texture raylib_clut_texture;
|
||||
Image raylib_fb, raylib_cursor, raylib_clut;
|
||||
|
||||
InitWindow(0, 0, "Pistorm RTG");
|
||||
InitWindow(pi_screen_width, pi_screen_height, "Pistorm RTG");
|
||||
HideCursor();
|
||||
SetTargetFPS(60);
|
||||
|
||||
pi_screen_width = GetScreenWidth();
|
||||
pi_screen_height = GetScreenHeight();
|
||||
|
||||
Color bef = { 0, 64, 128, 255 };
|
||||
Color black = { 0, 0, 0, 255 };
|
||||
|
||||
Shader clut_shader = LoadShader(NULL, "platforms/amiga/rtg/clut.shader");
|
||||
Shader bgra_swizzle_shader = LoadShader(NULL, "platforms/amiga/rtg/bgraswizzle.shader");
|
||||
@@ -325,6 +323,7 @@ reinit_raylib:;
|
||||
}
|
||||
}
|
||||
BeginDrawing();
|
||||
ClearBackground(black);
|
||||
rtg_output_in_vblank = 0;
|
||||
updating_screen = 1;
|
||||
|
||||
@@ -435,6 +434,14 @@ shutdown_raylib:;
|
||||
return args;
|
||||
}
|
||||
|
||||
void rtg_set_screen_width(uint32_t width) {
|
||||
pi_screen_width = width;
|
||||
}
|
||||
|
||||
void rtg_set_screen_height(uint32_t height) {
|
||||
pi_screen_height = height;
|
||||
}
|
||||
|
||||
void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
|
||||
//palette[index] = xrgb;
|
||||
unsigned char *src = (unsigned char *)&xrgb;
|
||||
|
||||
@@ -28,6 +28,8 @@ void rtg_set_scale_mode(uint16_t scale_mode);
|
||||
uint16_t rtg_get_scale_mode();
|
||||
void rtg_set_scale_rect(uint16_t scale_mode, int16_t x1, int16_t y1, int16_t x2, int16_t y2);
|
||||
void rtg_set_scale_filter(uint16_t _filter_mode);
|
||||
void rtg_set_screen_width(uint32_t width);
|
||||
void rtg_set_screen_height(uint32_t height);
|
||||
void rtg_show_clut_cursor(uint8_t show);
|
||||
void rtg_set_clut_cursor(uint8_t *bmp, uint32_t *pal, int16_t offs_x, int16_t offs_y, uint16_t w, uint16_t h, uint8_t mask_color);
|
||||
uint16_t rtg_get_scale_filter();
|
||||
|
||||
230
raylib_drm/config.h
Normal file
230
raylib_drm/config.h
Normal file
@@ -0,0 +1,230 @@
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* raylib configuration flags
|
||||
*
|
||||
* This file defines all the configuration flags for the different raylib modules
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2018-2021 Ahmad Fatoum & Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 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
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: core - Configuration Flags
|
||||
//------------------------------------------------------------------------------------
|
||||
// Camera module is included (rcamera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital
|
||||
#define SUPPORT_CAMERA_SYSTEM 1
|
||||
// Gestures module is included (rgestures.h) to support gestures detection: tap, hold, swipe, drag
|
||||
#define SUPPORT_GESTURES_SYSTEM 1
|
||||
// Mouse gestures are directly mapped like touches and processed by gestures system
|
||||
#define SUPPORT_MOUSE_GESTURES 1
|
||||
// Reconfigure standard input to receive key inputs, works with SSH connection.
|
||||
#define SUPPORT_SSH_KEYBOARD_RPI 1
|
||||
// Draw a mouse pointer on screen
|
||||
//#define SUPPORT_MOUSE_CURSOR_POINT 1
|
||||
// Setting a higher resolution can improve the accuracy of time-out intervals in wait functions.
|
||||
// However, it can also reduce overall system performance, because the thread scheduler switches tasks more often.
|
||||
#define SUPPORT_WINMM_HIGHRES_TIMER 1
|
||||
// Use busy wait loop for timing sync, if not defined, a high-resolution timer is setup and used
|
||||
//#define SUPPORT_BUSY_WAIT_LOOP 1
|
||||
// Use a partial-busy wait loop, in this case frame sleeps for most of the time, but then runs a busy loop at the end for accuracy
|
||||
#define SUPPORT_PARTIALBUSY_WAIT_LOOP
|
||||
// Wait for events passively (sleeping while no events) instead of polling them actively every frame
|
||||
//#define SUPPORT_EVENTS_WAITING 1
|
||||
// Allow automatic screen capture of current screen pressing F12, defined in KeyCallback()
|
||||
#define SUPPORT_SCREEN_CAPTURE 1
|
||||
// Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback()
|
||||
#define SUPPORT_GIF_RECORDING 1
|
||||
// Support CompressData() and DecompressData() functions
|
||||
#define SUPPORT_COMPRESSION_API 1
|
||||
// Support saving binary data automatically to a generated storage.data file. This file is managed internally.
|
||||
#define SUPPORT_DATA_STORAGE 1
|
||||
// Support automatic generated events, loading and recording of those events when required
|
||||
//#define SUPPORT_EVENTS_AUTOMATION 1
|
||||
// Support custom frame control, only for advance users
|
||||
// By default EndDrawing() does this job: draws everything + SwapScreenBuffer() + manage frame timming + PollInputEvents()
|
||||
// Enabling this flag allows manual control of the frame processes, use at your own risk
|
||||
//#define SUPPORT_CUSTOM_FRAME_CONTROL 1
|
||||
|
||||
// core: Configuration values
|
||||
//------------------------------------------------------------------------------------
|
||||
#if defined(__linux__)
|
||||
#define MAX_FILEPATH_LENGTH 4096 // Maximum length for filepaths (Linux PATH_MAX default value)
|
||||
#else
|
||||
#define MAX_FILEPATH_LENGTH 512 // Maximum length supported for filepaths
|
||||
#endif
|
||||
|
||||
#define MAX_GAMEPADS 4 // Max number of gamepads supported
|
||||
#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad)
|
||||
#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad)
|
||||
#define MAX_TOUCH_POINTS 8 // Maximum number of touch points supported
|
||||
#define MAX_KEY_PRESSED_QUEUE 16 // Max number of characters in the key input queue
|
||||
|
||||
#define STORAGE_DATA_FILE "storage.data" // Automatic storage filename
|
||||
|
||||
#define MAX_DECOMPRESSION_SIZE 64 // Max size allocated for decompression in MB
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: rlgl - Configuration values
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
// Enable OpenGL Debug Context (only available on OpenGL 4.3)
|
||||
//#define RLGL_ENABLE_OPENGL_DEBUG_CONTEXT 1
|
||||
|
||||
// Show OpenGL extensions and capabilities detailed logs on init
|
||||
//#define RLGL_SHOW_GL_DETAILS_INFO 1
|
||||
|
||||
//#define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 4096 // Default internal render batch elements limits
|
||||
#define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering)
|
||||
#define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture)
|
||||
#define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture())
|
||||
|
||||
#define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack
|
||||
|
||||
#define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported
|
||||
|
||||
#define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance
|
||||
#define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance
|
||||
|
||||
// Default shader vertex attribute names to set location points
|
||||
// NOTE: When a new shader is loaded, the following locations are tried to be set for convenience
|
||||
#define RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Binded by default to shader location: 0
|
||||
#define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Binded by default to shader location: 1
|
||||
#define RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Binded by default to shader location: 2
|
||||
#define RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Binded by default to shader location: 3
|
||||
#define RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Binded by default to shader location: 4
|
||||
#define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Binded by default to shader location: 5
|
||||
|
||||
#define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix
|
||||
#define RL_DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix
|
||||
#define RL_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix
|
||||
#define RL_DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix
|
||||
#define RL_DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView))
|
||||
#define RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color)
|
||||
#define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0)
|
||||
#define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1)
|
||||
#define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2)
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: shapes - Configuration Flags
|
||||
//------------------------------------------------------------------------------------
|
||||
// Use QUADS instead of TRIANGLES for drawing when possible
|
||||
// Some lines-based shapes could still use lines
|
||||
#define SUPPORT_QUADS_DRAW_MODE 1
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: textures - Configuration Flags
|
||||
//------------------------------------------------------------------------------------
|
||||
// Selecte desired fileformats to be supported for image data loading
|
||||
#define SUPPORT_FILEFORMAT_PNG 1
|
||||
//#define SUPPORT_FILEFORMAT_BMP 1
|
||||
//#define SUPPORT_FILEFORMAT_TGA 1
|
||||
//#define SUPPORT_FILEFORMAT_JPG 1
|
||||
#define SUPPORT_FILEFORMAT_GIF 1
|
||||
//#define SUPPORT_FILEFORMAT_PSD 1
|
||||
#define SUPPORT_FILEFORMAT_DDS 1
|
||||
#define SUPPORT_FILEFORMAT_HDR 1
|
||||
//#define SUPPORT_FILEFORMAT_KTX 1
|
||||
//#define SUPPORT_FILEFORMAT_ASTC 1
|
||||
//#define SUPPORT_FILEFORMAT_PKM 1
|
||||
//#define SUPPORT_FILEFORMAT_PVR 1
|
||||
|
||||
// Support image export functionality (.png, .bmp, .tga, .jpg)
|
||||
#define SUPPORT_IMAGE_EXPORT 1
|
||||
// Support procedural image generation functionality (gradient, spot, perlin-noise, cellular)
|
||||
#define SUPPORT_IMAGE_GENERATION 1
|
||||
// Support multiple image editing functions to scale, adjust colors, flip, draw on images, crop...
|
||||
// If not defined, still some functions are supported: ImageFormat(), ImageCrop(), ImageToPOT()
|
||||
#define SUPPORT_IMAGE_MANIPULATION 1
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: text - Configuration Flags
|
||||
//------------------------------------------------------------------------------------
|
||||
// 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
|
||||
#define SUPPORT_DEFAULT_FONT 1
|
||||
// Selected desired font fileformats to be supported for loading
|
||||
#define SUPPORT_FILEFORMAT_FNT 1
|
||||
#define SUPPORT_FILEFORMAT_TTF 1
|
||||
|
||||
// Support text management functions
|
||||
// If not defined, still some functions are supported: TextLength(), TextFormat()
|
||||
#define SUPPORT_TEXT_MANIPULATION 1
|
||||
|
||||
// text: Configuration values
|
||||
//------------------------------------------------------------------------------------
|
||||
#define MAX_TEXT_BUFFER_LENGTH 1024 // Size of internal static buffers used on some functions:
|
||||
// TextFormat(), TextSubtext(), TextToUpper(), TextToLower(), TextToPascal(), TextSplit()
|
||||
#define MAX_TEXTSPLIT_COUNT 128 // Maximum number of substrings to split: TextSplit()
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: models - Configuration Flags
|
||||
//------------------------------------------------------------------------------------
|
||||
// Selected desired model fileformats to be supported for loading
|
||||
#define SUPPORT_FILEFORMAT_OBJ 1
|
||||
#define SUPPORT_FILEFORMAT_MTL 1
|
||||
#define SUPPORT_FILEFORMAT_IQM 1
|
||||
#define SUPPORT_FILEFORMAT_GLTF 1
|
||||
#define SUPPORT_FILEFORMAT_VOX 1
|
||||
// Support procedural mesh generation functions, uses external par_shapes.h library
|
||||
// NOTE: Some generated meshes DO NOT include generated texture coordinates
|
||||
#define SUPPORT_MESH_GENERATION 1
|
||||
|
||||
// models: Configuration values
|
||||
//------------------------------------------------------------------------------------
|
||||
#define MAX_MATERIAL_MAPS 12 // Maximum number of shader maps supported
|
||||
#define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: audio - Configuration Flags
|
||||
//------------------------------------------------------------------------------------
|
||||
// Desired audio fileformats to be supported for loading
|
||||
#define SUPPORT_FILEFORMAT_WAV 1
|
||||
#define SUPPORT_FILEFORMAT_OGG 1
|
||||
#define SUPPORT_FILEFORMAT_XM 1
|
||||
#define SUPPORT_FILEFORMAT_MOD 1
|
||||
#define SUPPORT_FILEFORMAT_MP3 1
|
||||
//#define SUPPORT_FILEFORMAT_FLAC 1
|
||||
|
||||
// audio: Configuration values
|
||||
//------------------------------------------------------------------------------------
|
||||
#define AUDIO_DEVICE_FORMAT ma_format_f32 // Device output format (miniaudio: float-32bit)
|
||||
#define AUDIO_DEVICE_CHANNELS 2 // Device output channels: stereo
|
||||
#define AUDIO_DEVICE_SAMPLE_RATE 0 // Device sample rate (device default)
|
||||
|
||||
#define MAX_AUDIO_BUFFER_POOL_CHANNELS 16 // Maximum number of audio pool channels
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module: utils - Configuration Flags
|
||||
//------------------------------------------------------------------------------------
|
||||
// Standard file io library (stdio.h) included
|
||||
#define SUPPORT_STANDARD_FILEIO
|
||||
// Show TRACELOG() output messages
|
||||
// NOTE: By default LOG_DEBUG traces not shown
|
||||
#define SUPPORT_TRACELOG 1
|
||||
//#define SUPPORT_TRACELOG_DEBUG 1
|
||||
|
||||
// utils: Configuration values
|
||||
//------------------------------------------------------------------------------------
|
||||
#define MAX_TRACELOG_MSG_LENGTH 128 // Max length of one trace-log message
|
||||
BIN
raylib_drm/libraylib.a
Normal file
BIN
raylib_drm/libraylib.a
Normal file
Binary file not shown.
2380
raylib_drm/raudio.c
Normal file
2380
raylib_drm/raudio.c
Normal file
File diff suppressed because it is too large
Load Diff
198
raylib_drm/raudio.h
Normal file
198
raylib_drm/raudio.h
Normal file
@@ -0,0 +1,198 @@
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* raudio v1.0 - A simple and easy-to-use audio library based on miniaudio
|
||||
*
|
||||
* FEATURES:
|
||||
* - Manage audio device (init/close)
|
||||
* - Load and unload audio files
|
||||
* - Format wave data (sample rate, size, channels)
|
||||
* - Play/Stop/Pause/Resume loaded audio
|
||||
* - Manage mixing channels
|
||||
* - Manage raw audio context
|
||||
*
|
||||
* DEPENDENCIES:
|
||||
* miniaudio.h - Audio device management lib (https://github.com/dr-soft/miniaudio)
|
||||
* stb_vorbis.h - Ogg audio files loading (http://www.nothings.org/stb_vorbis/)
|
||||
* dr_mp3.h - MP3 audio file loading (https://github.com/mackron/dr_libs)
|
||||
* dr_flac.h - FLAC audio file loading (https://github.com/mackron/dr_libs)
|
||||
* jar_xm.h - XM module file loading
|
||||
* jar_mod.h - MOD audio file loading
|
||||
*
|
||||
* CONTRIBUTORS:
|
||||
* David Reid (github: @mackron) (Nov. 2017):
|
||||
* - Complete port to miniaudio library
|
||||
*
|
||||
* Joshua Reisenauer (github: @kd7tck) (2015)
|
||||
* - XM audio module support (jar_xm)
|
||||
* - MOD audio module support (jar_mod)
|
||||
* - Mixing channels support
|
||||
* - Raw audio context support
|
||||
*
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 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
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
#ifndef RAUDIO_H
|
||||
#define RAUDIO_H
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
// Allow custom memory allocators
|
||||
#ifndef RL_MALLOC
|
||||
#define RL_MALLOC(sz) malloc(sz)
|
||||
#endif
|
||||
#ifndef RL_CALLOC
|
||||
#define RL_CALLOC(n,sz) calloc(n,sz)
|
||||
#endif
|
||||
#ifndef RL_FREE
|
||||
#define RL_FREE(p) free(p)
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
#ifndef __cplusplus
|
||||
// Boolean type
|
||||
#if !defined(_STDBOOL_H)
|
||||
typedef enum { false, true } bool;
|
||||
#define _STDBOOL_H
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Wave, audio wave data
|
||||
typedef struct Wave {
|
||||
unsigned int frameCount; // Total number of frames (considering channels)
|
||||
unsigned int sampleRate; // Frequency (samples per second)
|
||||
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
|
||||
unsigned int channels; // Number of channels (1-mono, 2-stereo, ...)
|
||||
void *data; // Buffer data pointer
|
||||
} Wave;
|
||||
|
||||
typedef struct rAudioBuffer rAudioBuffer;
|
||||
|
||||
// AudioStream, custom audio stream
|
||||
typedef struct AudioStream {
|
||||
rAudioBuffer *buffer; // Pointer to internal data used by the audio system
|
||||
|
||||
unsigned int sampleRate; // Frequency (samples per second)
|
||||
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
|
||||
unsigned int channels; // Number of channels (1-mono, 2-stereo, ...)
|
||||
} AudioStream;
|
||||
|
||||
// Sound
|
||||
typedef struct Sound {
|
||||
AudioStream stream; // Audio stream
|
||||
unsigned int frameCount; // Total number of frames (considering channels)
|
||||
} Sound;
|
||||
|
||||
// Music, audio stream, anything longer than ~10 seconds should be streamed
|
||||
typedef struct Music {
|
||||
AudioStream stream; // Audio stream
|
||||
unsigned int frameCount; // Total number of frames (considering channels)
|
||||
bool looping; // Music looping enable
|
||||
|
||||
int ctxType; // Type of music context (audio filetype)
|
||||
void *ctxData; // Audio context data, depends on type
|
||||
} Music;
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
//...
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" { // Prevents name mangling of functions
|
||||
#endif
|
||||
|
||||
// Audio device management functions
|
||||
void InitAudioDevice(void); // Initialize audio device and context
|
||||
void CloseAudioDevice(void); // Close the audio device and context
|
||||
bool IsAudioDeviceReady(void); // Check if audio device has been initialized successfully
|
||||
void SetMasterVolume(float volume); // Set master volume (listener)
|
||||
|
||||
// Wave/Sound loading/unloading functions
|
||||
Wave LoadWave(const char *fileName); // Load wave data from file
|
||||
Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int dataSize); // Load wave from memory buffer, fileType refers to extension: i.e. ".wav"
|
||||
Sound LoadSound(const char *fileName); // Load sound from file
|
||||
Sound LoadSoundFromWave(Wave wave); // Load sound from wave data
|
||||
void UpdateSound(Sound sound, const void *data, int samplesCount);// Update sound buffer with new data
|
||||
void UnloadWave(Wave wave); // Unload wave data
|
||||
void UnloadSound(Sound sound); // Unload sound
|
||||
bool ExportWave(Wave wave, const char *fileName); // Export wave data to file, returns true on success
|
||||
bool ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h), returns true on success
|
||||
|
||||
// Wave/Sound management functions
|
||||
void PlaySound(Sound sound); // Play a sound
|
||||
void StopSound(Sound sound); // Stop playing a sound
|
||||
void PauseSound(Sound sound); // Pause a sound
|
||||
void ResumeSound(Sound sound); // Resume a paused sound
|
||||
void PlaySoundMulti(Sound sound); // Play a sound (using multichannel buffer pool)
|
||||
void StopSoundMulti(void); // Stop any sound playing (using multichannel buffer pool)
|
||||
int GetSoundsPlaying(void); // Get number of sounds playing in the multichannel
|
||||
bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing
|
||||
void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level)
|
||||
void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level)
|
||||
void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels); // Convert wave data to desired format
|
||||
Wave WaveCopy(Wave wave); // Copy a wave to a new wave
|
||||
void WaveCrop(Wave *wave, int initSample, int finalSample); // Crop a wave to defined samples range
|
||||
float *LoadWaveSamples(Wave wave); // Load samples data from wave as a floats array
|
||||
void UnloadWaveSamples(float *samples); // Unload samples data loaded with LoadWaveSamples()
|
||||
|
||||
// Music management functions
|
||||
Music LoadMusicStream(const char *fileName); // Load music stream from file
|
||||
Music LoadMusicStreamFromMemory(const char *fileType, unsigned char* data, int dataSize); // Load music stream from data
|
||||
void UnloadMusicStream(Music music); // Unload music stream
|
||||
void PlayMusicStream(Music music); // Start music playing
|
||||
bool IsMusicStreamPlaying(Music music); // Check if music is playing
|
||||
void UpdateMusicStream(Music music); // Updates buffers for music streaming
|
||||
void StopMusicStream(Music music); // Stop music playing
|
||||
void PauseMusicStream(Music music); // Pause music playing
|
||||
void ResumeMusicStream(Music music); // Resume playing paused music
|
||||
void SeekMusicStream(Music music, float position); // Seek music to a position (in seconds)
|
||||
void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level)
|
||||
void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level)
|
||||
float GetMusicTimeLength(Music music); // Get music time length (in seconds)
|
||||
float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)
|
||||
|
||||
// AudioStream management functions
|
||||
AudioStream LoadAudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels); // Load audio stream (to stream raw audio pcm data)
|
||||
void UpdateAudioStream(AudioStream stream, const void *data, int samplesCount); // Update audio stream buffers with data
|
||||
void UnloadAudioStream(AudioStream stream); // Unload audio stream and free memory
|
||||
bool IsAudioStreamProcessed(AudioStream stream); // Check if any audio stream buffers requires refill
|
||||
void PlayAudioStream(AudioStream stream); // Play audio stream
|
||||
void PauseAudioStream(AudioStream stream); // Pause audio stream
|
||||
void ResumeAudioStream(AudioStream stream); // Resume audio stream
|
||||
bool IsAudioStreamPlaying(AudioStream stream); // Check if audio stream is playing
|
||||
void StopAudioStream(AudioStream stream); // Stop audio stream
|
||||
void SetAudioStreamVolume(AudioStream stream, float volume); // Set volume for audio stream (1.0 is max level)
|
||||
void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
|
||||
void SetAudioStreamBufferSizeDefault(int size); // Default size for new audio streams
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // RAUDIO_H
|
||||
1536
raylib_drm/raylib.h
Normal file
1536
raylib_drm/raylib.h
Normal file
File diff suppressed because it is too large
Load Diff
1853
raylib_drm/raymath.h
Normal file
1853
raylib_drm/raymath.h
Normal file
File diff suppressed because it is too large
Load Diff
567
raylib_drm/rcamera.h
Normal file
567
raylib_drm/rcamera.h
Normal file
@@ -0,0 +1,567 @@
|
||||
/*******************************************************************************************
|
||||
*
|
||||
* rcamera - Basic camera system for multiple camera modes
|
||||
*
|
||||
* NOTE: Memory footprint of this library is aproximately 52 bytes (global variables)
|
||||
*
|
||||
* CONFIGURATION:
|
||||
*
|
||||
* #define CAMERA_IMPLEMENTATION
|
||||
* Generates the implementation of the library into the included file.
|
||||
* If not defined, the library is in header only mode and can be included in other headers
|
||||
* or source files without problems. But only ONE file should hold the implementation.
|
||||
*
|
||||
* #define CAMERA_STANDALONE
|
||||
* If defined, the library can be used as standalone as a camera system but some
|
||||
* functions must be redefined to manage inputs accordingly.
|
||||
*
|
||||
* CONTRIBUTORS:
|
||||
* Ramon Santamaria: Supervision, review, update and maintenance
|
||||
* Marc Palau: Initial implementation (2014)
|
||||
*
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2015-2021 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 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
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
#ifndef RCAMERA_H
|
||||
#define RCAMERA_H
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
//...
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
// NOTE: Below types are required for CAMERA_STANDALONE usage
|
||||
//----------------------------------------------------------------------------------
|
||||
#if defined(CAMERA_STANDALONE)
|
||||
// Vector2 type
|
||||
typedef struct Vector2 {
|
||||
float x;
|
||||
float y;
|
||||
} Vector2;
|
||||
|
||||
// Vector3 type
|
||||
typedef struct Vector3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} Vector3;
|
||||
|
||||
// Camera type, defines a camera position/orientation in 3d space
|
||||
typedef struct Camera3D {
|
||||
Vector3 position; // Camera position
|
||||
Vector3 target; // Camera target it looks-at
|
||||
Vector3 up; // Camera up vector (rotation over its axis)
|
||||
float fovy; // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic
|
||||
int type; // Camera type, defines projection type: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
|
||||
} Camera3D;
|
||||
|
||||
typedef Camera3D Camera; // Camera type fallback, defaults to Camera3D
|
||||
|
||||
// Camera system modes
|
||||
typedef enum {
|
||||
CAMERA_CUSTOM = 0,
|
||||
CAMERA_FREE,
|
||||
CAMERA_ORBITAL,
|
||||
CAMERA_FIRST_PERSON,
|
||||
CAMERA_THIRD_PERSON
|
||||
} CameraMode;
|
||||
|
||||
// Camera projection modes
|
||||
typedef enum {
|
||||
CAMERA_PERSPECTIVE = 0,
|
||||
CAMERA_ORTHOGRAPHIC
|
||||
} CameraProjection;
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
//...
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" { // Prevents name mangling of functions
|
||||
#endif
|
||||
|
||||
#if defined(CAMERA_STANDALONE)
|
||||
void SetCameraMode(Camera camera, int mode); // Set camera mode (multiple camera modes available)
|
||||
void UpdateCamera(Camera *camera); // Update camera position for selected mode
|
||||
|
||||
void SetCameraPanControl(int keyPan); // Set camera pan key to combine with mouse movement (free camera)
|
||||
void SetCameraAltControl(int keyAlt); // Set camera alt key to combine with mouse movement (free camera)
|
||||
void SetCameraSmoothZoomControl(int szoomKey); // Set camera smooth zoom key to combine with mouse (free camera)
|
||||
void SetCameraMoveControls(int keyFront, int keyBack,
|
||||
int keyRight, int keyLeft,
|
||||
int keyUp, int keyDown); // Set camera move controls (1st person and 3rd person cameras)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // CAMERA_H
|
||||
|
||||
|
||||
/***********************************************************************************
|
||||
*
|
||||
* CAMERA IMPLEMENTATION
|
||||
*
|
||||
************************************************************************************/
|
||||
|
||||
#if defined(CAMERA_IMPLEMENTATION)
|
||||
|
||||
#include <math.h> // Required for: sinf(), cosf(), sqrtf()
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#ifndef PI
|
||||
#define PI 3.14159265358979323846
|
||||
#endif
|
||||
#ifndef DEG2RAD
|
||||
#define DEG2RAD (PI/180.0f)
|
||||
#endif
|
||||
#ifndef RAD2DEG
|
||||
#define RAD2DEG (180.0f/PI)
|
||||
#endif
|
||||
|
||||
// Camera mouse movement sensitivity
|
||||
#define CAMERA_MOUSE_MOVE_SENSITIVITY 0.003f
|
||||
#define CAMERA_MOUSE_SCROLL_SENSITIVITY 1.5f
|
||||
|
||||
// FREE_CAMERA
|
||||
#define CAMERA_FREE_MOUSE_SENSITIVITY 0.01f
|
||||
#define CAMERA_FREE_DISTANCE_MIN_CLAMP 0.3f
|
||||
#define CAMERA_FREE_DISTANCE_MAX_CLAMP 120.0f
|
||||
#define CAMERA_FREE_MIN_CLAMP 85.0f
|
||||
#define CAMERA_FREE_MAX_CLAMP -85.0f
|
||||
#define CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY 0.05f
|
||||
#define CAMERA_FREE_PANNING_DIVIDER 5.1f
|
||||
|
||||
// ORBITAL_CAMERA
|
||||
#define CAMERA_ORBITAL_SPEED 0.01f // Radians per frame
|
||||
|
||||
// FIRST_PERSON
|
||||
//#define CAMERA_FIRST_PERSON_MOUSE_SENSITIVITY 0.003f
|
||||
#define CAMERA_FIRST_PERSON_FOCUS_DISTANCE 25.0f
|
||||
#define CAMERA_FIRST_PERSON_MIN_CLAMP 89.0f
|
||||
#define CAMERA_FIRST_PERSON_MAX_CLAMP -89.0f
|
||||
|
||||
#define CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER 8.0f
|
||||
#define CAMERA_FIRST_PERSON_STEP_DIVIDER 30.0f
|
||||
#define CAMERA_FIRST_PERSON_WAVING_DIVIDER 200.0f
|
||||
|
||||
// THIRD_PERSON
|
||||
//#define CAMERA_THIRD_PERSON_MOUSE_SENSITIVITY 0.003f
|
||||
#define CAMERA_THIRD_PERSON_DISTANCE_CLAMP 1.2f
|
||||
#define CAMERA_THIRD_PERSON_MIN_CLAMP 5.0f
|
||||
#define CAMERA_THIRD_PERSON_MAX_CLAMP -85.0f
|
||||
#define CAMERA_THIRD_PERSON_OFFSET (Vector3){ 0.4f, 0.0f, 0.0f }
|
||||
|
||||
// PLAYER (used by camera)
|
||||
#define PLAYER_MOVEMENT_SENSITIVITY 20.0f
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
// Camera move modes (first person and third person cameras)
|
||||
typedef enum {
|
||||
MOVE_FRONT = 0,
|
||||
MOVE_BACK,
|
||||
MOVE_RIGHT,
|
||||
MOVE_LEFT,
|
||||
MOVE_UP,
|
||||
MOVE_DOWN
|
||||
} CameraMove;
|
||||
|
||||
// Camera global state context data [56 bytes]
|
||||
typedef struct {
|
||||
unsigned int mode; // Current camera mode
|
||||
float targetDistance; // Camera distance from position to target
|
||||
float playerEyesPosition; // Player eyes position from ground (in meters)
|
||||
Vector2 angle; // Camera angle in plane XZ
|
||||
Vector2 previousMousePosition; // Previous mouse position
|
||||
|
||||
// Camera movement control keys
|
||||
int moveControl[6]; // Move controls (CAMERA_FIRST_PERSON)
|
||||
int smoothZoomControl; // Smooth zoom control key
|
||||
int altControl; // Alternative control key
|
||||
int panControl; // Pan view control key
|
||||
} CameraData;
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
static CameraData CAMERA = { // Global CAMERA state context
|
||||
.mode = 0,
|
||||
.targetDistance = 0,
|
||||
.playerEyesPosition = 1.85f,
|
||||
.angle = { 0 },
|
||||
.previousMousePosition = { 0 },
|
||||
.moveControl = { 'W', 'S', 'D', 'A', 'E', 'Q' },
|
||||
.smoothZoomControl = 341, // raylib: KEY_LEFT_CONTROL
|
||||
.altControl = 342, // raylib: KEY_LEFT_ALT
|
||||
.panControl = 2 // raylib: MOUSE_BUTTON_MIDDLE
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
#if defined(CAMERA_STANDALONE)
|
||||
// NOTE: Camera controls depend on some raylib input functions
|
||||
static void EnableCursor() {} // Unlock cursor
|
||||
static void DisableCursor() {} // Lock cursor
|
||||
|
||||
static int IsKeyDown(int key) { return 0; }
|
||||
|
||||
static int IsMouseButtonDown(int button) { return 0;}
|
||||
static float GetMouseWheelMove() { return 0.0f; }
|
||||
static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; }
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Select camera mode (multiple camera modes available)
|
||||
void SetCameraMode(Camera camera, int mode)
|
||||
{
|
||||
Vector3 v1 = camera.position;
|
||||
Vector3 v2 = camera.target;
|
||||
|
||||
float dx = v2.x - v1.x;
|
||||
float dy = v2.y - v1.y;
|
||||
float dz = v2.z - v1.z;
|
||||
|
||||
CAMERA.targetDistance = sqrtf(dx*dx + dy*dy + dz*dz); // Distance to target
|
||||
|
||||
// Camera angle calculation
|
||||
CAMERA.angle.x = atan2f(dx, dz); // Camera angle in plane XZ (0 aligned with Z, move positive CCW)
|
||||
CAMERA.angle.y = atan2f(dy, sqrtf(dx*dx + dz*dz)); // Camera angle in plane XY (0 aligned with X, move positive CW)
|
||||
|
||||
CAMERA.playerEyesPosition = camera.position.y; // Init player eyes position to camera Y position
|
||||
|
||||
CAMERA.previousMousePosition = GetMousePosition(); // Init mouse position
|
||||
|
||||
// Lock cursor for first person and third person cameras
|
||||
if ((mode == CAMERA_FIRST_PERSON) || (mode == CAMERA_THIRD_PERSON)) DisableCursor();
|
||||
else EnableCursor();
|
||||
|
||||
CAMERA.mode = mode;
|
||||
}
|
||||
|
||||
// Update camera depending on selected mode
|
||||
// NOTE: Camera controls depend on some raylib functions:
|
||||
// System: EnableCursor(), DisableCursor()
|
||||
// Mouse: IsMouseButtonDown(), GetMousePosition(), GetMouseWheelMove()
|
||||
// Keys: IsKeyDown()
|
||||
void UpdateCamera(Camera *camera)
|
||||
{
|
||||
static int swingCounter = 0; // Used for 1st person swinging movement
|
||||
|
||||
// TODO: Compute CAMERA.targetDistance and CAMERA.angle here (?)
|
||||
|
||||
// Mouse movement detection
|
||||
Vector2 mousePositionDelta = { 0.0f, 0.0f };
|
||||
Vector2 mousePosition = GetMousePosition();
|
||||
float mouseWheelMove = GetMouseWheelMove();
|
||||
|
||||
// Keys input detection
|
||||
// TODO: Input detection is raylib-dependant, it could be moved outside the module
|
||||
bool keyPan = IsMouseButtonDown(CAMERA.panControl);
|
||||
bool keyAlt = IsKeyDown(CAMERA.altControl);
|
||||
bool szoomKey = IsKeyDown(CAMERA.smoothZoomControl);
|
||||
bool direction[6] = { IsKeyDown(CAMERA.moveControl[MOVE_FRONT]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_BACK]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_RIGHT]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_LEFT]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_UP]),
|
||||
IsKeyDown(CAMERA.moveControl[MOVE_DOWN]) };
|
||||
|
||||
if (CAMERA.mode != CAMERA_CUSTOM)
|
||||
{
|
||||
mousePositionDelta.x = mousePosition.x - CAMERA.previousMousePosition.x;
|
||||
mousePositionDelta.y = mousePosition.y - CAMERA.previousMousePosition.y;
|
||||
|
||||
CAMERA.previousMousePosition = mousePosition;
|
||||
}
|
||||
|
||||
// Support for multiple automatic camera modes
|
||||
// NOTE: In case of CAMERA_CUSTOM nothing happens here, user must update it manually
|
||||
switch (CAMERA.mode)
|
||||
{
|
||||
case CAMERA_FREE: // Camera free controls, using standard 3d-content-creation scheme
|
||||
{
|
||||
// Camera zoom
|
||||
if ((CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
|
||||
{
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
if (CAMERA.targetDistance > CAMERA_FREE_DISTANCE_MAX_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MAX_CLAMP;
|
||||
}
|
||||
|
||||
// Camera looking down
|
||||
else if ((camera->position.y > camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
|
||||
{
|
||||
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
}
|
||||
else if ((camera->position.y > camera->target.y) && (camera->target.y >= 0))
|
||||
{
|
||||
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
|
||||
// if (camera->target.y < 0) camera->target.y = -0.001;
|
||||
}
|
||||
else if ((camera->position.y > camera->target.y) && (camera->target.y < 0) && (mouseWheelMove > 0))
|
||||
{
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
|
||||
}
|
||||
// Camera looking up
|
||||
else if ((camera->position.y < camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
|
||||
{
|
||||
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
}
|
||||
else if ((camera->position.y < camera->target.y) && (camera->target.y <= 0))
|
||||
{
|
||||
camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
|
||||
|
||||
// if (camera->target.y > 0) camera->target.y = 0.001;
|
||||
}
|
||||
else if ((camera->position.y < camera->target.y) && (camera->target.y > 0) && (mouseWheelMove > 0))
|
||||
{
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
|
||||
}
|
||||
|
||||
// Input keys checks
|
||||
if (keyPan)
|
||||
{
|
||||
if (keyAlt) // Alternative key behaviour
|
||||
{
|
||||
if (szoomKey)
|
||||
{
|
||||
// Camera smooth zoom
|
||||
CAMERA.targetDistance += (mousePositionDelta.y*CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Camera rotation
|
||||
CAMERA.angle.x += mousePositionDelta.x*-CAMERA_FREE_MOUSE_SENSITIVITY;
|
||||
CAMERA.angle.y += mousePositionDelta.y*-CAMERA_FREE_MOUSE_SENSITIVITY;
|
||||
|
||||
// Angle clamp
|
||||
if (CAMERA.angle.y > CAMERA_FREE_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MIN_CLAMP*DEG2RAD;
|
||||
else if (CAMERA.angle.y < CAMERA_FREE_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MAX_CLAMP*DEG2RAD;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Camera panning
|
||||
camera->target.x += ((mousePositionDelta.x*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.x) + (mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*sinf(CAMERA.angle.x)*sinf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER);
|
||||
camera->target.y += ((mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER);
|
||||
camera->target.z += ((mousePositionDelta.x*-CAMERA_FREE_MOUSE_SENSITIVITY)*sinf(CAMERA.angle.x) + (mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.x)*sinf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER);
|
||||
}
|
||||
}
|
||||
|
||||
// Update camera position with changes
|
||||
camera->position.x = -sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
|
||||
camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance + camera->target.y;
|
||||
camera->position.z = -cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z;
|
||||
|
||||
} break;
|
||||
case CAMERA_ORBITAL: // Camera just orbits around target, only zoom allowed
|
||||
{
|
||||
CAMERA.angle.x += CAMERA_ORBITAL_SPEED; // Camera orbit angle
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY); // Camera zoom
|
||||
|
||||
// Camera distance clamp
|
||||
if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;
|
||||
|
||||
// Update camera position with changes
|
||||
camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
|
||||
camera->position.y = ((CAMERA.angle.y <= 0.0f)? 1 : -1)*sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y;
|
||||
camera->position.z = cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z;
|
||||
|
||||
} break;
|
||||
case CAMERA_FIRST_PERSON: // Camera moves as in a first-person game, controls are configurable
|
||||
{
|
||||
camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_FRONT] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_LEFT] +
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
|
||||
|
||||
camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] -
|
||||
sinf(CAMERA.angle.y)*direction[MOVE_BACK] +
|
||||
1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])/PLAYER_MOVEMENT_SENSITIVITY;
|
||||
|
||||
camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_FRONT] +
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_LEFT] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
|
||||
|
||||
// Camera orientation calculation
|
||||
CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
|
||||
// Angle clamp
|
||||
if (CAMERA.angle.y > CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD;
|
||||
else if (CAMERA.angle.y < CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD;
|
||||
|
||||
// Calculate translation matrix
|
||||
Matrix matTranslation = { 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, (CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER),
|
||||
0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
// Calculate rotation matrix
|
||||
Matrix matRotation = { 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
float cosz = cosf(0.0f);
|
||||
float sinz = sinf(0.0f);
|
||||
float cosy = cosf(-(PI*2 - CAMERA.angle.x));
|
||||
float siny = sinf(-(PI*2 - CAMERA.angle.x));
|
||||
float cosx = cosf(-(PI*2 - CAMERA.angle.y));
|
||||
float sinx = sinf(-(PI*2 - CAMERA.angle.y));
|
||||
|
||||
matRotation.m0 = cosz*cosy;
|
||||
matRotation.m4 = (cosz*siny*sinx) - (sinz*cosx);
|
||||
matRotation.m8 = (cosz*siny*cosx) + (sinz*sinx);
|
||||
matRotation.m1 = sinz*cosy;
|
||||
matRotation.m5 = (sinz*siny*sinx) + (cosz*cosx);
|
||||
matRotation.m9 = (sinz*siny*cosx) - (cosz*sinx);
|
||||
matRotation.m2 = -siny;
|
||||
matRotation.m6 = cosy*sinx;
|
||||
matRotation.m10= cosy*cosx;
|
||||
|
||||
// Multiply translation and rotation matrices
|
||||
Matrix matTransform = { 0 };
|
||||
matTransform.m0 = matTranslation.m0*matRotation.m0 + matTranslation.m1*matRotation.m4 + matTranslation.m2*matRotation.m8 + matTranslation.m3*matRotation.m12;
|
||||
matTransform.m1 = matTranslation.m0*matRotation.m1 + matTranslation.m1*matRotation.m5 + matTranslation.m2*matRotation.m9 + matTranslation.m3*matRotation.m13;
|
||||
matTransform.m2 = matTranslation.m0*matRotation.m2 + matTranslation.m1*matRotation.m6 + matTranslation.m2*matRotation.m10 + matTranslation.m3*matRotation.m14;
|
||||
matTransform.m3 = matTranslation.m0*matRotation.m3 + matTranslation.m1*matRotation.m7 + matTranslation.m2*matRotation.m11 + matTranslation.m3*matRotation.m15;
|
||||
matTransform.m4 = matTranslation.m4*matRotation.m0 + matTranslation.m5*matRotation.m4 + matTranslation.m6*matRotation.m8 + matTranslation.m7*matRotation.m12;
|
||||
matTransform.m5 = matTranslation.m4*matRotation.m1 + matTranslation.m5*matRotation.m5 + matTranslation.m6*matRotation.m9 + matTranslation.m7*matRotation.m13;
|
||||
matTransform.m6 = matTranslation.m4*matRotation.m2 + matTranslation.m5*matRotation.m6 + matTranslation.m6*matRotation.m10 + matTranslation.m7*matRotation.m14;
|
||||
matTransform.m7 = matTranslation.m4*matRotation.m3 + matTranslation.m5*matRotation.m7 + matTranslation.m6*matRotation.m11 + matTranslation.m7*matRotation.m15;
|
||||
matTransform.m8 = matTranslation.m8*matRotation.m0 + matTranslation.m9*matRotation.m4 + matTranslation.m10*matRotation.m8 + matTranslation.m11*matRotation.m12;
|
||||
matTransform.m9 = matTranslation.m8*matRotation.m1 + matTranslation.m9*matRotation.m5 + matTranslation.m10*matRotation.m9 + matTranslation.m11*matRotation.m13;
|
||||
matTransform.m10 = matTranslation.m8*matRotation.m2 + matTranslation.m9*matRotation.m6 + matTranslation.m10*matRotation.m10 + matTranslation.m11*matRotation.m14;
|
||||
matTransform.m11 = matTranslation.m8*matRotation.m3 + matTranslation.m9*matRotation.m7 + matTranslation.m10*matRotation.m11 + matTranslation.m11*matRotation.m15;
|
||||
matTransform.m12 = matTranslation.m12*matRotation.m0 + matTranslation.m13*matRotation.m4 + matTranslation.m14*matRotation.m8 + matTranslation.m15*matRotation.m12;
|
||||
matTransform.m13 = matTranslation.m12*matRotation.m1 + matTranslation.m13*matRotation.m5 + matTranslation.m14*matRotation.m9 + matTranslation.m15*matRotation.m13;
|
||||
matTransform.m14 = matTranslation.m12*matRotation.m2 + matTranslation.m13*matRotation.m6 + matTranslation.m14*matRotation.m10 + matTranslation.m15*matRotation.m14;
|
||||
matTransform.m15 = matTranslation.m12*matRotation.m3 + matTranslation.m13*matRotation.m7 + matTranslation.m14*matRotation.m11 + matTranslation.m15*matRotation.m15;
|
||||
|
||||
camera->target.x = camera->position.x - matTransform.m12;
|
||||
camera->target.y = camera->position.y - matTransform.m13;
|
||||
camera->target.z = camera->position.z - matTransform.m14;
|
||||
|
||||
// If movement detected (some key pressed), increase swinging
|
||||
for (int i = 0; i < 6; i++) if (direction[i]) { swingCounter++; break; }
|
||||
|
||||
// Camera position update
|
||||
// NOTE: On CAMERA_FIRST_PERSON player Y-movement is limited to player 'eyes position'
|
||||
camera->position.y = CAMERA.playerEyesPosition - sinf(swingCounter/CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER)/CAMERA_FIRST_PERSON_STEP_DIVIDER;
|
||||
|
||||
camera->up.x = sinf(swingCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER;
|
||||
camera->up.z = -sinf(swingCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER;
|
||||
|
||||
} break;
|
||||
case CAMERA_THIRD_PERSON: // Camera moves as in a third-person game, following target at a distance, controls are configurable
|
||||
{
|
||||
camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_FRONT] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_LEFT] +
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
|
||||
|
||||
camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] -
|
||||
sinf(CAMERA.angle.y)*direction[MOVE_BACK] +
|
||||
1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])/PLAYER_MOVEMENT_SENSITIVITY;
|
||||
|
||||
camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] -
|
||||
cosf(CAMERA.angle.x)*direction[MOVE_FRONT] +
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_LEFT] -
|
||||
sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
|
||||
|
||||
// Camera orientation calculation
|
||||
CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY);
|
||||
|
||||
// Angle clamp
|
||||
if (CAMERA.angle.y > CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD;
|
||||
else if (CAMERA.angle.y < CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD;
|
||||
|
||||
// Camera zoom
|
||||
CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
|
||||
|
||||
// Camera distance clamp
|
||||
if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;
|
||||
|
||||
camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
|
||||
|
||||
if (CAMERA.angle.y <= 0.0f) camera->position.y = sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y;
|
||||
else camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y;
|
||||
|
||||
camera->position.z = cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z;
|
||||
|
||||
} break;
|
||||
case CAMERA_CUSTOM: break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
// Set camera pan key to combine with mouse movement (free camera)
|
||||
void SetCameraPanControl(int keyPan) { CAMERA.panControl = keyPan; }
|
||||
|
||||
// Set camera alt key to combine with mouse movement (free camera)
|
||||
void SetCameraAltControl(int keyAlt) { CAMERA.altControl = keyAlt; }
|
||||
|
||||
// Set camera smooth zoom key to combine with mouse (free camera)
|
||||
void SetCameraSmoothZoomControl(int szoomKey) { CAMERA.smoothZoomControl = szoomKey; }
|
||||
|
||||
// Set camera move controls (1st person and 3rd person cameras)
|
||||
void SetCameraMoveControls(int keyFront, int keyBack, int keyRight, int keyLeft, int keyUp, int keyDown)
|
||||
{
|
||||
CAMERA.moveControl[MOVE_FRONT] = keyFront;
|
||||
CAMERA.moveControl[MOVE_BACK] = keyBack;
|
||||
CAMERA.moveControl[MOVE_RIGHT] = keyRight;
|
||||
CAMERA.moveControl[MOVE_LEFT] = keyLeft;
|
||||
CAMERA.moveControl[MOVE_UP] = keyUp;
|
||||
CAMERA.moveControl[MOVE_DOWN] = keyDown;
|
||||
}
|
||||
|
||||
#endif // CAMERA_IMPLEMENTATION
|
||||
6772
raylib_drm/rcore.c
Normal file
6772
raylib_drm/rcore.c
Normal file
File diff suppressed because it is too large
Load Diff
566
raylib_drm/rgestures.h
Normal file
566
raylib_drm/rgestures.h
Normal file
@@ -0,0 +1,566 @@
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* rgestures - Gestures system, gestures processing based on input events (touch/mouse)
|
||||
*
|
||||
* NOTE: Memory footprint of this library is aproximately 128 bytes (global variables)
|
||||
*
|
||||
* CONFIGURATION:
|
||||
*
|
||||
* #define GESTURES_IMPLEMENTATION
|
||||
* Generates the implementation of the library into the included file.
|
||||
* If not defined, the library is in header only mode and can be included in other headers
|
||||
* or source files without problems. But only ONE file should hold the implementation.
|
||||
*
|
||||
* #define GESTURES_STANDALONE
|
||||
* If defined, the library can be used as standalone to process gesture events with
|
||||
* no external dependencies.
|
||||
*
|
||||
* CONTRIBUTORS:
|
||||
* Marc Palau: Initial implementation (2014)
|
||||
* Albert Martos: Complete redesign and testing (2015)
|
||||
* Ian Eito: Complete redesign and testing (2015)
|
||||
* Ramon Santamaria: Supervision, review, update and maintenance
|
||||
*
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 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
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
#ifndef RGESTURES_H
|
||||
#define RGESTURES_H
|
||||
|
||||
#ifndef PI
|
||||
#define PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#ifndef MAX_TOUCH_POINTS
|
||||
#define MAX_TOUCH_POINTS 8 // Maximum number of touch points supported
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
// NOTE: Below types are required for GESTURES_STANDALONE usage
|
||||
//----------------------------------------------------------------------------------
|
||||
// Boolean type
|
||||
#if defined(__STDC__) && __STDC_VERSION__ >= 199901L
|
||||
#include <stdbool.h>
|
||||
#elif !defined(__cplusplus) && !defined(bool) && !defined(RL_BOOL_TYPE)
|
||||
typedef enum bool { false, true } bool;
|
||||
#endif
|
||||
|
||||
#if !defined(RL_VECTOR2_TYPE)
|
||||
// Vector2 type
|
||||
typedef struct Vector2 {
|
||||
float x;
|
||||
float y;
|
||||
} Vector2;
|
||||
#endif
|
||||
|
||||
#if defined(GESTURES_STANDALONE)
|
||||
// Gestures type
|
||||
// NOTE: It could be used as flags to enable only some gestures
|
||||
typedef enum {
|
||||
GESTURE_NONE = 0,
|
||||
GESTURE_TAP = 1,
|
||||
GESTURE_DOUBLETAP = 2,
|
||||
GESTURE_HOLD = 4,
|
||||
GESTURE_DRAG = 8,
|
||||
GESTURE_SWIPE_RIGHT = 16,
|
||||
GESTURE_SWIPE_LEFT = 32,
|
||||
GESTURE_SWIPE_UP = 64,
|
||||
GESTURE_SWIPE_DOWN = 128,
|
||||
GESTURE_PINCH_IN = 256,
|
||||
GESTURE_PINCH_OUT = 512
|
||||
} Gesture;
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
TOUCH_ACTION_UP = 0,
|
||||
TOUCH_ACTION_DOWN,
|
||||
TOUCH_ACTION_MOVE,
|
||||
TOUCH_ACTION_CANCEL
|
||||
} TouchAction;
|
||||
|
||||
// Gesture event
|
||||
typedef struct {
|
||||
int touchAction;
|
||||
int pointCount;
|
||||
int pointId[MAX_TOUCH_POINTS];
|
||||
Vector2 position[MAX_TOUCH_POINTS];
|
||||
} GestureEvent;
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
//...
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" { // Prevents name mangling of functions
|
||||
#endif
|
||||
|
||||
void ProcessGestureEvent(GestureEvent event); // Process gesture event and translate it into gestures
|
||||
void UpdateGestures(void); // Update gestures detected (must be called every frame)
|
||||
|
||||
#if defined(GESTURES_STANDALONE)
|
||||
void SetGesturesEnabled(unsigned int flags); // Enable a set of gestures using flags
|
||||
bool IsGestureDetected(int gesture); // Check if a gesture have been detected
|
||||
int GetGestureDetected(void); // Get latest detected gesture
|
||||
|
||||
float GetGestureHoldDuration(void); // Get gesture hold time in milliseconds
|
||||
Vector2 GetGestureDragVector(void); // Get gesture drag vector
|
||||
float GetGestureDragAngle(void); // Get gesture drag angle
|
||||
Vector2 GetGesturePinchVector(void); // Get gesture pinch delta
|
||||
float GetGesturePinchAngle(void); // Get gesture pinch angle
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // GESTURES_H
|
||||
|
||||
/***********************************************************************************
|
||||
*
|
||||
* GESTURES IMPLEMENTATION
|
||||
*
|
||||
************************************************************************************/
|
||||
|
||||
#if defined(GESTURES_IMPLEMENTATION)
|
||||
|
||||
#if defined(_WIN32)
|
||||
#if defined(__cplusplus)
|
||||
extern "C" { // Prevents name mangling of functions
|
||||
#endif
|
||||
// Functions required to query time on Windows
|
||||
int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
|
||||
int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency);
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
#elif defined(__linux__)
|
||||
#if _POSIX_C_SOURCE < 199309L
|
||||
#undef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 199309L // Required for CLOCK_MONOTONIC if compiled with c99 without gnu ext.
|
||||
#endif
|
||||
#include <sys/time.h> // Required for: timespec
|
||||
#include <time.h> // Required for: clock_gettime()
|
||||
|
||||
#include <math.h> // Required for: sqrtf(), atan2f()
|
||||
#endif
|
||||
#if defined(__APPLE__) // macOS also defines __MACH__
|
||||
#include <mach/clock.h> // Required for: clock_get_time()
|
||||
#include <mach/mach.h> // Required for: mach_timespec_t
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#define FORCE_TO_SWIPE 0.0005f // Swipe force, measured in normalized screen units/time
|
||||
#define MINIMUM_DRAG 0.015f // Drag minimum force, measured in normalized screen units (0.0f to 1.0f)
|
||||
#define MINIMUM_PINCH 0.005f // Pinch minimum force, measured in normalized screen units (0.0f to 1.0f)
|
||||
#define TAP_TIMEOUT 300 // Tap minimum time, measured in milliseconds
|
||||
#define PINCH_TIMEOUT 300 // Pinch minimum time, measured in milliseconds
|
||||
#define DOUBLETAP_RANGE 0.03f // DoubleTap range, measured in normalized screen units (0.0f to 1.0f)
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Gestures module state context [136 bytes]
|
||||
typedef struct {
|
||||
unsigned int current; // Current detected gesture
|
||||
unsigned int enabledFlags; // Enabled gestures flags
|
||||
struct {
|
||||
int firstId; // Touch id for first touch point
|
||||
int pointCount; // Touch points counter
|
||||
double eventTime; // Time stamp when an event happened
|
||||
Vector2 upPosition; // Touch up position
|
||||
Vector2 downPositionA; // First touch down position
|
||||
Vector2 downPositionB; // Second touch down position
|
||||
Vector2 downDragPosition; // Touch drag position
|
||||
Vector2 moveDownPositionA; // First touch down position on move
|
||||
Vector2 moveDownPositionB; // Second touch down position on move
|
||||
int tapCounter; // TAP counter (one tap implies TOUCH_ACTION_DOWN and TOUCH_ACTION_UP actions)
|
||||
} Touch;
|
||||
struct {
|
||||
bool resetRequired; // HOLD reset to get first touch point again
|
||||
double timeDuration; // HOLD duration in milliseconds
|
||||
} Hold;
|
||||
struct {
|
||||
Vector2 vector; // DRAG vector (between initial and current position)
|
||||
float angle; // DRAG angle (relative to x-axis)
|
||||
float distance; // DRAG distance (from initial touch point to final) (normalized [0..1])
|
||||
float intensity; // DRAG intensity, how far why did the DRAG (pixels per frame)
|
||||
} Drag;
|
||||
struct {
|
||||
bool start; // SWIPE used to define when start measuring GESTURES.Swipe.timeDuration
|
||||
double timeDuration; // SWIPE time to calculate drag intensity
|
||||
} Swipe;
|
||||
struct {
|
||||
Vector2 vector; // PINCH vector (between first and second touch points)
|
||||
float angle; // PINCH angle (relative to x-axis)
|
||||
float distance; // PINCH displacement distance (normalized [0..1])
|
||||
} Pinch;
|
||||
} GesturesData;
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
static GesturesData GESTURES = {
|
||||
.Touch.firstId = -1,
|
||||
.current = GESTURE_NONE, // No current gesture detected
|
||||
.enabledFlags = 0b0000001111111111 // All gestures supported by default
|
||||
};
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
static float rgVector2Angle(Vector2 initialPosition, Vector2 finalPosition);
|
||||
static float rgVector2Distance(Vector2 v1, Vector2 v2);
|
||||
static double rgGetCurrentTime(void);
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Enable only desired getures to be detected
|
||||
void SetGesturesEnabled(unsigned int flags)
|
||||
{
|
||||
GESTURES.enabledFlags = flags;
|
||||
}
|
||||
|
||||
// Check if a gesture have been detected
|
||||
bool IsGestureDetected(int gesture)
|
||||
{
|
||||
if ((GESTURES.enabledFlags & GESTURES.current) == gesture) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
// Process gesture event and translate it into gestures
|
||||
void ProcessGestureEvent(GestureEvent event)
|
||||
{
|
||||
// Reset required variables
|
||||
GESTURES.Touch.pointCount = event.pointCount; // Required on UpdateGestures()
|
||||
|
||||
if (GESTURES.Touch.pointCount == 1) // One touch point
|
||||
{
|
||||
if (event.touchAction == TOUCH_ACTION_DOWN)
|
||||
{
|
||||
GESTURES.Touch.tapCounter++; // Tap counter
|
||||
|
||||
// Detect GESTURE_DOUBLE_TAP
|
||||
if ((GESTURES.current == GESTURE_NONE) && (GESTURES.Touch.tapCounter >= 2) && ((rgGetCurrentTime() - GESTURES.Touch.eventTime) < TAP_TIMEOUT) && (rgVector2Distance(GESTURES.Touch.downPositionA, event.position[0]) < DOUBLETAP_RANGE))
|
||||
{
|
||||
GESTURES.current = GESTURE_DOUBLETAP;
|
||||
GESTURES.Touch.tapCounter = 0;
|
||||
}
|
||||
else // Detect GESTURE_TAP
|
||||
{
|
||||
GESTURES.Touch.tapCounter = 1;
|
||||
GESTURES.current = GESTURE_TAP;
|
||||
}
|
||||
|
||||
GESTURES.Touch.downPositionA = event.position[0];
|
||||
GESTURES.Touch.downDragPosition = event.position[0];
|
||||
|
||||
GESTURES.Touch.upPosition = GESTURES.Touch.downPositionA;
|
||||
GESTURES.Touch.eventTime = rgGetCurrentTime();
|
||||
|
||||
GESTURES.Touch.firstId = event.pointId[0];
|
||||
|
||||
GESTURES.Drag.vector = (Vector2){ 0.0f, 0.0f };
|
||||
}
|
||||
else if (event.touchAction == TOUCH_ACTION_UP)
|
||||
{
|
||||
if (GESTURES.current == GESTURE_DRAG) GESTURES.Touch.upPosition = event.position[0];
|
||||
|
||||
// NOTE: GESTURES.Drag.intensity dependend on the resolution of the screen
|
||||
GESTURES.Drag.distance = rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition);
|
||||
GESTURES.Drag.intensity = GESTURES.Drag.distance/(float)((rgGetCurrentTime() - GESTURES.Swipe.timeDuration));
|
||||
|
||||
GESTURES.Swipe.start = false;
|
||||
|
||||
// Detect GESTURE_SWIPE
|
||||
if ((GESTURES.Drag.intensity > FORCE_TO_SWIPE) && (GESTURES.Touch.firstId == event.pointId[0]))
|
||||
{
|
||||
// NOTE: Angle should be inverted in Y
|
||||
GESTURES.Drag.angle = 360.0f - rgVector2Angle(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition);
|
||||
|
||||
if ((GESTURES.Drag.angle < 30) || (GESTURES.Drag.angle > 330)) GESTURES.current = GESTURE_SWIPE_RIGHT; // Right
|
||||
else if ((GESTURES.Drag.angle > 30) && (GESTURES.Drag.angle < 120)) GESTURES.current = GESTURE_SWIPE_UP; // Up
|
||||
else if ((GESTURES.Drag.angle > 120) && (GESTURES.Drag.angle < 210)) GESTURES.current = GESTURE_SWIPE_LEFT; // Left
|
||||
else if ((GESTURES.Drag.angle > 210) && (GESTURES.Drag.angle < 300)) GESTURES.current = GESTURE_SWIPE_DOWN; // Down
|
||||
else GESTURES.current = GESTURE_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
GESTURES.Drag.distance = 0.0f;
|
||||
GESTURES.Drag.intensity = 0.0f;
|
||||
GESTURES.Drag.angle = 0.0f;
|
||||
|
||||
GESTURES.current = GESTURE_NONE;
|
||||
}
|
||||
|
||||
GESTURES.Touch.downDragPosition = (Vector2){ 0.0f, 0.0f };
|
||||
GESTURES.Touch.pointCount = 0;
|
||||
}
|
||||
else if (event.touchAction == TOUCH_ACTION_MOVE)
|
||||
{
|
||||
if (GESTURES.current == GESTURE_DRAG) GESTURES.Touch.eventTime = rgGetCurrentTime();
|
||||
|
||||
if (!GESTURES.Swipe.start)
|
||||
{
|
||||
GESTURES.Swipe.timeDuration = rgGetCurrentTime();
|
||||
GESTURES.Swipe.start = true;
|
||||
}
|
||||
|
||||
GESTURES.Touch.moveDownPositionA = event.position[0];
|
||||
|
||||
if (GESTURES.current == GESTURE_HOLD)
|
||||
{
|
||||
if (GESTURES.Hold.resetRequired) GESTURES.Touch.downPositionA = event.position[0];
|
||||
|
||||
GESTURES.Hold.resetRequired = false;
|
||||
|
||||
// Detect GESTURE_DRAG
|
||||
if (rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.moveDownPositionA) >= MINIMUM_DRAG)
|
||||
{
|
||||
GESTURES.Touch.eventTime = rgGetCurrentTime();
|
||||
GESTURES.current = GESTURE_DRAG;
|
||||
}
|
||||
}
|
||||
|
||||
GESTURES.Drag.vector.x = GESTURES.Touch.moveDownPositionA.x - GESTURES.Touch.downDragPosition.x;
|
||||
GESTURES.Drag.vector.y = GESTURES.Touch.moveDownPositionA.y - GESTURES.Touch.downDragPosition.y;
|
||||
}
|
||||
}
|
||||
else if (GESTURES.Touch.pointCount == 2) // Two touch points
|
||||
{
|
||||
if (event.touchAction == TOUCH_ACTION_DOWN)
|
||||
{
|
||||
GESTURES.Touch.downPositionA = event.position[0];
|
||||
GESTURES.Touch.downPositionB = event.position[1];
|
||||
|
||||
//GESTURES.Pinch.distance = rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.downPositionB);
|
||||
|
||||
GESTURES.Pinch.vector.x = GESTURES.Touch.downPositionB.x - GESTURES.Touch.downPositionA.x;
|
||||
GESTURES.Pinch.vector.y = GESTURES.Touch.downPositionB.y - GESTURES.Touch.downPositionA.y;
|
||||
|
||||
GESTURES.current = GESTURE_HOLD;
|
||||
GESTURES.Hold.timeDuration = rgGetCurrentTime();
|
||||
}
|
||||
else if (event.touchAction == TOUCH_ACTION_MOVE)
|
||||
{
|
||||
GESTURES.Pinch.distance = rgVector2Distance(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB);
|
||||
|
||||
GESTURES.Touch.downPositionA = GESTURES.Touch.moveDownPositionA;
|
||||
GESTURES.Touch.downPositionB = GESTURES.Touch.moveDownPositionB;
|
||||
|
||||
GESTURES.Touch.moveDownPositionA = event.position[0];
|
||||
GESTURES.Touch.moveDownPositionB = event.position[1];
|
||||
|
||||
GESTURES.Pinch.vector.x = GESTURES.Touch.moveDownPositionB.x - GESTURES.Touch.moveDownPositionA.x;
|
||||
GESTURES.Pinch.vector.y = GESTURES.Touch.moveDownPositionB.y - GESTURES.Touch.moveDownPositionA.y;
|
||||
|
||||
if ((rgVector2Distance(GESTURES.Touch.downPositionA, GESTURES.Touch.moveDownPositionA) >= MINIMUM_PINCH) || (rgVector2Distance(GESTURES.Touch.downPositionB, GESTURES.Touch.moveDownPositionB) >= MINIMUM_PINCH))
|
||||
{
|
||||
if ((rgVector2Distance(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB) - GESTURES.Pinch.distance) < 0) GESTURES.current = GESTURE_PINCH_IN;
|
||||
else GESTURES.current = GESTURE_PINCH_OUT;
|
||||
}
|
||||
else
|
||||
{
|
||||
GESTURES.current = GESTURE_HOLD;
|
||||
GESTURES.Hold.timeDuration = rgGetCurrentTime();
|
||||
}
|
||||
|
||||
// NOTE: Angle should be inverted in Y
|
||||
GESTURES.Pinch.angle = 360.0f - rgVector2Angle(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB);
|
||||
}
|
||||
else if (event.touchAction == TOUCH_ACTION_UP)
|
||||
{
|
||||
GESTURES.Pinch.distance = 0.0f;
|
||||
GESTURES.Pinch.angle = 0.0f;
|
||||
GESTURES.Pinch.vector = (Vector2){ 0.0f, 0.0f };
|
||||
GESTURES.Touch.pointCount = 0;
|
||||
|
||||
GESTURES.current = GESTURE_NONE;
|
||||
}
|
||||
}
|
||||
else if (GESTURES.Touch.pointCount > 2) // More than two touch points
|
||||
{
|
||||
// TODO: Process gesture events for more than two points
|
||||
}
|
||||
}
|
||||
|
||||
// Update gestures detected (must be called every frame)
|
||||
void UpdateGestures(void)
|
||||
{
|
||||
// NOTE: Gestures are processed through system callbacks on touch events
|
||||
|
||||
// Detect GESTURE_HOLD
|
||||
if (((GESTURES.current == GESTURE_TAP) || (GESTURES.current == GESTURE_DOUBLETAP)) && (GESTURES.Touch.pointCount < 2))
|
||||
{
|
||||
GESTURES.current = GESTURE_HOLD;
|
||||
GESTURES.Hold.timeDuration = rgGetCurrentTime();
|
||||
}
|
||||
|
||||
if (((rgGetCurrentTime() - GESTURES.Touch.eventTime) > TAP_TIMEOUT) && (GESTURES.current == GESTURE_DRAG) && (GESTURES.Touch.pointCount < 2))
|
||||
{
|
||||
GESTURES.current = GESTURE_HOLD;
|
||||
GESTURES.Hold.timeDuration = rgGetCurrentTime();
|
||||
GESTURES.Hold.resetRequired = true;
|
||||
}
|
||||
|
||||
// Detect GESTURE_NONE
|
||||
if ((GESTURES.current == GESTURE_SWIPE_RIGHT) || (GESTURES.current == GESTURE_SWIPE_UP) || (GESTURES.current == GESTURE_SWIPE_LEFT) || (GESTURES.current == GESTURE_SWIPE_DOWN))
|
||||
{
|
||||
GESTURES.current = GESTURE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
// Get latest detected gesture
|
||||
int GetGestureDetected(void)
|
||||
{
|
||||
// Get current gesture only if enabled
|
||||
return (GESTURES.enabledFlags & GESTURES.current);
|
||||
}
|
||||
|
||||
// Hold time measured in ms
|
||||
float GetGestureHoldDuration(void)
|
||||
{
|
||||
// NOTE: time is calculated on current gesture HOLD
|
||||
|
||||
double time = 0.0;
|
||||
|
||||
if (GESTURES.current == GESTURE_HOLD) time = rgGetCurrentTime() - GESTURES.Hold.timeDuration;
|
||||
|
||||
return (float)time;
|
||||
}
|
||||
|
||||
// Get drag vector (between initial touch point to current)
|
||||
Vector2 GetGestureDragVector(void)
|
||||
{
|
||||
// NOTE: drag vector is calculated on one touch points TOUCH_ACTION_MOVE
|
||||
|
||||
return GESTURES.Drag.vector;
|
||||
}
|
||||
|
||||
// Get drag angle
|
||||
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
|
||||
float GetGestureDragAngle(void)
|
||||
{
|
||||
// NOTE: drag angle is calculated on one touch points TOUCH_ACTION_UP
|
||||
|
||||
return GESTURES.Drag.angle;
|
||||
}
|
||||
|
||||
// Get distance between two pinch points
|
||||
Vector2 GetGesturePinchVector(void)
|
||||
{
|
||||
// NOTE: Pinch distance is calculated on two touch points TOUCH_ACTION_MOVE
|
||||
|
||||
return GESTURES.Pinch.vector;
|
||||
}
|
||||
|
||||
// Get angle beween two pinch points
|
||||
// NOTE: Angle in degrees, horizontal-right is 0, counterclock-wise
|
||||
float GetGesturePinchAngle(void)
|
||||
{
|
||||
// NOTE: pinch angle is calculated on two touch points TOUCH_ACTION_MOVE
|
||||
|
||||
return GESTURES.Pinch.angle;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
// Get angle from two-points vector with X-axis
|
||||
static float rgVector2Angle(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
float angle = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/PI);
|
||||
|
||||
if (angle < 0) angle += 360.0f;
|
||||
|
||||
return angle;
|
||||
}
|
||||
|
||||
// Calculate distance between two Vector2
|
||||
static float rgVector2Distance(Vector2 v1, Vector2 v2)
|
||||
{
|
||||
float result;
|
||||
|
||||
float dx = v2.x - v1.x;
|
||||
float dy = v2.y - v1.y;
|
||||
|
||||
result = (float)sqrt(dx*dx + dy*dy);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Time measure returned are milliseconds
|
||||
static double rgGetCurrentTime(void)
|
||||
{
|
||||
double time = 0;
|
||||
|
||||
#if defined(_WIN32)
|
||||
unsigned long long int clockFrequency, currentTime;
|
||||
|
||||
QueryPerformanceFrequency(&clockFrequency); // BE CAREFUL: Costly operation!
|
||||
QueryPerformanceCounter(¤tTime);
|
||||
|
||||
time = (double)currentTime/clockFrequency*1000.0f; // Time in miliseconds
|
||||
#endif
|
||||
|
||||
#if defined(__linux__)
|
||||
// NOTE: Only for Linux-based systems
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
unsigned long long int nowTime = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec; // Time in nanoseconds
|
||||
|
||||
time = ((double)nowTime/1000000.0); // Time in miliseconds
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
//#define CLOCK_REALTIME CALENDAR_CLOCK // returns UTC time since 1970-01-01
|
||||
//#define CLOCK_MONOTONIC SYSTEM_CLOCK // returns the time since boot time
|
||||
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t now;
|
||||
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
|
||||
|
||||
// NOTE: OS X does not have clock_gettime(), using clock_get_time()
|
||||
clock_get_time(cclock, &now);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
unsigned long long int nowTime = (unsigned long long int)now.tv_sec*1000000000LLU + (unsigned long long int)now.tv_nsec; // Time in nanoseconds
|
||||
|
||||
time = ((double)nowTime/1000000.0); // Time in miliseconds
|
||||
#endif
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
#endif // GESTURES_IMPLEMENTATION
|
||||
126
raylib_drm/rglfw.c
Normal file
126
raylib_drm/rglfw.c
Normal file
@@ -0,0 +1,126 @@
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* rglfw - raylib GLFW single file compilation
|
||||
*
|
||||
* This file includes latest GLFW sources (https://github.com/glfw/glfw) to be compiled together
|
||||
* with raylib for all supported platforms, this way, no external dependencies are required.
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2017-2021 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 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
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
//#define _GLFW_BUILD_DLL // To build shared version
|
||||
// Ref: http://www.glfw.org/docs/latest/compile.html#compile_manual
|
||||
|
||||
// Platform options:
|
||||
// _GLFW_WIN32 to use the Win32 API
|
||||
// _GLFW_X11 to use the X Window System
|
||||
// _GLFW_WAYLAND to use the Wayland API (experimental and incomplete)
|
||||
// _GLFW_COCOA to use the Cocoa frameworks
|
||||
// _GLFW_OSMESA to use the OSMesa API (headless and non-interactive)
|
||||
// _GLFW_MIR experimental, not supported at this moment
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define _GLFW_WIN32
|
||||
#endif
|
||||
#if defined(__linux__)
|
||||
#if !defined(_GLFW_WAYLAND) // Required for Wayland windowing
|
||||
#define _GLFW_X11
|
||||
#endif
|
||||
#endif
|
||||
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
|
||||
#define _GLFW_X11
|
||||
#endif
|
||||
#if defined(__APPLE__)
|
||||
#define _GLFW_COCOA
|
||||
#define _GLFW_USE_MENUBAR // To create and populate the menu bar when the first window is created
|
||||
#define _GLFW_USE_RETINA // To have windows use the full resolution of Retina displays
|
||||
#endif
|
||||
#if defined(__TINYC__)
|
||||
#define _WIN32_WINNT_WINXP 0x0501
|
||||
#endif
|
||||
|
||||
// Common modules to all platforms
|
||||
#include "external/glfw/src/context.c"
|
||||
#include "external/glfw/src/init.c"
|
||||
#include "external/glfw/src/input.c"
|
||||
#include "external/glfw/src/monitor.c"
|
||||
#include "external/glfw/src/vulkan.c"
|
||||
#include "external/glfw/src/window.c"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include "external/glfw/src/win32_init.c"
|
||||
#include "external/glfw/src/win32_joystick.c"
|
||||
#include "external/glfw/src/win32_monitor.c"
|
||||
#include "external/glfw/src/win32_time.c"
|
||||
#include "external/glfw/src/win32_thread.c"
|
||||
#include "external/glfw/src/win32_window.c"
|
||||
#include "external/glfw/src/wgl_context.c"
|
||||
#include "external/glfw/src/egl_context.c"
|
||||
#include "external/glfw/src/osmesa_context.c"
|
||||
#endif
|
||||
|
||||
#if defined(__linux__)
|
||||
#if defined(_GLFW_WAYLAND)
|
||||
#include "external/glfw/src/wl_init.c"
|
||||
#include "external/glfw/src/wl_monitor.c"
|
||||
#include "external/glfw/src/wl_window.c"
|
||||
#include "external/glfw/src/wayland-pointer-constraints-unstable-v1-client-protocol.c"
|
||||
#include "external/glfw/src/wayland-relative-pointer-unstable-v1-client-protocol.c"
|
||||
#endif
|
||||
#if defined(_GLFW_X11)
|
||||
#include "external/glfw/src/x11_init.c"
|
||||
#include "external/glfw/src/x11_monitor.c"
|
||||
#include "external/glfw/src/x11_window.c"
|
||||
#include "external/glfw/src/glx_context.c"
|
||||
#endif
|
||||
|
||||
#include "external/glfw/src/linux_joystick.c"
|
||||
#include "external/glfw/src/posix_thread.c"
|
||||
#include "external/glfw/src/posix_time.c"
|
||||
#include "external/glfw/src/xkb_unicode.c"
|
||||
#include "external/glfw/src/egl_context.c"
|
||||
#include "external/glfw/src/osmesa_context.c"
|
||||
#endif
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__) || defined(__DragonFly__)
|
||||
#include "external/glfw/src/x11_init.c"
|
||||
#include "external/glfw/src/x11_monitor.c"
|
||||
#include "external/glfw/src/x11_window.c"
|
||||
#include "external/glfw/src/xkb_unicode.c"
|
||||
#include "external/glfw/src/null_joystick.c"
|
||||
#include "external/glfw/src/posix_time.c"
|
||||
#include "external/glfw/src/posix_thread.c"
|
||||
#include "external/glfw/src/glx_context.c"
|
||||
#include "external/glfw/src/egl_context.c"
|
||||
#include "external/glfw/src/osmesa_context.c"
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include "external/glfw/src/cocoa_init.m"
|
||||
#include "external/glfw/src/cocoa_joystick.m"
|
||||
#include "external/glfw/src/cocoa_monitor.m"
|
||||
#include "external/glfw/src/cocoa_window.m"
|
||||
#include "external/glfw/src/cocoa_time.c"
|
||||
#include "external/glfw/src/posix_thread.c"
|
||||
#include "external/glfw/src/nsgl_context.m"
|
||||
#include "external/glfw/src/egl_context.c"
|
||||
#include "external/glfw/src/osmesa_context.c"
|
||||
#endif
|
||||
4677
raylib_drm/rlgl.h
Normal file
4677
raylib_drm/rlgl.h
Normal file
File diff suppressed because it is too large
Load Diff
5041
raylib_drm/rmodels.c
Normal file
5041
raylib_drm/rmodels.c
Normal file
File diff suppressed because it is too large
Load Diff
1761
raylib_drm/rshapes.c
Normal file
1761
raylib_drm/rshapes.c
Normal file
File diff suppressed because it is too large
Load Diff
1790
raylib_drm/rtext.c
Normal file
1790
raylib_drm/rtext.c
Normal file
File diff suppressed because it is too large
Load Diff
4761
raylib_drm/rtextures.c
Normal file
4761
raylib_drm/rtextures.c
Normal file
File diff suppressed because it is too large
Load Diff
433
raylib_drm/utils.c
Normal file
433
raylib_drm/utils.c
Normal file
@@ -0,0 +1,433 @@
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* raylib.utils - Some common utility functions
|
||||
*
|
||||
* CONFIGURATION:
|
||||
*
|
||||
* #define SUPPORT_TRACELOG
|
||||
* Show TraceLog() output messages
|
||||
* NOTE: By default LOG_DEBUG traces not shown
|
||||
*
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 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
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
#include "raylib.h" // WARNING: Required for: LogType enum
|
||||
|
||||
// Check if config flags have been externally provided on compilation line
|
||||
#if !defined(EXTERNAL_CONFIG_FLAGS)
|
||||
#include "config.h" // Defines module configuration flags
|
||||
#endif
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
#include <errno.h> // Required for: Android error types
|
||||
#include <android/log.h> // Required for: Android log system: __android_log_vprint()
|
||||
#include <android/asset_manager.h> // Required for: Android assets manager: AAsset, AAssetManager_open(), ...
|
||||
#endif
|
||||
|
||||
#include <stdlib.h> // Required for: exit()
|
||||
#include <stdio.h> // Required for: FILE, fopen(), fseek(), ftell(), fread(), fwrite(), fprintf(), vprintf(), fclose()
|
||||
#include <stdarg.h> // Required for: va_list, va_start(), va_end()
|
||||
#include <string.h> // Required for: strcpy(), strcat()
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#ifndef MAX_TRACELOG_MSG_LENGTH
|
||||
#define MAX_TRACELOG_MSG_LENGTH 128 // Max length of one trace-log message
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
static int logTypeLevel = LOG_INFO; // Minimum log type level
|
||||
|
||||
static TraceLogCallback traceLog = NULL; // TraceLog callback function pointer
|
||||
static LoadFileDataCallback loadFileData = NULL; // LoadFileData callback funtion pointer
|
||||
static SaveFileDataCallback saveFileData = NULL; // SaveFileText callback funtion pointer
|
||||
static LoadFileTextCallback loadFileText = NULL; // LoadFileText callback funtion pointer
|
||||
static SaveFileTextCallback saveFileText = NULL; // SaveFileText callback funtion pointer
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Functions to set internal callbacks
|
||||
//----------------------------------------------------------------------------------
|
||||
void SetTraceLogCallback(TraceLogCallback callback) { traceLog = callback; } // Set custom trace log
|
||||
void SetLoadFileDataCallback(LoadFileDataCallback callback) { loadFileData = callback; } // Set custom file data loader
|
||||
void SetSaveFileDataCallback(SaveFileDataCallback callback) { saveFileData = callback; } // Set custom file data saver
|
||||
void SetLoadFileTextCallback(LoadFileTextCallback callback) { loadFileText = callback; } // Set custom file text loader
|
||||
void SetSaveFileTextCallback(SaveFileTextCallback callback) { saveFileText = callback; } // Set custom file text saver
|
||||
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
static AAssetManager *assetManager = NULL; // Android assets manager pointer
|
||||
static const char *internalDataPath = NULL; // Android internal data path
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
FILE *funopen(const void *cookie, int (*readfn)(void *, char *, int), int (*writefn)(void *, const char *, int),
|
||||
fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *));
|
||||
|
||||
static int android_read(void *cookie, char *buf, int size);
|
||||
static int android_write(void *cookie, const char *buf, int size);
|
||||
static fpos_t android_seek(void *cookie, fpos_t offset, int whence);
|
||||
static int android_close(void *cookie);
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition - Utilities
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Set the current threshold (minimum) log level
|
||||
void SetTraceLogLevel(int logType) { logTypeLevel = logType; }
|
||||
|
||||
// Show trace log messages (LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG)
|
||||
void TraceLog(int logType, const char *text, ...)
|
||||
{
|
||||
#if defined(SUPPORT_TRACELOG)
|
||||
// Message has level below current threshold, don't emit
|
||||
if (logType < logTypeLevel) return;
|
||||
|
||||
va_list args;
|
||||
va_start(args, text);
|
||||
|
||||
if (traceLog)
|
||||
{
|
||||
traceLog(logType, text, args);
|
||||
va_end(args);
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
switch (logType)
|
||||
{
|
||||
case LOG_TRACE: __android_log_vprint(ANDROID_LOG_VERBOSE, "raylib", text, args); break;
|
||||
case LOG_DEBUG: __android_log_vprint(ANDROID_LOG_DEBUG, "raylib", text, args); break;
|
||||
case LOG_INFO: __android_log_vprint(ANDROID_LOG_INFO, "raylib", text, args); break;
|
||||
case LOG_WARNING: __android_log_vprint(ANDROID_LOG_WARN, "raylib", text, args); break;
|
||||
case LOG_ERROR: __android_log_vprint(ANDROID_LOG_ERROR, "raylib", text, args); break;
|
||||
case LOG_FATAL: __android_log_vprint(ANDROID_LOG_FATAL, "raylib", text, args); break;
|
||||
default: break;
|
||||
}
|
||||
#else
|
||||
char buffer[MAX_TRACELOG_MSG_LENGTH] = { 0 };
|
||||
|
||||
switch (logType)
|
||||
{
|
||||
case LOG_TRACE: strcpy(buffer, "TRACE: "); break;
|
||||
case LOG_DEBUG: strcpy(buffer, "DEBUG: "); break;
|
||||
case LOG_INFO: strcpy(buffer, "INFO: "); break;
|
||||
case LOG_WARNING: strcpy(buffer, "WARNING: "); break;
|
||||
case LOG_ERROR: strcpy(buffer, "ERROR: "); break;
|
||||
case LOG_FATAL: strcpy(buffer, "FATAL: "); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
strcat(buffer, text);
|
||||
strcat(buffer, "\n");
|
||||
vprintf(buffer, args);
|
||||
#endif
|
||||
|
||||
va_end(args);
|
||||
|
||||
if (logType == LOG_FATAL) exit(EXIT_FAILURE); // If fatal logging, exit program
|
||||
|
||||
#endif // SUPPORT_TRACELOG
|
||||
}
|
||||
|
||||
// Internal memory allocator
|
||||
// NOTE: Initializes to zero by default
|
||||
void *MemAlloc(int size)
|
||||
{
|
||||
void *ptr = RL_CALLOC(size, 1);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// Internal memory reallocator
|
||||
void *MemRealloc(void *ptr, int size)
|
||||
{
|
||||
void *ret = RL_REALLOC(ptr, size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Internal memory free
|
||||
void MemFree(void *ptr)
|
||||
{
|
||||
RL_FREE(ptr);
|
||||
}
|
||||
|
||||
// Load data from file into a buffer
|
||||
unsigned char *LoadFileData(const char *fileName, unsigned int *bytesRead)
|
||||
{
|
||||
unsigned char *data = NULL;
|
||||
*bytesRead = 0;
|
||||
|
||||
if (fileName != NULL)
|
||||
{
|
||||
if (loadFileData)
|
||||
{
|
||||
data = loadFileData(fileName, bytesRead);
|
||||
return data;
|
||||
}
|
||||
#if defined(SUPPORT_STANDARD_FILEIO)
|
||||
FILE *file = fopen(fileName, "rb");
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
// WARNING: On binary streams SEEK_END could not be found,
|
||||
// using fseek() and ftell() could not work in some (rare) cases
|
||||
fseek(file, 0, SEEK_END);
|
||||
int size = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
if (size > 0)
|
||||
{
|
||||
data = (unsigned char *)RL_MALLOC(size*sizeof(unsigned char));
|
||||
|
||||
// NOTE: fread() returns number of read elements instead of bytes, so we read [1 byte, size elements]
|
||||
unsigned int count = (unsigned int)fread(data, sizeof(unsigned char), size, file);
|
||||
*bytesRead = count;
|
||||
|
||||
if (count != size) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially loaded", fileName);
|
||||
else TRACELOG(LOG_INFO, "FILEIO: [%s] File loaded successfully", fileName);
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read file", fileName);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
|
||||
#else
|
||||
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
||||
#endif
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// Unload file data allocated by LoadFileData()
|
||||
void UnloadFileData(unsigned char *data)
|
||||
{
|
||||
RL_FREE(data);
|
||||
}
|
||||
|
||||
// Save data to file from buffer
|
||||
bool SaveFileData(const char *fileName, void *data, unsigned int bytesToWrite)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
if (fileName != NULL)
|
||||
{
|
||||
if (saveFileData)
|
||||
{
|
||||
return saveFileData(fileName, data, bytesToWrite);
|
||||
}
|
||||
#if defined(SUPPORT_STANDARD_FILEIO)
|
||||
FILE *file = fopen(fileName, "wb");
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
unsigned int count = (unsigned int)fwrite(data, sizeof(unsigned char), bytesToWrite, file);
|
||||
|
||||
if (count == 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write file", fileName);
|
||||
else if (count != bytesToWrite) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
|
||||
else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName);
|
||||
|
||||
int result = fclose(file);
|
||||
if (result == 0) success = true;
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
|
||||
#else
|
||||
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
||||
#endif
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Load text data from file, returns a '\0' terminated string
|
||||
// NOTE: text chars array should be freed manually
|
||||
char *LoadFileText(const char *fileName)
|
||||
{
|
||||
char *text = NULL;
|
||||
|
||||
if (fileName != NULL)
|
||||
{
|
||||
if (loadFileText)
|
||||
{
|
||||
text = loadFileText(fileName);
|
||||
return text;
|
||||
}
|
||||
#if defined(SUPPORT_STANDARD_FILEIO)
|
||||
FILE *file = fopen(fileName, "rt");
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
// WARNING: When reading a file as 'text' file,
|
||||
// text mode causes carriage return-linefeed translation...
|
||||
// ...but using fseek() should return correct byte-offset
|
||||
fseek(file, 0, SEEK_END);
|
||||
unsigned int size = (unsigned int)ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
if (size > 0)
|
||||
{
|
||||
text = (char *)RL_MALLOC((size + 1)*sizeof(char));
|
||||
unsigned int count = (unsigned int)fread(text, sizeof(char), size, file);
|
||||
|
||||
// WARNING: \r\n is converted to \n on reading, so,
|
||||
// read bytes count gets reduced by the number of lines
|
||||
if (count < size) text = RL_REALLOC(text, count + 1);
|
||||
|
||||
// Zero-terminate the string
|
||||
text[count] = '\0';
|
||||
|
||||
TRACELOG(LOG_INFO, "FILEIO: [%s] Text file loaded successfully", fileName);
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to read text file", fileName);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
|
||||
#else
|
||||
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
||||
#endif
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
// Unload file text data allocated by LoadFileText()
|
||||
void UnloadFileText(char *text)
|
||||
{
|
||||
RL_FREE(text);
|
||||
}
|
||||
|
||||
// Save text data to file (write), string must be '\0' terminated
|
||||
bool SaveFileText(const char *fileName, char *text)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
if (fileName != NULL)
|
||||
{
|
||||
if (saveFileText)
|
||||
{
|
||||
return saveFileText(fileName, text);
|
||||
}
|
||||
#if defined(SUPPORT_STANDARD_FILEIO)
|
||||
FILE *file = fopen(fileName, "wt");
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
int count = fprintf(file, "%s", text);
|
||||
|
||||
if (count < 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName);
|
||||
else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName);
|
||||
|
||||
int result = fclose(file);
|
||||
if (result == 0) success = true;
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
|
||||
#else
|
||||
TRACELOG(LOG_WARNING, "FILEIO: Standard file io not supported, use custom file callback");
|
||||
#endif
|
||||
}
|
||||
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
// Initialize asset manager from android app
|
||||
void InitAssetManager(AAssetManager *manager, const char *dataPath)
|
||||
{
|
||||
assetManager = manager;
|
||||
internalDataPath = dataPath;
|
||||
}
|
||||
|
||||
// Replacement for fopen()
|
||||
// Ref: https://developer.android.com/ndk/reference/group/asset
|
||||
FILE *android_fopen(const char *fileName, const char *mode)
|
||||
{
|
||||
if (mode[0] == 'w')
|
||||
{
|
||||
// fopen() is mapped to android_fopen() that only grants read access to
|
||||
// assets directory through AAssetManager but we want to also be able to
|
||||
// write data when required using the standard stdio FILE access functions
|
||||
// Ref: https://stackoverflow.com/questions/11294487/android-writing-saving-files-from-native-code-only
|
||||
#undef fopen
|
||||
return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
|
||||
#define fopen(name, mode) android_fopen(name, mode)
|
||||
}
|
||||
else
|
||||
{
|
||||
// NOTE: AAsset provides access to read-only asset
|
||||
AAsset *asset = AAssetManager_open(assetManager, fileName, AASSET_MODE_UNKNOWN);
|
||||
|
||||
if (asset != NULL)
|
||||
{
|
||||
// Get pointer to file in the assets
|
||||
return funopen(asset, android_read, android_write, android_seek, android_close);
|
||||
}
|
||||
else
|
||||
{
|
||||
#undef fopen
|
||||
// Just do a regular open if file is not found in the assets
|
||||
return fopen(TextFormat("%s/%s", internalDataPath, fileName), mode);
|
||||
#define fopen(name, mode) android_fopen(name, mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // PLATFORM_ANDROID
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module specific Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
static int android_read(void *cookie, char *buf, int size)
|
||||
{
|
||||
return AAsset_read((AAsset *)cookie, buf, size);
|
||||
}
|
||||
|
||||
static int android_write(void *cookie, const char *buf, int size)
|
||||
{
|
||||
TRACELOG(LOG_WARNING, "ANDROID: Failed to provide write access to APK");
|
||||
|
||||
return EACCES;
|
||||
}
|
||||
|
||||
static fpos_t android_seek(void *cookie, fpos_t offset, int whence)
|
||||
{
|
||||
return AAsset_seek((AAsset *)cookie, offset, whence);
|
||||
}
|
||||
|
||||
static int android_close(void *cookie)
|
||||
{
|
||||
AAsset_close((AAsset *)cookie);
|
||||
return 0;
|
||||
}
|
||||
#endif // PLATFORM_ANDROID
|
||||
81
raylib_drm/utils.h
Normal file
81
raylib_drm/utils.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/**********************************************************************************************
|
||||
*
|
||||
* raylib.utils - Some common utility functions
|
||||
*
|
||||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||
* will the authors be held liable for any damages arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
*
|
||||
* 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
|
||||
* in the product documentation would be appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||
* as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
#include <stdio.h> // Required for: FILE
|
||||
#include <android/asset_manager.h> // Required for: AAssetManager
|
||||
#endif
|
||||
|
||||
#if defined(SUPPORT_TRACELOG)
|
||||
#define TRACELOG(level, ...) TraceLog(level, __VA_ARGS__)
|
||||
|
||||
#if defined(SUPPORT_TRACELOG_DEBUG)
|
||||
#define TRACELOGD(...) TraceLog(LOG_DEBUG, __VA_ARGS__)
|
||||
#else
|
||||
#define TRACELOGD(...) (void)0
|
||||
#endif
|
||||
#else
|
||||
#define TRACELOG(level, ...) (void)0
|
||||
#define TRACELOGD(...) (void)0
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Some basic Defines
|
||||
//----------------------------------------------------------------------------------
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
#define fopen(name, mode) android_fopen(name, mode)
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
//...
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Global Variables Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
// Nop...
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//----------------------------------------------------------------------------------
|
||||
#ifdef __cplusplus
|
||||
extern "C" { // Prevents name mangling of functions
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_ANDROID)
|
||||
void InitAssetManager(AAssetManager *manager, const char *dataPath); // Initialize asset manager from android app
|
||||
FILE *android_fopen(const char *fileName, const char *mode); // Replacement for fopen() -> Read-only!
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // UTILS_H
|
||||
Reference in New Issue
Block a user