From f57659e2e10eefa89c5c02ae05b460ce9fd46387 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Sat, 12 May 2012 22:02:23 -0500 Subject: [PATCH] Modfying draw method in AxiosGameScreen to draw Gleed2D textures --- axios/Axios_WP7.csproj | 1 + axios/Axios_Windows.csproj | 1 + axios/Axios_Xbox_360.csproj | 1 + axios/Axios_settings.cs | 2 + axios/Engine/AxiosGameScreen.cs | 26 +++++++++ axios/Engine/Gleed2D/Camera.cs | 82 +++++++++++++++++++++++++++ axios/Engine/Gleed2D/CircleItem.cs | 2 +- axios/Engine/Gleed2D/PathItem.cs | 2 +- axios/Engine/Gleed2D/RectangleItem.cs | 2 +- axios/Engine/Gleed2D/TextureItem.cs | 2 +- 10 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 axios/Engine/Gleed2D/Camera.cs diff --git a/axios/Axios_WP7.csproj b/axios/Axios_WP7.csproj index 1988eae..d280c7f 100644 --- a/axios/Axios_WP7.csproj +++ b/axios/Axios_WP7.csproj @@ -172,6 +172,7 @@ + diff --git a/axios/Axios_Windows.csproj b/axios/Axios_Windows.csproj index 35a045c..8f1f4cd 100644 --- a/axios/Axios_Windows.csproj +++ b/axios/Axios_Windows.csproj @@ -215,6 +215,7 @@ + diff --git a/axios/Axios_Xbox_360.csproj b/axios/Axios_Xbox_360.csproj index d76b353..9021116 100644 --- a/axios/Axios_Xbox_360.csproj +++ b/axios/Axios_Xbox_360.csproj @@ -165,6 +165,7 @@ + diff --git a/axios/Axios_settings.cs b/axios/Axios_settings.cs index a7de535..81e3a96 100644 --- a/axios/Axios_settings.cs +++ b/axios/Axios_settings.cs @@ -74,6 +74,8 @@ * - Adding a cache for loading in textures for Gleed2D * - Adding GetStream(FileMode) to get the stream of a file * - Adding support to load a Gleed2D level from a stream + * - Adjusting units for Gleed2D position for Farseer bodies + * - Modfying draw method in AxiosGameScreen to draw Gleed2D textures * */ diff --git a/axios/Engine/AxiosGameScreen.cs b/axios/Engine/AxiosGameScreen.cs index acb80b5..8cd820a 100644 --- a/axios/Engine/AxiosGameScreen.cs +++ b/axios/Engine/AxiosGameScreen.cs @@ -11,6 +11,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using GameStateManagement; +using Axios.Engine.Gleed2D; namespace Axios.Engine { @@ -36,6 +37,10 @@ namespace Axios.Engine private AxiosUIObject prevuiobj; private AxiosUIObject prevuifocusobj; + protected Level Level; + + private Camera camera; + public AxiosGameScreen() : base() { @@ -46,6 +51,7 @@ namespace Axios.Engine this._uiobjects = new List(); prevuiobj = null; prevuifocusobj = null; + } /*public void AddGameObject(T gameobject) @@ -163,6 +169,7 @@ namespace Axios.Engine public override void Activate(bool instancePreserved) { base.Activate(instancePreserved); + #if DEBUG if (!Axios.Settings.ScreenSaver) @@ -171,17 +178,36 @@ namespace Axios.Engine this.DebugSpriteFont = man.Load(this.DebugTextFont); } #endif + camera = new Camera(ScreenManager.GraphicsDevice.Viewport.Width, ScreenManager.GraphicsDevice.Viewport.Height); } public override void Draw(GameTime gameTime) { base.Draw(gameTime); + if (Level != null) + { + foreach (Layer layer in Level.Layers) + { + Vector2 oldcameraposition = camera.Position; + camera.Position *= layer.ScrollSpeed; + + ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, camera.matrix); + layer.draw(ScreenManager.SpriteBatch); + ScreenManager.SpriteBatch.End(); + + camera.Position = oldcameraposition; + } + } + foreach (AxiosGameObject g in (from x in (from i in _gameObjects where i is IDrawableAxiosGameObject select (IDrawableAxiosGameObject)i) orderby x.DrawOrder select x)) ((IDrawableAxiosGameObject)g).Draw(this, gameTime); foreach(AxiosUIObject g in (from x in _uiobjects orderby x.DrawOrder select x)) ((IDrawableAxiosGameObject)g).Draw(this, gameTime); + + //System.Diagnostics.Debugger.Break(); + } public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) diff --git a/axios/Engine/Gleed2D/Camera.cs b/axios/Engine/Gleed2D/Camera.cs new file mode 100644 index 0000000..1922daf --- /dev/null +++ b/axios/Engine/Gleed2D/Camera.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; + +namespace Axios.Engine.Gleed2D +{ + public class Camera + { + Vector2 position; + public Vector2 Position + { + get + { + return position; + } + set + { + position = value; + updatematrix(); + } + } + + float rotation; + public float Rotation + { + get + { + return rotation; + } + set + { + rotation = value; + updatematrix(); + } + } + + float scale; + public float Scale + { + get + { + return scale; + } + set + { + scale = value; + updatematrix(); + } + } + + public Matrix matrix; + Vector2 viewport; //width and height of the viewport + + + public Camera(float width, float height) + { + position = Vector2.Zero; + rotation = 0; + scale = 1.0f; + viewport = new Vector2(width, height); + updatematrix(); + } + + void updatematrix() + { + matrix = Matrix.CreateTranslation(-position.X, -position.Y, 0.0f) * + Matrix.CreateRotationZ(rotation) * + Matrix.CreateScale(scale) * + Matrix.CreateTranslation(viewport.X / 2, viewport.Y / 2, 0.0f); + } + + public void updateviewport(float width, float height) + { + viewport.X = width; + viewport.Y = height; + updatematrix(); + } + + } +} diff --git a/axios/Engine/Gleed2D/CircleItem.cs b/axios/Engine/Gleed2D/CircleItem.cs index 19fdef9..f48453f 100644 --- a/axios/Engine/Gleed2D/CircleItem.cs +++ b/axios/Engine/Gleed2D/CircleItem.cs @@ -28,7 +28,7 @@ namespace Axios.Engine.Gleed2D base.load(cm, world, ref cache); _body = BodyFactory.CreateCircle(world, Radius, 1f); - _body.Position = Position; + _body.Position = ConvertUnits.ToSimUnits(Position); _body.UserData = this; } diff --git a/axios/Engine/Gleed2D/PathItem.cs b/axios/Engine/Gleed2D/PathItem.cs index 202abd7..3af9567 100644 --- a/axios/Engine/Gleed2D/PathItem.cs +++ b/axios/Engine/Gleed2D/PathItem.cs @@ -35,7 +35,7 @@ namespace Axios.Engine.Gleed2D v.Add(new Vector2(ConvertUnits.ToSimUnits(vec.X), ConvertUnits.ToSimUnits(vec.Y))); _body = BodyFactory.CreateLoopShape(world, v); - _body.Position = this.Position; + _body.Position = ConvertUnits.ToSimUnits(this.Position); _body.UserData = this; } diff --git a/axios/Engine/Gleed2D/RectangleItem.cs b/axios/Engine/Gleed2D/RectangleItem.cs index 706a842..d455aad 100644 --- a/axios/Engine/Gleed2D/RectangleItem.cs +++ b/axios/Engine/Gleed2D/RectangleItem.cs @@ -29,7 +29,7 @@ namespace Axios.Engine.Gleed2D base.load(cm, world, ref cache); _body = BodyFactory.CreateRectangle(world, Width, Height, 1f); - _body.Position = Position; + _body.Position = ConvertUnits.ToSimUnits(Position); _body.UserData = this; } } diff --git a/axios/Engine/Gleed2D/TextureItem.cs b/axios/Engine/Gleed2D/TextureItem.cs index cd2d31b..f1c8467 100644 --- a/axios/Engine/Gleed2D/TextureItem.cs +++ b/axios/Engine/Gleed2D/TextureItem.cs @@ -25,7 +25,7 @@ namespace Axios.Engine.Gleed2D /// /// The item's scale factor. /// - public float Scale; + public Vector2 Scale; /// /// The color to tint the item's texture with (use white for no tint).