unit uMAMESettings;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, GR32_Image, ImgList, ComCtrls, Buttons, IniFiles,
  ExtCtrls, GR32_RangeBars, uGR32Extra;

type
  TScreenInfo = record
    eScreenName,
    eScreenAspectRatio,
    eScreenResolution,
    eScreenRefreshRate,
    eScreenView: String;
  end;

type
  TFormMAMESettings = class(TForm)
    TopImage: TImage32;
    LabelGameDescription: TLabel;
    PageControlGeneral: TPageControl;
    TabSheetFolders: TTabSheet;
    TabSheetOptions: TTabSheet;
    IL_MAMESettings: TImageList;
    ButtonOk: TButton;
    ButtonCancel: TButton;
    ReadFile: TButton;
    PageControlOptions: TPageControl;
    TabSheetVideo1: TTabSheet;
    TabSheetVideo2: TTabSheet;
    ButtonDefault: TButton;
    WindowMode: TCheckBox;
    MaximizeWindow: TCheckBox;
    KeepAspectRatio: TCheckBox;
    TabSheetInput: TTabSheet;
    Mouse: TCheckBox;
    Joystick: TCheckBox;
    LightGun: TCheckBox;
    DualLightGun: TCheckBox;
    LightGunOffScreenReload: TCheckBox;
    SteadyKey: TCheckBox;
    LabelControllerDefinitions: TLabel;
    ControllerDefinitions: TComboBox;
    LabelAnalogSettings: TGroupBox;
    LabelAnalog_Paddle: TLabel;
    LabelAnalog_Joystick: TLabel;
    LabelAnalog_LightGun: TLabel;
    LabelAnalog_Pedal: TLabel;
    LabelAnalog_Dial: TLabel;
    LabelAnalog_Trackball: TLabel;
    LabelAnalog_Digital: TLabel;
    LabelAnalog_Digital_Custom: TLabel;
    Analog_Paddle: TComboBox;
    Analog_Joystick: TComboBox;
    Analog_LightGun: TComboBox;
    Analog_Pedal: TComboBox;
    Analog_Dial: TComboBox;
    Analog_Trackball: TComboBox;
    Analog_Digital: TComboBox;
    Analog_Digital_Custom: TEdit;
    ArtworkGroupBox: TGroupBox;
    ArtworkBackdrops: TCheckBox;
    ArtworkBezels: TCheckBox;
    ArtworkOverlays: TCheckBox;
    ArtworkCrop: TCheckBox;
    TabSheetMiscellaneous: TTabSheet;
    SkipGameInfo: TCheckBox;
    Cheat: TCheckBox;
    LabelSystemBios: TLabel;
    BiosSets: TListView;
    AutoSave: TCheckBox;
    DebugGroupBox: TGroupBox;
    Log: TCheckBox;
    OSLog: TCheckBox;
    Verbose: TCheckBox;
    ReadConfigFiles: TCheckBox;
    CheatFile: TEdit;
    LabelCheatFile: TLabel;
    CheatFileBrowse: TButton;
    TabSheetAudio: TTabSheet;
    Sound: TCheckBox;
    Samples: TCheckBox;
    LabelSampleRate: TLabel;
    SampleRate: TComboBox;
    LabelAudioLatency: TLabel;
    VectorGroupBox: TGroupBox;
    Antialias: TCheckBox;
    LabelScreenRotation: TGroupBox;
    FlipX: TCheckBox;
    FlipY: TCheckBox;
    ImageGameIcon: TImage;
    Debugger: TCheckBox;
    LabelDebuggerScript: TLabel;
    DebuggerScript: TEdit;
    DebuggerScriptBrowse: TButton;
    Rotate: TCheckBox;
    RotateRight: TCheckBox;
    RotateLeft: TCheckBox;
    AutoRotateRight: TCheckBox;
    AutoRotateLeft: TCheckBox;
    PageControlFolders: TPageControl;
    TabSheetFoldersPage1: TTabSheet;
    TabSheetFoldersPage2: TTabSheet;
    LabelFolderROMsDisks: TLabel;
    FolderROMsDisksButtonSelect: TButton;
    FolderROMsDisksButtonClear: TButton;
    FolderROMsDisksAddSubFolders: TCheckBox;
    LabelFolderSamples: TLabel;
    FolderSamples: TEdit;
    FolderSamplesButtonSelect: TButton;
    LabelFolderIniFiles: TLabel;
    FolderIniFiles: TEdit;
    FolderIniFilesButtonSelect: TButton;
    FolderGamesConfigurationsButtonSelect: TButton;
    FolderGamesConfigurations: TEdit;
    LabelFolderGamesConfigurations: TLabel;
    FolderMemoryCardsButtonSelect: TButton;
    FolderMemoryCards: TEdit;
    LabelFolderMemoryCards: TLabel;
    FolderInputDeviceLogsButtonSelect: TButton;
    FolderInputDeviceLogs: TEdit;
    LabelFolderInputDeviceLogs: TLabel;
    FolderSaveStatesButtonSelect: TButton;
    FolderSaveStates: TEdit;
    LabelFolderSaveStates: TLabel;
    FolderArtworksButtonSelect: TButton;
    FolderArtworks: TEdit;
    LabelFolderArtworks: TLabel;
    FolderGameSnapshotsButtonSelect: TButton;
    FolderGameSnapshots: TEdit;
    LabelFolderGameSnapshots: TLabel;
    FolderDiffButtonSelect: TButton;
    FolderDiff: TEdit;
    LabelFolderDiff: TLabel;
    FolderControllerDefinitionsButtonSelect: TButton;
    FolderControllerDefinitions: TEdit;
    LabelFolderControllerDefinitions: TLabel;
    FolderDebuggerCommentsButtonSelect: TButton;
    FolderDebuggerComments: TEdit;
    LabelFolderDebuggerComments: TLabel;
    FolderROMsHardDiskImages: TListBox;
    ReadFileIni: TEdit;
    FolderNVRAM: TEdit;
    FolderNVRAMButtonSelect: TButton;
    LabelFolderNVRAM: TLabel;
    FolderFontFiles: TEdit;
    Label3: TLabel;
    FolderFontFilesButtonSelect: TButton;
    LabelEmuVersionInfo: TLabel;
    LabelAnalog_Positional: TLabel;
    Analog_Positional: TComboBox;
    LabelJoystickDeadzone: TGroupBox;
    LabelJoystickDeadzoneValue: TLabel;
    JoystickDeadzone: TGaugeBar2;
    LabelVolume: TGroupBox;
    Volume: TGaugeBar;
    LabelVolumeValue: TLabel;
    AudioLatency: TComboBox;
    LabelBrightness: TGroupBox;
    Brightness: TGaugeBar2;
    LabelBrightnessValue: TLabel;
    LabelContrast: TGroupBox;
    Contrast: TGaugeBar2;
    LabelContrastValue: TLabel;
    LabelGamma: TGroupBox;
    LabelGammaValue: TLabel;
    Gamma: TGaugeBar2;
    LabelPauseBrightness: TGroupBox;
    LabelPauseBrightnessValue: TLabel;
    PauseBrightness: TGaugeBar2;
    LabelJoystickSaturation: TGroupBox;
    LabelJoystickSaturationValue: TLabel;
    JoystickSaturation: TGaugeBar2;
    LabelVectorBeamWidth: TGroupBox;
    LabelVectorBeamWidthValue: TLabel;
    VectorBeamWidth: TGaugeBar2;
    LabelVectorFlickerEffect: TGroupBox;
    LabelVectorFlickerEffectValue: TLabel;
    VectorFlickerEffect: TGaugeBar2;
    LabelEffectOverlay: TLabel;
    EffectOverlay: TEdit;
    EffectOverlayBrowse: TButton;
    TabSheetVideo3: TTabSheet;
    ScreensBox: TGroupBox;
    LabelScreenName: TLabel;
    ScreenName: TComboBox;
    ScreenName_Custom: TEdit;
    LabelScreenAspectRatio: TLabel;
    ScreenAspectRatio: TComboBox;
    ScreenAspectRatio_Custom: TEdit;
    LabelScreenResolution: TLabel;
    ScreenResolution: TComboBox;
    ScreenResolution_Custom: TEdit;
    ScreenRefreshRate_Custom: TComboBox;
    LabelScreenRefreshRate_Custom: TLabel;
    LabelScreenView: TLabel;
    ScreenView: TComboBox;
    ScreenView_Custom: TEdit;
    LabelScreensSelector: TLabel;
    LabelNumberScreens: TGroupBox;
    NumberScreens: TComboBox;
    LabelVideoOutputMode: TLabel;
    VideoOutputMode: TComboBox;
    LabelPrescale_ScaleScreen: TGroupBox;
    LabelPrescale_ScaleScreenValue: TLabel;
    Prescale_ScaleScreen: TGaugeBar;
    WaitVerticalSync: TCheckBox;
    SyncronizeRefreshRate: TCheckBox;
    DirectDrawGroupBox: TGroupBox;
    HardwareStretch: TCheckBox;
    Direct3DGroupBox: TGroupBox;
    BilinearFilter: TCheckBox;
    Direct3DVersion: TComboBox;
    LabelSecondsToRun: TGroupBox;
    LabelSecondsToRunValue: TLabel;
    GroupBox3: TGroupBox;
    LabelFullScreenBrightness: TGroupBox;
    LabelFullScreenBrightnessValue: TLabel;
    FullScreenBrightness: TGaugeBar2;
    LabelFullScreenContrast: TGroupBox;
    LabelFullScreenContrastValue: TLabel;
    FullScreenContrast: TGaugeBar2;
    LabelFullScreenGamma: TGroupBox;
    LabelFullScreenGammaValue: TLabel;
    FullScreenGamma: TGaugeBar2;
    TripleBuffer: TCheckBox;
    SwitchResolution: TCheckBox;
    GroupBox1: TGroupBox;
    AutoFrameskip: TCheckBox;
    Throttle: TCheckBox;
    Sleep: TCheckBox;
    Multithreading: TCheckBox;
    LabelFrameskip: TGroupBox;
    LabelFrameskipValue: TLabel;
    Frameskip: TGaugeBar;
    LabelThreadPriority: TGroupBox;
    LabelThreadPriorityValue: TLabel;
    ThreadPriority: TGaugeBar;
    SecondsToRun: TGaugeBar;
    ScreensSelector: TComboBox;
    procedure ButtonCancelClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure ReadFileClick(Sender: TObject);
    procedure EffectOverlayBrowseClick(Sender: TObject);
    procedure FolderROMsDisksButtonSelectClick(Sender: TObject);
    procedure FolderSamplesButtonSelectClick(Sender: TObject);
    procedure FolderIniFilesButtonSelectClick(Sender: TObject);
    procedure FolderGamesConfigurationsButtonSelectClick(Sender: TObject);
    procedure FolderMemoryCardsButtonSelectClick(Sender: TObject);
    procedure FolderInputDeviceLogsButtonSelectClick(Sender: TObject);
    procedure FolderSaveStatesButtonSelectClick(Sender: TObject);
    procedure FolderArtworksButtonSelectClick(Sender: TObject);
    procedure FolderGameSnapshotsButtonSelectClick(Sender: TObject);
    procedure FolderDiffButtonSelectClick(Sender: TObject);
    procedure FolderControllerDefinitionsButtonSelectClick(
      Sender: TObject);
    procedure FolderDebuggerCommentsButtonSelectClick(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure CheatFileBrowseClick(Sender: TObject);
    procedure DebuggerScriptBrowseClick(Sender: TObject);
    procedure ScreenName1Select(Sender: TObject);
    procedure FolderROMsDisksButtonClearClick(Sender: TObject);
    procedure FolderROMsHardDiskImagesKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FolderFontFilesButtonSelectClick(Sender: TObject);
    procedure VolumeChange(Sender: TObject);
    procedure JoystickDeadzoneChange(Sender: TObject);
    procedure BrightnessChange(Sender: TObject);
    procedure ContrastChange(Sender: TObject);
    procedure GammaChange(Sender: TObject);
    procedure PauseBrightnessChange(Sender: TObject);
    procedure LabelJoystickSaturationValueClick(Sender: TObject);
    procedure Prescale_ScaleScreenChange(Sender: TObject);
    procedure FullScreenBrightnessChange(Sender: TObject);
    procedure FullScreenContrastChange(Sender: TObject);
    procedure FullScreenGammaChange(Sender: TObject);
    procedure VectorBeamWidthChange(Sender: TObject);
    procedure VectorFlickerEffectChange(Sender: TObject);
    procedure FrameskipChange(Sender: TObject);
    procedure ThreadPriorityChange(Sender: TObject);
    procedure SecondsToRunChange(Sender: TObject);
    procedure ScreenNameSelect(Sender: TObject);
    procedure ScreenAspectRatioSelect(Sender: TObject);
    procedure ScreenResolutionSelect(Sender: TObject);
    procedure ScreenViewSelect(Sender: TObject);
    procedure ScreensSelectorSelect(Sender: TObject);
    procedure ScreenName_CustomChange(Sender: TObject);
    procedure ScreenAspectRatio_CustomChange(Sender: TObject);
    procedure ScreenResolution_CustomChange(Sender: TObject);
    procedure ScreenRefreshRate_CustomSelect(Sender: TObject);
    procedure ScreenView_CustomChange(Sender: TObject);
  private
    { Private declarations }
    ScreenDetails: packed array[-1..3] of TScreenInfo;
    procedure LoadBiosList(SelectBiosName: String);
    procedure ReadMAMEIniFile(const EmulatorFileName, IniFile: String; CustomGameOption: Boolean);
    procedure WriteMAMEIniFile(const EmulatorFileName, emuIni, customIni: String; CustomGameOption: Boolean);
  public
    { Public declarations }
    EmulatorString, emuIni: String;
    SaveCfg: Boolean;
    GameName: String;
    sysID: ShortInt;
  end;

var
  FormMAMESettings: TFormMAMESettings;


implementation

uses uMain, uCommon;

{$R *.dfm}

procedure TFormMAMESettings.ReadMAMEIniFile(const EmulatorFileName, IniFile: String; CustomGameOption: Boolean);
var
  TextLine, Value, EntryString: String;
  MAMEIniFile, ctrlrDefinitionsList: THashedStringList;
  Loop, Loop2: Integer;

  function SetComboBoxItemZero(ComboBoxHolder: TComboBox): Boolean;
  begin
    if ComboBoxHolder.ItemIndex = -1 then
       ComboBoxHolder.ItemIndex:= 0;
  end;

  function SetResolutionOption(nScreenIdx: ShortInt): Boolean;
  var
    Loop, Position: Integer;
    cResolution, cRefreshRate, TempString: String;
  begin
    Value:= ExtractMAMEIniValue(TextLine);
    cResolution:= '';
    cRefreshRate:= '';
    if LowerCase(Value) <> 'auto' then
       begin
         Position:= Pos('@', Value);
         if Position <> 0 then
            cRefreshRate:= Copy(Value, Position+1, Length(Value)-Position);
         TempString:= '';
         for Loop:= 1 to Length(Value) do
         begin
           if (Loop = Length(Value)) then
              TempString:= TempString+Value[Loop];
           if (Value[Loop] = '@') or (Loop = Length(Value)) then
              begin
                cResolution:= TempString;
                Break;
              end
           else
              TempString:= TempString+Value[Loop];
         end;
       end
    else
       cResolution:= 'auto';

    ScreenDetails[nScreenIdx].eScreenResolution:= cResolution;
    ScreenDetails[nScreenIdx].eScreenRefreshRate:= cRefreshRate;
  end;

begin
  if FileExists(IniFile) then
     begin
       ThousandSeparator:= Char(',');
       DecimalSeparator:= Char('.');
       MAMEIniFile:= THashedStringList.Create;
       MAMEIniFile.LoadFromFile(IniFile);
       for Loop:=0 to MAMEIniFile.Count -1 do
       begin
         TextLine:= MAMEIniFile[Loop];
         with FormMAMESettings do
         begin
           if TextLine <> '' then
              begin
                EntryString:= XML_GetEntryName(TextLine);
                if EntryString <> '' then
                   EntryString:= EntryString+' ';
                // CONFIGURATION OPTIONS
                if (EntryString = 'readconfig ') or
                   (EntryString = 'rc ') then
                   ReadConfigFiles.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'skip_gameinfo ' then
                   SkipGameInfo.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // PATH AND DIRECTORY OPTIONS
                if (EntryString = 'rompath ') or
                   (EntryString = 'rp ') then
                   FormMain.ExtractROMsFolders(ExtractMAMEIniValue(TextLine), sysID, FolderROMsHardDiskImages)
                else
                if (EntryString = 'samplepath ') or
                   (EntryString = 'sp ') then
                   FolderSamples.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'inipath ' then
                   begin
                     FolderIniFiles.Text:= ExtractMAMEIniValue(TextLine);
                     if FolderIniFiles.Text = '.;ini' then
                        FolderIniFiles.Text:= 'ini';
                   end
                else
                if EntryString = 'fontpath ' then
                   FolderFontFiles.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'cfg_directory ' then
                   FolderGamesConfigurations.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'nvram_directory ' then
                   FolderNVRAM.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'memcard_directory ' then
                   FolderMemoryCards.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'input_directory ' then
                   FolderInputDeviceLogs.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'state_directory ' then
                   FolderSaveStates.Text:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'artpath ') or
                   (EntryString = 'artwork_directory ') then
                   FolderArtworks.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'snapshot_directory ' then
                   FolderGameSnapshots.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'diff_directory ' then
                   FolderDiff.Text:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'ctrlrpath ') or
                   (EntryString = 'ctrlr_directory ') then
                   begin
                     FolderControllerDefinitions.Text:= ExtractMAMEIniValue(TextLine);
                     ctrlrDefinitionsList:= THashedStringList.Create;
                     if (Pos(':\', FolderControllerDefinitions.Text) > 0) or (Pos(':', FolderControllerDefinitions.Text) > 0) then
                        GenerateControllerDefinitionsFilesList(FolderControllerDefinitions.Text, ctrlrDefinitionsList, True)
                     else
                        GenerateControllerDefinitionsFilesList(ExtractFilePath(EmulatorFileName)+FolderControllerDefinitions.Text, ctrlrDefinitionsList, True);
                     if ctrlrDefinitionsList.Count > 0 then
                        begin
                          Loop2:= ctrlrDefinitionsList.IndexOf('standard');
                          if Loop2 <> -1 then
                             ctrlrDefinitionsList.Delete(Loop2);
                          ControllerDefinitions.Items.AddStrings(ctrlrDefinitionsList);
                        end;
                     FreeAndNil(ctrlrDefinitionsList);
                   end
                else
                if EntryString = 'comment_directory ' then
                   FolderDebuggerComments.Text:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'cheat_file ' then
                   CheatFile.Text:= ExtractMAMEIniValue(TextLine)
                else
                // MISC OPTIONS
                if EntryString = 'bios ' then
                   begin
                     // select correct bios
                     if BiosSets.Items.Count > 0 then
                        begin
                          Value:= ExtractMAMEIniValue(TextLine);
                          if Value = '' then
                             Value:= 'default';
                          if Value = 'default' then
                             BiosSets.ItemIndex:= 0
                          else
                             begin
                                BiosSets.ItemIndex:= -1;
                                for Loop2:=0 to BiosSets.Items.Count-1 do
                                begin
                                  if BiosSets.Items[Loop2].SubItems[1] = Value then
                                     begin
                                       BiosSets.ItemIndex:= Loop2;
                                       Break;
                                     end;
                                end;
                                if BiosSets.ItemIndex = -1 then
                                   begin
                                     for Loop2:=0 to BiosSets.Items.Count-1 do
                                     begin
                                       if BiosSets.Items[Loop2].SubItems[0] = Value then
                                          begin
                                            BiosSets.ItemIndex:= Loop2;
                                            Break;
                                          end;
                                     end;
                                   end;
                                if BiosSets.ItemIndex = -1 then
                                   BiosSets.ItemIndex:= 0;
                             end;
                        end;
                   end
                else
                if (EntryString = 'cheat ') or
                   (EntryString = 'c ') then
                   Cheat.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // STATE/PLAYBACK OPTIONS
                if EntryString = 'autosave ' then
                   AutoSave.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // DEBUGGING OPTIONS
                if EntryString = 'log ' then
                   Log.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'oslog ' then
                   OSLog.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'verbose ') or
                   (EntryString = 'v ') then
                   Verbose.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'debug ') or
                   (EntryString = 'd ') then
                   Debugger.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if XML_CheckData(TextLine, '# debugscript ') then
                   DebuggerScript.Text:= ''
                else
                if EntryString = 'debugscript ' then
                   DebuggerScript.Text:= ExtractMAMEIniValue(TextLine)
                else
                // PERFORMANCE OPTIONS
                if (EntryString = 'autoframeskip ') or
                   (EntryString = 'afs ') then
                   AutoFrameSkip.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'frameskip ') or
                   (EntryString = 'fs ') then
                   FrameSkip.Position:= StrToInt(ExtractMAMEIniValue(TextLine))
                else
                if (EntryString = 'seconds_to_run ') or
                   (EntryString = 'str ') then
                   SecondsToRun.Position:= StrToInt(ExtractMAMEIniValue(TextLine))
                else
                if EntryString = 'throttle ' then
                   Throttle.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'sleep ' then
                   Sleep.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'priority ' then
                   ThreadPriority.Position:= StrToInt(ExtractMAMEIniValue(TextLine))
                else
                if (EntryString = 'multithreading ') or
                   (EntryString = 'mt ') then
                   Multithreading.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // VIDEO OPTIONS
                if EntryString = 'video ' then
                   begin
                     Value:= ExtractMAMEIniValue(TextLine);
                     if Value = 'gdi' then
                        VideoOutputMode.ItemIndex:= 0
                     else
                     if Value = 'ddraw' then
                        VideoOutputMode.ItemIndex:= 1
                     else
                     if Value = 'd3d' then
                        VideoOutputMode.ItemIndex:= 2
                     else
                     if Value = 'none' then
                        VideoOutputMode.ItemIndex:= 3
                     else
                        VideoOutputMode.ItemIndex:= 2;
                   end
                else
                if EntryString = 'numscreens ' then
                   begin
                     NumberScreens.ItemIndex:= StrToInt(ExtractMAMEIniValue(TextLine))-1;
                     SetComboBoxItemZero(NumberScreens);
                   end
                else
                if (EntryString = 'window ') or
                   (EntryString = 'w ') then
                   WindowMode.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'maximize ') or
                   (EntryString = 'max ') then
                   MaximizeWindow.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'keepaspect ') or
                   (EntryString = 'ka ') then
                   KeepAspectRatio.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'prescale ' then
                   Prescale_ScaleScreen.Position:= StrToInt((ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'effect ' then
                   begin
                     EffectOverlay.Text:= ExtractMAMEIniValue(TextLine);
                     if EffectOverlay.Text = 'none' then
                        EffectOverlay.Text:= '';
                   end
                else
                if EntryString = 'pause_brightness ' then
                   PauseBrightness.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if EntryString = 'waitvsync ' then
                   WaitVerticalSync.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'syncrefresh ' then
                   SyncronizeRefreshRate.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // VIDEO ROTATION OPTIONS
                if EntryString = 'rotate ' then
                   Rotate.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'ror ' then
                   RotateRight.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'rol ' then
                   RotateLeft.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'autoror ' then
                   AutoRotateRight.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'autorol ' then
                   AutoRotateLeft.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'flipx ' then
                   FlipX.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'flipy ' then
                   FlipY.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // DIRECTDRAW-SPECIFIC OPTIONS
                if (EntryString = 'hwstretch ') or
                   (EntryString = 'hws ') then
                   HardwareStretch.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // DIRECT3D-SPECIFIC OPTIONS
                if EntryString = 'd3dversion ' then
                   Direct3DVersion.ItemIndex:= (StrToInt(ExtractMAMEIniValue(TextLine)))-8
                else
                if (EntryString = 'filter ') or
                   (EntryString = 'd3dfilter ') or
                   (EntryString = 'flt ') then
                   BilinearFilter.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // PER-WINDOW VIDEO OPTIONS
                if EntryString = 'screen ' then
                   ScreenDetails[-1].eScreenName:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'aspect ') or
                   (EntryString = 'screen_aspect ') then
                   ScreenDetails[-1].eScreenAspectRatio:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'resolution ') or
                   (EntryString = 'r ') then
                   SetResolutionOption(-1)
                else
                if EntryString = 'view ' then
                   ScreenDetails[-1].eScreenView:= ExtractMAMEIniValue(TextLine)
                else

                if EntryString = 'screen0 ' then
                   ScreenDetails[0].eScreenName:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'aspect0 ' then
                   ScreenDetails[0].eScreenAspectRatio:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'resolution0 ') or
                   (EntryString = 'r0 ') then
                   SetResolutionOption(0)
                else
                if EntryString = 'view0 ' then
                   ScreenDetails[0].eScreenView:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'screen1 ' then
                   ScreenDetails[1].eScreenName:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'aspect1 ' then
                   ScreenDetails[1].eScreenAspectRatio:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'resolution1 ') or
                   (EntryString = 'r1 ') then
                   SetResolutionOption(1)
                else
                if EntryString = 'view1 ' then
                   ScreenDetails[1].eScreenView:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'screen2 ' then
                   ScreenDetails[2].eScreenName:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'aspect2 ' then
                   ScreenDetails[2].eScreenAspectRatio:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'resolution2 ') or
                   (EntryString = 'r2 ') then
                   SetResolutionOption(2)
                else
                if EntryString = 'view2 ' then
                   ScreenDetails[2].eScreenView:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'screen3 ' then
                   ScreenDetails[3].eScreenName:= ExtractMAMEIniValue(TextLine)
                else
                if EntryString = 'aspect3 ' then
                   ScreenDetails[3].eScreenAspectRatio:= ExtractMAMEIniValue(TextLine)
                else
                if (EntryString = 'resolution3 ') or
                   (EntryString = 'r3 ') then
                   SetResolutionOption(3)
                else
                if EntryString = 'view3 ' then
                   ScreenDetails[3].eScreenView:= ExtractMAMEIniValue(TextLine)
                else
                // FULL SCREEN OPTIONS
                if (EntryString = 'triplebuffer ') or
                   (EntryString = 'tb ') then
                   TripleBuffer.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'switchres ' then
                   SwitchResolution.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'full_screen_brightness ') or
                   (EntryString = 'fsb ') then
                   FullScreenBrightness.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if (EntryString = 'full_screen_contrast ') or
                   (EntryString = 'fsc ') then
                   FullScreenContrast.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if (EntryString = 'full_screen_gamma ') or
                   (EntryString = 'fsg ') then
                   FullScreenGamma.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                // GAME SCREEN OPTIONS
                if EntryString = 'brightness ' then
                   Brightness.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if EntryString = 'contrast ' then
                   Contrast.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if EntryString = 'gamma ' then
                   Gamma.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                // VECTOR RENDERING OPTIONS
                if (EntryString = 'antialias ') or
                   (EntryString = 'aa ') then
                   Antialias.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if EntryString = 'beam ' then
                   VectorBeamWidth.Position:= StrToFloat(Format('%2.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if EntryString = 'flicker ' then
                   VectorFlickerEffect.Position:= StrToFloat(Format('%3.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                // ARTWORK OPTIONS
                if (EntryString = 'artwork_crop ') or
                   (EntryString = 'artcrop ') then
                   ArtworkCrop.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'use_backdrops ') or
                   (EntryString = 'backdrop ') then
                   ArtworkBackdrops.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'use_overlays ') or
                   (EntryString = 'overlay ') then
                   ArtworkOverlays.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'use_bezels ') or
                   (EntryString = 'bezel ') then
                   ArtworkBezels.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                // SOUND OPTIONS
                if EntryString = 'sound ' then
                   Sound.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'samplerate ') or
                   (EntryString = 'sr ') then
                   begin
                     Value:= ExtractMAMEIniValue(TextLine);
                     case StrToInt(Value) of
                       11025 : SampleRate.ItemIndex:= 0;
                       22050 : SampleRate.ItemIndex:= 1;
                       44100 : SampleRate.ItemIndex:= 2;
                       48000 : SampleRate.ItemIndex:= 3;
                       96000 : SampleRate.ItemIndex:= 4;
                       192000: SampleRate.ItemIndex:= 5;
                       else    SampleRate.ItemIndex:= 3;
                     end;
                   end
                else
                if EntryString = 'samples ' then
                   Samples.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'volume ') or
                   (EntryString = 'vol ') then
                   Volume.Position:= StrToInt(ExtractMAMEIniValue(TextLine))
                else
                if EntryString = 'audio_latency ' then
                   AudioLatency.ItemIndex:= StrToInt(ExtractMAMEIniValue(TextLine))-1
                else
                // INPUT DEVICE OPTIONS
                if XML_CheckData(TextLine, '# ctrlr ') then
                   ControllerDefinitions.ItemIndex:= 0
                else
                if EntryString = 'ctrlr ' then
                    begin
                      ControllerDefinitions.ItemIndex:= ControllerDefinitions.Items.IndexOf(ExtractMAMEiniValue(TextLine));
                      SetComboBoxItemZero(ControllerDefinitions);
                    end
                else
                if EntryString = 'mouse ' then
                   Mouse.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'joystick ') or
                   (EntryString = 'joy ') then
                   Joystick.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'lightgun ') or
                   (EntryString = 'gun ') then
                   Lightgun.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'dual_lightgun ') or
                   (EntryString = 'dual ') then
                   DualLightGun.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'offscreen_reload ') or
                   (EntryString = 'reload ') then
                   LightGunOffScreenReload.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'steadykey ') or
                   (EntryString = 'steady ') then
                   SteadyKey.Checked:= Boolean(StrToInt(ExtractMAMEIniValue(TextLine)))
                else
                if (EntryString = 'joy_deadzone ') or
                   (EntryString = 'jdz ') then
                   JoystickDeadzone.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if (EntryString = 'joy_saturation ') or
                   (EntryString = 'jsat ') then
                   JoystickSaturation.Position:= StrToFloat(Format('%1.2f', [StrToFloat(ExtractMAMEIniValue(TextLine))]))
                else
                if EntryString = 'digital ' then
                   begin
                     Value:= ExtractMAMEIniValue(TextLine);
                     Analog_Digital.ItemIndex:= Analog_Digital.Items.IndexOf(Value);
                     if Analog_Digital.ItemIndex = -1 then
                        begin
                          Analog_Digital.ItemIndex:= 2;
                          Analog_Digital_Custom.Text:= Value;
                        end;
                   end
                else
                // AUTOMATIC DEVICE SELECTION OPTIONS
                if (EntryString = 'paddle_device ') or
                   (EntryString = 'paddle ') then
                   begin
                     Analog_Paddle.ItemIndex:= Analog_Paddle.Items.IndexOf(ExtractMAMEIniValue(TextLine));
                     SetComboBoxItemZero(Analog_Paddle);
                   end
                else
                if (EntryString = 'adstick_device ') or
                   (EntryString = 'adstick ') then
                   begin
                     Analog_Joystick.ItemIndex:= Analog_Joystick.Items.IndexOf(ExtractMAMEIniValue(TextLine));
                     SetComboBoxItemZero(Analog_Joystick);
                   end
                else
                if (EntryString = 'pedal_device ') or
                   (EntryString = 'pedal ') then
                   begin
                     Analog_Pedal.ItemIndex:= Analog_Pedal.Items.IndexOf(ExtractMAMEIniValue(TextLine));
                     SetComboBoxItemZero(Analog_Pedal);
                   end
                else
                if (EntryString = 'dial_device ') or
                   (EntryString = 'dial ') then
                   begin
                     Analog_Dial.ItemIndex:= Analog_Dial.Items.IndexOf(ExtractMAMEIniValue(TextLine));
                     SetComboBoxItemZero(Analog_Dial);
                   end
                else
                if (EntryString = 'trackball_device ') or
                   (EntryString = 'trackball ') then
                   begin
                     Analog_Trackball.ItemIndex:= Analog_Trackball.Items.IndexOf(ExtractMAMEIniValue(TextLine));
                     SetComboBoxItemZero(Analog_Trackball);
                   end
                else
                if EntryString = 'lightgun_device ' then
                   begin
                     Analog_LightGun.ItemIndex:= Analog_LightGun.Items.IndexOf(ExtractMAMEIniValue(TextLine));
                     SetComboBoxItemZero(Analog_LightGun);
                   end
                else
                if EntryString = 'positional_device ' then
                   begin
                     Analog_Positional.ItemIndex:= Analog_Positional.Items.IndexOf(ExtractMAMEIniValue(TextLine));
                     SetComboBoxItemZero(Analog_Positional);
                   end;
              end;
         end;
       end;
       FreeAndNil(MAMEIniFile);
     end;
end;

procedure TFormMAMESettings.WriteMAMEIniFile(const EmulatorFileName, emuIni, customIni: String; CustomGameOption: Boolean);
var
  MAMEIniFile, GameIniFile: THashedStringList;
  Loop: Integer;
  Value, tmpEntryStr, EntryString: String;

  //if (data->data != NULL)
	//   fprintf(inifile, "%-25s %s\n", data->names[0], data->data);
  //else
  //   fprintf(inifile, "# %-23s <NULL> (not set)\n", data->names[0]);

  function WriteLine(Entry, Value: String; AddComment: Boolean = False): String;
  begin
    if Value <> '' then
       Result:= Format('%-25s %s', [Entry, Value])
    else
       begin
         case AddComment of
           True : Result:= Format('# %-23s <NULL> (not set)', [Entry]);
           False: Format('%-25s %s', [Entry, Value]);
         end;
       end;
  end;

  function UpdateMAMELine(Entry, ValueHolder: String; AddComment: Boolean = False; FloatValue: Boolean = False): Boolean;
  begin
    case CustomGameOption of
      True:
        begin
          case FloatValue of
            True:
              begin
                if StrToFloat(ExtractMAMEIniValue(MAMEIniFile[Loop])) <>
                   StrToFloat(ValueHolder) then
                   GameIniFile.Add(WriteLine(Entry, ValueHolder, AddComment));
              end;
            False:
              begin
                if ExtractMAMEIniValue(MAMEIniFile[Loop]) <> ValueHolder then
                   GameIniFile.Add(WriteLine(Entry, ValueHolder, AddComment));
              end;
          end;
        end;
      False: MAMEIniFile[Loop]:= WriteLine(Entry, ValueHolder, AddComment);
    end;
  end;

  function SetScreenResolution(nScreenIdx: ShortInt; EntryStr: String): Boolean;
  begin
    Value:= ScreenDetails[nScreenIdx].eScreenResolution;
    if ScreenDetails[nScreenIdx].eScreenRefreshRate <> '' then
       Value:= Value+'@'+ScreenDetails[nScreenIdx].eScreenRefreshRate;
    UpdateMAMELine(EntryStr, Value);
  end;

begin
  if emuIni = '' then
     begin
       GenerateMessage('Error', 'Default ini filename is not defined! The emulator filename '+
                       'filename might be empty. Aborting...', 2);
       Exit;
     end;
  case CustomGameOption of
    True:
      begin
        ForceDirectories(ExtractFilePath(customIni));
        if FileExists(customIni) then
           DeleteFile(customIni);
        GameIniFile:= THashedStringList.Create;
        GameIniFile.BeginUpdate;
      end;
    False:
      begin
        if FormMain.CheckReadOnly(emuIni) then
           Exit;
      end;
  end;
  MAMEIniFile:= THashedStringList.Create;
  MAMEIniFile.LoadFromFile(emuIni);

  for Loop:=0 to MAMEIniFile.Count-1 do
  begin
    if MAMEIniFile[Loop] <> '' then
       begin
         EntryString:= XML_GetEntryName(MAMEIniFile[Loop]);
         tmpEntryStr:= EntryString+' ';
         // CONFIGURATION OPTIONS
         if (tmpEntryStr = 'readconfig ') or
            (tmpEntryStr = 'rc ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.ReadConfigFiles.Checked)))
         else
         if tmpEntryStr = 'skip_gameinfo ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.SkipGameInfo.Checked)))
         else
         // PATH AND DIRECTORY OPTIONS
         if (tmpEntryStr = 'rompath ') or
            (tmpEntryStr = 'rp ') then
            UpdateMAMELine(EntryString, FormMain.MountFoldersList(FormMAMESettings.FolderROMsHardDiskImages))
         else
         if (tmpEntryStr = 'samplepath ') or
            (tmpEntryStr = 'sp ') then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderSamples.Text)
         else
         if tmpEntryStr = 'inipath ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderINIFiles.Text)
         else
         if tmpEntryStr = 'fontpath ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderFontFiles.Text)
         else
         if tmpEntryStr = 'cfg_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderGamesConfigurations.Text)
         else
         if tmpEntryStr = 'nvram_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderNVRAM.Text)
         else
         if tmpEntryStr = 'memcard_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderMemoryCards.Text)
         else
         if tmpEntryStr = 'input_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderInputDeviceLogs.Text)
         else
         if tmpEntryStr = 'state_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderSaveStates.Text)
         else
         if (tmpEntryStr = 'artpath ') or
            (tmpEntryStr = 'artwork_directory ') then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderArtworks.Text)
         else
         if tmpEntryStr = 'snapshot_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderGameSnapshots.Text)
         else
         if tmpEntryStr = 'diff_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderDiff.Text)
         else
         if (tmpEntryStr = 'ctrlrpath ') or
            (tmpEntryStr = 'ctrlr_directory ') then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderControllerDefinitions.Text)
         else
         if tmpEntryStr = 'comment_directory ' then
            UpdateMAMELine(EntryString, FormMAMESettings.FolderDebuggerComments.Text)
         else
         if tmpEntryStr = 'cheat_file ' then
            UpdateMAMELine(EntryString, FormMAMESettings.CheatFile.Text)
         else
         // MISC OPTIONS
         if tmpEntryStr = 'bios ' then
            UpdateMAMELine(EntryString, FormMAMESettings.BiosSets.Selected.SubItems[0])
         else
         if (tmpEntryStr = 'cheat ') or
            (tmpEntryStr = 'c ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Cheat.Checked)))
         else
         // STATE/PLAYBACK OPTIONS
         if tmpEntryStr = 'autosave ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.AutoSave.Checked)))
         else
         // DEBUGGING OPTIONS
         if tmpEntryStr = 'log ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Log.Checked)))
         else
         if tmpEntryStr = 'oslog ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.OSLog.Checked)))
         else
         if (tmpEntryStr = 'verbose ') or
            (tmpEntryStr = 'v ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Verbose.Checked)))
         else
         if (tmpEntryStr = 'debug ') or
            (tmpEntryStr = 'd ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Debugger.Checked)))
         else
         if (tmpEntryStr = 'debugscript ') or
            (XML_CheckData(MAMEIniFile[Loop], '# debugscript ')) then
             UpdateMAMELine('debugscript', FormMAMESettings.DebuggerScript.Text, True)
         else
         // PERFORMANCE OPTIONS
         if (tmpEntryStr = 'autoframeskip ') or
            (tmpEntryStr = 'afs ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.AutoFrameSkip.Checked)))
         else
         if (tmpEntryStr = 'frameskip ') or
            (tmpEntryStr = 'fs ') then
            UpdateMAMELine(EntryString, FloatToStr(FormMAMESettings.FrameSkip.Position))
         else
         if (tmpEntryStr = 'seconds_to_run ') or
            (tmpEntryStr = 'str ') then
            UpdateMAMELine(EntryString, FloatToStr(FormMAMESettings.SecondsToRun.Position))
         else
         if tmpEntryStr = 'throttle ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Throttle.Checked)))
         else
         if tmpEntryStr = 'sleep ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Sleep.Checked)))
         else
         if tmpEntryStr = 'priority ' then
            UpdateMAMELine(EntryString, FloatToStr(FormMAMESettings.ThreadPriority.Position))
         else
         if (tmpEntryStr = 'multithreading ') or
            (tmpEntryStr = 'mt ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Multithreading.Checked)))
         else
         // VIDEO OPTIONS
         if tmpEntryStr = 'video ' then
            begin
              Value:= 'd3d';
              case FormMAMESettings.VideoOutputMode.ItemIndex of
                0: Value:= 'dgi';
                1: Value:= 'ddraw';
                2: Value:= 'd3d';
                3: Value:= 'none';
              end;
              UpdateMAMELine(EntryString, Value);
            end
         else
         if tmpEntryStr = 'numscreens ' then
            UpdateMAMELine(EntryString, IntToStr(FormMAMESettings.NumberScreens.ItemIndex+1))
         else
         if (tmpEntryStr = 'window ') or
            (tmpEntryStr = 'w ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.WindowMode.Checked)))
         else
         if (tmpEntryStr = 'maximize ') or
            (tmpEntryStr = 'max ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.MaximizeWindow.Checked)))
         else
         if (tmpEntryStr = 'keepaspect ') or
            (tmpEntryStr = 'ka') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.KeepAspectRatio.Checked)))
         else
         if tmpEntryStr = 'prescale ' then
            UpdateMAMELine(EntryString, FloatToStr(FormMAMESettings.Prescale_ScaleScreen.Position))
         else
         if tmpEntryStr = 'effect ' then
            begin
              if FormMAMESettings.EffectOverlay.Text = '' then
                 UpdateMAMELine(EntryString, 'none')
              else
                 UpdateMAMELine(EntryString, FormMAMESettings.EffectOverlay.Text)
            end
         else
         if tmpEntryStr = 'pause_brightness ' then
            UpdateMAMELine(EntryString, Format('%1.2f', [FormMAMESettings.PauseBrightness.Position]), True)
         else
         if tmpEntryStr = 'waitvsync ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.WaitVerticalSync.Checked)))
         else
         if tmpEntryStr = 'syncrefresh ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.SyncronizeRefreshRate.Checked)))
         else
         // VIDEO ROTATION OPTIONS
         if tmpEntryStr = 'rotate ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Rotate.Checked)))
         else
         if tmpEntryStr = 'ror ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.RotateRight.Checked)))
         else
         if tmpEntryStr = 'rol ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.RotateLeft.Checked)))
         else
         if tmpEntryStr = 'autoror ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.AutoRotateRight.Checked)))
         else
         if tmpEntryStr = 'autorol ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.AutoRotateLeft.Checked)))
         else
         if tmpEntryStr = 'flipx ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.FlipX.Checked)))
         else
         if tmpEntryStr = 'flipy ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.FlipY.Checked)))
         else
         // DIRECTDRAW-SPECIFIC OPTIONS
         if (tmpEntryStr = 'hwstretch ') or
            (tmpEntryStr = 'hws ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.HardwareStretch.Checked)))
         else
         // DIRECT3D-SPECIFIC OPTIONS
         if tmpEntryStr = 'd3dversion ' then
            UpdateMAMELine(EntryString, IntToStr(FormMAMESettings.Direct3DVersion.ItemIndex+8))
         else
         if (tmpEntryStr = 'filter ') or
            (tmpEntryStr = 'd3dfilter') or
            (tmpEntryStr = 'flt') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.BilinearFilter.Checked)))
         else
         // PER-WINDOW VIDEO OPTIONS
         if tmpEntryStr = 'screen ' then
            UpdateMAMELine(EntryString, ScreenDetails[-1].eScreenName)
         else
         if (tmpEntryStr = 'aspect ') or
            (tmpEntryStr = 'screen_aspect') then
            UpdateMAMELine(EntryString, ScreenDetails[-1].eScreenAspectRatio)
         else
         if (tmpEntryStr = 'resolution ') or
            (tmpEntryStr = 'r ') then
            SetScreenResolution(-1, EntryString)
         else
         if tmpEntryStr = 'view ' then
            UpdateMAMELine(EntryString, ScreenDetails[-1].eScreenView)
         else

         if tmpEntryStr = 'screen0 ' then
            UpdateMAMELine(EntryString, ScreenDetails[0].eScreenName)
         else
         if tmpEntryStr = 'aspect0 ' then
            UpdateMAMELine(EntryString, ScreenDetails[0].eScreenAspectRatio)
         else
         if (tmpEntryStr = 'resolution0 ') or
            (tmpEntryStr = 'r0 ') then
            SetScreenResolution(0, EntryString)
         else
         if tmpEntryStr = 'view0 ' then
            UpdateMAMELine(EntryString, ScreenDetails[0].eScreenView)
         else
         if tmpEntryStr = 'screen1 ' then
            UpdateMAMELine(EntryString, ScreenDetails[1].eScreenName)
         else
         if tmpEntryStr = 'aspect1 ' then
            UpdateMAMELine(EntryString, ScreenDetails[1].eScreenAspectRatio)
         else
         if (tmpEntryStr = 'resolution1 ') or
            (tmpEntryStr = 'r1 ') then
            SetScreenResolution(1, EntryString)
         else
         if tmpEntryStr = 'view1 ' then
            UpdateMAMELine(EntryString, ScreenDetails[1].eScreenView)
         else
         if tmpEntryStr = 'screen2 ' then
            UpdateMAMELine(EntryString, ScreenDetails[2].eScreenName)
         else
         if tmpEntryStr = 'aspect2 ' then
            UpdateMAMELine(EntryString, ScreenDetails[2].eScreenAspectRatio)
         else
         if (tmpEntryStr = 'resolution2 ') or
            (tmpEntryStr = 'r2 ') then
            SetScreenResolution(2, EntryString)
         else
         if tmpEntryStr = 'view2 ' then
            UpdateMAMELine(EntryString, ScreenDetails[2].eScreenView)
         else
         if tmpEntryStr = 'screen3 ' then
            UpdateMAMELine(EntryString, ScreenDetails[3].eScreenName)
         else
         if tmpEntryStr = 'aspect3 ' then
            UpdateMAMELine(EntryString, ScreenDetails[3].eScreenAspectRatio)
         else
         if (tmpEntryStr = 'resolution3 ') or
            (tmpEntryStr = 'r3 ') then
            SetScreenResolution(3, EntryString)
         else
         if tmpEntryStr = 'view3 ' then
            UpdateMAMELine(EntryString, ScreenDetails[3].eScreenView)
         else
         // FULL SCREEN OPTIONS
         if (tmpEntryStr = 'triplebuffer ') or
            (tmpEntryStr = 'tb ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.TripleBuffer.Checked)))
         else
         if tmpEntryStr = 'switchres ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.SwitchResolution.Checked)))
         else
         if (tmpEntryStr = 'full_screen_brightness ') or
            (tmpEntryStr = 'fsb ') then
            UpdateMAMELine(EntryString, Format('%1.2f', [FormMAMESettings.FullScreenBrightness.Position]), True)
         else
         if (tmpEntryStr = 'full_screen_contrast ') or
            (tmpEntryStr = 'fsc ') then
            UpdateMAMELine(EntryString, Format('%1.2f', [FormMAMESettings.FullScreenContrast.Position]), True)
         else
         if (tmpEntryStr = 'full_screen_gamma ') or
            (tmpEntryStr = 'fsg ') then
            UpdateMAMELine(EntryString, Format('%1.2f', [FormMAMESettings.FullScreenGamma.Position]), True)
         else
         // GAME SCREEN OPTIONS
         if tmpEntryStr = 'brightness ' then
            UpdateMAMELine(EntryString, Format('%1.2f', [FormMAMESettings.Brightness.Position]), True)
         else
         if tmpEntryStr = 'contrast ' then
            UpdateMAMELine(EntryString, Format('%1.2f', [FormMAMESettings.Contrast.Position]), True)
         else
         if tmpEntryStr = 'gamma ' then
            UpdateMAMELine(EntryString, Format('%1.2f', [FormMAMESettings.Gamma.Position]), True)
         else
         // VECTOR RENDERING OPTIONS
         if (tmpEntryStr = 'antialias ') or
            (tmpEntryStr = 'aa ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Antialias.Checked)))
         else
         if tmpEntryStr = 'beam ' then
            UpdateMAMELine(EntryString, Format('%2.2f', [FormMAMESettings.VectorBeamWidth.Position]), True)
         else
         if tmpEntryStr = 'flicker ' then
            UpdateMAMELine(EntryString, Format('%3.2f', [FormMAMESettings.VectorFlickerEffect.Position]), True)
         else
         // ARTWORK OPTIONS
         if (tmpEntryStr = 'artwork_crop ') or
            (tmpEntryStr = 'artcrop ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.ArtworkCrop.Checked)))
         else
         if (tmpEntryStr = 'use_backdrops ') or
            (tmpEntryStr = 'backdrop ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.ArtworkBackdrops.Checked)))
         else
         if (tmpEntryStr = 'use_overlays ') or
            (tmpEntryStr = 'overlay ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.ArtworkOverlays.Checked)))
         else
         if (tmpEntryStr = 'use_bezels ') or
            (tmpEntryStr = 'bezel ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.ArtworkBezels.Checked)))
         else
         // SOUND OPTIONS
         if tmpEntryStr = 'sound ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Sound.Checked)))
         else
         if (tmpEntryStr = 'samplerate ') or
            (tmpEntryStr = 'sr ') then
            begin
              Value:= '48000';
              case FormMAMESettings.SampleRate.ItemIndex of
                0: Value:= '11025';
                1: Value:= '22050';
                2: Value:= '44100';
                3: Value:= '48000';
                4: Value:= '96000';
                5: Value:= '192000';
              end;
              UpdateMAMELine(EntryString, Value);
            end
         else
         if tmpEntryStr = 'samples ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Samples.Checked)))
         else
         if (tmpEntryStr = 'volume ') or
            (tmpEntryStr = 'vol ') then
            UpdateMAMELine(EntryString, IntToStr(FormMAMESettings.Volume.Position))
         else
         if tmpEntryStr = 'audio_latency ' then
            UpdateMAMELine(EntryString, FloatToStr(FormMAMESettings.AudioLatency.ItemIndex+1))
         else
         // INPUT DEVICE OPTIONS
         if (tmpEntryStr = 'ctrlr ') or
            (XML_CheckData(MAMEIniFile[Loop], '# ctrlr ')) then
            begin
              if EntryString <> 'ctrlr' then
                 EntryString:= 'ctrlr';
              if FormMAMESettings.ControllerDefinitions.ItemIndex = 0 then
                 UpdateMAMELine(EntryString, '', True)
              else
                 UpdateMAMELine(EntryString, FormMAMESettings.ControllerDefinitions.Text);
            end
         else
         if tmpEntryStr = 'mouse ' then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Mouse.Checked)))
         else
         if (tmpEntryStr = 'joystick ') or
            (tmpEntryStr = 'joy ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Joystick.Checked)))
         else
         if (tmpEntryStr = 'lightgun ') or
            (tmpEntryStr = 'gun ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.Lightgun.Checked)))
         else
         if (tmpEntryStr = 'dual_lightgun ') or
            (tmpEntryStr = 'dual ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.DualLightGun.Checked)))
         else
         if (tmpEntryStr = 'offscreen_reload ') or
            (tmpEntryStr = 'reload ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.LightgunOffScreenReload.Checked)))
         else
         if (tmpEntryStr = 'steadykey ') or
            (tmpEntryStr = 'steady ') then
            UpdateMAMELine(EntryString, IntToStr(Ord(FormMAMESettings.SteadyKey.Checked)))
         else
         if (tmpEntryStr = 'joy_deadzone ') or
            (tmpEntryStr = 'jdz ') then
            UpdateMAMELine(EntryString, FloatToStr(FormMAMESettings.JoystickDeadzone.Position), True)
         else
         if (tmpEntryStr = 'joy_saturation ') or
            (tmpEntryStr = 'jsat ') then
            UpdateMAMELine(EntryString, FloatToStr(FormMAMESettings.JoystickSaturation.Position), True)
         else

         if tmpEntryStr = 'digital ' then
            begin
              if FormMAMESettings.Analog_Digital.ItemIndex <> 2 then
                 UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_Digital.Text))
              else
                 UpdateMAMELine(EntryString, FormMAMESettings.Analog_Digital_Custom.Text);
            end
         else
         // AUTOMATIC DEVICE SELECTION OPTIONS
         if (tmpEntryStr = 'paddle_device ') or
            (tmpEntryStr = 'paddle ') then
            UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_Paddle.Text))
         else
         if (tmpEntryStr = 'adstick_device ') or
            (tmpEntryStr = 'adstick ') then
            UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_Joystick.Text))
         else
         if (tmpEntryStr = 'pedal_device ') or
            (tmpEntryStr = 'pedal ') then
            UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_Pedal.Text))
         else
         if (tmpEntryStr = 'dial_device ') or
            (tmpEntryStr = 'dial ') then
            UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_Dial.Text))
         else
         if (tmpEntryStr = 'trackball_device ') or
            (tmpEntryStr = 'trackball ') then
            UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_Trackball.Text))
         else
         if tmpEntryStr = 'lightgun_device ' then
            UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_LightGun.Text))
         else
         if tmpEntryStr = 'positional_device ' then
            UpdateMAMELine(EntryString, LowerCase(FormMAMESettings.Analog_Positional.Text));
       end;
  end;
  case CustomGameOption of
    True:
      begin
        try
          if customIni <> '' then
             GameIniFile.SaveToFile(customIni)
          else
             GenerateMessage('Error', 'Custom ini filename is not defined! Cannot save file.', 2);
        finally
          FreeAndNil(GameIniFile);
        end;
      end;
    False:
      begin
        try
          MAMEIniFile.SaveToFile(emuIni);
        finally
        end;
      end;
  end;
  FreeAndNil(MAMEIniFile);
