#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <allegro.h>

#include "object.h"
#include "particle.h"
#include "world.h"

BITMAP* buf;

static void init(void)
{
	int i;
	PALETTE pal;
	
	srand(time(NULL));
	
	allegro_init();
	install_keyboard();
	install_mouse();
	install_timer();

	set_color_depth(8);
	set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0);

	for (i = 0; i < 16; i++) {
		pal[i].r = i * 4;
		pal[i].g = i;
		pal[i].b = 0;
		pal[i + 16].r = 63;
		pal[i + 16].g = 16 + i * 3;
		pal[i + 16].b = i;
	}
	for (i = 32; i < 256; i++)
		pal[i].r = pal[i].g = pal[i].b = 0;

	set_palette(pal);
}

volatile static int ticks = 0;

static void timer_proc(void)
{
	ticks++;
}
END_OF_FUNCTION(timer_proc);

int main(void)
{
	Object* objects;
	World world;
	world.x = 0;
	world.y = 0;

	init();
	LOCK_VARIABLE(ticks);
	LOCK_FUNCTION(timer_proc);

	buf = create_bitmap(320, 200);

	objects = make_object_list();
	
	append_obj(objects, make_particleburst(&world, 160, 100, 1024,
			0, M_PI / 8, 1.0, 20));

	install_int_ex(timer_proc, BPS_TO_TIMER(60));
	while (!keypressed()) {
		while (ticks == 0);
		for (; ticks > 0; ticks--)
			logic(objects);
		
		clear(buf);
		render(objects);
		vsync();
		blit(buf, screen, 0, 0, 0, 0, 320, 200);
	}
	
	destroy_bitmap(buf);

	return 0;
}
END_OF_MAIN();

