From d2d4b17633d623999eb2509ff24f741b5d669b35 Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 8 Apr 2017 00:16:03 +0200 Subject: Web: Support pointer lock --- src/core.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) (limited to 'src/core.c') diff --git a/src/core.c b/src/core.c index b9ea9315..c73a9418 100644 --- a/src/core.c +++ b/src/core.c @@ -343,7 +343,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 @@ -398,16 +400,22 @@ void InitWindow(int width, int height, const char *title) #if defined(PLATFORM_WEB) emscripten_set_fullscreenchange_callback(0, 0, 1, EmscriptenFullscreenChangeCallback); - - // NOTE: Some code examples + + // Support keyboard events + emscripten_set_keypress_callback("#canvas", NULL, 1, EmscriptenKeyboardCallback); + + // Support mouse events + emscripten_set_click_callback("#canvas", NULL, 1, EmscriptenMouseCallback); + + // Support touch events + emscripten_set_touchstart_callback("#canvas", NULL, 1, EmscriptenTouchCallback); + emscripten_set_touchend_callback("#canvas", NULL, 1, EmscriptenTouchCallback); + emscripten_set_touchmove_callback("#canvas", NULL, 1, EmscriptenTouchCallback); + emscripten_set_touchcancel_callback("#canvas", NULL, 1, EmscriptenTouchCallback); //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 @@ -2700,8 +2708,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++) -- cgit v1.2.3 From 7e65c300b6927bff3fb9cfa84e98fd662941fc20 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 16 Apr 2017 13:47:49 +0200 Subject: Make public TakeScreenshot() function --- src/core.c | 47 ++++++++++++++++++++++------------------------- src/raylib.h | 1 + 2 files changed, 23 insertions(+), 25 deletions(-) (limited to 'src/core.c') diff --git a/src/core.c b/src/core.c index c73a9418..3f3bc6ea 100644 --- a/src/core.c +++ b/src/core.c @@ -316,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 @@ -1001,6 +998,28 @@ void SetConfigFlags(char flags) if (configFlags & FLAG_FULLSCREEN_MODE) fullscreen = true; } +// Takes a screenshot and saves it in the same folder as executable +void TakeScreenshot(void) +{ +#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) { @@ -2284,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) diff --git a/src/raylib.h b/src/raylib.h index e8f301ec..b82ec342 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -690,6 +690,7 @@ RLAPI Color Fade(Color color, float alpha); // Color fade- RLAPI void ShowLogo(void); // Activates raylib logo at startup (can be done with flags) RLAPI void SetConfigFlags(char flags); // Setup some window configuration flags //RLAPI void TraceLog(int logType, const char *text, ...); // Show trace log messages (INFO, WARNING, ERROR, DEBUG) +RLAPI void TakeScreenshot(void); // Takes a screenshot and saves it in the same folder as executable RLAPI bool IsFileExtension(const char *fileName, const char *ext);// Check file extension RLAPI bool IsFileDropped(void); // Check if a file have been dropped into window -- cgit v1.2.3