Saturday, April 30, 2011

Setup Windows Phone 7 apps with Smaato ads for Microsoft XNA framework

You might be asking yourself why you would choose smaato to monetize with in-app ads.
Firstly, smaato supports many countries and it has the SOMA SDK that is compatible with XNA.
Secondly, it allows for transfering of ad revenue through http://www.paypal.com/.

Note: this tutorial is for Windows Phone 7 XNA framework and not for Silverlight framework.



1) Sign up for a smaato account at http://www.smaato.com/


2) Generate AdSpace under the tab My Sites & Apps

3) Select - App (Windows Phone 7)
Note: This will generate your PublisherID and AdSpaceID -- these are very important and will be required as identifiers for you and your app.


4) Download the SOMA SDK for Windows Phone 7


5) From the SOMA SDK zip file place the SOMAWP7.dll and SOMAWP7.pdb files into your Windows Phone 7 project where the projectname.csproj file is located.


6) In your Windows Phone 7 project Right click on References in your Solution Explorer and ad SOMAWP7.DLL from Add Reference Browse tab and Microsoft.Phone from Add Reference .Net tab
























7) Create a 480x79 png file called sampleAd.png in the project content folder and add sampleAd.png into the project's Content area inside the Solution Explorer.


8) In your project.cs file place the following line of code at the top your file

using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using SOMAWP7;
 

9) In your project class that inherits the XNA framework place the following code

namespace Project
{    public class Project1 : Microsoft.Xna.Framework.Game   
    {      
       SomaAd somaAd;
       Texture2D textureSomaAd;
       SpriteBatch spriteBatch;


10) Initialize SOMA SDK using the AdSpaceID and PublisherID

protected override void Initialize()
{ 
  //Set up SomaAd to get ads
  somaAd = new SomaAd(); 
  somaAd.Adspace = 12345678;
  somaAd.Pub = 12345678; 
 
  //gets user location from device
  somaAd.LocationUseOK = true;

  //information about the app running 
  somaAd.Kws = "games, windows phone, entertainment"; 
  //search string for ads to show
  somaAd.Qs = "games, windows phone, entertainment, fun, exciting";

  //Ask the user this informaton about them 
  //somaAd.Age = 30; 
  //somaAd.Gender = "m";  
  //By default, the SDK sets this to the current location 
  //somaAd.City = "Fremont"; 
  //somaAd.State = "Ca"; 
  //somaAd.Zip = "94539"; 
  //somaAd.Country = "United States";
  //somaAd.Countrycode = "us"; 
 
  //By default, adInterval will deliver ads after 60 seconds.  
  //somaAd.adInterval = 60000;
 
  somaAd.GetAd();
  base.Initialize();
}


11) Load content into app for SOMA

protected override void LoadContent()
{
  // Create a new SpriteBatch, which can be used to draw textures.
  spriteBatch = new SpriteBatch(GraphicsDevice);
  textureSomaAd = null;
  // setup touch
  TouchPanel.EnabledGestures = GestureType.Tap;

12) Make sure to Dispose SOMA with the UnloadContent function

protected override void UnloadContent()
{
  somaAd.Dispose();
}


13) Check for screen touch in the SOMA image. Get the file name of the SOMA image and load up browser with link if touch detected. 

string currentAdImageFileName = "";
protected override void Update(GameTime gameTime)
{

  // if the back button is pressed then go back to the main menu 
  if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  {
    Exit();
  } 

  Vector2 somaAdPosition = new Vector2(0, GraphicsDevice.PresentationParameters.BackBufferHeight - 80);
  Vector2 somaAdSize = new Vector2(GraphicsDevice.PresentationParameters.BackBufferWidth, 80);
  while (TouchPanel.IsGestureAvailable)
  {
    GestureSample gestureSample = TouchPanel.ReadGesture();
    if (gestureSample.GestureType == GestureType.Tap && somaAd.Uri != "" && textureSomaAd != null)
    {
      Vector2 touchPosition = gestureSample.Position;

      if (touchPosition.X >= 0 &&
        touchPosition.X < somaAdSize.X &&
        touchPosition.Y >= somaAdPosition.Y &&
        touchPosition.Y < (somaAdPosition.Y + somaAdSize.Y))
      {
        WebBrowserTask webBrowserTask = new WebBrowserTask();
        webBrowserTask.URL = somaAd.Uri;
        webBrowserTask.Show();
      }
    }
  }

    // if there is a new ad, get it from Isolated Storage and  show it
  if (somaAd.Status == "success" && somaAd.AdImageFileName != null && somaAd.ImageOK)
  {
    try
    {
      if (currentAdImageFileName != somaAd.AdImageFileName)
      {
        currentAdImageFileName = somaAd.AdImageFileName;
        IsolatedStorageFile myIsoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream myAd = new IsolatedStorageFileStream(somaAd.AdImageFileName, FileMode.Open, myIsoStore);
        textureSomaAd = this.Content.Load<Texture2D>("sampleAd");
        textureSomaAd = Texture2D.FromStream(this.GraphicsDevice, myAd);

        myAd.Close();
        myAd.Dispose();
        myIsoStore.Dispose();
      }
    }
    catch (IsolatedStorageException ise)
    {
      string message = ise.Message;
    }
  }
  base.Update(gameTime);
}


