root/cafu/trunk/CaWE/Options.cpp

Revision 455, 11.4 KB (checked in by Carsten, 4 months ago)

Updated copyright banners (in C++ files).

Line 
1/*
2=================================================================================
3This file is part of Cafu, the open-source game engine and graphics engine
4for multiplayer, cross-platform, real-time 3D action.
5Copyright (C) 2002-2012 Carsten Fuchs Software.
6
7Cafu is free software: you can redistribute it and/or modify it under the terms
8of the GNU General Public License as published by the Free Software Foundation,
9either version 3 of the License, or (at your option) any later version.
10
11Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with Cafu. If not, see <http://www.gnu.org/licenses/>.
17
18For support and more information about Cafu, visit us at <http://www.cafu.de>.
19=================================================================================
20*/
21
22#include "Options.hpp"
23#include "GameConfig.hpp"
24
25#include "wx/wx.h"
26#include "wx/confbase.h"
27#include "wx/datetime.h"
28#include "wx/fileconf.h"
29#include "wx/stdpaths.h"
30#include "wx/dir.h"
31#include "wx/filename.h"
32
33
34OptionsT::~OptionsT()
35{
36    DeleteGameConfigs();
37}
38
39
40static bool CompareGameConfigs(GameConfigT* const& GC1, GameConfigT* const& GC2)
41{
42    return wxStricmp(GC1->Name, GC2->Name)<0;
43}
44
45
46void OptionsT::Init()
47{
48    time_t Ticks=wxConfigBase::Get()->Read("Configured/Installed", 0L);
49
50    if (Ticks==0)
51    {
52        // If we are reading this config for the very first time,
53        // set the "ticks since installed" to the current time.
54        Ticks=wxDateTime::Now().GetTicks();
55        wxConfigBase::Get()->Write("Configured/Installed", long(Ticks));
56    }
57
58    DaysSinceInstalled=wxDateTime::Now().Subtract(wxDateTime(Ticks)).GetDays();
59
60
61    // Read settings.
62    wxConfigBase::Get()->SetPath("General");
63    wxConfigBase::Get()->Read("Undo Levels",       &general.UndoLevels,        50   );
64    wxConfigBase::Get()->Read("Locking Textures",  &general.LockingTextures,   true );
65    wxConfigBase::Get()->Read("UVs Face Aligned",  &general.NewUVsFaceAligned, false);
66#ifdef __WXMSW__
67    wxConfigBase::Get()->Read("Engine Executable", &general.EngineExe, "Cafu.exe");
68    wxConfigBase::Get()->Read("BSP Executable",    &general.BSPExe,    "CaBSP.exe");
69    wxConfigBase::Get()->Read("Light Executable",  &general.LightExe,  "CaLight.exe");
70    wxConfigBase::Get()->Read("PVS Executable",    &general.PVSExe,    "CaPVS.exe");
71#else
72    wxConfigBase::Get()->Read("Engine Executable", &general.EngineExe, "./Cafu");
73    wxConfigBase::Get()->Read("BSP Executable",    &general.BSPExe,    "./CaBSP");
74    wxConfigBase::Get()->Read("Light Executable",  &general.LightExe,  "./CaLight");
75    wxConfigBase::Get()->Read("PVS Executable",    &general.PVSExe,    "./CaPVS");
76#endif
77    wxConfigBase::Get()->SetPath("..");
78
79    wxConfigBase::Get()->SetPath("Views2D");
80    wxConfigBase::Get()->Read("Draw Vertices",     &view2d.DrawVertices,      true );
81    wxConfigBase::Get()->Read("SelectByHandles",   &view2d.SelectByHandles,   false);
82    wxConfigBase::Get()->Read("ShowEntityInfo",    &view2d.ShowEntityInfo,    true );
83    wxConfigBase::Get()->Read("ShowEntityTargets", &view2d.ShowEntityTargets, false);
84    wxConfigBase::Get()->Read("UseGroupColors",    &view2d.UseGroupColors,    true );
85    wxConfigBase::Get()->SetPath("..");
86
87    wxConfigBase::Get()->SetPath("Views3D");
88    wxConfigBase::Get()->Read("Reverse Y",         &view3d.ReverseY,          false);
89    wxConfigBase::Get()->Read("BackPlane",         &view3d.BackPlane,         6000);
90    wxConfigBase::Get()->Read("ModelDistance",     &view3d.ModelDistance,     3000);
91    wxConfigBase::Get()->Read("AnimateModels",     &view3d.AnimateModels,     true);
92    wxConfigBase::Get()->Read("MaxCameraVelocity", &view3d.MaxCameraVelocity, 1000);
93    wxConfigBase::Get()->Read("TimeToMaxSpeed",    &view3d.TimeToMaxSpeed,    500);
94    wxConfigBase::Get()->Read("MouseSensitivity",  &view3d.MouseSensitivity,  0.4f);
95    wxConfigBase::Get()->Read("SplitPlanesDepth",  &view3d.SplitPlanesDepth,  0);
96    wxConfigBase::Get()->SetPath("..");
97
98    wxConfigBase::Get()->SetPath("Grid");
99    wxConfigBase::Get()->Read("InitialSpacing",    &Grid.InitialSpacing,    8);
100    wxConfigBase::Get()->Read("MinPixelSpacing",   &Grid.MinPixelSpacing,   4);
101    wxConfigBase::Get()->Read("UseDottedGrid",     &Grid.UseDottedGrid,     false);
102    wxConfigBase::Get()->Read("ShowHighlight1",    &Grid.ShowHighlight1,    true);
103    wxConfigBase::Get()->Read("SpacingHighlight1", &Grid.SpacingHighlight1, 64);
104    wxConfigBase::Get()->Read("ShowHighlight2",    &Grid.ShowHighlight2,    true);
105    wxConfigBase::Get()->Read("SpacingHighlight2", &Grid.SpacingHighlight2, 1024);
106    wxConfigBase::Get()->Read("ColorBackground",   &Grid.ColorBackground,   wxColour(0, 0, 0));
107    wxConfigBase::Get()->Read("ColorBaseGrid",     &Grid.ColorBaseGrid,     wxColour(32, 32, 32));
108    wxConfigBase::Get()->Read("ColorHighlight1",   &Grid.ColorHighlight1,   wxColour(64, 64, 64));
109    wxConfigBase::Get()->Read("ColorHighlight2",   &Grid.ColorHighlight2,   wxColour(90, 53, 37));
110    wxConfigBase::Get()->Read("ColorAxes",         &Grid.ColorAxes,         wxColour(37, 90, 37));
111    wxConfigBase::Get()->SetPath("..");
112
113    // Assign the remaining colors.
114    colors.Vertex       =wxColour(255, 255, 255);
115    colors.ToolHandle   =wxColour(255, 255, 255);
116    colors.ToolDrag     =wxColour(255, 255, 0);
117    colors.ToolSelection=wxColour(255, 0, 0);
118    colors.Selection    =wxColour(255, 0, 0);
119    colors.SelectedFace =wxColour(220, 0, 0);
120    colors.SelectedEdge =wxColour(250, 220, 0);
121    colors.ToolMorph    =wxColour(255, 0, 0);
122    colors.Entity       =wxColour(220, 30, 220);
123
124
125    wxFileConfig CfgFile(wxTheApp->GetAppName(), wxTheApp->GetVendorName(), wxStandardPaths::Get().GetUserDataDir()+"/Games.config");
126    wxDir        GamesDir("Games");     // Should also work under Linux, otherwise use "./Games".
127    wxString     GameName;
128
129    // Iterate through all subdirectories of "Games/" and create a game configuration for each.
130    // The creation of a game configuration succeeds (that is, no exception is thrown) only when
131    // the related "EntityClassDefs.lua" script could properly be loaded and processed.
132    // Other game configuration specific data is initialized from the relevant section from the CfgFile.
133    for (bool more=GamesDir.GetFirst(&GameName, "", wxDIR_DIRS); more; more=GamesDir.GetNext(&GameName))
134    {
135        CfgFile.SetPath("GameConfig_"+GameName);
136
137        try
138        {
139            GameConfigs.PushBack(new GameConfigT(CfgFile, GameName, "Games/"+GameName));
140        }
141        catch (const GameConfigT::InitErrorT&)
142        {
143            // Just do nothing but warn the user if there was an error initializing the game config,
144            // that is, the game config will not be pushed back to the GameConfigs array.
145            wxMessageBox("Configuration for game \""+GameName+"\" could not be loaded!", "Warning", wxOK | wxICON_EXCLAMATION);
146        }
147
148        CfgFile.SetPath("..");
149    }
150
151    GameConfigs.QuickSort(CompareGameConfigs);
152
153    // If we couldn't load any game configurations, pop up the options screen.
154    /* if (GameConfigs.Size()==0)
155    {
156        wxMessageBox("I scanned the Games subdirectory for game configurations,\n"
157                     "but didn't find any.\n"
158                     "You need to have at least one game configuration inside your\n"
159                     "Games directory for the editor to work.\n"
160                     "Please re-install Cafu or contact the Cafu forums for help.", "No game configurations found", wxOK | wxICON_ERROR);
161    } */
162}
163
164
165void OptionsT::Write() const
166{
167    wxConfigBase::Get()->SetPath("General");
168    wxConfigBase::Get()->Write("Undo Levels",       general.UndoLevels       );
169    wxConfigBase::Get()->Write("Locking Textures",  general.LockingTextures  );
170    wxConfigBase::Get()->Write("UVs Face Aligned",  general.NewUVsFaceAligned);
171    wxConfigBase::Get()->Write("Engine Executable", general.EngineExe        );
172    wxConfigBase::Get()->Write("BSP Executable",    general.BSPExe           );
173    wxConfigBase::Get()->Write("Light Executable",  general.LightExe         );
174    wxConfigBase::Get()->Write("PVS Executable",    general.PVSExe           );
175    wxConfigBase::Get()->SetPath("..");
176
177    wxConfigBase::Get()->SetPath("Views2D");
178    wxConfigBase::Get()->Write("Draw Vertices",     view2d.DrawVertices     );
179    wxConfigBase::Get()->Write("SelectByHandles",   view2d.SelectByHandles  );
180    wxConfigBase::Get()->Write("ShowEntityInfo",    view2d.ShowEntityInfo   );
181    wxConfigBase::Get()->Write("ShowEntityTargets", view2d.ShowEntityTargets);
182    wxConfigBase::Get()->Write("UseGroupColors",    view2d.UseGroupColors   );
183    wxConfigBase::Get()->SetPath("..");
184
185    wxConfigBase::Get()->SetPath("Views3D");
186    wxConfigBase::Get()->Write("Reverse Y",         view3d.ReverseY         );
187    wxConfigBase::Get()->Write("BackPlane",         view3d.BackPlane        );
188    wxConfigBase::Get()->Write("ModelDistance",     view3d.ModelDistance    );
189    wxConfigBase::Get()->Write("AnimateModels",     view3d.AnimateModels    );
190    wxConfigBase::Get()->Write("MaxCameraVelocity", view3d.MaxCameraVelocity);
191    wxConfigBase::Get()->Write("TimeToMaxSpeed",    view3d.TimeToMaxSpeed   );
192    wxConfigBase::Get()->Write("MouseSensitivity",  view3d.MouseSensitivity );
193    wxConfigBase::Get()->Write("SplitPlanesDepth",  view3d.SplitPlanesDepth );
194    wxConfigBase::Get()->SetPath("..");
195
196    wxConfigBase::Get()->SetPath("Grid");
197    wxConfigBase::Get()->Write("InitialSpacing",    Grid.InitialSpacing     );
198    wxConfigBase::Get()->Write("MinPixelSpacing",   Grid.MinPixelSpacing    );
199    wxConfigBase::Get()->Write("UseDottedGrid",     Grid.UseDottedGrid      );
200    wxConfigBase::Get()->Write("ShowHighlight1",    Grid.ShowHighlight1     );
201    wxConfigBase::Get()->Write("SpacingHighlight1", Grid.SpacingHighlight1  );
202    wxConfigBase::Get()->Write("ShowHighlight2",    Grid.ShowHighlight2     );
203    wxConfigBase::Get()->Write("SpacingHighlight2", Grid.SpacingHighlight2  );
204    wxConfigBase::Get()->Write("ColorBackground",   Grid.ColorBackground    );
205    wxConfigBase::Get()->Write("ColorBaseGrid",     Grid.ColorBaseGrid      );
206    wxConfigBase::Get()->Write("ColorHighlight1",   Grid.ColorHighlight1    );
207    wxConfigBase::Get()->Write("ColorHighlight2",   Grid.ColorHighlight2    );
208    wxConfigBase::Get()->Write("ColorAxes",         Grid.ColorAxes          );
209    wxConfigBase::Get()->SetPath("..");
210
211    // Write the game configs. This is in an external file so we can distribute it with CaWE as a set of defaults.
212    wxFileConfig CfgFile(wxTheApp->GetAppName(), wxTheApp->GetVendorName(), wxStandardPaths::Get().GetUserDataDir()+"/Games.config");
213
214    for (unsigned long ConfigNr=0; ConfigNr<GameConfigs.Size(); ConfigNr++)
215    {
216        // Each game configuration is stored in an own group (directory).
217        CfgFile.SetPath("GameConfig_"+GameConfigs[ConfigNr]->Name);
218        GameConfigs[ConfigNr]->Save(CfgFile);
219        CfgFile.SetPath("..");
220    }
221}
222
223
224void OptionsT::DeleteGameConfigs()
225{
226    for (unsigned long i=0; i<GameConfigs.Size(); i++)
227        delete GameConfigs[i];
228
229    GameConfigs.Clear();
230}
Note: See TracBrowser for help on using the browser.