added support for (normal and adaptive) vertical sync (vsync)
[rocksndiamonds.git] / src / screens.c
index 12fc3bfe2676e1b8d09ca5084eda80e3ffd3e1de..b339ced690cbe546f406d7702b9800bf8c462978 100644 (file)
 #define SETUP_MODE_CHOOSE_WINDOW_SIZE  19
 #define SETUP_MODE_CHOOSE_SCALING_TYPE 20
 #define SETUP_MODE_CHOOSE_RENDERING    21
-#define SETUP_MODE_CHOOSE_GRAPHICS     22
-#define SETUP_MODE_CHOOSE_SOUNDS       23
-#define SETUP_MODE_CHOOSE_MUSIC                24
-#define SETUP_MODE_CHOOSE_VOLUME_SIMPLE        25
-#define SETUP_MODE_CHOOSE_VOLUME_LOOPS 26
-#define SETUP_MODE_CHOOSE_VOLUME_MUSIC 27
-#define SETUP_MODE_CHOOSE_TOUCH_CONTROL        28
-#define SETUP_MODE_CHOOSE_MOVE_DISTANCE        29
-#define SETUP_MODE_CHOOSE_DROP_DISTANCE        30
-#define SETUP_MODE_CHOOSE_TRANSPARENCY 31
-#define SETUP_MODE_CHOOSE_GRID_XSIZE_0 32
-#define SETUP_MODE_CHOOSE_GRID_YSIZE_0 33
-#define SETUP_MODE_CHOOSE_GRID_XSIZE_1 34
-#define SETUP_MODE_CHOOSE_GRID_YSIZE_1 35
-#define SETUP_MODE_CONFIG_VIRT_BUTTONS 36
-
-#define MAX_SETUP_MODES                        37
+#define SETUP_MODE_CHOOSE_VSYNC                22
+#define SETUP_MODE_CHOOSE_GRAPHICS     23
+#define SETUP_MODE_CHOOSE_SOUNDS       24
+#define SETUP_MODE_CHOOSE_MUSIC                25
+#define SETUP_MODE_CHOOSE_VOLUME_SIMPLE        26
+#define SETUP_MODE_CHOOSE_VOLUME_LOOPS 27
+#define SETUP_MODE_CHOOSE_VOLUME_MUSIC 28
+#define SETUP_MODE_CHOOSE_TOUCH_CONTROL        29
+#define SETUP_MODE_CHOOSE_MOVE_DISTANCE        30
+#define SETUP_MODE_CHOOSE_DROP_DISTANCE        31
+#define SETUP_MODE_CHOOSE_TRANSPARENCY 32
+#define SETUP_MODE_CHOOSE_GRID_XSIZE_0 33
+#define SETUP_MODE_CHOOSE_GRID_YSIZE_0 34
+#define SETUP_MODE_CHOOSE_GRID_XSIZE_1 35
+#define SETUP_MODE_CHOOSE_GRID_YSIZE_1 36
+#define SETUP_MODE_CONFIG_VIRT_BUTTONS 37
+
+#define MAX_SETUP_MODES                        38
 
 #define MAX_MENU_MODES                 MAX(MAX_INFO_MODES, MAX_SETUP_MODES)
 
 #define STR_SETUP_CHOOSE_WINDOW_SIZE   "Window Scaling"
 #define STR_SETUP_CHOOSE_SCALING_TYPE  "Anti-Aliasing"
 #define STR_SETUP_CHOOSE_RENDERING     "Rendering Mode"
+#define STR_SETUP_CHOOSE_VSYNC         "VSync Mode"
 #define STR_SETUP_CHOOSE_VOLUME_SIMPLE "Sound Volume"
 #define STR_SETUP_CHOOSE_VOLUME_LOOPS  "Loops Volume"
 #define STR_SETUP_CHOOSE_VOLUME_MUSIC  "Music Volume"
@@ -265,6 +267,9 @@ static TreeInfo *scaling_type_current = NULL;
 static TreeInfo *rendering_modes = NULL;
 static TreeInfo *rendering_mode_current = NULL;
 
+static TreeInfo *vsync_modes = NULL;
+static TreeInfo *vsync_mode_current = NULL;
+
 static TreeInfo *scroll_delays = NULL;
 static TreeInfo *scroll_delay_current = NULL;
 