14) Draw the texture of the SOMA image
protected override void Draw(GameTime gameTime)
{
  // draw ad
  //cubeToDraw.RenderToDevice(GraphicsDevice);
  if (textureSomaAd != null)
  {
    this.spriteBatch.Begin();
    this.spriteBatch.Draw(textureSomaAd, new Rectangle(0, GraphicsDevice.PresentationParameters.BackBufferHeight - 80, GraphicsDevice.PresentationParameters.BackBufferWidth, 80), Color.White);
    this.spriteBatch.End();
  }
  base.Draw(gameTime);
}


15) Create your app on Microsoft App Hub and submit it.

16) Go to http://www.smaato.com/ and click on Add New App. For the Download URL field copy and paste the Deep link example from your App Hub app information page.



17) Something to be aware of is that your phone must have 3G or a WIFI connection to the internet for your ads to show up.
Note: when you are debugging your app and the phone is connected to Visual Studio 2010 Express for Windows Phone the ads do not show up even with a working internet connection on the phone. You must disconnect the phone from the computer in order for the ads to appear.

Written by: Paul

Wednesday, April 27, 2011

How to compile STLport with Visual Studio 7 (.NET)



STLport is a fast, portable, free, multi-featured and opensource version of the standard library. In this tutorial I will demonstrate how to get it working under Visual Studio C++ 7 (.NET).

1. Download the windows version of STLport from www.stlport.org.

2. Unzip it into any directory go to the src directory and rename the file vc7.mak to makefile.

3. Go to your command prompt and change directory to the Visual C++ bin directory like this "cd Program Files\Microsoft Visual Studio .NET\Vc7\bin " this is where you find the vcvars32.bat file. Now enter in vcvars32.bat. Then change to the directory where you unzipped STLport.

4. Now enter "nmake clean all" and STLport will build itself as a release build. To get a debug build enter "nmake debug_static" and then "nmake debug_dynamic".

5. After building enter "nmake install" and it will install all necessary headers, libs and dlls into their appropriate directories.

6. Now open Visual Studio. Then go to the "Options" menu and find the "Projects" heading, click on that and go to the "VC++ directories" heading. There you select "Include Files" and you add the STLport header directory which should be "Program Files\Microsoft Visual Studio .NET\Vc7\include\stlport". Then click on the up arrow and make sure that the directory goes all the way to the top of the list.

7. To get STLport to run in debug mode for debug builds write the following preprocessor code before including any stl headers. Also you have to get rid of windows.h macros max and min because they interfere with numeric limits max and min functions. Here is how you can setup your headers.  

#ifdef NDEBUG 
# undef _STLP_DEBUG 
#else 
# define _STLP_DEBUG 1 
#endif 
#ifdef WIN32 
# undef max 
# undef min 
#endif 

//STL includes 
#include <limits> 
#include <vector> 
#include <map> 
#include <string> 
#include <set> 
#include <memory>  

//Stream includes  
#include <fstream> 
#include <iostream> 
#include <iomanip>  

namespace 
{ 
//C Library includes  
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>  
} 

#include <assert.h>  

using namespace _STL;

8. If you want to use stream includes from STLport you have to get rid of Microsoft's std libs from the build. You do this by going to the property sheet for the project you are building and go to the headings "Linker" and then "Input" and use the ignore specific library feature and type "libcmt.lib" for Release builds and "libcmtd.lib" for Debug builds. Other useful self defining features of STLport are:  

//changes its namespace _STL into std 
#define _STLP_NO_OWN_NAMESPACE 
//with this define you don't need to rely on using namespace_STL; 
#define _STLP_NO_NAMESPACES 
#define _STLP_USE_SYSTEM_ASSERT 
//define this with MFC and include <stl/_config.h> then include <afx.h> instead of <windows.h> 
#define _STLP_USE_MFC  
//bounds checker needs this defined but makes your app run slower 
#define _STLP_USE_MALLOC  
//bounds checker needs this defined but makes your app run slower 
#define _STLP_USE_NEWALLOC  
//set this if you don't want multithreading 
#define _STLP_NO_THREADS 
#define _STLP_USE_STATIC_LIB 
#define _STLP_USE_DYNAMIC_LIB  
//debug for memory allocations 
#define _STLP_DEBUG_ALLOC  
//does not initialize allocated memory to zero like release builds 
#define _STLP_DEBUG_UNINITIALIZED  
//if you are having trouble building in debug mode try this 
#define _STLP_USE_ABBREVS

Written by: Paul