﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace hic {

    public class ObjStatsDisplay : MonoBehaviour {

        private float[] m_lastDeltas; // looping array storing delta time for a sequence of frames
        private int m_deltaIndex;     // index in the looping array

        //
        //
        //
        void Start ()
        {
            m_lastDeltas = new float[50];
        }

        //
        //
        //
        void Update ()
        {
        
        }

        //
        //
        //
        void OnGUI()
        {
            string str = GetFPS();
            GUI.Label( new Rect (10,10,100,50), str);
        }

        string GetFPS()
        {
            // Add current value to the looping array
            m_deltaIndex = ++m_deltaIndex % m_lastDeltas.Length;
            m_lastDeltas[m_deltaIndex] = Time.deltaTime;

            // Compute average
            float totalDeltas = 0;
            foreach(float delta in m_lastDeltas)
            {
                totalDeltas += delta;
            }
            float avgDeltas = totalDeltas/m_lastDeltas.Length;

            // Compute FPS
            double fpsd = 1/avgDeltas;
            string str = string.Format("{0:0.##}", fpsd);
            return str;
        }
    }
}