Пример #1
0
 public void Decode()
 {
     if (ByteBuffer != null)
     {
         Polymono.DebugF("------------------------------START DECODE--------------------------------");
         Polymono.DebugF($"Byte buffer: {PrintByteArray(ByteBuffer)}");
         Polymono.DebugF("Converting bytes to objects...");
         // Do byte->object conversions.
         Type      = (PacketType)BitConverter.ToInt32(ByteBuffer, 0);
         TargetID  = BitConverter.ToInt32(ByteBuffer, PacketHandler.TypeSize);
         Terminate = Convert.ToBoolean(
             ByteBuffer[PacketHandler.TypeSize + PacketHandler.TargetIDSize]);
         DataBuffer = Encoding.UTF8.GetString(ByteBuffer,
                                              PacketHandler.TypeSize + PacketHandler.TargetIDSize + PacketHandler.TerminatorSize,
                                              PacketHandler.DataSize);
         DataBuffer = DataBuffer.Replace("\0", "");
         Polymono.DebugF("Conversion to objects finished.");
         Polymono.DebugF($"Type: {Type}");
         Polymono.DebugF($"Target ID: {TargetID}");
         Polymono.DebugF($"Terminate: {Terminate}");
         Polymono.DebugF($"DataBuffer: {DataBuffer}");
         Polymono.DebugF("-------------------------------END DECODE---------------------------------");
     }
     else
     {
         Polymono.Print(ConsoleLevel.Warning, "Attempting to encode a packet with no byte buffer.");
     }
 }
Пример #2
0
        private bool CreateProgram()
        {
            // Sets variables for shader compilation.
            int    success = 0;
            string infoLog = "";

            // Creates program to use for shaders, returns reference.
            ProgramID = GL.CreateProgram();
            // Attaches the shaders to the program.
            GL.AttachShader(ProgramID, VertexShader);
            GL.AttachShader(ProgramID, FragmentShader);
            // Link the program to GPU; further rendering uses this program/shaders.
            GL.LinkProgram(ProgramID);
            // Return the program parameter on linking status.
            GL.GetProgram(ProgramID, GetProgramParameterName.LinkStatus, out success);
            if (success != 1)
            {
                // Get the program ifnormation log on linking failure.
                GL.GetProgramInfoLog(ProgramID, out infoLog);
                Polymono.Warning($"Program [{ProgramName}:{ProgramID}] failed to link: {infoLog}");
                GL.DeleteShader(VertexShader);
                GL.DeleteShader(FragmentShader);
                return(false);
            }
            GL.DeleteShader(VertexShader);
            GL.DeleteShader(FragmentShader);
            return(true);
        }
Пример #3
0
        public static Packet[] Create(PacketType type, int targetID, string data)
        {
            Polymono.Debug($"Data length: {data.Length}");
            int PacketsToCreate = (data.Length / DataSize) + 1;

            Polymono.Debug($"Number of packets: {PacketsToCreate}");
            Packet[] packets = new Packet[PacketsToCreate];
            for (int i = 0; i < PacketsToCreate; i++)
            {
                Polymono.Debug($"Interation: {i}");
                int  size;
                bool terminate;
                if (data.Length - (i * DataSize) < DataSize)
                {
                    // Last Packet to populate.
                    size      = data.Length - (i * DataSize);
                    terminate = true;
                }
                else
                {
                    // There are more packets to populate.
                    size      = DataSize;
                    terminate = false;
                }
                packets[i] = new Packet(type, targetID, terminate, data.Substring(i * DataSize, size));
                packets[i].Encode();
            }
            return(packets);
        }
Пример #4
0
        private void Serialise()
        {
            byte[] typeBytes      = BitConverter.GetBytes((int)Type);
            byte[] targetIDBytes  = BitConverter.GetBytes(TargetID);
            byte   terminateBytes = Convert.ToByte(Terminate);

            for (int i = 0; i < typeBytes.Length; i++)
            {
                Bytes[i] = typeBytes[i];
            }
            for (int i = 0; i < targetIDBytes.Length; i++)
            {
                Bytes[i + 4] = targetIDBytes[i];
            }
            Bytes[8] = terminateBytes;
            for (int i = 0; i < Data.Length; i++)
            {
                Bytes[i + 9] = Data[i];
            }
            Polymono.Debug($"Packet serialised: {Environment.NewLine}    " +
                           $"[Type: {Type}->{PrintByteArray(typeBytes)}] {Environment.NewLine}    " +
                           $"[Target ID: {TargetID}->{PrintByteArray(targetIDBytes)}] {Environment.NewLine}    " +
                           $"[Terminate: {Terminate}->{terminateBytes}] {Environment.NewLine}    " +
                           $"[Data: {PrintByteArray(Data)}]");
        }
