+#if defined(TARGET_SDL2)
+void HandleWindowEvent(WindowEvent *event)
+{
+ int subtype = event->event;
+
+ char *event_name =
+ (subtype == SDL_WINDOWEVENT_SHOWN ? "SDL_WINDOWEVENT_SHOWN" :
+ subtype == SDL_WINDOWEVENT_HIDDEN ? "SDL_WINDOWEVENT_HIDDEN" :
+ subtype == SDL_WINDOWEVENT_EXPOSED ? "SDL_WINDOWEVENT_EXPOSED" :
+ subtype == SDL_WINDOWEVENT_MOVED ? "SDL_WINDOWEVENT_MOVED" :
+ subtype == SDL_WINDOWEVENT_SIZE_CHANGED ? "SDL_WINDOWEVENT_SIZE_CHANGED" :
+ subtype == SDL_WINDOWEVENT_RESIZED ? "SDL_WINDOWEVENT_RESIZED" :
+ subtype == SDL_WINDOWEVENT_MINIMIZED ? "SDL_WINDOWEVENT_MINIMIZED" :
+ subtype == SDL_WINDOWEVENT_MAXIMIZED ? "SDL_WINDOWEVENT_MAXIMIZED" :
+ subtype == SDL_WINDOWEVENT_RESTORED ? "SDL_WINDOWEVENT_RESTORED" :
+ subtype == SDL_WINDOWEVENT_ENTER ? "SDL_WINDOWEVENT_ENTER" :
+ subtype == SDL_WINDOWEVENT_LEAVE ? "SDL_WINDOWEVENT_LEAVE" :
+ subtype == SDL_WINDOWEVENT_FOCUS_GAINED ? "SDL_WINDOWEVENT_FOCUS_GAINED" :
+ subtype == SDL_WINDOWEVENT_FOCUS_LOST ? "SDL_WINDOWEVENT_FOCUS_LOST" :
+ subtype == SDL_WINDOWEVENT_CLOSE ? "SDL_WINDOWEVENT_CLOSE" :
+ "(UNKNOWN)");
+
+ Error(ERR_DEBUG, "WINDOW EVENT: '%s', %ld, %ld",
+ event_name, event->data1, event->data2);
+
+ if (event->event == SDL_WINDOWEVENT_EXPOSED)
+ SDLRedrawWindow();
+
+#if 0
+ if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED)
+ {
+ // if game started in fullscreen mode, window will also get fullscreen size
+ if (!video.fullscreen_enabled && video.fullscreen_initial)
+ {
+ SDLSetWindowScaling(setup.window_scaling_percent);
+
+ // only do this correction once
+ video.fullscreen_initial = FALSE;
+ }
+ }
+#endif
+
+ if (event->event == SDL_WINDOWEVENT_RESIZED && !video.fullscreen_enabled)
+ {
+#if 1
+ int new_window_width = event->data1;
+ int new_window_height = event->data2;
+
+ printf("::: RESIZED from %d, %d to %d, %d\n",
+ video.window_width, video.window_height,
+ new_window_width, new_window_height);
+
+ // if window size has changed after resizing, calculate new scaling factor
+ if (new_window_width != video.window_width ||
+ new_window_height != video.window_height)
+ {
+ int new_xpercent = (100 * new_window_width / video.width);
+ int new_ypercent = (100 * new_window_height / video.height);
+
+ setup.window_scaling_percent = video.window_scaling_percent =
+ MIN(MAX(MIN_WINDOW_SCALING_PERCENT, MIN(new_xpercent, new_ypercent)),
+ MAX_WINDOW_SCALING_PERCENT);
+
+ video.window_width = new_window_width;
+ video.window_height = new_window_height;
+
+ printf("::: setup.window_scaling_percent set to %d\n",
+ setup.window_scaling_percent);
+ }
+#else
+ // prevent slightly wrong scaling factor due to rounding differences
+ float scaling_factor = (float)setup.window_scaling_percent / 100;
+ int old_xsize = (int)(scaling_factor * video.width);
+ int old_ysize = (int)(scaling_factor * video.height);
+ int new_xsize = event->data1;
+ int new_ysize = event->data2;
+
+ // window size is unchanged when going from fullscreen to window mode,
+ // but reverse calculation of scaling factor might result in a scaling
+ // factor that is slightly different due to rounding differences;
+ // therefore compare old/new window size and not old/new scaling factor
+ if (old_xsize != new_xsize ||
+ old_ysize != new_ysize)
+ {
+ int new_xpercent = (100 * new_xsize / video.width);
+ int new_ypercent = (100 * new_ysize / video.height);
+
+ setup.window_scaling_percent = MIN(new_xpercent, new_ypercent);
+
+ if (setup.window_scaling_percent < MIN_WINDOW_SCALING_PERCENT)
+ setup.window_scaling_percent = MIN_WINDOW_SCALING_PERCENT;
+ else if (setup.window_scaling_percent > MAX_WINDOW_SCALING_PERCENT)
+ setup.window_scaling_percent = MAX_WINDOW_SCALING_PERCENT;
+
+ printf("::: setup.window_scaling_percent set to %d\n",
+ setup.window_scaling_percent);
+ }
+#endif
+ }
+}
+
+void HandleFingerEvent(FingerEvent *event)
+{
+#if 0
+ static int num_events = 0;
+ int max_events = 10;
+#endif
+
+#if DEBUG_EVENTS
+ Error(ERR_DEBUG, "FINGER EVENT: finger was %s, touch ID %lld, finger ID %lld, x/y %f/%f, dx/dy %f/%f, pressure %f",
+ event->type == EVENT_FINGERPRESS ? "pressed" :
+ event->type == EVENT_FINGERRELEASE ? "released" : "moved",
+ event->touchId,
+ event->fingerId,
+ event->x, event->y,
+ event->dx, event->dy,
+ event->pressure);
+#endif
+
+#if 0
+ int x = (int)(event->x * video.width);
+ int y = (int)(event->y * video.height);
+ int button = MB_LEFTBUTTON;
+
+ Error(ERR_DEBUG, "=> screen x/y %d/%d", x, y);
+#endif
+
+#if 0
+ if (++num_events >= max_events)
+ CloseAllAndExit(0);
+#endif
+
+#if 1
+#if 0
+ if (event->type == EVENT_FINGERPRESS ||
+ event->type == EVENT_FINGERMOTION)
+ button_status = button;
+ else
+ button_status = MB_RELEASED;
+
+ int max_x = SX + SXSIZE;
+ int max_y = SY + SYSIZE;
+#endif
+
+#if 1
+ if (game_status == GAME_MODE_PLAYING)
+#else
+ if (game_status == GAME_MODE_PLAYING &&
+ x < max_x)
+#endif
+ {
+ int key_status = (event->type == EVENT_FINGERRELEASE ? KEY_RELEASED :
+ KEY_PRESSED);
+#if 1
+ Key key = (event->y < 1.0 / 3.0 ? setup.input[0].key.up :
+ event->y > 2.0 / 3.0 ? setup.input[0].key.down :
+ event->x < 1.0 / 3.0 ? setup.input[0].key.left :
+ event->x > 2.0 / 3.0 ? setup.input[0].key.right :
+ setup.input[0].key.drop);
+#else
+ Key key = (y < max_y / 3 ? setup.input[0].key.up :
+ y > 2 * max_y / 3 ? setup.input[0].key.down :
+ x < max_x / 3 ? setup.input[0].key.left :
+ x > 2 * max_x / 3 ? setup.input[0].key.right :
+ setup.input[0].key.drop);
+#endif
+
+ Error(ERR_DEBUG, "=> key == %d, key_status == %d", key, key_status);
+
+ HandleKey(key, key_status);
+ }
+ else
+ {
+#if 0
+ Error(ERR_DEBUG, "::: button_status == %d, button == %d\n",
+ button_status, button);
+
+ HandleButton(x, y, button_status, button);
+#endif
+ }
+#endif
+}
+
+static boolean checkTextInputKeyModState()
+{
+ // when playing, only handle raw key events and ignore text input
+ if (game_status == GAME_MODE_PLAYING)
+ return FALSE;
+
+ return ((GetKeyModState() & KMOD_TextInput) != KMOD_None);
+}
+
+void HandleTextEvent(TextEvent *event)
+{
+ char *text = event->text;
+ Key key = getKeyFromKeyName(text);
+
+#if DEBUG_EVENTS
+ Error(ERR_DEBUG, "TEXT EVENT: text == '%s' [%d byte(s), '%c'/%d], resulting key == %d (%s)",
+ text,
+ strlen(text),
+ text[0], (int)(text[0]),
+ key,
+ getKeyNameFromKey(key));
+#endif
+
+ // if (game_status != GAME_MODE_PLAYING && GetKeyModState() != KMOD_None)
+ /*
+ if (game_status != GAME_MODE_PLAYING &&
+ (GetKeyModState() & KMOD_TextInput) != KMOD_None)
+ */
+ if (checkTextInputKeyModState())
+ {
+ HandleKey(key, KEY_PRESSED);
+ HandleKey(key, KEY_RELEASED);
+ }
+}
+#endif
+