C++ | DirectX | Class system ?!

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

I'd advise you make a factory to which you can delegate the creation/deletion of meshes. And you'd probably want only a few functions/methods to be able to create a mesh(example MeshFactory::createCube(), MeshFactory::loadMesh(char* filename), MeshFactory::createSphere(unsigned int segments)).

Basically something like this:


class Mesh
{
    //various variables go here - the Mesh may have an empty constructor
}

class MeshFactory
{
private:
    std::map<ID, Mesh*> m_meshes; //keep track of meshes by id
private:
    //various details
    //...
    ID createMeshFromBuffers(const std::vector<Vertex>& vertexBuffer, const std::vector<UINT>& indexBuffer)
    {
         //you'll need the d3d device to create the ID3D11Buffers from the given buffers
    }
public:
    ID createCube()
    {
        //make the vertex and index buffer vectors
        //...
        return createMeshFromBuffers(vertexBuffer, indexBuffer);
    }
    //etc.
}


You could choose a different architecture of course, there are plenty of books out there that use various architectures when it comes to a 3d renderer.

I have not much experience wiht C++.

Well I'd advise you get some experience with C++ before going into 3d. I don't believe it's a good idea to be studying 3d rendering in C++ if you don't know well enough the language. Rastertek has some pretty good tutorials, other things would be Frank Luna's book, Jason Zink's book, Jason Gregory's book, but as I said - before you delve into 3d with C++ and direct3d you might want to take some time to study C++.

Advertisement

Well I'd advise you get some experience with C++ before going into 3d. I don't believe it's a good idea to be studying 3d rendering in C++ if you don't know well enough the language. Rastertek has some pretty good tutorials, other things would be Frank Luna's book, Jason Zink's book, Jason Gregory's book, but as I said - before you delve into 3d with C++ and direct3d you might want to take some time to study C++.

So .. I'm acutally working on the new class system in the moment smile.png. Like a render class, mesh classes(but I'll change to factory soon).

My only problem is, that I can't use, for example, the device context over a class. It's giving me the error "A non static member has to be relativ to an object". Now ... I translated the error, but it's something like this.

Hmm ..

my problem is not the 3D space. I worked on game projects before. On big projects. But I only worked with Java + OpenGL. Now C++ and DirectX is .. kind of new for me. And thats my only problem. The structur and syntax is VERY different.

So how could I use the COM objects over classes ?

EDIT: The error is fixed. Now .. is there now better performenced way to use a rendering part, than creating an object of it ? :|
I mean it can't be so good for the performence to create instances of classes, or ?

I mean it can't be so good for the performence to create instances of classes, or ?

Why do you actually have this impression ? Have you profiled any bottleneck in this regard ?

The only thing I can think of you can do wrong is creating (and destroying) buffers/textures and whatnot every frame. You usually create everything you need at app startup (or, in the context of game, at level beginning). If you need to change something you have Map/Unmap or UpdateSubresource.


Why do you actually have this impression ? Have you profiled any bottleneck in this regard ?

The only thing I can think of you can do wrong is creating (and destroying) buffers/textures and whatnot every frame. You usually create everything you need at app startup (or, in the context of game, at level beginning). If you need to change something you have Map/Unmap or UpdateSubresource.

So I sayed that I moved from Java + OpenGL and I sayed that I like Java and OpenGL ways more then DirectX and C++.

So don't you ask why I actually moved when I don't want ?

It's because I had already a game on my own Java OpenGL engine. It was good for my impression.

But yeah .. it is running on a i7, 8gb ram and a really good graphics card with only 8-12 fps.

So after all I realized that I have to move. And now the paranoia of bad fps is still in my neck.

It's because I had already a game on my own Java OpenGL engine.

Bad optimization on your part most probably - Java is not actually that bad. On the other hand do not try to optimize things before you have at least the basics down - http://c2.com/cgi/wiki?PrematureOptimization

I agree. You switched API and language in the hope it will solve your performance issues. Nonetheless you're not sure it actually will. Then why switch at all.

I ask again: Have you profiled your code, with a profiling tool or manually ? And no, looking at FPS alone is not profiling. Do you use graphics profiling tools ?

How many draw calls do you have ? How many state changes ? And in the context of Java: Do you produce massive amounts of garbage each frame ? Also your graphics driver can make troubles: I hear that sometimes OpenGL drivers fall back to software rendering, if you use a feature unavailable in hardware.

