Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualMoonkis

Posted 20 May 2012 - 05:00 AM

Hello all!

I'v been adventurous enough to try to implement a Quad-tree.
But I keep getting these Unhandled exceptions everywhere around the code, it seems like it doesn't allocate the memory or the memory of the allocated items get corrupted.

Am I doing something wrong inside my code? I know that if I place to many objects near each other the program will most likely crash due to it trying to produce infinite nodes since there are to many nodes inside the new node.

But it's crashing during start up so it's not that.
Hopefully someone can help me and spot the problem somewhere inside my code.

Here is an example of the error message I am getting:

Unhandled exception at 0x00488cf6 in SFML Quadtree.exe: 0xC0000005: Access violation reading location 0x00000000.


And down below is the vital and "minimal" ( excluding the Object class since it only contains a rectangle object ) and the headerfile of the Quadtree since well most of the information is already inside the .cpp file.


#include <iostream>
#include <SFML/Graphics.hpp>
#include "Quadtree.hpp"


int main()
{
  sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Quadtree - Structure and Split Example");
  sf::Event evnt;
  Quadtree qTree( 0,0, window.getSize().x, window.getSize().y, window, 0 );
  int drawCalls = 0;
  int collisionCalls = 0;

  sf::Clock frameClock;
  const float TIMER = 1.0f / 0.5f;
  while(window.isOpen())
  {
    while(window.pollEvent(evnt))
    {
      if(evnt.type == sf::Event::Closed)
        window.close();

      if(evnt.type == sf::Event::MouseButtonReleased)
      {
        if(evnt.mouseButton.button == sf::Mouse::Button::Left)
        {
          qTree.add(new Object(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y));
          std::cout << "Added object to node" << std::endl;
          std::cout << collisionCalls << " collisions performed." << std::endl;
          collisionCalls = 0;
        }
      }

    }
    if(frameClock.getElapsedTime().asSeconds() > TIMER)
    {
      frameClock.restart();
      std::cout << drawCalls << " drawcalls performed." << std::endl;
    }
    drawCalls = 0;
    window.clear(sf::Color(94, 174, 255));
    qTree.display(drawCalls);
    window.display();
  }

  return 0;
}

And here is the Quadtree.cpp
#include "Quadtree.hpp"
#include <iostream>

const int Quadtree::MAX_DEPTH = 7;
const int Quadtree::MAX_OBJECTS = 4;

Quadtree::Quadtree(float x, float y, float width, float height, sf::RenderWindow& wndw, int depth)
{
  quadX = x;
  quadY = y;
  quadWidth = width;
  quadHeight = height;
  window = &wndw;
  nodeStatus = LEAF;
  quadDepth = depth;
  objectNumber = 0;

  rect.setFillColor(sf::Color::Black);
  rect.setOutlineColor(sf::Color::Magenta);
  rect.setOutlineThickness(1);
  rect.setPosition(x, y);
  rect.setSize(sf::Vector2f(width, height)); 

  quad.left = x;
  quad.top = y;
  quad.width = width;
  quad.height = height;

  for(int i = 0; i < 4; i++)
    nodes[i] = NULL;
}
Quadtree::~Quadtree()
{
  for(int n = 0; n < 4; n++)
  {
    if(nodes[n] != NULL)
      delete nodes[n];
  }
}


void Quadtree::add(Object* object)
{
  objectNumber++;
  objects.push_back(new Object(*object));
  if(objectNumber >= MAX_OBJECTS)
  {
    split();
  }
}

void Quadtree::split()
{
  if(quadDepth >= MAX_DEPTH)
    return;

  for(int i = 0; i < objects.size(); i++)
  {
    pass(objects[i]);
    delete objects[i];
    objects.erase(objects.begin() + i);
    --i;
  }
}

