/Assets/BG_Scripts/Sprites.cs

https://bitbucket.org/copelands/csc490_final · C# · 181 lines · 138 code · 36 blank · 7 comment · 3 complexity · 69f4dd21818f4dd714ad50cbbea4ab49 MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. #pragma warning disable 0168, 0219, 0414
  8. #region RECT
  9. [StructLayout(LayoutKind.Sequential)]
  10. public struct RECT
  11. {
  12. public int x;
  13. public int y;
  14. public int w;
  15. public int h;
  16. public override string ToString ()
  17. {
  18. return "x:" + x +
  19. " y:" + y +
  20. " w:" + w +
  21. " h:" + h;
  22. }
  23. };
  24. #endregion
  25. public class Sprites
  26. {
  27. #region Public Variables
  28. public String title = "/Art/title.png",
  29. controls = "/Art/controls.png";
  30. #endregion
  31. #region Public Properties
  32. public Vector3[] Verticies
  33. {
  34. get { return verts; }
  35. }
  36. public Vector2[] UVs
  37. {
  38. get { return uvmap; }
  39. }
  40. public int[] Triangles
  41. {
  42. get { return triangles; }
  43. }
  44. public Vector3[] Norms
  45. {
  46. get { return norms; }
  47. }
  48. #endregion
  49. #region Private Variables
  50. private int size = 745;
  51. private string EXT = ".png";
  52. private IDictionary spriteDictionary;
  53. public Texture2D spriteSheet;
  54. private Vector3[] verts, norms;
  55. private Vector2[] uvmap;
  56. private int[] triangles;
  57. #endregion
  58. public Sprites()
  59. {
  60. #region Verts, UVs, Tris
  61. uvmap = new Vector2[]
  62. {
  63. new Vector2(1, 1),
  64. new Vector2(1, 0),
  65. new Vector2(0, 1),
  66. new Vector2(0, 0)
  67. };
  68. norms = new Vector3[]
  69. {
  70. Vector3.back,
  71. Vector3.back,
  72. Vector3.back,
  73. Vector3.forward
  74. };
  75. triangles = new int[] {0,1,2, 2,1,3};
  76. #endregion
  77. return;
  78. #region Parse
  79. spriteDictionary = new Dictionary<string, RECT>();
  80. StreamReader sr;
  81. using(sr = new StreamReader("Sprites/SpriteMap.csm"))
  82. {
  83. CSMParser parse = new CSMParser();
  84. string temp = "";
  85. while (!(temp = sr.ReadLine()).Equals("#END"))
  86. {
  87. spriteDictionary.Add(temp, parse.GetRECT(sr.ReadLine()));
  88. }
  89. sr.Close();
  90. }
  91. spriteSheet = new Texture2D(745, 745);
  92. spriteSheet.LoadImage(File.ReadAllBytes(Directory.GetCurrentDirectory() + "/Resources/c.png"));
  93. #endregion
  94. }
  95. public void GetSprite(string name, ref Texture2D texture)
  96. {
  97. RECT rect = (RECT)spriteDictionary[name];
  98. texture = new Texture2D(rect.w, rect.h);
  99. texture = new Texture2D(rect.w, rect.h);
  100. Color c;
  101. for(int i = 0; i < rect.w; i++)
  102. {
  103. for(int j = 0; j < rect.h; j++)
  104. {
  105. c = spriteSheet.GetPixel (rect.x + i, size - rect.y - rect.h + j);
  106. texture.SetPixel(i, j, c);
  107. }
  108. }
  109. texture.Apply();
  110. }
  111. #region PARSER
  112. private class CSMParser
  113. {
  114. /// <summary>
  115. /// Gets the RECT for this frame.
  116. /// </summary>
  117. /// <param name='s'>
  118. /// String containing frame information.
  119. /// </param>
  120. public RECT GetRECT(string frameInfo)
  121. {
  122. try
  123. {
  124. RECT frame = new RECT();
  125. string s = frameInfo.Clone() as string;
  126. s = s.Substring(s.IndexOf("x") + 2);
  127. frame.x = Int32.Parse(s.Substring(0, s.IndexOf(',')));
  128. s = s.Substring(s.IndexOf(':') + 1);
  129. frame.y = Int32.Parse(s.Substring(0, s.IndexOf(',')));
  130. s = s.Substring(s.IndexOf(':') + 1);
  131. frame.w = Int32.Parse(s.Substring(0, s.IndexOf(',')));
  132. s = s.Substring(s.IndexOf(':') + 1);
  133. frame.h = Int32.Parse(s);
  134. return frame;
  135. }
  136. catch(Exception e)
  137. {
  138. Debug.Log ("File Altered.");
  139. //someone messed with the file. unacceptable
  140. return new RECT();
  141. }
  142. }
  143. }
  144. #endregion
  145. }