Update C sources and examples
This commit is contained in:
parent
3b28d17b95
commit
8f1ad11c49
100 changed files with 2081 additions and 1686 deletions
199
raylib/core.c
199
raylib/core.c
|
@ -2,7 +2,14 @@
|
|||
*
|
||||
* raylib.core - Basic functions to manage windows, OpenGL context and input on multiple platforms
|
||||
*
|
||||
* The following platforms are supported: Windows, Linux, Mac (OSX), Android, Raspberry Pi, HTML5, Oculus Rift CV1
|
||||
* PLATFORMS SUPPORTED:
|
||||
* - Windows (win32/Win64)
|
||||
* - Linux (tested on Ubuntu)
|
||||
* - Mac (OSX)
|
||||
* - Android (API Level 9 or greater)
|
||||
* - Raspberry Pi (Raspbian)
|
||||
* - HTML5 (Chrome, Firefox)
|
||||
* - Oculus Rift CV1
|
||||
*
|
||||
* CONFIGURATION:
|
||||
*
|
||||
|
@ -22,13 +29,15 @@
|
|||
* Windowing and input system configured for HTML5 (run on browser), code converted from C to asm.js
|
||||
* using emscripten compiler. OpenGL ES 2.0 required for direct translation to WebGL equivalent code.
|
||||
*
|
||||
* #define LOAD_DEFAULT_FONT (defined by default)
|
||||
* #define SUPPORT_DEFAULT_FONT (default)
|
||||
* Default font is loaded on window initialization to be available for the user to render simple text.
|
||||
* NOTE: If enabled, uses external module functions to load default raylib font (module: text)
|
||||
*
|
||||
* #define INCLUDE_CAMERA_SYSTEM / SUPPORT_CAMERA_SYSTEM
|
||||
* #define SUPPORT_CAMERA_SYSTEM
|
||||
* Camera module is included (camera.h) and multiple predefined cameras are available: free, 1st/3rd person, orbital
|
||||
*
|
||||
* #define INCLUDE_GESTURES_SYSTEM / SUPPORT_GESTURES_SYSTEM
|
||||
* #define SUPPORT_GESTURES_SYSTEM
|
||||
* Gestures module is included (gestures.h) to support gestures detection: tap, hold, swipe, drag
|
||||
*
|
||||
* #define SUPPORT_MOUSE_GESTURES
|
||||
* Mouse gestures are directly mapped like touches and processed by gestures system.
|
||||
|
@ -42,7 +51,7 @@
|
|||
*
|
||||
* LICENSE: zlib/libpng
|
||||
*
|
||||
* Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2014-2017 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.
|
||||
|
@ -61,19 +70,29 @@
|
|||
*
|
||||
**********************************************************************************************/
|
||||
|
||||
// Default configuration flags (supported features)
|
||||
//-------------------------------------------------
|
||||
#define SUPPORT_DEFAULT_FONT
|
||||
#define SUPPORT_MOUSE_GESTURES
|
||||
#define SUPPORT_CAMERA_SYSTEM
|
||||
#define SUPPORT_GESTURES_SYSTEM
|
||||
//-------------------------------------------------
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "rlgl.h" // raylib OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2
|
||||
#include "utils.h" // Required for: fopen() Android mapping, TraceLog()
|
||||
#include "utils.h" // Required for: fopen() Android mapping
|
||||
|
||||
#define RAYMATH_IMPLEMENTATION // Use raymath as a header-only library (includes implementation)
|
||||
#define RAYMATH_EXTERN_INLINE // Compile raymath functions as static inline (remember, it's a compiler hint)
|
||||
#include "raymath.h" // Required for: Vector3 and Matrix functions
|
||||
|
||||
#define GESTURES_IMPLEMENTATION
|
||||
#include "gestures.h" // Gestures detection functionality
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM)
|
||||
#define GESTURES_IMPLEMENTATION
|
||||
#include "gestures.h" // Gestures detection functionality
|
||||
#endif
|
||||
|
||||
#if !defined(PLATFORM_ANDROID)
|
||||
#if defined(SUPPORT_CAMERA_SYSTEM) && !defined(PLATFORM_ANDROID)
|
||||
#define CAMERA_IMPLEMENTATION
|
||||
#include "camera.h" // Camera system functionality
|
||||
#endif
|
||||
|
@ -83,10 +102,10 @@
|
|||
#include <stdint.h> // Required for: typedef unsigned long long int uint64_t, used by hi-res timer
|
||||
#include <time.h> // Required for: time() - Android/RPI hi-res timer (NOTE: Linux only!)
|
||||
#include <math.h> // Required for: tan() [Used in Begin3dMode() to set perspective]
|
||||
#include <string.h> // Required for: strcmp()
|
||||
#include <string.h> // Required for: strrchr(), strcmp()
|
||||
//#include <errno.h> // Macros for reporting and retrieving error conditions through error codes
|
||||
|
||||
#if defined __linux || defined(PLATFORM_WEB)
|
||||
#if defined __linux__ || defined(PLATFORM_WEB)
|
||||
#include <sys/time.h> // Required for: timespec, nanosleep(), select() - POSIX
|
||||
#elif defined __APPLE__
|
||||
#include <unistd.h> // Required for: usleep()
|
||||
|
@ -96,7 +115,7 @@
|
|||
//#define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3
|
||||
#include <GLFW/glfw3.h> // GLFW3 library: Windows, OpenGL context and Input management
|
||||
|
||||
#ifdef __linux
|
||||
#ifdef __linux__
|
||||
#define GLFW_EXPOSE_NATIVE_X11 // Linux specific definitions for getting
|
||||
#define GLFW_EXPOSE_NATIVE_GLX // native functions like glfwGetX11Window
|
||||
#include <GLFW/glfw3native.h> // which are required for hiding mouse
|
||||
|
@ -140,8 +159,6 @@
|
|||
//----------------------------------------------------------------------------------
|
||||
// Defines and Macros
|
||||
//----------------------------------------------------------------------------------
|
||||
#define STORAGE_FILENAME "storage.data"
|
||||
|
||||
#if defined(PLATFORM_RPI)
|
||||
// Old device inputs system
|
||||
#define DEFAULT_KEYBOARD_DEV STDIN_FILENO // Standard input
|
||||
|
@ -161,7 +178,7 @@
|
|||
#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad)
|
||||
#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad)
|
||||
|
||||
#define LOAD_DEFAULT_FONT // Load default font on window initialization (module: text)
|
||||
#define STORAGE_FILENAME "storage.data"
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
|
@ -259,7 +276,10 @@ static int lastGamepadButtonPressed = -1; // Register last gamepad button pres
|
|||
static int gamepadAxisCount = 0; // Register number of available gamepad axis
|
||||
|
||||
static Vector2 mousePosition; // Mouse position on screen
|
||||
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM)
|
||||
static Vector2 touchPosition[MAX_TOUCH_POINTS]; // Touch position on screen
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
static char **dropFilesPath; // Store dropped files paths as strings
|
||||
|
@ -277,7 +297,7 @@ static bool showLogo = false; // Track if showing logo at init is
|
|||
//----------------------------------------------------------------------------------
|
||||
// Other Modules Functions Declaration (required by core)
|
||||
//----------------------------------------------------------------------------------
|
||||
#if defined(LOAD_DEFAULT_FONT)
|
||||
#if defined(SUPPORT_DEFAULT_FONT)
|
||||
extern void LoadDefaultFont(void); // [Module: text] Loads default font on InitWindow()
|
||||
extern void UnloadDefaultFont(void); // [Module: text] Unloads default font from GPU memory
|
||||
#endif
|
||||
|
@ -296,9 +316,6 @@ static void PollInputEvents(void); // Register user events
|
|||
static void SwapBuffers(void); // Copy back buffer to front buffers
|
||||
static void LogoAnimation(void); // Plays raylib logo appearing animation
|
||||
static void SetupViewport(void); // Set viewport parameters
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
|
||||
static void TakeScreenshot(void); // Takes a screenshot and saves it in the same folder as executable
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||
static void ErrorCallback(int error, const char *description); // GLFW3 Error Callback, runs on GLFW3 error
|
||||
|
@ -323,7 +340,9 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event)
|
|||
|
||||
#if defined(PLATFORM_WEB)
|
||||
static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *e, void *userData);
|
||||
static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData);
|
||||
static EM_BOOL EmscriptenKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData);
|
||||
static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
|
||||
static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData);
|
||||
static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData);
|
||||
#endif
|
||||
|
||||
|
@ -359,7 +378,7 @@ void InitWindow(int width, int height, const char *title)
|
|||
// Init graphics device (display device and OpenGL context)
|
||||
InitGraphicsDevice(width, height);
|
||||
|
||||
#if defined(LOAD_DEFAULT_FONT)
|
||||
#if defined(SUPPORT_DEFAULT_FONT)
|
||||
// Load default font
|
||||
// NOTE: External function (defined in module: text)
|
||||
LoadDefaultFont();
|
||||
|
@ -378,16 +397,22 @@ void InitWindow(int width, int height, const char *title)
|
|||
|
||||
#if defined(PLATFORM_WEB)
|
||||
emscripten_set_fullscreenchange_callback(0, 0, 1, EmscriptenFullscreenChangeCallback);
|
||||
|
||||
// Support keyboard events
|
||||
emscripten_set_keypress_callback("#canvas", NULL, 1, EmscriptenKeyboardCallback);
|
||||
|
||||
// Support mouse events
|
||||
emscripten_set_click_callback("#canvas", NULL, 1, EmscriptenMouseCallback);
|
||||
|
||||
// NOTE: Some code examples
|
||||
// Support touch events
|
||||
emscripten_set_touchstart_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
||||
emscripten_set_touchend_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
||||
emscripten_set_touchmove_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
||||
emscripten_set_touchcancel_callback("#canvas", NULL, 1, EmscriptenTouchCallback);
|
||||
//emscripten_set_touchstart_callback(0, NULL, 1, Emscripten_HandleTouch);
|
||||
//emscripten_set_touchend_callback("#canvas", data, 0, Emscripten_HandleTouch);
|
||||
emscripten_set_touchstart_callback("#canvas", NULL, 1, EmscriptenInputCallback);
|
||||
emscripten_set_touchend_callback("#canvas", NULL, 1, EmscriptenInputCallback);
|
||||
emscripten_set_touchmove_callback("#canvas", NULL, 1, EmscriptenInputCallback);
|
||||
emscripten_set_touchcancel_callback("#canvas", NULL, 1, EmscriptenInputCallback);
|
||||
|
||||
// Support gamepad (not provided by GLFW3 on emscripten)
|
||||
// Support gamepad events (not provided by GLFW3 on emscripten)
|
||||
emscripten_set_gamepadconnected_callback(NULL, 1, EmscriptenGamepadCallback);
|
||||
emscripten_set_gamepaddisconnected_callback(NULL, 1, EmscriptenGamepadCallback);
|
||||
#endif
|
||||
|
@ -471,7 +496,7 @@ void InitWindow(int width, int height, void *state)
|
|||
// Close Window and Terminate Context
|
||||
void CloseWindow(void)
|
||||
{
|
||||
#if defined(LOAD_DEFAULT_FONT)
|
||||
#if defined(SUPPORT_DEFAULT_FONT)
|
||||
UnloadDefaultFont();
|
||||
#endif
|
||||
|
||||
|
@ -621,7 +646,7 @@ int GetScreenHeight(void)
|
|||
void ShowCursor()
|
||||
{
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
#ifdef __linux
|
||||
#ifdef __linux__
|
||||
XUndefineCursor(glfwGetX11Display(), glfwGetX11Window(window));
|
||||
#else
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
||||
|
@ -634,7 +659,7 @@ void ShowCursor()
|
|||
void HideCursor()
|
||||
{
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
#ifdef __linux
|
||||
#ifdef __linux__
|
||||
XColor col;
|
||||
const char nil[] = {0};
|
||||
|
||||
|
@ -958,6 +983,12 @@ Color Fade(Color color, float alpha)
|
|||
return (Color){color.r, color.g, color.b, (unsigned char)colorAlpha};
|
||||
}
|
||||
|
||||
// Activates raylib logo at startup
|
||||
void ShowLogo(void)
|
||||
{
|
||||
showLogo = true;
|
||||
}
|
||||
|
||||
// Enable some window/system configurations
|
||||
void SetConfigFlags(char flags)
|
||||
{
|
||||
|
@ -967,10 +998,40 @@ void SetConfigFlags(char flags)
|
|||
if (configFlags & FLAG_FULLSCREEN_MODE) fullscreen = true;
|
||||
}
|
||||
|
||||
// Activates raylib logo at startup
|
||||
void ShowLogo(void)
|
||||
// Takes a screenshot and saves it in the same folder as executable
|
||||
void TakeScreenshot(void)
|
||||
{
|
||||
showLogo = true;
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
|
||||
static int shotNum = 0; // Screenshot number, increments every screenshot take during program execution
|
||||
char buffer[20]; // Buffer to store file name
|
||||
|
||||
unsigned char *imgData = rlglReadScreenPixels(renderWidth, renderHeight);
|
||||
|
||||
sprintf(buffer, "screenshot%03i.png", shotNum);
|
||||
|
||||
// Save image as PNG
|
||||
SavePNG(buffer, imgData, renderWidth, renderHeight, 4);
|
||||
|
||||
free(imgData);
|
||||
|
||||
shotNum++;
|
||||
|
||||
TraceLog(INFO, "[%s] Screenshot taken #03i", buffer, shotNum);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Check file extension
|
||||
bool IsFileExtension(const char *fileName, const char *ext)
|
||||
{
|
||||
bool result = false;
|
||||
const char *fileExt;
|
||||
|
||||
if ((fileExt = strrchr(fileName, '.')) != NULL)
|
||||
{
|
||||
if (strcmp(fileExt, ext) == 0) result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
|
@ -2004,6 +2065,9 @@ static double GetTime(void)
|
|||
}
|
||||
|
||||
// Wait for some milliseconds (stop program execution)
|
||||
// NOTE: Sleep() granularity could be around 10 ms, it means, Sleep() could
|
||||
// take longer than expected... for that reason we use the busy wait loop
|
||||
// http://stackoverflow.com/questions/43057578/c-programming-win32-games-sleep-taking-longer-than-expected
|
||||
static void Wait(float ms)
|
||||
{
|
||||
//#define SUPPORT_BUSY_WAIT_LOOP
|
||||
|
@ -2015,8 +2079,8 @@ static void Wait(float ms)
|
|||
while ((nextTime - prevTime) < ms/1000.0f) nextTime = GetTime();
|
||||
#else
|
||||
#if defined _WIN32
|
||||
Sleep(ms);
|
||||
#elif defined __linux || defined(PLATFORM_WEB)
|
||||
Sleep((unsigned int)ms);
|
||||
#elif defined __linux__ || defined(PLATFORM_WEB)
|
||||
struct timespec req = { 0 };
|
||||
time_t sec = (int)(ms/1000.0f);
|
||||
ms -= (sec*1000);
|
||||
|
@ -2064,9 +2128,11 @@ static bool GetMouseButtonStatus(int button)
|
|||
// Poll (store) all input events
|
||||
static void PollInputEvents(void)
|
||||
{
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM)
|
||||
// NOTE: Gestures update must be called every frame to reset gestures correctly
|
||||
// because ProcessGestureEvent() is just called on an event, not every frame
|
||||
UpdateGestures();
|
||||
#endif
|
||||
|
||||
// Reset last key pressed registered
|
||||
lastKeyPressed = -1;
|
||||
|
@ -2237,28 +2303,6 @@ static void SwapBuffers(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_RPI)
|
||||
// Takes a screenshot and saves it in the same folder as executable
|
||||
static void TakeScreenshot(void)
|
||||
{
|
||||
static int shotNum = 0; // Screenshot number, increments every screenshot take during program execution
|
||||
char buffer[20]; // Buffer to store file name
|
||||
|
||||
unsigned char *imgData = rlglReadScreenPixels(renderWidth, renderHeight);
|
||||
|
||||
sprintf(buffer, "screenshot%03i.png", shotNum);
|
||||
|
||||
// Save image as PNG
|
||||
SavePNG(buffer, imgData, renderWidth, renderHeight, 4);
|
||||
|
||||
free(imgData);
|
||||
|
||||
shotNum++;
|
||||
|
||||
TraceLog(INFO, "[%s] Screenshot taken!", buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB)
|
||||
// GLFW3 Error Callback, runs on GLFW3 error
|
||||
static void ErrorCallback(int error, const char *description)
|
||||
|
@ -2296,8 +2340,7 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
|
|||
{
|
||||
currentMouseState[button] = action;
|
||||
|
||||
#define ENABLE_MOUSE_GESTURES
|
||||
#if defined(ENABLE_MOUSE_GESTURES)
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
|
||||
// Process mouse events as touches to be able to use mouse-gestures
|
||||
GestureEvent gestureEvent;
|
||||
|
||||
|
@ -2328,8 +2371,7 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
|
|||
// GLFW3 Cursor Position Callback, runs on mouse move
|
||||
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
|
||||
{
|
||||
#define ENABLE_MOUSE_GESTURES
|
||||
#if defined(ENABLE_MOUSE_GESTURES)
|
||||
#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
|
||||
// Process mouse events as touches to be able to use mouse-gestures
|
||||
GestureEvent gestureEvent;
|
||||
|
||||
|
@ -2459,7 +2501,7 @@ static void AndroidCommandCallback(struct android_app *app, int32_t cmd)
|
|||
// Init graphics device (display device and OpenGL context)
|
||||
InitGraphicsDevice(screenWidth, screenHeight);
|
||||
|
||||
#if defined(LOAD_DEFAULT_FONT)
|
||||
#if defined(SUPPORT_DEFAULT_FONT)
|
||||
// Load default font
|
||||
// NOTE: External function (defined in module: text)
|
||||
LoadDefaultFont();
|
||||
|
@ -2663,8 +2705,39 @@ static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const Emscripte
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Register keyboard input events
|
||||
static EM_BOOL EmscriptenKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData)
|
||||
{
|
||||
if ((eventType == EMSCRIPTEN_EVENT_KEYPRESS) && (strcmp(keyEvent->code, "Escape") == 0))
|
||||
{
|
||||
emscripten_exit_pointerlock();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Register mouse input events
|
||||
static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
|
||||
{
|
||||
if (eventType == EMSCRIPTEN_EVENT_CLICK)
|
||||
{
|
||||
EmscriptenPointerlockChangeEvent plce;
|
||||
emscripten_get_pointerlock_status(&plce);
|
||||
|
||||
if (!plce.isActive) emscripten_request_pointerlock(0, 1);
|
||||
else
|
||||
{
|
||||
emscripten_exit_pointerlock();
|
||||
emscripten_get_pointerlock_status(&plce);
|
||||
//if (plce.isActive) TraceLog(WARNING, "Pointer lock exit did not work!");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Register touch input events
|
||||
static EM_BOOL EmscriptenInputCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
|
||||
static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData)
|
||||
{
|
||||
/*
|
||||
for (int i = 0; i < touchEvent->numTouches; i++)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue