aboutsummaryrefslogtreecommitdiff
path: root/examples/core_gestures_detection.c
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2016-02-11 12:26:45 +0100
committerraysan5 <raysan5@gmail.com>2016-02-11 12:26:45 +0100
commit54c7fa491ef285838a64a8ee015de888c3d24a75 (patch)
treed2e192c9485a05793e072145719bfd4d8d47b68c /examples/core_gestures_detection.c
parentc69ce1d7504b94108721403c867bddbf50d7b890 (diff)
downloadraylib-54c7fa491ef285838a64a8ee015de888c3d24a75.tar.gz
raylib-54c7fa491ef285838a64a8ee015de888c3d24a75.zip
Added 6 new examples
Diffstat (limited to 'examples/core_gestures_detection.c')
-rw-r--r--examples/core_gestures_detection.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/examples/core_gestures_detection.c b/examples/core_gestures_detection.c
new file mode 100644
index 00000000..b69497c5
--- /dev/null
+++ b/examples/core_gestures_detection.c
@@ -0,0 +1,115 @@
+/*******************************************************************************************
+*
+* raylib [core] example - Gestures Detection
+*
+* This example has been created using raylib 1.4 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2016 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include <string.h>
+
+#define MAX_GESTURE_STRINGS 20
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [core] example - gestures detection");
+
+ Vector2 touchPosition = { 0, 0 };
+ Rectangle touchArea = { 220, 10, screenWidth - 230, screenHeight - 20 };
+
+ int gesturesCount = 0;
+ char gestureStrings[MAX_GESTURE_STRINGS][32];
+
+ int currentGesture = GESTURE_NONE;
+ int lastGesture = GESTURE_NONE;
+
+ //SetGesturesEnabled(0b0000000000001001); // Enable only some gestures to be detected
+
+ SetTargetFPS(30);
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ lastGesture = currentGesture;
+ touchPosition = GetTouchPosition(0);
+
+ if (CheckCollisionPointRec(touchPosition, touchArea) && IsGestureDetected())
+ {
+ currentGesture = GetGestureType();
+
+ if (currentGesture != lastGesture)
+ {
+ // Store gesture string
+ switch (currentGesture)
+ {
+ case GESTURE_TAP: strcpy(gestureStrings[gesturesCount], "GESTURE TAP"); break;
+ case GESTURE_DOUBLETAP: strcpy(gestureStrings[gesturesCount], "GESTURE DOUBLETAP"); break;
+ case GESTURE_HOLD: strcpy(gestureStrings[gesturesCount], "GESTURE HOLD"); break;
+ case GESTURE_DRAG: strcpy(gestureStrings[gesturesCount], "GESTURE DRAG"); break;
+ case GESTURE_SWIPE_RIGHT: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE RIGHT"); break;
+ case GESTURE_SWIPE_LEFT: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE LEFT"); break;
+ case GESTURE_SWIPE_UP: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE UP"); break;
+ case GESTURE_SWIPE_DOWN: strcpy(gestureStrings[gesturesCount], "GESTURE SWIPE DOWN"); break;
+ default: break;
+ }
+
+ gesturesCount++;
+
+ // Reset gestures strings
+ if (gesturesCount >= MAX_GESTURE_STRINGS)
+ {
+ for (int i = 0; i < MAX_GESTURE_STRINGS; i++) strcpy(gestureStrings[i], "\0");
+
+ gesturesCount = 0;
+ }
+ }
+ }
+ else currentGesture = GESTURE_NONE;
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ DrawRectangleRec(touchArea, GRAY);
+ DrawRectangle(225, 15, screenWidth - 240, screenHeight - 30, RAYWHITE);
+
+ DrawText("GESTURES TEST AREA", screenWidth - 270, screenHeight - 40, 20, Fade(GRAY, 0.5f));
+
+ for (int i = 0; i < gesturesCount; i++)
+ {
+ if (i%2 == 0) DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.5f));
+ else DrawRectangle(10, 30 + 20*i, 200, 20, Fade(LIGHTGRAY, 0.3f));
+
+ if (i < gesturesCount - 1) DrawText(gestureStrings[i], 35, 36 + 20*i, 10, DARKGRAY);
+ else DrawText(gestureStrings[i], 35, 36 + 20*i, 10, MAROON);
+ }
+
+ DrawRectangleLines(10, 29, 200, screenHeight - 50, GRAY);
+ DrawText("DETECTED GESTURES", 50, 15, 10, GRAY);
+
+ if (currentGesture != GESTURE_NONE) DrawCircleV(touchPosition, 30, MAROON);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+} \ No newline at end of file