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 Microsoft.DirectX;
00023 using Microsoft.DirectX.Direct3D;
00024 using Microsoft.Samples.DirectX.UtilityToolkit;
00025 using Microsoft.DirectX.DirectSound;
00026 using DXGfxLib;
00027 using Game;
00028
00029 namespace Sample
00030 {
00031 class Sample : IFrameworkCallback, IDeviceCreation
00032 {
00033
00034 private Framework sampleFramework = null;
00035 private Font statsFont = null;
00036 private Sprite textSprite = null;
00037
00038 private bool isHelpShowing = true;
00039 private Dialog hud = null;
00040 private Dialog missionUi = null;
00041 private Matrix worldCenter;
00042
00043
00044 private const int ToggleFullscreen = 1;
00045 private const int ToggleReference = 3;
00046 private const int ChangeDevice = 4;
00047
00048
00049 private OutDoorScene outdoorScene;
00050 private Matrix computedViewMat;
00051 private const int MissionText = 1;
00052
00053
00054
00055 private World world;
00056 private Boat boat;
00057 private Vegetals veg;
00058 private double lastAIUpdate = 0.0f;
00059 private Hashtable worldEntities = new Hashtable();
00060
00061
00062 private Microsoft.DirectX.DirectSound.Device soundDevice = null;
00063
00064 private StateBlock defaultState = null;
00065
00066 #region Creation
00068 public Sample(Framework f)
00069 {
00070
00071 sampleFramework = f;
00072
00073 hud = new Dialog(sampleFramework);
00074 missionUi = new Dialog(sampleFramework);
00075
00076 DXGfxManager.GetGlobalInstance().Init(sampleFramework);
00077 }
00078 #endregion
00079
00080 public void InitSound()
00081 {
00082 soundDevice = new Microsoft.DirectX.DirectSound.Device();
00083 soundDevice.SetCooperativeLevel(this.sampleFramework.Window.Handle, CooperativeLevel.Priority);
00084 }
00085
00092 public bool IsDeviceAcceptable(Microsoft.DirectX.Direct3D.Caps caps, Format adapterFormat, Format backBufferFormat, bool windowed)
00093 {
00094
00095 if (caps.PixelShaderVersion < new Version(1, 1))
00096 return false;
00097
00098
00099 if (!Manager.CheckDeviceFormat(caps.AdapterOrdinal, caps.DeviceType, adapterFormat,
00100 Usage.QueryPostPixelShaderBlending, ResourceType.Textures, backBufferFormat))
00101 return false;
00102
00103 return true;
00104 }
00105
00114 public void ModifyDeviceSettings(DeviceSettings settings, Microsoft.DirectX.Direct3D.Caps caps)
00115 {
00116
00117
00118
00119
00120 if(!caps.DeviceCaps.SupportsHardwareTransformAndLight)
00121 {
00122 settings.BehaviorFlags = CreateFlags.SoftwareVertexProcessing;
00123 settings.BehaviorFlags = CreateFlags.MixedVertexProcessing;
00124 }
00125 else
00126 {
00127 settings.BehaviorFlags = CreateFlags.HardwareVertexProcessing;
00128 }
00129
00130
00131
00132 if ((caps.DeviceCaps.SupportsPureDevice) &&
00133 ((settings.BehaviorFlags & CreateFlags.HardwareVertexProcessing) != 0))
00134 {
00135
00136
00137
00138 }
00139
00140
00141 #if(DEBUG_VS)
00142 if (settings.DeviceType != DeviceType.Reference )
00143 {
00144 settings.BehaviorFlags &= ~CreateFlags.HardwareVertexProcessing;
00145 settings.BehaviorFlags |= CreateFlags.SoftwareVertexProcessing;
00146 }
00147 #endif
00148 #if(DEBUG_PS)
00149 settings.DeviceType = DeviceType.Reference;
00150 #endif
00151
00152
00153 if (settings.DeviceType == DeviceType.Reference)
00154 {
00155 Utility.DisplaySwitchingToRefWarning(sampleFramework, "Sample");
00156 }
00157 }
00158
00166 private void OnCreateDevice(object sender, DeviceEventArgs e)
00167 {
00168
00169 statsFont = ResourceCache.GetGlobalInstance().CreateFont(e.Device, 15, 0, FontWeight.Bold, 1, false, CharacterSet.Default,
00170 Precision.Default, FontQuality.Default, PitchAndFamily.FamilyDoNotCare | PitchAndFamily.DefaultPitch
00171 , "Arial");
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 ShaderFlags shaderFlags = ShaderFlags.NotCloneable;
00185 #if(DEBUG_VS)
00186 shaderFlags |= ShaderFlags.ForceVertexShaderSoftwareNoOptimizations;
00187 #endif
00188 #if(DEBUG_PS)
00189 shaderFlags |= ShaderFlags.ForcePixelShaderSoftwareNoOptimizations;
00190 #endif
00191
00192
00193 DXGfxManager.GetGlobalInstance().OnCreateDevice(sender, e);
00194
00195
00196 AABBox desiredBBox = new AABBox(new BBox(new Vector3(0.0f, 0.0f, 0.0f), 1000.0f));
00197 outdoorScene = new OutDoorScene(desiredBBox, 2);
00198 DXGfxManager.GetGlobalInstance().SetScene(outdoorScene);
00199
00200 world = new World();
00201 world.Attach(outdoorScene);
00202
00203
00204 DXGfxManager.GetGlobalInstance().CurrentCamera.SetViewParameters(new Vector3(0.0f, 0.0f, -5.0f), Vector3.Empty);
00205
00206
00207 outdoorScene.LoadTerrain(e.Device, "heigthmap.bmp");
00208
00209
00210 Water2 theWater = new Water2(2000);
00211 outdoorScene.SceneWater = theWater;
00212
00213
00214
00215 SkyDome theSky = new SkyDome();
00216 theSky.LoadFromFile(e.Device, "domei.x");
00217 outdoorScene.SceneSky = theSky;
00218
00219
00220
00221 FrameworkMesh boatMesh = new FrameworkMesh(e.Device, "tiger.x");
00222 SceneObject sceneObject = outdoorScene.Attach(boatMesh);
00223
00224 Random rand = new Random();
00225 float tigerx = rand.Next(-500, 500);
00226 float tigerz = rand.Next(-500, 500);
00227 float tigery = sceneObject.extents.Y * 0.5f + outdoorScene.GetHeigth(tigerx, tigerz);
00228
00229
00230 while (tigery > 0.0f)
00231 {
00232 tigerx = rand.Next(-500, 500);
00233 tigerz = rand.Next(-500, 500);
00234 tigery = sceneObject.extents.Y * 0.5f + outdoorScene.GetHeigth(tigerx, tigerz);
00235 }
00236
00237 sceneObject.WorldMat = Matrix.Translation(tigerx, 0.0f, tigerz);
00238
00239
00240
00241
00242
00243
00244 boat = new Boat();
00245 boat.SceneObject = sceneObject;
00246 worldEntities.Add(sceneObject, boat);
00247 boat.speed = 20.0f;
00248
00249
00250
00251
00252
00253
00254
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 DXGfxManager.GetGlobalInstance().CurrentCamera.Person = boat.SceneObject;
00341 DXGfxManager.GetGlobalInstance().CurrentCamera.camera.positionConstraint = outdoorScene;
00342 DXGfxManager.GetGlobalInstance().CurrentCamera.Person.positionConstraint = outdoorScene;
00343 }
00344
00352 private void OnResetDevice(object sender, DeviceEventArgs e)
00353 {
00354 SurfaceDescription desc = e.BackBufferDescription;
00355
00356 textSprite = new Sprite(e.Device);
00357
00358
00359 DXGfxManager.GetGlobalInstance().OnDeviceReset(sender, e);
00360
00361
00362 hud.SetLocation(desc.Width - 170, 0);
00363 hud.SetSize(170, 170);
00364
00365
00366 outdoorScene.DeviceChanged(e.Device);
00367
00368 missionUi.SetLocation(0, desc.Height - 120);
00369 missionUi.SetSize(170, 120);
00370 }
00371
00379 private void OnLostDevice(object sender, EventArgs e)
00380 {
00381 if (outdoorScene != null)
00382 {
00383 outdoorScene.CleanOnLostDevice();
00384 }
00385
00386 if (textSprite != null)
00387 {
00388 textSprite.Dispose();
00389 textSprite = null;
00390 }
00391 }
00392
00399 private void OnDestroyDevice(object sender, EventArgs e)
00400 {
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 }
00417
00424 public void OnFrameMove(Microsoft.DirectX.Direct3D.Device device, double appTime, float elapsedTime)
00425 {
00426
00427 DXGfxManager.GetGlobalInstance().CurrentInputHandler.FrameMove(elapsedTime);
00428
00429
00430 double tmp = appTime - lastAIUpdate;
00431
00432
00433
00434
00435 boat.UpdateAI(appTime, elapsedTime);
00436 lastAIUpdate = appTime;
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 }
00465
00472 public void OnFrameRender(Microsoft.DirectX.Direct3D.Device device, double appTime, float elapsedTime)
00473 {
00474 bool beginSceneCalled = false;
00475
00476 if (defaultState==null)
00477 {
00478 defaultState = new StateBlock(device, StateBlockType.All);
00479 defaultState.Capture();
00480 }
00481
00482
00483 Matrix worldMatrix = worldCenter * DXGfxManager.GetGlobalInstance().CurrentCamera.WorldMatrix;
00484 computedViewMat = Matrix.Multiply(DXGfxManager.GetGlobalInstance().CurrentCamera.WorldMatrix, DXGfxManager.GetGlobalInstance().CurrentCamera.ViewMatrix);
00485 outdoorScene.Update(device, computedViewMat, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, appTime, elapsedTime);
00486
00487 defaultState.Apply();
00488
00489
00490 device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x00424B79, 1.0f, 0);
00491 try
00492 {
00493
00494 beginSceneCalled = true;
00495
00496
00497 device.SetTransform(TransformType.World, DXGfxManager.GetGlobalInstance().CurrentCamera.WorldMatrix);
00498 device.SetTransform(TransformType.View, computedViewMat);
00499 device.SetTransform(TransformType.Projection, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix);
00500 outdoorScene.RenderScene(device, outdoorScene.currentFrustrum);
00501
00502 device.RenderState.AlphaBlendEnable = false;
00503
00504
00505 RenderText();
00506
00507
00508 hud.OnRender(elapsedTime);
00509 missionUi.OnRender(elapsedTime);
00510 }
00511 finally
00512 {
00513
00514
00515 }
00516 }
00517
00522 private void RenderText()
00523 {
00524 TextHelper txtHelper = new TextHelper(statsFont, textSprite, 15);
00525
00526
00527 txtHelper.Begin();
00528 txtHelper.SetInsertionPoint(5, 5);
00529 txtHelper.SetForegroundColor(System.Drawing.Color.Yellow);
00530 txtHelper.DrawTextLine(sampleFramework.FrameStats);
00531 txtHelper.DrawTextLine(sampleFramework.DeviceStats);
00532
00533 txtHelper.SetForegroundColor(System.Drawing.Color.White);
00534
00535 txtHelper.End();
00536 }
00537
00544 private void OnKeyEvent(object sender, System.Windows.Forms.KeyEventArgs e)
00545 {
00546 switch (e.KeyCode)
00547 {
00548 case System.Windows.Forms.Keys.F1:
00549 isHelpShowing = !isHelpShowing;
00550 break;
00551 }
00552 }
00553
00554 private void OnDoubleMouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
00555 {
00556 WorldLocation destinationLocation = new WorldLocation();
00557
00558 Vector2 point = new Vector2(e.X, e.Y);
00559
00560 Vector3 rayO; Vector3 rayD;
00561 MathUtil.RayFromScreenPos(sampleFramework.Device.Viewport, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, computedViewMat, point, out rayO, out rayD);
00562 List<SceneObject> theList = new List<SceneObject>();
00563
00564 bool foundnothing = true;
00565
00566 if (outdoorScene.Intersect(rayO, rayD, ref theList))
00567 {
00568 foreach (SceneObject obj in theList)
00569 {
00570 WorldEntity entity = (WorldEntity)worldEntities[obj];
00571 if (entity != null)
00572 {
00573 foundnothing = false;
00574 missionUi.GetButton(MissionText).SetText(entity.selectString);
00575
00576 if (entity.selectSound != null)
00577 {
00578 entity.selectSound.Play(0, BufferPlayFlags.Default);
00579 }
00580 }
00581 else
00582 {
00583 destinationLocation.SceneObject.Position = outdoorScene.PickEnvironmentPosition(sampleFramework.Device.Viewport, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, computedViewMat, point);
00584
00585 Moving moveAction = new Moving(boat);
00586 moveAction.destination = destinationLocation;
00587 boat.CurrentAction = moveAction;
00588 }
00589 }
00590 }
00591
00592 if (foundnothing)
00593 {
00594 destinationLocation.SceneObject.Position = outdoorScene.PickEnvironmentPosition(sampleFramework.Device.Viewport, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, computedViewMat, point);
00595
00596 if (destinationLocation.SceneObject.Position.Y > 0)
00597 {
00598
00599 float dist = Vector3.Length(destinationLocation.SceneObject.Position - boat.SceneObject.Position);
00600
00601 if (dist < 50.0f)
00602 {
00603 FrameworkMesh boatMesh = new FrameworkMesh(DXGfxManager.GetGlobalInstance().d3dDevice, "tiger.x");
00604 SceneObject sceneObject = outdoorScene.Attach(boatMesh);
00605 sceneObject.Position = destinationLocation.SceneObject.Position;
00606
00607 Boat newboat = new Boat();
00608 newboat.SceneObject = sceneObject;
00609 worldEntities.Add(sceneObject, newboat);
00610 newboat.speed = 20.0f;
00611 }
00612 }
00613
00614 Moving moveAction = new Moving(boat);
00615 moveAction.destination = destinationLocation;
00616 boat.CurrentAction = moveAction;
00617 }
00618 }
00619
00625 public IntPtr OnMsgProc(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam, ref bool noFurtherProcessing)
00626 {
00627
00628 noFurtherProcessing = hud.MessageProc(hWnd, msg, wParam, lParam);
00629 if (noFurtherProcessing)
00630 return IntPtr.Zero;
00631
00632 noFurtherProcessing = missionUi.MessageProc(hWnd, msg, wParam, lParam);
00633 if (noFurtherProcessing)
00634 return IntPtr.Zero;
00635
00636
00637
00638 if (DXGfxManager.GetGlobalInstance().CurrentInputHandler != null)
00639 {
00640 DXGfxManager.GetGlobalInstance().CurrentInputHandler.OnMsgProc(hWnd, msg, wParam, lParam);
00641 }
00642 return IntPtr.Zero;
00643 }
00644
00648 public void InitializeApplication()
00649 {
00650 int y = 10;
00651
00652
00653 Button fullScreen = hud.AddButton(ToggleFullscreen, "Toggle full screen", 35, y, 125, 22);
00654 Button toggleRef = hud.AddButton(ToggleReference, "Toggle reference (F3)", 35, y += 24, 125, 22);
00655 Button changeDevice = hud.AddButton(ChangeDevice, "Change Device (F2)", 35, y += 24, 125, 22);
00656
00657 fullScreen.Click += new EventHandler(OnFullscreenClicked);
00658 toggleRef.Click += new EventHandler(OnRefClicked);
00659 changeDevice.Click += new EventHandler(OnChangeDevicClicked);
00660
00661
00662 Button missionText = missionUi.AddButton(MissionText, "Salut!!", 0, 0, 170, 120);
00663 missionText.Click += new EventHandler(missionText_Click);
00664 }
00665
00666 void missionText_Click(object sender, EventArgs e)
00667 {
00668 missionUi.GetButton(MissionText).SetText("Button Clicked!!");
00669 }
00670
00672 private void OnChangeDevicClicked(object sender, EventArgs e)
00673 {
00674 sampleFramework.ShowSettingsDialog(!sampleFramework.IsD3DSettingsDialogShowing);
00675 }
00676
00678 private void OnFullscreenClicked(object sender, EventArgs e)
00679 {
00680 sampleFramework.ToggleFullscreen();
00681 }
00682
00684 private void OnRefClicked(object sender, EventArgs e)
00685 {
00686 sampleFramework.ToggleReference();
00687 }
00688
00693 static int Main()
00694 {
00695 System.Windows.Forms.Application.EnableVisualStyles();
00696 using (Framework sampleFramework = new Framework())
00697 {
00698 Sample sample = new Sample(sampleFramework);
00699
00700
00701
00702
00703
00704
00705
00706
00707 sampleFramework.Disposing += new EventHandler(sample.OnDestroyDevice);
00708 sampleFramework.DeviceLost += new EventHandler(sample.OnLostDevice);
00709 sampleFramework.DeviceCreated += new DeviceEventHandler(sample.OnCreateDevice);
00710 sampleFramework.DeviceReset += new DeviceEventHandler(sample.OnResetDevice);
00711
00712 sampleFramework.SetWndProcCallback(new WndProcCallback(sample.OnMsgProc));
00713
00714 sampleFramework.SetCallbackInterface(sample);
00715
00716
00717
00718
00719 sampleFramework.SetCursorSettings(true, true);
00720
00721
00722
00723
00724
00725
00726 sample.InitializeApplication();
00727
00728
00729
00730
00731 sampleFramework.Initialize(true, true, true);
00732 sampleFramework.CreateWindow("Sample: Using DXGfxLib");
00733
00734
00735
00736
00737
00738
00739 sample.InitSound();
00740
00741
00742 sampleFramework.Window.KeyDown += new System.Windows.Forms.KeyEventHandler(sample.OnKeyEvent);
00743 sampleFramework.Window.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(sample.OnDoubleMouseClick);
00744 sampleFramework.CreateDevice(0, true, Framework.DefaultSizeWidth, Framework.DefaultSizeHeight,
00745 sample);
00746
00747
00748
00749
00750
00751
00752 sampleFramework.MainLoop();
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771 return sampleFramework.ExitCode;
00772 }
00773 }
00774 }
00775 }