end;

procedure TFormMAMESettings.LoadBiosList(SelectBiosName: String);
var
  BiosSet: TMemIniFile;
  Loop, LoopSets: Integer;
  BiosList, BiosSetList: TStringList;
  DefaultBios: String;
begin
  if FileExists(FormMain.FrontendPath+'ini_files\biosset.ini') then
     begin
       BiosSet:= TMemIniFile.Create(FormMain.FrontendPath+'ini_files\biosset.ini');
       BiosList:= TStringList.Create;
       BiosSet.ReadSectionValues('Descriptions', BiosList);
       BiosList.Sort;
       BiosSetList:= TStringList.Create;
       BiosSets.Items.BeginUpdate;
       for Loop:=0 to BiosList.Count-1 do
       begin
         DefaultBios:= '';
         with BiosSets.Items.Add do
         begin
           ImageIndex:= 65;
           Caption:= BiosList.ValueFromIndex[Loop];
           SubItems.Add(BiosList.Names[Loop]);
           SubItems.Add('');
           SubItems.Add('');
         end;
         if BiosSet.SectionExists(BiosList.Names[Loop]) then
            begin
              // there are multiple sets in this bios
              BiosSetList.Clear;
              BiosSet.ReadSectionValues(BiosList.Names[Loop], BiosSetList);
              DefaultBios:= BiosSetList.Values['default_bios'];
              for LoopSets:=0 to BiosSetList.Count-1 do
              begin
                if (BiosSetList.Names[LoopSets] <> 'default_bios') and
                   (BiosSetList.Names[LoopSets] <> 'selected_bios') then
                   begin
                     with BiosSets.Items.Add do
                     begin
                       Indent:= 1;
                       ImageIndex:= 65;
                       Caption:= BiosSetList.ValueFromIndex[LoopSets];
                       SubItems.Add(BiosSetList.Names[LoopSets]);
                       SubItems.Add(BiosList.Names[Loop]);
                       if BiosSetList.Names[LoopSets] = DefaultBios then
                          SubItems.Add('yes')
                       else
                          SubItems.Add('');
                     end;
                   end;
              end;
            end;
       end;
       BiosSets.Items.EndUpdate;
       FreeAndNil(BiosSet);
       FreeAndNil(BiosList);
       FreeAndNil(BiosSetList);
     end;
