Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: VS2010 C#/.NET and SFML 2.0  (Read 11509 times)

0 Members and 1 Guest are viewing this topic.

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
VS2010 C#/.NET and SFML 2.0
« Reply #15 on: September 04, 2011, 07:18:32 pm »
Thx 4 your help.
I'm quite new to .Net...
With c++ i used sf::packet to convert data types like int etc. into raw data (void * / unsigned char *)
Then I sent it using enet (not the sfml network classes).

What can I use instead of sf::Packet for this conversion?

omeg

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • http://omeg.pl/
VS2010 C#/.NET and SFML 2.0
« Reply #16 on: September 04, 2011, 08:25:30 pm »
You can use BitConverter class for simple types (but manual byte extraction using bitwise operators is faster). For more complex objects, use MemoryStream with BinaryFormatter:

Code: [Select]

using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;

        /// <summary>
        /// Serializes object into a byte array.
        /// </summary>
        /// <param name="o">Object to serialize.</param>
        /// <returns>Serialized bytes.</returns>
        public byte[] SerializeObject(object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, o);
            return ms.ToArray();
        }

        /// <summary>
        /// Deserializes object from a byte array.
        /// </summary>
        /// <param name="data">Serialized data.</param>
        /// <returns>Deserialized object.</returns>
        public object DeserializeObject(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            BinaryFormatter bf = new BinaryFormatter();
            ms.Position = 0;
            return bf.Deserialize(ms);
        }

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
VS2010 C#/.NET and SFML 2.0
« Reply #17 on: September 04, 2011, 09:51:46 pm »
What should I use instead of sf::Clock (it's in the missing system package).

I saw a game made in C# which used dll-imports of QueryPerformanceCounter.
You said that the package is missing, because C# already offers such functionality, so is there a C# clock with the same accuracy as the performancecounter or sf::Clock (which probably also uses the counter)?

And is this the right way to use BitConverter?
Code: [Select]

int toSerialize1 = 42;
int toSerialize1 = 43;
List<byte> bytes = new List<byte>();
byte[] arr = BitConverter.GetBytes(toSerialize1);
foreach(byte b in arr)
{
   list.Add(b);
}
arr = BitConverter.GetBytes(toSerialize2);
foreach(byte b in arr)
{
   list.Add(b);
}
byte[] bytesToSend = new byte[list.Count];

//lets pretend we just received that and want to deserialize it
byte[] received = bytesToSend;
int received1 = BitConverter.ToInt32(received, 0);
int received2 = BitConverter.ToInt32(received, 4);