void Quadtree::pass(Object* object)
{
  /* Determite where it should be placed */
  if(object->getX() > quadWidth/2) /* If true, place somewhere on the right side */
  {
    /* SE or NE */
    if(object->getY() > quadHeight/2) /* It's the SE Node */
    {
      if(nodes[1] == NULL)
      {
        nodes[1] = new Quadtree(quadX + quadWidth/2, quadY + quadHeight/2, quadWidth/2, quadHeight/2, *window, quadDepth +1); // SE
      }
      nodes[1]->add(object);
      return;
    }
    else
    {
      if(nodes[0] == NULL)
      {
        nodes[0] = new Quadtree(quadX + quadWidth/2, quadY, quadWidth/2, quadHeight/2, *window, quadDepth + 1); // NE
      }
      nodes[0]->add(object);
      return;
    }
  }
  else
  {
    /* Is it NW or SW*/
    if(object->getY() > quadHeight/2)
    {
      /* SW */
      if(nodes[2] == NULL)
      {
        nodes[2] = new Quadtree(quadX, quadY + quadHeight/2, quadWidth/2, quadHeight/2, *window, quadDepth +1);// SW
      }
      nodes[2]->add(object);
      return;
    }
    else
    {
      if(nodes[3] == NULL)
      {
        nodes[3] = new Quadtree(quadX, quadY, quadWidth/2, quadHeight/2, *window, quadDepth +1); // NW
      }
      nodes[3]->add(object);
      return;
    }
  }
}

void Quadtree::display(int& drawCalls)
{
  drawCalls++;
  window->draw(rect);
  for(int n = 0; n < objects.size(); n++)
    window->draw(objects[n]->getRect());


  for(int i = 0; i < 4; i++)
    if(nodes[i] != NULL)
      nodes[i]->display(drawCalls);
}

#4Moonkis

Posted 19 May 2012 - 02:31 PM

Hello all!

I'v been adventurous enough to try to implement a Quad-tree.
But I keep getting these Unhandled exceptions everywhere around the code, it seems like it doesn't allocate the memory or the memory of the allocated items get corrupted.

Am I doing something wrong inside my code? I know that if I place to many objects near each other the program will most likely crash due to it trying to produce infinite nodes since there are to many nodes inside the new node.

But it's crashing during start up so it's not that.
Hopefully someone can help me and spot the problem somewhere inside my code.

Here is an example of the error message I am getting:

Unhandled exception at 0x00488cf6 in SFML Quadtree.exe: 0xC0000005: Access violation reading location 0x00000000.


And down below is the vital and "minimal" ( excluding the Object class since it only contains a rectangle object ) and the headerfile of the Quadtree since well most of the information is already inside the .cpp file.

#include <iostream>
#include <SFML/Graphics.hpp>
#include "Quadtree.hpp"


int main()
{
  sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Quadtree Example");
  sf::Event evnt;
  Quadtree qTree( 0,0, window.getSize().x, window.getSize().y, &window );
  sf::Sprite sprite;


  while(window.isOpen())
  {
	while(window.pollEvent(evnt))
	{
	  if(evnt.type == sf::Event::Closed)
		window.close();

	  if(evnt.type == sf::Event::MouseButtonReleased)
	  {
		if(evnt.mouseButton.button == sf::Mouse::Button::Left)
		{
		  qTree.add(new Object(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y));
		  std::cout << "Added a new object" << std::endl;
		}
		else
		  std::cout << "This is the object in the current node" << std::endl;
	  }

	}

	window.clear(sf::Color(94, 174, 255));
	qTree.display(sprite);
	window.display();
  }

  return 0;
}

