using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
public class ScrollingBackgroundExample : MonoBehaviour {

    public string persistentDataPath;
    public string currentRomName;
    public GameObject ScrollingBackground;
    public Material ScrollingBackgroundMat;
    public Texture2D ScrollingBackgroundT2D;
    public float speed = 1.00f;
    public Text DEBUGTEXT;
    // Use this for initialization
    public IEnumerator OnScriptLoaded() {
        persistentDataPath = Application.dataPath + "/../";
        currentRomName = RxScriptInit.currentRomName;
        ScrollingBackground = GameObject.Find("CustomBackground");
        ScrollingBackgroundMat = ScrollingBackground.GetComponent<Renderer>().material;
        ScrollingBackgroundT2D = ScrollingBackgroundMat.mainTexture as Texture2D;
        byte[] pngBytes = File.ReadAllBytes(persistentDataPath + "/Enhancements/" + currentRomName + "/-MyPackage-/Scrolling001.png");
        ScrollingBackgroundT2D.LoadImage(pngBytes);
        yield return new WaitForSeconds(0.5f);
        Debug.Log("Loaded : ScrollingBackgroundExample!");
        DEBUGTEXT = GameObject.Find("DEBUGTEXT").GetComponent<Text>();
    }

    public void Update()
    {
        /*
        if (Input.GetKeyUp(KeyCode.I))
        {
            speed -= 0.001f;
        }
        if (Input.GetKeyUp(KeyCode.U))
        {
            speed += 0.001f;
        }*/
        
        ScrollingBackgroundMat.mainTextureOffset = new Vector2((float)((((MyNes.NesEmu.mem_wram[109]*255) + MyNes.NesEmu.mem_wram[1820]) * 0.003921568627451f)), 0);
       //DEBUGTEXT.text = ((((MyNes.NesEmu.mem_wram[109]*255) + MyNes.NesEmu.mem_wram[1820]) * 0.003921568627451f)).ToString();
    }
}