Skip to content

Instantly share code, notes, and snippets.

@grrussel
Created July 2, 2017 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grrussel/a9571543408ec1581177e2aed38bb126 to your computer and use it in GitHub Desktop.
Save grrussel/a9571543408ec1581177e2aed38bb126 to your computer and use it in GitHub Desktop.
Touch Event Support for Simple2D
diff --git a/README.md b/README.md
index 2f09eed..f5412d7 100644
--- a/README.md
+++ b/README.md
@@ -596,6 +596,43 @@ S2D_ShowCursor(true);
S2D_ShowCursor(false);
```
+### Touch Displays
+
+There are three types of touch event captured by the window: touch start, finger movement, and touch end.
+When a touch event is detected, the window calls its `on_touch` function.
+
+To capture game touch input, first define the `on_touch` function and read the
+event details from the S2D_Event structure, for example:
+
+void on_touch(S2D_Event e) {
+ // Check `e.touch_id` for the device being interacted with
+ // Check `e.x` and `e.y` for the coordinates of a touch event
+ switch (e.type) {
+ case S2D_FINGER_DOWN:
+ // A touch sensitive device was touched
+ // Use `e.finger_id` to trace which finger is touching
+ break;
+
+ case S2D_FINGER_UP:
+ // A finger has been lifted from a touch sensitive device
+ // Use `e.finger_id` to see what finger was lifted
+ break;
+
+ case S2D_FINGER_MOTION:
+ // A finger has moved on a touch device
+ // Check `e.delta_x` and `e.delta_y` for the difference in x and y position
+ break;
+
+ }
+}
+```
+
+Then, attach the callback to the window:
+
+```c
+window->on_touch = on_touch;
+```
+
### Game controllers and joysticks
There are two types of controller or joystick events captured by the window: axis motion and button presses. When a button is pressed or a joystick moved, the window calls its `on_controller` function.
diff --git a/include/simple2d.h b/include/simple2d.h
index 69c7806..b8f810b 100644
--- a/include/simple2d.h
+++ b/include/simple2d.h
@@ -7,7 +7,7 @@ extern "C" {
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
-
+#include <inttypes.h>
// Set Platform Constants //////////////////////////////////////////////////////
// If ARM, assume GLES
@@ -109,6 +109,11 @@ extern "C" {
#define S2D_BUTTON_DOWN 2
#define S2D_BUTTON_UP 3
+// Touch events
+#define S2D_FINGER_DOWN 1
+#define S2D_FINGER_UP 2
+#define S2D_FINGER_MOTION 3
+
// Internal Shared Data ////////////////////////////////////////////////////////
extern char S2D_msg[1024]; // for S2D_Log messages
@@ -130,6 +135,8 @@ typedef struct {
int direction;
int axis;
int value;
+ int64_t finger_id;
+ int64_t touch_id;
} S2D_Event;
typedef void (*S2D_Update)();
@@ -137,6 +144,7 @@ typedef void (*S2D_Render)();
typedef void (*S2D_On_Key)(S2D_Event e);
typedef void (*S2D_On_Mouse)(S2D_Event e);
typedef void (*S2D_On_Controller)(S2D_Event e);
+typedef void (*S2D_On_Touch)(S2D_Event e);
// S2D_Color
typedef struct {
@@ -183,6 +191,7 @@ typedef struct {
S2D_On_Key on_key;
S2D_On_Mouse on_mouse;
S2D_On_Controller on_controller;
+ S2D_On_Touch on_touch;
bool vsync;
int fps_cap;
S2D_Color background;
diff --git a/src/window.c b/src/window.c
index 90b3641..e242b06 100644
--- a/src/window.c
+++ b/src/window.c
@@ -29,6 +29,7 @@ S2D_Window *S2D_CreateWindow(const char *title, int width, int height,
window->on_key = NULL;
window->on_mouse = NULL;
window->on_controller = NULL;
+ window->on_touch = NULL;
window->vsync = true;
window->fps_cap = 60;
window->background.r = 0.0;
@@ -190,6 +191,58 @@ int S2D_Show(S2D_Window *window) {
window->on_mouse(event);
}
break;
+ case SDL_FINGERMOTION:
+ {
+ if (window->on_touch) {
+ float fx,dx, fy, dy;
+ fx = e.tfinger.x * window->width; // TODO: Viewport in scaling
+ fy = e.tfinger.y * window->height;
+ dx = e.tfinger.dx * window->width;
+ dy = e.tfinger.dy * window->height;
+ S2D_GetMouseOnViewport(window, (int)fx, (int)fy, &mx, &my);
+ S2D_Event event = {
+ .x = mx, .y = my, .delta_x = dx, .delta_y = dy, .finger_id = e.tfinger.fingerId,
+ .touch_id = e.tfinger.touchId
+ };
+ event.type = S2D_FINGER_MOTION;
+ window->on_touch(event);
+ }
+
+ }break;
+ case SDL_FINGERDOWN:
+ {
+ if (window->on_touch) {
+ float fx, fy;
+ fx = e.tfinger.x * window->width; // TODO: Viewport in scaling
+ fy = e.tfinger.y * window->height;
+ S2D_GetMouseOnViewport(window, (int)fx, (int)fy, &mx, &my);
+ S2D_Event event = {
+ .x = mx, .y = my, .finger_id = e.tfinger.fingerId,
+ .touch_id = e.tfinger.touchId
+
+ };
+ event.type = S2D_FINGER_DOWN;;
+ window->on_touch(event);
+ }
+ }break;
+ case SDL_FINGERUP:
+ {
+ if (window->on_touch) {
+ float fx, fy;
+ // TODO: Finger ID tracking
+ fx = e.tfinger.x * window->width; // TODO: Viewport in scaling
+ fy = e.tfinger.y * window->height;
+ S2D_GetMouseOnViewport(window, (int)fx, (int)fy, &mx, &my);
+ S2D_Event event = {
+ .x = mx, .y = my, .finger_id = e.tfinger.fingerId,
+ .touch_id = e.tfinger.touchId
+
+ };
+ event.type = S2D_FINGER_UP;
+ window->on_touch(event);
+ }
+
+ }break;
case SDL_JOYAXISMOTION:
if (window->on_controller) {
diff --git a/test/testcard.c b/test/testcard.c
index 335310a..7be67cc 100644
--- a/test/testcard.c
+++ b/test/testcard.c
@@ -108,6 +108,27 @@ void on_mouse(S2D_Event e) {
if (e.type != S2D_MOUSE_SCROLL) printf("x: %i, y: %i\n", e.x, e.y);
}
+void on_touch(S2D_Event e) {
+ puts("=== Touch Event ===");
+ printf("finger: %" PRId64 ", device:%" PRId64 " ",e.finger_id,e.touch_id);
+ switch (e.type) {
+ case S2D_FINGER_DOWN:
+ puts("Finger down");
+ break;
+
+ case S2D_FINGER_UP:
+ puts("Finger up");
+ break;
+
+ case S2D_FINGER_MOTION:
+ puts("Finger motion");
+ printf("delta x: %i\ndelta y: %i\n", e.delta_x, e.delta_y);
+ break;
+
+ }
+
+ printf("x: %i, y: %i\n", e.x, e.y);
+}
void on_controller(S2D_Event e) {
puts("=== Controller Event ===");
@@ -334,12 +355,12 @@ int main() {
S2D_Diagnostics(true);
- window = S2D_CreateWindow("Simple 2D — Test Card", 600, 500, update, render, S2D_RESIZABLE);
+ window = S2D_CreateWindow("Simple 2D — Test Card", 800, 480, update, render, S2D_RESIZABLE);
window->on_key = on_key;
window->on_mouse = on_mouse;
window->on_controller = on_controller;
-
+ window->on_touch = on_touch;
img_png = S2D_CreateImage("media/image.png");
img_png->x = 300;
img_png->y = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment