replaced bitmask for game/tape key/mouse actions by flags again
authorHolger Schemel <info@artsoft.org>
Wed, 22 Apr 2020 10:57:15 +0000 (12:57 +0200)
committerHolger Schemel <info@artsoft.org>
Tue, 19 May 2020 16:21:19 +0000 (18:21 +0200)
The previous solution using bitmasks was simply over-engineered.

src/events.c
src/files.c
src/game.c
src/game.h
src/tape.c
src/tape.h

index 45da8607964b7be19a441b0b194fb8b71ffe1352..c32f35e91356cf6800d7a9a50a56c5ab028bda74 100644 (file)
@@ -464,7 +464,7 @@ static void SetPlayerMouseAction(int mx, int my, int button)
   local_player->mouse_action.ly = ly;
   local_player->mouse_action.button = button;
 
-  if (tape.recording && tape.pausing && (tape.event_mask & GAME_EVENTS_MOUSE))
+  if (tape.recording && tape.pausing && tape.use_mouse_actions)
   {
     // un-pause a paused game only if mouse button was newly pressed down
     if (new_button)
@@ -1375,7 +1375,7 @@ static void HandleButtonOrFinger(int mx, int my, int button)
   {
     if (strEqual(setup.touch.control_type, TOUCH_CONTROL_FOLLOW_FINGER))
       HandleButtonOrFinger_FollowFinger(mx, my, button);
-    else if ((game.event_mask & GAME_EVENTS_MOUSE) && valid_mouse_event)
+    else if (game.use_mouse_actions && valid_mouse_event)
       SetPlayerMouseAction(mx, my, button);
   }
 }
@@ -2098,7 +2098,7 @@ void HandleKey(Key key, int key_status)
       if (stored_player[pnr].snap_action)
        stored_player[pnr].action |= JOY_BUTTON_SNAP;
 
-      if (tape.recording && tape.pausing && (tape.event_mask & GAME_EVENTS_KEYS))
+      if (tape.recording && tape.pausing && tape.use_key_actions)
       {
        if (tape.single_step)
        {
@@ -2633,7 +2633,7 @@ void HandleJoystick(void)
        return;
       }
 
-      if (tape.recording && tape.pausing && (tape.event_mask & GAME_EVENTS_KEYS))
+      if (tape.recording && tape.pausing && tape.use_key_actions)
       {
        if (tape.single_step)
        {
index 95256480249e9e672862aed626d16d9629e8358f..6024f1f8ea8788d368185a0536f8ea13d4f7baf0 100644 (file)
@@ -7623,10 +7623,10 @@ static int getTapePosSize(struct TapeInfo *tape)
 {
   int tape_pos_size = 0;
 
-  if (tape->event_mask & GAME_EVENTS_KEYS)
+  if (tape->use_key_actions)
     tape_pos_size += tape->num_participating_players;
 
-  if (tape->event_mask & GAME_EVENTS_MOUSE)
+  if (tape->use_mouse_actions)
     tape_pos_size += 3;                // x and y position and mouse button mask
 
   tape_pos_size += 1;          // tape action delay value
@@ -7634,40 +7634,25 @@ static int getTapePosSize(struct TapeInfo *tape)
   return tape_pos_size;
 }
 
-static int getGameEventMaskFromTapeEventValue(int value)
+static void setTapeActionFlags(struct TapeInfo *tape, int value)
 {
-  switch (value)
-  {
-    case TAPE_EVENTS_KEYS_ONLY:
-      return GAME_EVENTS_KEYS;
-
-    case TAPE_EVENTS_MOUSE_ONLY:
-      return GAME_EVENTS_MOUSE;
+  tape->use_key_actions = FALSE;
+  tape->use_mouse_actions = FALSE;
 
-    case TAPE_EVENTS_KEYS_AND_MOUSE:
-      return GAME_EVENTS_KEYS | GAME_EVENTS_MOUSE;
+  if (value != TAPE_USE_MOUSE_ACTIONS_ONLY)
+    tape->use_key_actions = TRUE;
 
-    default:
-      return GAME_EVENTS_DEFAULT;
-  }
+  if (value != TAPE_USE_KEY_ACTIONS_ONLY)
+    tape->use_mouse_actions = TRUE;
 }
 
-static int getTapeEventValueFromGameEventMask(int mask)
+static int getTapeActionValue(struct TapeInfo *tape)
 {
-  switch (mask)
-  {
-    case GAME_EVENTS_KEYS:
-      return TAPE_EVENTS_KEYS_ONLY;
-
-    case GAME_EVENTS_MOUSE:
-      return TAPE_EVENTS_MOUSE_ONLY;
-
-    case GAME_EVENTS_KEYS | GAME_EVENTS_MOUSE:
-      return TAPE_EVENTS_KEYS_AND_MOUSE;
-
-    default:
-      return TAPE_EVENTS_DEFAULT;
-  }
+  return (tape->use_key_actions &&
+         tape->use_mouse_actions ? TAPE_USE_KEY_AND_MOUSE_ACTIONS :
+         tape->use_key_actions   ? TAPE_USE_KEY_ACTIONS_ONLY :
+         tape->use_mouse_actions ? TAPE_USE_MOUSE_ACTIONS_ONLY :
+         TAPE_ACTIONS_DEFAULT);
 }
 
 static int LoadTape_VERS(File *file, int chunk_size, struct TapeInfo *tape)
@@ -7705,7 +7690,7 @@ static int LoadTape_HEAD(File *file, int chunk_size, struct TapeInfo *tape)
       }
     }
 
-    tape->event_mask = getGameEventMaskFromTapeEventValue(getFile8Bit(file));
+    setTapeActionFlags(tape, getFile8Bit(file));
 
     ReadUnusedBytesFromFile(file, TAPE_CHUNK_HEAD_UNUSED);
 
@@ -7765,7 +7750,7 @@ static int LoadTape_BODY(File *file, int chunk_size, struct TapeInfo *tape)
       break;
     }
 
