1
0
mirror of https://github.com/Memorix101/UnityXNA/ synced 2024-12-30 15:25:35 +01:00

Added Viewport support

This commit is contained in:
Memorix101 2023-04-19 21:55:55 +02:00
parent 64046bbdfe
commit 602464fbc6
9 changed files with 101 additions and 29 deletions

View File

@ -13,6 +13,7 @@ class DrawSpriteCall
private Vector4 color;
private Vector2 origin;
private SpriteEffects spriteEffects;
private Viewport viewport;
public Texture2D Texture2D {
get {
@ -49,8 +50,16 @@ class DrawSpriteCall
return this.spriteEffects;
}
}
public DrawSpriteCall(Texture2D texture2D, Vector2 position, Nullable<Rectangle> source, Vector4 color, Vector2 origin, SpriteEffects spriteEffects)
public Viewport Viewport
{
get
{
return this.viewport;
}
}
public DrawSpriteCall(Texture2D texture2D, Vector2 position, Nullable<Rectangle> source, Vector4 color, Vector2 origin, SpriteEffects spriteEffects, Viewport viewport)
{
// TODO: Complete member initialization
this.texture2D = texture2D;
@ -59,6 +68,7 @@ class DrawSpriteCall
this.color = color;
this.origin = origin;
this.spriteEffects = spriteEffects;
}
this.viewport = viewport;
}
}

View File

@ -10,9 +10,10 @@ class DrawStringCall
private SpriteFont font;
private string value;
private Vector2 position;
private Vector4 color ;
private Vector4 color;
private Viewport viewport;
public SpriteFont Font {
public SpriteFont Font {
get {
return this.font;
}
@ -35,12 +36,22 @@ class DrawStringCall
return this.color;
}
}
public DrawStringCall(SpriteFont font, string value, Vector2 position, Vector4 color)
public Viewport Viewport
{
get
{
return this.viewport;
}
}
public DrawStringCall(SpriteFont font, string value, Vector2 position, Vector4 color, Viewport viewport)
{
this.font = font;
this.value = value;
this.position = position;
this.color = color;
this.viewport = viewport;
}
}

View File

@ -44,10 +44,18 @@ namespace Microsoft.Xna.Framework
{
content = new ContentManager(null, "");
_components = new GameComponentCollection();
_components = new GameComponentCollection();
}
protected virtual void Initialize()
{
}
protected virtual void Update(GameTime gameTime)
{
}
protected virtual void UnloadContent()
{
}
@ -96,14 +104,14 @@ namespace Microsoft.Xna.Framework
}
public void Dispose()
{
UnloadContent();
Dispose(true);
GC.SuppressFinalize(this);
}
internal void Begin()
{
Initialize();
LoadContent();
// XNA's first update call has a zero elapsed time, so do one now.
GameTime gameTime = new GameTime(new TimeSpan(0), new TimeSpan(0), new TimeSpan(0, 0, 0, 0, 0), new TimeSpan(0, 0, 0, 0, 0));
@ -118,8 +126,6 @@ namespace Microsoft.Xna.Framework
GameTime gameTime = new GameTime(new TimeSpan(0), new TimeSpan(0), new TimeSpan(totalTicks), new TimeSpan(ticks));
Update(gameTime);
Draw(gameTime);
}
}
}
}

View File

@ -10,6 +10,9 @@ namespace Microsoft.Xna.Framework
{
private Game game;
private int _preferredBackBufferHeight;
private int _preferredBackBufferWidth;
public GraphicsDevice GraphicsDevice
{
get
@ -23,5 +26,31 @@ namespace Microsoft.Xna.Framework
// TODO: Complete member initialization
this.game = game;
}
public int PreferredBackBufferWidth
{
get
{
return _preferredBackBufferWidth;
}
set
{
//_shouldApplyChanges = true;
_preferredBackBufferWidth = value;
}
}
public int PreferredBackBufferHeight
{
get
{
return _preferredBackBufferHeight;
}
set
{
//_shouldApplyChanges = true;
_preferredBackBufferHeight = value;
}
}
}
}

View File

@ -27,11 +27,11 @@ namespace Microsoft.Xna.Framework
{
if (!Application.isEditor)
{
return new Rectangle(Screen.mainWindowPosition.x, Screen.mainWindowPosition.y, Screen.height, Screen.width);
return new Rectangle(Screen.mainWindowPosition.x, Screen.mainWindowPosition.y, Screen.width, Screen.height);
}
else
{
return new Rectangle(0, 0, Screen.height, Screen.width);
return new Rectangle(0, 0, Screen.width, Screen.height);
}
}
}

View File