end;

procedure TFormMAMESettings.ButtonCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFormMAMESettings.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  SetCurrentDir(FormMain.FrontendPath);
end;

procedure TFormMAMESettings.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #27 then
     ButtonCancel.Click;
end;

procedure TFormMAMESettings.FormShow(Sender: TObject);
begin
  case sysID of
    1: FormMain.LoadTopBar(TopImage, 'emu_mame.png');
    4: FormMain.LoadTopBar(TopImage, 'emu_agemame.png');
    5: FormMain.LoadTopBar(TopImage, 'emu_pinmame.png');
    6: FormMain.LoadTopBar(TopImage, 'emu_hazemd.png');
  end;
  if Tag = 1 then
     begin
       if FormMain.GetGameData(FormMain.SelectedNode) then
          FormMain.GetGameIcon(FormMain.IL_StandardIconsLarge, FormMain.IL_StandardIconsStandard,
                                  FormMain.IL_StandardIconsSmall, FormMain.GameInfoData.eImageIndex,
                                  ImageGameIcon.Picture.Icon);
     end;

  case Tag of
    0: LabelGameDescription.Caption:= '> '+FormMain.GetEmuFullDescription(sysID)+' <'+#13#10+
                                      LabelGameDescription.Hint;
    1: LabelGameDescription.Caption:= '> '+FormMain.GameInfoData.eDescription+' <'+#13#10+
                                      LabelGameDescription.Hint;
   end;
  LoadBiosList('');
  ReadFile.Click;
