Click here to Skip to main content
15,882,017 members
Articles / XNA

Does Lightning Really Strike Twice?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Feb 2013CPOL5 min read 9.7K   3   2
Does lightning really strike twice?

Image 1

It all started one sunny morning when Michael Hoffman who wrote a shocking article on some fantastic lighting effects all done with XNA. I read this and was immediately impressed by just how simple his solution was.

Image 2

Not long after this, a guy down under called Jose Fajardo was so impressed, he went and got the same framework running on Windows 8 /Windows RT using Sharp DX combined with XAML.

Both demos really show the power and ease of building projects using XNA, I was tempted to follow up like Jose following Michael’s article but was busy with other projects. With Jose’s article, I felt freshly invigorated in the light of my XNA futures series to show both how easy it is to use this solution, but also to go further, e.g. BEYOND!!! Open-mouthed smile

The source for the series can be found here on Codeplex as well as the code drop for this stage here.

Going Portable with MonoGame

As you would expect, porting Michael's solution over to MonoGame is very simple since MonoGame is just a direct replacement for XNA at present. Not all plain sailing as especially with the Windows version, not all graphical capabilities are available, usually because they didn’t make sense (a bridge too far) or where further work is still required either in SharpDX (the graphical underlay of MonoGame) or the current implementation.

First off, you had better swing over to the new MonoGame website and grab the latest MonoGame installer, they updated it in January and added a host of new features and supported platforms.

image

(Pretty Ain’t it Open-mouthed smile,I especially like the rotating images of popular MonoGame projects.)

With the new version installed, you’ll be presented with the new project templates when starting a new project:

image

As expected, the Android and Linux builds are still there, there is a new player to the party with the Ouya (be sure to check on the requirements as Ouya deployment will also need a Xamarin Android license to deploy), two features to existing users that will catch your eye is that the Windows Project has now been renamed to OpenGL (unknown as yet whether there will be a DX variant or if this is a permanent shift?), the other thing of note is that there has been significant work on the Content building side (also now supported in VS 2012 but more on that later). After much criticism, the content project has been simplified and updated to support more traditional content plus the addition of content building for the new platforms.

After you have started your new Windows project, just add an additional project for the Content to the solution and you should have a clean setup as below:

image

**Note, at the time of writing, there is an issue in the new "Windows OpenGL" project template which incorrectly references the SDL.DLL. The MonoGame team is aware of the issue and no doubt it will be resolved shortly. For now, you can simply remove the SD.dll.

As with any simple conversion project like this with MonoGame, getting started is easy, just copy over all the class files to your new project (except for the Game class, always best to port that manually, just in case), then copy over all the content from the original project in to the Content Project solution and you should have something now looking like this:

image

(*Note: Don’t forget to update the namespace in the copied files.)

All that’s left is to copy over all the Game Class code:

  • Class properties
  • Constructor code
  • Content loading in "LoadContent"
  • Game Update method
  • Game Draw method
  • And any additional helper classes / functions

All in all, it took me all of 3 minutes to get it copied across to the new solution, all that’s left is to build and link the content files and you are ready to go, well almost.

Final Steps

To finish things off, you just need to link the content to your project and sort out any incompatibilities, likely both are only minor tweaks.

For Content, I prefer to just "Link" the compiled filed from the content project but of course you can just copy the content files over to the build directory or setup a Build time task to copy them across, all methods have been mentioned before when I went through MonoGame, if you need to look back, check here.

For now, just right click on the folder named "Content" and select "Add –> Existing Item", browse to the build folder for the content project (note the Builder Project not the content project itself) but instead of just clicking on "Add", hit the down arrow next to the button and select "Link Files".

Once you have linked the files, make sure you also:

  • Change the "Build Action" of the .XNB files to "Content"
  • And the "Copy to Output Directory" option to "Copy if newer"

image

TIPS

If you cannot see the build files, ensure you have:

  • Clicked on the Content or Content builder project and selected the correct "Solution Configuration" in the  drop down, in this case "Windows"
  • Build the content project builder project by right clicking on it and selection "Build"
  • Selected "All Files" in the file browser, else you’ll see nothing Open-mouthed smile

As for fixes, in this case, I found only one in the Windows project which was to do with the RenderTargets used for the Lightning Text renderer, use a mode that is not implemented in MonoGame for Windows (strangely enough, the mode is supported in Windows 8, odd, but more on that later), to fix this, just change the following lines from:

C#
lastFrame = new RenderTarget2D(GraphicsDevice, screenSize.X, screenSize.Y, false, 
            SurfaceFormat.HdrBlendable, DepthFormat.None);
currentFrame = new RenderTarget2D(GraphicsDevice, screenSize.X, screenSize.Y, false, 
               SurfaceFormat.HdrBlendable, DepthFormat.None);

And replace it with:

C#
lastFrame = new RenderTarget2D(GraphicsDevice, screenSize.X, screenSize.Y, false, 
            SurfaceFormat.Color, DepthFormat.None);
currentFrame = new RenderTarget2D(GraphicsDevice, screenSize.X, screenSize.Y, false, 
               SurfaceFormat.Color, DepthFormat.None);

All that has changed is the "SurfaceFormat" used to prepare the render target for use from "HdrRenderable" (a High def format) with "Color" (a more basic variant).

Now fire it up and you’ll get the same experience as you saw in the previous demos, but now running smoothly in MonoGame:

image

To Be Continued

So that’s the first stage of many, sure it’s working in MonoGame but we can do better than that, first let's add a bit more and then go fully Multi-Platform.

Laters…

The source for the series can be found here on codeplex as well as the code drop for this stage here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ZenithMoon Studios
United Kingdom United Kingdom
Long-time game developer / IT maniac.
By day I architect, design, build and deliver enriching Mixed Reality solutions to clients, bringing the work of AR/VR to light in new and interesting ways, by night I Masquerade as the Master Chief of ZenithMoon Studios, my own game development studio.

At heart, I am a community developer breaking down lots of fun and curious technologies and bringing them to the masses.

I'm also a contributor to several open-source projects, most notably, the Reality Toolkit and all the services provided by the Reality Collective, The Unity-UI-Extensions project, as well as in the past the AdRotator advertising rotator project for Windows and Windows Phone.

Currently, I spend my time fulfilling contracts in the Mixed Reality space (primarily for an XR experience firm called Ethar), writing books, technically reviewing tons of material and continuing my long tradition of contributing to open-source development, as well as delivering talks, but that goes without saying Big Grin | :-D

Mixed Reality MVP, Xbox Ambassador, MS GameDevelopment Ambassador & Best selling author:

[Accelerating Unity Through Automation](https://www.amazon.co.uk/Accelerating-Unity-Through-Automation-Offloading/dp/1484295072/ref=rvi_sccl_3/262-0817396-1418043)
[Mastering Unity 2D Game Development] (https://www.packtpub.com/game-development/mastering-unity-2d-game-development)
[Unity 3D UI Essentials] (https://www.packtpub.com/game-development/unity-3d-gui-essentials)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Shaheed Legion11-Feb-13 23:55
Shaheed Legion11-Feb-13 23:55 
GeneralRe: My vote of 5 Pin
Simon Jackson12-Feb-13 13:04
Simon Jackson12-Feb-13 13:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.