Difference between revisions of "Game Programming Class2"

esse quam videri
Jump to: navigation, search
(Texture2D and SpriteBatches)
(Texture2D and SpriteBatches)
Line 106: Line 106:
 
==Texture2D and SpriteBatches==
 
==Texture2D and SpriteBatches==
 
simple pacmac bouncing back and forth
 
simple pacmac bouncing back and forth
<syntaxhighlight lang="csharp" line="1" highlight="[17-20,54,56-60,87,91-96, 100, 119-122]">
+
<syntaxhighlight lang="csharp" line="1" highlight="[17-20,54,56-60,87,91-96,100,119-122]">
 
public class Game1 : Microsoft.Xna.Framework.Game
 
public class Game1 : Microsoft.Xna.Framework.Game
 
     {
 
     {

Revision as of 19:44, 1 February 2016

Fonts

free game fonts XNA Redistributable Font Pack

Fun with Fonts in XNA

note this font pack is now included in XNA 4

SVN Checkout

All of the examples for this class and the basic game library we wil be building is in our SVN repo this week we'll practice checking out a folder from the repo.


Tools

Repo for Spring 2016

Trunk : this contains all the old student work as well as my demos so you probably don't want this at least not quite yet

https://iam.colum.edu:8443/svn/GPMonogame3/trunk

Just the Jeff folder

https://iam.colum.edu:8443/svn/GPMonogame3/trunk/jeff


Demos Projects : this is the link to my demos in SVN

https://iam.colum.edu:8443/svn/GPMonogame3/trunk/jeff


tortoisesvn http://tortoisesvn.tigris.org/

ankhsvn http://ankhsvn.open.collab.net/

SNV checkout demo.

In the coming weeks we'll learn how to commit to the SNV. Until then all you need to know is how to checkout.

in class

Spritefont demo

IntroFonts/Game1.cs IntroFonts.zip

XNA Structures

Game Class

The XNA Game class has two private properties

  • GraphicsDeviceManager graphics;
  • ContentManager content;
Graphics for windows and xbox are 60 fps. XBOx supports NTSC resolutions (480i, 480p, 720p, 1080i and 1080p 1080p is only on newer hdmi models).
//the game constuctor can be used to set some graphics settings.
graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
//graphics.PreferredBackBufferHeight = 1080;
//graphics.PreferredBackBufferWidth = 1920;
//graphics.IsFullScreen = true;
Graphics for Mobile Game is 30 fps and 480 x 800.
1     // Frame rate is 30 fps by default for Windows Phone.  
2     TargetElapsedTime = TimeSpan.FromTicks(333333);  
3   
4     // Pre-autoscale settings.  
5     graphics.PreferredBackBufferWidth = 480;  
6     graphics.PreferredBackBufferHeight = 800;


  1. Declare SpriteBatch spriteBatch;
  2. Initialize()
  3. LoadContent()
  4. UnloadContent()
  5. Update(GameTime gameTime)

GameTime

FrameRate and GameTime

Time and Timespan

Example of update using GameTime to calculate elapsed time

  • IntroSimpleSpriteUpdateWindows.zip
  • IntroSimpleSpriteWindows/Game1.cs

Understanding GameTime We'll revisit game time when we talk about performance profiling.

Time corrected sprite movement

  1. Draw(GameTime gameTime)

Console And Trace

We will build a console class later and debug in game.

Texture2D and SpriteBatches

simple pacmac bouncing back and forth

  1 public class Game1 : Microsoft.Xna.Framework.Game
  2     {
  3         GraphicsDeviceManager graphics;
  4         SpriteBatch spriteBatch;
  5 
  6         Texture2D PacMan;
  7         Vector2 PacManLoc;      //Pacman location
  8         Vector2 PacManDir;      //Pacman direction
  9         float PacManSpeed;              //speed for the PacMan Sprite in pixels per frame per second
 10 
 11         public Game1()
 12         {
 13             graphics = new GraphicsDeviceManager(this);
 14             Content.RootDirectory = "Content";
 15 
 16             //Change the frame fate to 30 Frames per second the default is 60fps
 17             //TargetElapsedTime = TimeSpan.FromTicks(333333); // you may need to add using System; to get the TimeSpan function
 18         }
 19 
 20         /// <summary>
 21         /// Allows the game to perform any initialization it needs to before starting to run.
 22         /// This is where it can query for any required services and load any non-graphic
 23         /// related content.  Calling base.Initialize will enumerate through any components
 24         /// and initialize them as well.
 25         /// </summary>
 26         protected override void Initialize()
 27         {
 28             // TODO: Add your initialization logic here
 29 
 30             base.Initialize();
 31         }
 32 
 33         /// <summary>
 34         /// LoadContent will be called once per game and is the place to load
 35         /// all of your content.
 36         /// </summary>
 37         protected override void LoadContent()
 38         {
 39             // Create a new SpriteBatch, which can be used to draw textures.
 40             spriteBatch = new SpriteBatch(GraphicsDevice);
 41 
 42             // TODO: use this.Content to load your game content here
 43             PacMan = Content.Load<Texture2D>("pacmanSingle");
 44             //Center PacMan
 45             PacManLoc = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2,
 46                 graphics.GraphicsDevice.Viewport.Height / 2); //Start with PacManLoc in the center of the screen
 47             PacManDir = new Vector2(1, 0); //start moving left
 48 
 49             PacManSpeed = 20;          //initial pacman speed
 50 
 51         }
 52 
 53         /// <summary>
 54         /// UnloadContent will be called once per game and is the place to unload
 55         /// all content.
 56         /// </summary>
 57         protected override void UnloadContent()
 58         {
 59             // TODO: Unload any non ContentManager content here
 60         }
 61 
 62         /// <summary>
 63         /// Allows the game to run logic such as updating the world,
 64         /// checking for collisions, gathering input, and playing audio.
 65         /// </summary>
 66         /// <param name="gameTime">Provides a snapshot of timing values.</param>
 67         protected override void Update(GameTime gameTime)
 68         {
 69             // Allows the game to exit
 70             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
 71                 this.Exit();
 72 
 73             // TODO: Add your update logic here
 74 
 75             //Elapsed time since last update will be used to correct movement speed
 76             float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
 77 
 78 
 79             //Turn PacMan Around if it hits the edge of the screen
 80             if ((PacManLoc.X > graphics.GraphicsDevice.Viewport.Width - PacMan.Width)
 81                 || (PacManLoc.X < 0)
 82                )
 83             {
 84                 PacManDir = Vector2.Negate(PacManDir);
 85             }
 86 
 87             //Move PacMan
 88             //Simple move Moves PacMac by PacManDiv on every update
 89             PacManLoc = PacManLoc + PacManDir * PacManSpeed;      //no good not time corrected
 90 
 91             //Time corrected move. MOves PacMan By PacManDiv every Second
 92             //PacManLoc = PacManLoc + ((PacManDir * PacManSpeed) * (time/1000));      //Simple Move PacMan by PacManDir
 93 
 94 
 95 
 96             base.Update(gameTime);
 97         }
 98 
 99         /// <summary>
100         /// This is called when the game should draw itself.
101         /// </summary>
102         /// <param name="gameTime">Provides a snapshot of timing values.</param>
103         protected override void Draw(GameTime gameTime)
104         {
105             graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
106 
107             // TODO: Add your drawing code here
108             spriteBatch.Begin();
109             spriteBatch.Draw(PacMan, PacManLoc, Color.White);
110             spriteBatch.End();
111 
112             base.Draw(gameTime);
113         }
114     }

2D Graphics Overview

About textures and Batching

Displays, ViewPorts, Client Bounds


Alpha channels

Demo

SpriteBacth

demo project

  • IntroSimpleSpriteBatchOptionsWindows.zip

spriteBatch.Begin();

IntroSpriteModesDemo.png

  1. Options SpriteBlendMode
  2. Additive Enable additive blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-3.aspx
  3. AlphaBlend Enable alpha blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-2.aspx
  4. None No blending specified.

Other Projects

IntroSimpleSpriteUpdateGravityWindows.zip IntroSimpleSpriteUpdateWindows.zip

Homework

  • XNA Picture With Update
Update your picture to include some movement with the update method. Use the keyboard or mouse to interact with something on the screen
  • Read Chapter 2 in XNA 3.0
  • Read Chapter 4 in XNA 3.0 (yes we skipped 3 we'll come back)

Links