recrownedathenaeum/CameraTest/CameraTest.cs

150 lines
6.0 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Camera;
using RecrownedAthenaeum.Render;
using System;
namespace CameraTest
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class CameraTest : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
PrimitiveBatch pb;
Camera2D camera;
RectangleRenderer rr;
BasicEffect be;
public CameraTest()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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.
camera = new Camera2D(graphics.GraphicsDevice);
spriteBatch = new SpriteBatch(GraphicsDevice);
rr = new RectangleRenderer(camera, GraphicsDevice);
pb = new PrimitiveBatch(camera, GraphicsDevice);
be = new BasicEffect(GraphicsDevice);
be.World = camera.worldMatrix;
be.View = camera.ViewMatrix;
be.Projection = camera.projectionMatrix;
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
rr.Dispose();
}
/// <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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
Console.WriteLine("moving..");
camera.MoveCamera(new Vector2(-5, 0));
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
Console.WriteLine("moving..");
camera.MoveCamera(new Vector2(5, 0));
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
Console.WriteLine("moving..");
camera.MoveCamera(new Vector2(0, 5));
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
Console.WriteLine("moving..");
camera.MoveCamera(new Vector2(0, -5));
}
camera.Apply();
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)
{
GraphicsDevice.Clear(Color.Black);
rr.Draw(25, 25, 70, 70, Color.Purple);
/*
* float rotation = 0;
double width = 20;
double height = 20;
int x = 50;
int y = 50;
double topRightAngleFromOrig = Math.Atan(height / width);
pb.Begin(PrimitiveType.LineList);
Vector2 bottomLeft = new Vector2(x, y);
Vector2 bottomRight = new Vector2(x + (float)(Math.Cos(rotation) * width), y + (float)(Math.Sin(rotation) * width));
float origDiagonalHypotenuse = (float)Math.Sqrt(width * width + height * height);
Vector2 topRight = new Vector2(x + (float)Math.Cos(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse, y + (float)Math.Sin(topRightAngleFromOrig + rotation) * origDiagonalHypotenuse);
Vector2 topLeft = new Vector2(x - (float)(Math.Cos((Math.PI / 2f) - rotation) * height), y + (float)(Math.Sin((Math.PI / 2f) - rotation) * height));
verts[0] = bottomLeft;
verts[1] = bottomRight;
verts[2] = topRight;
verts[3] = topLeft;
vertposcol[0] = new VertexPositionColor(new Vector3(verts[0], 0), Color.White);
vertposcol[1] = new VertexPositionColor(new Vector3(verts[1], 0), Color.White);
vertposcol[2] = new VertexPositionColor(new Vector3(verts[2], 0), Color.White);
vertposcol[3] = new VertexPositionColor(new Vector3(verts[3], 0), Color.White);
EffectTechnique effectTechnique = be.Techniques[0];
EffectPassCollection effectPassCollection = effectTechnique.Passes;
foreach (EffectPass pass in effectPassCollection)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertposcol, 0, 4);
}
pb.End();
*/
base.Draw(gameTime);
}
}
}