And here is the Quadtree.cpp
#include "Quadtree.hpp"
const int Quadtree::MAX_OBJECTS = 2;
Quadtree::Quadtree(float x, float y, float width, float height, sf::RenderWindow* window)
{
  quadX = x;
  quadY = y;
  quadWidth = width;
  quadHeight = height;
  this->window = window;
  rect.setFillColor(sf::Color::Black);
  rect.setOutlineColor(sf::Color::Magenta);
  for(int i = 0; i < 4; i++)
	nodes[i] = NULL;
}
Quadtree::~Quadtree()
{
  for(int i = 0; i < objects.size(); i++)
  {
	delete objects[i];
  }
  for(int n = 0; n < 4; n++)
  {
	if(nodes[n] != NULL)
	  delete nodes[n];
  }
}
void Quadtree::add(Object* object)
{
  if(nodeStatus == LEAF && objects.size() <= MAX_OBJECTS)
  {
	objects.push_back(object);
	if(objects.size() > MAX_OBJECTS)
	{
	  nodeStatus = BRANCH;
	  split();
	}
  }
  else
  {
	pass(object);
  }
}
void Quadtree::split()
{
  nodes[0] = new Quadtree(quadX + quadWidth/2, quadY, quadWidth/2, quadHeight/2, window); // NE
  nodes[1] = new Quadtree(quadX + quadWidth/2, quadY + quadHeight/2, quadWidth/2, quadHeight/2, window); // SE
  nodes[2] = new Quadtree(quadX, quadY + quadHeight/2, quadWidth/2, quadHeight/2, window);// SW
  nodes[3] = new Quadtree(quadX, quadY, quadWidth/2, quadHeight/2, window); // NW
  for(int i = 0; i < objects.size(); i++)
  {
	pass(objects[i]);
  }
  objects.clear();
}
void Quadtree::pass(Object* object)
{
  if(object->getX() > quadWidth/2)
  {
	if(object->getY() > quadHeight/2)
	{
	  nodes[1]->add(object);
	}
	else
	{
	  nodes[0]->add(object);
	}
  }
  else
  {
	if(object->getY() > quadWidth/2)
	{
	  nodes[2]->add(object);
	}
	else
	{
	  nodes[3]->add(object);
	}
  }
}

void Quadtree::display(sf::Sprite& sprite)
{
  window->draw(rect);
  if(nodeStatus == LEAF && !objects.empty())
  {
	for(int n = 0; n < objects.size(); n++)
	{
	  window->draw(objects[n]->getRect());
	}
  }
  for(int i = 0; i < 4; i++)
  {
	nodes[i]->display(sprite);
  }
}

#3Moonkis

Posted 19 May 2012 - 01:35 PM

Hello all!

I'v been adventurous enough to try to implement a Quad-tree.
But I keep getting these Unhandled exceptions everywhere around the code, it seems like it doesn't allocate the memory or the memory of the allocated items get corrupted.

Am I doing something wrong inside my code? I know that if I place to many objects near each other the program will most likely crash due to it trying to produce infinite nodes since there are to many nodes inside the new node.

But it's crashing during start up so it's not that.
Hopefully someone can help me and spot the problem somewhere inside my code.

Here is an example of the error message I am getting:

Unhandled exception at 0x00488cf6 in SFML Quadtree.exe: 0xC0000005: Access violation reading location 0x00000000.


And down below is the vital and "minimal" ( excluding the Object class since it only contains a rectangle object ) and the headerfile of the Quadtree since well most of the information is already inside the .cpp file.

#include <iostream>
#include <SFML/Graphics.hpp>
#include "Quadtree.hpp"


int main()
{
  sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Quadtree Example");
  sf::Event evnt;
  Quadtree qTree( 0,0, window.getSize().x, window.getSize().y, &window );
  sf::Sprite sprite;


  while(window.isOpen())
  {
	while(window.pollEvent(evnt))
	{
	  if(evnt.type == sf::Event::Closed)
		window.close();

	  if(evnt.type == sf::Event::MouseButtonReleased)
	  {
		if(evnt.mouseButton.button == sf::Mouse::Button::Left)
		{
		  qTree.add(new Object(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y));
		  std::cout << "Added a new object" << std::endl;
		}
		else
		  std::cout << "This is the object in the current node" << std::endl;
	  }

	}

	window.clear(sf::Color(94, 174, 255));
	qTree.display(sprite);
	window.display();
  }

  return 0;
}

