Separating data, scene & rendering from user interface (preferably using bindings?)

Apprentice
Posts: 18
Joined: 2006.06
Post: #1
Hi everyone,

I am developing a simple 3D application that hopefully will get ported to both Windows/Linux some day. I am still at the design stage, so I am trying to think not only once, but twice, so that I can maximize reusability of my code.

What would be ideal (at least from my novel point of view) is to have C++ classes that represents the onscreen objects of my application. A simple example would be a triangle class (e.g. with a position in space and with a certain color), with an accompanying draw method (that makes use of OpenGL's functionality).

Of course, I would also need some sort of scene class. The scene itself could have a member varible vector, containing any number of my above mentioned objects that has been instanced from the UI (I'm picking vector just as an example, not saying that it's the most efficient way of storing my objects). The scene could then go through the vector, calling each object's draw method.

So far so good. What I have described above could easily be moved to e.g. Windows, since the objects and the scene are written in C++.

Now to my problem; I would like to make use of Cocoa's binding possibilities, still using the above mentioned method for storing/drawing etc. I am familiar with the concept of bindings and NSArrayController, but I have never used them with anything else than Objective-C classes Blink It would be great IF I could use this approach, since it would mean that I only have to take care of UI specific programming for each platform, using the same code for the scene and it's objects.

I found an example over at Apple that illustrates a somewhat similar idea, though they are using Objective-C all the way.

I have been trying to think how to achieve something similar, but I really can't see how I still could save my objects the C++ way, while incorporating bindings...?

Any tips, ideas, links to tutorials that mentiones something that might lead me the right way, are more than welcome.

Thank you in advance!
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #2
You might be able to write some glue to make Cocoa bindings work with C++ objects, but frankly, it will defeat the purpose of bindings. You'll have to have 1st class ObjC classes to mirror each of your C++ classes, which will modifiy the C++ representation.

This is one of those cases where a more old fashioned MVC is appropriate. Write your UI in Cocoa, have an ObjC controller which modifies an underlying C++ model. I do this all the time, works peachy.
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.06
Post: #3
TomorrowPlusX Wrote:You might be able to write some glue to make Cocoa bindings work with C++ objects, but frankly, it will defeat the purpose of bindings. You'll have to have 1st class ObjC classes to mirror each of your C++ classes, which will modifiy the C++ representation.

This is one of those cases where a more old fashioned MVC is appropriate. Write your UI in Cocoa, have an ObjC controller which modifies an underlying C++ model. I do this all the time, works peachy.

Thanks for your input! I did manage to write Objective-C wrappers around the C++ classes, having set/get functions calling the C++ base class equivalents. Then setting the NSArrayController to the wrapper type works just fine... Are you sure that this really leads to "more" work than doing things the "old fashioned way"? Keeping objects synchronized is of importance within my application, and I really don't want to think of all the glue code I have to write if I am to do it manually Sad

The only problem is that I need to get those objects out of the array and into an C++ vector or similar, so that I can loop through it inside my scene class. I'm thinking of having a vector of pointers to the objects inside the Obj-C array, or something similar... This way, no data has to be copied, just have to update the vector whenever I insert new elements into the array (updating an object inside the array isn't a problem since I'm having a pointer to it, right?).

Or maybe this is stupid, and I should go the suggested way above?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
If you wanted to do this purely in C++, you're going to have your own binding-like system by either storing C function pointers or having an abstract class with virtual functions for the ones you want to call and store the objects (which are subclasses) to what you want to "bind" to. If you ever want to port this, you're going to need to eliminate your dependancy on ObjectiveC.
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.06
Post: #5
akb825 Wrote:If you wanted to do this purely in C++, you're going to have your own binding-like system by either storing C function pointers or having an abstract class with virtual functions for the ones you want to call and store the objects (which are subclasses) to what you want to "bind" to. If you ever want to port this, you're going to need to eliminate your dependancy on ObjectiveC.

In short; you are not recommending using the built-in bindings system?
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
I recommend it only if you want to stay on the Mac platform. It just adds more to change out when it comes time to port. (if you do things correctly, however, with the right libraries, it can sometimes come down to a mere recompile to take it over to both Windows and Linux.)
Quote this message in a reply
Apprentice
Posts: 18
Joined: 2006.06
Post: #7
akb825 and TomorrowPlusX; thanks for your input. I think I'll abandon the original idea with bindings after all...

Now, what I would like to see is some sample code that could illustrate your points. Is there any open source project out there that I can learn from, or do you guys have some code that can give me the ideas I need to start with? I enjoy programming, but I have a feeling of that I will make things unecessary complicated if I design the flow myself (hey, it was me suggesting the bindings approach you know!).

Any suggestions/links/project code is appreciated.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Separating update rate from render rate Jar445 5 3,935 Jan 17, 2009 01:43 PM
Last Post: AnotherJake
  Separating Axis Collision Detection Joseph Duchesne 4 3,875 Dec 22, 2005 10:18 AM
Last Post: Leisure Suit Lurie