-    if (tape->event_mask & GAME_EVENTS_KEYS)
+    if (tape->use_key_actions)
     {
       for (j = 0; j < MAX_PLAYERS; j++)
       {
@@ -7776,7 +7761,7 @@ static int LoadTape_BODY(File *file, int chunk_size, struct TapeInfo *tape)
       }
     }
 
-    if (tape->event_mask & GAME_EVENTS_MOUSE)
+    if (tape->use_mouse_actions)
     {
       tape->pos[i].action[TAPE_ACTION_LX]     = getFile8Bit(file);
       tape->pos[i].action[TAPE_ACTION_LY]     = getFile8Bit(file);
@@ -8106,7 +8091,7 @@ static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
 
   putFile8Bit(file, store_participating_players);
 
-  putFile8Bit(file, getTapeEventValueFromGameEventMask(tape->event_mask));
+  putFile8Bit(file, getTapeActionValue(tape));
 
   // unused bytes not at the end here for 4-byte alignment of engine_version
   WriteUnusedBytesToFile(file, TAPE_CHUNK_HEAD_UNUSED);
@@ -8133,14 +8118,14 @@ static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
 
   for (i = 0; i < tape->length; i++)
   {
-    if (tape->event_mask & GAME_EVENTS_KEYS)
+    if (tape->use_key_actions)
     {
       for (j = 0; j < MAX_PLAYERS; j++)
        if (tape->player_participates[j])
          putFile8Bit(file, tape->pos[i].action[j]);
     }
 
-    if (tape->event_mask & GAME_EVENTS_MOUSE)
+    if (tape->use_mouse_actions)
     {
       putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LX]);
       putFile8Bit(file, tape->pos[i].action[TAPE_ACTION_LY]);
index 9fe37cc551bb053b6536517d1809a297b6e6a1be..504e2d89cf341a2075fb0ed7181bc28c3a87a9e8 100644 (file)
@@ -3280,14 +3280,18 @@ static void InitGameEngine(void)
   if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
     level.time = 0;
 
-  // ---------- initialize mask for handling game action events ---------------
+  // ---------- initialize flags for handling game actions --------------------
 
-  // set game action events mask to default value
-  game.event_mask = GAME_EVENTS_DEFAULT;
+  // set flags for game actions to default values
+  game.use_key_actions = TRUE;
+  game.use_mouse_actions = FALSE;
 
   // when using Mirror Magic game engine, handle mouse events only
   if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
-    game.event_mask = GAME_EVENTS_MOUSE;
+  {
+    game.use_key_actions = FALSE;
+    game.use_mouse_actions = TRUE;
+  }
 
   // check for custom elements with mouse click events
   if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
@@ -3300,7 +3304,7 @@ static void InitGameEngine(void)
          HAS_CHANGE_EVENT(element, CE_PRESSED_BY_MOUSE) ||
          HAS_CHANGE_EVENT(element, CE_MOUSE_CLICKED_ON_X) ||
          HAS_CHANGE_EVENT(element, CE_MOUSE_PRESSED_ON_X))
-       game.event_mask = GAME_EVENTS_KEYS | GAME_EVENTS_MOUSE;
+       game.use_mouse_actions = TRUE;
     }
   }
 }
@@ -3415,9 +3419,12 @@ void InitGame(void)
   InitGameEngine();
   InitGameControlValues();
 
-  // initialize tape action events from game when recording tape
+  // initialize tape actions from game when recording tape
   if (tape.recording)
-    tape.event_mask = game.event_mask;
+  {
+    tape.use_key_actions   = game.use_key_actions;
+    tape.use_mouse_actions = game.use_mouse_actions;
+  }
 
   // don't play tapes over network
   network_playing = (network.enabled && !tape.playing);
