- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/Sample/Sample.cs

Go to the documentation of this file.

    00001 //BSD License
    00002 //DXGfx® - http://www.eteractions.com
    00003 //Copyright (c) 2005
    00004 //by Guillaume Randon
    00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    00006 //and associated documentation files (the "Software"), to deal in the Software without restriction,
    00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    00009 //subject to the following conditions:
    00010 //The above copyright notice and this permission notice shall be included in all copies or substantial
    00011 //portions of the Software.
    00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
    00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    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         // Variables
    00034         private Framework sampleFramework = null; // Framework for samples
    00035         private Font statsFont = null; // Font for drawing text
    00036         private Sprite textSprite = null; // Sprite for batching text calls
    00037         //private ThirdPersonCamera camera = new ThirdPersonCamera(); // A model viewing camera
    00038         private bool isHelpShowing = true; // If true, renders the UI help text
    00039         private Dialog hud = null; // dialog for standard controls
    00040         private Dialog missionUi = null;
    00041         private Matrix worldCenter;
    00042
    00043         // HUD Ui Control constants
    00044         private const int ToggleFullscreen = 1;
    00045         private const int ToggleReference = 3;
    00046         private const int ChangeDevice = 4;
    00047
    00048         // DXGfxLib related variables
    00049         private OutDoorScene outdoorScene;
    00050         private Matrix computedViewMat;
    00051         private const int MissionText = 1;
    00052
    00053         // Wordl related variables
    00054         //private Tiger tiger;
    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         // Need a soundDevice in order to be able to play sounds.
    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             // Store framework
    00071             sampleFramework = f;
    00072             // Create dialogs
    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             // No fallback, need at least PS1.1
    00095             if (caps.PixelShaderVersion < new Version(1, 1))
    00096                 return false;
    00097
    00098             // Skip back buffer formats that don't support alpha blending
    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             // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW
    00117             // then switch to SWVP.
    00118             //if ((!caps.DeviceCaps.SupportsHardwareTransformAndLight) ||
    00119             //    (caps.VertexShaderVersion < new Version(1, 1)))
    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             // This application is designed to work on a pure device by not using
    00131             // any get methods, so create a pure device if supported and using HWVP.
    00132             if ((caps.DeviceCaps.SupportsPureDevice) &&
    00133                 ((settings.BehaviorFlags & CreateFlags.HardwareVertexProcessing) != 0))
    00134             {
    00135                 // Until the state manager is implemented it is better to keep this commented !!
    00136                 //settings.BehaviorFlags |= CreateFlags.DisableDriverManagement;
    00137                 //settings.BehaviorFlags |= CreateFlags.PureDevice;
    00138             }
    00139             // Debugging vertex shaders requires either REF or software vertex processing
    00140             // and debugging pixel shaders requires REF.
    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             // For the first device created if its a REF device, optionally display a warning dialog box
    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             // Initialize the stats font
    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             // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the
    00174             // shader debugger. Debugging vertex shaders requires either REF or software vertex
    00175             // processing, and debugging pixel shaders requires REF.  The
    00176             // ShaderFlags.Force*SoftwareNoOptimizations flag improves the debug experience in the
    00177             // shader debugger.  It enables source level debugging, prevents instruction
    00178             // reordering, prevents dead code elimination, and forces the compiler to compile
    00179             // against the next higher available software target, which ensures that the
    00180             // unoptimized shaders do not exceed the shader model limitations.  Setting these
    00181             // flags will cause slower rendering since the shaders will be unoptimized and
    00182             // forced into software.  See the DirectX documentation for more information about
    00183             // using the shader debugger.
    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             // Before everything let's notify the DXGfxManager
    00193             DXGfxManager.GetGlobalInstance().OnCreateDevice(sender, e);
    00194
    00195             // Let's create our scene
    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             // Setup the camera's view parameters
    00204             DXGfxManager.GetGlobalInstance().CurrentCamera.SetViewParameters(new Vector3(0.0f, 0.0f, -5.0f), Vector3.Empty);
    00205
    00206             //outdoorScene.camera = camera;
    00207             outdoorScene.LoadTerrain(e.Device, "heigthmap.bmp");
    00208
    00209             //Water theWater = new Water(e.Device, 2000, 2000, 10, 10, "water.bmp");
    00210             Water2 theWater = new Water2(2000);
    00211             outdoorScene.SceneWater = theWater;
    00212
    00213             //SkyBox theSky = new SkyBox(e.Device, "skybox2.x", 160);
    00214             //outdoorScene.SceneSky = theSky;
    00215             SkyDome theSky = new SkyDome();
    00216             theSky.LoadFromFile(e.Device, "domei.x");
    00217             outdoorScene.SceneSky = theSky;
    00218             //FrameworkMesh tigerMesh = new FrameworkMesh(e.Device, "tiger.x");
    00219             //SceneObject sceneObject = outdoorScene.Attach(tigerMesh);
    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             // This will guarantee we put our boat on Water !!
    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             //tiger = new Tiger();
    00239             //tiger.SceneObject = sceneObject;
    00240             //worldEntities.Add(sceneObject, tiger);
    00241             //tiger.speed = 20.0f;
    00242             //tiger.selectSound = new SecondaryBuffer("oink5.wav", soundDevice);
    00243             //tiger.channels.Add(new TigerChannel());
    00244             boat = new Boat();
    00245             boat.SceneObject = sceneObject;
    00246             worldEntities.Add(sceneObject, boat);
    00247             boat.speed = 20.0f;
    00248
    00249             //ObjectFromObj testObj = new ObjectFromObj();
    00250             //testObj.Load(e.Device, "ZMapper_Elephant.obj");
    00251             //testObj.Position = new Vector3(tigerx + 5.0f, tigery + 5.0f, tigerz + 5.0f);
    00252             //outdoorScene.Attach(testObj);
    00253
    00254             //MeshRessource tree = new MeshRessource(e.Device, "sapin.x");
    00256
    00257             //for (int i = 0; i < 2000; i++)
    00258             //{
    00259             //    ObjectFromMeshRessource treeObj = new ObjectFromMeshRessource();
    00260             //    treeObj.Attach(tree);
    00261             //    float x = rand.Next(-500, 500);
    00262             //    float z = rand.Next(-500, 500);
    00263             //    float y = outdoorScene.GetHeigth(x, z);
    00264
    00265             //    while (y < 35.0f)
    00266             //    {
    00267             //        x = rand.Next(-500, 500);
    00268             //        z = rand.Next(-500, 500);
    00269             //        y = outdoorScene.GetHeigth(x, z);
    00270             //    }
    00271             //    treeObj.Position = new Vector3(x, y, z);
    00272             //    outdoorScene.AttachStatic(treeObj);
    00273             //    //matrices[i] = Matrix.Translation(x, y, z);
    00274             //}
    00275             //Impostor imp = new Impostor();
    00276             //imp.Attach(tree);
    00277
    00278             //ExtendedMaterial[] materials = null;
    00279             //Mesh theMesh = Mesh.FromFile("sapin.x", MeshFlags.SystemMemory, e.Device, out materials);
    00280
    00282             //Texture[] meshTextures = new Texture[materials.Length];
    00283             //Material[] meshMaterials = new Material[materials.Length];
    00284             //for (int i = 0; i < materials.Length; i++)
    00285             //{
    00286             //    meshMaterials[i] = materials[i].Material3D;
    00287             //    // Set the ambient color for the material (D3DX does not do this)
    00288             //    meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
    00289
    00290             //    // Create the texture
    00291             //    meshTextures[i] = ResourceCache.GetGlobalInstance().CreateTextureFromFile(e.Device,
    00292             //                                                                      materials[i].TextureFilename);
    00293             //}
    00294             //outdoorScene.AttachBatch(imp.meshes[0], imp.meshMaterials, imp.meshTextures, matrices);
    00295
    00296
    00297
    00298
    00299
    00300             //MeshRessource tour = new MeshRessource(e.Device, "tour.x");
    00301
    00302             //for (int i = 0; i < 100; i++)
    00303             //{
    00304             //    ObjectFromMeshRessource tourObj = new ObjectFromMeshRessource();
    00305             //    tourObj.Attach(tour);
    00306             //    float x = rand.Next(-500, 500);
    00307             //    float z = rand.Next(-500, 500);
    00308             //    float y = outdoorScene.GetHeigth(x, z);
    00309
    00310             //    while (y < 60.0f)
    00311             //    {
    00312             //        x = rand.Next(-500, 500);
    00313             //        z = rand.Next(-500, 500);
    00314             //        y = outdoorScene.GetHeigth(x, z);
    00315             //    }
    00316             //    Matrix s = Matrix.Scaling(2.0f, 3.0f, 2.0f);
    00317             //    tourObj.ApplyLocalTransform(s);
    00318             //    tourObj.Position = new Vector3(x, y, z);
    00319             //    outdoorScene.AttachStatic(tourObj);
    00320             //}
    00321
    00322             //Eating eat = new Eating(tiger);
    00323             //eat.complement = veg;
    00324             //Mission tigerMission = new Mission();
    00325             //Step uniqueStep = new Step();
    00326             //uniqueStep.actionToBePerformed = eat;
    00327             //tigerMission.start = uniqueStep;
    00328             //tigerMission.currentStep = uniqueStep;
    00329             //tiger.mission = tigerMission;
    00330
    00331             // At the moment the positionning of an articulated model is not correct. Articulated mode
    00332             // display the model, run the default animation but the model is always in 0,0,0 in world space coordinates.
    00333             // This should be fixed soon.
    00334             //ArticulatedModel tiny = new ArticulatedModel();
    00335             //tiny.LoadFromFile(e.Device, "tiny.x");
    00336             //float tinyy = tiny.extents.Y*0.5f + outdoorScene.GetHeigth(2.0f, 10.0f);
    00337             //tiny.Position = new Vector3(2.0f, tinyy, 10.0f);
    00338             //outdoorScene.Attach(tiny);
    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             // Create a sprite to help batch calls when drawing many lines of text
    00356             textSprite = new Sprite(e.Device);
    00357
    00358             // Setup the camera's projection parameters
    00359             DXGfxManager.GetGlobalInstance().OnDeviceReset(sender, e);
    00360
    00361             // Setup UI locations
    00362             hud.SetLocation(desc.Width - 170, 0);
    00363             hud.SetSize(170, 170);
    00364
    00365             // Scene call
    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             //meshTextures = null;
    00402             //meshMaterials = null;
    00403
    00404             //if (fullMesh != null)
    00405             //    fullMesh.Dispose();
    00406
    00407             //if ((meshes != null) && (meshes.Length > 0))
    00408             //{
    00409             //    for (int i = 0; i < meshes.Length; i++)
    00410             //    {
    00411             //        if (meshes[i] != null)
    00412             //            meshes[i].Dispose();
    00413             //    }
    00414             //}
    00415             //meshes = null;
    00416         }
    00417
    00424         public void OnFrameMove(Microsoft.DirectX.Direct3D.Device device, double appTime, float elapsedTime)
    00425         {
    00426             // Update the camera's position based on user input
    00427             DXGfxManager.GetGlobalInstance().CurrentInputHandler.FrameMove(elapsedTime);
    00428
    00429             // Let's update our tiger
    00430             double tmp = appTime - lastAIUpdate;
    00431             //if (tmp > 10)
    00432             //{
    00433             //tiger.UpdateAI(appTime, elapsedTime);
    00434             //tiger.UpdateIA(elapsedTime, appTime);
    00435             boat.UpdateAI(appTime, elapsedTime);
    00436             lastAIUpdate = appTime;
    00437             //}
    00438
    00439             //if (tiger.mission.missionCompleted)
    00440             //{
    00441             //    missionUi.GetButton(MissionText).SetText("Mission Completed!!");
    00442             //}
    00443             //switch (tiger.currentState)
    00444             //{
    00445             //    case State.Bois:
    00446             //        missionUi.GetButton(MissionText).SetText("Bois!!");
    00447             //        break;
    00448             //    case State.Dort:
    00449             //        missionUi.GetButton(MissionText).SetText("Dort!!");
    00450             //        break;
    00451             //    case State.Explore:
    00452             //        missionUi.GetButton(MissionText).SetText("Explore!!");
    00453             //        break;
    00454             //    case State.Fuit:
    00455             //        missionUi.GetButton(MissionText).SetText("Fuit!!");
    00456             //        break;
    00457             //    case State.Mange:
    00458             //        missionUi.GetButton(MissionText).SetText("Mange!!");
    00459             //        break;
    00460             //    default:
    00461             //        missionUi.GetButton(MissionText).SetText("Default!!");
    00462             //        break;
    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             // Get the world matrix
    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             // Clear the render target and the zbuffer
    00490             device.Clear(ClearFlags.ZBuffer | ClearFlags.Target, 0x00424B79, 1.0f, 0);
    00491             try
    00492             {
    00493                 //device.BeginScene();
    00494                 beginSceneCalled = true;
    00495
    00496                 // Show scene
    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                 // Show frame rate
    00505                 RenderText();
    00506
    00507                 // Show UI
    00508                 hud.OnRender(elapsedTime);
    00509                 missionUi.OnRender(elapsedTime);
    00510             }
    00511             finally
    00512             {
    00513                 //if (beginSceneCalled)
    00514                 //    device.EndScene();
    00515             }
    00516         }
    00517
    00522         private void RenderText()
    00523         {
    00524             TextHelper txtHelper = new TextHelper(statsFont, textSprite, 15);
    00525
    00526             // Output statistics
    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                     // Check if boat is near if yes add a tiger on ground
    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             // Give the dialog a chance to handle the message first
    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             // Pass all remaining windows messages to camera so it can respond to user input
    00637             //DXGfxManager.CurrentCamera.HandleMessages(hWnd, msg, wParam, lParam);
    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             // Initialize the HUD
    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             // Hook the button events for when these items are clicked
    00657             fullScreen.Click += new EventHandler(OnFullscreenClicked);
    00658             toggleRef.Click += new EventHandler(OnRefClicked);
    00659             changeDevice.Click += new EventHandler(OnChangeDevicClicked);
    00660
    00661             // Our own UI
    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                 // Set the callback functions. These functions allow the sample framework to notify
    00700                 // the application about device changes, user input, and windows messages.  The
    00701                 // callbacks are optional so you need only set callbacks for events you're interested
    00702                 // in. However, if you don't handle the device reset/lost callbacks then the sample
    00703                 // framework won't be able to reset your device since the application must first
    00704                 // release all device resources before resetting.  Likewise, if you don't handle the
    00705                 // device created/destroyed callbacks then the sample framework won't be able to
    00706                 // recreate your device resources.
    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                 //try
    00716                 //{
    00717
    00718                     // Show the cursor and clip it when in full screen
    00719                     sampleFramework.SetCursorSettings(true, true);
    00720
    00721
    00722
    00723
    00724                     // Initialize
    00725
    00726                     sample.InitializeApplication();
    00727
    00728                     // Initialize the sample framework and create the desired window and Direct3D
    00729                     // device for the application. Calling each of these functions is optional, but they
    00730                     // allow you to set several options which control the behavior of the sampleFramework.
    00731                     sampleFramework.Initialize(true, true, true); // Parse the command line, handle the default hotkeys, and show msgboxes
    00732                     sampleFramework.CreateWindow("Sample: Using DXGfxLib");
    00733                     // Change windows size
    00734                     //sampleFramework.Window.Width = 1024;
    00735                     //sampleFramework.Window.Height = 768;
    00736
    00737                     // Now we should be able to call InitSound (it needs at least that the window be created!)
    00738                     // But we need to do this before the create device because we load the sound when the tiger in created!
    00739                     sample.InitSound();
    00740
    00741                     // Hook the keyboard event
    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                     // Pass control to the sample framework for handling the message pump and
    00750                     // dispatching render calls. The sample framework will call your FrameMove
    00751                     // and FrameRender callback when there is idle time between handling window messages.
    00752                     sampleFramework.MainLoop();
    00753
    00754 //                }
    00755 //#if(DEBUG)
    00756 //                catch (Exception e)
    00757 //                {
    00758 //                    // In debug mode show this error (maybe - depending on settings)
    00759 //                    sampleFramework.DisplayErrorMessage(e);
    00760 //#else
    00761 //            catch
    00762 //            {
    00763 //                // In release mode fail silently
    00764 //#endif
    00765 //                    // Ignore any exceptions here, they would have been handled by other areas
    00766 //                    return (sampleFramework.ExitCode == 0) ? 1 : sampleFramework.ExitCode; // Return an error code here
    00767 //                }
    00768
    00769                 // Perform any application-level cleanup here. Direct3D device resources are released within the
    00770                 // appropriate callback functions and therefore don't require any cleanup code here.
    00771                 return sampleFramework.ExitCode;
    00772             }
    00773         }
    00774     }
    00775 }