And here is the Quadtree.cpp
#include "Quadtree.hpp"
const int Quadtree::MAX_OBJECTS = 2;
Quadtree::Quadtree(float x, float y, float width, float height, sf::RenderWindow* window)
{
  quadX = x;
  quadY = y;
  quadWidth = width;
  quadHeight = height;
  this->window = window;
  rect.setFillColor(sf::Color::Black);
  rect.setOutlineColor(sf::Color::Magenta);
  for(int i = 0; i < 4; i++)
	nodes[i] = NULL;
}
Quadtree::~Quadtree()
{
  for(int i = 0; i < objects.size(); i++)
  {
	delete objects[i];
  }
  for(int n = 0; n < 4; n++)
  {
	if(nodes[n] != NULL)
	  delete nodes[n];
  }
}
void Quadtree::add(Object* object)
{
  if(nodeStatus == LEAF && objects.size() <= MAX_OBJECTS)
  {
	objects.push_back(object);
	if(objects.size() > MAX_OBJECTS)
	{
	  nodeStatus = BRANCH;
	  split();
	}
  }
  else
  {
	pass(object);
  }
}
void Quadtree::split()
{
  nodes[0] = new Quadtree(quadX + quadWidth/2, quadY, quadWidth/2, quadHeight/2, window); // NE
  nodes[1] = new Quadtree(quadX + quadWidth/2, quadY + quadHeight/2, quadWidth/2, quadHeight/2, window); // SE
  nodes[2] = new Quadtree(quadX, quadY + quadHeight/2, quadWidth/2, quadHeight/2, window);// SW
  nodes[3] = new Quadtree(quadX, quadY, quadWidth/2, quadHeight/2, window); // NW
  for(int i = 0; i < objects.size(); i++)
  {
	pass(objects[i]);
  }
  objects.clear();
}
void Quadtree::pass(Object* object)
{
  if(object->getX() > quadWidth/2)
  {
	if(object->getY() > quadHeight/2)
	{
	  nodes[1]->add(object);
	}
	else
	{
	  nodes[0]->add(object);
	}
  }
  else
  {
	if(object->getY() > quadWidth/2)
	{
	  nodes[2]->add(object);
	}
	else
	{
	  nodes[3]->add(object);
	}
  }
}

void Quadtree::display(sf::Sprite& sprite)
{
  window->draw(rect);
  if(nodeStatus == LEAF && !objects.empty())
  {
	for(int n = 0; n < objects.size(); n++)
	{
	  window->draw(objects[n]->getRect());
	}
  }
  for(int i = 0; i < 4; i++)
  {
	nodes[i]->display(sprite);
  }
}

#2Moonkis

Posted 19 May 2012 - 01:30 PM

Hello all!

I'v been adventurous enough to try to implement a Quad-tree for my ( soon to become ) tile game.
When I though that I finished it I ran into some major problems which after hours of debugging I can't figure out what is causing the errors.

Honestly I need some help figuring out what is WRONG with my code. The error I'm currently getting is:

Unhandled exception at 0x00488cf6 in SFML Quadtree.exe: 0xC0000005: Access violation reading location 0x00000000.


EDIT: Redid the code and reduced the size of it.


#include <iostream>
#include <SFML/Graphics.hpp>
#include "Quadtree.hpp"


int main()
{
  sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Quadtree Example");
  sf::Event evnt;
  Quadtree qTree( 0,0, window.getSize().x, window.getSize().y, &window );
  sf::Sprite sprite;


  while(window.isOpen())
  {
    while(window.pollEvent(evnt))
    {
      if(evnt.type == sf::Event::Closed)
        window.close();

      if(evnt.type == sf::Event::MouseButtonReleased)
      {
        if(evnt.mouseButton.button == sf::Mouse::Button::Left)
        {
          qTree.add(new Object(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y));
          std::cout << "Added a new object" << std::endl;
        }
        else
          std::cout << "This is the object in the current node" << std::endl;
      }

    }

    window.clear(sf::Color(94, 174, 255));
    qTree.display(sprite);
    window.display();
  }

  return 0;
}