Пример #5
0
        public async void Click(Vector2 vector)
        {
            vector.Y = WindowHeight - vector.Y;
            Vector3 position   = Models[Selector].Position;
            bool    isHovering = PointInRectangle(
                new Vector2(position.X, position.Y),
                new Vector2(position.X + Width, position.Y),
                new Vector2(position.X + Width, position.Y - Height),
                new Vector2(position.X, position.Y - Height), vector);

            if (isHovering && !Models[Selector].IsHidden && State != ControlState.Clicked)
            {
                Polymono.Debug($"Button clicked: {Text}[{ID}]");
                State    = ControlState.Clicked;
                Selector = "Clicked";
                try
                {
                    await ExecDelegate();
                }
                catch (Exception e)
                {
                    Polymono.Error(e.Message);
                    Polymono.ErrorF(e.StackTrace);
                }
                State    = ControlState.Normal;
                Selector = "Default";
            }
        }
Пример #6
0
        public void Click(Vector2 vector)
        {
            vector.Y = WindowHeight - vector.Y;
            Vector3 position   = Models[Selector].Position;
            bool    isHovering = PointInRectangle(
                new Vector2(position.X, position.Y),
                new Vector2(position.X + Width, position.Y),
                new Vector2(position.X + Width, position.Y - Height),
                new Vector2(position.X, position.Y - Height), vector);

            if (isHovering && !Models[Selector].IsHidden)
            {
                Polymono.Debug($"Checkbox clicked: [{ID}]");
                if (State == ControlState.Normal)
                {
                    State    = ControlState.Clicked;
                    Selector = "Clicked";
                }
                else
                {
                    State    = ControlState.Normal;
                    Selector = "Default";
                }
            }
        }
Пример #7
0
 public Client(GameClient gameClient, bool v6 = true)
 {
     GameClient     = gameClient;
     V6             = v6;
     NetworkHandler = new SocketHandler(v6);
     Packets        = new Queue <Packet>();
     Polymono.Debug("Client initialised.");
 }
Пример #8
0
 public Server(GameClient gameClient, string name, bool v6 = true)
 {
     GameClient = gameClient;
     V6         = v6;
     //Clients = new ISocket[Polymono.MaxPlayers];
     //Clients[ID] = new SocketHandler(v6);
     GameClient.Board.AddPlayer(ID, name);
     GetClients()[ID].SetNetworkHandle(new SocketHandler(v6));
     Packets = new Queue <Packet>();
     Polymono.Debug("Server initialised. [IPv6: " + v6 + "]");
 }
Пример #9
0
        public void AddPlayer(int id, string name)
        {
            Polymono.Debug($"Board::AddPlayer(ID: {id}, Name: {name})");
            Player player = new Player(PlayerProgram, this)
            {
                PlayerName = name
            };

            Players[id] = player;
            SetPlayerPosition(id, 0);
        }
Пример #10
0
 private void Deserialise()
 {
     Type      = (PacketType)BitConverter.ToInt32(Bytes, PacketHandler.TypeOffset);
     TargetID  = BitConverter.ToInt32(Bytes, PacketHandler.TargetIDOffset);
     Terminate = BitConverter.ToBoolean(Bytes, PacketHandler.TerminateOffset);
     Buffer.BlockCopy(Bytes, PacketHandler.DataOffset, Data, 0, PacketHandler.DataSize);
     Polymono.Debug($"Packet deserialised: {Environment.NewLine}    " +
                    $"[Type: {Type}] {Environment.NewLine}    " +
                    $"[Target ID: {TargetID}] {Environment.NewLine}    " +
                    $"[Terminate: {Terminate}] {Environment.NewLine}    " +
                    $"[Data Bytes: {PrintByteArray(Data)}]");
 }
