OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / FDK17プロジェクト / コード / 01.フレームワーク / DeviceSettings / DeviceSettings.cs
1 /*\r
2 * Copyright (c) 2007-2009 SlimDX Group\r
3\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy\r
5 * of this software and associated documentation files (the "Software"), to deal\r
6 * in the Software without restriction, including without limitation the rights\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
8 * copies of the Software, and to permit persons to whom the Software is\r
9 * furnished to do so, subject to the following conditions:\r
10\r
11 * The above copyright notice and this permission notice shall be included in\r
12 * all copies or substantial portions of the Software.\r
13\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
20 * THE SOFTWARE.\r
21 */\r
22 using System;\r
23 using SlimDX.Direct3D9;\r
24 namespace SampleFramework\r
25 {\r
26     /// <summary>\r
27     /// Contains settings for creating a 3D device.\r
28     /// </summary>\r
29     public class DeviceSettings : ICloneable\r
30     {\r
31         /// <summary>\r
32         /// Gets or sets the adapter ordinal.\r
33         /// </summary>\r
34         /// <value>The adapter ordinal.</value>\r
35         public int AdapterOrdinal\r
36         {\r
37             get;\r
38             set;\r
39         }\r
40 \r
41         /// <summary>\r
42         /// Gets or sets the type of the device.\r
43         /// </summary>\r
44         /// <value>The type of the device.</value>\r
45         public DeviceType DeviceType\r
46         {\r
47             get;\r
48             set;\r
49         }\r
50 \r
51         /// <summary>\r
52         /// Gets or sets the refresh rate.\r
53         /// </summary>\r
54         /// <value>The refresh rate.</value>\r
55         public int RefreshRate\r
56         {\r
57             get;\r
58             set;\r
59         }\r
60 \r
61         /// <summary>\r
62         /// Gets or sets the width of the back buffer.\r
63         /// </summary>\r
64         /// <value>The width of the back buffer.</value>\r
65         public int BackBufferWidth\r
66         {\r
67             get;\r
68             set;\r
69         }\r
70 \r
71         /// <summary>\r
72         /// Gets or sets the height of the back buffer.\r
73         /// </summary>\r
74         /// <value>The height of the back buffer.</value>\r
75         public int BackBufferHeight\r
76         {\r
77             get;\r
78             set;\r
79         }\r
80 \r
81         /// <summary>\r
82         /// Gets or sets the back buffer format.\r
83         /// </summary>\r
84         /// <value>The back buffer format.</value>\r
85         public Format BackBufferFormat\r
86         {\r
87             get;\r
88             set;\r
89         }\r
90 \r
91         /// <summary>\r
92         /// Gets or sets the back buffer count.\r
93         /// </summary>\r
94         /// <value>The back buffer count.</value>\r
95         public int BackBufferCount\r
96         {\r
97             get;\r
98             set;\r
99         }\r
100 \r
101         /// <summary>\r
102         /// Gets or sets a value indicating whether the device is windowed.\r
103         /// </summary>\r
104         /// <value><c>true</c> if windowed; otherwise, <c>false</c>.</value>\r
105         public bool Windowed\r
106         {\r
107             get;\r
108             set;\r
109         }\r
110 \r
111         /// <summary>\r
112         /// Gets or sets a value indicating whether VSync is enabled.\r
113         /// </summary>\r
114         /// <value><c>true</c> if VSync is enabled; otherwise, <c>false</c>.</value>\r
115         public bool EnableVSync\r
116         {\r
117             get;\r
118             set;\r
119         }\r
120 \r
121         /// <summary>\r
122         /// Gets or sets a value indicating whether this <see cref="DeviceSettings"/> is multithreaded.\r
123         /// </summary>\r
124         /// <value><c>true</c> if multithreaded; otherwise, <c>false</c>.</value>\r
125         /// <remarks>This only has an effect for Direct3D9 devices.</remarks>\r
126         public bool Multithreaded\r
127         {\r
128             get;\r
129             set;\r
130         }\r
131 \r
132         /// <summary>\r
133         /// Gets or sets the multisample type.\r
134         /// </summary>\r
135         /// <value>The multisample type.</value>\r
136         public MultisampleType MultisampleType\r
137         {\r
138             get;\r
139             set;\r
140         }\r
141 \r
142         /// <summary>\r
143         /// Gets or sets the multisample quality.\r
144         /// </summary>\r
145         /// <value>The multisample quality.</value>\r
146         public int MultisampleQuality\r
147         {\r
148             get;\r
149             set;\r
150         }\r
151 \r
152         /// <summary>\r
153         /// Gets or sets the depth stencil format.\r
154         /// </summary>\r
155         /// <value>The depth stencil format.</value>\r
156         public Format DepthStencilFormat\r
157         {\r
158             get;\r
159             set;\r
160         }\r
161 \r
162         /// <summary>\r
163         /// Gets or sets the Direct3D9 specific settings.\r
164         /// </summary>\r
165         /// <value>The Direct3D9 specific settings.</value>\r
166         internal Direct3D9Settings Direct3D9\r
167         {\r
168             get;\r
169             set;\r
170         }\r
171 \r
172         /// <summary>\r
173         /// Initializes a new instance of the <see cref="DeviceSettings"/> class.\r
174         /// </summary>\r
175         public DeviceSettings()\r
176         {\r
177             // set sane defaults\r
178             DeviceType = DeviceType.Hardware;\r
179             BackBufferFormat = Format.Unknown;\r
180             BackBufferCount = 1;\r
181             MultisampleType = MultisampleType.None;\r
182             DepthStencilFormat = Format.Unknown;\r
183             Windowed = true;\r
184             EnableVSync = true;\r
185         }\r
186 \r
187         /// <summary>\r
188         /// Creates a new object that is a copy of the current instance.\r
189         /// </summary>\r
190         /// <returns>\r
191         /// A new object that is a copy of this instance.\r
192         /// </returns>\r
193         public DeviceSettings Clone()\r
194         {\r
195             DeviceSettings result = new DeviceSettings();\r
196             result.DeviceType = DeviceType;\r
197             result.RefreshRate = RefreshRate;\r
198             result.BackBufferCount = BackBufferCount;\r
199             result.BackBufferFormat = BackBufferFormat;\r
200             result.BackBufferHeight = BackBufferHeight;\r
201             result.BackBufferWidth = BackBufferWidth;\r
202             result.DepthStencilFormat = DepthStencilFormat;\r
203             result.MultisampleQuality = MultisampleQuality;\r
204             result.MultisampleType = MultisampleType;\r
205             result.Windowed = Windowed;\r
206             result.EnableVSync = EnableVSync;\r
207             result.AdapterOrdinal = AdapterOrdinal;\r
208             result.Multithreaded = Multithreaded;\r
209 \r
210             if (Direct3D9 != null)\r
211                 result.Direct3D9 = Direct3D9.Clone();\r
212 \r
213             return result;\r
214         }\r
215 \r
216         /// <summary>\r
217         /// Creates a new object that is a copy of the current instance.\r
218         /// </summary>\r
219         /// <returns>\r
220         /// A new object that is a copy of this instance.\r
221         /// </returns>\r
222         object ICloneable.Clone()\r
223         {\r
224             return Clone();\r
225         }\r
226 \r
227         /// <summary>\r
228         /// Finds valid device settings based upon the desired settings.\r
229         /// </summary>\r
230         /// <param name="settings">The desired settings.</param>\r
231         /// <returns>The best valid device settings matching the input settings.</returns>\r
232                 public static DeviceSettings FindValidSettings( DeviceSettings settings )\r
233                 {\r
234                         try\r
235                         {\r
236                                 GraphicsDeviceManager.EnsureD3D9();\r
237                         }\r
238                         catch( Exception e )\r
239                         {\r
240                                 throw new NoCompatibleDevicesException( "Could not initialize Direct3D9.", e );\r
241                         }\r
242 \r
243                         if( !Enumeration9.HasEnumerated )\r
244                                 Enumeration9.Enumerate();\r
245 \r
246                         DeviceSettings newSettings = settings.Clone();\r
247                         Direct3D9Settings d3d9 = FindValidD3D9Settings( settings );\r
248                         newSettings.Direct3D9 = d3d9;\r
249                         return newSettings;\r
250                 }\r
251 \r
252         static Direct3D9Settings FindValidD3D9Settings(DeviceSettings settings)\r
253         {\r
254             Direct3D9Settings optimal = Direct3D9Settings.BuildOptimalSettings(settings);\r
255 \r
256             SettingsCombo9 bestCombo = null;\r
257             float bestRanking = -1.0f;\r
258 \r
259             foreach (AdapterInfo9 adapterInfo in Enumeration9.Adapters)\r
260             {\r
261                 DisplayMode desktopMode = GraphicsDeviceManager.Direct3D9Object.GetAdapterDisplayMode(adapterInfo.AdapterOrdinal);\r
262                 foreach (DeviceInfo9 deviceInfo in adapterInfo.Devices)\r
263                 {\r
264                     foreach (SettingsCombo9 combo in deviceInfo.DeviceSettings)\r
265                     {\r
266                         if (combo.Windowed && combo.AdapterFormat != desktopMode.Format)\r
267                             continue;\r
268 \r
269                         float ranking = Direct3D9Settings.RankSettingsCombo(combo, optimal, desktopMode);\r
270                         if (ranking > bestRanking)\r
271                         {\r
272                             bestCombo = combo;\r
273                             bestRanking = ranking;\r
274                         }\r
275                     }\r
276                 }\r
277             }\r
278 \r
279             if (bestCombo == null)\r
280                 throw new NoCompatibleDevicesException("No compatible Direct3D9 devices found.");\r
281 \r
282             return Direct3D9Settings.BuildValidSettings(bestCombo, optimal);\r
283         }\r
284     }\r
285 }\r