# Rendering 2D

This topic is 1867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I have question about rendering in general. I have windows or a HUD if you want... how do I handle telling the renderer to draw the informaiton for each window? should they just draw themselves? I'm not sure how to handle turning window specific information into a form that can be handled in a general sense by the renderer.

Another way of putting it:

I want the window to "tell" the renderer to draw it at a position and then to draw its contents, whatever they may be.

Dont know where to start and I'm just scratching my head thinking about it.

Thanks,
Nick

##### Share on other sites
A window should know it's own location. It's children should know their own location (relative to the window's topleft corner).
When told to draw, a window draws itself, then tells each child to draw themselves, and so on.

A window is a widget, and buttons, text labels, line edits, etc... are all also widgets. Thus, you have a tree hierarchy of abstract widgets that are overrided as windows, buttons, menus, and other GUI elements. Each widget draws itself at the parent's absolute pos, then tells each other widget to draw themselves.
The same thing happens with events, but in reverse. A mouse click gets passed (untouched) from parent to child, until the bottom-most child gets it. If the bottom-most child decides to process the click, then afterward the event is discarded. Otherwise, the second-to-the-bottom child gets an opportunity to process the click and discard, and so on.

This is a very common way GUIs are handled. Almost everything is a widget. Sometimes a 'widget' is also called a 'window', like in Win32 - which in Win32, a button IS-A window. But I think it's better to use the term 'widget', because it separates out the 'abstract' concept from the specific idea of a application window and pop-up windows that contains buttons and other elements.

##### Share on other sites
How do you store these Windows? When you perform your rendering phase, you will have to loop through all the windows and draw them; I would suggest having a Draw() function each window has, and it handles drawing itself since it knows where it is, and what all it needs to draw. Something like this

 // Let's assume you put all the windows into a vector of TWindows, and they have a Draw() funciton std::vector<TWindows> Windows; // in main rendering loop void Render() { // Draw all object in game 1st since HUD sits on top of the game for (int i = 0; i < Windows.size(); ++i) { Windows.Draw(SurfaceToDraw); } } 

##### Share on other sites
Servant of the Lord-

Thanks for the info I wasn;t sure how they were handled. The hierarchy definitely makes sense, hopefully I can put it to good use!

BeerNutts-

Yeah, basically I have a container that holds them in my renderer object. However, I am handling sprites and such by reading an xml file to load the appropriate data. so the "playerCharacter" doesn't actually hold is sprite it holds the ID of where it is located in the list of loaded images. the image is drawn @ the playerCharacters location. Thats how my tile map works as well loads the tilemap xml and adds the resources and reads what texture to use based on a textureID. Right now I am just rendering rectangles to get a feel for displaying the data and getting everyhting working right before I start using images and such for the widget/window Appropriate?

Thanks,
Nick

##### Share on other sites
Nick,

There are many different ways to solve this kind of problem, but I'm of the opinion the individual entities should handle drawing themselves, since they have all the information, and you don't have to share it. And, if you take the path of having the windows draw themselves (which would be best since these "windows" could have different data that it has to display; images, text, etc.), but keep the characters drawn externally, it's a clash of styles.

It's fine how you load the images, and the character only stores an ID for the image it is using, but that doesn't preclude you from still having the character (and all other on-screen entities) draw themselves.

You are limiting how you can draw the player; will your player ONLY ever be the image it's assigned to? Will he not have special FX as well (blinking, glowing, transparent, wearing different items)? If so, then you're going to be writing a lot of code outside the player or other objects to get every possible status. However, if you handle the objects drawing themselves, it will all be contained within the draw:
 void Player::Draw() { uint8_t Alpha = 255; // if our player is invisible, set the alpha small so he looks transparent if (IsInvisible) { Alpha = 50; } // Draw the image we currently are using RenderingEngine->Draw(CurrentImage, Alpha, LocationX, LocationY); // If we have a powerup, create a glow around the player if (HasQuadDamage) { RenderingEngine->CircleGlow(LocationX, LocationY, COLOR_BLUE); } ... } 

As an example, you can check out my Old Blog (see the link in my Sig) for a simple game I made. You can also download the source from here

Good luck and have fun!