Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

BSP class

This topic is 4614 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

Hi, I wrote a BSP class and since I'm still pretty new to witeing classes, I wanted to see what you guys thought:

struct BSP{
 float height, width;
 float centerX, centerY;
 MinMax limits;
 BSP *p_sec1, *p_sec2, *p_sec3, *p_sec4;
 bool found_limits;
 bool is_leaf;
};

class CBspTree{
 public: 
   CBspTree();
  ~CBspTree();
   int number_of_parts;
 private:
  BSP* map;
  
  void InitTree(BSP* node, float x, float y, float h, float w);
  void EraseBSP(BSP* node);
};

#include "BSP.h"

CBspTree :: CBspTree()
{
  map = new BSP;
  InitTree(map,MAP_SIZE/2, MAP_SIZE/2, MAP_SIZE, MAP_SIZE);
}

CBspTree :: ~CBspTree()
{
  EraseBSP(map);
}

void CBspTree :: InitTree(BSP* node, float x, float y, float h, float w)
{
   node->centerX = x; node->centerY = y; 
   node->height = h; node->width = w;
   node->found_limits = false;
   number_of_parts++;
   if(w>32)
   {
     node->p_sec1 = new BSP;
     node->p_sec2 = new BSP;
     node->p_sec3 = new BSP;
     node->p_sec4 = new BSP;

     InitTree(node->p_sec1, x-w/4, y-h/4, h/2, w/2);
     InitTree(node->p_sec2, x+w/4, y-h/4, h/2, w/2);
     InitTree(node->p_sec3, x+w/4, y+h/4, h/2, w/2);
     InitTree(node->p_sec4, x-w/4, y+h/4, h/2, w/2);
   }
   
   else
   {
     node->is_leaf=true;
   }
}

void CBspTree :: EraseBSP(BSP* node)
{
   if(!node->is_leaf)
    {
      EraseBSP(node->p_sec1); 
      EraseBSP(node->p_sec2); 
      EraseBSP(node->p_sec3); 
      EraseBSP(node->p_sec4); 
      delete node;
    } 
    else
      delete node;
}
      

Is the class built well? Are the ctor and dtor ok? Any input would be appriciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by daniel_i_l
What do you mean?
Thanks.
A lot of your member variables are never intiialized. is_leaf is only set if it's actually a leaf. Otherwise the value is undefined. You should really zero/null all your member variables in your constructor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!