Why does this not work (calling xna from c++)
// Hi,
// Linker option is /clr, entrypoint "main". Don't forget to add the required references. Heres the code :
// file "main.cpp"
usingnamespace Microsoft::Xna::Framework;
usingnamespace Microsoft::Xna::Framework::Content;using
namespace Microsoft::Xna::Framework::Graphics;namespace
SimpleGameLoop{
ref
class Game1 :public Microsoft::Xna::Framework::Game{
public
:Game1()
{
hGraphics =
gcnew GraphicsDeviceManager(this);hContent =
gcnew ContentManager(Services);}
protected
:virtual
void Initialize (void)override{
Microsoft::Xna::Framework::Game::Initialize();
}
virtual
void Update(GameTime ^hGameTime)override{
Microsoft::Xna::Framework::Game::Update(hGameTime);
}
virtual
void Draw(GameTime ^hGameTime)override{
hGraphics->GraphicsDevice->Clear(Color::Black);
Microsoft::Xna::Framework::Game::Draw(hGameTime);
}
virtual
void LoadGraphicsContent(bool loadAllContent)override {
if
(loadAllContent){
}
}
virtual
void UnloadGraphicsContent(bool unloadAllContent)override{
if
(unloadAllContent){
hContent->Unload();
}
}
private
:GraphicsDeviceManager ^hGraphics;
ContentManager ^hContent;
};
void
main(void){
Game1 ^hGame =gcnew Game1();
hGame->Run();
}
}
Thx, I corrected the function name. But still no luck, im not very deep into managed code at the moment, could it be something with the memory allocation in the garbage collection, or with the function declaration (missing sealed, etc.)?
To comment how this problem arose, although I like c# I like c++ even better cuz of the ability of goin to the bone (including the 3D max libraries, testing a bit of DirectX10, etc.), so I decided to give it a try but couldn't find any documentation on the Inet, and as a hobbiest its difficult to find bugs that you aren't notified by the compiler or linker ^^. But I guess if someone has the answer on it its on this forum.
Thx, it works finally (the function draw was not called)! I adjusted the the code in first post, so you can see how to call XNA from c++ now. So XNA is in fact not only c# (eventhough i start to think c# is better).
You can just copy paste the whole first post into a .cpp and see that it works (don't forget the references and the linker options)
Linker options: (minimal)
Entrypoint main [ /Entry:"main"]
for managed code [ /clr ],
can be [ /clr:pure ] if you only use managed code, but then you have to define
[ /subsystem:windows ] or [ /subsystem:console ]
if you specify [ /subsystem:windows ] the console window will no longer show up
References: (minimal)
Microsoft.Xna.Framework
Microsoft.Xna.Framework.Game
(optional) Microsoft.Xna.Framework.Content.Pipeline
(optional) all other references