And here is the Quadtree.cpp
#include "Quadtree.hpp"
const int Quadtree::MAX_OBJECTS = 2;
Quadtree::Quadtree(float x, float y, float width, float height, sf::RenderWindow* window)
{
  quadX = x;
  quadY = y;
  quadWidth = width;
  quadHeight = height;
  this->window = window;
  rect.setFillColor(sf::Color::Black);
  rect.setOutlineColor(sf::Color::Magenta);
  for(int i = 0; i < 4; i++)
    nodes[i] = NULL;
}
Quadtree::~Quadtree()
{
  for(int i = 0; i < objects.size(); i++)
  {
    delete objects[i];
  }
  for(int n = 0; n < 4; n++)
  {
    if(nodes[n] != NULL)
	  delete nodes[n];
  }
}
void Quadtree::add(Object* object)
{
  if(nodeStatus == LEAF && objects.size() <= MAX_OBJECTS)
  {
    objects.push_back(object);
    if(objects.size() > MAX_OBJECTS)
    {
	  nodeStatus = BRANCH;
	  split();
    }
  }
  else
  {
    pass(object);
  }
}
void Quadtree::split()
{
  nodes[0] = new Quadtree(quadX + quadWidth/2, quadY, quadWidth/2, quadHeight/2, window); // NE
  nodes[1] = new Quadtree(quadX + quadWidth/2, quadY + quadHeight/2, quadWidth/2, quadHeight/2, window); // SE
  nodes[2] = new Quadtree(quadX, quadY + quadHeight/2, quadWidth/2, quadHeight/2, window);// SW
  nodes[3] = new Quadtree(quadX, quadY, quadWidth/2, quadHeight/2, window); // NW
  for(int i = 0; i < objects.size(); i++)
  {
    pass(objects[i]);
  }
  objects.clear();
}
void Quadtree::pass(Object* object)
{
  if(object->getX() > quadWidth/2)
  {
    if(object->getY() > quadHeight/2)
    {
	  nodes[1]->add(object);
    }
    else
    {
	  nodes[0]->add(object);
    }
  }
  else
  {
    if(object->getY() > quadWidth/2)
    {
	  nodes[2]->add(object);
    }
    else
    {
	  nodes[3]->add(object);
    }
  }
}

void Quadtree::display(sf::Sprite& sprite)
{
  window->draw(rect);
  if(nodeStatus == LEAF && !objects.empty())
  {
    for(int n = 0; n < objects.size(); n++)
    {
	  window->draw(objects[n]->getRect());
    }
  }
  for(int i = 0; i < 4; i++)
  {
    nodes[i]->display(sprite);
  }
}

#1Moonkis

Posted 19 May 2012 - 10:30 AM

Hello all!

I'v been adventurous enough to try to implement a Quad-tree for my ( soon to become ) tile game.
When I though that I finished it I ran into some major problems which after hours of debugging I can't figure out what is causing the errors.

Honestly I need some help figuring out what is WRONG with my code. The error I'm currently getting is:

Unhandled exception at 0x00488cf6 in SFML Quadtree.exe: 0xC0000005: Access violation reading location 0x00000000.


I checked everywhere so that I didn't access an null pointer or stuff like that.
The code is with SFML ( 2.0 RC ) and is a bit hackish due to my attempt to debug the code.

Here it is:

Quadtree.hpp
#ifndef QUADTREE_HPP
#define QUADTREE_HPP
#include <SFML\Graphics.hpp>
#include "Tile.hpp"
namespace TreeStatus
{
  enum Status
  {
	LEAF,
	BRANCH
  };
}

