using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WOMGeneral { public class UtilBase { public static int ReadTwoBytes(byte b1, byte b2) { return b1 * 256 + b2; } public static int ReadTwoBytes(int b1, int b2) { return ReadTwoBytes((byte)b1, (byte)b2); } public static long ReadFourBytes(byte b1, byte b2, byte b3, byte b4) { return b1 * 16777216 + b2 * 65536 + b3 * 256 + b4; } public static long ReadFourBytes(int b1, int b2, int b3, int b4) { return ReadFourBytes((byte)b1, (byte)b2, (byte)b3, (byte)b4); } public static bool InRange(int x, int y, int startX, int startY, int width, int height) { if (x >= startX && x < startX + width) { if (y >= startY && y < startY + height) { return true; } } return false; } public static byte[] WriteTwoBytes(int number) { byte[] toReturn = new byte[2]; toReturn[0] = (byte)Math.Floor((float)number / 256f); toReturn[1] = (byte)(number - toReturn[0] * 256); return toReturn; } public static byte[] MergeArrays(byte[][] toMerge) { int arrayLength = 0; for (int i = 0; i < toMerge.Length; i++) { arrayLength += toMerge[i].Length; } byte[] toReturn = new byte[arrayLength]; long position = 0; for (int i = 0; i < toMerge.Length; i++) { for (int ii = 0; ii < toMerge[i].Length; ii++) { toReturn[position] = toMerge[i][ii]; position++; } } return toReturn; } public static bool CheckCollision(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { if (x1 + w1 > x2) { if (x2 + w2 > x1) { if (y1 + h1 > y2) { if (y2 + h2 > y1) { return true; } } } } return false; } private static byte[] binaryDivisors = new byte[] { 128, 64, 32, 16, 8, 4, 2, 1 }; public static bool[] ByteToBooleans(byte b) { bool[] toReturn = new bool[8]; for (int i = 0; i < binaryDivisors.Length; i++) { if (b >= binaryDivisors[i]) { toReturn[i] = true; b -= binaryDivisors[i]; } } return toReturn; } public static byte BooleansToByte(bool[] bools) { byte toReturn = 0; for (int i = 0; i < binaryDivisors.Length; i++) { if (bools[i]) toReturn += binaryDivisors[i]; } return toReturn; } } }