summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorzlago2024-10-14 13:29:23 +0200
committerzlago2024-10-14 13:37:49 +0200
commit33efe41c2a1d36ab1c2f567535b4eba31d9c50ef (patch)
tree69e3103e1fe7ebca0597692c9235e2538353392d /src
parent03b817a354913240b3bccd4148c9acec39933acc (diff)
touchscreen support
Diffstat (limited to 'src')
-rw-r--r--src/main.c60
1 files changed, 56 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index 85d0917..345f6fb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -45,6 +45,17 @@ SDL_Scancode keybinds[] = {
 	SDL_SCANCODE_A,
 	SDL_SCANCODE_S,
 };
+unsigned input_keyboard = 0;
+
+struct touch {
+	size_t touches;
+	size_t allocated;
+	struct touch_vec {
+		float x, y;
+		int active;
+	} *positions;
+	unsigned input_touch;
+} touch = {0, 0, NULL, 0};
 
 struct entities entities = {{{0}}, {{0}}, 0, {{0}}, 0}, next_entities = {{{0}}, {{0}}, 0, {{0}}, 0};
 
@@ -430,9 +441,9 @@ void main_loop(void) {
 					for (unsigned key = 0, bit = 1; key < INPUT_LENGTH; key++, bit <<= 1) {
 						if (evt.key.keysym.scancode == keybinds[key]) {
 							if (evt.key.state == SDL_PRESSED)
-								input_held |= bit;
+								input_keyboard |= bit;
 							else
-								input_held &= ~bit;
+								input_keyboard &= ~bit;
 						}
 					}
 					//fprintf(stderr, "input: %0*b\n", INPUT_LENGTH, input_held);
@@ -454,10 +465,48 @@ void main_loop(void) {
 					break;
 				case SDL_CLIPBOARDUPDATE: // annoying
 					break;
+
+				case SDL_FINGERUP:;
+					size_t i = evt.tfinger.fingerId;
+					touch.positions[i].active = 0;
+					goto reset_touch;
 				case SDL_FINGERDOWN:
-				case SDL_FINGERUP:
+					i = evt.tfinger.fingerId;
+					if (i >= touch.allocated) {
+						touch.allocated = i + 1;
+						touch.positions = realloc(touch.positions, sizeof (struct touch_vec) * touch.allocated);
+					}
+					touch.positions[i].active = 1;
 				case SDL_FINGERMOTION:
+					i = evt.tfinger.fingerId;
+					touch.positions[i].x = evt.tfinger.x;
+					touch.positions[i].y = evt.tfinger.y;
+					printf("%" PRIu64 " %" PRIu64 " %f %f\n", evt.tfinger.touchId, evt.tfinger.fingerId, evt.tfinger.x, evt.tfinger.y);
+					reset_touch:
+					touch.input_touch = 0;
+					for (size_t i = 0; i < touch.allocated; i++) {
+						if (touch.positions[i].active) {
+							switch ((int) (touch.positions[i].x * 5)) {
+								case 0:
+									touch.input_touch |= 1 << INPUT_LEFT;
+									break;
+								case 1:
+									touch.input_touch |= 1 << INPUT_RIGHT;
+									break;
+								case 3:
+									touch.input_touch |= 1 << INPUT_S;
+									break;
+								case 4:
+									touch.input_touch |= 1 << INPUT_A;
+									break;
+								default:;
+							}
+						}
+					}
 					break;
+				case SDL_MULTIGESTURE:
+					break;
+				
 				case SDL_WINDOWEVENT: // window related events
 					switch (evt.window.event) {
 						case SDL_WINDOWEVENT_SHOWN:
@@ -509,6 +558,7 @@ void main_loop(void) {
 					fprintf(stderr, "unknown event type %i\n", evt.type);
 			}
 		}
+		input_held = input_keyboard | touch.input_touch;
 		input_pressed = ~input_pressed & input_held;
 		//fprintf(stderr, "input: %0*b\n", INPUT_LENGTH, input_pressed);
 
@@ -589,7 +639,9 @@ void main_loop(void) {
 	SDL_DestroyRenderer(renderer);
 	SDL_DestroyWindow(window);
 	SDL_QuitSubSystem(INIT_SUBSYSTEMS);
-#if !defined(__EMSCRIPTEN__)
+#if defined(__EMSCRIPTEN__)
+	emscripten_cancel_main_loop();
+#else
 	return 0;
 #endif
 }