end;

procedure TFormMAMESettings.ReadFileClick(Sender: TObject);
begin
  if not FileExists(ReadFileIni.Text) then
     FormMain.CheckMAMEIniFile(EmulatorString, sysID);
  ReadMAMEIniFile(EmulatorString, emuIni, False); // read all mame.ini options
  if Tag = 1 then
     begin
       if Pos(':', FormMain.IniFilesDir[sysID]) <> 0 then
          ReadFileIni.Text:= FormMain.IniFilesDir[sysID]+'\'+GameName+'.ini'
       else
          ReadFileIni.Text:= ExtractFilePath(EmulatorString)+FormMain.IniFilesDir[sysID]+'\'+GameName+'.ini';
       ReadFile.Hint:= Format('Read "%s"', [ReadFileIni.Text]);
       ReadMAMEIniFile(EmulatorString, ReadFileIni.Text, True); // read all gamename.ini options
     end;
  ScreensSelector.OnSelect(Self);
end;

procedure TFormMAMESettings.EffectOverlayBrowseClick(Sender: TObject);
begin
  FormMain.DialogOpenFile(5, 'Select a file for effect overlay', EffectOverlay, False, False, FolderArtworks.Text);
  if EffectOverlay.Text <> '' then
     EffectOverlay.Text:= ChangeFileExt(ExtractFileName(EffectOverlay.Text), '');