@@ -353,6 +358,19 @@ static struct
   {    NULL,                            NULL                            },
 };
 
+static struct
+{
+  char *value;
+  char *text;
+} vsync_modes_list[] =
+{
+  {    STR_VSYNC_MODE_OFF,             "Off"           },
+  {    STR_VSYNC_MODE_NORMAL,          "Normal"        },
+  {    STR_VSYNC_MODE_ADAPTIVE,        "Adaptive"      },
+
+  {    NULL,                            NULL           },
+};
+
 static struct
 {
   int value;
@@ -1415,13 +1433,13 @@ static void drawChooseTreeCursor(int ypos, boolean active)
   drawCursorExt(0, ypos, active, -1);
 }
 
-void DrawHeadline(void)
+static void DrawHeadline(void)
 {
   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, main_text_title_1);
   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2, main_text_title_2);
 }
 
-void DrawTitleScreenImage(int nr, boolean initial)
+static void DrawTitleScreenImage(int nr, boolean initial)
 {
   int graphic = getTitleScreenGraphic(nr, initial);
   Bitmap *bitmap = graphic_info[graphic].bitmap;
@@ -1465,7 +1483,7 @@ void DrawTitleScreenImage(int nr, boolean initial)
   redraw_mask = REDRAW_ALL;
 }
 
-void DrawTitleScreenMessage(int nr, boolean initial)
+static void DrawTitleScreenMessage(int nr, boolean initial)
 {
   char *filename = getLevelSetTitleMessageFilename(nr, initial);
   struct TitleMessageInfo *tmi = getTitleMessageInfo(nr, initial);
@@ -1516,14 +1534,14 @@ void DrawTitleScreenMessage(int nr, boolean initial)
   ResetFontStatus();
 }
 
-void DrawTitleScreen(void)
+static void DrawTitleScreen(void)
 {
   KeyboardAutoRepeatOff();
 
   HandleTitleScreen(0, 0, 0, 0, MB_MENU_INITIALIZE);
 }
 
-boolean CheckTitleScreen(boolean levelset_has_changed)
+static boolean CheckTitleScreen(boolean levelset_has_changed)
 {
   static boolean show_title_initial = TRUE;
   boolean show_titlescreen = FALSE;
@@ -1887,7 +1905,8 @@ void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
   }
 }
 
-void HandleMainMenu_SelectLevel(int step, int direction, int selected_level_nr)
+static void HandleMainMenu_SelectLevel(int step, int direction,
+                                      int selected_level_nr)
 {
   int old_level_nr = level_nr;
   int new_level_nr;
@@ -2367,8 +2386,9 @@ static void DrawInfoScreen_Main(void)
 
 static void changeSetupValue(int, int, int);
 
-void HandleMenuScreen(int mx, int my, int dx, int dy, int button,
-                     int mode, int num_page_entries, int max_page_entries)
+static void HandleMenuScreen(int mx, int my, int dx, int dy, int button,
+                            int mode, int num_page_entries,
+                            int max_page_entries)
 {
   static int num_page_entries_all_last[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
   static int choice_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
@@ -2885,7 +2905,7 @@ void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
                 TRUE, FALSE, FALSE);
 }
 
-void DrawInfoScreen_TitleScreen(void)
+static void DrawInfoScreen_TitleScreen(void)
 {
   SetGameStatus(GAME_MODE_TITLE);
 
@@ -2897,7 +2917,7 @@ void HandleInfoScreen_TitleScreen(int button)
   HandleTitleScreen(0, 0, 0, 0, button);
 }
 
-void DrawInfoScreen_Elements(void)
+static void DrawInfoScreen_Elements(void)
 {
   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
 
@@ -2990,7 +3010,7 @@ void HandleInfoScreen_Elements(int button)
   }
 }
 
-void DrawInfoScreen_Music(void)
+static void DrawInfoScreen_Music(void)
 {
   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
 
@@ -3401,7 +3421,7 @@ static void DrawInfoScreen_CreditsScreen(int screen_nr)
                    "Press any key or button for next page");
 }
 
-void DrawInfoScreen_Credits(void)
+static void DrawInfoScreen_Credits(void)
 {
   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
 
@@ -3471,7 +3491,7 @@ void HandleInfoScreen_Credits(int button)
   }
 }
 
