OSDN Git Service

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