Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: My petty dillema: Separation of graphical drawing of UI objects  (Read 4320 times)

0 Members and 1 Guest are viewing this topic.

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
In my new project, I have started to seperate the graphic drawing from the ingame objects ( i.e. an object contains the position, size, velocity ect... and a Graphic class contains sf::sprite)

My dillema is, does the same code design should also apply to the UI objects, like mostly stationary buttons and labels?

I can understand such logic for game objects, where they would get constantly updated and changed and should not be burdened by all the drawing components, but for UI objects, I do not view them as complex enteties, and I wonder should they contain the drawing components and draw themselves?

Sorry if the question is kinda noobish to you guys. ;D

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: My petty dillema: Separation of graphical drawing of UI objects
« Reply #1 on: June 03, 2016, 10:27:20 pm »
Hi.  I personally dont see whats wrong with having game objects draw themselves.  In fact all of mine do.  It sounds like you may be trying to optimize too early but example code could help in understanding your concern.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: My petty dillema: Separation of graphical drawing of UI objects
« Reply #2 on: June 04, 2016, 03:57:09 pm »
I think it would just be a design choice. There may be very little benefit in separating a user interface element from its visual representation. In fact, you may consider that the element itself should be a drawable one; that's what it is - a display element, a graphic.

I do agree with separating for the main objects though, although this is not required. It allows for a number of benefits including using proxies for collision and ability to batch graphics.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: My petty dillema: Separation of graphical drawing of UI objects
« Reply #3 on: June 04, 2016, 08:14:54 pm »
Hi.  I personally dont see whats wrong with having game objects draw themselves.  In fact all of mine do.  It sounds like you may be trying to optimize too early but example code could help in understanding your concern.

When I was starting out with SFML, I too was writing objects in a way that they could draw themselves. It is indeed more convinient for a beginner to write code like that, but it becomes more limitating as the code becomes more complex. Maybe it can affect preformanse significantly, but my projects were never that much demanding, so I can not tell. :(

In this project I wanted to change my code desing to be more modular, so the classes should not contain the more variables and function than they need.

Oh, and this is how I consider separation of drawing from objects:
(click to show/hide)

Quote
There may be very little benefit in separating a user interface element from its visual representation. In fact, you may consider that the element itself should be a drawable one; that's what it is - a display element, a graphic.

Exactly what I tought.

Maybe I should rephrase my question: Should my code design be the same for all elements, or should I make exeptions where applicable, in case the code design would make things more complex that necessary?

Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Re: My petty dillema: Separation of graphical drawing of UI objects
« Reply #4 on: June 05, 2016, 04:19:50 am »
In this project I wanted to change my code desing to be more modular, so the classes should not contain the more variables and function than they need.

Thank you Brax for the code example, now I understand your point much better.
I think I may want to investigate the method you are talking about as my code gets more complex.

Recoil

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: My petty dillema: Separation of graphical drawing of UI objects
« Reply #5 on: June 05, 2016, 04:49:09 am »
Just throwing this out there because I just finished reading this: Game Programming Patterns

It really helped me get a better feel for how I was designing my engine, and how to better setup the architecture.  Following along the best I can with the code example, it looks like you are simply trying to break up the objects rendering from the object.  The section Design Patterns Revisited may be of interest in this situation, but the whole book is really good...and it's free to read ;)

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: My petty dillema: Separation of graphical drawing of UI objects
« Reply #6 on: June 05, 2016, 12:13:06 pm »
i have tried it once, i was expected at least those classes would be an independent. but  i found myself i have to find a way for those classes class Graphic and class Object to be interacting. although, i ended up with some thing like Graphic::update and object::update which is, IMO, pointless.

i come up with a design that allow me to store update functions (logic parts) and render functions in arrays. c++ offers inheritance and virtual function to do it. but it has performance drawback. however, it can be done by help of Boost.Variant/Boost.Any + Visitor Pattern. for me i just created those boost classes from scratch for self-teaching as shown here. with simple example how it works.
« Last Edit: June 05, 2016, 12:29:04 pm by MORTAL »

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: My petty dillema: Separation of graphical drawing of UI objects
« Reply #7 on: June 05, 2016, 08:05:15 pm »
Alright, thanks everyone for writing, and thanks recoil for the link, I will read it whole. :)
I will go with the route of less ressistance and combine the UI objects with drawing components. I do not intent to make them complex, so they can live with it.  :P

Quote
but  i found myself i have to find a way for those classes class Graphic and class Object to be interacting. although, i ended up with some thing like Graphic::update and object::update which is, IMO, pointless.

You do need to compensate for decoupleing objects from drawing. It may look pointless to write two functions that do almost identical thing, but as I said, the perk of decoupleing is to make smaller objects, making them easier to manipulate.

As for their mutual interaction, that is why my objects have an ID variable (simple unsigned int) within them, so the graphic class could know which sprite (or sf::RectangleShape) belongs to which object. It is indeed obvious to me that this aproach can have preformanse impact - graphic class first needs to find a coresponding ID within the std::map before it can update the spirte, and that thing alone can be bad.

But I will not bother with optimisation just yet. I will do so when my FPS drops bellow 30 with 50 drawn objects. In release mode. ;D