views:

1054

answers:

5

We're building a method of connecting components visually via a GUI in a Visual C++ application (MFC). Simple things like clicking on boxes and drawing lines between those that are connected, and storing information on them. The problem is that we're making all this by ourselves from the ground up in GDI and it quickly becomes a heck of a lot of code to maintain.

Are we really reinventing the wheel here?

I've looked around on the web for components that provide an object-oriented 2D framework (vector graphics being interesting here). Object-oriented in the sense that a square on the screen is a square object in code, or at least that you can store custom information on the graphical object. It should support retrieving information on object positions etc in code to draw lines between objects, and detect whether the mouse is over an object or not.

Not really complicated things, but it becomes quite complicated and hard to maintain if there are hundreds or thousands of line to maintain just because you wrote all of it yourself, not to mention the potential for bugs creeping in, that would be avoided in a mature framework.

+2  A: 

Have you had a look at direct2d which is kinda the replacement for gdi. http://blogs.technet.com/thomasolsen/archive/2008/10/29/introducing-the-microsoft-direct2d-api.aspx http://msdn.microsoft.com/en-us/library/dd370990(VS.85).aspx

Tim Matthews
There's some controversy around Direct2D, Jonathan Blow has a big angry blog post about it here: http://braid-game.com/news/?p=466
grapefrukt
DAM it. Just when everyone though win seven was here to the rescue
Tim Matthews
Jonas
Whatever valid points Johnathan Blow makes are completely lost in that rant. He seems more concerned about the structure and coding style of the demo code itself.
Aardvark
I've worked with these gentlemen before - excellent coders. The blog entry makes a lot of claims - my guess is that they pulled it off.
Frank Krueger
Just deleted 1 of my own comments here ^ that was supposed to be on a completly different question.
Tim Matthews
+2  A: 

Maybe overkill but check out this. And also this

Malkocoglu
Ahh, yes, a simple SVG library would perhaps be useful here. Hmm, I may look more into that; Wikipedia had some interesting external links.
Jonas
Don't pass the Box2D demonstration @ http://www.crayonphysics.com/
Malkocoglu
A: 

As an SVG library that looks useful (thanks Malkocoglu for the idea!), I found this one: libboard. The simplicity in the code samples look awesome; my only remaining issue would then be in having the user interact with its generated SVG drawings. :/ AFAIK, it doesn't even include a renderer, much less a method to interact with its drawing. I'm not sure I'm willing to develop an SVG parser myself to control these needs. But the code simplicity to programmatically build the drawings look like just what I'm after. Hmm.

Jonas
I think a better use of Stackoverflow would be to add this info to your qustion and NOT as an another answer.
Aardvark
A: 

I'm sure you will go with an OO package, but don't expect miracles. Here's why.

I assume you start out with some application data, a set of application objects, let's call them objects A.

You could use a package of OO graphical objects to represent the graphical view of objects A, call this new set of objects G.

Now you have two sets of objects, A and G, either one of which could change dynamically, and you are faced with the problem of keeping them in correct correspondence. Not only do you have to generate G from A, you have to modify G when A changes, and modify A when G changes. This calls for lots of event-driven linkage code, and you can never be sure you've handled every case correctly. You can easily get into situations where what you see is not what you get. (WYSINWYG)

I have two suggestions:

  • What you're doing

Have a "paint" routine that directly renders objects A (using "blt" if you want to avoid flashing). Attach simple graphical information to objects A, like screen position and size. Handle mouse events yourself, for highlighting, dragging, creating wires, etc. This may seem like a lot of trouble, but it avoids all the linkage trouble you get into with redundant sets of objects. And, you have complete control of the code.

This is a general technique for managing redundant sets of objects. However, it has a tough learning curve. Most programmers are not up to it, but it does reduce code and guarantee correctness.

Mike Dunlavey
I completely see what you're saying, as we basically had to go through the job of using the paint routine and "blitting" the graphics for the reason you state. I can take a look at your other suggested model, because simplified code is getting quite important here.
Jonas
@Jonas: If I can be of any help, please feel free to contact me directly, via my wikipedia user page.
Mike Dunlavey
For 2D pixel graphics, frankly I use the first method. Some coders are scared of graphics and think it's a big deal, so they layer objects on it, thinking that will make it better. It makes it worse.
Mike Dunlavey
A: 

The description of "drawing objects and connect them together", sounds vaguely like something that Fig (xfig / winfig / et al) handles.

Another product that might fit the purpose (albeit at a price) is Visio -- the Microsoft Office Visio SDK (see http://office.microsoft.com/en-us/visio/HA101656401033.aspx) is supposedly quite rich.

I have not looked either fig or Visio from a programmer's standpoint, however, so I have no idea what the underlying code looks like, or whether it's suitable for your purpose... But I think they both are good starting points for inspiration. Perhaps there is another graph/figure library out there that might suit your purpose.

BTW, while noodling (er, Googling) around on this, I stumbled across AGD - automatic graph drawing at http://www.ads.tuwien.ac.at/research/graphDrawing.html. Again, not sure how suitable it is for your particular situation, but it seemed interesting enough to point it out.

Good luck!

Toybuilder