#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <SDL/SDL.h>
#include "ZeEngineGP2X.h"
#include "precalcs.h"
#include "engine3d.h"

SDL_Surface *screen;
SDL_Joystick *joystick;
SDL_Event event;

int quit = 0;

int nfrm=0, pfrm=0;
int fps=0, atime=0;
int partime=0, prticks=0;
char sbuffer[64];

extern unsigned int RenderMode = ENVMAP;
short ObjectNumber = 0;
unsigned int maxobj = 6;

extern unsigned short shades[64][256];
extern unsigned short env1[];
extern object3d object[256];
int zb = 0;


void exit2menu()
{
	chdir("/usr/gp2x");
	execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
}


void CheckJoy()
{
	while (SDL_PollEvent(&event))
	{
		switch (event.type) 
		{
        case SDL_JOYBUTTONDOWN:
            switch(event.jbutton.button)
            {
                case GP2X_BUTTON_START:
                    quit = 1;
                    break;
                case GP2X_BUTTON_L:
                    ObjectNumber = (ObjectNumber + 1) % maxobj;
                    break;
                case GP2X_BUTTON_R:
                    ObjectNumber--;
                    if (ObjectNumber<0) ObjectNumber = maxobj - 1;
                    break;
                case GP2X_BUTTON_A:
                    RenderMode = RenderMode>>1;
                    if (RenderMode==0) RenderMode = ENVMAP;
                    break;
                case GP2X_BUTTON_B:
                    RenderMode = RenderMode<<1;
                    if (RenderMode==TEXTURE) RenderMode = POINTS;
                    break;
	     }
        break;
	}
     }
}


void ClearScreen()
{
    fast_memset((unsigned int*)screen->pixels, 0, sizeof(unsigned short) * SSIZE);
}


void RunScene3d(unsigned short *vram)
{
    switch(ObjectNumber)
    {
        case 0:
            object[ObjectNumber].rot.x = prticks/512.0;
            object[ObjectNumber].rot.y = -prticks/768.0;
            Calc3d(&object[ObjectNumber]);
            zb = 0;
            Render(&object[ObjectNumber], shades[1], env1, 64, vram);
        break;

        case 1:
            object[ObjectNumber].rot.x = sin(prticks/512.0)*1.0;
            object[ObjectNumber].rot.y = PI/2 + sin(prticks/768.0)*0.75;
            Calc3d(&object[ObjectNumber]);
            zb = 0;
            Render(&object[ObjectNumber], shades[1], env1, 64, vram);
        break;

        case 2:
            object[ObjectNumber].rot.y = -prticks/1024.0;
            Calc3d(&object[ObjectNumber]);
            zb = 0;
            Render(&object[ObjectNumber], shades[1], env1, 64, vram);
        break;

        case 3:
            object[ObjectNumber].rot.x = prticks/512.0;
            object[ObjectNumber].rot.y = -prticks/768.0;
            Calc3d(&object[ObjectNumber]);
            zb = 0;
            Render(&object[ObjectNumber], shades[1], env1, 64, vram);
        break;

        case 4:
            object[ObjectNumber].rot.y = -prticks/1024.0;
            Calc3d(&object[ObjectNumber]);
            zb = 0;
            Render(&object[ObjectNumber], shades[1], env1, 64, vram);
        break;

        case 5:
            object[ObjectNumber].rot.x = PI/2;
            object[ObjectNumber].rot.y = -prticks/768.0;
            Calc3d(&object[ObjectNumber]);
            zb = 1;
            Render(&object[ObjectNumber], shades[1], env1, 64, vram);
        break;
    }
}

void Script()
{
    unsigned short *vram = (unsigned short*)screen->pixels;
    prticks = SDL_GetTicks() - partime;

    ClearScreen();
	RunScene3d(vram);
}



void Init()
{
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) 
    	exit2menu();

	screen = SDL_SetVideoMode(WIDTH, HEIGHT, 16, SDL_SWSURFACE);
	if (!screen)
        exit2menu();

	joystick = SDL_JoystickOpen(0);
	if (!joystick)
        exit2menu();

	SDL_ShowCursor(SDL_DISABLE);
	precalcs();
}

void CountFps()
{
	if (SDL_GetTicks()-atime>=1000)
	{
		atime = SDL_GetTicks();
		fps=(nfrm-pfrm);
		pfrm=nfrm;
	}
	sprintf(sbuffer, "FPS = %d", fps);
    DrawText(8, 16, 10, sbuffer, 16, (unsigned short*)screen->pixels);
}


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

	do{
    SDL_LockSurface(screen);
	Script();
	CountFps();
	nfrm++;
    SDL_UnlockSurface(screen);

    SDL_Flip(screen);

	CheckJoy();
	}while(!quit);

    SDL_Quit();
	exit2menu();
	return 0;
}
