Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fresh

A hard 'un. - Home made GUIs.

This topic is 6458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Thanks to the excellent developing a gui in directx series of articles, I''ve developed an (almost) functional GUI myself. However one thing which has confused me is the question of allocating and deallocating memory at runtime for the windows. For example, my msgbox code looks like this:
///////////////////////////////
// MESSAGEBOX
class
CMFBDeleteParent : public CMetaformButton {
 virtual int fm_lclick(int x_in, int y_in) {
  if (m_pParent)
   ::delete m_pParent;
  return (1);
 }
};
 
void
MsgBox(CMetaform *parent, char *text, char *caption, int type) {
 int height = 0;
 
 if (!parent)
  return;
 
 CMetaformPanel *MFP = ::new CMetaformPanel;
 CMetaformStaticGDI *MFSGDI = ::new CMetaformStaticGDI;
 CMFBDeleteParent *MFBDeleteParentOk = ::new CMFBDeleteParent;
 
 if (!MFP || !MFSGDI || !MFBDeleteParentOk) {
  g_Console.Message("Insufficient Memory!");
  return;
 }
 
 parent->AddMetaform(MFP);
  MFP->AddMetaform(MFSGDI);
   height = MFSGDI->HeightWithWidth(380);
   MFSGDI->SetSize(380, height);
  MFP->AddMetaform(MFBDeleteParentOk);
   MFBDeleteParentOk->Initialise();
   MFBDeleteParentOk->SetSize(60, 20);
   MFBDeleteParentOk->SetPos(180, height + 20 + 10);
   MFBDeleteParentOk->SetCaption("OK");
  MFP->Initialise(caption, (parent->GetWidth()/2)-200, (parent->GetHeight()/2)-((height + 50)/2), 400, height + 60);
  MFSGDI->SetPos(10, MFP->GetCharHeight() + 5);
}
// END MESSAGEBOX
///////////////////////////////
 
And the destructor for all the windows is like this:
 
CMetaform::~CMetaform(void) {
 CMetaform *child;
 char buf[128];
 
 vector::iterator first = m_SubForms.begin();
 vector::iterator last  = m_SubForms.end();
 
 if (m_pParent)
  m_pParent->RemoveMetaform(this);
 
 while (first != last) {
  child = *(first++);
  if (child) {
   sprintf(buf, "This is %s, deleting %s", caption, child->GetCaption());
   g_Console.Message(buf);
   g_ErrorLog.Log(buf);
   ::delete &child;
  }
 }
 
 if (lpddsMetaform)
  lpddsMetaform->Release();
 
 sprintf(buf, "This is %s, deleting self.", caption);
 g_Console.Message(buf);
 g_ErrorLog.Log(buf);
}
 
This more or less works fine. However a msgbox is deleted by clicking on its CMFBDeleteParent button, the next time I try to create a msgbox the program exits. I suspect the problem lies in the way the windows are being created and deleted. There wasn''t any sample code in the tutorial of this, so I have no idea how correct this is. It seems to make sense, however. In the GUIs you people implement, how are the windows dynamically created and destroyed, for example with a msgbox? m_SubForms is an STL vector of CMetaform pointers. I''ve heard somewhere using pointers with STL is a quick way to arrange a meeting with El Diablo himself, but everything else seems to work fine. This is, however, driving me truly insane. What am I doing wrong here? Do you need further code for a diagnosis :?) r. "The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."

Share this post


Link to post
Share on other sites
Advertisement
The way I have my GUI set up, all windows are created when the GUI is loaded from a file, and all are destroyed before loading a new GUI. But the GUI keeps a list of pointers to any windows that are visible or active, and only draws/processes them. The windows in that array are drawn first to last, so z-ordering is done by adjusting the position in that array of each pointer. It''s nice and fast, although it''s a little more of a memory hog than your implementation.

So to display a message box, I''d say pMessageBox->Show( ) and later pMessageBox->Hide( ).

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Thanks for thinking about it - but that wasn''t the question I asked. My problem is creating windows at any time during runtime, say halfway during the my program running, then destroying them, and then creating more. Am I doing that correctly as my code indicates?

"The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."

Share this post


Link to post
Share on other sites
You can have multiple children but only one parent. If you have two children destroyed, they both try to delete the parent.

Instead, have each parent destroy its children.

Share this post


Link to post
Share on other sites
Null_pointer, that''s what I *am* doing. The button on the msgbox is of the clas CMFBDeleteParent, which when clicked calls for its parent to be deleted (and I therefore I assume has it''s destructor called) which then as the destructor shows, iterates recursively through it''s children, calling for them to be deleted, which should cause them to call for their children to be deleted. This is why I am confused. Why should this cause problems?


"The mere thought hadn''t even begun to speculate about the slightest possibility of traversing the eternal wasteland that is my mind..."

Share this post


Link to post
Share on other sites
Some strange code there... However, I should have looked at it more carefully.

Anyway, if you are using CMetaform* as template parameter T to vector, then the dereferencing operator for the iterator will return CMetaform*. Therefore the variable "child" is a pointer. Why are you calling delete on the address of the pointer? It should be "delete child;"

(Probably part of some old code - I hate coding GUIs, don''t you? )

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!