diff --git a/Facepunch.Steamworks.Test/Client/Friends.cs b/Facepunch.Steamworks.Test/Client/Friends.cs index fb1187d..9604516 100644 --- a/Facepunch.Steamworks.Test/Client/Friends.cs +++ b/Facepunch.Steamworks.Test/Client/Friends.cs @@ -1,5 +1,6 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; namespace Facepunch.Steamworks.Test { @@ -37,5 +38,53 @@ public void FriendListWithoutRefresh() } } } + + [TestMethod] + public void Avatar() + { + using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) + { + Assert.IsTrue( client.Valid ); + + var friend = client.Friends.All.First(); + + var img = client.Friends.GetAvatar( Steamworks.Friends.AvatarSize.Medium, friend.Id ); + + Assert.AreEqual( img.Width, 64 ); + Assert.AreEqual( img.Height, 64 ); + + while ( !img.IsLoaded && !img.IsError ) + { + client.Update(); + System.Threading.Thread.Sleep( 10 ); + } + + Assert.AreEqual( img.Data.Length, img.Width * img.Height * 4 ); + + DrawImage( img ); + } + } + + public static void DrawImage( Image img ) + { + var grad = " -:+#"; + + for ( int y = 0; y @@ -18,10 +21,61 @@ public class Image /// public bool IsError { get; internal set; } - public void Read( byte[] buffer ) + unsafe internal bool TryLoad( ISteamUtils utils ) { + if ( IsLoaded ) return true; + uint width = 0, height = 0; + + if ( utils.GetImageSize( Id, ref width, ref height ) == false ) + { + IsError = true; + return true; + } + + var buffer = new byte[ width * height * 4 ]; + + fixed ( byte* ptr = buffer ) + { + if ( utils.GetImageRGBA( Id, (IntPtr) ptr, buffer.Length ) == false ) + { + IsError = true; + return true; + } + } + + Width = (int) width; + Height = (int) height; + Data = buffer; + IsLoaded = true; + IsError = false; + + return true; } + public Color GetPixel( int x, int y ) + { + if ( !IsLoaded ) throw new System.Exception( "Image not loaded" ); + if ( x < 0 || x >= Width ) throw new System.Exception( "x out of bounds" ); + if ( y < 0 || y >= Height ) throw new System.Exception( "y out of bounds" ); + + Color c = new Color(); + + var i = ( y * Width + x ) * 4; + + c.r = Data[i + 0]; + c.g = Data[i + 1]; + c.b = Data[i + 2]; + c.a = Data[i + 3]; + + return c; + } + } + + public struct Color + { + public byte r, g, b, a; } } + +