#include "SDL_image.h"
#include "images.h"

image background[2];
image lunalu;
image cursor;
image star;
image balloonface[3];
image bin[3];
image selectball[2][3];

SDL_Surface *surface[17];

void CopySurfaceToImage(SDL_Surface *surf, image *img)
{
	img->width = surf->w;
	img->height = surf->h;
	img->bits = surf->format->BitsPerPixel;
	img->size = img->width * img->height;
	img->data = (unsigned int*)malloc(img->size * sizeof(unsigned int));

	unsigned char *src8 = (unsigned char*)surf->pixels;
	unsigned int *dest = img->data;

	int i, a, r, g, b;

	for (i=0; i<img->size; i++)
	{
		r = *src8++;
		g = *src8++;
		b = *src8++;
		if (img->bits==24) a = 255;
		else a = *src8++;
		*dest++ = (a<<24) | (r<<16) | (g<<8) | b;
	}
}

void LoadImages()
{
    surface[0] = IMG_Load("./Data/Images/Backgrounds/Background1.png");
    surface[1] = IMG_Load("./Data/Images/Backgrounds/Background2.png");
    surface[2] = IMG_Load("./Data/Images/Sprites/LunaLu.png");
    surface[3] = IMG_Load("./Data/Images/Sprites/BalloonScared.png");
    surface[4] = IMG_Load("./Data/Images/Sprites/BalloonShocked.png");
    surface[5] = IMG_Load("./Data/Images/Sprites/BalloonVampire.png");
    surface[6] = IMG_Load("./Data/Images/Sprites/MouseCursor.png");
    surface[7] = IMG_Load("./Data/Images/Sprites/Star.png");
    surface[8] = IMG_Load("./Data/Images/Backgrounds/BinRed.png");
    surface[9] = IMG_Load("./Data/Images/Backgrounds/BinGreen.png");
    surface[10] = IMG_Load("./Data/Images/Backgrounds/BinBlue.png");
    surface[11] = IMG_Load("./Data/Images/Backgrounds/SelectBallRed_Empty.png");
    surface[12] = IMG_Load("./Data/Images/Backgrounds/SelectBallGreen_Empty.png");
    surface[13] = IMG_Load("./Data/Images/Backgrounds/SelectBallBlue_Empty.png");
    surface[14] = IMG_Load("./Data/Images/Backgrounds/SelectBallRed.png");
    surface[15] = IMG_Load("./Data/Images/Backgrounds/SelectBallGreen.png");
    surface[16] = IMG_Load("./Data/Images/Backgrounds/SelectBallBlue.png");

	CopySurfaceToImage(surface[0], &background[0]);
	CopySurfaceToImage(surface[1], &background[1]);
	CopySurfaceToImage(surface[2], &lunalu);
	CopySurfaceToImage(surface[3], &balloonface[0]);
	CopySurfaceToImage(surface[4], &balloonface[1]);
	CopySurfaceToImage(surface[5], &balloonface[2]);
	CopySurfaceToImage(surface[6], &cursor);
	CopySurfaceToImage(surface[7], &star);
	CopySurfaceToImage(surface[8], &bin[0]);
	CopySurfaceToImage(surface[9], &bin[1]);
	CopySurfaceToImage(surface[10], &bin[2]);
	CopySurfaceToImage(surface[11], &selectball[0][0]);
	CopySurfaceToImage(surface[12], &selectball[0][1]);
	CopySurfaceToImage(surface[13], &selectball[0][2]);
	CopySurfaceToImage(surface[14], &selectball[1][0]);
	CopySurfaceToImage(surface[15], &selectball[1][1]);
	CopySurfaceToImage(surface[16], &selectball[1][2]);

	int i;
	for (i=0; i<17; i++)
	    free(surface[i]);
}
