Windows Phone 7 development

I was wondering how to use some cool font in my XNA game. I came up with an idea to draw white and black text with an offset to give it “3d” look.

Here is the result:

Image

I made extension methods for SpriteBatch  that write this kind of text

public static class ExtensionMethods   
{       /// <summary>
        /// Draws white text with a black stroke
        /// </summary>
        public static void DrawStringBlackAndWhite(this SpriteBatch spriteBatch, SpriteFont smallFont, String text, Vector2 position)
        {
            spriteBatch.DrawStringWithStroke(smallFont, text, position, Color.White, Color.Black);
        }

        /// <summary>
        /// Draws white text with a black stroke
        /// </summary>
        public static void DrawStringBlackAndWhite(this SpriteBatch spriteBatch, SpriteFont smallFont, StringBuilder text, Vector2 position)
        {
            spriteBatch.DrawStringWithStroke(smallFont, text, position, Color.White, Color.Black);
        }

		/// <summary>
		/// Draws string with a stroke on up and right side of text
		/// </summary>
		public static void DrawStringWithStroke(this SpriteBatch spriteBatch, SpriteFont smallFont, String text, Vector2 position, Color color, Color stroke)
		{
			spriteBatch.DrawString(smallFont, text, position, stroke);
			spriteBatch.DrawString(smallFont, text, new Vector2(position.X - 1, position.Y + 1), color);
		}

		/// <summary>
		/// Draws string with a stroke on up and right side of text
		/// </summary>
		public static void DrawStringWithStroke(this SpriteBatch spriteBatch, SpriteFont smallFont, StringBuilder text, Vector2 position, Color color, Color stroke)
		{
			spriteBatch.DrawString(smallFont, text, position, stroke);
			spriteBatch.DrawString(smallFont, text, new Vector2(position.X - 1, position.Y + 1), color);
		}
	}

Recently, I’ve been looking for all usages of “this” in C#. I couldn’t find any good article, so I’ve “grepped” the spec.
Surprisingly, I had wrong idea of “this” that is the same as in Java.

What is “this”? It is a keyword used in many object-oriented programming languages to refer to the current object. It can be implemented as a pointer (f.e. C++) or reference (f.e. Java), but in C# it has a few different meanings.

Classes

We can use “this” in 3 different context

-call other contructors

-refer to instance fields or methods

-pass current object

public class Person
{
    private string name;
    public Person()
    {
       name = "John";
    }

    public Person(string name)
    : this() // call other constructor
    {
        // this is used to qualify the field
        // “name” is hidden by parameter
        this.name = name;
    }

    public string Name
    {
        get { return name; }
    }

    private void sayHi()
    {
        Console.WriteLine("Hi");
        Foo.SayName(this); //use this to pass current object
    }

    public void Speak()
    {
        this.sayHi(); // use this to refer to an instance method
         Console.WriteLine("Want to come up and see my etchings? ");
    }
}

class Foo
{
public static void SayName(Person person)
{
Console.WriteLine("My name is  {0}", person.Name);
}
}

 

Indexers

Define an indexer for the type to have array-like semantics.

public int this[int index]
{
    get { return array[index]; }
    set { array[index] = value; }
}

Extension methods

class Foo
{
     // with “this” modifiler we can use it like instance method of Person
     public static void SayName(this Person person)
     {
          Console.WriteLine(“My name is  {0}”, person.Name);
     }
}
 

Structures

struct MyVeryOwnInteger
{
    private int x;
    public int Gimme()
    {
    // inside struct this is treated as a variable
    // not reference to current structure
    return this.x;
    }
}
 

From the spe:

1.51.7This access

A this-access consists of the reserved word this.

this-access:
this

A this-access is permitted only in the block of an instance constructor, an instance method, or an instance accessor. It has one of the following meanings:

When this is used in a primary-expression within an instance constructor of a class, it is classified as a value. The type of the value is the instance type (§1.89.1) of the class within which the usage occurs, and the value is a reference to the object being constructed.

When this is used in a primary-expression within an instance method or instance accessor of a class, it is classified as a value. The type of the value is the instance type (§1.89.1) of the class within which the usage occurs, and the value is a reference to the object for which the method or accessor was invoked.

When this is used in a primary-expression within an instance constructor of a struct, it is classified as a variable. The type of the variable is the instance type (§1.89.1) of the struct within which the usage occurs, and the variable represents the struct being constructed. The this variable of an instance constructor of a struct behaves exactly the same as an out parameter of the struct type—in particular, this means that the variable must be definitely assigned in every execution path of the instance constructor.

When this is used in a primary-expression within an instance method or instance accessor of a struct, it is classified as a variable. The type of the variable is the instance type (§1.89.1) of the struct within which the usage occurs.

If the method or accessor is not an iterator (§1.100), the this variable represents the struct for which the method or accessor was invoked, and behaves exactly the same as a ref parameter of the struct type.

