Beispiel #1
0
        //prints messages sent by server
        //FIX: PRINT MULTIPLE IMAGES, AND IN VERTICAL ARRAY (PRINTS IMAGES, BUT IS NOT STABLE)
        //TODO: PRINT USERNAME WITH MESSAGE
        public void PrintMessages()
        {
            int x      = 100;
            int y      = 450;
            int xImage = 150;
            int yImage = 500;

            for (int i = messages.Count - 1; i >= 0; i--)
            {
                if (messages[i].messageText != string.Empty)
                {
                    DrawText(messages[i].name + ": " + messages[i].messageText, x, y, 16, Raylib_cs.Color.WHITE);
                    y      -= 15;
                    yImage -= 15;
                }
                if (messages[i].image != string.Empty)
                {
                    //FIX: FIRST IMAGE DOES NOT LOAD, instead do bool to toggle if new picture, then turn of bool
                    if (img.data != previousImg.data)
                    {
                        System.Console.WriteLine("loaded image");
                        imageTexture = Raylib.LoadTextureFromImage(img);
                        messages[i].SetImage(imageTexture);
                        previousImg = img;
                    }
                    Texture2D currentImage = messages[i].GetImage();
                    double    ratio        = 0;
                    if (currentImage.width > currentImage.height && currentImage.width > 500)
                    {
                        ratio = currentImage.width / 500;
                        currentImage.width  = 500;
                        currentImage.height = (int)(currentImage.height / ratio);
                    }
                    else if (currentImage.height > 500)
                    {
                        ratio = currentImage.height / 500;
                        currentImage.height = 500;
                        currentImage.width  = (int)(currentImage.width / ratio);
                    }
                    yImage -= currentImage.height + 15;
                    y      -= currentImage.height + 15;
                    DrawText(messages[i].name + ": ", x, yImage, 16, Raylib_cs.Color.WHITE);
                    Raylib.DrawTexture(currentImage, xImage, yImage, Raylib_cs.Color.WHITE);
                }
            }
        }
Beispiel #2
0
 private void LiveChat()
 {
     try
     {
         /*this while loop uses stream.Read to get data from the server, the stream we are using is the server and by reading it
          * we can get the bytes coming from the stream and then we can use the GetString to convert bytes to a string, which we then
          * print to the user
          */
         NetworkStream stream = client.GetStream();
         byte[]        data2  = new byte[512];
         while (liveChat || !Raylib.WindowShouldClose())
         {
             string responeseData = string.Empty;
             int    bytes         = stream.Read(data2, 0, data2.Length);
             responeseData = System.Text.Encoding.UTF8.GetString(data2, 0, bytes);
             System.Console.WriteLine(responeseData);
             Message newMessage = JsonConvert.DeserializeObject <Message>(responeseData);
             if (newMessage.header == "MESSAGELENGTH")
             {
                 data2 = new byte[newMessage.length + 256];
             }
             if (newMessage.image != string.Empty)
             {
                 byte[] imageData    = Convert.FromBase64String(newMessage.image);
                 string imageDataRaw = Encoding.UTF8.GetString(imageData);
                 using (MemoryStream imageStream = new MemoryStream(imageData))
                 {
                     System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream, true, true);
                     image.Save("file.png", ImageFormat.Png);
                     img = Raylib.LoadImage("file.png");
                     File.Delete("file.png");
                 }
             }
             messages.Add(newMessage);
         }
         //if the server closes down for some reason, it will deliver this message to you and return you to the start.
     }
     catch (Exception)
     {
         stream.Close();
         System.Console.WriteLine("Connection with the server broke!");
         return;
     }
 }