Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Windows.Forms;
- namespace ConGFXLib
- {
- unsafe public struct TxtGFX
- {
- public short Size;
- public short Width;
- public short Height;
- public ushort* Image;
- }
- unsafe public static partial class pCon
- {
- // C# console wouldn't display extended ASCII correctly so we can
- // use this table to display the characters instead of converting
- // our buffers from ASCII to unicode.
- public static readonly ushort[] UTF16Lookup = {
- 0x0000,0x263A,0x263B,0x2665,0x2666,0x2663,0x2660,0x2022,
- 0x25D8,0x25CB,0x25D9,0x2642,0x2640,0x266A,0x266B,0x263C,
- 0x25BA,0x25C4,0x2195,0x203C,0x00B6,0x00A7,0x25AC,0x21A8,
- 0x2191,0x2193,0x2192,0x2190,0x221F,0x2194,0x25B2,0x25BC,
- 0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027,
- 0x0028,0x0029,0x002A,0x002B,0x002C,0x002D,0x002E,0x002F,
- 0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,
- 0x0038,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x003F,
- 0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,
- 0x0048,0x0049,0x004A,0x004B,0x004C,0x004D,0x004E,0x004F,
- 0x0050,0x0051,0x0052,0x0053,0x0054,0x0055,0x0056,0x0057,
- 0x0058,0x0059,0x005A,0x005B,0x005C,0x005D,0x005E,0x005F,
- 0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,
- 0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F,
- 0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,
- 0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x2302,
- 0x00C7,0x00FC,0x00E9,0x00E2,0x00E4,0x00E0,0x00E5,0x00E7,
- 0x00EA,0x00EB,0x00E8,0x00EF,0x00EE,0x00EC,0x00C4,0x00C5,
- 0x00C9,0x00E6,0x00C6,0x00F4,0x00F6,0x00F2,0x00FB,0x00F9,
- 0x00FF,0x00D6,0x00DC,0x00F8,0x00A3,0x00D8,0x00D7,0x0192,
- 0x00E1,0x00ED,0x00F3,0x00FA,0x00F1,0x00D1,0x00AA,0x00BA,
- 0x00BF,0x00AE,0x00AC,0x00BD,0x00BC,0x00A1,0x00AB,0x00BB,
- 0x2591,0x2592,0x2593,0x2502,0x2524,0x00C1,0x00C2,0x00C0,
- 0x00A9,0x2563,0x2551,0x2557,0x255D,0x00A2,0x00A5,0x2510,
- 0x2514,0x2534,0x252C,0x251C,0x2500,0x253C,0x00E3,0x00C3,
- 0x255A,0x2554,0x2569,0x2566,0x2560,0x2550,0x256C,0x00A4,
- 0x00F0,0x00D0,0x00CA,0x00CB,0x00C8,0x0131,0x00CD,0x00CE,
- 0x00CF,0x2518,0x250C,0x2588,0x2584,0x00A6,0x00CC,0x2580,
- 0x00D3,0x00DF,0x00D4,0x00D2,0x00F5,0x00D5,0x00B5,0x00FE,
- 0x00DE,0x00DA,0x00DB,0x00D9,0x00FD,0x00DD,0x00AF,0x00B4,
- 0x00AD,0x00B1,0x2017,0x00BE,0x00B6,0x00A7,0x00F7,0x00B8,
- 0x00B0,0x00A8,0x00B7,0x00B9,0x00B3,0x00B2,0x25A0,0x00A0
- };
- static byte[] FILE_MAGIC = { //0x54474658
- (byte)'T', (byte)'G', (byte)'F', (byte)'X'
- };
- // Constants for the console color codes
- public static class Colors
- {
- public const byte
- BLACK = 0x00, BLUE = 0x01, GREEN = 0x02, CYAN = 0x03,
- RED = 0x04, MAGENTA = 0x05, BROWN = 0x06, LIGHT_GRAY = 0x07,
- DARK_GRAY = 0x08, BRIGHT_BLUE = 0x09, BRIGHT_GREEN = 0x0A,
- BRIGHT_CYAN = 0x0B, BRIGHT_RED = 0x0C, BRIGHT_MAGENTA = 0x0D,
- BRIGHT_YELLOW = 0x0E, WHITE = 0x0F;
- }
- public static int Width, Height;
- public static int CursorX, CursorY;
- public static byte PrintColor = 0x0F;
- static void* hOldScreen;
- static ushort* pNewScreen;
- public static void* Create(int iWidth = 80, int iHeight = 25)
- {
- if (iWidth <= 8 || iHeight <= 4)
- {
- MessageBox(0, "ConsoleCreate: Bad console size selected", null, MB_ICONERROR);
- return (uint*)0;
- }
- Console.CursorVisible = false;
- CursorX = 0; CursorY = 0;
- Console.SetWindowSize(iWidth, iHeight);
- Width = iWidth; Height = iHeight;
- hOldScreen = GetStdHandle(STD_OUTPUT_HANDLE);
- pNewScreen = (ushort*)Marshal.AllocHGlobal(iWidth * iHeight * 2);
- return pNewScreen;
- }
- public static void Update()
- {
- COORD ConSize, BuffStart;
- BuffStart.X = 0; BuffStart.Y = 0;
- ConSize.X = (short)Width; ConSize.Y = (short)Height;
- SMALL_RECT WriteRegion;
- WriteRegion.Left = 0; WriteRegion.Top = 0;
- WriteRegion.Right = (short)(Width - 1); WriteRegion.Bottom = (short)(Height - 1);
- //Convert from DOS format to Windows console format
- CHAR_INFO[] tNative = new CHAR_INFO[Width * Height];
- for (int iN = 0; iN < (Width * Height); iN++)
- {
- //tNative[iN].AsciiChar = (ushort)(pNewScreen[iN] & 0xFF);
- tNative[iN].UnicodeChar = UTF16Lookup[(byte)(pNewScreen[iN] & 0xFF)];
- tNative[iN].Attributes = (ushort)(pNewScreen[iN] >> 8);
- }
- var result = WriteConsoleOutput(hOldScreen, tNative,
- ConSize, BuffStart, ref WriteRegion);
- if (result == false)
- {
- MessageBox(0, "ConsoleUpdate: Failed to WriteConsoleOutput", null, MB_ICONERROR);
- }
- }
- public static byte ConColor(byte Fore = 0xFF, byte Back = 0xFF)
- {
- if (Back == 0xFF) { Back = (byte)(PrintColor >> 4); }
- if (Fore == 0xFF) { Fore = (byte)(PrintColor & 0xFF); }
- return (byte)(Fore + (Back << 4));
- }
- public static void SetChar(int X, int Y, byte ConChar)
- {
- byte* TmpPtr = (byte*)pNewScreen;
- TmpPtr[(X + (Y * Width)) << 1] = ConChar;
- }
- public static byte GetChar(int X, int Y)
- {
- byte* TmpPtr = (byte*)pNewScreen;
- return TmpPtr[(X + (Y * Width)) << 1];
- }
- public static void SetAttribute(int X, int Y, byte Attribute)
- {
- byte* TmpPtr = (byte*)pNewScreen;
- TmpPtr[((X + (Y * Width)) << 1) + 1] = Attribute;
- }
- public static byte GetAttribute(int X, int Y)
- {
- byte* TmpPtr = (byte*)pNewScreen;
- return TmpPtr[((X + (Y * Width)) << 1) + 1];
- }
- public static void SetBlock(int X, int Y, ushort TextBlock)
- {
- pNewScreen[(X + (Y * Width))] = TextBlock;
- }
- public static ushort GetBlock(int X, int Y)
- {
- return pNewScreen[(X + (Y * Width))];
- }
- public static void Clear(byte ConColor = 0x0F, byte ConChar = 32)
- {
- ushort CharBlock = (ushort)((ConColor << 8) | ConChar);
- for (int x = 0; x < (Height * Width); x += 1)
- {
- pNewScreen[x] = CharBlock;
- }
- }
- public static void Print(int x = 0xFF, int y = 0xFF, string TextInpt = "", byte ConColor = 0xFF)
- {
- if (x == 0xFF) { x = CursorX; }
- if (y == 0xFF) { y = CursorY; }
- if (ConColor == 0xFF) { ConColor = PrintColor; }
- int ScreenPos = ((x + (y * Width)) << 1);
- byte* pScreenPtr = (byte*)pNewScreen;
- for (int TextPos = 0; TextPos < TextInpt.Length; TextPos += 1)
- {
- if (TextInpt[TextPos] == 10)
- {
- ScreenPos += Width << 1;
- }
- else if (TextInpt[TextPos] != 13)
- {
- pScreenPtr[ScreenPos] = (byte)TextInpt[TextPos];
- pScreenPtr[ScreenPos + 1] = ConColor;
- ScreenPos += 2;
- }
- }
- }
- public static void DrawRect(short X, short Y, short Wid, short Hei, byte ConColor, byte ConChar)
- {
- ushort CharBlock = (ushort)(ConChar | (ConColor << 8));
- int RectPart = (X + (Y * Width));
- int RectY = 0;
- while (RectY != Hei)
- {
- for (int RectX = 0; RectX < Wid; RectX++)
- {
- pNewScreen[RectPart + RectX] = CharBlock;
- }
- RectPart += Width;
- RectY += 1;
- }
- }
- public static void Destroy()
- {
- Marshal.FreeHGlobal((IntPtr)pNewScreen);
- Width = 0; Height = 0;
- }
- //.PointerTextGraphicsFileFormatSystemExtended
- // Graphics functions
- public static TxtGFX CreateImage(short Wid, short Hei)
- {
- TxtGFX NewImg;
- NewImg.Size = (short)(Wid * Hei * sizeof(ushort));
- NewImg.Width = Wid;
- NewImg.Height = Hei;
- NewImg.Image = (ushort*)Marshal.AllocHGlobal(NewImg.Size);
- return NewImg;
- }
- public static void DestroyImage(TxtGFX OldImage)
- {
- if (OldImage.Image != null)
- {
- Marshal.FreeHGlobal((IntPtr)pNewScreen);
- }
- OldImage.Width = 0;
- OldImage.Height = 0;
- OldImage.Image = null;
- }
- public static void GetGFX(short X, short Y, ref TxtGFX ImageBuff)
- {
- short ImagePos = 0;
- int ScreenStart = (X + (Y * Width));
- int CurPos = 0; int RowNum = 0;
- while (RowNum != ImageBuff.Height)
- {
- for (int LineOffset = 0; LineOffset < ImageBuff.Width; LineOffset++)
- {
- ImageBuff.Image[ImagePos] = pNewScreen[ScreenStart + CurPos + LineOffset];
- ImagePos++;
- }
- CurPos += Width; RowNum += 1;
- }
- }
- public static void PutGFX(short X, short Y, ref TxtGFX ImageBuff)
- {
- short ImagePos = 0;
- int ScreenStart = (X + (Y * Width));
- int CurPos = 0; int RowNum = 0;
- while (RowNum != ImageBuff.Height)
- {
- for (int LineOffset = 0; LineOffset < ImageBuff.Width; LineOffset++)
- {
- pNewScreen[ScreenStart + CurPos + LineOffset] = ImageBuff.Image[ImagePos];
- ImagePos++;
- }
- CurPos += Width; RowNum += 1;
- }
- }
- public static void SaveGFX(TxtGFX Image, string Filename)
- {
- Stream stream = new FileStream(Filename, FileMode.Create, FileAccess.Write);
- BinaryWriter Output = new BinaryWriter(stream);
- // Write header to file
- Output.Write(FILE_MAGIC);
- Output.Write(Image.Width);
- Output.Write(Image.Height);
- // Write actual image
- Output.Write(PtrToByteArray((byte*)Image.Image, Image.Size));
- Output.Close();
- stream.Close();
- }
- public static TxtGFX LoadGFX(string Filename)
- {
- byte[] magic = new byte[4];
- TxtGFX Image;
- Image.Size = 0; Image.Width = 0;
- Image.Height = 0; Image.Image = null;
- Stream stream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
- BinaryReader Input = new BinaryReader(stream);
- // Make sure this is a valid file to load
- magic = Input.ReadBytes(4);
- if (BitConverter.ToInt32(magic, 0) != BitConverter.ToInt32(FILE_MAGIC, 0))
- {
- MessageBox(0, "Error: Not a valid console graphics file!", null, MB_ICONERROR);
- return Image;
- }
- // Get width/height of the graphic
- Image.Width = Input.ReadInt16();
- Image.Height = Input.ReadInt16();
- // Calculate size and generate new buffer
- Image.Size = (short)(Image.Width * Image.Height * sizeof(ushort));
- // Create temp array to read image data into
- byte[] TempArray = new byte[Image.Size];
- Input.Read(TempArray, 0, Image.Size);
- // Convert to an unmanaged buffer to save into struct
- Image.Image = (ushort*)ByteArrayToPtr(TempArray, Image.Size);
- Input.Close();
- stream.Close();
- return Image;
- }
- private static byte[] PtrToByteArray(byte* DataPointer, short Size)
- {
- byte[] OutArray = new byte[Size];
- for (int X = 0; X < Size; X++)
- {
- OutArray[X] = DataPointer[X];
- }
- return OutArray;
- }
- private static byte* ByteArrayToPtr(byte[] InArray, short Size)
- {
- byte* ImgPtr = (byte*)Marshal.AllocHGlobal(Size);
- for (int X = 0; X < Size; X++)
- {
- ImgPtr[X] = InArray[X];
- }
- return ImgPtr;
- }
- public static bool Multikey(Keys KeyCode)
- {
- if (GetForegroundWindow() != FindWindow(null, Console.Title))
- {
- return false;
- }
- if ((GetAsyncKeyState(KeyCode) & 0x8000) != 0)
- {
- return true;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement