DirectX Style Engine Design Questions: Object Drawing and Drawcalls..

Started by
3 comments, last by NightCreature83 10 years, 7 months ago

Hi,

I used to spend a lot of time messing around with xna then i realized that i needed to get some some professional education in Software Development so i put xna on hold... Now, i want to start doing some more projects with my now improved knowledge.

The way i used to approach drawing objects on the screen was to have something like this (doing this off the top of my head)

Class Sprite : Game
{

bool drawable = false;

Public override Draw()

{

//Draw Something

}

}

Class Manager
{

List<Sprite> sprites = new list<Sprite>();

foreach s in sprites

{

if (s.drawable)

{

s.Draw();

}

}

}

So all the sprites would draw themselves if their flaged as drawable...

Now i'm looking at doing some Directx work using SharpDx purely as learning experience. But im wondering if this technique would work or even if its the "Way its done" when it comes to directx.
Im also under no illusion that there is much more involved with directx, But as a concept is this how its done?

I hope people can interpret what i'm asking...

Thanks in advance,

Mell.



Advertisement

It totally depends on the scope/scale of things and your goals. DirectX, though I haven't used SharpDx is in essence not all that different than XNA from what I'm aware of, if you take all the high-level abstractions away. Yout create your resources, set a bunch of states, bind those resources and issue a draw call. Things get a bit more complicated when you start using shader instead of the fixed function pipeline (which I'd recommend), but practically, your approach would work. Is this what you want, then go with it, you can do all that in each class that describes a renderable object. I personally wouldn't consider it a good design though, since it isn't flexible at all. As an advanced system, you would e.g. build a low level rendering system with render commands and queues, as thoroughly described in this thread (pages 2+ specially), and probably apply an entity/component-like system on the very top of things. Its certainly a hell lot of work to get things running, but once you have it, its totally satisfactory to use (if integrated right).

Two more things though, I'm uncertain if this is just because its sample code, but:

- class Sprite : Game ... so Sprite derives from Game? So your sprite IS a game? Inheritance is normally an IS-A relationship, so eighter this is a slight naming issue (Drawable instead of Game), or there is some serious design flaw going on I'd encourage you to look at more closely.

- class Manager : there is some discussion about whether "manager" classes are too wide-ranged, unspecific and bloated, as IMHO, I'd agree that those classes often end up doing to much work, but this is up to you to decide. However, I assume too that this should really be DrawManager/RenderManager, since otherwise this would be one unspecific, possible monster class, and I'd suggest splitting that up a little bit too.

Hope that helps, and that those are thing you'd wanted to hear? (Your questions are a bit wague, so I'm not sure).

It totally depends on the scope/scale of things and your goals. DirectX, though I haven't used SharpDx is in essence not all that different than XNA from what I'm aware of, if you take all the high-level abstractions away. Yout create your resources, set a bunch of states, bind those resources and issue a draw call. Things get a bit more complicated when you start using shader instead of the fixed function pipeline (which I'd recommend), but practically, your approach would work. Is this what you want, then go with it, you can do all that in each class that describes a renderable object. I personally wouldn't consider it a good design though, since it isn't flexible at all. As an advanced system, you would e.g. build a low level rendering system with render commands and queues, as thoroughly described in this thread (pages 2+ specially), and probably apply an entity/component-like system on the very top of things. Its certainly a hell lot of work to get things running, but once you have it, its totally satisfactory to use (if integrated right).

Well, i eventfully want to have better, faster, more efficient algorithms and designs but if you recon this approach is plausible for the time being i can use it as a crutch..

At the moment i'm still only experimenting in 2D, but when i start with 3D experimentation i would very much like a thorough understand of the best practices.

Two more things though, I'm uncertain if this is just because its sample code, but:

- class Sprite : Game ... so Sprite derives from Game? So your sprite IS a game? Inheritance is normally an IS-A relationship, so eighter this is a slight naming issue (Drawable instead of Game), or there is some serious design flaw going on I'd encourage you to look at more closely.

Its more complicated than the code i written above, but eventually something will inherit from game. Game being the main component in xna that has: "initialize", "Update" And "Draw" methods.

If i passed game as a parameter in the constructor, or passed it by some other means i could not invoke and override game.Draw() and not take advantage of polymorphisim. Unless i'm missing something?

I know inheritance is usually an "Is-A" model, but the way XNA seems to be constructed they kinda bend this rule abit..

- class Manager : there is some discussion about whether "manager" classes are too wide-ranged, unspecific and bloated, as IMHO, I'd agree that those classes often end up doing to much work, but this is up to you to decide. However, I assume too that this should really be DrawManager/RenderManager, since otherwise this would be one unspecific, possible monster class, and I'd suggest splitting that up a little bit too.

Hope that helps, and that those are thing you'd wanted to hear? (Your questions are a bit wague, so I'm not sure).

Yeah that's kinda the bulk of my question, If i had something that iterates through all the drawable objects in a level, Then invokes a draw method in a super class accordingly, wouldn't that be completely inefficient?

Seems you have pointed me in the right direction of some healthy alternatives so ill give them a read, shortly.

Although i know my way around code better, Directx and game programming concepts i'm still not too sharp on. Most of the information on directx programming is pretty much "how to draw a triangle" ect, but i haven't found any real "meaty" discussions on actual engine design, i'e how to draw a scene of triangles with different states.

When you leave the drawing to the individual object to handle, then you're limited to draw for only that object. It does make thing easier to read, organize and understand. But with the manager with more information on multiple objects, it can organize to draw many in one swoop. Imagine if you have 1000 bullet sprites on screen, you could have individual bullet draw itself or have the some render manager issue a single draw call to draw 1000 quads with the same texture at 1000 locations. I hope that I interpret what you're asking correctly.

First of in XNA you will want to inherit from GameComponent or DrawableComponent in C#.

Generally it is not a good idea to have a game object draw itself, it doesn't need to know any of that information, it can have a model object and that can know how to draw itself or generate a list of render commands to be drawn. I generally prefer the second method as it allows you to do some sorting and other optimisations in the render passes, like sort on material, shader, texture or even transparency, but also batching of vertex buffers that have same render settings. But also visibility querries and the like, early culling.

If using scenegraphs use a visitor pattern to collect all of the draw commands you need to pass on to the renderer.

In rendering you want to minimize the amount of state changes you need to do to be able to draw a complete scene to the screen, state changes are things like: switching texture, constant buffers, shaders, ibs and vbs, renderstates and rendertargets.

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

This topic is closed to new replies.

Advertisement