Hello Windows 8 XNA-ish

Yes you read that right. You can really get a flavor of XNA on that snazzy new Windows 8 installation of yours.

Anything you build using this can work as Modern UI app. I know for many of us, like the crew at Three Red Cubes, this revelation put a smile across our faces.

Before you think I am bluffing, much of what I am about to detail has been covered by the original posts. Also, there is a Channel9 post about it.

Here is a rundown of my experience and the broad strokes of the steps.

Backstory

The answer to the owes of XNA developers comes in the form of MonoGame which is an open source implementation of Microsoft XNA 4. MonoGame allows XNA-style development for plenty of platforms but that’s not the focus.

The development of MonoGame for Windows 8 has been a little slow so the hackers at SickHeadGames came to the rescue.

Now it is possible to migrate existing XNA games without having to dive through SharpDX.

(Kind of Long) Setup

a. Getting into Github

Github account and the client are free. Also, One click installs are awesome!

b. MonoGame Source Code

1. Run Git Shell.
2. Change directory (cd) to where you want to store your code
3. Copy and paste this to the shell and hit enter:

git clone https://github.com/SickheadGames/MonoGame && cd MonoGame && git submodule init && git submodule update

to start pulling the code.

c. Visual Studio Template

If you browse to C:\Users\[you]\Documents\GitHub\MonoGame\ProjectTemplates\VisualStudio11.MonoGame.2.5\VS11MGWindowsMetroTemplate you’ll find a whole bunch of files that you can zip up and copy over to C:\Users\[you]\Documents\Visual Studio 2012\Templates\ProjectTemplates\Visual C#. This will create a Visual Studio 2012 project template once you restart Visual Studio.

d. Reference MonoGame

Once you start a solution using that new template we just created, right click on your solution in the Solution Explorer and add the MonoGame Framework Windows 8 Project from C:\Users\[you]\Documents\GitHub\MonoGame\MonoGame.Framework\MonoGame.Framework.Windows8.sln.

Now, add a reference to the MonoGame Framework in your game project by right clicking on references, select Add Reference. Under Projects choose the MonoGame Framework project (check the box!) and click OK.

e. Hit F5

Make sure you select the game project to be the startup project and compile and run the project.
You should be greeted with the good-old cornflower blue that XNA programmers know and love.

f. Content Pipeline

You may have noticed the absence of that good old content project. At this time, MonoGame does not have an implementation of the Content Pipeline and Visual Studio 2012 does not have native support for XNA development therefore we need a workaround until which is in about a month at the time of writing:

1. Head over to Aaron Stebner’s blog post and (possibly) install XNA Game Studio or Windows Phone on Windows 8.
2. Open up Visual Studio 2010 and create a XNA project that will come with the content project.
3. Import all your resources and build the project which will output xnb files to C:\Users\[you]\Documents\Visual Studio 2010\Projects\[project name]\[project name]\[project name]\bin\x86\Debug\Content.
4. Copy these files to your MonoGame project’s Content folder: C:\Users\[you]\Documents\Visual Studio 2012\Projects\[project name]\[project name]\bin\Debug\AppX\Content.

Now, you can use the ContentManager object to load game assets during runtime. Just like the XNA we know.

Credits

All of this came into light, once again, from Bob Familiar’s blog posts. And it is only possible because of the hard work of the MonoGame project and SickHeadGames.

Q & A

So, is XNA dead?
I don’t think so. It has been reincarnated in Windows 8.

Will there be a Windows 8 XNA Game Studio release from Microsoft?
We don’t know yet, but you never know.

Is MonoGame the only solution?
No, SharpDX is just an awesome a solution.

For now though, here’s to having XNA back on Windows 8.


Back to Basics

After some more reading I find myself a bit lost and wishing to go back through some of the things I have read. So I put together this review of sorts summarizing the things I read which I am certain will serve as a great cheat sheet afterward.

//defining the structure of the class
#include "stuff"
@interface ClassName : SuperClassName
{
    variableTypes stateVariables;
}
//functions go here
@end

//building the structure of the class
#include "theInterface"
@implmentation Class
//functions implemented
@end

//running the main routine to execute logic
#include "stuff"
int main (int argc, char *argv[])
{
    NSLog(@"printing out to the console");
    return 0;
}
//variables used same way as in C/Java
int aVariable;

//objects are declared and initialized
NSObject anObject = [[NSObject alloc] init];
//they are freed by
[anObject dealloc];

//functions go this way
methodIdentifier (returnType)functionName:(parameterType)parameterName selector:(parameterType)parameterName
{
    return somethingOfReturnType;
}
//functions are called
[anObjectTakingMessages functionName:argument selector:argument];

That’s all I can think of being basic for now. I will update this posts if I think up some more things that should be considered basic.


Improving is Learning

After a bit of thinking about the class I wrote in my previous post, I figured I should make the class have more features, and learn a few things while doing so.
The interface tells the story, I added methods to get and set the state variables and calculate the power rating. Also, the class can be instantiated with the initial values for the state variables.


//fileName: Resistor.h

@interface Resistor : NSObject
{
	double resistance, voltageAcross, currentThrough;
}

- (id) initWithResistance:(double)r
				  voltage:(double)v
				  current:(double)i;
- (void)setVoltageAcross:(double)v;
- (void)setCurrentThrough:(double)i;
- (double)getResistance;
- (double)getVoltageAcross;
- (double)getCurrentThrough;
- (double)getPowerRating;
@end

As I did in the previous post, I return self which is equivalent to this in Java or C#. It (rather obviously) refers to the class it’s invoked in.


//fileName: Resistor.m

#import "Resistor.h"

@implementation Resistor
- (id) initWithResistance:(double)r
				  voltage:(double)v
				  current:(double)i
{

	resistance = r;
	voltageAcross = v;
	currentThrough = i;
	
	return self;
}

