2012-07-07 20:57:54 +01:00
|
|
|
using UnityEngine;
|
|
|
|
using System.Collections;
|
2023-04-16 17:46:17 +02:00
|
|
|
using Platformer; // your game namespace here
|
|
|
|
using System.IO;
|
2012-07-07 20:57:54 +01:00
|
|
|
|
|
|
|
public class XNATest : MonoBehaviour {
|
2023-04-16 17:46:17 +02:00
|
|
|
PlatformerGame game; // your game routine here
|
2012-07-07 20:57:54 +01:00
|
|
|
DrawQueue drawQueue;
|
|
|
|
|
|
|
|
public float updateInterval = 0.5F;
|
|
|
|
|
|
|
|
private float accum = 0; // FPS accumulated over the interval
|
|
|
|
private int frames = 0; // Frames drawn over the interval
|
|
|
|
private float timeleft; // Left time for current interval
|
|
|
|
float fps;
|
|
|
|
// Use this for initialization
|
|
|
|
void Start ()
|
|
|
|
{
|
|
|
|
Application.targetFrameRate = 60;
|
|
|
|
|
|
|
|
// Add an audio source and tell the media player to use it for playing sounds
|
|
|
|
Microsoft.Xna.Framework.Media.MediaPlayer.AudioSource = gameObject.AddComponent<AudioSource>();
|
|
|
|
|
|
|
|
drawQueue = new DrawQueue();
|
2023-04-16 17:46:17 +02:00
|
|
|
game = new PlatformerGame(); // your game routine here
|
2012-07-07 20:57:54 +01:00
|
|
|
game.DrawQueue = drawQueue;
|
|
|
|
game.Begin();
|
|
|
|
timeleft = updateInterval;
|
|
|
|
//Application.targetFrameRate = 30;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update () {
|
|
|
|
|
|
|
|
float deltaTime = Time.deltaTime;
|
|
|
|
if(deltaTime > 0.050f)
|
|
|
|
{
|
|
|
|
deltaTime = 0.050f;
|
|
|
|
}
|
|
|
|
//Debug.Log(deltaTime);
|
|
|
|
game.Tick(deltaTime);
|
|
|
|
drawQueue.Clear();
|
|
|
|
|
|
|
|
timeleft -= Time.deltaTime;
|
|
|
|
accum += Time.timeScale/Time.deltaTime;
|
|
|
|
++frames;
|
|
|
|
|
|
|
|
// Interval ended - update GUI text and start new interval
|
|
|
|
if( timeleft <= 0.0 )
|
|
|
|
{
|
|
|
|
// display two fractional digits (f2 format)
|
|
|
|
fps = accum/frames;
|
|
|
|
|
|
|
|
// DebugConsole.Log(format,level);
|
|
|
|
timeleft = updateInterval;
|
|
|
|
accum = 0.0F;
|
|
|
|
frames = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
bool a= false;
|
|
|
|
|
|
|
|
void OnGUI()
|
|
|
|
{
|
|
|
|
// Draw sprites from SpriteBatch.Draw()
|
|
|
|
for (int i = 0; i < drawQueue.LastSpriteQueue.Length; i++)
|
|
|
|
{
|
|
|
|
DrawSpriteCall call = drawQueue.LastSpriteQueue[i];
|
|
|
|
float x = call.Position.X;
|
|
|
|
float y = call.Position.Y;
|
|
|
|
x -= call.Origin.X;
|
|
|
|
y -= call.Origin.Y;
|
|
|
|
float width = call.Texture2D.UnityTexture.width;
|
|
|
|
float height = call.Texture2D.UnityTexture.height;
|
|
|
|
GUI.color = new Color(call.Color.X, call.Color.Y, call.Color.Z, call.Color.W);
|
|
|
|
|
2023-04-16 17:46:17 +02:00
|
|
|
Rect sourceRect = new Rect(0, 0, 1, 1);
|
2012-07-07 20:57:54 +01:00
|
|
|
|
|
|
|
if(call.Source != null)
|
|
|
|
{
|
|
|
|
sourceRect.x = call.Source.Value.X / width;
|
|
|
|
sourceRect.y = call.Source.Value.Y / height;
|
|
|
|
sourceRect.width = call.Source.Value.Width / width;
|
|
|
|
sourceRect.height = call.Source.Value.Height / height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(call.SpriteEffects == Microsoft.Xna.Framework.Graphics.SpriteEffects.FlipHorizontally)
|
|
|
|
{
|
|
|
|
sourceRect.x = 1-sourceRect.x;
|
|
|
|
sourceRect.width *= -1;
|
|
|
|
}
|
|
|
|
else if(call.SpriteEffects == Microsoft.Xna.Framework.Graphics.SpriteEffects.FlipVertically)
|
|
|
|
{
|
|
|
|
sourceRect.y = 1-sourceRect.y;
|
|
|
|
sourceRect.height *= -1;
|
|
|
|
}
|
2023-04-16 17:46:17 +02:00
|
|
|
|
2023-04-19 21:55:55 +02:00
|
|
|
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);
|
2023-04-16 17:46:17 +02:00
|
|
|
}
|
|
|
|
|
2012-07-07 20:57:54 +01:00
|
|
|
// Draw strings from SpriteBatch.DrawString()
|
|
|
|
for (int i = 0; i < drawQueue.LastStringQueue.Length; i++)
|
|
|
|
{
|
|
|
|
DrawStringCall call = drawQueue.LastStringQueue[i];
|
|
|
|
|
|
|
|
GUI.color = new Color(call.Color.X, call.Color.Y, call.Color.Z, call.Color.W);
|
2023-04-16 17:46:17 +02:00
|
|
|
|
|
|
|
Vector2 size = GUI.skin.label.CalcSize(new GUIContent(call.Value));
|
2023-04-20 03:38:56 +02:00
|
|
|
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));
|
2023-04-16 17:46:17 +02:00
|
|
|
//GUI.skin.font = myFont;
|
|
|
|
|
|
|
|
GUIStyle myStyle = new GUIStyle();
|
|
|
|
myStyle.font = myFont;
|
|
|
|
myStyle.fontSize = (int)call.Font.Size;
|
|
|
|
myStyle.normal.textColor = new Color(call.Color.X, call.Color.Y, call.Color.Z, call.Color.W);
|
|
|
|
|
2023-04-19 21:55:55 +02:00
|
|
|
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);
|
2012-07-07 20:57:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//GUIStyle style = new GUIStyle();
|
|
|
|
//style.font = Font.
|
|
|
|
|
|
|
|
//GUILayout.Label()
|
|
|
|
//GUI.color = Color.black;
|
|
|
|
//GUILayout.Label(string.Format("{0} fps, {1} sprite draw calls", fps, drawQueue.LastSpriteQueue.Length));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|