C++ | DirectX | Class system ?!

Started by
36 comments, last by Mate 9 years, 4 months ago

Hello,

just to justify my question I have to say, that I moved from Java to C++.

Now my problem is, that I have some objects in my DirectX game. They are drawing fine, but all the code I have for the objects are in the main.cpp class.

And thats a problem for me.

Now in Java, I could create a class and just write all the objects code in there and then just create an instance of it, and call the draw method of the objects class in draw.

....

Object object = new Object(blablabla);

..

void draw()

{

...

object.draw();

...

}

Now my question is how to do this in C++/ DirectX ?

As you have all these COM objects, that only can be used in the main class and you have to bind it all to buffers and then draw the buffer.

How could I make this constructor based ? So like the Java exaple ?

A code exaple would be a great help :-)

Advertisement

I'm not really sure what exactly you are trying to do, but from what I get you just want to have a class encapsulate things. Something like this?:


class Mesh
{
private:
	XMFLOAT4x4 m_WVP;
	ID3D11Buffer* m_vertexBuffer;
	ID3D11Buffer* m_indexBuffer;
	ID3D11EffectTechnique* m_drawTech;
	//etc.
};

class Renderer
{
private:
	ID3D11Device* m_d3dDevice;
	ID3D11DeviceContext m_d3dImmediateContext;

	void draw(Mesh& mesh);
	//etc.
};

class GameApp
{
private:
	std::vector<Mesh*> m_meshes;
public:
	Renderer renderer;

	void renderWorld();
	//etc.
};

void GameApp::renderWorld()
{
	for (auto iter = m_meshes.begin(); iter != m_meshes.end(); iter++)
	{
		renderer.draw(**iter);
	}
}

In C++ you have header(.h) and implementation files(.cpp, .c, .cxx) you need to create these files yourself and add them to the project. In the header file you specify how the class looks, so which methods and variables it has, in the cpp file you actually implement the methods, this is split from how Java or C# does it. When you want to use a class thats defined in another file you include the header file , with a #include statement, into the file where you want to use the class name.

Be careful with including headers in other headers because you can create loops and the compiler can't deal with that. there are other ways to deal with that, forward declarations, but this is a more advanced topic.

PS you can use COM in other files as well they dont have to be in the main.cpp file, the main.cpp is usually just the file that defines and implements the main function so the application can start.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Thank you for the awnsers !

I got the header system before. My problem is, that I don't exactly know how to make objects/ meshes with an constructor in C++.

Because I just want to say for example

...

MeshLaptop laptop;

...

.

laptop::define(bla bla bla);

.

...

void draw()

{

...

laptop::transformRotateDraw(bla bla bla);

...

}

But this didn't worked for C++/ DirectX because of the buffer and this confusing stuff.

So I want to make a lot of object instances in a class and draw them over a method. As easy as possible, without a big mess in the main.cpp.

Is that even possible in C++ ? :|

http://www.cplusplus.com/doc/tutorial/program_structure/

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Ok, I had a lession in this before, but that shouldn't work for my case, because of the COM object system, right ?

There are a lots of things, the device needs to work with. And this doesnt work class-global, or ?

I still don't get what your issue is. What precisely is your issue with COM objects?

Can you elaborate a bit more, rather than just saying "this confusing stuff":

But this didn't worked for C++/ DirectX because of the buffer and this confusing stuff.

In your example you want the laptop to draw itself - that's bad by design, let your renderer draw the laptop. Check the example I gave you - you have some class Mesh containing its vertex & index buffers, its wvp matrix, a pointer to the draw technique it will use etc. Your renderer given this mesh should be able to draw it (the draw(Mesh& mesh) method). You should store all the meshes you have in some structure, and when you want to draw all of them, you can just iterate over this structure and call draw for each of them. To draw a mesh you usually need the d3d device and device context + the mesh's vertex and index buffers - you could store a pointer to the d3d device and device context in the renderer and the model specific data in the mesh; there could be additional data like textures, draw technique, material, render states etc. Here's something you might find useful: http://rastertek.com/tutdx11.html

I still don't get what your issue is. What precisely is your issue with COM objects?

Can you elaborate a bit more, rather than just saying "this confusing stuff":

But this didn't worked for C++/ DirectX because of the buffer and this confusing stuff.

In your example you want the laptop to draw itself - that's bad by design, let your renderer draw the laptop. Check the example I gave you - you have some class Mesh containing its vertex & index buffers, its wvp matrix, a pointer to the draw technique it will use etc. Your renderer given this mesh should be able to draw it (the draw(Mesh& mesh) method). You should store all the meshes you have in some structure, and when you want to draw all of them, you can just iterate over this structure and call draw for each of them. To draw a mesh you usually need the d3d device and device context + the mesh's vertex and index buffers - you could store a pointer to the d3d device and device context in the renderer and the model specific data in the mesh; there could be additional data like textures, draw technique, material, render states etc. Here's something you might find useful: http://rastertek.com/tutdx11.html

I like the idea of making some sort of a buffer for the final data. But the index buffer wouldn't work anymore when I put mulitple meshes in only one buffering structure ...

I mean if I had 3 vertex data buffers and I would translate and rotate them, then I could sent it all to one final buffer, that would get drawn in the rendering class. But this shouldn't work for the index buffer ? Because when I had 3 vertex data holders for each mesh and I would stick them together, the index numbers would raise...

So I mean that the vertex number 2 from the first index buffer is not the same as the vertex number 2 from the second vertex buffer ...

Do you have a solution for that ?

I like the idea of making some sort of a buffer for the final data.

Lest you want to do some optimizations, for now don't do only one buffer. Later you could do it (then you'll just add an offset per index buffer), but as a beginning try with a buffer per mesh. Basically try to go from simple to hard - make the simpler code work, then you could try optimizing it.

Lest you want to do some optimizations, for now don't do only one buffer. Later you could do it (then you'll just add an offset per index buffer), but as a beginning try with a buffer per mesh. Basically try to go from simple to hard - make the simpler code work, then you could try optimizing it.I like the idea of making some sort of a buffer for the final data.

Thank you ! :)

The point, why I didn't make multiple buffers was, that I'm worried about the performence. I'm really paranoid about the performence.

Just one thing left : Could you please give me a simple example of an constructor based mesh ? So that I can create multiple instances for a mesh with only a bit of code. Thats what I'm trying to do all time :(.

I have not much experience wiht C++.

So please don't just post a link of some tutorial, thats never the best way.

I also know the header thingy aswell. The problem is, that the constructor system/ the whole class system is very different to Java.

This topic is closed to new replies.

Advertisement