Creating the DocumentSo now we have a flashy little app that doesn’t really do anything. If we were making a commercial product we’d be laughing our way to the bank by this time, but unfortunately we plan on using this tool so it’s going to have to do something. Up until this point we’ve dealt with views to a great extent, but now it’s time to put the "document" back into "document/view architecture". We’ll be looking at YourProjectNameDoc.cpp and YourProjectNameDoc.h from this point on. Adding Data to the DocumentThe first (and most obvious step) is to add our data to our document class. In our case we’ll just add a model object, which will contain all the info the application needs to draw the *.md3 file. We also add some code to the constructor and destructor to handle memory allocation for the model: Where: CMd3Model *m_currentModel; Is a member variable of CYourProjectNameDoc: CYourProjectNameDoc:: CYourProjectNameDoc () { // No model has been loaded yet. m_currentModel = NULL; } CYourProjectNameDoc::~ CYourProjectNameDoc () { // If a model is currently loaded // delete it. if ( m_currentModel != NULL ) delete m_currentModel; } Loading the ModelThis step is fairly simple because all the message maps you need have already been created by the wizard. Simply add your loading code to the serialize method. This is also where you add code for saving, however we won’t be covering that here. The CArchive object that is passed to the method will be the file that the user is asking to load: void CYourProjectNameDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // Load the MD3 file. if ( m_currentModel != NULL ) delete m_currentModel; m_currentModel = new CMd3Model; if ( !LoadMd3( ar, m_currentModel ) ) { MessageBox( NULL, "Not a valid md3 file!", "File Error!", MB_OK | MB_ICONERROR ); return; } } } And that’s it. You can further customize this process by intersecting the menu message and applying file filters to the file dialog, etc. Refer to the accompanying source for more information on how to do this. At this point you can simply add your draw code to the RenderScene method of each of your viewports. The application document can be accessed by any of its child views with the function GetDocument. Note that I used a rather cryptic method for rendering scenes in the accompanying source. I find that method works best for me, so I included it for advanced users - however I won’t explain it here as it looks like voodoo chicken scratch ( void SetRenderFunc(void (*func) (CViewerDoc* )) { m_RenderScene = func; } ).
ConclusionDespite popular opinion, creating proprietary tools for game development doesn’t have to be time consuming, tedious, or intimidating, (like writing articles on creating proprietary tools for game development). Through experience and code reuse new applications can be written in mere hours (my first MFC app took about two days to complete, my second took about two hours). Hobbyists and professionals alike no longer have an excuse to hack about, now that they have the proper tools. Joe Houston If you’re interested in seeing other articles relating to this subject (picking objects/verts in 3D space, drawing marquees in 3D, etc) please let me know. I’m always interested in feedback, and I welcome all comments and corrections. |