#include <stdio.h>
#include <SDL/SDL.h>
#include <string.h>
#include "main.h"
#include "images.h"
#include "precalcs.h"
#include "script.h"
#undef main

SDL_Surface *screen;
SDL_Surface *gamescreen;
SDL_Surface *panel1, *panel2;
bool fullscreen = false;
bool ballphysics = true;

float ballgravity = 0.05;
float blobmassgravity = 0.005;
float stiffness = 256;

int atomblockx = 3;
int atomblocky = 3;

// ==== ReadCFG ====

void readCFG()
{
	char fls[] = "FULLSCREEN=";
	char bph[] = "BALLPHYSICS=";
	char tr[] = "TRUE";
	char fl[] = "FALSE";

	char bgr[] = "BALLGRAVITY=";
	char bmg[] = "BLOBMASSGRAVITY=";
	char stf[] = "STIFFNESS=";

	char abx[] = "ATOMBLOCKX=";
	char aby[] = "ATOMBLOCKY=";

	char text[256];
	char num[64];

	FILE *cfgfile;
	cfgfile = fopen("LunaLu.cfg","rb");

	while (!feof(cfgfile))
	{
		fgets (text, 256, cfgfile);
		if (strncmp(text, "//", 2)!=0)
		{
			if (strncmp(text, fls, sizeof(fls)-1)==0)
			{
			    if (strncmp(&text[sizeof(fls)-1], tr, sizeof(tr)-1)==0)
			        fullscreen = true;
				else
					fullscreen = false;
			}

			if (strncmp(text, bph, sizeof(bph)-1)==0)
			{
			    if (strncmp(&text[sizeof(bph)-1], tr, sizeof(tr)-1)==0)
			        ballphysics = true;
				else
					ballphysics = false;
			}
			if (strncmp(text, bgr, sizeof(bgr)-1)==0)
			{
				strcpy(num, &text[sizeof(bgr)-1]);
				ballgravity = atof(num);
			}
			if (strncmp(text, bmg, sizeof(bmg)-1)==0)
			{
				strcpy(num, &text[sizeof(bmg)-1]);
				blobmassgravity = atof(num);
			}
			if (strncmp(text, stf, sizeof(stf)-1)==0)
			{
				strcpy(num, &text[sizeof(stf)-1]);
				stiffness = atof(num);
				if (stiffness<16) stiffness = 16;
				if (stiffness>512) stiffness = 512;
			}

			if (strncmp(text, abx, sizeof(abx)-1)==0)
			{
				strcpy(num, &text[sizeof(abx)-1]);
				atomblockx = atof(num);
				if (atomblockx<1) atomblockx = 1;
				if (atomblockx>6) atomblockx = 6;
			}
			if (strncmp(text, aby, sizeof(aby)-1)==0)
			{
				strcpy(num, &text[sizeof(aby)-1]);
				atomblocky = atof(num);
				if (atomblocky<1) atomblocky = 1;
				if (atomblocky>6) atomblocky = 6;
			}
		}
	}
	fclose(cfgfile);
}


// ==== Init ====

void InitSurfaces()
{
    gamescreen = SDL_CreateRGBSurface(SDL_SWSURFACE, GameScreenWidth, GameScreenHeight, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
    panel1 = SDL_CreateRGBSurface(SDL_SWSURFACE, PanelScreenWidth, GameScreenHeight, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
    panel2 = SDL_CreateRGBSurface(SDL_SWSURFACE, ScreenWidth, PanelScreenHeight, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
}

void Init()
{
    readCFG();
    int fs = 0;
    if (fullscreen) fs = SDL_FULLSCREEN;
    screen = SDL_SetVideoMode(ScreenWidth, ScreenHeight, 32, SDL_SWSURFACE | fs);
    SDL_WM_SetCaption ("Luna Lu concept preview, copyright Dot Red Games Ltd. 2007", 0);
    SDL_ShowCursor(SDL_DISABLE);
	InitLevels();
	InitSurfaces();
	LoadImages();
	Precalcs();
}

// ==== Main ====

int main( int argc, char* argv[] )
{
    Init();
	Run();
    return 0;
}