@ -71,19 +71,19 @@ namespace Microsoft.Xna.Framework.Graphics
internal void Draw(Texture2D texture2D, Vector2 position, Nullable<Rectangle> source, Color color, float p, Vector2 Origin, float p_2, SpriteEffects spriteEffects, float p_3)
{
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, position, source, color.ToVector4(), Origin, spriteEffects));
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, position, source, color.ToVector4(), Origin, spriteEffects, graphicsDevice.Viewport));
}
//TODO: Draw stretching
internal void Draw(Texture2D texture2D, Nullable<Rectangle> source, Color color)
{
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, Vector2.Zero, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None));
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, Vector2.Zero, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None, graphicsDevice.Viewport));
}
//Draw texture section
internal void Draw(Texture2D texture2D, Vector2 position, Nullable<Rectangle> source, Color color)
{
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, position, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None));
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, position, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None, graphicsDevice.Viewport));
}
//TODO:
@ -115,23 +115,23 @@ namespace Microsoft.Xna.Framework.Graphics
texCoordTL.X = temp;
}
*/
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture, Vector2.Zero, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None));
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture, Vector2.Zero, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None, graphicsDevice.Viewport));
}
//TODO:
internal void Draw (Texture2D texture, Nullable<Rectangle> source, Rectangle? sourceRectangle, Color color)
{
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture, Vector2.Zero, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None));
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture, Vector2.Zero, source, color.ToVector4(), Vector2.Zero, SpriteEffects.None, graphicsDevice.Viewport));
}
internal void Draw(Texture2D texture2D, Vector2 position, Color color)
{
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, position, null, color.ToVector4(), Vector2.Zero, SpriteEffects.None));
graphicsDevice.DrawQueue.EnqueueSprite(new DrawSpriteCall(texture2D, position, null, color.ToVector4(), Vector2.Zero, SpriteEffects.None, graphicsDevice.Viewport));
}
internal void DrawString(SpriteFont font, string value, Vector2 position, Color color)
{
graphicsDevice.DrawQueue.EnqueueString(new DrawStringCall(font, value, position, color.ToVector4()));
graphicsDevice.DrawQueue.EnqueueString(new DrawStringCall(font, value, position, color.ToVector4(), graphicsDevice.Viewport));
}
}
}

View File

@ -35,6 +35,14 @@ namespace Microsoft.Xna.Framework.Graphics
get { return unityTexture.height; }
}
public Rectangle Bounds
{
get
{
return new Rectangle(0, 0, unityTexture.width, unityTexture.height);
}
}
public void Dispose()
{ }
}

View File

@ -33,6 +33,7 @@ SOFTWARE.
using Microsoft.Xna.Framework;
using System;
using System.Globalization;
using UnityEngine;
namespace Microsoft.Xna.Framework.Graphics
{
@ -106,8 +107,7 @@ namespace Microsoft.Xna.Framework.Graphics
int x,
int y,
int width,
int height
)
int height)
{
this.x = x;
this.y = y;
@ -117,7 +117,14 @@ namespace Microsoft.Xna.Framework.Graphics
minDepth = 0;
maxDepth = 0;
titleSafeArea = new Rectangle(x, y, width, height);
}
/*GameObject _camGo = new GameObject("Camera");
_camGo.transform.position = new UnityEngine.Vector3((float)x, (float)y, 0.0f);
var _camera = _camGo.AddComponent<Camera>();
_camera.rect = new Rect(x, y, width, height);*/
//Camera.main.rect = new Rect(x, y, width, height);
}
#region Public Methods
public Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)

View File

@ -95,7 +95,9 @@ public class XNATest : MonoBehaviour {
sourceRect.height *= -1;
}
GUI.DrawTextureWithTexCoords(new Rect(x, y, width * Mathf.Abs(sourceRect.width), height * Mathf.Abs(sourceRect.height)), call.Texture2D.UnityTexture, sourceRect);
var viewport = call.Viewport;
//Debug.Log("Viewport: " + viewport);
GUI.DrawTextureWithTexCoords(new Rect(x + viewport.X, y + viewport.Y, width * Mathf.Abs(sourceRect.width), height * Mathf.Abs(sourceRect.height)), call.Texture2D.UnityTexture, sourceRect);
}
// Draw strings from SpriteBatch.DrawString()
@ -106,9 +108,7 @@ public class XNATest : MonoBehaviour {
GUI.color = new Color(call.Color.X, call.Color.Y, call.Color.Z, call.Color.W);
Vector2 size = GUI.skin.label.CalcSize(new GUIContent(call.Value));
string[] x = call.Font.PathTo.Split('/');
string path = $"{call.Font.PathTo.Remove(call.Font.PathTo.Length - 1 - x[x.Length-1].Length)}/{call.Font.FontName.ToString()}";
Font myFont = (Font)UnityEngine.Resources.Load($"{path}", typeof(Font));
Font myFont = (Font)UnityEngine.Resources.Load($"Content/{call.Font.FontName.ToString()}", typeof(Font));
//GUI.skin.font = myFont;
GUIStyle myStyle = new GUIStyle();
@ -116,7 +116,8 @@ public class XNATest : MonoBehaviour {
myStyle.fontSize = (int)call.Font.Size;
myStyle.normal.textColor = new Color(call.Color.X, call.Color.Y, call.Color.Z, call.Color.W);
GUI.Label(new Rect(call.Position.X, call.Position.Y, size.x, size.y), call.Value, myStyle);
var viewport = call.Viewport;
GUI.Label(new Rect(call.Position.X + viewport.X, call.Position.Y + viewport.Y, size.x, size.y), call.Value, myStyle);
}
//GUIStyle style = new GUIStyle();