-void DrawInfoScreen_Program(void)
+static void DrawInfoScreen_Program(void)
 {
   int font_title = MENU_INFO_FONT_TITLE;
   int font_head  = MENU_INFO_FONT_HEAD;
@@ -3556,7 +3576,7 @@ void HandleInfoScreen_Program(int button)
   }
 }
 
-void DrawInfoScreen_Version(void)
+static void DrawInfoScreen_Version(void)
 {
   int font_title = MENU_INFO_FONT_TITLE;
   int font_head  = MENU_INFO_FONT_HEAD;
@@ -3753,7 +3773,7 @@ void HandleInfoScreen_Version(int button)
   }
 }
 
-void DrawInfoScreen_LevelSet(void)
+static void DrawInfoScreen_LevelSet(void)
 {
   struct TitleMessageInfo *tmi = &readme;
   char *filename = getLevelSetInfoFilename();
@@ -3815,7 +3835,7 @@ void DrawInfoScreen_LevelSet(void)
   FadeIn(REDRAW_FIELD);
 }
 
-void HandleInfoScreen_LevelSet(int button)
+static void HandleInfoScreen_LevelSet(int button)
 {
   if (button == MB_MENU_LEAVE)
   {
@@ -4166,7 +4186,8 @@ static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
        execSetupGame();
       else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
               setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
-              setup_mode == SETUP_MODE_CHOOSE_RENDERING)
+              setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
+              setup_mode == SETUP_MODE_CHOOSE_VSYNC)
        execSetupGraphics();
       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
               setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
@@ -4323,7 +4344,8 @@ static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
            execSetupGame();
          else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
                   setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
-                  setup_mode == SETUP_MODE_CHOOSE_RENDERING)
+                  setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
+                  setup_mode == SETUP_MODE_CHOOSE_VSYNC)
            execSetupGraphics();
          else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
                   setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
@@ -4394,7 +4416,8 @@ static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
            execSetupGame();
          else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
                   setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
-                  setup_mode == SETUP_MODE_CHOOSE_RENDERING)
+                  setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
+                  setup_mode == SETUP_MODE_CHOOSE_VSYNC)
            execSetupGraphics();
          else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
                   setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
@@ -4665,6 +4688,7 @@ static int max_setup_info;        /* total number of setup entries in list */
 static char *window_size_text;
 static char *scaling_type_text;
 static char *rendering_mode_text;
+static char *vsync_mode_text;
 static char *scroll_delay_text;
 static char *snapshot_mode_text;
 static char *game_speed_text;
@@ -5062,6 +5086,56 @@ static void execSetupGraphics_setRenderingModes(void)
   rendering_mode_text = rendering_mode_current->name;
 }
 
+static void execSetupGraphics_setVsyncModes(void)
+{
+  if (vsync_modes == NULL)
+  {
+    int i;
+
+    for (i = 0; vsync_modes_list[i].value != NULL; i++)
+    {
+      TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
+      char identifier[32], name[32];
+      char *value = vsync_modes_list[i].value;
+      char *text = vsync_modes_list[i].text;
+
+      ti->node_top = &vsync_modes;
+      ti->sort_priority = i;
+
+      sprintf(identifier, "%s", value);
+      sprintf(name, "%s", text);
+
+      setString(&ti->identifier, identifier);
+      setString(&ti->name, name);
+      setString(&ti->name_sorting, name);
+      setString(&ti->infotext, STR_SETUP_CHOOSE_VSYNC);
+
+      pushTreeInfo(&vsync_modes, ti);
+    }
+
+    /* sort vsync mode values to start with lowest vsync mode value */
+    sortTreeInfo(&vsync_modes);
+
+    /* set current vsync mode value to configured vsync mode value */
+    vsync_mode_current =
+      getTreeInfoFromIdentifier(vsync_modes, setup.vsync_mode);
+
+    /* if that fails, set current vsync mode to reliable default value */
+    if (vsync_mode_current == NULL)
+      vsync_mode_current =
+       getTreeInfoFromIdentifier(vsync_modes, STR_VSYNC_MODE_DEFAULT);
+
+    /* if that also fails, set current vsync mode to first available one */
+    if (vsync_mode_current == NULL)
+      vsync_mode_current = vsync_modes;
+  }
+
+  setup.vsync_mode = vsync_mode_current->identifier;
+
+  /* needed for displaying vsync mode text instead of identifier */
+  vsync_mode_text = vsync_mode_current->name;
+}
+
 static void execSetupGraphics(void)
 {
   // update "setup.window_scaling_percent" from list selection
@@ -5075,6 +5149,7 @@ static void execSetupGraphics(void)
 
   execSetupGraphics_setScalingTypes();
   execSetupGraphics_setRenderingModes();
+  execSetupGraphics_setVsyncModes();
 
   setup_mode = SETUP_MODE_GRAPHICS;
 
@@ -5090,6 +5165,9 @@ static void execSetupGraphics(void)
 
   // screen rendering mode may have changed at this point
   SDLSetScreenRenderingMode(setup.screen_rendering_mode);
+
+  // screen vsync mode may have changed at this point
+  SDLSetScreenVsyncMode(setup.vsync_mode);
 #endif
 }
 