class Quadtree
{
public:
  Quadtree(float loc_x, float loc_y, float width, float height, sf::RenderWindow* window);
  ~Quadtree();
  TreeStatus::Status treeStatus();
  void addObject(Tile* tile);
  void display(sf::FloatRect& screenbox, sf::Sprite& sprite, int& drawCalls);
  void setWindow(sf::RenderWindow& window);
private:
  void Split();
  void Pass(Tile* tile);
  static const int MAX_OBJECTS;
  static const int MAX_DEPTH;
  static int CURRENT_DEPTH;
  sf::FloatRect* collisionBox;
  float loc_x, loc_y, width, height;
  sf::RenderWindow* window;
  std::vector<Tile*> objects;
  TreeStatus::Status status;
  Quadtree* nodes[4];
};

#endif

And Quadtree.cpp:
#include <iostream>
#include "Quadtree.hpp"
#include "Tile.hpp"
#define NULL 0
const int Quadtree::MAX_OBJECTS = 10;
const int Quadtree::MAX_DEPTH = 4;
int Quadtree::CURRENT_DEPTH = 0;
Quadtree::Quadtree(float loc_x, float loc_y, float width, float height, sf::RenderWindow* window)
{
  /* Set all data */
  this->loc_x = loc_x;
  this->loc_y = loc_y;
  this->width = width;
  this->height = height;
  this->window = window;
  this->status = TreeStatus::LEAF;
  this->collisionBox = new sf::FloatRect(loc_x, loc_y, width, height);
  /* Null all our pointers */
  for(int i = 0; i < 4; i++)
	nodes[i] = NULL;
  std::cout << "A node was created!" << std::endl;
}
Quadtree::~Quadtree()
{
  for(int i = 0; i < objects.size(); i++)
  {
	delete objects[i];
	objects.erase(objects.begin() + i);
  }
  for(int i = 0; i < 4; i++)
  {
	if(nodes[i] != NULL)
	  nodes[i]->~Quadtree();
  }
  delete collisionBox;
}
TreeStatus::Status Quadtree::treeStatus()
{
  return status;
}
void Quadtree::addObject(Tile* tile)
{
  if(CURRENT_DEPTH >= MAX_DEPTH)
  {
	/* If this is a tree Status then it's the bottom of the tree. */
	if(status == TreeStatus::LEAF)
	  objects.push_back(tile);
	else /* Push it down further */
	  Pass(tile);
	return;
  }
  /* If current leaf-node hasn't reached the max-limit then store it */
  if(objects.size() <= MAX_OBJECTS && status == TreeStatus::LEAF)
  {
	objects.push_back(tile);
	if(objects.size() == MAX_OBJECTS && status == TreeStatus::LEAF )
	{
	  Split();
	}
  }
  else
  {
	/* Node allready contains max objects, pass it to child */
	Pass(tile);
  }
}

