Avatar loading, tests

This commit is contained in:
Garry Newman 2016-09-30 13:49:11 +01:00
parent 2b39d693ef
commit de6b6c8962
3 changed files with 117 additions and 13 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
namespace Facepunch.Steamworks.Test 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<img.Height; y++ )
{
var str = "";
for ( int x = 0; x < img.Width; x++ )
{
var p = img.GetPixel( x, y );
var brightness = 1 - ((float)(p.r + p.g + p.b) / (255.0f * 3.0f));
var c = (int) ((grad.Length) * brightness);
str += grad[c];
}
Console.WriteLine( str );
}
}
} }
} }

View File

@ -214,20 +214,21 @@ public Image GetAvatar( AvatarSize size, ulong steamid )
break; break;
} }
uint width = 0; var img = new Image()
uint height = 0;
if ( imageid != 0)
{ {
client.native.utils.GetImageSize( imageid, ref width, ref height ); Id = imageid
}
return new Image()
{
Id = imageid,
Width = (int)width,
Height = (int)height
}; };
if ( imageid == 0 )
return img;
if ( img.TryLoad( client.native.utils ) )
return img;
throw new System.NotImplementedException( "Deferred Avatar Loading Todo" );
// Add to image loading list
return img;
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Valve.Steamworks;
namespace Facepunch.Steamworks namespace Facepunch.Steamworks
{ {
@ -11,6 +12,8 @@ public class Image
public int Width { get; internal set; } public int Width { get; internal set; }
public int Height { get; internal set; } public int Height { get; internal set; }
public byte[] Data { get; internal set; }
public bool IsLoaded { get; internal set; } public bool IsLoaded { get; internal set; }
/// <summary> /// <summary>
@ -18,10 +21,61 @@ public class Image
/// </summary> /// </summary>
public bool IsError { get; internal set; } 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;
} }
} }