code cleanup (using new and already existing structures)
[rocksndiamonds.git] / src / screens.c
index 6dd964f3f3df109cba6f08c22dcb2d8a825a004a..1ab2be290547204e844e7ba65e3b442e436c2006 100644 (file)
@@ -310,11 +310,7 @@ static TreeInfo *grid_size_current[2][2] = { { NULL, NULL }, { NULL, NULL } };
 static TreeInfo *level_number = NULL;
 static TreeInfo *level_number_current = NULL;
 
-static struct
-{
-  int value;
-  char *text;
-} window_sizes_list[] =
+static struct ValueTextInfo window_sizes_list[] =
 {
   {    50,     "50 %"                          },
   {    80,     "80 %"                          },
@@ -332,11 +328,7 @@ static struct
   {    -1,     NULL                            },
 };
 
-static struct
-{
-  char *value;
-  char *text;
-} scaling_types_list[] =
+static struct StringValueTextInfo scaling_types_list[] =
 {
   {    SCALING_QUALITY_NEAREST, "Off"          },
   {    SCALING_QUALITY_LINEAR,  "Linear"       },
@@ -345,11 +337,7 @@ static struct
   {    NULL,                    NULL           },
 };
 
-static struct
-{
-  char *value;
-  char *text;
-} rendering_modes_list[] =
+static struct StringValueTextInfo rendering_modes_list[] =
 {
   {    STR_SPECIAL_RENDERING_OFF,      "Off (May show artifacts, fast)" },
   {    STR_SPECIAL_RENDERING_BITMAP,   "Bitmap/Texture mode (slower)"   },
@@ -362,11 +350,7 @@ static struct
   {    NULL,                            NULL                            },
 };
 
-static struct
-{
-  char *value;
-  char *text;
-} vsync_modes_list[] =
+static struct StringValueTextInfo vsync_modes_list[] =
 {
   {    STR_VSYNC_MODE_OFF,             "Off"           },
   {    STR_VSYNC_MODE_NORMAL,          "Normal"        },
@@ -375,11 +359,7 @@ static struct
   {    NULL,                            NULL           },
 };
 
-static struct
-{
-  int value;
-  char *text;
-} game_speeds_list[] =
+static struct ValueTextInfo game_speeds_list[] =
 {
 #if 1
   {    30,     "Very Slow"                     },
@@ -406,11 +386,7 @@ static struct
   {    -1,     NULL                            },
 };
 
-static struct
-{
-  int value;
-  char *text;
-} scroll_delays_list[] =
+static struct ValueTextInfo scroll_delays_list[] =
 {
   {    0,      "0 Tiles (No Scroll Delay)"     },
   {    1,      "1 Tile"                        },
@@ -425,11 +401,7 @@ static struct
   {    -1,     NULL                            },
 };
 
-static struct
-{
-  char *value;
-  char *text;
-} snapshot_modes_list[] =
+static struct StringValueTextInfo snapshot_modes_list[] =
 {
   {    STR_SNAPSHOT_MODE_OFF,                  "Off"           },
   {    STR_SNAPSHOT_MODE_EVERY_STEP,           "Every Step"    },
@@ -439,11 +411,7 @@ static struct
   {    NULL,                                   NULL            },
 };
 
-static struct
-{
-  int value;
-  char *text;
-} volumes_list[] =
+static struct ValueTextInfo volumes_list[] =
 {
   {    0,      "0 %"                           },
   {    1,      "1 %"                           },
@@ -463,11 +431,7 @@ static struct
   {    -1,     NULL                            },
 };
 
-static struct
-{
-  char *value;
-  char *text;
-} touch_controls_list[] =
+static struct StringValueTextInfo touch_controls_list[] =
 {
   {    TOUCH_CONTROL_OFF,              "Off"                   },
   {    TOUCH_CONTROL_VIRTUAL_BUTTONS,  "Virtual Buttons"       },
@@ -477,11 +441,7 @@ static struct
   {    NULL,                           NULL                    },
 };
 
-static struct
-{
-  int value;
-  char *text;
-} distances_list[] =
+static struct ValueTextInfo distances_list[] =
 {
   {    1,      "1 %"                           },
   {    2,      "2 %"                           },
@@ -496,11 +456,7 @@ static struct
   {    -1,     NULL                            },
 };
 
-static struct
-{
-  int value;
-  char *text;
-} transparencies_list[] =
+static struct ValueTextInfo transparencies_list[] =
 {
   {    0,      "0 % (Opaque)"                  },
   {    10,     "10 %"                          },
@@ -517,11 +473,7 @@ static struct
   {    -1,     NULL                            },
 };
 
-static struct
-{
-  int value;
-  char *text;
-} grid_sizes_list[] =
+static struct ValueTextInfo grid_sizes_list[] =
 {
   {    3,      "3"                             },
   {    4,      "4"                             },
@@ -4751,7 +4703,7 @@ static void execSetupMain(void)
   DrawSetupScreen();
 }
 
