/* ctrace.c */

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h> // FIXME

#include "config.h"
#include "object.h"
#include "illum.h"
#include "camera.h"
#include "scene.h"
#include "lodepng/lodepng.h"

/* Program entry point */

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

	char* fname;	
	if (argc != 2) { 
		fputs("WARNING: No output file specified, using out.png\n", stderr);
		fname = "out.png";
	} else fname = argv[1];
	
	/* Initialise scene */
	
	camera theCamera;
	scene_init(&theCamera);
	
#ifdef ENABLE_FSAA
	theCamera.v->width  *= 2;
	theCamera.v->height *= 2;
#endif
	
	/* Render to pixel buffer */
	
	u32 canvas_size = (WIN_SIZE_X * WIN_SIZE_Y) * sizeof(u32);	
	u32 *canvas = (u32*) malloc(canvas_size);
	memset(canvas, 0xFF, canvas_size);
	
	fprintf(stderr, "INFO: Rendering... ");	
	long timePre = GetTickCount();	
	camera_paint(&theCamera, canvas);
	long timeAft = GetTickCount();
	
	/* Output to file */

	LodePNG_encode32_file(
		fname, (const unsigned char*)canvas,
		WIN_SIZE_X, WIN_SIZE_Y
	);	
	fprintf(
		stderr,
		"done.\nINFO: Rendered in %2.2fs.\n",
		(timeAft-timePre) / 1000.0
	);
	
	/* Tidy up */	

	free(canvas);
	scene_free(&theCamera);
	
	return 0;
}