end;

procedure TFormMAMESettings.FolderROMsDisksButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectMultiFolders(FolderROMsHardDiskImages, FolderROMsDisksAddSubFolders.Checked);
end;

procedure TFormMAMESettings.FolderSamplesButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderSamples, False);
end;

procedure TFormMAMESettings.FolderIniFilesButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderIniFiles, False);
end;

procedure TFormMAMESettings.FolderGamesConfigurationsButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderGamesConfigurations, False);
end;

procedure TFormMAMESettings.FolderMemoryCardsButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderMemoryCards, False);
end;

procedure TFormMAMESettings.FolderInputDeviceLogsButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderInputDeviceLogs, False);
end;

procedure TFormMAMESettings.FolderSaveStatesButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderSaveStates, False);
end;

procedure TFormMAMESettings.FolderArtworksButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderArtworks, False);
end;

procedure TFormMAMESettings.FolderGameSnapshotsButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderGameSnapshots, False);
end;

procedure TFormMAMESettings.FolderDiffButtonSelectClick(Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderDiff, False);
end;

procedure TFormMAMESettings.FolderControllerDefinitionsButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderControllerDefinitions, False);
end;

procedure TFormMAMESettings.FolderDebuggerCommentsButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderDebuggerComments, False);
end;

procedure TFormMAMESettings.ButtonOkClick(Sender: TObject);
begin
  SetCurrentDir(FormMain.FrontendPath);
  if EmulatorString = '' then
     GenerateMessage('Error', 'Emulator filename is empty!', 2);
  WriteMAMEIniFile(EmulatorString, emuIni, ReadFileIni.Text, Boolean(Tag));// update all mame.ini options
  Close;
end;

procedure TFormMAMESettings.CheatFileBrowseClick(Sender: TObject);
begin
  FormMain.DialogOpenFile(3, 'Select a cheat file', CheatFile, False, False, CheatFile.Text);
end;

procedure TFormMAMESettings.DebuggerScriptBrowseClick(Sender: TObject);
begin
  FormMain.DialogOpenFile(11, 'Select a debugger script file', DebuggerScript, False, False, DebuggerScript.Text);
end;

procedure TFormMAMESettings.ScreenName1Select(Sender: TObject);
begin
  ScreenName_Custom.Enabled:= ScreenName.ItemIndex = 1;
end;

procedure TFormMAMESettings.FolderROMsDisksButtonClearClick(
  Sender: TObject);
begin
  FolderROMsHardDiskImages.Clear;
end;

procedure TFormMAMESettings.FolderROMsHardDiskImagesKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key = VK_DELETE then
     FolderROMsHardDiskImages.DeleteSelected;
end;

procedure TFormMAMESettings.FolderFontFilesButtonSelectClick(
  Sender: TObject);
begin
  FormMain.DialogSelectFolder(FolderFontFiles, False);
end;

procedure TFormMAMESettings.VolumeChange(Sender: TObject);
begin
  LabelVolumeValue.Caption:= IntToStr(Volume.Position)+' dB';
end;

procedure TFormMAMESettings.JoystickDeadzoneChange(Sender: TObject);
begin
  LabelJoystickDeadzoneValue.Caption:= Format('%1.2f', [JoystickDeadzone.Position]);
end;

procedure TFormMAMESettings.BrightnessChange(Sender: TObject);
begin
  LabelBrightnessValue.Caption:= Format('%1.2f', [Brightness.Position]);
end;

procedure TFormMAMESettings.ContrastChange(Sender: TObject);
begin
  LabelContrastValue.Caption:= Format('%1.2f', [Contrast.Position]);
end;

procedure TFormMAMESettings.GammaChange(Sender: TObject);
begin
  LabelGammaValue.Caption:= Format('%1.2f', [Gamma.Position]);
end;

procedure TFormMAMESettings.PauseBrightnessChange(Sender: TObject);
begin
  LabelPauseBrightnessValue.Caption:= Format('%1.2f', [PauseBrightness.Position]);
end;

procedure TFormMAMESettings.LabelJoystickSaturationValueClick(
  Sender: TObject);
begin
  LabelJoystickSaturation.Caption:= Format('%1.2f', [JoystickSaturation.Position]);
end;

