/***************************************************************************
                         main.c  -  description
                             -------------------
    begin                : Sat Apr 5 2003
    copyright            : (C) 2003 by Nick Winters
    email                : n_winters99 yahoo.com
 ***************************************************************************/

#include "includes.h"

static SDL_Surface *screen;
static int fullscreen = 0; // during execution 1 is full, 0 is not
// but before execution 1 would mean not, 0 would mean yes

// variables so that may be specified in commandline arguments
static int screenWidth = SCREEN_WIDTH;
static int screenHeight = SCREEN_HEIGHT;
static int screenBits = SCREEN_BITS;

SDL_Surface *getScreen() {
  // only used in main.c?
  return screen;
}

int getScreenWidth() {
  return screenWidth;
}
int getScreenHeight() {
  return screenHeight;
}

void toggleFullscreen() {
  SDL_GL_SetAttribute( SDL_GL_RED_SIZE, screenBits / 3 ); // 5 for 16bit color
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, screenBits / 3 );
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, screenBits / 3 );
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); // ? precision of depth buffer
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  // will quit if encounters error, no need for a return value
  if (fullscreen == 0) {
    screen = SDL_SetVideoMode(
      screenWidth, screenHeight, screenBits,
      SDL_OPENGL|SDL_FULLSCREEN);
    fullscreen = 1;
  } else {
    screen = SDL_SetVideoMode(
      screenWidth, screenHeight, screenBits,
      SDL_OPENGL|SDL_HWSURFACE); // |SDL_NOFRAME
    fullscreen = 0;
  }
  if ( ! screen ) {
    fprintf(stderr, "Couldn't set a particular GL video mode: %s\n", SDL_GetError());
    SDL_Quit();
    exit(2); // what is this error code?
  }
}
/********************************************************
 *                    Main function!                    *
 * really only calls important parts of other functions *
 ********************************************************/
int main(int argc, char *argv[])
{
  enum gameReturn thisGameReturn;
  int numScenes = -1, i, numValues, code;
	FILE *f;
	char tempString[20];
  
  char **sceneFiles; // to be an array of strings length MAX_FILENAME_LENGTH ?


  // this part should be based on cmdline args
  // I should loop through them....
  if (argc > 0) {
    for (i =1; i< argc; i++)
    // starts at 1 because I don't need to know the command
    // used to start this
    {
      // placeholder for something useful
      printf("%d %s\n",i, argv[i]);
      /*
      -fullscreen or -nofullscreen
      rez=640.480.16
      -sceneList=ohMy.scene
			--donthidecursor // but another option to show the gl cursor?
			--dontgrabcursor
			--help -h
      */
    }
  }

	printf("loading list of scenes...\n");

  f = fopen("scenelist.txt", "r");
	if (f) {
    // read the file...
    // first lines says how many levels the game has
    numValues = fscanf(f, "%s %d", tempString, &numScenes);
    printf("%d scenes\n", numScenes);
    ////printf("%d / 2\n", numScenes);
    if (numValues == 2 && numScenes > 0) {
      sceneFiles = (char**)calloc(numScenes, sizeof(char*));
      ////printf("allocate memory for char**\n");
      for (i = 0; i<numScenes; i++) {
        sceneFiles[i] = (char*)calloc(MAX_FILENAME_LENGTH,sizeof(char)); // memory for string
        ////printf("allocated memory for char*\n");
        if (numValues) numValues = fscanf(f,
          "%s %"MAX_FILENAME_LENGTH_AS_STRING"s", tempString, sceneFiles[i]);
        ////printf("%d / 2\n", numValues);
        if (numValues < 2) break;
      }
      // i should be > numScenes if all of them were loaded
      if (i < numScenes) {
        printf("Not all scene entries valid\n");
        numScenes = i;  // kinda a hack, but works
        // free the memory I created, may segfault...
        for (i = 0; i<numScenes; i++) {
          if (sceneFiles[i]) free(sceneFiles[i]);
        }
        free(sceneFiles);
        numScenes = 0;
      }
    } else {
      numValues = 0;
      printf("invalid first line, or number of scenes stated\n");
    }
    if (!numValues) {
      printf("Error in scene list file, scenelist.txt\n");
      return 0; // maybe another error code?
    }
  }
  if (!f) {
		printf("Unable to open scenelist.txt, exiting...\n");
		return 0; // maybe another error code?
	}
  fclose(f);
  printf("done loading scene list\n"); 

  /////////////////////////////////
  /////////////////////////////////
  // in it's own little world since I was having problems with it before
  // due to some other code, and am too lazy to change it now
  // returns -1 on failure
  code = SDL_Init(SDL_INIT_VIDEO);
  if (code) {
    printf("SDL_Init failed with code %d\n", code);
  } 

  ////printf("going to do fullscreen thing...\n");

  // set the icon here
  //SDL_WM_SetIcon(SDL_LoadBMP("nickGame.ico"), NULL);

  // this will initalize the window
  toggleFullscreen();

  SDL_WM_SetCaption("Almost Bowling", "whatDoesThisDo?");

  text_init(screenWidth, screenHeight);

  // useful opengl information...
  printf("OpenGL Vendor:   %s\n",glGetString(GL_VENDOR));
  printf("OpenGL Renderer: %s\n", glGetString(GL_RENDERER));
  printf("OpenGL Version:  %s\n",glGetString(GL_VERSION));

  /////////////////////////////////
  /////////////////////////////////  

	// the system cursor is invisible
	SDL_ShowCursor(0);
	// and it cannot exit the window
  SDL_WM_GrabInput(SDL_GRAB_ON); 


  //// loop through the levels ////
  i = 0;
  do {
    //printf("doing scene %s, or number %d\n", sceneFiles[i], i);
    if (i+1 != numScenes) {
      thisGameReturn = sceneMain(sceneFiles[i], awaitingThrow);
    } else {
      thisGameReturn = sceneMain(sceneFiles[i], over);
    }
    if (thisGameReturn != restart) {
      i++;
    }
    if (i+1>numScenes) {
      printf("completed game...\n");
      i = 0;
    }
  } while (thisGameReturn != quit);


  // if you quit early this is the only memory leak I know of
  for (i = 0; i<numScenes; i++) {
    if (sceneFiles[i]) free(sceneFiles[i]);
  } 
  free(sceneFiles);
  SDL_Quit();
  printf("\n....Done....\n");
  return 0;
}
