XNA with VC8 (C++) Express Edition 2005
Please note: The copy-and-paste of the code is not really working (I’m not too agile around BLog tools), so some of the code snippets are coming out as ampersand-gt or something weird… So you can download the actual solution/project/cpp files from here.
First of all, why you’d want to do this (use Managed C++ with XNA) is beyond me… The reasons why I believe Microsoft wants you to use C# is also beyond me (but I’d suspect it would be something to do with being more secure such as memory managements with GC is more reliable, prohibiting unsafe code, etc). You’d most likely not be able to also use their converter tool to convert from Windows to XBox360 (when you right click on the project in C#, you get an option “Create copy of project for XBox 360″) as well as not being able to submit your mini-games to XNA for XBox, nor be able to do installer, etc. So with that said, this writing is mainly for the purpose of experimental values of “I can hack it” and no more than that.
Another thing to keep in mind is that if you’re not used to bridging codes between C++ and C#, you’d usually not realize the primitive types issues. For example, in C# the long type is 64-bits as compared to unmanaged C++ is 32-bits unless you explicitly set your VCProj with 64 bits flag, or char in C# is Unicode (16-bits) while C++ is 8-bits. For an inexperienced, whom have not gone through all these types of issues, I’d recommend you’d stick with C# for XNA until you’re comfortable with some experiences. Of course, all this is irrelevant as long as you stick strictly with Managed C++ and you don’t change any settings in your project file which may affect it. Note that all these issues are not too relavent as well on Express Edition since it doesn’t allow the solutions to mix C# and C++ (on “real” Visual Studio, you can have other projects integrate with C++, which makes it quite easy to work on C++/C# applications).
If you are on Vista, make sure you get the Service Pack for EE2005 (manually) because when you launch the “Help -> Check for Updates”, it won’t update it… By here, I will also assume that you have installed all the requirements including XNA SDK 2.x. I do not know if it is possible to just have VC2005EE, but because I wanted to see the project template for C# version which gets installed with the XNA SDK, I have both C# and C++ installed on my system.
Create a new project as “Managed” CLR based Console project:
- File -> New project
- CLR -> CLR Console Application
- Enter project name (it’s up to you whether you want “Create directory for solution” or not, I don’t like it so I usually disable it)
- Right click on the new project created and verify that this project has the “Common Language Runtime support” set with “/clr” (it should, but just in case).
Now you should have the plain old main() but with __GC based syntax (the ^ is the GC based heap). So your code currently looks like this:
int main(array ^args) { ... return(0); }
Next, you need to add (at the minimum) two references to your VCProj:
- Right click on your project and select “References” (don’t get confused with “Web References”)
- A dialog box should pop up, and here click on “Add new references” button and search for “Microsoft.Xna.Framework” (add it) and “Microsoft.Xna.Framework.Game” (add this as well).
Note that if you do not see any of the “Microsoft.Xna.*” references, you’ll need to re-install your XNA SDK (for me, I didn’t install the SP1 and so it didn’t show up, as soon as I’ve updated both C# and C++ with SP1 and re-installed the XNA SDK, these Assembly references showed up).
Now let’s add a class called Game1, similar to what gets generated from the C# template (call this calls whatever you want, but I’m just trying to match the C# templates).
- Right click on the VCProj and select Add -> Class
- Choose “C++ Class” (double click on it)
- Enter the class name “Game1″
- Enter “Microsoft::Xna::Framework::Game” for your base class name
- Make sure that “Managed” is checked
By here, at the minimal, you need to add the “using namespace xxx;” to make it able to compile. So your header file should look like so:
#pragma once using namespace Microsoft::Xna::Framework; ref class Game1 : public Microsoft::Xna::Framework::Game { public: Game1(void); };
Want to try it out (run it)? Modify your main() function like so:
#include "stdafx.h" #include "Game1.h" using namespace System; int main(array ^args) { Game1 ^ myGame = gcnew Game1(); myGame->Run(); return 0; }
See if you can compile, if all goes well, no error should occur… Now run it…
Now, let’s make it similar to the skeleton C# project so you have a base project to begin your project. For the sake of clarity, I’m going to use full namespace where possible although the line will look longer.
Here’s the header file with two overrides:
#pragma once using namespace Microsoft::Xna::Framework; using namespace Microsoft::Xna::Framework::Graphics; using namespace Microsoft::Xna::Framework::Input; ref class Game1 : public Microsoft::Xna::Framework::Game { public: Game1(void); protected: virtual void Update(Microsoft::Xna::Framework::GameTime ^ gameTime) override; virtual void Draw(Microsoft::Xna::Framework::GameTime ^ gameTime) override; protected: Microsoft::Xna::Framework::GraphicsDeviceManager ^ m_GraphicsDeviceManager; };
I’ve added Update() and Draw() (both virtual overrides) and member variable of type GraphicsDeviceManager. I’ve also added Framework::Graphics and Framework::Input so I can draw and get input for Escape key.
Next the (Managed) C++ code to go along with the header:
#include "StdAfx.h" #include "Game1.h" Game1::Game1(void) { m_GraphicsDeviceManager = gcnew Microsoft::Xna::Framework::GraphicsDeviceManager(this); } void Game1::Update(Microsoft::Xna::Framework::GameTime ^ gameTime) { if (Keyboard::GetState(PlayerIndex::One).IsKeyDown(Keys::Escape)) { Exit(); } // call the base Microsoft::Xna::Framework::Game::Update(gameTime); } void Game1::Draw(Microsoft::Xna::Framework::GameTime ^ gameTime) { if (m_GraphicsDeviceManager) { m_GraphicsDeviceManager->GraphicsDevice->Clear(Color::CornflowerBlue); } // call the base Microsoft::Xna::Framework::Game::Draw(gameTime); }
Quite simple, notice that I call the base class Update() and Draw() just like C# skeleton code.
And that is how simple it is to integrate XNA with Managed C++.
Please note: The copy-and-paste of the code is not really working, so some of the code snippets are coming out as ampersand-gt or something weird… So you can download the actual solution/project/cpp files from here.
Recent Comments