#include "example2.h"

#define NUM_PYRAMID_ROWS 22

using namespace std;

RatPhysicsExample2::RatPhysicsExample2(string name): RatPhysicsExample(name) {}
RatPhysicsExample2::RatPhysicsExample2(const char *name): RatPhysicsExample(name) {}
RatPhysicsExample2::RatPhysicsExample2(char *name): RatPhysicsExample(name) {}

bool RatPhysicsExample2::InitWorld()
{
	SDL_WM_SetCaption(title.c_str(),NULL);

	aabb space={{0,0},{bounds.x,bounds.y}};
	vector2 cell_size={150,150};
	vector2 gravity={0,12};

	world=rat_world_create(space,rat_integrator_euler,10,
		rat_body_manager_type_quad_tree,&cell_size);

	rat_world_set_body_left_world_callback(world,&rat_examples_leave_world_callback,NULL);

	rat_world_set_gravity(world,gravity);
	rat_world_set_damping(world,0.95);
	rat_examples_create_walls(world);

	vector2 boxsize={20,20};

	register unsigned int i;
	for (i=0; i<NUM_PYRAMID_ROWS; i++)
		rat_examples_create_box_row(world,boxsize,3,SCENE_HEIGHT-10-NUM_PYRAMID_ROWS*25+i*25,SCENE_WIDTH/2.0,i+1,0);

	return (bool)world;
}

bool RatPhysicsExample2::UpdateWorld(rat_real timestep)
{
	timestep=timestep>2.0?2.0:timestep;
	rat_world_update(world,timestep*0.05,timestep<=1.0?1:0);

	return true;
}

bool RatPhysicsExample2::UpdateVisuals()
{
	rat_draw_world(world,show_quadtree,show_arbiters,show_aabbs);
	return true;
}

bool RatPhysicsExample2::FreeWorld()
{
	rat_world_free_entities(world);
	rat_world_destroy(world);

	return true;
}

