00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 using System;
00018 using System.Collections;
00019 using System.Collections.Generic;
00020 using System.Text;
00021 using System.Diagnostics;
00022 using System.IO;
00023 using System.Windows.Forms;
00024 using Microsoft.DirectX;
00025 using Microsoft.DirectX.Direct3D;
00026 using Microsoft.Samples.DirectX.UtilityToolkit;
00027 using Microsoft.DirectX.DirectSound;
00028 using DXGfxLib;
00029
00030 namespace SampleSceneLoader
00031 {
00032 class Loader : IFrameworkCallback, IDeviceCreation
00033 {
00034
00035 private Framework sampleFramework = null;
00036 private Font statsFont = null;
00037 private Sprite textSprite = null;
00038
00039 private bool isHelpShowing = true;
00040 private Dialog hud = null;
00041 private Dialog missionUi = null;
00042 private Matrix worldCenter;
00043
00044
00045 private const int ToggleFullscreen = 1;
00046 private const int ToggleReference = 3;
00047 private const int ChangeDevice = 4;
00048
00049
00050 private OutDoorScene outdoorScene;
00051 private Matrix computedViewMat;
00052 private const int MissionText = 1;
00053
00054
00055 private Microsoft.DirectX.DirectSound.Device soundDevice = null;
00056
00057 private StateBlock defaultState = null;
00058
00059 public string selectedFile = "";
00060
00061 #region Creation
00063 public Loader(Framework f)
00064 {
00065
00066 sampleFramework = f;
00067
00068 hud = new Dialog(sampleFramework);
00069 missionUi = new Dialog(sampleFramework);
00070
00071 DXGfxManager.GetGlobalInstance().Init(sampleFramework);
00072 }
00073 #endregion
00074
00075 public void InitSound()
00076 {
00077 soundDevice = new Microsoft.DirectX.DirectSound.Device();
00078 soundDevice.SetCooperativeLevel(this.sampleFramework.Window.Handle, CooperativeLevel.Priority);
00079 }
00080
00087 public bool IsDeviceAcceptable(Microsoft.DirectX.Direct3D.Caps caps, Format adapterFormat, Format backBufferFormat, bool windowed)
00088 {
00089
00090 if (caps.PixelShaderVersion < new Version(1, 1))
00091 return false;
00092
00093
00094 if (!Manager.CheckDeviceFormat(caps.AdapterOrdinal, caps.DeviceType, adapterFormat,
00095 Usage.QueryPostPixelShaderBlending, ResourceType.Textures, backBufferFormat))
00096 return false;
00097
00098 return true;
00099 }
00100
00109 public void ModifyDeviceSettings(DeviceSettings settings, Microsoft.DirectX.Direct3D.Caps caps)
00110 {
00111
00112
00113
00114
00115 if (!caps.DeviceCaps.SupportsHardwareTransformAndLight)
00116 {
00117 settings.BehaviorFlags = CreateFlags.SoftwareVertexProcessing;
00118 settings.BehaviorFlags = CreateFlags.MixedVertexProcessing;
00119 }
00120 else
00121 {
00122 settings.BehaviorFlags = CreateFlags.HardwareVertexProcessing;
00123 }
00124
00125
00126
00127 if ((caps.DeviceCaps.SupportsPureDevice) &&
00128 ((settings.BehaviorFlags & CreateFlags.HardwareVertexProcessing) != 0))
00129 {
00130
00131
00132
00133 }
00134
00135
00136 #if(DEBUG_VS)
00137 if (settings.DeviceType != DeviceType.Reference )
00138 {
00139 settings.BehaviorFlags &= ~CreateFlags.HardwareVertexProcessing;
00140 settings.BehaviorFlags |= CreateFlags.SoftwareVertexProcessing;
00141 }
00142 #endif
00143 #if(DEBUG_PS)
00144 settings.DeviceType = DeviceType.Reference;
00145 #endif
00146
00147
00148 if (settings.DeviceType == DeviceType.Reference)
00149 {
00150 Utility.DisplaySwitchingToRefWarning(sampleFramework, "Sample");
00151 }
00152 }
00153
00161 private void OnCreateDevice(object sender, DeviceEventArgs e)
00162 {
00163
00164 statsFont = ResourceCache.GetGlobalInstance().CreateFont(e.Device, 15, 0, FontWeight.Bold, 1, false, CharacterSet.Default,
00165 Precision.Default, FontQuality.Default, PitchAndFamily.FamilyDoNotCare | PitchAndFamily.DefaultPitch
00166 , "Arial");
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 ShaderFlags shaderFlags = ShaderFlags.NotCloneable;
00180 #if(DEBUG_VS)
00181 shaderFlags |= ShaderFlags.ForceVertexShaderSoftwareNoOptimizations;
00182 #endif
00183 #if(DEBUG_PS)
00184 shaderFlags |= ShaderFlags.ForcePixelShaderSoftwareNoOptimizations;
00185 #endif
00186
00187
00188 DXGfxManager.GetGlobalInstance().OnCreateDevice(sender, e);
00189
00190 DXGfxManager.GetGlobalInstance().Load(selectedFile);
00191
00192
00193 DXGfxManager.GetGlobalInstance().CurrentCamera.SetViewParameters(new Vector3(0.0f, 0.0f, -5.0f), Vector3.Empty);
00194 }
00195
00196 private void ProceduralSceneCreation(Microsoft.DirectX.Direct3D.Device device)
00197 {
00198
00199 outdoorScene = DXGfxManager.GetGlobalInstance().NewScene("Sample!!", 1000.0f, 2);
00200
00201
00202
00203 outdoorScene.LoadTerrain(device, "newheigthmap.bmp");
00204
00205
00206 Water2 theWater = new Water2(2000);
00207 outdoorScene.SceneWater = theWater;
00208
00209 SkyBox theSky = new SkyBox(device, "skybox2.x", 160);
00210
00211
00212 FrameworkMesh tigerMesh = new FrameworkMesh(device, "tiger.x");
00213 SceneObject sceneObject = outdoorScene.Attach(tigerMesh);
00214 DXGfxManager.GetGlobalInstance().CurrentAvatar = new Avatar(sceneObject);
00215
00216 Random rand = new Random();
00217 float tigerx = rand.Next(-500, 500);
00218 float tigerz = rand.Next(-500, 500);
00219 float tigery = sceneObject.extents.Y * 0.5f + outdoorScene.GetHeigth(tigerx, tigerz);
00220
00221 while (tigery < 35.0f)
00222 {
00223 tigerx = rand.Next(-500, 500);
00224 tigerz = rand.Next(-500, 500);
00225 tigery = sceneObject.extents.Y * 0.5f + outdoorScene.GetHeigth(tigerx, tigerz);
00226 }
00227 sceneObject.WorldMat = Matrix.Translation(tigerx, tigery, tigerz);
00228
00229
00230
00231
00232
00233
00234 MeshRessource tree = new MeshRessource(device, "sapin.x");
00236
00237 for (int i = 0; i < 2000; i++)
00238 {
00239 ObjectFromMeshRessource treeObj = new ObjectFromMeshRessource();
00240 treeObj.Attach(tree);
00241 float x = rand.Next(-500, 500);
00242 float z = rand.Next(-500, 500);
00243 float y = outdoorScene.GetHeigth(x, z);
00244
00245 while (y < 35.0f)
00246 {
00247 x = rand.Next(-500, 500);
00248 z = rand.Next(-500, 500);
00249 y = outdoorScene.GetHeigth(x, z);
00250 }
00251 treeObj.Position = new Vector3(x, y, z);
00252 outdoorScene.AttachStatic(treeObj);
00253
00254 }
00255
00256
00257
00258
00259
00260
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 MeshRessource tour = new MeshRessource(device, "tour.x");
00281
00282 for (int i = 0; i < 100; i++)
00283 {
00284 ObjectFromMeshRessource tourObj = new ObjectFromMeshRessource();
00285 tourObj.Attach(tour);
00286 float x = rand.Next(-500, 500);
00287 float z = rand.Next(-500, 500);
00288 float y = outdoorScene.GetHeigth(x, z);
00289
00290 while (y < 60.0f)
00291 {
00292 x = rand.Next(-500, 500);
00293 z = rand.Next(-500, 500);
00294 y = outdoorScene.GetHeigth(x, z);
00295 }
00296 Matrix s = Matrix.Scaling(2.0f, 3.0f, 2.0f);
00297 tourObj.ApplyLocalTransform(s);
00298 tourObj.Position = new Vector3(x, y, z);
00299 outdoorScene.AttachStatic(tourObj);
00300 }
00301
00302 DXGfxManager.GetGlobalInstance().CurrentCamera.Person = sceneObject;
00303 DXGfxManager.GetGlobalInstance().CurrentCamera.camera.positionConstraint = outdoorScene;
00304 DXGfxManager.GetGlobalInstance().CurrentCamera.Person.positionConstraint = outdoorScene;
00305 }
00306
00314 private void OnResetDevice(object sender, DeviceEventArgs e)
00315 {
00316 SurfaceDescription desc = e.BackBufferDescription;
00317
00318 textSprite = new Sprite(e.Device);
00319
00320
00321 DXGfxManager.GetGlobalInstance().OnDeviceReset(sender, e);
00322
00323
00324 hud.SetLocation(desc.Width - 170, 0);
00325 hud.SetSize(170, 170);
00326
00327
00328
00329
00330 missionUi.SetLocation(0, desc.Height - 120);
00331 missionUi.SetSize(170, 120);
00332 }
00333
00341 private void OnLostDevice(object sender, EventArgs e)
00342 {
00343 if (outdoorScene != null)
00344 {
00345 outdoorScene.CleanOnLostDevice();
00346 }
00347
00348 if (textSprite != null)
00349 {
00350 textSprite.Dispose();
00351 textSprite = null;
00352 }
00353 }
00354
00361 private void OnDestroyDevice(object sender, EventArgs e)
00362 {
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 }
00379
00386 public void OnFrameMove(Microsoft.DirectX.Direct3D.Device device, double appTime, float elapsedTime)
00387 {
00388
00389 DXGfxManager.GetGlobalInstance().CurrentInputHandler.FrameMove(elapsedTime);
00390 }
00391
00398 public void OnFrameRender(Microsoft.DirectX.Direct3D.Device device, double appTime, float elapsedTime)
00399 {
00400 bool beginSceneCalled = false;
00401
00402 if (defaultState == null)
00403 {
00404 defaultState = new StateBlock(device, StateBlockType.All);
00405 defaultState.Capture();
00406 }
00407
00409
00410
00411
00412
00413 DXGfxManager.GetGlobalInstance().UpdateOutdoorScene(elapsedTime, appTime);
00414
00415 defaultState.Apply();
00416
00417
00418 device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x00424B79, 1.0f, 0);
00419 DXGfxManager.GetGlobalInstance().DrawOutdoorScene();
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 }
00447
00452 private void RenderText()
00453 {
00454 TextHelper txtHelper = new TextHelper(statsFont, textSprite, 15);
00455
00456
00457 txtHelper.Begin();
00458 txtHelper.SetInsertionPoint(5, 5);
00459 txtHelper.SetForegroundColor(System.Drawing.Color.Yellow);
00460 txtHelper.DrawTextLine(sampleFramework.FrameStats);
00461 txtHelper.DrawTextLine(sampleFramework.DeviceStats);
00462
00463 txtHelper.SetForegroundColor(System.Drawing.Color.White);
00464
00465 txtHelper.End();
00466 }
00467
00474 private void OnKeyEvent(object sender, System.Windows.Forms.KeyEventArgs e)
00475 {
00476 switch (e.KeyCode)
00477 {
00478 case System.Windows.Forms.Keys.F1:
00479 isHelpShowing = !isHelpShowing;
00480 break;
00481 }
00482 }
00483
00489 public IntPtr OnMsgProc(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam, ref bool noFurtherProcessing)
00490 {
00491
00492 noFurtherProcessing = hud.MessageProc(hWnd, msg, wParam, lParam);
00493 if (noFurtherProcessing)
00494 return IntPtr.Zero;
00495
00496 noFurtherProcessing = missionUi.MessageProc(hWnd, msg, wParam, lParam);
00497 if (noFurtherProcessing)
00498 return IntPtr.Zero;
00499
00500
00501
00502 if (DXGfxManager.GetGlobalInstance().CurrentInputHandler != null)
00503 {
00504 DXGfxManager.GetGlobalInstance().CurrentInputHandler.OnMsgProc(hWnd, msg, wParam, lParam);
00505 }
00506
00507 return IntPtr.Zero;
00508 }
00509
00513 public void InitializeApplication()
00514 {
00515 int y = 10;
00516
00517
00518 Microsoft.Samples.DirectX.UtilityToolkit.Button fullScreen = hud.AddButton(ToggleFullscreen, "Toggle full screen", 35, y, 125, 22);
00519 Microsoft.Samples.DirectX.UtilityToolkit.Button toggleRef = hud.AddButton(ToggleReference, "Toggle reference (F3)", 35, y += 24, 125, 22);
00520 Microsoft.Samples.DirectX.UtilityToolkit.Button changeDevice = hud.AddButton(ChangeDevice, "Change Device (F2)", 35, y += 24, 125, 22);
00521
00522 fullScreen.Click += new EventHandler(OnFullscreenClicked);
00523 toggleRef.Click += new EventHandler(OnRefClicked);
00524 changeDevice.Click += new EventHandler(OnChangeDevicClicked);
00525
00526
00527 Microsoft.Samples.DirectX.UtilityToolkit.Button missionText = missionUi.AddButton(MissionText, "Salut!!", 0, 0, 170, 120);
00528 missionText.Click += new EventHandler(missionText_Click);
00529 }
00530
00531 void missionText_Click(object sender, EventArgs e)
00532 {
00533 missionUi.GetButton(MissionText).SetText("Button Clicked!!");
00534 }
00535
00537 private void OnChangeDevicClicked(object sender, EventArgs e)
00538 {
00539 sampleFramework.ShowSettingsDialog(!sampleFramework.IsD3DSettingsDialogShowing);
00540 }
00541
00543 private void OnFullscreenClicked(object sender, EventArgs e)
00544 {
00545 sampleFramework.ToggleFullscreen();
00546 }
00547
00549 private void OnRefClicked(object sender, EventArgs e)
00550 {
00551 sampleFramework.ToggleReference();
00552 }
00553
00558 [STAThread]
00559 static int Main()
00560 {
00561
00562
00563 System.Windows.Forms.Application.EnableVisualStyles();
00564 using (Framework sampleFramework = new Framework())
00565 {
00566 Loader sample = new Loader(sampleFramework);
00567
00568
00569
00570
00571
00572
00573 sample.selectedFile = "waterscene.xml";
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583 sampleFramework.Disposing += new EventHandler(sample.OnDestroyDevice);
00584 sampleFramework.DeviceLost += new EventHandler(sample.OnLostDevice);
00585 sampleFramework.DeviceCreated += new DeviceEventHandler(sample.OnCreateDevice);
00586 sampleFramework.DeviceReset += new DeviceEventHandler(sample.OnResetDevice);
00587
00588 sampleFramework.SetWndProcCallback(new WndProcCallback(sample.OnMsgProc));
00589
00590 sampleFramework.SetCallbackInterface(sample);
00591
00592
00593
00594
00595 sampleFramework.SetCursorSettings(true, true);
00596
00597
00598
00599
00600
00601
00602 sample.InitializeApplication();
00603
00604
00605
00606
00607 sampleFramework.Initialize(true, true, true);
00608 sampleFramework.CreateWindow("Sample: Using DXGfxLib");
00609
00610
00611
00612
00613
00614
00615 sample.InitSound();
00616
00617
00618 sampleFramework.Window.KeyDown += new System.Windows.Forms.KeyEventHandler(sample.OnKeyEvent);
00619 sampleFramework.CreateDevice(0, true, Framework.DefaultSizeWidth, Framework.DefaultSizeHeight,
00620 sample);
00621
00622
00623
00624
00625
00626
00627 sampleFramework.MainLoop();
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646 return sampleFramework.ExitCode;
00647 }
00648 }
00649 }
00650 }