Advertisement
Ham62

pConLib.cs

Jan 22nd, 2017
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.42 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Serialization;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace ConGFXLib
  10. {
  11.     unsafe public struct TxtGFX
  12.     {
  13.         public short Size;
  14.         public short Width;
  15.         public short Height;
  16.         public ushort* Image;
  17.     }
  18.  
  19.     unsafe public static partial class pCon
  20.     {
  21.         // C# console wouldn't display extended ASCII correctly so we can
  22.         // use this table to display the characters instead of converting
  23.         // our buffers from ASCII to unicode.
  24.         public static readonly ushort[] UTF16Lookup = {
  25.             0x0000,0x263A,0x263B,0x2665,0x2666,0x2663,0x2660,0x2022,
  26.             0x25D8,0x25CB,0x25D9,0x2642,0x2640,0x266A,0x266B,0x263C,
  27.             0x25BA,0x25C4,0x2195,0x203C,0x00B6,0x00A7,0x25AC,0x21A8,
  28.             0x2191,0x2193,0x2192,0x2190,0x221F,0x2194,0x25B2,0x25BC,
  29.             0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027,
  30.             0x0028,0x0029,0x002A,0x002B,0x002C,0x002D,0x002E,0x002F,
  31.             0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,
  32.             0x0038,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x003F,
  33.             0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,
  34.             0x0048,0x0049,0x004A,0x004B,0x004C,0x004D,0x004E,0x004F,
  35.             0x0050,0x0051,0x0052,0x0053,0x0054,0x0055,0x0056,0x0057,
  36.             0x0058,0x0059,0x005A,0x005B,0x005C,0x005D,0x005E,0x005F,
  37.             0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,
  38.             0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F,
  39.             0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,
  40.             0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x2302,
  41.             0x00C7,0x00FC,0x00E9,0x00E2,0x00E4,0x00E0,0x00E5,0x00E7,
  42.             0x00EA,0x00EB,0x00E8,0x00EF,0x00EE,0x00EC,0x00C4,0x00C5,
  43.             0x00C9,0x00E6,0x00C6,0x00F4,0x00F6,0x00F2,0x00FB,0x00F9,
  44.             0x00FF,0x00D6,0x00DC,0x00F8,0x00A3,0x00D8,0x00D7,0x0192,
  45.             0x00E1,0x00ED,0x00F3,0x00FA,0x00F1,0x00D1,0x00AA,0x00BA,
  46.             0x00BF,0x00AE,0x00AC,0x00BD,0x00BC,0x00A1,0x00AB,0x00BB,
  47.             0x2591,0x2592,0x2593,0x2502,0x2524,0x00C1,0x00C2,0x00C0,
  48.             0x00A9,0x2563,0x2551,0x2557,0x255D,0x00A2,0x00A5,0x2510,
  49.             0x2514,0x2534,0x252C,0x251C,0x2500,0x253C,0x00E3,0x00C3,
  50.             0x255A,0x2554,0x2569,0x2566,0x2560,0x2550,0x256C,0x00A4,
  51.             0x00F0,0x00D0,0x00CA,0x00CB,0x00C8,0x0131,0x00CD,0x00CE,
  52.             0x00CF,0x2518,0x250C,0x2588,0x2584,0x00A6,0x00CC,0x2580,
  53.             0x00D3,0x00DF,0x00D4,0x00D2,0x00F5,0x00D5,0x00B5,0x00FE,
  54.             0x00DE,0x00DA,0x00DB,0x00D9,0x00FD,0x00DD,0x00AF,0x00B4,
  55.             0x00AD,0x00B1,0x2017,0x00BE,0x00B6,0x00A7,0x00F7,0x00B8,
  56.             0x00B0,0x00A8,0x00B7,0x00B9,0x00B3,0x00B2,0x25A0,0x00A0
  57.         };
  58.         static byte[] FILE_MAGIC = { //0x54474658
  59.             (byte)'T', (byte)'G', (byte)'F', (byte)'X'
  60.         };
  61.  
  62.         // Constants for the console color codes
  63.         public static class Colors
  64.         {
  65.             public const byte
  66.                 BLACK = 0x00, BLUE = 0x01, GREEN = 0x02, CYAN = 0x03,
  67.                 RED = 0x04, MAGENTA = 0x05, BROWN = 0x06, LIGHT_GRAY = 0x07,
  68.                 DARK_GRAY = 0x08, BRIGHT_BLUE = 0x09, BRIGHT_GREEN = 0x0A,
  69.                 BRIGHT_CYAN = 0x0B, BRIGHT_RED = 0x0C, BRIGHT_MAGENTA = 0x0D,
  70.                 BRIGHT_YELLOW = 0x0E, WHITE = 0x0F;
  71.         }
  72.  
  73.         public static int Width, Height;
  74.         public static int CursorX, CursorY;
  75.         public static byte PrintColor = 0x0F;
  76.  
  77.         static void* hOldScreen;
  78.         static ushort* pNewScreen;
  79.  
  80.         public static void* Create(int iWidth = 80, int iHeight = 25)
  81.         {
  82.             if (iWidth <= 8 || iHeight <= 4)
  83.             {
  84.                 MessageBox(0, "ConsoleCreate: Bad console size selected", null, MB_ICONERROR);
  85.                 return (uint*)0;
  86.             }
  87.  
  88.             Console.CursorVisible = false;
  89.             CursorX = 0; CursorY = 0;
  90.  
  91.             Console.SetWindowSize(iWidth, iHeight);
  92.             Width = iWidth; Height = iHeight;
  93.  
  94.             hOldScreen = GetStdHandle(STD_OUTPUT_HANDLE);
  95.             pNewScreen = (ushort*)Marshal.AllocHGlobal(iWidth * iHeight * 2);
  96.  
  97.             return pNewScreen;
  98.         }
  99.  
  100.         public static void Update()
  101.         {
  102.             COORD ConSize, BuffStart;
  103.             BuffStart.X = 0; BuffStart.Y = 0;
  104.             ConSize.X = (short)Width; ConSize.Y = (short)Height;
  105.  
  106.             SMALL_RECT WriteRegion;
  107.             WriteRegion.Left = 0; WriteRegion.Top = 0;
  108.             WriteRegion.Right = (short)(Width - 1); WriteRegion.Bottom = (short)(Height - 1);
  109.  
  110.             //Convert from DOS format to Windows console format
  111.             CHAR_INFO[] tNative = new CHAR_INFO[Width * Height];
  112.             for (int iN = 0; iN < (Width * Height); iN++)
  113.             {
  114.                 //tNative[iN].AsciiChar = (ushort)(pNewScreen[iN] & 0xFF);
  115.                 tNative[iN].UnicodeChar = UTF16Lookup[(byte)(pNewScreen[iN] & 0xFF)];
  116.                 tNative[iN].Attributes = (ushort)(pNewScreen[iN] >> 8);
  117.             }
  118.  
  119.             var result = WriteConsoleOutput(hOldScreen, tNative,
  120.                             ConSize, BuffStart, ref WriteRegion);
  121.  
  122.             if (result == false)
  123.             {
  124.                 MessageBox(0, "ConsoleUpdate: Failed to WriteConsoleOutput", null, MB_ICONERROR);
  125.             }
  126.         }
  127.  
  128.         public static byte ConColor(byte Fore = 0xFF, byte Back = 0xFF)
  129.         {
  130.             if (Back == 0xFF) { Back = (byte)(PrintColor >> 4); }
  131.             if (Fore == 0xFF) { Fore = (byte)(PrintColor & 0xFF); }
  132.             return (byte)(Fore + (Back << 4));
  133.         }
  134.  
  135.         public static void SetChar(int X, int Y, byte ConChar)
  136.         {
  137.             byte* TmpPtr = (byte*)pNewScreen;
  138.             TmpPtr[(X + (Y * Width)) << 1] = ConChar;
  139.         }
  140.  
  141.         public static byte GetChar(int X, int Y)
  142.         {
  143.             byte* TmpPtr = (byte*)pNewScreen;
  144.             return TmpPtr[(X + (Y * Width)) << 1];
  145.         }
  146.  
  147.         public static void SetAttribute(int X, int Y, byte Attribute)
  148.         {
  149.             byte* TmpPtr = (byte*)pNewScreen;
  150.             TmpPtr[((X + (Y * Width)) << 1) + 1] = Attribute;
  151.         }
  152.  
  153.         public static byte GetAttribute(int X, int Y)
  154.         {
  155.             byte* TmpPtr = (byte*)pNewScreen;
  156.             return TmpPtr[((X + (Y * Width)) << 1) + 1];
  157.         }
  158.  
  159.         public static void SetBlock(int X, int Y, ushort TextBlock)
  160.         {
  161.             pNewScreen[(X + (Y * Width))] = TextBlock;
  162.         }
  163.  
  164.         public static ushort GetBlock(int X, int Y)
  165.         {
  166.             return pNewScreen[(X + (Y * Width))];
  167.         }
  168.  
  169.         public static void Clear(byte ConColor = 0x0F, byte ConChar = 32)
  170.         {
  171.             ushort CharBlock = (ushort)((ConColor << 8) | ConChar);
  172.             for (int x = 0; x < (Height * Width); x += 1)
  173.             {
  174.                 pNewScreen[x] = CharBlock;
  175.             }
  176.         }
  177.  
  178.         public static void Print(int x = 0xFF, int y = 0xFF, string TextInpt = "", byte ConColor = 0xFF)
  179.         {
  180.             if (x == 0xFF) { x = CursorX; }
  181.             if (y == 0xFF) { y = CursorY; }
  182.             if (ConColor == 0xFF) { ConColor = PrintColor; }
  183.  
  184.             int ScreenPos = ((x + (y * Width)) << 1);
  185.             byte* pScreenPtr = (byte*)pNewScreen;
  186.  
  187.             for (int TextPos = 0; TextPos < TextInpt.Length; TextPos += 1)
  188.             {
  189.                 if (TextInpt[TextPos] == 10)
  190.                 {
  191.                     ScreenPos += Width << 1;
  192.                 }
  193.                 else if (TextInpt[TextPos] != 13)
  194.                 {
  195.                     pScreenPtr[ScreenPos] = (byte)TextInpt[TextPos];
  196.                     pScreenPtr[ScreenPos + 1] = ConColor;
  197.                     ScreenPos += 2;
  198.                 }
  199.             }
  200.         }
  201.  
  202.  
  203.  
  204.         public static void DrawRect(short X, short Y, short Wid, short Hei, byte ConColor, byte ConChar)
  205.         {
  206.             ushort CharBlock = (ushort)(ConChar | (ConColor << 8));
  207.  
  208.             int RectPart = (X + (Y * Width));
  209.             int RectY = 0;
  210.             while (RectY != Hei)
  211.             {
  212.                 for (int RectX = 0; RectX < Wid; RectX++)
  213.                 {
  214.                     pNewScreen[RectPart + RectX] = CharBlock;
  215.                 }
  216.                 RectPart += Width;
  217.                 RectY += 1;
  218.             }
  219.         }
  220.  
  221.         public static void Destroy()
  222.         {
  223.             Marshal.FreeHGlobal((IntPtr)pNewScreen);
  224.             Width = 0; Height = 0;
  225.         }
  226.  
  227.         //.PointerTextGraphicsFileFormatSystemExtended
  228.         // Graphics functions
  229.         public static TxtGFX CreateImage(short Wid, short Hei)
  230.         {
  231.             TxtGFX NewImg;
  232.             NewImg.Size = (short)(Wid * Hei * sizeof(ushort));
  233.             NewImg.Width = Wid;
  234.             NewImg.Height = Hei;
  235.             NewImg.Image = (ushort*)Marshal.AllocHGlobal(NewImg.Size);
  236.             return NewImg;
  237.         }
  238.  
  239.         public static void DestroyImage(TxtGFX OldImage)
  240.         {
  241.             if (OldImage.Image != null)
  242.             {
  243.                 Marshal.FreeHGlobal((IntPtr)pNewScreen);
  244.             }
  245.             OldImage.Width = 0;
  246.             OldImage.Height = 0;
  247.             OldImage.Image = null;
  248.         }
  249.  
  250.         public static void GetGFX(short X, short Y, ref TxtGFX ImageBuff)
  251.         {
  252.             short ImagePos = 0;
  253.             int ScreenStart = (X + (Y * Width));
  254.             int CurPos = 0; int RowNum = 0;
  255.             while (RowNum != ImageBuff.Height)
  256.             {
  257.                 for (int LineOffset = 0; LineOffset < ImageBuff.Width; LineOffset++)
  258.                 {
  259.                     ImageBuff.Image[ImagePos] = pNewScreen[ScreenStart + CurPos + LineOffset];
  260.                     ImagePos++;
  261.                 }
  262.                 CurPos += Width; RowNum += 1;
  263.             }
  264.         }
  265.  
  266.         public static void PutGFX(short X, short Y, ref TxtGFX ImageBuff)
  267.         {
  268.             short ImagePos = 0;
  269.             int ScreenStart = (X + (Y * Width));
  270.             int CurPos = 0; int RowNum = 0;
  271.             while (RowNum != ImageBuff.Height)
  272.             {
  273.                 for (int LineOffset = 0; LineOffset < ImageBuff.Width; LineOffset++)
  274.                 {
  275.                     pNewScreen[ScreenStart + CurPos + LineOffset] = ImageBuff.Image[ImagePos];
  276.                     ImagePos++;
  277.                 }
  278.                 CurPos += Width; RowNum += 1;
  279.             }
  280.         }
  281.  
  282.         public static void SaveGFX(TxtGFX Image, string Filename)
  283.         {
  284.  
  285.             Stream stream = new FileStream(Filename, FileMode.Create, FileAccess.Write);
  286.             BinaryWriter Output = new BinaryWriter(stream);
  287.             // Write header to file
  288.             Output.Write(FILE_MAGIC);
  289.             Output.Write(Image.Width);
  290.             Output.Write(Image.Height);
  291.             // Write actual image
  292.             Output.Write(PtrToByteArray((byte*)Image.Image, Image.Size));
  293.             Output.Close();
  294.             stream.Close();
  295.         }
  296.  
  297.         public static TxtGFX LoadGFX(string Filename)
  298.         {
  299.             byte[] magic = new byte[4];
  300.             TxtGFX Image;
  301.             Image.Size = 0; Image.Width = 0;
  302.             Image.Height = 0; Image.Image = null;
  303.  
  304.             Stream stream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
  305.             BinaryReader Input = new BinaryReader(stream);
  306.            
  307.             // Make sure this is a valid file to load
  308.             magic = Input.ReadBytes(4);
  309.             if (BitConverter.ToInt32(magic, 0) != BitConverter.ToInt32(FILE_MAGIC, 0))
  310.             {
  311.                 MessageBox(0, "Error: Not a valid console graphics file!", null, MB_ICONERROR);
  312.                 return Image;
  313.             }
  314.             // Get width/height of the graphic
  315.             Image.Width = Input.ReadInt16();
  316.             Image.Height = Input.ReadInt16();
  317.             // Calculate size and generate new buffer
  318.             Image.Size = (short)(Image.Width * Image.Height * sizeof(ushort));
  319.             // Create temp array to read image data into
  320.             byte[] TempArray = new byte[Image.Size];
  321.             Input.Read(TempArray, 0, Image.Size);
  322.             // Convert to an unmanaged buffer to save into struct
  323.             Image.Image = (ushort*)ByteArrayToPtr(TempArray, Image.Size);
  324.  
  325.             Input.Close();
  326.             stream.Close();
  327.             return Image;
  328.         }
  329.  
  330.         private static byte[] PtrToByteArray(byte* DataPointer, short Size)
  331.         {
  332.             byte[] OutArray = new byte[Size];
  333.             for (int X = 0; X < Size; X++)
  334.             {
  335.                 OutArray[X] = DataPointer[X];
  336.             }
  337.             return OutArray;
  338.         }
  339.  
  340.         private static byte* ByteArrayToPtr(byte[] InArray, short Size)
  341.         {
  342.             byte* ImgPtr = (byte*)Marshal.AllocHGlobal(Size);
  343.             for (int X = 0; X < Size; X++)
  344.             {
  345.                 ImgPtr[X] = InArray[X];
  346.             }
  347.             return ImgPtr;
  348.         }
  349.  
  350.         public static bool Multikey(Keys KeyCode)
  351.         {
  352.             if (GetForegroundWindow() != FindWindow(null, Console.Title))
  353.             {
  354.                 return false;
  355.             }
  356.  
  357.             if ((GetAsyncKeyState(KeyCode) & 0x8000) != 0)
  358.             {
  359.                 return true;
  360.             }
  361.             return false;
  362.         }
  363.     }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement