// ---------------------------------------------------------------------------
// 
//   Lily Engine alpha
// 
//   Copyright (c) 2010 Clifford Jolly
//     http://cliffjolly.com
//     cliff@cliffjolly.com
// 
// ---------------------------------------------------------------------------
// 
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//  
//   This program is distributed in the hope that it will be useful, but
//   WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   General Public License for more details.
// 
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
//   USA
// -------------------------- END HEADER -------------------------------------

#include "../hacks/winhacks.h"

#include <iostream>
using namespace std;

#include <SDL.h>

#include "../filesystem/filesystem.h"
#include "image.h"

void testStuff(void) {

	int tgaDataLen;
	char *tgaData = FileSystem::loadFile("test.tga", &tgaDataLen);

	if(!tgaData) {
		cout << "TGA Load error" << endl;
		return;
	}

	//Pixel *pix = loadTGA(tgaData, tgaDataLen);
	Image *img = loadTGA(tgaData, tgaDataLen);

	int saveLength;
	unsigned char *saveData = img->saveTGA(&saveLength);
	FileSystem::saveFile("outcrap.tga", (char*)saveData, saveLength);

	int scrWidth  = img->getWidth();
	int scrHeight = img->getHeight();

	SDL_Init(SDL_INIT_VIDEO);
	SDL_Surface *screen = SDL_SetVideoMode(scrWidth, scrHeight, 32, 0);
	SDL_Surface *tgaSurf = img->createSurface();

	bool quit = false;

	while(!quit) {
		
		// Draw a checkered pattern thinger. We'll blit onto that so
		// we can tell when something's actually transparent.
		SDL_LockSurface(screen);
		for(int x = 0; x < scrWidth; x++) {
			for(int y = 0; y < scrHeight; y++) {
				Pixel px;
				bool inverty = ((y/8) % 2);
				bool invertx = ((x/8) % 2);
				px.value = (invertx ^ inverty) ? 0xff888888 : 0xff444444;

				*((int*)(screen->pixels) + (x + y * scrWidth)) = px.value;
			}
		}
		SDL_UnlockSurface(screen);

		// Draw the thing from the TGA.
		SDL_BlitSurface(tgaSurf, NULL, screen, NULL);

		SDL_Flip(screen);

		SDL_Event event;
		SDL_WaitEvent(&event);
		switch(event.type) {
		case SDL_QUIT:
			quit = true;
			break;
		}

	}

	delete[] tgaData;
	delete img;

	SDL_Quit();
}

int main(int argc, char *argv[]) {

	cout << "Testing TGA junk" << endl;

	testStuff();

	return 0;
}