Пример #11
0
        public int CreateTexture(string path, int quality = 1, bool repeat = false, bool flip_y = false)
        {
            Bitmap bitmap;

            try
            {
                bitmap = new Bitmap(path);
            }
            catch (FileNotFoundException e)
            {
                Polymono.Debug($"Cannot find {path}: {e}");
                return(-1);
            }
            if (flip_y)
            {
                bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            }
            int texture = GL.GenTexture();

            GL.BindTexture(TextureTarget.Texture2D, texture);
            switch (quality)
            {
            case 0:
            default:
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
                break;

            case 1:
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Nearest);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Nearest);
                break;
            }
            if (repeat)
            {
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);
            }
            else
            {
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.ClampToEdge);
                GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.ClampToEdge);
            }
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, bitmap.Width, bitmap.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
            System.Drawing.Imaging.BitmapData bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                                            System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, bitmap.Width, bitmap.Height, PixelFormat.Bgra, PixelType.UnsignedByte, bitmap_data.Scan0);
            bitmap.UnlockBits(bitmap_data);
            bitmap.Dispose();
            GL.BindTexture(TextureTarget.Texture2D, 0);
            return(texture);
        }
Пример #12
0
        public void UniformMatrix4(string uniformName, ref Matrix4 matrix)
        {
            int location = GetUniform(uniformName);

            if (location != -1)
            {
                GL.UniformMatrix4(location, false, ref matrix);
            }
            else
            {
                Polymono.Warning($"Program [{ProgramID}]: UniformMatrix4 could not be set. [{uniformName}] does not exist.");
            }
        }
Пример #13
0
        public void MoveSpaces(int spaces, int playerID)
        {
            Polymono.Debug($"Board::MoveSpaces(spaces: {spaces}, sender: {playerID})");
            // TODO: Gradual player movement.
            Players[playerID].NextLocation = NextPropertyID(Players[playerID].CurrentLocation);
            int step = Players[playerID].CurrentLocation;

            for (int i = 1; i < spaces + 1; i++)
            {
                step = NextPropertyID(step);
            }
            Players[playerID].FinalLocation = step;
        }
Пример #14
0
        public void Uniform4(string uniformName, ref Vector4 vector)
        {
            int location = GetUniform(uniformName);

            if (location != -1)
            {
                GL.Uniform4(location, ref vector);
            }
            else
            {
                Polymono.Warning($"Program [{ProgramID}]: Uniform4 could not be set. [{uniformName}] does not exist.");
            }
        }
Пример #15
0
        public void Uniform1(string uniformName, float value)
        {
            int location = GetUniform(uniformName);

            if (location != -1)
            {
                GL.Uniform1(location, value);
            }
            else
            {
                Polymono.Warning($"Program [{ProgramID}]: Uniform1 could not be set. [{uniformName}] does not exist.");
            }
        }
Пример #16
0
        public bool UpdatePositions(int playerID, float updateFrequency)
        {
            updateFrequency /= MovementSpeed;
            Player player = Players[playerID];

            if (player.CurrentLocation == player.FinalLocation)
            {
                return(false);
            }
            // Points of the line.
            Vector3 BeginPoint = player.OriginPosition;
            Vector3 EndPoint   = Properties[player.NextLocation].BoardLocationOffset + GetOffsetByPlayers(player.NextLocation);
            // The difference vector.
            Vector3 Difference = EndPoint - BeginPoint;
            Vector3 UpdateDiff = Difference / updateFrequency;
            // Updated player position.
            Vector3 pastPosition = player.Position;
            Vector3 NewPosition  = player.Position + UpdateDiff;

            player.Position = NewPosition;
            // Magnitudes
            float completeMagnitude      = Difference.Length;
            float firstPartialMagnitude  = (NewPosition - BeginPoint).Length;
            float secondPartialMagnitude = UpdateDiff.Length;
            float adjustedMagnitude      = firstPartialMagnitude + secondPartialMagnitude;

            // Check magnitudes
            if (completeMagnitude <= adjustedMagnitude)
            {
                Polymono.Debug($"Board::UpdatePosition(ID, UpFreq): End of movement segment.");
                // If magnitude is further than is supposed to be, or equal, force position -> Finish.
                player.Position        = EndPoint;
                player.OriginPosition  = EndPoint;
                player.CurrentLocation = player.NextLocation;
                if (player.CurrentLocation != player.FinalLocation)
                {
                    player.NextLocation = NextPropertyID(player.CurrentLocation);
                    return(false);
                }
                else if (player.CurrentLocation == player.FinalLocation)
                {
                    Polymono.Debug($"Board::UpdatePosition(ID, UpFreq): Movement finished.");
                    GameClient.State = GameState.PlayerOptions;
                    // Moving is done, fully.
                    return(true);
                }
            }
            return(false);
        }
