Got refactored raylib working in the vis_ast binary

Now to make that debugger...
This commit is contained in:
2023-11-19 00:01:35 -05:00
parent 053daaf877
commit 36ebbfe29b
19 changed files with 4581 additions and 4469 deletions

View File

@ -25,12 +25,12 @@
*
**********************************************************************************************/
#ifndef RL_CONFIG_H
#define RL_CONFIG_H
#ifndef CONFIG_H
#define CONFIG_H
//------------------------------------------------------------------------------------
// Module selection - Some modules could be avoided
// Mandatory modules: rcore, RLGL_, utils
// Mandatory modules: rcore, rlgl, utils
//------------------------------------------------------------------------------------
#define RL_SUPPORT_MODULE_RSHAPES 1
#define RL_SUPPORT_MODULE_RTEXTURES 1
@ -41,12 +41,12 @@
//------------------------------------------------------------------------------------
// Module: rcore - Configuration Flags
//------------------------------------------------------------------------------------
// RL_Camera module is included (rcamera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital
// Camera module is included (rcamera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital
#define RL_SUPPORT_CAMERA_SYSTEM 1
// Gestures module is included (rgestures.h) to support gestures detection: tap, hold, swipe, drag
#define RL_SUPPORT_GESTURES_SYSTEM 1
// Include pseudo-random numbers generator (rprand.h), based on Xoshiro128** and SplitMix64
#define SUPPORT_RPRAND_GENERATOR 1
#define RL_SUPPORT_RPRAND_GENERATOR 1
// Mouse gestures are directly mapped like touches and processed by gestures system
#define RL_SUPPORT_MOUSE_GESTURES 1
// Reconfigure standard input to receive key inputs, works with SSH connection.
@ -55,21 +55,21 @@
// However, it can also reduce overall system performance, because the thread scheduler switches tasks more often.
#define RL_SUPPORT_WINMM_HIGHRES_TIMER 1
// Use busy wait loop for timing sync, if not defined, a high-resolution timer is set up and used
//#define SUPPORT_BUSY_WAIT_LOOP 1
//#define RL_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 RL_SUPPORT_PARTIALBUSY_WAIT_LOOP 1
// Allow automatic screen capture of current screen pressing F12, defined in KeyCallback()
#define RL_SUPPORT_SCREEN_CAPTURE 1
// Allow automatic gif recording of current screen pressing CTRL+F12, defined in KeyCallback()
#define RL_SUPPORT_GIF_RECORDING 1
// Support RL_CompressData() and RL_DecompressData() functions
// Support CompressData() and DecompressData() functions
#define RL_SUPPORT_COMPRESSION_API 1
// Support automatic generated events, loading and recording of those events when required
#define RL_SUPPORT_AUTOMATION_EVENTS 1
// Support custom frame control, only for advance users
// By default RL_EndDrawing() does this job: draws everything + RL_SwapScreenBuffer() + manage frame timing + RL_PollInputEvents()
// By default end_drawing() does this job: draws everything + swap_screen_buffer() + manage frame timing + poll_input_events()
// Enabling this flag allows manual control of the frame processes, use at your own risk
//#define SUPPORT_CUSTOM_FRAME_CONTROL 1
//#define RL_SUPPORT_CUSTOM_FRAME_CONTROL 1
// rcore: Configuration values
//------------------------------------------------------------------------------------
@ -90,7 +90,7 @@
#define RL_MAX_AUTOMATION_EVENTS 16384 // Maximum number of automation events to record
//------------------------------------------------------------------------------------
// Module: RLGL_ - Configuration values
// Module: rlgl - Configuration values
//------------------------------------------------------------------------------------
// Enable OpenGL Debug Context (only available on OpenGL 4.3)
@ -102,9 +102,9 @@
//#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 (RL_SetShaderValueTexture())
#define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (set_shader_value_texture())
#define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal RL_Matrix stack
#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
@ -168,7 +168,7 @@
// Support procedural image generation functionality (gradient, spot, perlin-noise, cellular)
#define RL_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: RL_ImageFormat(), RL_ImageCrop(), RL_ImageToPOT()
// If not defined, still some functions are supported: image_format(), image_crop(), image_to_pot()
#define RL_SUPPORT_IMAGE_MANIPULATION 1
@ -183,18 +183,18 @@
#define RL_SUPPORT_FILEFORMAT_TTF 1
// Support text management functions
// If not defined, still some functions are supported: RL_TextLength(), RL_TextFormat()
// If not defined, still some functions are supported: text_length(), TextFormat()
#define RL_SUPPORT_TEXT_MANIPULATION 1
// On font atlas image generation [RL_GenImageFontAtlas()], add a 3x3 pixels white rectangle
// On font atlas image generation [gen_image_font_atlas()], add a 3x3 pixels white rectangle
// at the bottom-right corner of the atlas. It can be useful to for shapes drawing, to allow
// drawing text and shapes with a single draw call [RL_SetShapesTexture()].
// drawing text and shapes with a single draw call [set_shapes_texture()].
#define RL_SUPPORT_FONT_ATLAS_WHITE_REC 1
// rtext: Configuration values
//------------------------------------------------------------------------------------
#define RL_MAX_TEXT_BUFFER_LENGTH 1024 // RL_Size of internal static buffers used on some functions:
// RL_TextFormat(), RL_TextSubtext(), RL_TextToUpper(), RL_TextToLower(), RL_TextToPascal(), TextSplit()
#define RL_MAX_TEXT_BUFFER_LENGTH 1024 // Size of internal static buffers used on some functions:
// TextFormat(), TextSubtext(), TextToUpper(), TextToLower(), TextToPascal(), TextSplit()
#define RL_MAX_TEXTSPLIT_COUNT 128 // Maximum number of substrings to split: TextSplit()
@ -243,7 +243,7 @@
// Standard file io library (stdio.h) included
#define RL_SUPPORT_STANDARD_FILEIO 1
// Show RL_TRACELOG() output messages
// NOTE: By default RL_LOG_DEBUG traces not shown
// NOTE: By default LOG_DEBUG traces not shown
#define RL_SUPPORT_TRACELOG 1
//#define RL_SUPPORT_TRACELOG_DEBUG 1
@ -251,12 +251,35 @@
//------------------------------------------------------------------------------------
#define RL_MAX_TRACELOG_MSG_LENGTH 256 // Max length of one trace-log message
#endif // RL_CONFIG_H
#endif // CONFIG_H
#if defined(__cplusplus)
#define RL_NS_BEGIN namespace raylib {
// Indicates of raylib has been refactored
#ifndef RL_REFACTORED_CPP
#define RL_REFACTORED_CPP
#endif
#define RL_USE_CPP_NAMESPACE 1
#define RL_USE_CPP_MANGLING 1
#if RL_USE_CPP_NAMESPACE && defined(__cplusplus)
#pragma message("USING CPP NAMESPACE")
#define RL_NS_BEGIN namespace rl {
#define RL_NS_END }
#else
#define RL_NS_BEGIN
#define RL_NS_END
#endif
#if RL_USE_CPP_MANGLING && defined(__cplusplus)
#pragma message("USING CPP MANGLING")
#define RL_EXTERN_C_BEGIN
#define RL_EXTERN_C_END
#else
#ifdef __cplusplus
#define RL_EXTERN_C_BEGIN extern "C" {
#define RL_EXTERN_C_END }
#else
#define RL_EXTERN_C_BEGIN
#define RL_EXTERN_C_END
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@ -72,57 +72,59 @@
#define RL_CAMERA_CULL_DISTANCE_FAR RL_CULL_DISTANCE_FAR
#endif
RL_NS_BEGIN
//----------------------------------------------------------------------------------
// Types and Structures Definition
// NOTE: Below types are required for standalone usage
//----------------------------------------------------------------------------------
#if defined(RCAMERA_STANDALONE)
// RL_Vector2, 2 components
typedef struct RL_Vector2 {
// Vector2, 2 components
typedef struct Vector2 {
float x; // Vector x component
float y; // Vector y component
} RL_Vector2;
} Vector2;
// RL_Vector3, 3 components
typedef struct RL_Vector3 {
// Vector3, 3 components
typedef struct Vector3 {
float x; // Vector x component
float y; // Vector y component
float z; // Vector z component
} RL_Vector3;
} Vector3;
// RL_Matrix, 4x4 components, column major, OpenGL style, right-handed
typedef struct RL_Matrix {
float m0, m4, m8, m12; // RL_Matrix first row (4 components)
float m1, m5, m9, m13; // RL_Matrix second row (4 components)
float m2, m6, m10, m14; // RL_Matrix third row (4 components)
float m3, m7, m11, m15; // RL_Matrix fourth row (4 components)
} RL_Matrix;
// Matrix, 4x4 components, column major, OpenGL style, right-handed
typedef struct Matrix {
float m0, m4, m8, m12; // Matrix first row (4 components)
float m1, m5, m9, m13; // Matrix second row (4 components)
float m2, m6, m10, m14; // Matrix third row (4 components)
float m3, m7, m11, m15; // Matrix fourth row (4 components)
} Matrix;
// RL_Camera type, defines a camera position/orientation in 3d space
typedef struct RL_Camera3D {
RL_Vector3 position; // RL_Camera position
RL_Vector3 target; // RL_Camera target it looks-at
RL_Vector3 up; // RL_Camera up vector (rotation over its axis)
float fovy; // RL_Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic
int projection; // RL_Camera projection type: RL_CAMERA_PERSPECTIVE or RL_CAMERA_ORTHOGRAPHIC
} RL_Camera3D;
// 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 projection; // Camera projection type: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
} Camera3D;
typedef RL_Camera3D RL_Camera; // RL_Camera type fallback, defaults to RL_Camera3D
typedef Camera3D Camera; // Camera type fallback, defaults to Camera3D
// RL_Camera projection
// Camera projection
typedef enum {
RL_CAMERA_PERSPECTIVE = 0, // Perspective projection
RL_CAMERA_ORTHOGRAPHIC // Orthographic projection
} RL_CameraProjection;
CAMERA_PERSPECTIVE = 0, // Perspective projection
CAMERA_ORTHOGRAPHIC // Orthographic projection
} CameraProjection;
// RL_Camera system modes
// Camera system modes
typedef enum {
RL_CAMERA_CUSTOM = 0, // RL_Camera custom, controlled by user (RL_UpdateCamera() does nothing)
RL_CAMERA_FREE, // RL_Camera free mode
RL_CAMERA_ORBITAL, // RL_Camera orbital, around target, zoom supported
RL_CAMERA_FIRST_PERSON, // RL_Camera first person
RL_CAMERA_THIRD_PERSON // RL_Camera third person
} RL_CameraMode;
CAMERA_CUSTOM = 0, // Camera custom, controlled by user (update_camera() does nothing)
CAMERA_FREE, // Camera free mode
CAMERA_ORBITAL, // Camera orbital, around target, zoom supported
CAMERA_FIRST_PERSON, // Camera first person
CAMERA_THIRD_PERSON // Camera third person
} CameraMode;
#endif
//----------------------------------------------------------------------------------
@ -134,31 +136,29 @@
// Module Functions Declaration
//----------------------------------------------------------------------------------
#if defined(__cplusplus)
extern "C" { // Prevents name mangling of functions
#endif
RL_EXTERN_C_BEGIN
RLAPI RL_Vector3 GetCameraForward(RL_Camera *camera);
RLAPI RL_Vector3 GetCameraUp(RL_Camera *camera);
RLAPI RL_Vector3 GetCameraRight(RL_Camera *camera);
RLAPI Vector3 get_camera_forward(Camera *camera);
RLAPI Vector3 get_camera_up(Camera *camera);
RLAPI Vector3 get_camera_right(Camera *camera);
// RL_Camera movement
RLAPI void CameraMoveForward(RL_Camera *camera, float distance, bool moveInWorldPlane);
RLAPI void CameraMoveUp(RL_Camera *camera, float distance);
RLAPI void CameraMoveRight(RL_Camera *camera, float distance, bool moveInWorldPlane);
RLAPI void CameraMoveToTarget(RL_Camera *camera, float delta);
// Camera movement
RLAPI void camera_move_forward(Camera *camera, float distance, bool moveInWorldPlane);
RLAPI void camera_move_up(Camera *camera, float distance);
RLAPI void camera_move_right(Camera *camera, float distance, bool moveInWorldPlane);
RLAPI void camera_move_to_target(Camera *camera, float delta);
// RL_Camera rotation
RLAPI void CameraYaw(RL_Camera *camera, float angle, bool rotateAroundTarget);
RLAPI void CameraPitch(RL_Camera *camera, float angle, bool lockView, bool rotateAroundTarget, bool rotateUp);
RLAPI void CameraRoll(RL_Camera *camera, float angle);
// Camera rotation
RLAPI void camera_yaw(Camera *camera, float angle, bool rotateAroundTarget);
RLAPI void camera_pitch(Camera *camera, float angle, bool lockView, bool rotateAroundTarget, bool rotateUp);
RLAPI void camera_roll(Camera *camera, float angle);
RLAPI RL_Matrix GetCameraViewMatrix(RL_Camera *camera);
RLAPI RL_Matrix GetCameraProjectionMatrix(RL_Camera* camera, float aspect);
RLAPI Matrix get_camera_view_matrix(Camera *camera);
RLAPI Matrix get_camera_projection_matrix(Camera* camera, float aspect);
#if defined(__cplusplus)
}
#endif
RL_EXTERN_C_END
RL_NS_END
#endif // RCAMERA_H
@ -172,47 +172,47 @@ RLAPI RL_Matrix GetCameraProjectionMatrix(RL_Camera* camera, float aspect);
#if defined(RCAMERA_IMPLEMENTATION)
#include "raymath.h" // Required for vector maths:
// RL_Vector3Add()
// RL_Vector3Subtract()
// RL_Vector3Scale()
// RL_Vector3Normalize()
// RL_Vector3Distance()
// RL_Vector3CrossProduct()
// RL_Vector3RotateByAxisAngle()
// RL_Vector3Angle()
// RL_Vector3Negate()
// RL_MatrixLookAt()
// RL_MatrixPerspective()
// RL_MatrixOrtho()
// RL_MatrixIdentity()
// vector3_add()
// vector3_subtract()
// vector3_scale()
// vector3_normalize()
// vector3_distance()
// vector3_cross_product()
// vector3_rotate_by_axis_angle()
// vector3_angle()
// vector3_negate()
// matrix_look_at()
// matrix_perspective()
// matrix_ortho()
// matrix_identity()
// raylib required functionality:
// RL_GetMouseDelta()
// RL_GetMouseWheelMove()
// RL_IsKeyDown()
// RL_IsKeyPressed()
// RL_GetFrameTime()
// get_mouse_delta()
// get_mouse_wheel_move()
// is_key_down()
// is_key_pressed()
// get_frame_time()
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#define RL_CAMERA_MOVE_SPEED 0.09f
#define RL_CAMERA_ROTATION_SPEED 0.03f
#define RL_CAMERA_PAN_SPEED 0.2f
#define CAMERA_MOVE_SPEED 0.09f
#define CAMERA_ROTATION_SPEED 0.03f
#define CAMERA_PAN_SPEED 0.2f
// RL_Camera mouse movement sensitivity
#define RL_CAMERA_MOUSE_MOVE_SENSITIVITY 0.003f // TODO: it should be independant of framerate
#define RL_CAMERA_MOUSE_SCROLL_SENSITIVITY 1.5f
// Camera mouse movement sensitivity
#define CAMERA_MOUSE_MOVE_SENSITIVITY 0.003f // TODO: it should be independant of framerate
#define CAMERA_MOUSE_SCROLL_SENSITIVITY 1.5f
#define RL_CAMERA_ORBITAL_SPEED 0.5f // Radians per second
#define CAMERA_ORBITAL_SPEED 0.5f // Radians per second
#define RL_CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER 8.0f
#define RL_CAMERA_FIRST_PERSON_STEP_DIVIDER 30.0f
#define RL_CAMERA_FIRST_PERSON_WAVING_DIVIDER 200.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
// PLAYER (used by camera)
#define RL_PLAYER_MOVEMENT_SENSITIVITY 20.0f
#define PLAYER_MOVEMENT_SENSITIVITY 20.0f
//----------------------------------------------------------------------------------
// Types and Structures Definition
@ -229,88 +229,90 @@ RLAPI RL_Matrix GetCameraProjectionMatrix(RL_Camera* camera, float aspect);
//----------------------------------------------------------------------------------
//...
RL_NS_BEGIN
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Returns the cameras forward vector (normalized)
RL_Vector3 GetCameraForward(RL_Camera *camera)
Vector3 get_camera_forward(Camera *camera)
{
return RL_Vector3Normalize(RL_Vector3Subtract(camera->target, camera->position));
return vector3_normalize(vector3_subtract(camera->target, camera->position));
}
// Returns the cameras up vector (normalized)
// Note: The up vector might not be perpendicular to the forward vector
RL_Vector3 GetCameraUp(RL_Camera *camera)
Vector3 get_camera_up(Camera *camera)
{
return RL_Vector3Normalize(camera->up);
return vector3_normalize(camera->up);
}
// Returns the cameras right vector (normalized)
RL_Vector3 GetCameraRight(RL_Camera *camera)
Vector3 get_camera_right(Camera *camera)
{
RL_Vector3 forward = GetCameraForward(camera);
RL_Vector3 up = GetCameraUp(camera);
Vector3 forward = get_camera_forward(camera);
Vector3 up = get_camera_up(camera);
return RL_Vector3CrossProduct(forward, up);
return vector3_cross_product(forward, up);
}
// Moves the camera in its forward direction
void CameraMoveForward(RL_Camera *camera, float distance, bool moveInWorldPlane)
void camera_move_forward(Camera *camera, float distance, bool moveInWorldPlane)
{
RL_Vector3 forward = GetCameraForward(camera);
Vector3 forward = get_camera_forward(camera);
if (moveInWorldPlane)
{
// Project vector onto world plane
forward.y = 0;
forward = RL_Vector3Normalize(forward);
forward = vector3_normalize(forward);
}
// Scale by distance
forward = RL_Vector3Scale(forward, distance);
forward = vector3_scale(forward, distance);
// Move position and target
camera->position = RL_Vector3Add(camera->position, forward);
camera->target = RL_Vector3Add(camera->target, forward);
camera->position = vector3_add(camera->position, forward);
camera->target = vector3_add(camera->target, forward);
}
// Moves the camera in its up direction
void CameraMoveUp(RL_Camera *camera, float distance)
void camera_move_up(Camera *camera, float distance)
{
RL_Vector3 up = GetCameraUp(camera);
Vector3 up = get_camera_up(camera);
// Scale by distance
up = RL_Vector3Scale(up, distance);
up = vector3_scale(up, distance);
// Move position and target
camera->position = RL_Vector3Add(camera->position, up);
camera->target = RL_Vector3Add(camera->target, up);
camera->position = vector3_add(camera->position, up);
camera->target = vector3_add(camera->target, up);
}
// Moves the camera target in its current right direction
void CameraMoveRight(RL_Camera *camera, float distance, bool moveInWorldPlane)
void camera_move_right(Camera *camera, float distance, bool moveInWorldPlane)
{
RL_Vector3 right = GetCameraRight(camera);
Vector3 right = get_camera_right(camera);
if (moveInWorldPlane)
{
// Project vector onto world plane
right.y = 0;
right = RL_Vector3Normalize(right);
right = vector3_normalize(right);
}
// Scale by distance
right = RL_Vector3Scale(right, distance);
right = vector3_scale(right, distance);
// Move position and target
camera->position = RL_Vector3Add(camera->position, right);
camera->target = RL_Vector3Add(camera->target, right);
camera->position = vector3_add(camera->position, right);
camera->target = vector3_add(camera->target, right);
}
// Moves the camera position closer/farther to/from the camera target
void CameraMoveToTarget(RL_Camera *camera, float delta)
void camera_move_to_target(Camera *camera, float delta)
{
float distance = RL_Vector3Distance(camera->position, camera->target);
float distance = vector3_distance(camera->position, camera->target);
// Apply delta
distance += delta;
@ -319,213 +321,213 @@ void CameraMoveToTarget(RL_Camera *camera, float delta)
if (distance <= 0) distance = 0.001f;
// Set new distance by moving the position along the forward vector
RL_Vector3 forward = GetCameraForward(camera);
camera->position = RL_Vector3Add(camera->target, RL_Vector3Scale(forward, -distance));
Vector3 forward = get_camera_forward(camera);
camera->position = vector3_add(camera->target, vector3_scale(forward, -distance));
}
// Rotates the camera around its up vector
// Yaw is "looking left and right"
// If rotateAroundTarget is false, the camera rotates around its position
// Note: angle must be provided in radians
void CameraYaw(RL_Camera *camera, float angle, bool rotateAroundTarget)
void camera_yaw(Camera *camera, float angle, bool rotateAroundTarget)
{
// Rotation axis
RL_Vector3 up = GetCameraUp(camera);
Vector3 up = get_camera_up(camera);
// View vector
RL_Vector3 targetPosition = RL_Vector3Subtract(camera->target, camera->position);
Vector3 targetPosition = vector3_subtract(camera->target, camera->position);
// Rotate view vector around up axis
targetPosition = RL_Vector3RotateByAxisAngle(targetPosition, up, angle);
targetPosition = vector3_rotate_by_axis_angle(targetPosition, up, angle);
if (rotateAroundTarget)
{
// Move position relative to target
camera->position = RL_Vector3Subtract(camera->target, targetPosition);
camera->position = vector3_subtract(camera->target, targetPosition);
}
else // rotate around camera.position
{
// Move target relative to position
camera->target = RL_Vector3Add(camera->position, targetPosition);
camera->target = vector3_add(camera->position, targetPosition);
}
}
// Rotates the camera around its right vector, pitch is "looking up and down"
// - lockView prevents camera overrotation (aka "somersaults")
// - rotateAroundTarget defines if rotation is around target or around its position
// - rotateUp rotates the up direction as well (typically only usefull in RL_CAMERA_FREE)
// - rotateUp rotates the up direction as well (typically only usefull in CAMERA_FREE)
// NOTE: angle must be provided in radians
void CameraPitch(RL_Camera *camera, float angle, bool lockView, bool rotateAroundTarget, bool rotateUp)
void camera_pitch(Camera *camera, float angle, bool lockView, bool rotateAroundTarget, bool rotateUp)
{
// Up direction
RL_Vector3 up = GetCameraUp(camera);
Vector3 up = get_camera_up(camera);
// View vector
RL_Vector3 targetPosition = RL_Vector3Subtract(camera->target, camera->position);
Vector3 targetPosition = vector3_subtract(camera->target, camera->position);
if (lockView)
{
// In these camera modes we clamp the Pitch angle
// to allow only viewing straight up or down.
// RL_Clamp view up
float maxAngleUp = RL_Vector3Angle(up, targetPosition);
// clamp view up
float maxAngleUp = vector3_angle(up, targetPosition);
maxAngleUp -= 0.001f; // avoid numerical errors
if (angle > maxAngleUp) angle = maxAngleUp;
// RL_Clamp view down
float maxAngleDown = RL_Vector3Angle(RL_Vector3Negate(up), targetPosition);
// clamp view down
float maxAngleDown = vector3_angle(vector3_negate(up), targetPosition);
maxAngleDown *= -1.0f; // downwards angle is negative
maxAngleDown += 0.001f; // avoid numerical errors
if (angle < maxAngleDown) angle = maxAngleDown;
}
// Rotation axis
RL_Vector3 right = GetCameraRight(camera);
Vector3 right = get_camera_right(camera);
// Rotate view vector around right axis
targetPosition = RL_Vector3RotateByAxisAngle(targetPosition, right, angle);
targetPosition = vector3_rotate_by_axis_angle(targetPosition, right, angle);
if (rotateAroundTarget)
{
// Move position relative to target
camera->position = RL_Vector3Subtract(camera->target, targetPosition);
camera->position = vector3_subtract(camera->target, targetPosition);
}
else // rotate around camera.position
{
// Move target relative to position
camera->target = RL_Vector3Add(camera->position, targetPosition);
camera->target = vector3_add(camera->position, targetPosition);
}
if (rotateUp)
{
// Rotate up direction around right axis
camera->up = RL_Vector3RotateByAxisAngle(camera->up, right, angle);
camera->up = vector3_rotate_by_axis_angle(camera->up, right, angle);
}
}
// Rotates the camera around its forward vector
// Roll is "turning your head sideways to the left or right"
// Note: angle must be provided in radians
void CameraRoll(RL_Camera *camera, float angle)
void camera_roll(Camera *camera, float angle)
{
// Rotation axis
RL_Vector3 forward = GetCameraForward(camera);
Vector3 forward = get_camera_forward(camera);
// Rotate up direction around forward axis
camera->up = RL_Vector3RotateByAxisAngle(camera->up, forward, angle);
camera->up = vector3_rotate_by_axis_angle(camera->up, forward, angle);
}
// Returns the camera view matrix
RL_Matrix GetCameraViewMatrix(RL_Camera *camera)
Matrix get_camera_view_matrix(Camera *camera)
{
return RL_MatrixLookAt(camera->position, camera->target, camera->up);
return matrix_look_at(camera->position, camera->target, camera->up);
}
// Returns the camera projection matrix
RL_Matrix GetCameraProjectionMatrix(RL_Camera *camera, float aspect)
Matrix get_camera_projection_matrix(Camera *camera, float aspect)
{
if (camera->projection == RL_CAMERA_PERSPECTIVE)
if (camera->projection == CAMERA_PERSPECTIVE)
{
return RL_MatrixPerspective(camera->fovy*RL_DEG2RAD, aspect, RL_CAMERA_CULL_DISTANCE_NEAR, RL_CAMERA_CULL_DISTANCE_FAR);
return matrix_perspective(camera->fovy*RL_DEG2RAD, aspect, RL_CAMERA_CULL_DISTANCE_NEAR, RL_CAMERA_CULL_DISTANCE_FAR);
}
else if (camera->projection == RL_CAMERA_ORTHOGRAPHIC)
else if (camera->projection == CAMERA_ORTHOGRAPHIC)
{
double top = camera->fovy/2.0;
double right = top*aspect;
return RL_MatrixOrtho(-right, right, -top, top, RL_CAMERA_CULL_DISTANCE_NEAR, RL_CAMERA_CULL_DISTANCE_FAR);
return matrix_ortho(-right, right, -top, top, RL_CAMERA_CULL_DISTANCE_NEAR, RL_CAMERA_CULL_DISTANCE_FAR);
}
return RL_MatrixIdentity();
return matrix_identity();
}
#if !defined(RCAMERA_STANDALONE)
// Update camera position for selected mode
// RL_Camera mode: RL_CAMERA_FREE, RL_CAMERA_FIRST_PERSON, RL_CAMERA_THIRD_PERSON, RL_CAMERA_ORBITAL or CUSTOM
void RL_UpdateCamera(RL_Camera *camera, int mode)
// Camera mode: CAMERA_FREE, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON, CAMERA_ORBITAL or CUSTOM
void update_camera(Camera *camera, int mode)
{
RL_Vector2 mousePositionDelta = RL_GetMouseDelta();
Vector2 mousePositionDelta = get_mouse_delta();
bool moveInWorldPlane = ((mode == RL_CAMERA_FIRST_PERSON) || (mode == RL_CAMERA_THIRD_PERSON));
bool rotateAroundTarget = ((mode == RL_CAMERA_THIRD_PERSON) || (mode == RL_CAMERA_ORBITAL));
bool lockView = ((mode == RL_CAMERA_FIRST_PERSON) || (mode == RL_CAMERA_THIRD_PERSON) || (mode == RL_CAMERA_ORBITAL));
bool moveInWorldPlane = ((mode == CAMERA_FIRST_PERSON) || (mode == CAMERA_THIRD_PERSON));
bool rotateAroundTarget = ((mode == CAMERA_THIRD_PERSON) || (mode == CAMERA_ORBITAL));
bool lockView = ((mode == CAMERA_FIRST_PERSON) || (mode == CAMERA_THIRD_PERSON) || (mode == CAMERA_ORBITAL));
bool rotateUp = false;
if (mode == RL_CAMERA_ORBITAL)
if (mode == CAMERA_ORBITAL)
{
// Orbital can just orbit
RL_Matrix rotation = RL_MatrixRotate(GetCameraUp(camera), RL_CAMERA_ORBITAL_SPEED*RL_GetFrameTime());
RL_Vector3 view = RL_Vector3Subtract(camera->position, camera->target);
view = RL_Vector3Transform(view, rotation);
camera->position = RL_Vector3Add(camera->target, view);
Matrix rotation = matrix_rotate(get_camera_up(camera), CAMERA_ORBITAL_SPEED*get_frame_time());
Vector3 view = vector3_subtract(camera->position, camera->target);
view = vector3_transform(view, rotation);
camera->position = vector3_add(camera->target, view);
}
else
{
// RL_Camera rotation
if (RL_IsKeyDown(RL_KEY_DOWN)) CameraPitch(camera, -RL_CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp);
if (RL_IsKeyDown(RL_KEY_UP)) CameraPitch(camera, RL_CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp);
if (RL_IsKeyDown(RL_KEY_RIGHT)) CameraYaw(camera, -RL_CAMERA_ROTATION_SPEED, rotateAroundTarget);
if (RL_IsKeyDown(RL_KEY_LEFT)) CameraYaw(camera, RL_CAMERA_ROTATION_SPEED, rotateAroundTarget);
if (RL_IsKeyDown(RL_KEY_Q)) CameraRoll(camera, -RL_CAMERA_ROTATION_SPEED);
if (RL_IsKeyDown(RL_KEY_E)) CameraRoll(camera, RL_CAMERA_ROTATION_SPEED);
// Camera rotation
if (is_key_down(KEY_DOWN)) camera_pitch(camera, -CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp);
if (is_key_down(KEY_UP)) camera_pitch(camera, CAMERA_ROTATION_SPEED, lockView, rotateAroundTarget, rotateUp);
if (is_key_down(KEY_RIGHT)) camera_yaw(camera, -CAMERA_ROTATION_SPEED, rotateAroundTarget);
if (is_key_down(KEY_LEFT)) camera_yaw(camera, CAMERA_ROTATION_SPEED, rotateAroundTarget);
if (is_key_down(KEY_Q)) camera_roll(camera, -CAMERA_ROTATION_SPEED);
if (is_key_down(KEY_E)) camera_roll(camera, CAMERA_ROTATION_SPEED);
// RL_Camera movement
if (!RL_IsGamepadAvailable(0))
// Camera movement
if (!is_gamepad_available(0))
{
// RL_Camera pan (for RL_CAMERA_FREE)
if ((mode == RL_CAMERA_FREE) && (RL_IsMouseButtonDown(RL_MOUSE_BUTTON_MIDDLE)))
// Camera pan (for CAMERA_FREE)
if ((mode == CAMERA_FREE) && (is_mouse_button_down(MOUSE_BUTTON_MIDDLE)))
{
const RL_Vector2 mouseDelta = RL_GetMouseDelta();
if (mouseDelta.x > 0.0f) CameraMoveRight(camera, RL_CAMERA_PAN_SPEED, moveInWorldPlane);
if (mouseDelta.x < 0.0f) CameraMoveRight(camera, -RL_CAMERA_PAN_SPEED, moveInWorldPlane);
if (mouseDelta.y > 0.0f) CameraMoveUp(camera, -RL_CAMERA_PAN_SPEED);
if (mouseDelta.y < 0.0f) CameraMoveUp(camera, RL_CAMERA_PAN_SPEED);
const Vector2 mouseDelta = get_mouse_delta();
if (mouseDelta.x > 0.0f) camera_move_right(camera, CAMERA_PAN_SPEED, moveInWorldPlane);
if (mouseDelta.x < 0.0f) camera_move_right(camera, -CAMERA_PAN_SPEED, moveInWorldPlane);
if (mouseDelta.y > 0.0f) camera_move_up(camera, -CAMERA_PAN_SPEED);
if (mouseDelta.y < 0.0f) camera_move_up(camera, CAMERA_PAN_SPEED);
}
else
{
// Mouse support
CameraYaw(camera, -mousePositionDelta.x*RL_CAMERA_MOUSE_MOVE_SENSITIVITY, rotateAroundTarget);
CameraPitch(camera, -mousePositionDelta.y*RL_CAMERA_MOUSE_MOVE_SENSITIVITY, lockView, rotateAroundTarget, rotateUp);
camera_yaw(camera, -mousePositionDelta.x*CAMERA_MOUSE_MOVE_SENSITIVITY, rotateAroundTarget);
camera_pitch(camera, -mousePositionDelta.y*CAMERA_MOUSE_MOVE_SENSITIVITY, lockView, rotateAroundTarget, rotateUp);
}
// Keyboard support
if (RL_IsKeyDown(RL_KEY_W)) CameraMoveForward(camera, RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (RL_IsKeyDown(RL_KEY_A)) CameraMoveRight(camera, -RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (RL_IsKeyDown(RL_KEY_S)) CameraMoveForward(camera, -RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (RL_IsKeyDown(RL_KEY_D)) CameraMoveRight(camera, RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (is_key_down(KEY_W)) camera_move_forward(camera, CAMERA_MOVE_SPEED, moveInWorldPlane);
if (is_key_down(KEY_A)) camera_move_right(camera, -CAMERA_MOVE_SPEED, moveInWorldPlane);
if (is_key_down(KEY_S)) camera_move_forward(camera, -CAMERA_MOVE_SPEED, moveInWorldPlane);
if (is_key_down(KEY_D)) camera_move_right(camera, CAMERA_MOVE_SPEED, moveInWorldPlane);
}
else
{
// Gamepad controller support
CameraYaw(camera, -(RL_GetGamepadAxisMovement(0, RL_GAMEPAD_AXIS_RIGHT_X) * 2)*RL_CAMERA_MOUSE_MOVE_SENSITIVITY, rotateAroundTarget);
CameraPitch(camera, -(RL_GetGamepadAxisMovement(0, RL_GAMEPAD_AXIS_RIGHT_Y) * 2)*RL_CAMERA_MOUSE_MOVE_SENSITIVITY, lockView, rotateAroundTarget, rotateUp);
camera_yaw(camera, -(get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_X) * 2)*CAMERA_MOUSE_MOVE_SENSITIVITY, rotateAroundTarget);
camera_pitch(camera, -(get_gamepad_axis_movement(0, GAMEPAD_AXIS_RIGHT_Y) * 2)*CAMERA_MOUSE_MOVE_SENSITIVITY, lockView, rotateAroundTarget, rotateUp);
if (RL_GetGamepadAxisMovement(0, RL_GAMEPAD_AXIS_LEFT_Y) <= -0.25f) CameraMoveForward(camera, RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (RL_GetGamepadAxisMovement(0, RL_GAMEPAD_AXIS_LEFT_X) <= -0.25f) CameraMoveRight(camera, -RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (RL_GetGamepadAxisMovement(0, RL_GAMEPAD_AXIS_LEFT_Y) >= 0.25f) CameraMoveForward(camera, -RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (RL_GetGamepadAxisMovement(0, RL_GAMEPAD_AXIS_LEFT_X) >= 0.25f) CameraMoveRight(camera, RL_CAMERA_MOVE_SPEED, moveInWorldPlane);
if (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_Y) <= -0.25f) camera_move_forward(camera, CAMERA_MOVE_SPEED, moveInWorldPlane);
if (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_X) <= -0.25f) camera_move_right(camera, -CAMERA_MOVE_SPEED, moveInWorldPlane);
if (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_Y) >= 0.25f) camera_move_forward(camera, -CAMERA_MOVE_SPEED, moveInWorldPlane);
if (get_gamepad_axis_movement(0, GAMEPAD_AXIS_LEFT_X) >= 0.25f) camera_move_right(camera, CAMERA_MOVE_SPEED, moveInWorldPlane);
}
if (mode == RL_CAMERA_FREE)
if (mode == CAMERA_FREE)
{
if (RL_IsKeyDown(RL_KEY_SPACE)) CameraMoveUp(camera, RL_CAMERA_MOVE_SPEED);
if (RL_IsKeyDown(RL_KEY_LEFT_CONTROL)) CameraMoveUp(camera, -RL_CAMERA_MOVE_SPEED);
if (is_key_down(KEY_SPACE)) camera_move_up(camera, CAMERA_MOVE_SPEED);
if (is_key_down(KEY_LEFT_CONTROL)) camera_move_up(camera, -CAMERA_MOVE_SPEED);
}
}
if ((mode == RL_CAMERA_THIRD_PERSON) || (mode == RL_CAMERA_ORBITAL) || (mode == RL_CAMERA_FREE))
if ((mode == CAMERA_THIRD_PERSON) || (mode == CAMERA_ORBITAL) || (mode == CAMERA_FREE))
{
// Zoom target distance
CameraMoveToTarget(camera, -RL_GetMouseWheelMove());
if (RL_IsKeyPressed(RL_KEY_KP_SUBTRACT)) CameraMoveToTarget(camera, 2.0f);
if (RL_IsKeyPressed(RL_KEY_KP_ADD)) CameraMoveToTarget(camera, -2.0f);
camera_move_to_target(camera, -get_mouse_wheel_move());
if (is_key_pressed(KEY_KP_SUBTRACT)) camera_move_to_target(camera, 2.0f);
if (is_key_pressed(KEY_KP_ADD)) camera_move_to_target(camera, -2.0f);
}
}
#endif // !RCAMERA_STANDALONE
// Update camera movement, movement/rotation values should be provided by user
void RL_UpdateCameraPro(RL_Camera *camera, RL_Vector3 movement, RL_Vector3 rotation, float zoom)
void update_camera_pro(Camera *camera, Vector3 movement, Vector3 rotation, float zoom)
{
// Required values
// movement.x - Move forward/backward
@ -541,18 +543,20 @@ void RL_UpdateCameraPro(RL_Camera *camera, RL_Vector3 movement, RL_Vector3 rotat
bool rotateUp = false;
bool moveInWorldPlane = true;
// RL_Camera rotation
CameraPitch(camera, -rotation.y*RL_DEG2RAD, lockView, rotateAroundTarget, rotateUp);
CameraYaw(camera, -rotation.x*RL_DEG2RAD, rotateAroundTarget);
CameraRoll(camera, rotation.z*RL_DEG2RAD);
// Camera rotation
camera_pitch(camera, -rotation.y*RL_DEG2RAD, lockView, rotateAroundTarget, rotateUp);
camera_yaw(camera, -rotation.x*RL_DEG2RAD, rotateAroundTarget);
camera_roll(camera, rotation.z*RL_DEG2RAD);
// RL_Camera movement
CameraMoveForward(camera, movement.x, moveInWorldPlane);
CameraMoveRight(camera, movement.y, moveInWorldPlane);
CameraMoveUp(camera, movement.z);
// Camera movement
camera_move_forward(camera, movement.x, moveInWorldPlane);
camera_move_right(camera, movement.y, moveInWorldPlane);
camera_move_up(camera, movement.z);
// Zoom target distance
CameraMoveToTarget(camera, zoom);
camera_move_to_target(camera, zoom);
}
RL_NS_END
#endif // RCAMERA_IMPLEMENTATION

View File

@ -65,30 +65,34 @@
typedef enum bool { false = 0, true = !false } bool;
#endif
#include "config.h"
RL_NS_BEGIN
#if !defined(RL_VECTOR2_TYPE)
// RL_Vector2 type
typedef struct RL_Vector2 {
// Vector2 type
typedef struct Vector2 {
float x;
float y;
} RL_Vector2;
} Vector2;
#endif
#if defined(RGESTURES_STANDALONE)
// Gestures type
// NOTE: It could be used as flags to enable only some gestures
typedef enum {
RL_GESTURE_NONE = 0,
RL_GESTURE_TAP = 1,
RL_GESTURE_DOUBLETAP = 2,
RL_GESTURE_HOLD = 4,
RL_GESTURE_DRAG = 8,
RL_GESTURE_SWIPE_RIGHT = 16,
RL_GESTURE_SWIPE_LEFT = 32,
RL_GESTURE_SWIPE_UP = 64,
RL_GESTURE_SWIPE_DOWN = 128,
RL_GESTURE_PINCH_IN = 256,
RL_GESTURE_PINCH_OUT = 512
} RL_Gesture;
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 {
@ -96,15 +100,15 @@ typedef enum {
TOUCH_ACTION_DOWN,
TOUCH_ACTION_MOVE,
TOUCH_ACTION_CANCEL
} RL_TouchAction;
} TouchAction;
// RL_Gesture event
// Gesture event
typedef struct {
int touchAction;
int pointCount;
int pointId[RL_MAX_TOUCH_POINTS];
RL_Vector2 position[RL_MAX_TOUCH_POINTS];
} RL_GestureEvent;
Vector2 position[RL_MAX_TOUCH_POINTS];
} GestureEvent;
//----------------------------------------------------------------------------------
// Global Variables Definition
@ -115,28 +119,26 @@ typedef struct {
// Module Functions Declaration
//----------------------------------------------------------------------------------
#if defined(__cplusplus)
extern "C" { // Prevents name mangling of functions
#endif
RL_EXTERN_C_BEGIN
void RL_ProcessGestureEvent(RL_GestureEvent event); // Process gesture event and translate it into gestures
void RL_UpdateGestures(void); // Update gestures detected (must be called every frame)
void process_gesture_event(GestureEvent event); // Process gesture event and translate it into gestures
void update_gestures(void); // Update gestures detected (must be called every frame)
#if defined(RGESTURES_STANDALONE)
void RL_SetGesturesEnabled(unsigned int flags); // Enable a set of gestures using flags
bool RL_IsGestureDetected(int gesture); // Check if a gesture have been detected
int RL_GetGestureDetected(void); // Get latest detected gesture
void set_gestures_enabled(unsigned int flags); // Enable a set of gestures using flags
bool is_gesture_detected(int gesture); // Check if a gesture have been detected
int get_gesture_detected(void); // Get latest detected gesture
float RL_GetGestureHoldDuration(void); // Get gesture hold time in seconds
RL_Vector2 RL_GetGestureDragVector(void); // Get gesture drag vector
float RL_GetGestureDragAngle(void); // Get gesture drag angle
RL_Vector2 RL_GetGesturePinchVector(void); // Get gesture pinch delta
float RL_GetGesturePinchAngle(void); // Get gesture pinch angle
float get_gesture_hold_duration(void); // Get gesture hold time in seconds
Vector2 get_gesture_drag_vector(void); // Get gesture drag vector
float get_gesture_drag_angle(void); // Get gesture drag angle
Vector2 get_gesture_pinch_vector(void); // Get gesture pinch delta
float get_gesture_pinch_angle(void); // Get gesture pinch angle
#endif
#if defined(__cplusplus)
}
#endif
RL_EXTERN_C_END
RL_NS_END
#endif // RGESTURES_H
@ -154,8 +156,8 @@ float RL_GetGesturePinchAngle(void); // Get gesture pinch
extern "C" { // Prevents name mangling of functions
#endif
// Functions required to query time on Windows
int __stdcall RL_QueryPerformanceCounter(unsigned long long int *lpPerformanceCount);
int __stdcall RL_QueryPerformanceFrequency(unsigned long long int *lpFrequency);
int __stdcall query_performance_counter(unsigned long long int *lpPerformanceCount);
int __stdcall query_performance_frequency(unsigned long long int *lpFrequency);
#if defined(__cplusplus)
}
#endif
@ -186,6 +188,8 @@ float RL_GetGesturePinchAngle(void); // Get gesture pinch
#define RL_PINCH_TIMEOUT 0.3f // Pinch minimum time, measured in seconds
#define RL_DOUBLETAP_RANGE 0.03f // DoubleTap range, measured in normalized screen units (0.0f to 1.0f)
RL_NS_BEGIN
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
@ -198,14 +202,14 @@ typedef struct {
int firstId; // Touch id for first touch point
int pointCount; // Touch points counter
double eventTime; // Time stamp when an event happened
RL_Vector2 upPosition; // Touch up position
RL_Vector2 downPositionA; // First touch down position
RL_Vector2 downPositionB; // Second touch down position
RL_Vector2 downDragPosition; // Touch drag position
RL_Vector2 moveDownPositionA; // First touch down position on move
RL_Vector2 moveDownPositionB; // Second touch down position on move
RL_Vector2 previousPositionA; // Previous position A to compare for pinch gestures
RL_Vector2 previousPositionB; // Previous position B to compare for pinch gestures
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
Vector2 previousPositionA; // Previous position A to compare for pinch gestures
Vector2 previousPositionB; // Previous position B to compare for pinch gestures
int tapCounter; // TAP counter (one tap implies TOUCH_ACTION_DOWN and TOUCH_ACTION_UP actions)
} Touch;
struct {
@ -213,7 +217,7 @@ typedef struct {
double timeDuration; // HOLD duration in seconds
} Hold;
struct {
RL_Vector2 vector; // DRAG vector (between initial and current position)
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)
@ -222,269 +226,287 @@ typedef struct {
double startTime; // SWIPE start time to calculate drag intensity
} Swipe;
struct {
RL_Vector2 vector; // PINCH vector (between first and second touch points)
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;
} RL_GesturesData;
} GesturesData;
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static RL_GesturesData RL_GESTURES = {
static GesturesData GESTURES = {
#ifdef __cplusplus
(unsigned int)-1,
GESTURE_NONE,
0b0000001111111111
#else
.Touch.firstId = -1,
.current = RL_GESTURE_NONE, // No current gesture detected
.current = GESTURE_NONE, // No current gesture detected
.enabledFlags = 0b0000001111111111 // All gestures supported by default
#endif
};
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
static float rgVector2Angle(RL_Vector2 initialPosition, RL_Vector2 finalPosition);
static float rgVector2Distance(RL_Vector2 v1, RL_Vector2 v2);
static double rgGetCurrentTime(void);
static float rg_vector2_angle(Vector2 initialPosition, Vector2 finalPosition);
static float rg_vector2_distance(Vector2 v1, Vector2 v2);
static double rg_get_current_time(void);
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Enable only desired gestures to be detected
void RL_SetGesturesEnabled(unsigned int flags)
void set_gestures_enabled(unsigned int flags)
{
RL_GESTURES.enabledFlags = flags;
GESTURES.enabledFlags = flags;
}
// Check if a gesture have been detected
bool RL_IsGestureDetected(unsigned int gesture)
bool is_gesture_detected(unsigned int gesture)
{
if ((RL_GESTURES.enabledFlags & RL_GESTURES.current) == gesture) return true;
if ((GESTURES.enabledFlags & GESTURES.current) == gesture) return true;
else return false;
}
// Process gesture event and translate it into gestures
void RL_ProcessGestureEvent(RL_GestureEvent event)
void process_gesture_event(GestureEvent event)
{
// Reset required variables
RL_GESTURES.Touch.pointCount = event.pointCount; // Required on RL_UpdateGestures()
GESTURES.Touch.pointCount = event.pointCount; // Required on update_gestures()
if (RL_GESTURES.Touch.pointCount == 1) // One touch point
if (GESTURES.Touch.pointCount == 1) // One touch point
{
if (event.touchAction == TOUCH_ACTION_DOWN)
{
RL_GESTURES.Touch.tapCounter++; // Tap counter
GESTURES.Touch.tapCounter++; // Tap counter
// Detect GESTURE_DOUBLE_TAP
if ((RL_GESTURES.current == RL_GESTURE_NONE) && (RL_GESTURES.Touch.tapCounter >= 2) && ((rgGetCurrentTime() - RL_GESTURES.Touch.eventTime) < RL_TAP_TIMEOUT) && (rgVector2Distance(RL_GESTURES.Touch.downPositionA, event.position[0]) < RL_DOUBLETAP_RANGE))
if ((GESTURES.current == GESTURE_NONE) && (GESTURES.Touch.tapCounter >= 2) && ((rg_get_current_time() - GESTURES.Touch.eventTime) < RL_TAP_TIMEOUT) && (rg_vector2_distance(GESTURES.Touch.downPositionA, event.position[0]) < RL_DOUBLETAP_RANGE))
{
RL_GESTURES.current = RL_GESTURE_DOUBLETAP;
RL_GESTURES.Touch.tapCounter = 0;
GESTURES.current = GESTURE_DOUBLETAP;
GESTURES.Touch.tapCounter = 0;
}
else // Detect RL_GESTURE_TAP
else // Detect GESTURE_TAP
{
RL_GESTURES.Touch.tapCounter = 1;
RL_GESTURES.current = RL_GESTURE_TAP;
GESTURES.Touch.tapCounter = 1;
GESTURES.current = GESTURE_TAP;
}
RL_GESTURES.Touch.downPositionA = event.position[0];
RL_GESTURES.Touch.downDragPosition = event.position[0];
GESTURES.Touch.downPositionA = event.position[0];
GESTURES.Touch.downDragPosition = event.position[0];
RL_GESTURES.Touch.upPosition = RL_GESTURES.Touch.downPositionA;
RL_GESTURES.Touch.eventTime = rgGetCurrentTime();
GESTURES.Touch.upPosition = GESTURES.Touch.downPositionA;
GESTURES.Touch.eventTime = rg_get_current_time();
RL_GESTURES.Swipe.startTime = rgGetCurrentTime();
GESTURES.Swipe.startTime = rg_get_current_time();
RL_GESTURES.Drag.vector = (RL_Vector2){ 0.0f, 0.0f };
#ifdef __cplusplus
GESTURES.Drag.vector = Vector2{ 0.0f, 0.0f };
#else
GESTURES.Drag.vector = (Vector2){ 0.0f, 0.0f };
#endif
}
else if (event.touchAction == TOUCH_ACTION_UP)
{
// A swipe can happen while the current gesture is drag, but (specially for web) also hold, so set upPosition for both cases
if (RL_GESTURES.current == RL_GESTURE_DRAG || RL_GESTURES.current == RL_GESTURE_HOLD) RL_GESTURES.Touch.upPosition = event.position[0];
if (GESTURES.current == GESTURE_DRAG || GESTURES.current == GESTURE_HOLD) GESTURES.Touch.upPosition = event.position[0];
// NOTE: RL_GESTURES.Drag.intensity dependent on the resolution of the screen
RL_GESTURES.Drag.distance = rgVector2Distance(RL_GESTURES.Touch.downPositionA, RL_GESTURES.Touch.upPosition);
RL_GESTURES.Drag.intensity = RL_GESTURES.Drag.distance/(float)((rgGetCurrentTime() - RL_GESTURES.Swipe.startTime));
// NOTE: GESTURES.Drag.intensity dependent on the resolution of the screen
GESTURES.Drag.distance = rg_vector2_distance(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition);
GESTURES.Drag.intensity = GESTURES.Drag.distance/(float)((rg_get_current_time() - GESTURES.Swipe.startTime));
// Detect GESTURE_SWIPE
if ((RL_GESTURES.Drag.intensity > RL_FORCE_TO_SWIPE) && (RL_GESTURES.current != RL_GESTURE_DRAG))
if ((GESTURES.Drag.intensity > RL_FORCE_TO_SWIPE) && (GESTURES.current != GESTURE_DRAG))
{
// NOTE: Angle should be inverted in Y
RL_GESTURES.Drag.angle = 360.0f - rgVector2Angle(RL_GESTURES.Touch.downPositionA, RL_GESTURES.Touch.upPosition);
GESTURES.Drag.angle = 360.0f - rg_vector2_angle(GESTURES.Touch.downPositionA, GESTURES.Touch.upPosition);
if ((RL_GESTURES.Drag.angle < 30) || (RL_GESTURES.Drag.angle > 330)) RL_GESTURES.current = RL_GESTURE_SWIPE_RIGHT; // Right
else if ((RL_GESTURES.Drag.angle >= 30) && (RL_GESTURES.Drag.angle <= 150)) RL_GESTURES.current = RL_GESTURE_SWIPE_UP; // Up
else if ((RL_GESTURES.Drag.angle > 150) && (RL_GESTURES.Drag.angle < 210)) RL_GESTURES.current = RL_GESTURE_SWIPE_LEFT; // Left
else if ((RL_GESTURES.Drag.angle >= 210) && (RL_GESTURES.Drag.angle <= 330)) RL_GESTURES.current = RL_GESTURE_SWIPE_DOWN; // Down
else RL_GESTURES.current = RL_GESTURE_NONE;
if ((GESTURES.Drag.angle < 30) || (GESTURES.Drag.angle > 330)) GESTURES.current = GESTURE_SWIPE_RIGHT; // Right
else if ((GESTURES.Drag.angle >= 30) && (GESTURES.Drag.angle <= 150)) GESTURES.current = GESTURE_SWIPE_UP; // Up
else if ((GESTURES.Drag.angle > 150) && (GESTURES.Drag.angle < 210)) GESTURES.current = GESTURE_SWIPE_LEFT; // Left
else if ((GESTURES.Drag.angle >= 210) && (GESTURES.Drag.angle <= 330)) GESTURES.current = GESTURE_SWIPE_DOWN; // Down
else GESTURES.current = GESTURE_NONE;
}
else
{
RL_GESTURES.Drag.distance = 0.0f;
RL_GESTURES.Drag.intensity = 0.0f;
RL_GESTURES.Drag.angle = 0.0f;
GESTURES.Drag.distance = 0.0f;
GESTURES.Drag.intensity = 0.0f;
GESTURES.Drag.angle = 0.0f;
RL_GESTURES.current = RL_GESTURE_NONE;
GESTURES.current = GESTURE_NONE;
}
RL_GESTURES.Touch.downDragPosition = (RL_Vector2){ 0.0f, 0.0f };
RL_GESTURES.Touch.pointCount = 0;
#if __cplusplus
GESTURES.Touch.downDragPosition = Vector2{ 0.0f, 0.0f };
#else
GESTURES.Touch.downDragPosition = (Vector2){ 0.0f, 0.0f };
#endif
GESTURES.Touch.pointCount = 0;
}
else if (event.touchAction == TOUCH_ACTION_MOVE)
{
RL_GESTURES.Touch.moveDownPositionA = event.position[0];
GESTURES.Touch.moveDownPositionA = event.position[0];
if (RL_GESTURES.current == RL_GESTURE_HOLD)
if (GESTURES.current == GESTURE_HOLD)
{
if (RL_GESTURES.Hold.resetRequired) RL_GESTURES.Touch.downPositionA = event.position[0];
if (GESTURES.Hold.resetRequired) GESTURES.Touch.downPositionA = event.position[0];
RL_GESTURES.Hold.resetRequired = false;
GESTURES.Hold.resetRequired = false;
// Detect RL_GESTURE_DRAG
if ((rgGetCurrentTime() - RL_GESTURES.Touch.eventTime) > RL_DRAG_TIMEOUT)
// Detect GESTURE_DRAG
if ((rg_get_current_time() - GESTURES.Touch.eventTime) > RL_DRAG_TIMEOUT)
{
RL_GESTURES.Touch.eventTime = rgGetCurrentTime();
RL_GESTURES.current = RL_GESTURE_DRAG;
GESTURES.Touch.eventTime = rg_get_current_time();
GESTURES.current = GESTURE_DRAG;
}
}
RL_GESTURES.Drag.vector.x = RL_GESTURES.Touch.moveDownPositionA.x - RL_GESTURES.Touch.downDragPosition.x;
RL_GESTURES.Drag.vector.y = RL_GESTURES.Touch.moveDownPositionA.y - RL_GESTURES.Touch.downDragPosition.y;
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 (RL_GESTURES.Touch.pointCount == 2) // Two touch points
else if (GESTURES.Touch.pointCount == 2) // Two touch points
{
if (event.touchAction == TOUCH_ACTION_DOWN)
{
RL_GESTURES.Touch.downPositionA = event.position[0];
RL_GESTURES.Touch.downPositionB = event.position[1];
GESTURES.Touch.downPositionA = event.position[0];
GESTURES.Touch.downPositionB = event.position[1];
RL_GESTURES.Touch.previousPositionA = RL_GESTURES.Touch.downPositionA;
RL_GESTURES.Touch.previousPositionB = RL_GESTURES.Touch.downPositionB;
GESTURES.Touch.previousPositionA = GESTURES.Touch.downPositionA;
GESTURES.Touch.previousPositionB = GESTURES.Touch.downPositionB;
//RL_GESTURES.Pinch.distance = rgVector2Distance(RL_GESTURES.Touch.downPositionA, RL_GESTURES.Touch.downPositionB);
//GESTURES.Pinch.distance = rg_vector2_distance(GESTURES.Touch.downPositionA, GESTURES.Touch.downPositionB);
RL_GESTURES.Pinch.vector.x = RL_GESTURES.Touch.downPositionB.x - RL_GESTURES.Touch.downPositionA.x;
RL_GESTURES.Pinch.vector.y = RL_GESTURES.Touch.downPositionB.y - RL_GESTURES.Touch.downPositionA.y;
GESTURES.Pinch.vector.x = GESTURES.Touch.downPositionB.x - GESTURES.Touch.downPositionA.x;
GESTURES.Pinch.vector.y = GESTURES.Touch.downPositionB.y - GESTURES.Touch.downPositionA.y;
RL_GESTURES.current = RL_GESTURE_HOLD;
RL_GESTURES.Hold.timeDuration = rgGetCurrentTime();
GESTURES.current = GESTURE_HOLD;
GESTURES.Hold.timeDuration = rg_get_current_time();
}
else if (event.touchAction == TOUCH_ACTION_MOVE)
{
RL_GESTURES.Pinch.distance = rgVector2Distance(RL_GESTURES.Touch.moveDownPositionA, RL_GESTURES.Touch.moveDownPositionB);
GESTURES.Pinch.distance = rg_vector2_distance(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB);
RL_GESTURES.Touch.moveDownPositionA = event.position[0];
RL_GESTURES.Touch.moveDownPositionB = event.position[1];
GESTURES.Touch.moveDownPositionA = event.position[0];
GESTURES.Touch.moveDownPositionB = event.position[1];
RL_GESTURES.Pinch.vector.x = RL_GESTURES.Touch.moveDownPositionB.x - RL_GESTURES.Touch.moveDownPositionA.x;
RL_GESTURES.Pinch.vector.y = RL_GESTURES.Touch.moveDownPositionB.y - RL_GESTURES.Touch.moveDownPositionA.y;
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(RL_GESTURES.Touch.previousPositionA, RL_GESTURES.Touch.moveDownPositionA) >= RL_MINIMUM_PINCH) || (rgVector2Distance(RL_GESTURES.Touch.previousPositionB, RL_GESTURES.Touch.moveDownPositionB) >= RL_MINIMUM_PINCH))
if ((rg_vector2_distance(GESTURES.Touch.previousPositionA, GESTURES.Touch.moveDownPositionA) >= RL_MINIMUM_PINCH) || (rg_vector2_distance(GESTURES.Touch.previousPositionB, GESTURES.Touch.moveDownPositionB) >= RL_MINIMUM_PINCH))
{
if ( rgVector2Distance(RL_GESTURES.Touch.previousPositionA, RL_GESTURES.Touch.previousPositionB) > rgVector2Distance(RL_GESTURES.Touch.moveDownPositionA, RL_GESTURES.Touch.moveDownPositionB) ) RL_GESTURES.current = RL_GESTURE_PINCH_IN;
else RL_GESTURES.current = RL_GESTURE_PINCH_OUT;
if ( rg_vector2_distance(GESTURES.Touch.previousPositionA, GESTURES.Touch.previousPositionB) > rg_vector2_distance(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB) ) GESTURES.current = GESTURE_PINCH_IN;
else GESTURES.current = GESTURE_PINCH_OUT;
}
else
{
RL_GESTURES.current = RL_GESTURE_HOLD;
RL_GESTURES.Hold.timeDuration = rgGetCurrentTime();
GESTURES.current = GESTURE_HOLD;
GESTURES.Hold.timeDuration = rg_get_current_time();
}
// NOTE: Angle should be inverted in Y
RL_GESTURES.Pinch.angle = 360.0f - rgVector2Angle(RL_GESTURES.Touch.moveDownPositionA, RL_GESTURES.Touch.moveDownPositionB);
GESTURES.Pinch.angle = 360.0f - rg_vector2_angle(GESTURES.Touch.moveDownPositionA, GESTURES.Touch.moveDownPositionB);
}
else if (event.touchAction == TOUCH_ACTION_UP)
{
RL_GESTURES.Pinch.distance = 0.0f;
RL_GESTURES.Pinch.angle = 0.0f;
RL_GESTURES.Pinch.vector = (RL_Vector2){ 0.0f, 0.0f };
RL_GESTURES.Touch.pointCount = 0;
GESTURES.Pinch.distance = 0.0f;
GESTURES.Pinch.angle = 0.0f;
#if __cplusplus
GESTURES.Pinch.vector = Vector2{ 0.0f, 0.0f };
#else
GESTURES.Pinch.vector = (Vector2){ 0.0f, 0.0f };
#endif
GESTURES.Touch.pointCount = 0;
RL_GESTURES.current = RL_GESTURE_NONE;
GESTURES.current = GESTURE_NONE;
}
}
else if (RL_GESTURES.Touch.pointCount > 2) // More than two touch points
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 RL_UpdateGestures(void)
void update_gestures(void)
{
// NOTE: Gestures are processed through system callbacks on touch events
// Detect RL_GESTURE_HOLD
if (((RL_GESTURES.current == RL_GESTURE_TAP) || (RL_GESTURES.current == RL_GESTURE_DOUBLETAP)) && (RL_GESTURES.Touch.pointCount < 2))
// Detect GESTURE_HOLD
if (((GESTURES.current == GESTURE_TAP) || (GESTURES.current == GESTURE_DOUBLETAP)) && (GESTURES.Touch.pointCount < 2))
{
RL_GESTURES.current = RL_GESTURE_HOLD;
RL_GESTURES.Hold.timeDuration = rgGetCurrentTime();
GESTURES.current = GESTURE_HOLD;
GESTURES.Hold.timeDuration = rg_get_current_time();
}
// Detect RL_GESTURE_NONE
if ((RL_GESTURES.current == RL_GESTURE_SWIPE_RIGHT) || (RL_GESTURES.current == RL_GESTURE_SWIPE_UP) || (RL_GESTURES.current == RL_GESTURE_SWIPE_LEFT) || (RL_GESTURES.current == RL_GESTURE_SWIPE_DOWN))
// Detect GESTURE_NONE
if ((GESTURES.current == GESTURE_SWIPE_RIGHT) || (GESTURES.current == GESTURE_SWIPE_UP) || (GESTURES.current == GESTURE_SWIPE_LEFT) || (GESTURES.current == GESTURE_SWIPE_DOWN))
{
RL_GESTURES.current = RL_GESTURE_NONE;
GESTURES.current = GESTURE_NONE;
}
}
// Get latest detected gesture
int RL_GetGestureDetected(void)
int get_gesture_detected(void)
{
// Get current gesture only if enabled
return (RL_GESTURES.enabledFlags & RL_GESTURES.current);
return (GESTURES.enabledFlags & GESTURES.current);
}
// Hold time measured in ms
float RL_GetGestureHoldDuration(void)
float get_gesture_hold_duration(void)
{
// NOTE: time is calculated on current gesture HOLD
double time = 0.0;
if (RL_GESTURES.current == RL_GESTURE_HOLD) time = rgGetCurrentTime() - RL_GESTURES.Hold.timeDuration;
if (GESTURES.current == GESTURE_HOLD) time = rg_get_current_time() - GESTURES.Hold.timeDuration;
return (float)time;
}
// Get drag vector (between initial touch point to current)
RL_Vector2 RL_GetGestureDragVector(void)
Vector2 get_gesture_drag_vector(void)
{
// NOTE: drag vector is calculated on one touch points TOUCH_ACTION_MOVE
return RL_GESTURES.Drag.vector;
return GESTURES.Drag.vector;
}
// Get drag angle
// NOTE: Angle in degrees, horizontal-right is 0, counterclockwise
float RL_GetGestureDragAngle(void)
float get_gesture_drag_angle(void)
{
// NOTE: drag angle is calculated on one touch points TOUCH_ACTION_UP
return RL_GESTURES.Drag.angle;
return GESTURES.Drag.angle;
}
// Get distance between two pinch points
RL_Vector2 RL_GetGesturePinchVector(void)
Vector2 get_gesture_pinch_vector(void)
{
// NOTE: Pinch distance is calculated on two touch points TOUCH_ACTION_MOVE
return RL_GESTURES.Pinch.vector;
return GESTURES.Pinch.vector;
}
// Get angle between two pinch points
// NOTE: Angle in degrees, horizontal-right is 0, counterclockwise
float RL_GetGesturePinchAngle(void)
float get_gesture_pinch_angle(void)
{
// NOTE: pinch angle is calculated on two touch points TOUCH_ACTION_MOVE
return RL_GESTURES.Pinch.angle;
return GESTURES.Pinch.angle;
}
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
// Get angle from two-points vector with X-axis
static float rgVector2Angle(RL_Vector2 v1, RL_Vector2 v2)
static float rg_vector2_angle(Vector2 v1, Vector2 v2)
{
float angle = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/RL_PI);
@ -493,8 +515,8 @@ static float rgVector2Angle(RL_Vector2 v1, RL_Vector2 v2)
return angle;
}
// Calculate distance between two RL_Vector2
static float rgVector2Distance(RL_Vector2 v1, RL_Vector2 v2)
// Calculate distance between two Vector2
static float rg_vector2_distance(Vector2 v1, Vector2 v2)
{
float result;
@ -507,18 +529,18 @@ static float rgVector2Distance(RL_Vector2 v1, RL_Vector2 v2)
}
// Time measure returned are seconds
static double rgGetCurrentTime(void)
static double rg_get_current_time(void)
{
double time = 0;
#if !defined(RGESTURES_STANDALONE)
time = RL_GetTime();
time = get_time();
#else
#if defined(_WIN32)
unsigned long long int clockFrequency, currentTime;
RL_QueryPerformanceFrequency(&clockFrequency); // BE CAREFUL: Costly operation!
RL_QueryPerformanceCounter(&currentTime);
query_performance_frequency(&clockFrequency); // BE CAREFUL: Costly operation!
query_performance_counter(&currentTime);
time = (double)currentTime/clockFrequency; // Time in seconds
#endif
@ -552,4 +574,6 @@ static double rgGetCurrentTime(void)
return time;
}
RL_NS_END
#endif // RGESTURES_IMPLEMENTATION

File diff suppressed because it is too large Load Diff

View File

@ -33,10 +33,10 @@
#endif
#if defined(RL_SUPPORT_TRACELOG)
#define RL_TRACELOG(level, ...) RL_TraceLog(level, __VA_ARGS__)
#define RL_TRACELOG(level, ...) RL_NS(trace_log)(level, __VA_ARGS__)
#if defined(RL_SUPPORT_TRACELOG_DEBUG)
#define TRACELOGD(...) RL_TraceLog(RL_LOG_DEBUG, __VA_ARGS__)
#define TRACELOGD(...) RL_NS(trace_log)(LOG_DEBUG, __VA_ARGS__)
#else
#define TRACELOGD(...) (void)0
#endif
@ -70,7 +70,7 @@ 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
void init_asset_manager(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