-static void execSetupGame_setGameSpeeds(void)
+static void execSetupGame_setGameSpeeds(boolean update_value)
 {
   if (game_speeds == NULL)
   {
@@ -4781,6 +4733,11 @@ static void execSetupGame_setGameSpeeds(void)
     /* sort game speed values to start with slowest game speed */
     sortTreeInfo(&game_speeds);
 
+    update_value = TRUE;
+  }
+
+  if (update_value)
+  {
     /* set current game speed to configured game speed value */
     game_speed_current =
       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
@@ -4918,9 +4875,32 @@ static void execSetupGame_setNetworkServerText(void)
   network_server_text = network_server_hostname;
 }
 
+static void CheckGameSpeedForVsync(boolean force_vsync_game_speed)
+{
+  if (strEqual(setup.vsync_mode, STR_VSYNC_MODE_OFF) ||
+      setup.game_frame_delay <= MAX_VSYNC_FRAME_DELAY)
+    return;
+
+  if (force_vsync_game_speed)
+  {
+    /* set game speed to existing list value that is fast enough for vsync */
+    setup.game_frame_delay = 15;
+
+    execSetupGame_setGameSpeeds(TRUE);
+
+    Request("Game speed was set to \"fast\" for VSync to work!", REQ_CONFIRM);
+  }
+  else
+  {
+    Request("Warning! Game speed too low for VSync to work!", REQ_CONFIRM);
+  }
+}
+
 static void execSetupGame(void)
 {
-  execSetupGame_setGameSpeeds();
+  boolean check_vsync_game_speed = (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED);
+
+  execSetupGame_setGameSpeeds(FALSE);
   execSetupGame_setScrollDelays();
   execSetupGame_setSnapshotModes();
 
@@ -4929,6 +4909,10 @@ static void execSetupGame(void)
   setup_mode = SETUP_MODE_GAME;
 
   DrawSetupScreen();
+
+  // check if game speed is high enough for 60 Hz vsync to work
+  if (check_vsync_game_speed)
+    CheckGameSpeedForVsync(FALSE);
 }
 
 static void execSetupChooseGameSpeed(void)
@@ -5197,6 +5181,8 @@ static void execSetupGraphics_setVsyncModes(void)
 
 static void execSetupGraphics(void)
 {
+  boolean check_vsync_game_speed = (setup_mode == SETUP_MODE_CHOOSE_VSYNC);
+
   // update "setup.window_scaling_percent" from list selection
   // (in this case, window scaling was changed on setup screen)
   if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
@@ -5214,6 +5200,10 @@ static void execSetupGraphics(void)
 
   DrawSetupScreen();
 
+  // check if game speed is high enough for 60 Hz vsync to work
+  if (check_vsync_game_speed)
+    CheckGameSpeedForVsync(TRUE);
+
 #if defined(TARGET_SDL2)
   // window scaling may have changed at this point
   ToggleFullscreenOrChangeWindowScalingIfNeeded();
@@ -5953,6 +5943,12 @@ static void execGadgetNetworkServer(void)
 
 static void ToggleNetworkModeIfNeeded(void)
 {
+  int font_title = FONT_TITLE_1;
+  int font_foot = FC_BLUE;
+  int ystart  = mSY - SY + 16;
+  int ybottom = mSY - SY + SYSIZE - 20;
+  char *text = (setup.network_mode ? "Start Network" : "Stop Network");
+
   if (setup.network_mode == network.enabled)
     return;
 
@@ -5962,6 +5958,8 @@ static void ToggleNetworkModeIfNeeded(void)
 
   ClearField();
 
+  DrawTextSCentered(ystart, font_title, text);
+
   FadeIn(REDRAW_ALL);
 
   if (network.enabled)
@@ -5969,6 +5967,11 @@ static void ToggleNetworkModeIfNeeded(void)
   else
     DisconnectFromNetworkServer();
 
+  DrawTextSCentered(ybottom, font_foot,
+                   "Press any key or button for setup menu");
+
+  WaitForEventToContinue();
+
   DrawSetupScreen();
 }
 
@@ -6476,10 +6479,12 @@ static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
   int startx = mSX + xpos * 32;
   int starty = mSY + ypos * 32;
-  int font_nr, font_nr_default, font_width_default;
   int type = si->type;
   void *value = si->value;
   char *value_string = getSetupValue(type, value);
+  int font_nr_default = getSetupValueFont(type, value);
+  int font_width_default = getFontWidth(font_nr_default);
+  int font_nr = font_nr_default;
   int i;
 
   if (value_string == NULL)
@@ -6494,7 +6499,7 @@ static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
   }
   else if (type & TYPE_STRING)
   {
-    int max_value_len = (SCR_FIELDX - 2) * 2;
+    int max_value_len = (SXSIZE - 2 * TILEX) / font_width_default;
 
     xpos = MENU_SCREEN_START_XPOS;
 
@@ -6514,10 +6519,6 @@ static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
 
   startx = mSX + xpos * 32;
   starty = mSY + ypos * 32;
-  font_nr_default = getSetupValueFont(type, value);
-  font_width_default = getFontWidth(font_nr_default);
-
-  font_nr = font_nr_default;
 
   // special check if right-side setup values moved left due to scrollbar
   if (scrollbar_needed && xpos > MENU_SCREEN_START_XPOS)
@@ -6583,10 +6584,17 @@ static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
 
   if (type & TYPE_PLAYER)
   {
+    struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
     int player_nr = *(int *)value;
-    int xoff = getFontWidth(font_nr);
+    int xoff = font->draw_xoffset + getFontWidth(font_nr);
+    int yoff = font->draw_yoffset + (getFontHeight(font_nr) - TILEY) / 2;
+    int startx2 = startx + xoff;
+    int starty2 = starty + yoff;
+
+    if (DrawingOnBackground(startx2, starty2))
+      ClearRectangleOnBackground(drawto, startx2, starty2, TILEX, TILEY);
 
-    DrawFixedGraphicThruMaskExt(drawto, startx + xoff, starty,
+    DrawFixedGraphicThruMaskExt(drawto, startx2, starty2,
                                PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
   }