If the method or accessor is an iterator, the this variable represents a copy of the struct for which the method or accessor was invoked, and behaves exactly the same as a value parameter of the struct type.

Use of this in a primary-expression in a context other than the ones listed above is a compile-time error. In particular, it is not possible to refer to this in a static method, a static property accessor, or in a variable-initializer of a field declaration.

Yet another example that C# is not Java.

Usefull helper

The default parsing method for C# using current culture, which sucks really badly. ( I wrote about it on SO)

Here a lil helper  that I use

public static class Helper
{
public static bool TryParseDouble(this TextBox textbox, out double value)
{
if (double.TryParse(textbox.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out value))
{
textbox.Foreground = Brushes.Black;
return true;
}
else
{
textbox.Foreground = Brushes.Red;
return false;
}
}

public static bool TryParseDouble(this string text, out double value)
{
return double.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out value);
}
}

To answer this question, we need to know what kind of Operating System and architecture we are dealing with,  the Standard and articles refers to “storage” or “space”. These are the most general terms and the only valid ones, unless we make a bunch of assumptions.

For example:

malloc allocates a block of Virtual Address Space on the heap

This is not correct for many embedded systems. Many of them do not use Virtual Memory, because there is no need (no multitasking etc.) or for performance reasons. What is more, it is possible that some exotic device does not have the idea of heap – doubt that malloc would be used, but fairly that is one of the reasons why the Standard refers to “storage” – it is implementation-specific.

On the other hand, the example is correct for Window and Linux in our PCs. Let’s analyze it to answer the question.

First off, we need to define what is Virtual Address Space.

Virtual Address Space (VAS) is a memory mapping mechanism that helps with managing multiple processes.

  • isolate processes – each of them has his own address space
  • allow to allocate memory that 32-bit architecture is limited to.
  • provides one concise model of memory

Back to question, ”Does malloc allocate a block of memory on the heap or should it be called Virtual Adress Space ?”

Both statements are correct. I would rather say VAP instead of memory – it is more explicit. There is a common myth that malloc = RAM memory. Back in old day, DOS memory allocation was very simple. Whenever we ask for memory it was always RAM, but in modern Oses it may vary.

There had been an issue with MAC adress on WP7 – the MAC wasn’t exposed. As a result, there phone can’t be connected to MAC-secure WIFI.

After March update, the MAC is in About page.

 

Go Setting -> About -> More info.

voila.

For the engineers among us who understand that the obvious is not always the solution, and that the facts, no matter how implausible, are still the facts …

A complaint was received by the Pontiac Division of General Motors: “This is the second time I have written you, and I don’t blame you for not answering me, because I kind of sounded crazy, but it is a fact that we have a tradition in our family of ice cream for dessert after dinner each night. But the kind of ice cream varies so, every night, after we’ve eaten, the whole family votes on which kind of ice cream we should have and I drive down to the store to get it. It’s also a fact that I recently purchased a new Pontiac and since then my trips to the store have created a problem. You see, every time I buy vanilla ice cream, when I start back from the store my car won’t start. If I get any other kind of ice cream, the car starts just fine. I want you to know I’m serious about this question, no matter how silly it sounds: ‘What is there about a Pontiac that makes it not start when I get vanilla ice cream, and easy to start whenever I get any other kind?'”

The Pontiac President was understandably skeptical about the letter, but sent an engineer to check it out anyway. The latter was surprised to be greeted by a successful, obviously well-educated man in a fine neighborhood. He had arranged to meet the man just after dinner time, so the two hopped into the car and drove to the ice cream store. It was vanilla ice cream that night and, sure enough, after they came back to the car, it wouldn’t start.

The engineer returned for three more nights. The first night, the man got chocolate. The car started. The second night, he got strawberry. The car started. The third night he ordered vanilla. The car failed to start.

Now the engineer, being a logical man, refused to believe that this man’s car was allergic to vanilla ice cream. He arranged, therefore, to continue his visits for as long as it took to solve the problem. And toward this end he began to take notes: he jotted down all sorts of data, time of day, type of gas used, time to drive back and forth, etc.

In a short time, he had a clue: the man took less time to buy vanilla than any other flavor. Why? The answer was in the layout of the store. Vanilla, being the most popular flavor, was in a separate case at the front of the store for quick pickup. All the other flavors were kept in the back of the store at a different counter where it took considerably longer to find the flavor and get checked out.

Now the question for the engineer was why the car wouldn’t start when it took less time. Once time–not the vanilla ice cream–became the problem the engineer quickly came up with the answer: vapor lock. It was happening every night, but the extra time taken to get the other flavors allowed the engine to cool down sufficiently to start. When the man got vanilla, the engine was still too hot for the vapor lock to dissipate.

It seems that VS 2010 screws the project properties when namespaces are changed.

I posted it on SO.

And the solution is:

Check the “Startup Object” in the project properties page. The sometimes requires manually being set/corrected when the namespace of the app is changed.

Thx Matt ;)

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: