Two Headers Using Each Other

One of my early frustrations ¬†was when I couldn’t make two headers include each other, but after a while, I actually figured out that you can ‘trick’ the compiler to do so! Heres how:

You have two headers, Mesh and Shader, and the Mesh uses some of Shader’s stuff, and Shader uses a reference of Mesh. In Mesh you include Shader.h, and use it from there, but in Shader.h, you don’t include Mesh.h, instead you make a Class Prototype so you can use the class name freely, and then in Mesh.cpp, you then include Mesh.h and do stuff with it. Now the code version:
Mesh.h

#include "Shader.h"

class Mesh
{
    void Create(ShaderProperty *a);
};

Mesh.cpp

#include "Mesh.h"

Mesh::Create(ShaderProperty *a)
{
 ...
}

Shader.h

class Mesh;
class Shader
{
 Mesh* stored;

 void SetStored(Mesh* ref);
}

Shader.cpp

#include "Shader.h"

#include "Mesh.h"

Shader::SetStored(Mesh* ref)
{
 stored = ref;
 stored.foo(10);
}

I just though I would share this tip to you guys, as it could become useful!

Advertisements

CLI-C++ .net forms Game Loop

One of my past wonders was:
How can i render Direct 11(or…) on a .net form?

Its actually relatively simple, these are the steps:

When you create the project, create a new file called main.cpp, and an entrance for the program.

To start the .net form:

If you just declare an instance of the .net form inside the cpp file, it WILL go wrong, because it needs to be declared in a managed envoirement.
To do that, you need to do so:

#include <vcclr.h> // Include this one, containing a template class called gcroot.

public class ManagedGlobals
{
public:
gcroot<Editor ^> MainEditor;
};

ManagedGlobals MG;

Now you have a wrapper for your form, to start it in the appropriate way:

int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
MG.MainEditor = gcnew Editor();
//Application::Run(MG.MainEditor);

MG.MainEditor->Show();
while (MG.MainEditor->Created)
{
Application::DoEvents();
RenderFrame();
}
return 0;
}

As you see i have a while loop simply saying:

While the editor(in this case) is open, do the following:

Let the application(editor) do all the stuff it has to do right now, check for input blablabla…
Then after that render the frame.

To render in a, lets say a frame, we can do so:

–In the startup of the editor–

HWND pHandle = (HWND)RenderPanel->Handle.ToPointer();
InitD3D(pHandle);
InitGraphics();

Here we’ve got the handle of the panel, and sent it to a function called InitD3D.

InitD3D:

void InitD3D(HWND hWnd)
{

….

// In the DXGI_SWAP_CHAIN_DESC

scd.OutputWindow = hWnd;

…..

}

Then for each of the frames, the panel is treated as a window, meaning that the output will appear in that panel.

Good Luck!