procedure TFormMAMESettings.Prescale_ScaleScreenChange(Sender: TObject);
begin
  LabelPrescale_ScaleScreenValue.Caption:= IntToStr(Prescale_ScaleScreen.Position)+'x';
end;

procedure TFormMAMESettings.FullScreenBrightnessChange(Sender: TObject);
begin
  LabelFullScreenBrightnessValue.Caption:= Format('%1.2f', [FullScreenBrightness.Position]);
end;

procedure TFormMAMESettings.FullScreenContrastChange(Sender: TObject);
begin
  LabelFullScreenContrastValue.Caption:= Format('%1.2f', [FullScreenContrast.Position]);
end;

procedure TFormMAMESettings.FullScreenGammaChange(Sender: TObject);
begin
  LabelFullScreenGammaValue.Caption:= Format('%1.2f', [FullScreenGamma.Position]);
end;

procedure TFormMAMESettings.VectorBeamWidthChange(Sender: TObject);
begin
  LabelVectorBeamWidthValue.Caption:= Format('%2.2f', [VectorBeamWidth.Position]);
end;

procedure TFormMAMESettings.VectorFlickerEffectChange(Sender: TObject);
begin
  LabelVectorFlickerEffectValue.Caption:= Format('%3.2f', [VectorFlickerEffect.Position]);
end;

procedure TFormMAMESettings.FrameskipChange(Sender: TObject);
begin
  LabelFrameskipValue.Caption:= IntToStr(Frameskip.Position);
end;

procedure TFormMAMESettings.ThreadPriorityChange(Sender: TObject);
begin
  LabelThreadPriorityValue.Caption:= IntToStr(ThreadPriority.Position);
end;

procedure TFormMAMESettings.SecondsToRunChange(Sender: TObject);
begin
  LabelSecondsToRunValue.Caption:= GetPlayTime(SecondsToRun.Position, False, False);
end;

procedure TFormMAMESettings.ScreenNameSelect(Sender: TObject);
begin
  ScreenName_Custom.Enabled:= ScreenName.ItemIndex = 1;
  if ScreenName.ItemIndex <> 1 then
     if ScreenDetails[ScreensSelector.Tag].eScreenName <> ScreenName.Text then
        ScreenDetails[ScreensSelector.Tag].eScreenName:= ScreenName.Text;
end;

procedure TFormMAMESettings.ScreenAspectRatioSelect(Sender: TObject);
begin
  ScreenAspectRatio_Custom.Enabled:= ScreenAspectRatio.ItemIndex = 1;
  if ScreenAspectRatio.ItemIndex <> 1 then
     if ScreenDetails[ScreensSelector.Tag].eScreenAspectRatio <> ScreenAspectRatio.Text then
        ScreenDetails[ScreensSelector.Tag].eScreenAspectRatio:= ScreenAspectRatio.Text;
end;

procedure TFormMAMESettings.ScreenResolutionSelect(Sender: TObject);
begin
  ScreenResolution_Custom.Enabled:= ScreenResolution.ItemIndex = 1;
  ScreenRefreshRate_Custom.Enabled:= ScreenResolution.ItemIndex > 0;
  LabelScreenRefreshRate_Custom.Enabled:= ScreenRefreshRate_Custom.Enabled;
  if ScreenResolution.ItemIndex <> 1 then
     if ScreenDetails[ScreensSelector.Tag].eScreenResolution <> ScreenResolution.Text then
        ScreenDetails[ScreensSelector.Tag].eScreenResolution:= ScreenResolution.Text;
end;

procedure TFormMAMESettings.ScreenViewSelect(Sender: TObject);
begin
  ScreenView_Custom.Enabled:= ScreenView.ItemIndex = 1;
  if ScreenView.ItemIndex <> 1 then
     if ScreenDetails[ScreensSelector.Tag].eScreenView <> ScreenView.Text then
        ScreenDetails[ScreensSelector.Tag].eScreenView:= ScreenView.Text;
end;

procedure TFormMAMESettings.ScreensSelectorSelect(Sender: TObject);
var
  tmpString: String;
  sIndex: ShortInt;
begin
  ScreensSelector.Tag:= ScreensSelector.ItemIndex-1;

  // screen name
  sIndex:= ScreenName.Items.IndexOf(ScreenDetails[ScreensSelector.Tag].eScreenName);
  if sIndex <> -1 then
     begin
       SetSelectedComboBox(sIndex, ScreenName);
       ScreenName_Custom.Text:= '';
     end
  else
     begin
       SetSelectedComboBox(1, ScreenName);
       ScreenName_Custom.Text:= ScreenDetails[ScreensSelector.Tag].eScreenName;
     end;

  // aspect ratio
  sIndex:= 0;
  sIndex:= ScreenAspectRatio.Items.IndexOf(ScreenDetails[ScreensSelector.Tag].eScreenAspectRatio);
  if sIndex <> -1 then
     begin
       SetSelectedComboBox(sIndex, ScreenAspectRatio);
       ScreenAspectRatio.ItemIndex:= sIndex;
       ScreenAspectRatio_Custom.Text:= '';
     end
  else
     begin
       ScreenAspectRatio_Custom.Text:= ScreenDetails[ScreensSelector.Tag].eScreenAspectRatio;
       SetSelectedComboBox(1, ScreenAspectRatio);
     end;

  // resolution
  sIndex:= 0;
  sIndex:= ScreenResolution.Items.IndexOf(ScreenDetails[ScreensSelector.Tag].eScreenResolution);
  if sIndex <> -1 then
     begin
       SetSelectedComboBox(sIndex, ScreenResolution);
       ScreenResolution_Custom.Text:= '';
     end
  else
     begin
       SetSelectedComboBox(1, ScreenResolution);
       ScreenResolution_Custom.Text:= ScreenDetails[ScreensSelector.Tag].eScreenResolution;
     end;

  // screen refresh rate
  if ScreenDetails[ScreensSelector.Tag].eScreenRefreshRate = '' then
     SetSelectedComboBox(0, ScreenRefreshRate_Custom)
  else
     begin
       sIndex:= 0;
       sIndex:= ScreenRefreshRate_Custom.Items.IndexOf(ScreenDetails[ScreensSelector.Tag].eScreenRefreshRate);
       if sIndex <> -1 then
          SetSelectedComboBox(sIndex, ScreenRefreshRate_Custom)
       else
          SetSelectedComboBox(0, ScreenRefreshRate_Custom);
     end;

  // screen view
  sIndex:= 0;
  sIndex:= ScreenView.Items.IndexOf(ScreenDetails[ScreensSelector.Tag].eScreenView);
  if sIndex <> -1 then
     begin
       SetSelectedComboBox(sIndex, ScreenView);
       ScreenView_Custom.Text:= '';
     end
  else
     begin
       SetSelectedComboBox(1, ScreenView);
       ScreenView_Custom.Text:= ScreenDetails[ScreensSelector.Tag].eScreenView;
     end;
end;

procedure TFormMAMESettings.ScreenName_CustomChange(Sender: TObject);
begin
  if ScreenName.ItemIndex = 1 then
     begin
       if ScreenDetails[ScreensSelector.Tag].eScreenName <> ScreenName_Custom.Text then
          ScreenDetails[ScreensSelector.Tag].eScreenName:= ScreenName_Custom.Text;
     end;
end;

procedure TFormMAMESettings.ScreenAspectRatio_CustomChange(
  Sender: TObject);
begin
  if ScreenAspectRatio.ItemIndex = 1 then
     begin
       if ScreenDetails[ScreensSelector.Tag].eScreenAspectRatio <> ScreenAspectRatio_Custom.Text then
          ScreenDetails[ScreensSelector.Tag].eScreenAspectRatio:= ScreenAspectRatio_Custom.Text;
     end;
end;

procedure TFormMAMESettings.ScreenResolution_CustomChange(Sender: TObject);
begin
  if ScreenResolution.ItemIndex = 1 then
     begin
       if ScreenDetails[ScreensSelector.Tag].eScreenResolution <> ScreenResolution_Custom.Text then
          ScreenDetails[ScreensSelector.Tag].eScreenResolution:= ScreenResolution_Custom.Text;
     end;
end;

procedure TFormMAMESettings.ScreenRefreshRate_CustomSelect(
  Sender: TObject);
begin
  if ScreenResolution.ItemIndex > 0 then
     begin
       if ScreenRefreshRate_Custom.ItemIndex = 0 then
          ScreenDetails[ScreensSelector.Tag].eScreenRefreshRate:= ''
       else
       if ScreenDetails[ScreensSelector.Tag].eScreenRefreshRate <> ScreenRefreshRate_Custom.Text then
          ScreenDetails[ScreensSelector.Tag].eScreenRefreshRate:= ScreenRefreshRate_Custom.Text;
     end
end;

procedure TFormMAMESettings.ScreenView_CustomChange(Sender: TObject);
begin
  if ScreenView.ItemIndex = 1 then
     begin
       if ScreenDetails[ScreensSelector.Tag].eScreenView <> ScreenView_Custom.Text then
          ScreenDetails[ScreensSelector.Tag].eScreenView:= ScreenView_Custom.Text;
     end;
end;

end.