Пример #17
0
 public void EndCurrentTurn(int playerID)
 {
     Polymono.Debug($"Board::EndCurrentTurn(ID: {playerID})");
     // If in lobby roll phase.
     if (GameClient.State == GameState.LobbyRoll)
     {
         int nextPlayerTurn = CurrentPlayerTurn + 1;
         // If end of lobby roll phase
         if (nextPlayerTurn >= GetPlayers().Count)
         {
             // Carry on
             // Assign an order list and start over.
             CompleteOrder();
             CurrentPlayerTurn = PlayerOrder[0];
             GameClient.State  = GameState.PlayerOptions;
         }
         else
         {
             CurrentPlayerTurn += 1;
         }
     }
     else if (GameClient.State == GameState.PlayerOptions)
     {
         // End turn pressed.
         for (int i = 0; i < PlayerOrder.Length; i++)
         {
             // Get current turn's position.
             if (PlayerOrder[i] == CurrentPlayerTurn)
             {
                 // Current playerOrder, increment order.
                 if (i + 1 >= PlayerOrder.Length)
                 {
                     CurrentPlayerTurn = PlayerOrder[0];
                 }
                 else
                 {
                     CurrentPlayerTurn = PlayerOrder[i + 1];
                 }
                 break;
             }
         }
     }
 }
Пример #18
0
 public void LoadFromFile(string filename, Color4 colour)
 {
     try
     {
         using (StreamReader reader = new StreamReader(new FileStream(filename,
                                                                      FileMode.Open, FileAccess.Read)))
         {
             LoadFromString(reader.ReadToEnd(), colour);
         }
     }
     catch (FileNotFoundException e)
     {
         Polymono.Error($"File not found: {filename + Environment.NewLine + e.ToString()}");
     }
     catch (Exception e)
     {
         Console.WriteLine($"Error loading file: {filename + Environment.NewLine + e.ToString()}");
     }
 }
