home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwwin / settings.c_ / settings.bin
Text File  |  1995-11-14  |  10KB  |  295 lines

  1. /**********************************************************************
  2.  *
  3.  * File :     settings.c
  4.  *
  5.  * Abstract : The implementation of some functions which handle the
  6.  *            persistent settings of the viewer.
  7.  *
  8.  *            This application had been written to be compatible with
  9.  *            both the fixed and floating-point versions of the
  10.  *            RenderWare library, i.e., it uses the macros CREAL,
  11.  *            INT2REAL, RAdd, RDiv, RSub etc. If your application is
  12.  *            intended for the floating-point version of the library
  13.  *            only these macros are not necessary.
  14.  *
  15.  *            Please note that this application is intended for
  16.  *            demonstration purposes only. No support will be
  17.  *            provided for this code and it comes with no warranty.
  18.  *
  19.  **********************************************************************
  20.  *
  21.  * This file is a product of Criterion Software Ltd.
  22.  *
  23.  * This file is provided as is with no warranties of any kind and is
  24.  * provided without any obligation on Criterion Software Ltd. or
  25.  * Canon Inc. to assist in its use or modification.
  26.  *
  27.  * Criterion Software Ltd. will not, under any
  28.  * circumstances, be liable for any lost revenue or other damages arising
  29.  * from the use of this file.
  30.  *
  31.  * Copyright (c) 1994, 1995 Criterion Software Ltd.
  32.  * All Rights Reserved.
  33.  *
  34.  * RenderWare is a trademark of Canon Inc.
  35.  *
  36.  **********************************************************************/
  37.  
  38. /**********************************************************************
  39.  *
  40.  * Header files.
  41.  *
  42.  **********************************************************************/
  43.  
  44. #include <windows.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47.  
  48. #include <rwlib.h>
  49. #include <rwwin.h>
  50.  
  51. #include "resource.h"
  52. #include "common.h"
  53. #include "settings.h"
  54.  
  55. /**********************************************************************
  56.  *
  57.  * Constant definitions.
  58.  *
  59.  **********************************************************************/
  60.  
  61. /*
  62.  * Name of the initialization file itself.
  63.  */
  64. #define INIFILENAME               "rwview.ini"
  65.  
  66. /*
  67.  * Initialization file section and option strings.
  68.  */
  69. #define OPTIONSSECTION            "Options"
  70. #define     SHOWLIGHTSENTRY       "ShowLights"
  71. #define     SHOWSELECTIONENTRY    "ShowSelection"
  72. #define     PLAYMOVIESENTRY       "PlayMovies"
  73. #define     OBJECTMOMENTUMENTRY   "ObjectMomentum"
  74. #define     SHAPEPATHENTRY        "ShapePath"
  75. #define     CENTERBACKDROP        "CenterBackdrop"
  76. #define     BACKDROPFILENAME      "BackdropFileName"
  77. #define     BACKGROUNDCOLORENTRY  "BackgroundColor"
  78.  
  79. #define MRUFILESECTION            "Files"
  80. #define     MRUFILEOPTION         "File%d"
  81.  
  82. /**********************************************************************
  83.  *
  84.  * Exported Data.
  85.  *
  86.  **********************************************************************/
  87.  
  88. /*
  89.  * This flag is set if the user has modified the settings in this
  90.  * session.
  91.  */
  92. RwBool       SettingsChanged = FALSE;
  93.  
  94. /*
  95.  * Basic options.
  96.  */
  97. COLORREF     BackgroundColor;
  98. char         BackdropFileName[_MAX_PATH];
  99. RwBool       CenterBackdrop;
  100. RwBool       ShowHighlight;
  101. RwBool       ShowLights;
  102. RwBool       PlayMovies;
  103. RwBool       Momentum;
  104.  
  105. /*
  106.  * MRU file names.
  107.  */
  108. int          NumMRUFiles = 0;
  109. static char  MRUFileStrings[MAXMRUFILES][_MAX_PATH];
  110. char        *MRUFiles[MAXMRUFILES];
  111.  
  112. /**********************************************************************
  113.  *
  114.  * Functions.
  115.  *
  116.  **********************************************************************/
  117.  
  118. /**********************************************************************/
  119.  
  120. /*
  121.  * Read the saved settings from the initialization file.
  122.  */
  123. void
  124. ReadSettings(void)
  125. {
  126.     char buffer[128];
  127.     int  r;
  128.     int  g;
  129.     int  b;
  130.     int  i;
  131.     char optionString[_MAX_PATH];
  132.     
  133.     /*
  134.      * Parse the settings.
  135.      */
  136.     GetPrivateProfileString(OPTIONSSECTION, BACKGROUNDCOLORENTRY, "0 0 0",
  137.                             buffer, sizeof(buffer), INIFILENAME);
  138.     sscanf(buffer, "%d %d %d", &r, &g, &b);
  139.     BackgroundColor = PALETTERGB(r, g, b);
  140.     GetPrivateProfileString(OPTIONSSECTION, SHOWLIGHTSENTRY, "0",
  141.                             buffer, sizeof(buffer), INIFILENAME);
  142.     ShowLights = (RwBool)atoi(buffer);
  143.     GetPrivateProfileString(OPTIONSSECTION, SHOWSELECTIONENTRY, "1",
  144.                             buffer, sizeof(buffer), INIFILENAME);
  145.     ShowHighlight = (RwBool)atoi(buffer);
  146.     GetPrivateProfileString(OPTIONSSECTION, PLAYMOVIESENTRY, "0",
  147.                             buffer, sizeof(buffer), INIFILENAME);
  148.     PlayMovies = (RwBool)atoi(buffer);
  149.     GetPrivateProfileString(OPTIONSSECTION, OBJECTMOMENTUMENTRY, "1",
  150.                             buffer, sizeof(buffer), INIFILENAME);
  151.     Momentum = (RwBool)atoi(buffer);
  152.     GetPrivateProfileString(OPTIONSSECTION, CENTERBACKDROP, "0",
  153.                             buffer, sizeof(buffer), INIFILENAME);
  154.     CenterBackdrop = (RwBool)atoi(buffer);
  155.     GetPrivateProfileString(OPTIONSSECTION, BACKDROPFILENAME, "",
  156.                             BackdropFileName, sizeof(BackdropFileName), INIFILENAME);
  157.     
  158.     /*
  159.      * Read the MRU file list.
  160.      */
  161.     for (i = 0; i < MAXMRUFILES; i++)
  162.         MRUFiles[i] = MRUFileStrings[i];
  163.     for (i = 0; i < MAXMRUFILES; i++)
  164.     {
  165.         sprintf(optionString, MRUFILEOPTION, i);
  166.         GetPrivateProfileString(MRUFILESECTION, optionString, "",
  167.                                 MRUFiles[i], _MAX_PATH, INIFILENAME);
  168.         if (*MRUFiles[i] == '\0')
  169.             break;
  170.     }
  171.     NumMRUFiles = i;
  172. }
  173.  
  174. /**********************************************************************/
  175.  
  176. /*
  177.  * Write the current settings back to the initialization file. If the
  178.  * parameter writeAll is TRUE then all settings are written otherwise
  179.  * only the MRU files are written.
  180.  */
  181. void
  182. WriteSettings(BOOL writeAll)
  183. {
  184.     char buffer[128];
  185.     int  i;
  186.     
  187.     if (writeAll)
  188.     {
  189.         wsprintf(buffer, "%d %d %d", GetRValue(BackgroundColor),
  190.                                      GetGValue(BackgroundColor),
  191.                                      GetBValue(BackgroundColor));
  192.         WritePrivateProfileString(OPTIONSSECTION, BACKGROUNDCOLORENTRY,
  193.                                   buffer, INIFILENAME);
  194.         WritePrivateProfileString(OPTIONSSECTION, SHOWLIGHTSENTRY,
  195.                                   (ShowLights ? "1" : "0"), INIFILENAME);    
  196.         WritePrivateProfileString(OPTIONSSECTION, SHOWSELECTIONENTRY,
  197.                                   (ShowHighlight ? "1" : "0"), INIFILENAME);    
  198.         WritePrivateProfileString(OPTIONSSECTION, PLAYMOVIESENTRY,
  199.                                   (PlayMovies ? "1" : "0"), INIFILENAME);    
  200.         WritePrivateProfileString(OPTIONSSECTION, OBJECTMOMENTUMENTRY,
  201.                                   (Momentum ? "1" : "0"), INIFILENAME);
  202.         WritePrivateProfileString(OPTIONSSECTION, CENTERBACKDROP,
  203.                                   (CenterBackdrop ? "1" : "0"), INIFILENAME);
  204.         WritePrivateProfileString(OPTIONSSECTION, BACKDROPFILENAME,
  205.                                   BackdropFileName, INIFILENAME);
  206.     }
  207.     
  208.     /*
  209.      * Write the MRU files. We write all the MRU files out - the ones
  210.      * we have not used will be written out as empty strings which
  211.      * ReadSettings() will ignore.
  212.      */
  213.     for (i = 0; i < MAXMRUFILES; i++)     
  214.     {
  215.         sprintf(buffer, MRUFILEOPTION, i);
  216.         WritePrivateProfileString(MRUFILESECTION, buffer, MRUFiles[i],
  217.                                   INIFILENAME);
  218.     }                              
  219. }
  220.  
  221. /**********************************************************************/
  222.  
  223. /*
  224.  * Append the MRU files to the file menu.
  225.  */
  226. void
  227. AppendMRUFileMenuItems(HWND window)
  228. {
  229.     HMENU fileMenu;
  230.     int   i;
  231.     char  buffer[_MAX_PATH];
  232.     
  233.     if (NumMRUFiles != 0)
  234.     {
  235.         fileMenu = GetSubMenu(GetMenu(window), 0);
  236.         AppendMenu(fileMenu, MF_SEPARATOR, IDM_FILE_MRUSEPARATOR, NULL);
  237.         for (i = 0; i < NumMRUFiles; i++)
  238.         {
  239.             sprintf(buffer, "&%d %s", i + 1, MRUFiles[i]);
  240.             AppendMenu(fileMenu, MF_ENABLED, IDM_FILE_MRUFILE + i, buffer);
  241.         }
  242.     }
  243. }
  244.  
  245. /**********************************************************************/
  246.  
  247. /*
  248.  * Delete the MRU files from the file menu.
  249.  */
  250. void
  251. DeleteMRUFileMenuItems(HWND window)
  252. {
  253.     HMENU fileMenu;
  254.     int   i;
  255.     
  256.     if (NumMRUFiles != 0)
  257.     {
  258.         fileMenu = GetSubMenu(GetMenu(window), 0);
  259.         DeleteMenu(fileMenu, IDM_FILE_MRUSEPARATOR, MF_BYCOMMAND);
  260.         for (i = 0; i < NumMRUFiles; i++)
  261.             DeleteMenu(fileMenu, IDM_FILE_MRUFILE + i, MF_BYCOMMAND);
  262.     }
  263. }
  264.  
  265. /**********************************************************************/
  266.  
  267. /*
  268.  * Add a new file to the MRU file list.
  269.  */
  270. void
  271. AddFileToMRUFileList(char *fileName)
  272. {
  273.     char *tmp;
  274.     int   i;
  275.     
  276.     /*
  277.      * Scroll the MRU list of file names to make room for the new
  278.      * file.
  279.      */
  280.     tmp = MRUFiles[MAXMRUFILES - 1];
  281.     for (i = (MAXMRUFILES - 1); i > 0; i--)
  282.         MRUFiles[i] = MRUFiles[i - 1];
  283.     MRUFiles[0] = tmp;
  284.     
  285.     /*
  286.      * Add the new file.
  287.      */
  288.     strcpy(MRUFiles[0], fileName);
  289.     NumMRUFiles++;
  290.     if (NumMRUFiles > MAXMRUFILES)
  291.         NumMRUFiles = MAXMRUFILES;
  292. }
  293.  
  294. /**********************************************************************/
  295.