void Quadtree::Split()
{
  CURRENT_DEPTH++;
  if(CURRENT_DEPTH <= MAX_DEPTH)
  {
	/* Since we are splitting it into child nodes, it's no longer a leaf node */
	status = TreeStatus::BRANCH;
	/* Create the child nodes */
	nodes[0] = new Quadtree(loc_x + width/2, loc_y, width/2, height / 2, window); // NE
	nodes[1] = new Quadtree(loc_x + width/2, loc_y + height/2, width / 2, height/2, window); // SE
	nodes[2] = new Quadtree(loc_x, loc_y, width / 2, height / 2, window); // NW
	nodes[3] = new Quadtree(loc_x, loc_y + height / 2, width / 2, height / 2, window); // SW
	for(int i = 0; i < objects.size(); i++)
	{
	  Pass(objects[i]);
	}
	objects.clear();
	std::cout << "NODE DID A SPLIT, IT HAS " << objects.size() << " OBJECTS LEFT" << std::endl;
  }
}
void Quadtree::Pass(Tile* tile)
{
  /* It's on the left side of the total area */
  if(tile->getX() < loc_x + (width/2) )
  {
	if(tile->getY() < loc_y + (height/2) ) /* It's on the top area of the left side */
	{
	  nodes[2]->addObject(tile);
	}
	else
	{
	  nodes[3]->addObject(tile);
	}
  }
  else /* It's on the right side of the total area */
  {
	if(tile->getY() < loc_y + (height/2) ) /* It's on the top area of the right side */
	{
	  nodes[0]->addObject(tile);
	}
	else
	{
	  nodes[1]->addObject(tile);
	}
  }
}
void Quadtree::display(sf::FloatRect& screenbox, sf::Sprite& sprite, int& drawCalls)
{
  /* Every action is related to the node actually intersecting the view */
  if(collisionBox != NULL)
  {
	if(collisionBox->intersects(screenbox))
	{
	  /* If the current node is a leaf node and is NOT empty draw the items. */
	  if(status == TreeStatus::LEAF && !objects.empty())
	  {
		for(int i = 0; i < objects.size(); i++)
		{
		  sprite.setPosition(objects[i]->getX(), objects[i]->getY());
		  window->draw(sprite);
		  drawCalls++;
		}
	  }
	  else
	  {
		/* Even if it's an BRANCH node it still might not be empty, if not draw the objects before going into child nodes */
		if(!objects.empty())
		{
		  for(int i = 0; i < objects.size(); i++)
		  {
			sprite.setPosition(objects[i]->getX(), objects[i]->getY());
			window->draw(sprite);
			drawCalls++;
		  }
		}
		/* Go into child nodes */
		for(int n = 0; n < 4; n++)
		  nodes[n]->display(screenbox, sprite, drawCalls);

	  }
	}
  }

}

And lastly the main.cpp
#include <SFML\Graphics.hpp>
#include <iostream>
#include <sstream>
#include "Tile.hpp"
#include "Quadtree.hpp"
int main()
{
  const int MAP_WIDTH = 255;
  const int MAP_HEIGHT = 255;
  sf::RenderWindow window(sf::VideoMode(800, 600), "Tilemap and Quadtree culling");
  sf::Event evnt;
  Quadtree qTileTree(0, 0, MAP_WIDTH, MAP_HEIGHT, &window);

  for(int y = 0; y < MAP_HEIGHT; y++)
  {
	for(int x = 0; x < MAP_WIDTH; x++)
	{
	  Tile* tile = new Tile();
	  tile->setPosition( x * 32, y * 32);
	  tile->setID(0);
	  qTileTree.addObject(tile);
	}
  }
  sf::Texture texture;
  texture.loadFromFile("tile.png");
  sf::Sprite sprite(texture);
  /* Frames per Seconds */
  const float REFRESH = 1.0f / 60.0f;
  sf::Text text;
  text.setColor(sf::Color::Green);
  text.setCharacterSize(16);
  sf::String string("");
  text.setPosition(0,0);
  int drawCalls = 0;
  sf::Clock frameClock;
  while(window.isOpen())
  {
	sf::View veiw = window.getView();
	while(window.pollEvent(evnt))
	{
	  if(evnt.type == sf::Event::Closed)
		window.close();
	}

	if(frameClock.getElapsedTime().asSeconds() > REFRESH)
	{
	  frameClock.restart();
	  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	  {
		veiw.move(2.0f, 0);
	  }
	  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	  {
		veiw.move(-2.0f, 0);
	  }
	  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
	  {
		veiw.move(0, -2.0f);
	  }
	  if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
	  {
		veiw.move(0, 2.0f);
	  }
	  std::cout << "Draw Calls " << drawCalls << std::endl;
	}
	sf::FloatRect screenRect(sf::Vector2f(veiw.getCenter().x - (veiw.getSize().x/2 + 64), veiw.getCenter().y - (veiw.getSize().y/2 + 32)), sf::Vector2f(veiw.getSize().x+64,veiw.getSize().y+64));
	
	/* Set the new veiew */
	window.setView(veiw);
	window.clear(sf::Color::Black);
	drawCalls = 0;
	qTileTree.display(screenRect, sprite, drawCalls);
	window.display();
  }

  return 0;
}

PARTNERS