#include <SDL/SDL.h>
#include <math.h>
#include "main.h"
#include "atoms.h"
#include "blobs.h"
#include "particles.h"
#include "render.h"
#include "images.h"
#include "things.h"
#include "panels.h"
#include "script.h"

extern atom atoms[MAXATOMS];
extern bond bonds[MAXBONDS];
extern particle particles[MAXPARTICLES];
extern int atom_num, bond_num, particle_num;

extern atompoint atompoints[MAXATOMPOINTS];
extern triangle2d triangles[MAXTRIANGLES];
extern int atompoint_num, triangle_num;

extern image background[2];
extern image lunalu;
extern image balloonface[3];

extern blob_bitmap blobgfx[4];
extern blob_bitmap bstar[4];

extern thing player;

extern int current_level;
extern level levels[level_num];

unsigned char blobBuffer[GameScreenSize];

void ShowAtoms(unsigned int *vram)
{
    atom *a;
    blob b;
    int i;

    for (i=0; i<atom_num; i++)
    {
        a = &atoms[i];
		if (a->exists)
		{
	        atom2blob(a,&b);
	        DrawBlob(&b, blobBuffer, vram, a->col.r, a->col.g, a->col.b);
		}
    }
}

void ShowParticles(unsigned int *vram)
{
    int i;
    for (i=0; i<particle_num; i++)
		if (particles[i].exists)
		    if (particles[i].gfx==1)
		        DrawZoomParticle(&particles[i], &bstar[0], vram, true);
			else
				DrawZoomParticle(&particles[i], &blobgfx[0], vram, true);
}

void ShowEdgeAtoms(unsigned int *vram)
{
    atom *a;
    blob b;
    int i;
    for (i=0; i<atom_num; i++)
    {
        a = &atoms[i];
        if (a->nbonds<8)
		{
			if (a->exists)
			{
				atom2blob(a,&b);
	            DrawBlob(&b, blobBuffer, vram, a->col.r, a->col.g, a->col.b);
			}
		}
    }
}

void ClearAtoms(unsigned int *vram)
{
	atom *a;
    blob b;
    int i;
    for (i=0; i<atom_num; i++)
    {
        a = &atoms[i];
		if (a->exists)
		{
			atom2blob(a, &b);
	        ClearBlob(&b, blobBuffer, vram);
		}
	}
}

void ShowBonds(unsigned int *vram)
{
    bond *b;
    int i;
    for (i=0; i<bond_num; i++)
    {
        b = &bonds[i];
        if (!b->broken) DrawLine(atoms[b->atom1].pos.x, atoms[b->atom1].pos.y, atoms[b->atom2].pos.x, atoms[b->atom2].pos.y, vram, 0xFFFFFF, true);
    }
}

void ClearBonds(unsigned int *vram)
{
    int i;
    for (i=0; i<bond_num; i++)
        if (!bonds[i].broken) DrawLine(atoms[bonds[i].atom1].pos.x, atoms[bonds[i].atom1].pos.y, atoms[bonds[i].atom2].pos.x, atoms[bonds[i].atom2].pos.y, vram, 0, false);
}

void ShowFlatBlob(unsigned int *vram)
{
    atompoint *a0, *a1, *a2;
    int i;
    for (i=0; i<triangle_num; i++)
    {
        a0 = &atompoints[triangles[i].p0];
        a1 = &atompoints[triangles[i].p1];
        a2 = &atompoints[triangles[i].p2];
        if ((atoms[a0->a].nbonds>2) && (atoms[a1->a].nbonds>2) && (atoms[a2->a].nbonds>2))
            DrawFlatTriangleBlob(&triangles[i], blobBuffer, vram);
    }
}



void ShowAll(unsigned int *vram)
{
	int t = SDL_GetTicks();
	RenderBackground(&background[levels[current_level].bgnum], vram);
	ShowPanels();
	ShowAtoms(vram);
	ShowParticles(vram);
	RenderSprite(player.pos.x, player.pos.y + sin(t/256.0)*16, &lunalu, vram, GameScreenWidth, GameScreenHeight);
	ShowThings(vram);
}

void ClearAll(unsigned int *vram)
{
    ClearAtoms(vram);
    ClearThings(vram);
}