If you just transliterate from Java/OpenGL to C++/DirectX you very likely have the same performance.

I agree. You switched API and language in the hope it will solve your performance issues. Nonetheless you're not sure it actually will. Then why switch at all.

I ask again: Have you profiled your code, with a profiling tool or manually ? And no, looking at FPS alone is not profiling. Do you use graphics profiling tools ?

How many draw calls do you have ? How many state changes ? And in the context of Java: Do you produce massive amounts of garbage each frame ? Also your graphics driver can make troubles: I hear that sometimes OpenGL drivers fall back to software rendering, if you use a feature unavailable in hardware.

If you just transliterate from Java/OpenGL to C++/DirectX you very likely have the same performance.

Yeah, thats true. The language at it's own can't change the performence. You actually could programm it the same way. But Java/OpenGL doesn't give you the same possibilities for the rendering part. What I write in C++ in 100 lines, I properly write in Java within 30 lines. This makes it more comfortable for me, but you can't interact the same in 30 lines, than in 100 lines. Like I never used a complicated buffer system in Java, in C++ I do.

I mean .. I had buffers, but not this amount and type of.

So the thing is, that you can write the same game in C++ with better performence. Now that my game is really performence eating, I prperly have to choose the way I don't like. But when I learned one of business then it's that the way you don't like is the way, where you CAN raise the most money and experience of.

EDIT:

I started rewriting the code today. Now I want to make 100% clear, readable and less linked code. And I'm building in a system to precisionly detet errors and avoid them quickly and efficent. :-) When I'm finished, I optimize it like you guys said so thanks at all. I think, 2 days ago, I wouldn't even think at rewriting anything.

And to the upper part : Java isn't even clearly made for games. DirectX is a game library, OpenGL is a graphics library. Another reason, why DirectX has better performence.

I started rewriting the code today. Now I want to make 100% clear, readable and less linked code. And I'm building in a system to precisionaly detet errors and avoid them quickly and efficent. :-) When I'm finished, I optimize it like you guys said so thanks at all. I think, 2 days ago, I wouldn't even think at rewriting anything.

It's certainly better to have readable and well architected code. I'd recommend you check out design patterns: http://www.gameprogrammingpatterns.com/, http://sourcemaking.com/, http://en.wikipedia.org/wiki/Design_Patterns

Java isn't even clearly made for games. DirectX is a game library, OpenGL is a graphics library. Another reason, why DirectX has better performence.

C++ isn't "even clearly made for games" either. Sure it provides greater freedom, but it's not "better" than Java. And I don't believe that DirectX "has better performance" than OpenGL.

Java isn't even clearly made for games. DirectX is a game library, OpenGL is a graphics library. Another reason, why DirectX has better performence.

C++ isn't "even clearly made for games" either. Sure it provides greater freedom, but it's not "better" than Java. And I don't believe that DirectX "has better performance" than OpenGL.

Ehm sorry ... I wanted to say OpenGL and not Java sad.png. Gives a new definition of the sentence. biggrin.png
Sure DirectX has better performence IN GAMES. DirectX is made for games. OpenGL is not made for that. As they created it, there main goal was the graphics, not the performence.

And I never sayed that C++ is better. I sayed, it's the better chose for a demanding game. And C++ on it self doesn't make the difference, DirectX does. But DirectX is only avalable for C++ so .. you have to use C++ with DirectX.

And even when it wouldn't be forced, C++ has more freedom and more freedom means more way to enhance the performence, in this case.

So then I'll take a look at the patterns ..

EDIT :

Is there any structure in C++ compareable to the "List" structure from Java ?
I could need something like this :)

So the main attributes are that you don't have to set a final amount of index values, you can add items of the same type by some command

(in Java it's " listname.add[buffer3] " for example), and you could get the size of the structure by " listname.lenght "

(I know, that there is a way to get it in C++ anyway).

Sure DirectX has better performence IN GAMES.

Where did you read this?

And even when it wouldn't be forced, C++ has more freedom and more freedom means more way to enhance the performence, in this case.

More freedom can also mean an easier way to break things + some things require more effort to do in C++.

Is there any structure in C++ compareable to the "List" structure from Java ?

You are looking for the stl library - that's why I said learn c++ before trying to code a 3d engine on it. std::list would do the job. Check out this: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

You might find this useful too: http://www.cplusplus.com/reference/stl/

This topic is closed to new replies.

Advertisement