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 visLU.Effects;
00015 #endregion
00016
00017 namespace visLU
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 private Data input;
00033 private RenderTarget2D sceneTarget;
00034 private Camera[] cameras;
00035 private Viewport viewport;
00036
00037
00038 #endregion
00039
00040 #region Properties
00041 public Data Input
00042 {
00043 set { input = value; }
00044 get { return input; }
00045 }
00046 #endregion
00047
00048 #region Constructor
00049
00050
00051
00052
00053
00054 public Engine(Game _game) : base(_game)
00055 {
00056 game = _game;
00057
00058
00059 input = new Data(GameProperties.Instance.dataDir + GameProperties.Instance.dataFilename, _game);
00060 if(input == null)
00061 throw new InvalidOperationException("Data was not loaded.");
00062
00063 cameras = Game.Services.GetService(typeof(Camera[])) as Camera[];
00064 if (cameras == null)
00065 throw new InvalidOperationException("Camera Service not found.");
00066
00067 Console.WriteLine("ENGINE STARTED");
00068
00069 }
00070 #endregion
00071
00072 #region Methods
00073
00074
00075 #region Initialize()
00076
00077
00083 public override void Initialize()
00084 {
00085
00086 spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
00087 if (spriteBatch == null)
00088 throw new InvalidOperationException("SpriteBatch Service not found");
00089
00090 device = Game.GraphicsDevice;
00091
00092
00093
00094
00095 cameras[0].CameraPosition = new Vector3(1.5f, 1.5f, 2.0f);
00096
00097 cameras[0].CameraTarget = GameProperties.Instance.dataOrigin;
00098 cameras[0].CameraUpVector = Vector3.Left;
00099 #region debug
00100
00101
00102 #endregion
00103
00104
00105 cameras[1].CameraPosition = new Vector3(0.0f, 2.0f, 0.0f);
00106
00107 cameras[0].CameraTarget = GameProperties.Instance.dataOrigin;
00108 cameras[1].CameraUpVector = Vector3.UnitZ;
00109
00110
00111 cameras[2].CameraPosition = new Vector3(2.0f, 0.0f, 0.0f);
00112
00113 cameras[0].CameraTarget = GameProperties.Instance.dataOrigin;
00114 cameras[2].CameraUpVector = Vector3.Up;
00115
00116
00117 cameras[3].CameraPosition = new Vector3(0.0f, 0.0f, 2.0f);
00118
00119 cameras[0].CameraTarget = GameProperties.Instance.dataOrigin;
00120 cameras[3].CameraUpVector = Vector3.Up;
00121
00122
00123 input.Initialize();
00124
00125 base.Initialize();
00126 }
00127
00128 #endregion
00129
00130 #region LoadContent()
00131
00136 protected override void LoadContent()
00137 {
00138 spriteBatchFont = Game.Content.Load<SpriteFont>("Fonts\\spriteFont1");
00139
00140 viewport = GraphicsDevice.Viewport;
00141 GameProperties.Instance.XNAViewport = viewport;
00142
00143
00144 initializeTextureFilter();
00145
00146
00147
00148 device.PresentationParameters.MultiSampleType = device.DepthStencilBuffer.MultiSampleType;
00149 sceneTarget = (RenderTarget2D)device.GetRenderTarget(0);
00150
00151 base.LoadContent();
00152
00153 }
00154
00155
00156
00157 #endregion
00158
00159 #region UnloadContent()
00160
00161
00166 protected override void UnloadContent()
00167 {
00168
00169 base.UnloadContent();
00170 }
00171
00172
00173 #endregion
00174
00175 #region Update()
00176
00177
00182 public override void Update(GameTime gameTime)
00183 {
00184
00185
00186 if (GameProperties.Instance.engineState.loadData)
00187 {
00188 input = new Data(GameProperties.Instance.dataDir + GameProperties.Instance.dataFilename, game);
00189 input.Initialize();
00190 if (!GameProperties.Instance.fullscreen)
00191 GameProperties.Instance.engineState.loadData = false;
00192
00193 }
00194
00195
00196 if (GameProperties.Instance.engineState.updateCamera)
00197 {
00198 int currentView;
00199 switch (GameProperties.Instance.viewMode)
00200 {
00201 case ViewMode.VolView:
00202
00203 currentView = 0;
00204 break;
00205
00206 case ViewMode.TopView:
00207 currentView = 1;
00208 break;
00209
00210 case ViewMode.SideView:
00211 currentView = 2;
00212 break;
00213
00214 case ViewMode.FrontView:
00215 currentView = 3;
00216 break;
00217
00218 default:
00219 currentView = 0;
00220 break;
00221
00222 }
00223
00224
00225 input.Camera = cameras[currentView];
00226 input.CurrentCamera = currentView;
00227 if (!GameProperties.Instance.fullscreen)
00228 GameProperties.Instance.engineState.updateCamera = false;
00229
00230 }
00231
00232
00233 if (GameProperties.Instance.engineState.updateData)
00234 {
00235 input.Update(gameTime);
00236 if (!GameProperties.Instance.fullscreen)
00237 GameProperties.Instance.engineState.updateData = false;
00238 }
00239
00240 base.Update(gameTime);
00241 }
00242
00243
00244 #endregion
00245
00246 #region Draw()
00247
00248
00249 public override void Draw(GameTime gameTime)
00250 {
00251
00252 if (GameProperties.Instance.engineState.drawData)
00253 {
00254 device.RenderState.DepthBufferEnable = true;
00255 device.RenderState.DepthBufferWriteEnable = true;
00256
00257 device.RenderState.CullMode = CullMode.None;
00258 device.SetRenderTarget(0, sceneTarget);
00259
00260 try
00261 {
00262 device.Clear(Color.Black);
00263 device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
00264 }
00265 catch (Exception e)
00266 {
00267 Console.WriteLine(e);
00268 }
00269
00270
00271 drawText(gameTime);
00272
00273 input.Draw(gameTime);
00274
00275
00276 if(!GameProperties.Instance.fullscreen)
00277 GameProperties.Instance.engineState.drawData = false;
00278 }
00279
00280 base.Draw(gameTime);
00281
00282 }
00283
00284 #region Draw Scene
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
00341 #endregion
00342
00343 private void drawText(GameTime gameTime)
00344 {
00345 GraphicsDevice.RenderState.DepthBufferEnable = false;
00346 GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
00347 spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
00348
00349 switch ((int)GameProperties.Instance.viewMode)
00350 {
00351 case 0:
00352 spriteBatch.DrawString(spriteBatchFont, ("3D View"),
00353 new Vector2(50, 50), Color.Silver);
00354 break;
00355 case 1:
00356 spriteBatch.DrawString(spriteBatchFont, ("Top View"),
00357 new Vector2(50, 50), Color.Silver);
00358 break;
00359 case 2:
00360 spriteBatch.DrawString(spriteBatchFont, ("Side View"),
00361 new Vector2(50, 50), Color.Silver);
00362 break;
00363 case 3:
00364 spriteBatch.DrawString(spriteBatchFont, ("Front View"),
00365 new Vector2(50,50), Color.Silver);
00366 break;
00367 default:
00368 spriteBatch.DrawString(spriteBatchFont, ("3d View"),
00369 new Vector2(50, 50), Color.Silver);
00370 break;
00371 }
00372
00373 spriteBatch.End();
00374 GraphicsDevice.RenderState.DepthBufferEnable = true;
00375 GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
00376 }
00377
00378 #endregion
00379
00380 #region additional Methods
00381
00382
00383
00384 #region Initialize Texture Filter
00385
00386
00387
00388 private void initializeTextureFilter()
00389 {
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 if (device.GraphicsDeviceCapabilities.
00402 TextureFilterCapabilities.SupportsMipMapLinear == true)
00403 {
00404 device.SamplerStates[0].MipFilter = TextureFilter.Linear;
00405 Console.WriteLine("using Linear MipFilter");
00406 }
00407
00408 if (device.GraphicsDeviceCapabilities.
00409 TextureFilterCapabilities.SupportsMinifyAnisotropic == true)
00410 {
00411 device.SamplerStates[0].MinFilter = TextureFilter.Anisotropic;
00412 Console.WriteLine("using Anisotropic MinFilter");
00413 }
00414 else if (device.GraphicsDeviceCapabilities.
00415 TextureFilterCapabilities.SupportsMinifyLinear == true)
00416 {
00417 device.SamplerStates[0].MinFilter = TextureFilter.Linear;
00418 Console.WriteLine("using Linear MinFilter");
00419 }
00420
00421 if (device.GraphicsDeviceCapabilities.
00422 TextureFilterCapabilities.SupportsMagnifyAnisotropic == true)
00423 {
00424 device.SamplerStates[0].MagFilter = TextureFilter.Anisotropic;
00425 Console.WriteLine("using Anisotropic MagFilter");
00426 }
00427 else if (device.GraphicsDeviceCapabilities.
00428 TextureFilterCapabilities.SupportsMagnifyLinear == true)
00429 {
00430 device.SamplerStates[0].MagFilter = TextureFilter.Linear;
00431 Console.WriteLine("using Linear MagFilter");
00432 }
00433
00434 }
00435
00436 #endregion
00437
00438 private void ActivateAlphaBlending()
00439 {
00440 device.RenderState.AlphaBlendEnable = true;
00441 device.RenderState.SourceBlend = Blend.SourceAlpha;
00442 device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
00443 }
00444
00445 private void DeactivateAlphaBlending()
00446 {
00447 device.RenderState.AlphaBlendEnable = false;
00448 device.RenderState.SourceBlend = Blend.One;
00449 device.RenderState.DestinationBlend = Blend.Zero;
00450 }
00451
00452 private void setNonTextureFilter()
00453 {
00454 device.SamplerStates[0].MipFilter = TextureFilter.None;
00455 device.SamplerStates[0].MinFilter = TextureFilter.None;
00456 device.SamplerStates[0].MagFilter = TextureFilter.None;
00457 }
00458
00459
00460
00461
00462 #endregion
00463
00464
00465 #endregion
00466 }
00467 }
00468
00469