Go to the documentation of this file.00001 #region Using Statements
00002 using System;
00003 using System.Collections.Generic;
00004 using System.Linq;
00005 using Microsoft.Xna.Framework;
00006 using Microsoft.Xna.Framework.Audio;
00007 using Microsoft.Xna.Framework.Content;
00008 using Microsoft.Xna.Framework.GamerServices;
00009 using Microsoft.Xna.Framework.Graphics;
00010 using Microsoft.Xna.Framework.Input;
00011 using Microsoft.Xna.Framework.Media;
00012 using Microsoft.Xna.Framework.Net;
00013 using Microsoft.Xna.Framework.Storage;
00014 using visLU2.Effects;
00015 #endregion
00016
00017 namespace visLU2
00018 {
00022 class Engine : Microsoft.Xna.Framework.DrawableGameComponent
00023 {
00024 #region Variables
00025
00026
00027 private Game game;
00028 private GraphicsDevice device;
00029 private SpriteBatch spriteBatch;
00030 private SpriteFont spriteBatchFont;
00031
00032
00033 private FlowData flowData;
00034 private RenderTarget2D sceneTarget;
00035 private Camera[] cameras;
00036 private Viewport viewport;
00037
00038
00039 #endregion
00040
00041 #region Properties
00042 #endregion
00043
00044 #region Constructor
00045
00046
00047
00048
00049
00050 public Engine(Game _game) : base(_game)
00051 {
00052 game = _game;
00053
00054 cameras = Game.Services.GetService(typeof(Camera[])) as Camera[];
00055 if (cameras == null)
00056 throw new InvalidOperationException("Camera Service not found.");
00057
00058
00059 GameProperties.Instance.engineState = new EngineState(false, false, false, false);
00060
00061 Console.WriteLine("ENGINE STARTED");
00062
00063 }
00064 #endregion
00065
00066 #region Methods
00067
00068
00069 #region Initialize()
00070
00071
00077 public override void Initialize()
00078 {
00079
00080 spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
00081 if (spriteBatch == null)
00082 throw new InvalidOperationException("SpriteBatch Service not found");
00083
00084 device = Game.GraphicsDevice;
00085
00086
00087
00088
00089
00090
00091
00092 #region debug
00093
00094
00095 #endregion
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 cameras[3].CameraPosition = new Vector3(0.0f, 0.0f, 1.0f);
00109 cameras[3].CameraTarget = GameProperties.Instance.dataOrigin;
00110 cameras[3].CameraUpVector = Vector3.Up;
00111
00112 base.Initialize();
00113 }
00114
00115 #endregion
00116
00117 #region LoadContent()
00118
00123 protected override void LoadContent()
00124 {
00125 spriteBatchFont = Game.Content.Load<SpriteFont>("Fonts\\spriteFont1");
00126
00127 viewport = GraphicsDevice.Viewport;
00128 GameProperties.Instance.XNAViewport = viewport;
00129
00130
00131 initializeTextureFilter();
00132
00133
00134
00135 device.PresentationParameters.MultiSampleType = device.DepthStencilBuffer.MultiSampleType;
00136 sceneTarget = (RenderTarget2D)device.GetRenderTarget(0);
00137
00138 base.LoadContent();
00139
00140 }
00141
00142
00143
00144 #endregion
00145
00146 #region UnloadContent()
00147
00148
00153 protected override void UnloadContent()
00154 {
00155
00156 base.UnloadContent();
00157 }
00158
00159
00160 #endregion
00161
00162 #region Update()
00163
00164
00169 public override void Update(GameTime gameTime)
00170 {
00171
00172
00173 if (GameProperties.Instance.engineState.loadData)
00174 {
00175 flowData = null;
00176 flowData = new FlowData(GameProperties.Instance.dataDir + GameProperties.Instance.dataFilename, GameProperties.Instance.relativePath, game);
00177 flowData.Initialize();
00178
00179 #region set camera position and zoom in respect to the data set
00180
00181 float boundary = Math.Max(flowData.BoundaryLength.X, flowData.BoundaryLength.Y);
00182 float zoom = Math.Min(Game.Window.ClientBounds.Height / boundary, Game.Window.ClientBounds.Width / boundary);
00183
00184 float camPosX = flowData.BoundaryLength.X * 0.5f + flowData.BoundaryMin.X;
00185 float camPosY = flowData.BoundaryLength.Y * 0.5f + flowData.BoundaryMin.Y;
00186
00187 GameProperties.Instance.cameraPosition.Z = zoom;
00188 GameProperties.Instance.cameraPosition.X = camPosX;
00189 GameProperties.Instance.cameraPosition.Y = camPosY;
00190
00191 cameras[3].Zoom = zoom;
00192 cameras[3].CameraPosition = new Vector3(camPosX, camPosY, 0.0001f);
00193 cameras[3].CameraTarget = new Vector3(camPosX, camPosY, 0.0f);
00194
00195 #endregion
00196 flowData.Camera = cameras[3];
00197 GameProperties.Instance.engineState.updateCamera = true;
00198 GameProperties.Instance.engineState.loadData = false;
00199 GameProperties.Instance.engineState.updateData = true;
00200 GameProperties.Instance.engineState.drawData = true;
00201 GameProperties.Instance.activeTab = ActiveTab.Default;
00202 GameProperties.Instance.updateGuiForm = true;
00203 GameProperties.Instance.dataLoaded = true;
00204 }
00205
00206
00207 if (GameProperties.Instance.engineState.updateCamera)
00208 {
00209 #region depricated
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 #endregion
00236
00237
00238 GameProperties.Instance.engineState.updateCamera = false;
00239
00240 }
00241
00242
00243 if (GameProperties.Instance.dataLoaded && GameProperties.Instance.engineState.updateData)
00244 {
00245 flowData.Update(gameTime);
00246 GameProperties.Instance.engineState.updateData = false;
00247 }
00248
00249 base.Update(gameTime);
00250 }
00251
00252
00253 #endregion
00254
00255 #region Draw()
00256
00257
00258 public override void Draw(GameTime gameTime)
00259 {
00260
00261 if (GameProperties.Instance.dataLoaded && GameProperties.Instance.engineState.drawData)
00262 {
00263 device.RenderState.DepthBufferEnable = true;
00264 device.RenderState.DepthBufferWriteEnable = true;
00265
00266 device.RenderState.CullMode = CullMode.None;
00267 device.SetRenderTarget(0, sceneTarget);
00268
00269 try
00270 {
00271 device.Clear(Color.Black);
00272 device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
00273 }
00274 catch (Exception e)
00275 {
00276 Console.WriteLine(e);
00277 }
00278
00279
00280
00281
00282
00283
00284
00285 flowData.Draw(gameTime);
00286 GameProperties.Instance.engineState.drawData = false;
00287 }
00288
00289 base.Draw(gameTime);
00290
00291 }
00292
00293 #region Draw Scene
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
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 #endregion
00351
00352 private void drawText(GameTime gameTime)
00353 {
00354 GraphicsDevice.RenderState.DepthBufferEnable = false;
00355 GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
00356 spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
00357
00358 switch ((int)GameProperties.Instance.viewMode)
00359 {
00360 case 0:
00361 spriteBatch.DrawString(spriteBatchFont, ("3D View"),
00362 new Vector2(50, 50), Color.Silver);
00363 break;
00364 case 1:
00365 spriteBatch.DrawString(spriteBatchFont, ("Top View"),
00366 new Vector2(50, 50), Color.Silver);
00367 break;
00368 case 2:
00369 spriteBatch.DrawString(spriteBatchFont, ("Side View"),
00370 new Vector2(50, 50), Color.Silver);
00371 break;
00372 case 3:
00373 spriteBatch.DrawString(spriteBatchFont, ("Front View"),
00374 new Vector2(50,50), Color.Silver);
00375 break;
00376 default:
00377 spriteBatch.DrawString(spriteBatchFont, ("3d View"),
00378 new Vector2(50, 50), Color.Silver);
00379 break;
00380 }
00381
00382 spriteBatch.End();
00383 GraphicsDevice.RenderState.DepthBufferEnable = true;
00384 GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
00385 }
00386
00387 #endregion
00388
00389 #region additional Methods
00390
00391
00392
00393 #region Initialize Texture Filter
00394
00395
00396
00397 private void initializeTextureFilter()
00398 {
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 if (device.GraphicsDeviceCapabilities.
00411 TextureFilterCapabilities.SupportsMipMapLinear == true)
00412 {
00413 device.SamplerStates[0].MipFilter = TextureFilter.Linear;
00414 Console.WriteLine("using Linear MipFilter");
00415 }
00416
00417 if (device.GraphicsDeviceCapabilities.
00418 TextureFilterCapabilities.SupportsMinifyAnisotropic == true)
00419 {
00420 device.SamplerStates[0].MinFilter = TextureFilter.Anisotropic;
00421 Console.WriteLine("using Anisotropic MinFilter");
00422 }
00423 else if (device.GraphicsDeviceCapabilities.
00424 TextureFilterCapabilities.SupportsMinifyLinear == true)
00425 {
00426 device.SamplerStates[0].MinFilter = TextureFilter.Linear;
00427 Console.WriteLine("using Linear MinFilter");
00428 }
00429
00430 if (device.GraphicsDeviceCapabilities.
00431 TextureFilterCapabilities.SupportsMagnifyAnisotropic == true)
00432 {
00433 device.SamplerStates[0].MagFilter = TextureFilter.Anisotropic;
00434 Console.WriteLine("using Anisotropic MagFilter");
00435 }
00436 else if (device.GraphicsDeviceCapabilities.
00437 TextureFilterCapabilities.SupportsMagnifyLinear == true)
00438 {
00439 device.SamplerStates[0].MagFilter = TextureFilter.Linear;
00440 Console.WriteLine("using Linear MagFilter");
00441 }
00442
00443 }
00444
00445 #endregion
00446
00447 private void ActivateAlphaBlending()
00448 {
00449 device.RenderState.AlphaBlendEnable = true;
00450 device.RenderState.SourceBlend = Blend.SourceAlpha;
00451 device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
00452 }
00453
00454 private void DeactivateAlphaBlending()
00455 {
00456 device.RenderState.AlphaBlendEnable = false;
00457 device.RenderState.SourceBlend = Blend.One;
00458 device.RenderState.DestinationBlend = Blend.Zero;
00459 }
00460
00461 private void setNonTextureFilter()
00462 {
00463 device.SamplerStates[0].MipFilter = TextureFilter.None;
00464 device.SamplerStates[0].MinFilter = TextureFilter.None;
00465 device.SamplerStates[0].MagFilter = TextureFilter.None;
00466 }
00467
00468
00469
00470
00471 #endregion
00472
00473
00474 #endregion
00475 }
00476 }
00477
00478