@@ -5114,6 +5192,13 @@ static void execSetupChooseRenderingMode(void)
   DrawSetupScreen();
 }
 
+static void execSetupChooseVsyncMode(void)
+{
+  setup_mode = SETUP_MODE_CHOOSE_VSYNC;
+
+  DrawSetupScreen();
+}
+
 static void execSetupChooseVolumeSimple(void)
 {
   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
@@ -5816,6 +5901,9 @@ static struct
   { &setup.screen_rendering_mode,      execSetupChooseRenderingMode    },
   { &setup.screen_rendering_mode,      &rendering_mode_text            },
 
+  { &setup.vsync_mode,                 execSetupChooseVsyncMode        },
+  { &setup.vsync_mode,                 &vsync_mode_text                },
+
   { &setup.graphics_set,               execSetupChooseGraphics         },
   { &setup.graphics_set,               &graphics_set_name              },
 
@@ -5964,6 +6052,8 @@ static struct TokenInfo setup_info_graphics[] =
   { TYPE_ENTER_LIST,   execSetupChooseScrollDelay, "Scroll Delay:"     },
   { TYPE_STRING,       &scroll_delay_text,     ""                      },
 #endif
+  { TYPE_ENTER_LIST,   execSetupChooseVsyncMode, "Vertical Sync (VSync):" },
+  { TYPE_STRING,       &vsync_mode_text,       ""                      },
   { TYPE_SWITCH,       &setup.fade_screens,    "Fade Screens:"         },
   { TYPE_SWITCH,       &setup.quick_switch,    "Quick Player Focus Switch:" },
   { TYPE_SWITCH,       &setup.quick_doors,     "Quick Menu Doors:"     },
@@ -6643,7 +6733,7 @@ void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
                   setup_mode, num_setup_info, max_setup_info);
 }
 
-void DrawSetupScreen_Input(void)
+static void DrawSetupScreen_Input(void)
 {
   int i;
 
@@ -6797,7 +6887,7 @@ static void drawPlayerSetupInputInfo(int player_nr, boolean active)
 
 static int input_player_nr = 0;
 
-void HandleSetupScreen_Input_Player(int step, int direction)
+static void HandleSetupScreen_Input_Player(int step, int direction)
 {
   int old_player_nr = input_player_nr;
   int new_player_nr;
@@ -7551,7 +7641,7 @@ void ConfigureJoystick(int player_nr)
   DrawSetupScreen_Input();
 }
 
-boolean ConfigureVirtualButtonsMain(void)
+static boolean ConfigureVirtualButtonsMain(void)
 {
   static char *customize_step_text[] =
   {
@@ -7831,6 +7921,8 @@ void DrawSetupScreen(void)
     DrawChooseTree(&scaling_type_current);
   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
     DrawChooseTree(&rendering_mode_current);
+  else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
+    DrawChooseTree(&vsync_mode_current);
   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
     DrawChooseTree(&artwork.gfx_current);
   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
@@ -7911,6 +8003,8 @@ void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
     HandleChooseTree(mx, my, dx, dy, button, &rendering_mode_current);
+  else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
+    HandleChooseTree(mx, my, dx, dy, button, &vsync_mode_current);
   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
@@ -8333,7 +8427,7 @@ void MapScreenMenuGadgets(int screen_mask)
       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
 }
 
-void UnmapScreenMenuGadgets(int screen_mask)
+static void UnmapScreenMenuGadgets(int screen_mask)
 {
   int i;