- (void) setVoltageAcross:(double)v
{
	voltageAcross = v;
}

- (void) setCurrentThrough:(double)i
{
	currentThrough = i;
}

- (double) getResistance
{
	return resistance;
}

- (double) getVoltageAcross
{
	return voltageAcross;
}

- (double) getCurrentThrough
{
	return currentThrough;
}

- (double) getPowerRating {
	double p;
	
	if (voltageAcross == 0.0)
		p = currentThrough * currentThrough * resistance;
	else if (currentThrough == 0)	
		p = (voltageAcross * voltageAcross) / resistance;
	else
		p = voltageAcross * currentThrough;
	
	return p;
}
@end

Instead of including the print statement in the resistor class, I manipulated the main method to print out the information of the Resistor.

One thing to mentions is on line 14. Since the classes have an alloc method, which allocates memory there must be a way to deallocate the memory, and what do you know, dealloc does indeed deallocate the memory allotted for the instance.


//fileName: main.m

#import <Foundation/Foundation.h>
#import "Resistor.h"

int main (int argc, char *argv[]) {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	Resistor * aResistor = [[Resistor alloc] initWithResistance:10 voltage:25 current:1];
	
	NSLog(@"Resistance: %f, Voltage: %f, Current: %f, Power Consumed: %f",
		  [aResistor getResistance], [aResistor getVoltageAcross], [aResistor getCurrentThrough], [aResistor getPowerRating]);	
	
	[aResistor dealloc];
	
	[pool drain];
	return 0;
}

Another thing (other than the compiler directive) that’s really cool is how messages are passed to objects. [[Resistor alloc] initWithResistance:10 voltage:25 current:1]; calls the initWithResistance metod and passes arguments, which I understand could have also been done by [[Resistor alloc] initWithResistance:10:25:1];. But it makes the code more readable and neater to use names for the arguments (called, selectors) being passed.


run
[Switching to process 8053]
Running…
2009-12-06 21:02:24.335 Untitled[8053:a0f] Resistance: 10.000000, Voltage: 25.000000, Current: 1.000000, Power Consumed: 25.000000

Debugger stopped.
Program exited with status value:0.

I think that’s pretty much it for the Resistor class. I am going to try out something new next.


It is All About the Class

At the heart of any object oriented language are well, objects. Defining these objects are classes. It’s common to start learning from all the variable types and naming conventions but as I already know a little about these things from Java and C# I am taking the bold step of writing a class which defines and tests a resistor.

First there’s the interface presenting the class’s members.

The @interface thing is called a compiler directive. I find them really cool for some reason. They are used to declare and define a whole lot of things, as I understand. I will be learning more of them later.


//fileName: Resistor.h

@interface Resistor : NSObject
{
 double resistance, voltageAcross, currentThrough;
}

- (id) init:(double)r;
- (void) setVoltageAcross:(double)v;
- (void) setCurrentThrough:(double)i;
- (void) print;
@end

Next the actual definition of the class and all the things it can do, in accordance with the interface defined above.

The highlighted line below prints out information about the resistor. NSLog calls the console in xCode and writes a string to it. Writes a NSString, to be exact, which is defined and initialized by the compiler directive beginning with @.


//fileName: Resistor.m

#import "Resistor.h"

@implementation Resistor
- (id) init:(double)r
{
 resistance = r;
 return self;
}

- (void) setVoltageAcross:(double)v
{
 voltageAcross = v;
}

- (void) setCurrentThrough:(double)i
{
 currentThrough = i;
}

- (void) print
{
 NSLog(@"V:%f I:%f R:%f", voltageAcross, currentThrough, resistance);
}
@end

Now to test the class I wrote a main routine which goes pretty much like C but with some differences.


//fileName: main.m

#import <Foundation/Foundation.h>
#import "Resistor.h"

int main (int argc, char *argv[]) {
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 Resistor * aResistor = [[Resistor alloc] init:10];

 [aResistor setVoltageAcross:25];
 [aResistor setCurrentThrough:1];
 [aResistor print];

 [pool drain];
 return 0;
}

The highlighted lines are foreign to me, and I will seek our their reason for existence soon. But as far as I understand thus far, a class called NSAutoreleasePool is instansiated and one of its methods is called on in the second last line.

The #import statements in lines 3 and 4 respectively import the base library and the Resistor class to the routine.

Line 9 creates an object of Resistor type and set its resistance to 10.
Lines 11 to 13 ask the aResistor object to execute the methods I wrote in the Resistor class. They set the values of voltage and current of the resistor and call on print to give the output.

Now the output, as expected is:

run
[Switching to process 6845]
Running…
2009-12-06 15:35:58.708 Untitled[6845:a0f] V:25.000000 I:1.000000 R:10.000000

Debugger stopped.
Program exited with status value:0.

This is all too pretty, let’s see what comes next.


Origins

History is vital to gaining insight and intimacy with any subject. So I explore the history behind Objective-C, its roots and its branches.

Objective-C was designed by Brad J. Cox in the early 1980’s. It was modelled after SmallTalk-80 but it was essentially an extension of the C programming language. It added a layering to C that enabled creation and manipulation of objects.

NeXT Software (later acquired by Apple) licensed the Objective-C language and developed its libraries. Slowly the language was supported by FSF’s GNU (enabling everyone’s free access to the language and its tools) and other platforms such as Linux in the form of LinuxStep.
When Apple acquired Next Software in 1996, the development environment created with Objective-C NEXTSTEP became the standard for Apple’s operating system OS X as we know it now.
Along with OS X came a wave of tools that allow leverage on the features of Objective-C and its libraries.

The latest major of the language came in 2007 when Objective-C 2.0 was released. Which I will be diving into, of course.