34
Dept. of Electronics Engineering XNA4遊戲程式框架 吳錫修 October 16, 2014

XNA遊戲程式框架

Embed Size (px)

DESCRIPTION

介紹XNA 4.0遊戲專案框架程式及流程

Citation preview

  • 1. XNA4October 16, 2014Dept. of Electronics Engineering

2. shape the future XNA XNA Content Pipeline XNA Game GameComponent DrawableGameComponent2 Wu, ShyiShiou 3. shape the futureXNA XNA Game Studio 4.0 Windows Games (4.0)3 Wu, ShyiShiou 4. shape the futureXNA 4 Wu, ShyiShiou 5. shape the future1/25 Wu, ShyiShiou 6. shape the future2/2 AudioImporters EffectImporter.fx FBXImporter.fbx3D Ximporter.x3D TextureImporter2D.bmp.dds.dib.hdr.jpg.pfm.png.ppm.tga VideoImporter6 Wu, ShyiShiou 7. shape the futureXNA Content Pipeline (Importer)(Content Processor)(ContentCompiler).xnb(Content Loader)http://xna.gamedev.ru/articles/ContentPipelineOverview7 Wu, ShyiShiou 8. shape the future8 Wu, ShyiShiou 9. shape the futureProgram.cs9 Wu, ShyiShiouusing System;namespace WindowsGame1{#if WINDOWS || XBOXstatic class Program{/// /// The main entry point for the application./// static void Main(string[] args){using (Game1 game = new Game1()) //{game.Run(); //}}}#endif} 10. shape the futureXNAGame10 Wu, ShyiShiou(2D3D)()(AI)()Initialize()LoadContent()Update(GameTime gameTime)Draw(GameTime gameTime)UnloadContent() 11. shape the futuregame.Run() 60 this.TargetElaspedTime = new TimeSpan(0,0,0,0,33); //1/30Initialize()LoadContent()Update(GameTime gametime)Draw(GameTime gameTime)UnloadContent()11 Wu, ShyiShiou 12. shape the futureGame1.cs 1/5using System;using System.Collections.Generic;using System.Linq;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Media;12 Wu, ShyiShiounamespace WindowsGame1{public class Game1 : Microsoft.Xna.Framework.Game{GraphicsDeviceManager graphics; // SpriteBatch spriteBatch; // 2D}...};XNA 13. Game1.cs 2/5public Game1(){graphics = new GraphicsDeviceManager(this);Content.RootDirectory = "Content"; // }/// ////// protected override void Initialize(){// TODO: Add your initialization logic herebase.Initialize();}13 Wu, ShyiShiou 14. shape the futureGame1.cs 3/5/// /// LoadContent/// protected override void LoadContent(){// Create a new SpriteBatch, which can be used to draw textures.spriteBatch = new SpriteBatch(GraphicsDevice);// TODO: use this.Content to load your game content here14 Wu, ShyiShiou}/// /// UnloadContent/// protected override void UnloadContent(){// TODO: Unload any non ContentManager content here} 15. shape the futureGame1.cs 4/5/// /// /// /// Provides a snapshot of timing values.protected override void Update(GameTime gameTime){// GamePadBackif (GamePad.GetState(PlayerIndex.One).Buttons.Back ==15 Wu, ShyiShiouButtonState.Pressed)this.Exit();// TODO: Add your update logic herebase.Update(gameTime);} 16. Game1.cs 5/5/// /// /// /// Provides a snapshot of timing values.protected override void Draw(GameTime gameTime){GraphicsDevice.Clear(Color.CornflowerBlue); //()// TODO: Add your drawing code herebase.Draw(gameTime);}16 Wu, ShyiShiou 17. 17 Wu, ShyiShiou 18. 1. XNA 4.0XNALabI2. Initialize()30Hzprotected override void Initialize(){// TODO: Add your initialization logic herethis.IsMouseVisible = true; // this.Window.Title = - "; // this.TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 33); // 30 base.Initialize();}18 Wu, ShyiShiou 19. 3. Update()Escprotected override void Update(GameTime gameTime){// Allows the game to exitif (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)this.Exit();KeyboardState newState;newState = Keyboard.GetState();if (newState.IsKeyDown(Keys.Escape)) this.Exit(); // Esc// TODO: Add your update logic herebase.Update(gameTime);}4. XNALabI19 Wu, ShyiShiou 20. shape the futureGameComponent 1/6 /20 Wu, ShyiShiou 21. shape the futureGameComponent 2/6 GameComponent1.cspublic class GameComponent1 : Microsoft.Xna.Framework.GameComponent{public GameComponent1(Game game) : base(game){// TODO: Construct any child components here21 Wu, ShyiShiou}public override void Initialize(){// TODO: Add your initialization code herebase.Initialize();}public override void Update(GameTime gameTime){// TODO: Add your update code herebase.Update(gameTime);}} 22. shape the futureGameComponent 3/6 Game1.csGameComponent1Game122 Wu, ShyiShiouGameComponent1 gc;public Game1(){graphics = new GraphicsDeviceManager(this);Content.RootDirectory = "Content";gc = new GameComponent1(this);Components.Add(gc);} 23. shape the futureGameComponent 4/6 Game1GameComponentGame1 Initialize()GameComponentInitialize()GameComponentInitialize()23 Wu, ShyiShiouGameComponentInitialize() 24. shape the futureGameComponent 5/6 Game1GameComponentGame1 Update()GameComponentUpdate()GameComponentUpdate()24 Wu, ShyiShiouGameComponentUpdate() 25. shape the futureGameComponent 6/6GameComponent25 Wu, ShyiShiouGame1Initialize()LoadContent()Update(GameTime gameTime)Draw(GameTime gameTime)UnloadContent()()Initialize()Update(GameTime gameTime) 26. shape the futureDrawableGameComponent 1/3 GameComponent1DrawableGameComponentoverride Darw()GameComponent1public class DrawableGameComponent1 :Microsoft.Xna.Framework.DrawableGameComponentpublic Game1(){graphics = new GraphicsDeviceManager(this);Content.RootDirectory = "Content";dgc = new DrawableGameComponent1(this);Components.Add(dgc);}26 Wu, ShyiShiou 27. shape the futureDrawableGameComponent 2/3 Game1DrawableGameComponentGame1 Draw()GameComponentDraw()GameComponentDraw()27 Wu, ShyiShiouGameComponentDraw() 28. shape the futureDrawableGameComponent 3/3DrawableGameComponent28 Wu, ShyiShiouGame1Initialize()LoadContent()Update(GameTime gameTime)Draw(GameTime gameTime)UnloadContent()()Initialize()Update(GameTime gameTime)Draw(GameTime gameTime) 29. 29 Wu, ShyiShiou 30. 1. XNALabICross4.pngXNALabIContent2. Game1public class Game1 : Microsoft.Xna.Framework.Game{GraphicsDeviceManager graphics;SpriteBatch spriteBatch;byte R = 0, G = 0, B = 0; // Texture2D bg_frame;}3. LoadContent()protected override void LoadContent(){// Create a new SpriteBatch, which can be used to draw textures.spriteBatch = new SpriteBatch(GraphicsDevice);// TODO: use this.Content to load your game content herebg_frame = this.Content.Load("Cross4"); // }30 Wu, ShyiShiou 31. 4. Update()protected override void Update(GameTime gameTime){// TODO: Add your update logic hereMouseState mouse = Mouse.GetState(); // if (mouse.X > graphics.PreferredBackBufferWidth / 2) // R = 255;elseR = 0;if (mouse.Y > graphics.PreferredBackBufferHeight / 2) // G = 255;elseG = 0;base.Update(gameTime);}31 Wu, ShyiShiou 32. 5. Draw()protected override void Draw(GameTime gameTime){// TODO: Add your drawing code heregraphics.GraphicsDevice.Clear(new Color(R, G, B));spriteBatch.Begin();spriteBatch.Draw(bg_frame,new Rectangle(0, 0, this.graphics.PreferredBackBufferWidth,this.graphics.PreferredBackBufferHeight),Color.White);spriteBatch.End();base.Draw(gameTime);}6. XNALabI32 Wu, ShyiShiou 33. shape the future1/2 Microsoft.Xna.Framework.Game GraphicsDeviceManager SpriteBatch2D TargetElapsedTime33 Wu, ShyiShiou 34. shape the future2/2 GamePad.GetState() Mouse.GetState() Keyboard.GetState() GraphicsDeviceManagerPreferredBackBufferWidthPreferredBackBufferHeight34 Wu, ShyiShiou