Difference between revisions of "Game Programming Class2"

esse quam videri
Jump to: navigation, search
(Texture2D and SpriteBatches)
(Texture2D and SpriteBatches)
 
(52 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Fonts==
 
  
free game fonts [http://creators.xna.com/en-us/contentpack/fontpack XNA Redistributable Font Pack]
+
==Objectives==
 +
*Time corrected move bases on elapsed game time Monogame
 +
*Vector2 for direction with and without magnitude
 +
*Single responsibility principal for methods
  
[http://geekswithblogs.net/cwilliams/archive/2007/11/03/116573.aspx Fun with Fonts in XNA]
+
==Skills==
 +
*Get samples from SVN and Git
  
==in class==
+
==SVN Checkout==
===Spritefont demo===
 
[http://brookfield.rice.iit.edu/jmeyers/gbrowser.php?file=/ITM496-595/ClassSource/Projects/IntroFonts/IntroFonts/Game1.cs IntroFonts/Game1.cs]
 
[http://brookfield.rice.iit.edu/jmeyers/ITM496-595/ClassSource/Projects/IntroFonts.zip IntroFonts.zip]
 
  
==XNA Structures==
+
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.
 +
 
 +
<!-- https://iam.colum.edu:8443/svn/XNAProg/trunk/Jeff -->
 +
 
 +
Tools
 +
* Ahnk SVN for Visual Studio http://visualstudiogallery.msdn.microsoft.com/E721D830-7664-4E02-8D03-933C3F1477F2
 +
* TortoiseSVN http://tortoisesvn.net/
 +
 
 +
Repo for Fall 2017
 +
 
 +
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.5/trunk
 +
 
 +
Just the Jeff folder
 +
 
 +
https://iam.colum.edu:8443/svn/GPMonogame3.5/trunk/jeff
 +
 
 +
 
 +
Demos Projects : this is the link to my demos in SVN
 +
 
 +
https://iam.colum.edu:8443/svn/GPMonogame3.5/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.
 +
 
 +
===SVN ingnore git and GIT ignore SVN===
 +
http://stackoverflow.com/questions/21156065/how-to-locally-ignore-git-and-gitignore-in-a-svn-repo
 +
 
 +
http://stackoverflow.com/questions/821895/exclude-svn-folders-within-git
 +
 
 +
==Monogame Structures==
 
Game Class
 
Game Class
  
The XNA Game class has two private properties
+
The MonogameGame class has two private properties
 
*GraphicsDeviceManager graphics;
 
*GraphicsDeviceManager graphics;
 
*ContentManager content;
 
*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).
  
<pre>
+
<syntaxhighlight lang="csharp">
 
//the game constuctor can be used to set some graphics settings.
 
//the game constuctor can be used to set some graphics settings.
 
graphics.PreferredBackBufferHeight = 768;
 
graphics.PreferredBackBufferHeight = 768;
Line 24: Line 63:
 
//graphics.PreferredBackBufferWidth = 1920;
 
//graphics.PreferredBackBufferWidth = 1920;
 
//graphics.IsFullScreen = true;
 
//graphics.IsFullScreen = true;
</pre>
+
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="csharp">
 +
    // Frame rate is 30 fps by default for Windows Phone. 
 +
    TargetElapsedTime = TimeSpan.FromTicks(333333); 
 +
 
 +
    // Pre-autoscale settings. 
 +
    graphics.PreferredBackBufferWidth = 480; 
 +
    graphics.PreferredBackBufferHeight = 800; 
 +
</syntaxhighlight>
 
 
  
Line 33: Line 81:
 
# Update(GameTime gameTime)
 
# Update(GameTime gameTime)
  
==GameTime==
+
==Texture2D and SpriteBatches==
FrameRate and GameTime
+
simple pacmac bouncing back and forth
* [http://msdn.microsoft.com/en-us/library/bb203873.aspx#ID2EYB Game Loop Timing]
+
<syntaxhighlight lang="csharp" highlight="17-20,54,56-60,87,91-96,100,103,119-121">
* [http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gametime_members.aspx Game Time Members]
+
using System;
 +
using Microsoft.Xna.Framework;
 +
using Microsoft.Xna.Framework.Graphics;
 +
using Microsoft.Xna.Framework.Input;
 +
 
 +
 
 +
namespace GameUpdate
 +
{
 +
    /// <summary>
 +
    /// This is the main type for your game.
 +
    /// </summar>
 +
public class Game1 : Microsoft.Xna.Framework.Game
 +
    {
 +
        GraphicsDeviceManager graphics;
 +
        SpriteBatch spriteBatch;
 +
 
 +
        Texture2D PacMan;
 +
        Vector2 PacManLoc;      //Pacman location
 +
        Vector2 PacManDir;      //Pacman direction
 +
        float PacManSpeed;              //speed for the PacMan Sprite in pixels per frame per second
 +
 
 +
        public Game1()
 +
        {
 +
            graphics = new GraphicsDeviceManager(this);
 +
            Content.RootDirectory = "Content";
 +
 
 +
            //Change the frame fate to 30 Frames per second the default is 60fps
 +
            //TargetElapsedTime = TimeSpan.FromTicks(333333); // you may need to add using System; to get the TimeSpan function
 +
        }
 +
 
 +
        /// <summary>
 +
        /// Allows the game to perform any initialization it needs to before starting to run.
 +
        /// This is where it can query for any required services and load any non-graphic
 +
        /// related content. Calling base.Initialize will enumerate through any components
 +
        /// and initialize them as well.
 +
        /// </summary>
 +
        protected override void Initialize()
 +
        {
 +
            // TODO: Add your initialization logic here
 +
 
 +
            base.Initialize();
 +
        }
 +
 
 +
        /// <summary>
 +
        /// LoadContent will be called once per game and is the place to load
 +
        /// all of your content.
 +
        /// </summary>
 +
        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 here
 +
            PacMan = Content.Load<Texture2D>("pacmanSingle");
 +
            //Center PacMan
 +
            PacManLoc = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2,
 +
                graphics.GraphicsDevice.Viewport.Height / 2); //Start with PacManLoc in the center of the screen
 +
            PacManDir = new Vector2(1, 0); //start moving left
 +
 
 +
            PacManSpeed = 20;          //initial pacman speed
 +
 
 +
        }
  
===Time and Timespan===
+
        /// <summary>
Example of update using GameTime to calculate elapsed time
+
        /// UnloadContent will be called once per game and is the place to unload
 +
        /// all content.
 +
        /// </summary>
 +
        protected override void UnloadContent()
 +
        {
 +
            // TODO: Unload any non ContentManager content here
 +
        }
  
*IntroSimpleSpriteUpdateWindows.zip
+
        /// <summary>
*IntroSimpleSpriteWindows/Game1.cs
+
        /// Allows the game to run logic such as updating the world,
 +
        /// checking for collisions, gathering input, and playing audio.
 +
        /// </summary>
 +
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
 +
        protected override void Update(GameTime gameTime)
 +
        {
 +
            // Allows the game to exit
 +
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
 +
                this.Exit();
  
Understanding GameTime We'll revisit game time when we talk about performance profiling.
+
            // TODO: Add your update logic here
# Draw(GameTime gameTime)
 
  
Console And Trace
+
            //Elapsed time since last update will be used to correct movement speed
 +
            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
  
We will build a console class later and debug in game.
 
  
==Texture2D and SpriteBatches==
+
            //Turn PacMan Around if it hits the edge of the screen
 +
            if ((PacManLoc.X > graphics.GraphicsDevice.Viewport.Width - PacMan.Width)
 +
                || (PacManLoc.X < 0)
 +
              )
 +
            {
 +
                PacManDir = Vector2.Negate(PacManDir);
 +
            }
  
 +
            //Move PacMan
 +
            //Simple move Moves PacMac by PacManDiv on every update
 +
            PacManLoc = PacManLoc + PacManDir * PacManSpeed;      //no good not time corrected
  
'''[http://msdn.microsoft.com/en-us/library/bb203919.aspx 2D Graphics Overview]'''
+
            //Time corrected move. MOves PacMan By PacManDiv every Second
 +
            //PacManLoc = PacManLoc + ((PacManDir * PacManSpeed) * (time/1000));      //Simple Move PacMan by PacManDir
  
About textures and Batching
 
  
[http://msdn.microsoft.com/en-us/library/bb203889.aspx Displays, ViewPorts, Client Bounds]
 
  
 +
            base.Update(gameTime);
 +
        }
  
===Alpha channels===
+
        /// <summary>
 +
        /// This is called when the game should draw itself.
 +
        /// </summary>
 +
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
 +
        protected override void Draw(GameTime gameTime)
 +
        {
 +
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  
Demo
+
            // TODO: Add your drawing code here
* [http://gimp.org Gimp]
+
            spriteBatch.Begin();
* [http://www.getpaint.net/ Paint.NET]
+
            spriteBatch.Draw(PacMan, PacManLoc, Color.White);
 +
            spriteBatch.End();
  
===SpriteBacth===
+
            base.Draw(gameTime);
 +
        }
 +
    }
 +
</syntaxhighlight>
  
demo project
+
==GameTime==
*IntroSimpleSpriteBatchOptionsWindows.zip
+
FrameRate and GameTime
 +
* [http://msdn.microsoft.com/en-us/library/bb203873.aspx#ID2EYB Game Loop Timing]
 +
* [http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gametime_members.aspx Game Time Members]
  
spriteBatch.Begin();
+
===Time and Timespan===
 +
Example of update using GameTime to calculate elapsed time
  
[[Image:IntroSpriteModesDemo.png]]
+
Demo
  
# Options SpriteBlendMode
+
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/SimpleUpdateMovement SimpleUpdateMovement] : move with Keyboard
# Additive Enable additive blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-3.aspx
+
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/SimpleMovementWGravity SimpleUpdateMovementWGravity]: simple gravity moves sprite down
# AlphaBlend Enable alpha blending. http://blogs.msdn.com/etayrien/archive/2006/12/19/alpha-blending-part-2.aspx
+
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/SimpleUpdateMovementWState SimpleUpdateMovementWState]: better movement show different types with state selector
# None No blending specified.
+
*[https://github.com/dsp56001/GameProgramming/tree/master/Monogame/3.5/SimpleMovementJump SimpleMovementJump]: Very simple Jump and Gravity
  
===Other Projects===
 
  
IntroSimpleSpriteUpdateGravityWindows.zip
+
Understanding GameTime We'll revisit game time when we talk about performance profiling.
IntroSimpleSpriteUpdateWindows.zip
 
  
 
==Homework==
 
==Homework==
*XNA Picture With Update
+
*Interesting Movement Monogame : Create a Monogame project the uses keyboard input to move a sprite. The movement should be more interesting than linear up, down, left right. There are several examples of this in the SimpleUpdateMovementWState project. The sprite movement should be time corrected and frame rate independant.
:Update you picture to include some movement with the update method.
+
*Sprite Class
+
*Gravity and or Jump with 2 sprites
:Build a class that is reusable that can draw a single texture on the screen. Be sure to encapsulate all the properties and method that you need to draw the sprite.
+
**Start with SimpleUpdateMovementWGravity or SimpleMovementJump
 +
**How many sprites can you draw?
 +
**Can they have different controls?
 +
**Can you use a class to make the code more organized?
 +
 
 +
 
 +
Optional Reading
 +
 
 
*Read Chapter 2 in XNA 3.0
 
*Read Chapter 2 in XNA 3.0
 
*Read Chapter 4 in XNA 3.0 (yes we skipped 3 we'll come back)
 
*Read Chapter 4 in XNA 3.0 (yes we skipped 3 we'll come back)
 +
 +
Extra Bonus
 +
 +
http://advances.realtimerendering.com/s2016/Siggraph2016_idTech6.pdf
  
 
==Links==
 
==Links==

Latest revision as of 02:05, 8 September 2017

Objectives

  • Time corrected move bases on elapsed game time Monogame
  • Vector2 for direction with and without magnitude
  • Single responsibility principal for methods

Skills

  • Get samples from SVN and Git

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 Fall 2017

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.5/trunk

Just the Jeff folder

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


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

https://iam.colum.edu:8443/svn/GPMonogame3.5/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.

SVN ingnore git and GIT ignore SVN

http://stackoverflow.com/questions/21156065/how-to-locally-ignore-git-and-gitignore-in-a-svn-repo

http://stackoverflow.com/questions/821895/exclude-svn-folders-within-git

Monogame Structures

Game Class

The MonogameGame 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;
    // Frame rate is 30 fps by default for Windows Phone.  
    TargetElapsedTime = TimeSpan.FromTicks(333333);  
  
    // Pre-autoscale settings.  
    graphics.PreferredBackBufferWidth = 480;  
    graphics.PreferredBackBufferHeight = 800;


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

Texture2D and SpriteBatches

simple pacmac bouncing back and forth

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace GameUpdate
{
    /// <summary>
    /// This is the main type for your game.
    /// </summar>
public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D PacMan;
        Vector2 PacManLoc;      //Pacman location
        Vector2 PacManDir;      //Pacman direction
        float PacManSpeed;              //speed for the PacMan Sprite in pixels per frame per second

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            //Change the frame fate to 30 Frames per second the default is 60fps
            //TargetElapsedTime = TimeSpan.FromTicks(333333); // you may need to add using System; to get the TimeSpan function
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        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 here
            PacMan = Content.Load<Texture2D>("pacmanSingle");
            //Center PacMan
            PacManLoc = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2,
                graphics.GraphicsDevice.Viewport.Height / 2); //Start with PacManLoc in the center of the screen
            PacManDir = new Vector2(1, 0); //start moving left

            PacManSpeed = 20;          //initial pacman speed

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            //Elapsed time since last update will be used to correct movement speed
            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;


            //Turn PacMan Around if it hits the edge of the screen
            if ((PacManLoc.X > graphics.GraphicsDevice.Viewport.Width - PacMan.Width)
                || (PacManLoc.X < 0)
               )
            {
                PacManDir = Vector2.Negate(PacManDir);
            }

            //Move PacMan
            //Simple move Moves PacMac by PacManDiv on every update
            PacManLoc = PacManLoc + PacManDir * PacManSpeed;      //no good not time corrected

            //Time corrected move. MOves PacMan By PacManDiv every Second
            //PacManLoc = PacManLoc + ((PacManDir * PacManSpeed) * (time/1000));      //Simple Move PacMan by PacManDir



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(PacMan, PacManLoc, Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }

GameTime

FrameRate and GameTime

Time and Timespan

Example of update using GameTime to calculate elapsed time

Demo


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

Homework

  • Interesting Movement Monogame : Create a Monogame project the uses keyboard input to move a sprite. The movement should be more interesting than linear up, down, left right. There are several examples of this in the SimpleUpdateMovementWState project. The sprite movement should be time corrected and frame rate independant.
  • Gravity and or Jump with 2 sprites
    • Start with SimpleUpdateMovementWGravity or SimpleMovementJump
    • How many sprites can you draw?
    • Can they have different controls?
    • Can you use a class to make the code more organized?


Optional Reading

  • Read Chapter 2 in XNA 3.0
  • Read Chapter 4 in XNA 3.0 (yes we skipped 3 we'll come back)

Extra Bonus

http://advances.realtimerendering.com/s2016/Siggraph2016_idTech6.pdf

Links