} // the rate for each state (how many ticks before switching a frame) #endregion public SpriteAnalyzer(Hero hero, State state) { Rate = (int)state / 10; //using the rate of each state declared in Global to decide how many ticks pass until the frame switches #region tex and color Tex = Global.cm.Load <Texture2D>(hero.ToString() + "/" + state.ToString()); //accessing the desired sprite Color[] c = new Color[Tex.Width]; //creating a new color array Tex.GetData <Color>(0, new Rectangle(0, Tex.Height - 1, Tex.Width, 1), c, 0, c.Length); //inserting the color from the texture into the color array #endregion #region black List <int> black = new List <int>(); //creating a new list for the locations of the black dots responsible for dividing the sprite for (int i = 0; i < c.Length; i++) //for every pixel in the bottom of the sprite { if (c[i] != c[1]) //if the dot is black { black.Add(i); //add the location of the dot into the list } } #endregion #region recs for (int i = 0; i < black.Count() - 2; i += 2) //for every second black dot in the array starting from the first one besides the last two -- we dont go over the last 2 because for every dot we have to access the two dots after it { Recs.Add(new Rectangle(black[i], 0, black[i + 2] - black[i], Tex.Height - 2)); //make a rectangle starting at the dot and ending two dots after it which marks the frame } #endregion #region orgs for (int i = 1; i < black.Count() - 1; i += 2) // for every second dot starting from the second dot { Orgs.Add(new Vector2(black[i] - black[i - 1], Tex.Height - 2)); // we add every second dot into the orgs array to mark it is and origin for the specific frame } #endregion makeTranBg(); //calling for a function to erase the picture background and make it transparent }
void makeTranBg() { Color[] c = new Color[Tex.Width * Tex.Height]; //making a new color array Tex.GetData <Color>(c); //inserting the color from the texture into the color array Color back = c[0]; //declaring the first pixel of the texture to be the background color for (int i = 0; i < c.Length; i++) // for every spot in the color array { if (c[i] == back) // if the color is the background color { c[i] = Color.Transparent; // change the color to transparent } } Tex.SetData <Color>(c); //sending all the pixels back into the texture after making the background transparent }
void MakeBgTrans() { //This Method Tekes the whole Texture and turns the background color into clear pixels. Color[] c = new Color[Tex.Width * Tex.Height]; Tex.GetData <Color>(c); Color trans = c[0]; for (int i = 0; i < c.Length; i++) { if (c[i] == trans) { c[i] = Color.Transparent; } } //Putting back the clear version of the texture into the Texture. Tex.SetData <Color>(c); }
/// <summary> /// This Method takes all the information to drawing a frame from /// Dots on the Texture itself. black Dots On Tex To represent The /// Origin And Rectangle places, and the green spots on the /// CollisionMask represent the center of the circels and their radiuses. /// </summary> /// <param name="hero">A chracter from enum Character</param> /// <param name="state">A state from enum State</param> public SpriteStrip(Character hero, State state) { //Loading the Texture and Collision Texture respectivly. string path = Directory.GetCurrentDirectory(); String TexPath = path + "/content/" + hero.ToString() + "/" + state.ToString() + ".xnb"; if (File.Exists(TexPath)) { Tex = G.cm.Load <Texture2D>(hero.ToString() + "/" + state.ToString()); } String ColMaskPath = path + "/content/" + hero.ToString() + "Mask" + "/" + state.ToString() + "Mask" + ".xnb"; if (File.Exists(ColMaskPath)) { CollisionMask = G.cm.Load <Texture2D>(hero.ToString() + "Mask" + "/" + state.ToString() + "Mask"); } //Creats an array of color, with width of the tex.width Color[] Cstrip = new Color[Tex.Width]; Tex.GetData <Color>(0, new Rectangle(0, Tex.Height - 1, Tex.Width, 1), Cstrip, 0, Cstrip.Length); // Takes a rectangle from a texture and puts it into a color array (Cstrip) List <int> Bpnts = new List <int>(); // a list of int points that contain the X position of the balck dots //Puts every black point in the arrray. for (int i = 0; i < Cstrip.Length; i++) { if (Cstrip[i] != Cstrip[1]) { Bpnts.Add(i); } } // Creates the diffrent "frames" of the animation strip by skipping // Two at a time on the array of black point. for (int i = 0; i < Bpnts.Count - 2; i += 2) { Rectangle r = new Rectangle(Bpnts[i], 0, Bpnts[i + 2] - Bpnts[i], Tex.Height - 1); Recs.Add(r); } //The method uses radCount to count the radius of the Current circle. //And tmpCulmn to skip over the next green Dot and not mistake it for another circle. int radCount = 0; int tmpCulmn; for (int i = 0; i < Recs.Count; i++) { // Creats an array of color, with width and height of the current rectangel Color[] tmpCS = new Color[Recs[i].Width * Recs[i].Height]; CollisionMask.GetData <Color>(0, Recs[i], tmpCS, 0, tmpCS.Length); List <CollisionCircle> SemiCollisionCircles = new List <CollisionCircle>(); for (int row = 0; row < Recs[i].Height; row++) { radCount = 0; tmpCulmn = 0; for (int culmn = 1; culmn < Recs[i].Width; culmn++) { if (tmpCS[(row * Recs[i].Width) + culmn + tmpCulmn] == tmpCS[0]) { radCount = 0; tmpCulmn = culmn + 1; while (tmpCS[(row * Recs[i].Width) + tmpCulmn] != tmpCS[0]) { tmpCulmn++; radCount++; } //Creating the Circle. CollisionCircle c = new CollisionCircle(new Vector2(culmn, row), (float)radCount); SemiCollisionCircles.Add(c); } } } Circuli.Add(SemiCollisionCircles); } // Creates the diffrent "origins" of each frame. for (int i = 1; i < Bpnts.Count - 1; i += 2) { Vector2 v = new Vector2(Bpnts[i] - Bpnts[i - 1], Tex.Height - 2); Origs.Add(v); } MakeBgTrans(); }