Skip to main content
GameDev.net gamedev.net
🔒 Locked

Access a dynamically created object (MFC)

Started by Hydrael Oct 27, 2005 at 12:45 AM 5 replies 950+ views
Original Post
Hydrael
Hydrael
Hello everyone, I have a problem understanding how to correctly work with CRuntimeClass. Given is the following example: I have an SDI MFC application which is seperated by several splitters (followed this tutorial). One of the splitter panes is a preview window, which renders a given OBJ file via OpenGL. The pane itself is created using RUNTIME_CLASS(CPreviewPane), so the object is dynamically created in runtime. The question now is: How can I interact with that object? Like I said, this pane renders obj files. For testing purposes I hardcode included a LoadOBJ(char *Filename) call within CPreviewPane's constructor. But what if I want to change the Filename? I can't seem to use overloaded constructors with RUNTIME_CLASS, nor can I find any references to the created object form CPreviewPane. Is there anything like a "GetObject()" Method or something similar? Thanks for any help or hints in advance Chris
HellRiZZer
HellRiZZer
You can always use state static variable for the class that will hold the filename.

class CPreviewPane : // etc{public:CPreviewPlane(CString strFilename);virtual ~CPreviewPane();virtual int LoadOBJ(CString strFilename);static CString m_strFilename;};// .Source fileCPreviewPane::CPreviewPane(CString strFilename){   m_strFilename = strFilename;  LoadOBJ(m_strFilename);}


Although my preffered method would be just call LoadOBJ and passing it the filename of the OBJ file instead of doing it in the constructor, where you cannot really do any extensive testing on whether or not loading has succeeded.

Hydrael
Hydrael
That's what I basically wanted to do, but the problem is, that my program won't compile when I try to pass something to the constructor within RUNTIME_CLASS

i.e. RUNTIME_CLASS(CPreviewPane("c:\meshes\dummy.obj")) won't work :(
jeroenb
jeroenb
I haven't read the tutorial, but the structure I would use in a viewer like you described is the following. In MFC most programs are document/view related, so is yours. In the background you have a CDocument like class. In this class you would load all the data. Then when splitting the view in subviews, you must ensure that the new view keeps connected with the document class which loaded your model. Then when the documents changes you can call UpdateAllViews to update all your open views in one call.
Crafter 2D: the open source 2D game framework ?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]
Hydrael
Hydrael
I don't know if I got you right.
Do you mean I shouldn't load the model within the actual subview class, but load it within the document class and then just update my views with the (new) model?
Hydrael
Hydrael
Ah, after re-reading the tutorial once again I've found what I missed: exactly what you stated ;)
Basically everything that's being viewed within the application in any way has to be stored in the document class - so does my model.

I guess I have to internalize object oriented programming a bit more ;)

Thanks guys
jeroenb
jeroenb
Indeed, that's what I meant.

Changes in the model are then easily reflected in the views. Also retreiving the model is very easy from the views or from the mainframe application as you can simply query for the active document and make a Get and Set functions for your model.
Crafter 2D: the open source 2D game framework ?Github: https://github.com/crafter2d/crafter2d
Twitter: [twitter]crafter_2d[/twitter]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.