Пример #19
0
        private bool SetShader(string location, ShaderType type)
        {
            // Sets variables for shader compilation.
            string infoLog = "";

            if (type == ShaderType.VertexShader)
            {
                // Creates shader then assigns reference.
                VertexShader = GL.CreateShader(type);
                // Specifies the shader data to OpenGL.
                GL.ShaderSource(VertexShader, File.ReadAllText(@"Resources\Graphics\" + location));
                // Compiles the shader from the source data.
                GL.CompileShader(VertexShader);
                // Return the shader parameter on compilation status.
                GL.GetShader(VertexShader, ShaderParameter.CompileStatus, out int success);
                if (success != 1)
                {
                    // Get the shader information log on compilation failure.
                    infoLog = GL.GetShaderInfoLog(VertexShader);
                    Polymono.Warning($"Shader [{location}:{VertexShader}] failed to compile: {infoLog}");
                    return(false);
                }
            }
            else if (type == ShaderType.FragmentShader)
            {
                // Creates shader then assigns reference.
                FragmentShader = GL.CreateShader(type);
                // Specifies the shader data to OpenGL.
                GL.ShaderSource(FragmentShader, File.ReadAllText(@"Resources\Graphics\" + location));
                // Compiles the shader from the source data.
                GL.CompileShader(FragmentShader);
                // Return the shader parameter on compilation status.
                GL.GetShader(FragmentShader, ShaderParameter.CompileStatus, out int success);
                if (success != 1)
                {
                    // Get the shader information log on compilation failure.
                    infoLog = GL.GetShaderInfoLog(FragmentShader);
                    Polymono.Warning($"Shader [{location}:{FragmentShader}] failed to compile: {infoLog}");
                    return(false);
                }
            }
            return(true);
        }
Пример #20
0
 public void Encode()
 {
     if (DataBuffer != null)
     {
         Polymono.DebugF("------------------------------START ENCODE--------------------------------");
         Polymono.DebugF($"Type: {Type}");
         Polymono.DebugF($"Target ID: {TargetID}");
         Polymono.DebugF($"Terminate: {Terminate}");
         Polymono.DebugF($"Data buffer: {DataBuffer}");
         Polymono.DebugF("Converting data to byte arrays...");
         // Do object->byte conversions.
         byte[] typeBytes       = BitConverter.GetBytes((int)Type);
         byte[] targetIDBytes   = BitConverter.GetBytes(TargetID);
         byte   terminatorBytes = Convert.ToByte(Terminate);
         byte[] dataBytes       = Encoding.UTF8.GetBytes(DataBuffer);
         // Create Byte Buffer allocation.
         ByteBuffer = new byte[PacketHandler.TypeSize + PacketHandler.TargetIDSize +
                               PacketHandler.TerminatorSize + PacketHandler.DataSize];
         // Append the Type onto the Byte Buffer.
         Buffer.BlockCopy(typeBytes, 0, ByteBuffer, 0, PacketHandler.TypeSize);
         // Append the Target ID onto the Byte Buffer.
         Buffer.BlockCopy(targetIDBytes, 0, ByteBuffer, PacketHandler.TypeSize, PacketHandler.TargetIDSize);
         // Append the Terminator onto the Byte Buffer.
         ByteBuffer[PacketHandler.TypeSize + PacketHandler.TargetIDSize] = terminatorBytes;
         // Append the Data Buffer onto the Byte Buffer.
         Buffer.BlockCopy(dataBytes, 0, ByteBuffer,
                          PacketHandler.TypeSize + PacketHandler.TargetIDSize + PacketHandler.TerminatorSize,
                          dataBytes.Length);
         Polymono.DebugF("Conversion to byte arrays finished.");
         Polymono.DebugF($"Type bytes: {PrintByteArray(typeBytes)}");
         Polymono.DebugF($"Target ID bytes: {PrintByteArray(targetIDBytes)}");
         Polymono.DebugF($"Data bytes: {PrintByteArray(dataBytes)}");
         Polymono.DebugF($"Terminator bytes: {terminatorBytes}");
         Polymono.DebugF($"Byte buffer: {PrintByteArray(ByteBuffer)}");
         Polymono.DebugF("-------------------------------END ENCODE---------------------------------");
     }
     else
     {
         Polymono.Print(ConsoleLevel.Warning, "Attempting to encode a packet with no data buffer.");
     }
 }
Пример #21
0
        public static Packet[] Create(PacketType type, int targetID, byte[] data)
        {
            Polymono.Debug($"====== PacketHandler creation ======");
            Polymono.Debug($"Data length: {data.Length}");
            int PacketsToCreate = (data.Length / DataSize) + 1;

            Polymono.Debug($"Number of packets: {PacketsToCreate}");
            Packet[] packets = new Packet[PacketsToCreate];
            for (int i = 0; i < PacketsToCreate; i++)
            {
                int iterationOffset = (i * DataSize);
                int iterationSize   = data.Length - iterationOffset;
                Polymono.Debug($"Interation: {i}");
                Polymono.Debug($"Interation offset: {iterationOffset}");
                Polymono.Debug($"Interation size: {iterationSize}");
                if (iterationSize < DataSize)
                {
                    // Last Packet to populate.
                    byte[] temp = new byte[iterationSize];
                    for (int n = 0; n < iterationSize; n++)
                    {
                        temp[n] = data[n + iterationOffset];
                    }
                    packets[i] = new Packet(type, targetID, true, temp);
                }
                else
                {
                    // There are more packets to populate.
                    // Create temporary set of bytes.
                    byte[] temp = new byte[DataSize];
                    for (int n = 0; n < DataSize; n++)
                    {
                        temp[n] = data[n + iterationOffset];
                    }
                    packets[i] = new Packet(type, targetID, false, temp);
                }
            }
            Polymono.Debug($"====== PacketHandler finished ======");
            return(packets);
        }
Пример #22
0
        public async void Click(Vector2 vector)
        {
            vector.Y = WindowHeight - vector.Y;
            Vector3 position   = Models[Selector].Position;
            bool    isHovering = PointInRectangle(
                new Vector2(position.X, position.Y),
                new Vector2(position.X + Width, position.Y),
                new Vector2(position.X + Width, position.Y - Height),
                new Vector2(position.X, position.Y - Height), vector);

            if (isHovering && !Models[Selector].IsHidden)
            {
                if (State == ControlState.Unfocused || State == ControlState.Normal)
                {
                    Polymono.Debug($"Textbox clicked: {Text}[{ID}]");
                    // Unfocus everything else.
                    foreach (var control in Controls.Values)
                    {
                        if (control is Textbox textbox)
                        {
                            if (textbox.State == ControlState.Focused)
                            {
                                textbox.State    = ControlState.Unfocused;
                                textbox.Selector = "Default";
                            }
                        }
                    }
                    // Focus this object.
                    State    = ControlState.Focused;
                    Selector = "Focused";
                }
                else
                {
                    State    = ControlState.Unfocused;
                    Selector = "Default";
                }
            }
        }
Пример #23
0
 public void FinalisePlayerMovement(int playerID)
 {
     Polymono.Debug($"Board::FinalisePlayerMovement(ID: {playerID})");
     SetPlayerPosition(playerID, Players[playerID].FinalLocation);
 }