# Component-based Rendering

I have component Renderer, that Draws Texture2D (or sprite) Then I have method OnUpdate, and there should be my rendering code, something like

spriteBatch.Draw(Texture, Vector2.Zero, Color.White)


But first I need to do spriteBatch.Begin();. Where should i call it? And how can I make sure it's called before any Renderer components OnUpdate method?

(i need to do more stuff then just Begin() i also need to set right rendertarget for camera etc.)

• "According to component-based architecture i should have only method OnUpdate" Who says that? – Paul Manta Feb 13 '12 at 17:13
• I don't know, i read it somewhere, originaly i had OnDraw method and then called it in RendererManager in right time, but i read in some ppt that they had just OnUpdate in some Attributes/Behaviour system and it worked, so I wonderer how. – Kikaimaru Feb 13 '12 at 17:17
• I'm not going to give you complete answer, but: do the rendering OUTSIDE of the component. The component is a component which tells the rendering system what to draw and how to draw it, but it's not the component that draws itself. – TomsonTom Feb 13 '12 at 17:17
• Duplicate question: gamedev.stackexchange.com/questions/27172/… – Sean Middleditch Apr 14 '12 at 2:51

How I like to go about this is, instead of calling spriteBatch.Draw(...) right in your component's OnUpdate method, I submit it to a rendering system and draw every object that has been submitted after they have all been updated.

So inside your component you could have something like this:

if( inViewOfCamera() ) {
submitToRenderSystem(spriteBatch);
}


Inside your render system you will build up an array of spriteBatch objects that you need to draw. Then, after all of your objects have been updated, you can call the draw method on your render system, which could look something like this:

SpriteBatch.Begin();
for( int i = 0; i < renderObjects.size(); i++ ) {
renderObjects.get(i).Draw(...);
}
SpriteBatch.End();

• This is how I would do it. +1 – jmegaffin Jan 9 '13 at 20:06

It sounds like you have every component type potentially in a single list.

I would suggest that you split the lists up so that like components are in their own list and potentially within their own management system. This would then allow your manager to do any sort of global prep work, like call .Begin() before then processing (OnUpdate()) all of its components.

Also, remember that just because everything is a component it does not mean every component has Only the component interface to its data and/or logic. The Update call to a Rendering component for example might just simply update animations if need be before the draw calls are done.

• I have Managers that get message when component is created and they can store components in their internal lists, and until now i had RendererManager that stored all RendererComponents and then draw them, but i was wondering if there isnt some other method... – Kikaimaru Feb 13 '12 at 17:36
• @Kikaimaru If you ever find yourself trying to decide between good theory and good/practical design, always go with design over theory :) – James Feb 13 '12 at 17:38

When you're using a component based model, you have two real options when it comes to the individual components.

• Keep track of all components in one massive "Components" list (or array, or dictionary, etc.), calling OnUpdate() on each one sequentially, in turn.

• Group the components by type or usage, and call the groups you need, in turn.

I'm oversimplifying, clearly, but I hope it's obvious that the latter is much more useful than the former.

Either way, you need to both keep track of all of the components, and update each of the components. It sounds like you're already using a "Subsystem" kind of class that handles your individual render components. Great! You're well on your way to the second bullet point. As long as your Render Subsystem knows how to access the render components you need, you can start the spritebatch, update your components en masse, and then end it.

I wrote a component-based render system myself, and this was all the code within my Draw() call (in my case, my GraphicsSubsystem extended XNA's DrawableGameComponent class, which means it has both an "Update" and a "Draw" method):

 public override void Draw(GameTime gametime)
{
sprites.Begin();

foreach (var pair in graphicsDict)
{
//only draw them if they're set to visible
if (pair.Value.visible)
{
pair.Value.draw(sprites);
}
}
sprites.End();

base.Draw(gametime);
}


Where GraphicsDict is a GUID,RenderComponent Dictionary that holds all of the components, and I pass in sprites, the SpriteBatch as a parameter. This way you can do all of your rendering that requires certain batch settings at once, and you don't have to worry about switching between settings between the drawing of the individual components.