XNA Mono Games uses XNA 4 is a framework for developing... Windows, Linux, and other systems.

advertisement
XNA
MonoGames XNA Basics
Mono Games uses XNA 4 is a framework for developing C# games for
Windows, Linux, and other systems.
Read XNA 4 docs on " Creating a Windows Game or Library Project "
Visual Studio XNA 4 projects will create:
Program.cs – class that contains a main method that
creates a Game1 instance and invokes its run()
static void Main(string[] args) {
using (Game1 game = new Game1()) { game.Run(); } }
Game1.cs – class that derives (extends) Game
public class Game1 : Microsoft.Xna.Framework.Game {..}
Content subdirectory – contains *.x model and texture files and
other "content resources“
My examples will not have a Program.cs or Game1.cs file
I’ll put the Main in the example source file.
1
Skeleton XNA project
MonoGames XNA Basics
A new XNA 4 project generates a “skeleton” application.
using directives – DLLs for framework classes aka "imports"
namespace -- all classes in project should share namespace
namespace usually the name of the project, directory
public class Game1 : Microsoft.Xna.Framework.Game{
public Game1() {...}
protected override void Initialize {...}
protected override void LoadContent() {...}
protected override void UnloadContent() {...}
protected override void Update(GameTime gameTime) {...}
protected override void Draw(GameTime gameTime) {...}
}
2
Game class
MonoGames XNA Basics
The GameName class derives from Game.
Game initialization sequence
Constructor – creates a GraphicsDeviceManager
sets Content directory
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Initialize method – called after construction but before
LoadContent. Developer can initialize and GameComponents they
added.
LoadContent method – add "content" files { models *.x, textures...}
3
Game loop: Update(), Draw()
MonoGames XNA Basics
Game hides the rendering loop from the developer. << see docs>>
IsFixedTimeStep – property determines interval of calls to Update
Update method – contains changes in "logic" of the game:
rotation changes, user input (keyboard, gamepad)
gameTime is the elapsed time of program execution
protected virtual void Update ( GameTime gameTime )
Draw method – contains drawing requests for meshes, sprites, and
primitives. Called as frequently as possible.
protected virtual void Draw (GameTime gameTime)
4
AxisXNA example
MonoGames XNA Basics
Demonstrates:
LoadContent
*.x model file
w/ textures
Update w/ text display,
user input, and rotation.
Draw with text and 3D
1 camera,
3 lights { red, from right, green from top, white from front}
Content models (*.x) must be stored in Content subdirectory and added
to project. If models have textures, they should be in Content
subdirectory.
5
Build axisXNA project
MonoGames XNA Basics
Start | Microsoft Visual Studio 2012 | New | Project
Source control:
do not submit
class files w/
source control
6
Delete Game1.cs Program.cs
MonoGames XNA Basics
My examples have only 1 file "exampleName.cs" . Create project
"ExampleName", delete program.cs and game1.cs files add existing
item ExampleName.cs file to project.
7
MonoGames XNA Basics
Add source and content files
Download the axisDistro.zip file and extract into a
temporary directory.
In Solution Explorer right click and open the
project, select the "Add" from walking menu, select
"Existing Item". In the file browser select your
axisXNA.cs file from the axisXNA directory.
Do the same for the "(Content)" project – shown here.
and add all content files from ContentXNA.
8
Hi Def Game Profile
In Solution Explorer open the project's
properties dialog.
Set Game Profile to "Hi Def"
MonoGames XNA Basics
9
add conditional build __XNA4__
Select menu Debug | Start without debuging
to compile and run project.
MonoGames XNA Basics
10
AxisXNA
class diagram
MonoGames XNA Basics
11
BasicEffect
MonoGames XNA Basics
12
BasicEffect (subclasses Effect) represents shader model 1.1 effects
Used to set values in shaders :
<< see XNA docs – Creating a BasicEffect >>
Matrix worldMatrix, viewMatrix, projectionMatrix;
BasicEffect be;
...
be = new BasicEffect(graphics.GraphicsDevice);
...
be.Alpha = 1.0f;
be.DiffuseColor = new Vector3(1.0f, 0.0f, 1.0f);
be.SpecularColor = new Vector3(0.25f, 0.25f, 0.25f);
be.SpecularPower = 5.0f;
be.AmbientLightColor = new Vector3(0.75f, 0.75f, 0.75f);
Lighting and Matrices
MonoGames XNA Basics
BasicEffect has 3 DirectionalLights: 0, 1, 2
be.DirectionalLight0.Enabled = true;
be.DirectionalLight0.DiffuseColor = Vector3.One;
be.DirectionalLight0.Direction =
Vector3.Normalize(new Vector3(1.0f, -1.0f, -1.0f));
be.DirectionalLight0.SpecularColor = Vector3.One;
be.DirectionalLight1.Enabled = true;
be.DirectionalLight1.DiffuseColor = new Vector3(0.5f,
0.5f, 0.5f);
be.DirectionalLight1.Direction = Vector3.Normalize(new
Vector3(-1.0f, -1.0f, 1.0f));
be.DirectionalLight1.SpecularColor =
new Vector3(0.5f, 0.5f, 0.5f);
be.LightingEnabled = true;
be.World = worldMatrix;
// object transformations
be.View = viewMatrix;
// camera transformations
be.Projection = projectionMatrix; // world  view coords
13
Draw a modelled (*.x) mesh
MonoGames XNA Basics
<< look at xnaAxis.x model file in AxisXNA\Content >>
private void drawModel(GameTime gameTime, Model m) {
Matrix[] transforms = new Matrix[m.Bones.Count];
// get model's can have embedded transforms
m.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in m.Meshes) {
foreach (BasicEffect effect in mesh.Effects) {
effect.EnableDefaultLighting();
effect.View = view;
effect.Projection = projection;
effect.World = Matrix.Identity * rotateY *
transforms[mesh.ParentBone.Index]; }
mesh.Draw();
}
}
14
DrawableGameComponents
MonoGames XNA Basics
Classes that derive from GameCompnent or DrawableGameComponent
have virtual methods that can be overridden.
GameComponent : IGameComponent, IUpdateable, IDisposable
DrawableGameComponent : GameComponent, IDrawable
GameComponent.Initialize()
GameComponent.Update()
DrawableGameComponent.Draw()
Adding these objects to a Game.Components collection
aGame.Add(object)
enables the overridden methods to be called automatically using
base.methodName() – base.Update()
15
MonoGames XNA Basics
16
MonoGames XNA Basics
17
Build MonoGame
MonoGames XNA Basics
18
MonoGames XNA Basics
Follow all the steps for Build XNA project, except:
1. You do not need to add any content files to your project's Content
directory. Your Mono Games project's Content directory is empty.
2. You do not need to set "Hi Def" and you do not need to set any
Build "conditional compilation symbols"
Copy the ContentMono/Content subdirectory (directory with all the
*.xnb files) to your project's Debug directory
\axisMono\axisMono\bin\WindowsGL\Debug
to create
\axisMono\axisMono\bin\WindowsGL\Debug\Content
Your program should now compile and execute.
19
Download