Showing posts with label silverlight. Show all posts
Showing posts with label silverlight. Show all posts

Wednesday, 1 May 2013

Enable Azure Mobile Services for "pure" Windows Phone 7.1 Xna Games

     Recently somebody asked me for an advice on what library should he use to communicate with an Azure Mobile Service for an Windows Phone OS 7.1 and Windows 8 Xna game. My answer was, of course, the Windows Azure Mobile Services client library available on NuGet as I remembered that the prerelease version works also for Windows Phone OS 7.x applications.

The "problem" is that the library will not install if the game is a pure Xna Windows Phone project.

There are 2 (maybe 3 :) ) solutions to this problem:
1. Migrate your XNA project to an "Windows Phone XAML and XNA App" and on this type of project the client library will install correctly.
2. You could use a small hack in order to force NuGet to install the Azure Mobile Services client library on your XNA project. The hack is pretty simple:
  • open the YourProject.csproj file with an text editor (like Notepad) and add these 3 line (not sure if all 3 of them are needed but doesn't matter as it is only done to force the library to install):
  •  <SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>  
    <TargetFrameworkProfile>WindowsPhone71</TargetFrameworkProfile>
    <TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
  •  Visual Studio will see that the project has been modified so it will ask to Reload the project. Reload it (don't try to build the project as it won't), open the Package Manager Console and write:
     Install-Package WindowsAzure.MobileServices -Pre   
    the package should now install.
  • Reopen the .csproj file in Notepad and remove the 3 lines we've previously added, save and reload the project in Visual Studio. The project should compile correctly and you should be able to use the Azure Mobile Services client library.

The only thing that doesn't seem to work is make Visual Studio recognize the await keyword for the XNA project. You will have to use the ContinueWith syntax:

 void TestAzureMobile()  
{
todoitem item = new todoitem { Text = "Awesome item XNA" };
ms.GetTable<todoitem>().InsertAsync(item).ContinueWith((task)
=>
{
//do what you need here
});
}

3. Came to me while writing the post. Guess the most "elegant" way is to add an Windows Phone Class library to your project, add and use the Azure Mobile Service client library from the class library, reference the class library from your XNA project. Tried this one but the MobileServiceClient seems to fail initialization with 'System.TypeInitializationException'.

Take in consideration that the Azure Mobile Services client library is still a pre-release version in this moment so for the release they might fix/enhance it and you could install it on XNA projects without any hack needed (it is a PCL library).

Wednesday, 26 October 2011

WP7 A better InkPresenter using XNA

  Last week I was at SMAU Italia  together with Matteo Pagani giving a hand at Windows Phone 7 labs and having a good time with my friends at Microsoft. I also got the pleasure to meet Ben Riga. One of the attendees at the laboratory raised a really good question about the performance of the InkPresenter in WP7. He is using the control to capture a signature, but if you are pretty fast (usually people when they do their signature are fast) the result is "ugly" and not really usable. You won't get a smooth curve, but something like this:


 This screenshot is taken in the emulator, but on the real device the curve looks worst and it's easy to reproduce this behavior. I took this "problem" as a challenge (two nights of work and now the third to write the post) convinced that I can get better results with a mix between XNA and Silverlight, mix that in Mango is possible. T
 The main problem is the number of points returned by the event MouseMove of the InkPresenter control which is not enough points to draw a decent curve.
  The first thing I've tried in the XNA version was to use the TouchPanel.GetState() but I was surprised to see that I get the same number of sampling points as the MouseMove in InkPresenter. The things improved a lot when I've used TouchPanel.ReadGesture() with GestureType.FreeDrag. Using these sampling points as StylusPoints for the InkPresenter the situation improved a little:


 The black line is the one obtained with MouseMove and the red one is the one obtained with ReadGesture.The result is better, but not satisfying. The only way to further improve the result was to manually draw the curve without InkPresenter. I've have used BezierSegment to draw the curve. Without any processing this would be the result:


The new curve is the blue one which is better than the others, but it is still not smooth in some points. This is because a Bézier path is smooth if each endpoint and its two surrounding control points lie in a straight line. In other words, the two tangents at each Bézier endpoint are parallel.



There is more than one approach to solve this behavior and I've implemented two (with all the mathematics I felt back at the University http://en.wikipedia.org/wiki/B%C3%A9zier_curve). 

The first is an algorithm for Automatically Fitting Digitized Curves with the Douglas Peucker algorithm to reduce the number of points (http://stackoverflow.com/questions/5525665/smoothing-a-hand-drawn-curve).

The second one uses Bézier splines which creates the First and Second control points for each Bézier segment http://www.codeproject.com/KB/graphics/BezierSpline.aspx . 

With both algorithms the results are pretty good:


If you are interested to play with the sample you can change the following parameters:

double PhilipSchneiderTolerance = 0;
double PhilipSchneiderError = 4;
-used when the fitting algorithm is PhilipSchneider (run this sample http://cid-c27e99281f78a67a.office.live.com/self.aspx/Public/Simplify.zip on the desktop to understand what changes when you change the Tolerance and the Error)

 private bool _showPoints = false;
-shows or hides the points read by ReadGesture
        
private bool _showOtherCurves = false;
-shows or hides the InkPresenter curve using MouseMove and ReadGesture

private FitCurveAlgorithm _algorithm = FitCurveAlgorithm.PhilipSchneider;
-changes the fitting algorithm: None, BezierSpline, PhilipSchneider




P.S. The project still needs some working/polishing in order to be used in production, but that is the simple part so... HAVE FUN

NAMASTE

Thursday, 25 August 2011

Where XNA beats up Silverlight

   Finally a blog post. I am in the process of updating (it's more of a rewriting) Boxfiles for Mango. Till now I have rewritten the Dropbox library from scratch (took off all the dependencies Hammock, Json.Net and now it's pure HttpWebRequest) and developed a library for accessing Skydrive resources (it works and it is possible to list the folders and download the files mainly what I need to get the modified Office files back to Dropbox). Anyway the Skydrive API it's a mess for the moment and I really hope they will get a "clean" API. The current version of the Rest Api is usable only for contacts, photos and videos. I will post both libraries on Codeplex (very probable after I finish the update of Boxfiles). 
    So what is this post all about. One of the features of the new version of Boxfiles will be pinch to zoom. Searching on the web I have found the XNA and the Silverlight solutions. I've started with Silverlight and with pinch to zoom added the application would easily eat more than 90MB of RAM while the image window is opened.  Digging up on what was consuming so much memory it turned up that the image window eats around 40 MB of memory (also because I have enabled CacheMode="BitmapCache" in order to have a smooth zoom). This is an abomination for a simple page with one menu and an Image control displaying a file  of 3MB. I than remembered that, in Mango, Silverlight and Xna can "live" together. The feature was introduced for enabling Silverlight in Xna (menus and stuff) , but in this particular case I will use Xna in Silverlight. I've created a project where I can compare the pinch to zoom on both Xna and Silverlight. The results are not bad: XNA uses around 10MB while Silverlight uses 40MB and the pinch to zoom is much smoother in Xna (the sample starts with 10MB of memory occupied).




    In order to have a realistic comparison I've added an ApplicationBar to the Xna window and both of the windows have a Menu where you can force the GC to collect. While testing the solution I've found a really strange behavior (in my opinion it's a memory leak of the image control and maybe somebody from Microsoft should have a look into it): the Silverlight window, after the first launch, was not releasing 10MB of memory. In order to release the memory I had to manually remove the Image control from the ContentPanel and give it a null value.


ContentPanel.Children.Remove(image);
image = null;

If you play with the solution just comment the two lines located in OnNavigatedFrom (ImageSilverlight.xaml.cs) and you will see the leak.

Here is the Source Code

NAMASTE!

Tuesday, 11 May 2010

WP7 Memory Requirements

Finally some time for blogging. Let's go to the subject...

The new "unlocked" image gives the possibility to do some tests of how much memory does WP7 eat as OS and also how much memory the developed apps occupy.
The emulator starts with 77 MB occupied. To understand how much that changed in comparison to previous versions I started all the other emulators I have installed on my development system and this is the result:

Not so bad... The memory requirement only doubled (the same happened when they passed from PPC2003 to WM5).
As the apps don't offer the option to exit them programmatically I've started launching all included applications. Arriving at 160 MB of memory occupied the emulator doesn't work correctly anymore and you will get screens like these ones:
























This may be a bug of the RC emulator image, but I still prefer a GOOD OLD EXIT function for my apps.
I than run some tests to see how much memory a simple application occupies. The "empty" application generated by VS 2010 started with the debugging option occupies around 8 MB of memory and without debugging 6MB. I did the same test on Win32 and NET CF and grouped the results in a chart:


Let's say that an empty app that does nothing and occupies 6MB means that the framework is not yet optimized. It would be interesting to see how much memory requires a more complex application. Looking at this chart I think Microsoft should offer the possibility to develop natively or using NETCF on the WP7 platform.

Then there is the whole Running,Paused,Suspended,Resume mechanism that doesn't convince me 100%. If the OS decides when to suspend(kill) an application why doesn't the OS automatically save the state of the suspended application and then when the app resumes the OS could automatically reload the last state? The OS has access to the File System which is faster than IsolatedStorage accessible to the user. Personally I prefer the "old" way to work with the apps not this one that seems copied from the iPhone OS. In fact the whole OS looks like is developed taking iPhone as a model and not looking at what was good in the old OS. They erased not only what was bad but also what was good like the possibility to group the applications in folders which was implemented since Pocket PC , that misses in WP7 and which will be implemented in iPhone OS4.

In hope I will find more time for blogging.
In the meanwhile NAMASTE!

Wednesday, 17 March 2010

WP7 Emulator Build 7.0.0.6077

Tonight I start looking how the new emulator is integrated in Visual Studio 2010. Using Process Explorer I saw that VS launches this command:
"C:\Program Files\Microsoft XDE\1.0\XDE.exe" "C:\Program Files\Microsoft SDKs\WindowsPhone\v7.0\Emulation\Images\WM70C1.bin"

So the new Device Emulator changed name in XDE (version 10.0.30128.39) and we have the bin file that contains the emulator image. I then remembered that you can extract the files from a bin image, but the guys at XDA were already ahead. They made a dump of the ROM files. A lot of people are digging up and we might see some interested things in the next couple of days.

This is what I've found out. The new OS, build 7.0.0.6077, has NET CF 3.7 embedded. I then remembered that there was a CF 3.7 cab version that was running on WM 6.5 and I got this "crazy" idea that I have to try... What if we could run the Silverlight applications that we compile for WP 7 on WM 6.5? I'm gonna dig this up tomorrow.

Anyway in the dump files I could find the Silverlight assemblies:
GAC_System.Windows.Browser_v2_0_5_0_cneutral_1.dll 129.328
GAC_System.Windows.RuntimeHost_v2_0_5_0_cneutral_1.dll 35.120
GAC_System.Windows_v2_0_5_0_cneutral_1.dll 1.038.640
that might just work on WM 6.5

and also the XNA assemblies:
GAC_Microsoft.Xna.Framework.GamerServices_v4_0_0_0_cneutral_1.dll 112.088
GAC_Microsoft.Xna.Framework.Game_v4_0_0_0_cneutral_1.dll 71.128
GAC_Microsoft.Xna.Framework.Graphics_v4_0_0_0_cneutral_1.dll 189.912
GAC_Microsoft.Xna.Framework.Input.Touch_v4_0_0_0_cneutral_1.dll 34.264
GAC_Microsoft.Xna.Framework_v4_0_0_0_cneutral_1.dll 382.424
XnaFrameworkCore.dll 357.848

XnaFrameworkCore.dll it's a native file so I think we would need one from a real device not emulator.

There is also SQL Ce Compact 3.5
GAC_System.Data.SqlServerCe_v3_5_1_0_cneutral_1.dll the version is 3.5.5722.1

And more managed assemblies:
GAC_Microsoft.Devices_v2_0_5_0_cneutral_1.dll
GAC_Microsoft.Phone.Controls.Navigation_v2_0_5_0_cneutral_1.dll
GAC_Microsoft.Phone.Controls.WebBrowserInterop_v2_0_5_0_cneutral_1.dll
GAC_Microsoft.Phone.Controls.WebBrowser_v1_0_0_0_cneutral_1.dll
GAC_Microsoft.Phone.Controls_v1_0_0_0_cneutral_1.dll
GAC_Microsoft.Phone.Execution_v1_0_0_0_cneutral_1.dll
GAC_Microsoft.Phone.Info_v1_0_0_0_cneutral_1.dll
GAC_Microsoft.Phone.License_v2_0_5_0_cneutral_1.dll
GAC_Microsoft.Phone.Notification_v2_0_5_0_cneutral_1.dll
GAC_Microsoft.Phone.Shell.Interop_v2_0_5_0_cneutral_1.dll
GAC_Microsoft.Phone.Shell_v1_0_0_0_cneutral_1.dll
GAC_Microsoft.Phone.TaskModel.Interop_v2_0_5_0_cneutral_1.dll
GAC_Microsoft.Phone.Tasks_v2_0_5_0_cneutral_1.dll
GAC_System.Device.Location_v2_0_5_0_cneutral_1.dll
GAC_CaptureApiManaged_v1_0_0_0_cneutral_1.dll


It's a start...We can look a little under the hood of WP 7 and see how it is designed. Pretty interesting, right?