@@ -11176,7 +11183,7 @@ static byte PlayerActions(struct PlayerInfo *player, byte player_action)
 static void SetMouseActionFromTapeAction(struct MouseActionInfo *mouse_action,
                                         byte *tape_action)
 {
-  if (!(tape.event_mask & GAME_EVENTS_MOUSE))
+  if (!tape.use_mouse_actions)
     return;
 
   mouse_action->lx     = tape_action[TAPE_ACTION_LX];
@@ -11187,7 +11194,7 @@ static void SetMouseActionFromTapeAction(struct MouseActionInfo *mouse_action,
 static void SetTapeActionFromMouseAction(byte *tape_action,
                                         struct MouseActionInfo *mouse_action)
 {
-  if (!(tape.event_mask & GAME_EVENTS_MOUSE))
+  if (!tape.use_mouse_actions)
     return;
 
   tape_action[TAPE_ACTION_LX]     = mouse_action->lx;
index e20f50fa6101ce28bff631c2a0016ba5703f3575..36d4a6af33589e1b34a81af1edd052af8a294754 100644 (file)
 #define SNAPSHOT_MODE_EVERY_COLLECT    3
 #define SNAPSHOT_MODE_DEFAULT          SNAPSHOT_MODE_OFF
 
-// values for game action events handled by game engine
-#define GAME_EVENTS_NONE               0
-#define GAME_EVENTS_KEYS               (1 << 0)
-#define GAME_EVENTS_MOUSE              (1 << 1)
-
-#define GAME_EVENTS_DEFAULT            GAME_EVENTS_KEYS
-
 
 struct GamePanelInfo
 {
@@ -185,8 +178,9 @@ struct GameInfo
   boolean max_num_changes_per_frame;
   boolean use_reverse_scan_direction;
 
-  // bit mask to indicate game action events handled by game engine
-  int event_mask;
+  // flags to indicate which game actions are used in this game
+  boolean use_key_actions;
+  boolean use_mouse_actions;
 
   // variable within running game
   int yamyam_content_nr;
index 0995976329d36c8ba5026037dfe5c8eea5c99e2c..f064f6cbb26b01e6808dbe42a8ec45fdf110aa72 100644 (file)
@@ -547,7 +547,9 @@ void TapeErase(void)
   tape.centered_player_nr_next = -1;
   tape.set_centered_player = FALSE;
 
-  tape.event_mask = GAME_EVENTS_DEFAULT;
+  // set flags for game actions to default values (may be overwritten later)
+  tape.use_key_actions = TRUE;
+  tape.use_mouse_actions = FALSE;
 }
 
 static void TapeRewind(void)
@@ -706,7 +708,7 @@ void TapeRecordAction(byte action_raw[MAX_TAPE_ACTIONS])
   for (i = 0; i < MAX_TAPE_ACTIONS; i++)
     action[i] = action_raw[i];
 
-  if ((tape.event_mask & GAME_EVENTS_KEYS) && tape.set_centered_player)
+  if (tape.use_key_actions && tape.set_centered_player)
   {
     for (i = 0; i < MAX_PLAYERS; i++)
       if (tape.centered_player_nr_next == i ||
@@ -887,7 +889,7 @@ byte *TapePlayAction(void)
   tape.set_centered_player = FALSE;
   tape.centered_player_nr_next = -999;
 
-  if (tape.event_mask & GAME_EVENTS_KEYS)
+  if (tape.use_key_actions)
   {
     for (i = 0; i < MAX_PLAYERS; i++)
     {
index d2878581ed8942dba68c668cbf349083d2161557..a33c859f13e9383df4e2b2f27e18be50f3cdc1a5 100644 (file)
 // values for tape properties
 #define MAX_TAPE_LEN           (1000 * FRAMES_PER_SECOND) // max.time x fps
 
-// values for tape mouse actions
+// values for tape action array positions
 #define TAPE_ACTION_LX         (MAX_PLAYERS + 0)
 #define TAPE_ACTION_LY         (MAX_PLAYERS + 1)
 #define TAPE_ACTION_BUTTON     (MAX_PLAYERS + 2)
 
 #define MAX_TAPE_ACTIONS       (MAX_PLAYERS + 3)
 
-// values for tape action events stored in tape file
-#define TAPE_EVENTS_KEYS_ONLY          0
-#define TAPE_EVENTS_MOUSE_ONLY         1
-#define TAPE_EVENTS_KEYS_AND_MOUSE     2
+// values for tape actions stored in tape file
+#define TAPE_USE_KEY_ACTIONS_ONLY      0
+#define TAPE_USE_MOUSE_ACTIONS_ONLY    1
+#define TAPE_USE_KEY_AND_MOUSE_ACTIONS 2
 
-#define TAPE_EVENTS_DEFAULT            TAPE_EVENTS_KEYS_ONLY
+#define TAPE_ACTIONS_DEFAULT           TAPE_USE_KEY_ACTIONS_ONLY
 
 // some positions in the video tape control window
 #define VIDEO_DISPLAY1_XPOS    5
@@ -198,7 +198,9 @@ struct TapeInfo
   int centered_player_nr_next;
   boolean set_centered_player;
 
-  int event_mask;      // game action events stored in tape actions
+  // flags to indicate which game actions are stored in this tape
+  boolean use_key_actions;
+  boolean use_mouse_actions;
 
   struct
   {