#include "main.h"
#define SCROLLTEXT "                 HELLO HAPPY SCENERS! INTROX PRESENTS USCH, THE GROUPS FIRST OFFICIAL ACCELERATED DEMO! HAVE YOU SEEN FY, THE LITTLE BROTHER TO THIS DEMO?, INTROX KEEP IT REAL WITH USCH AND FY!    THIS SCROLLER IS PRETTY HARD TO READ BUT I NEVER HEARD ANYONE COMPLAIN ABOUT SUCH A THING ON THE AMIGA!  CHEERS TO EVERYONE WHO RELEASED SOMETHING AT REMEDY 11, YOU KEEP THE SPIRIT ALIVE!             "

float tmpx=0.0, tmpy=90.0,tmpzoom=-40.0f;
float alpha=0.0f;
GLint te=0;

void handle_key( SDL_keysym *keysym )
{
  switch ( keysym->sym )
    {
    case SDLK_ESCAPE:
      exit( 0 );
      break;
      /* temp */
 /*--------------------*/
    case SDLK_LEFT:
      tmpx -= 1.0f;
      break;
    case SDLK_RIGHT:
      tmpx += 1.0f;
      break;
    case SDLK_UP:
      tmpy += 1.0f;
      break;
    case SDLK_DOWN:
      tmpy -= 1.0f;
      break;
    case SDLK_PLUS:
      tmpzoom += 1.0f;
      break;
    case SDLK_MINUS:
      tmpzoom -= 1.0f;
      break;
    case SDLK_k:
      alpha -= 0.5;
      break;
  /*-----------------------*/
						      
    case SDLK_F1:
      SDL_WM_ToggleFullScreen( screen );
      break;
    default:
      break;
    }  
  return;
}

void putb( int c, float x, float y ){
  int xl, yl;

  glPushMatrix();
  glTranslatef( x,y,0.0f );
  for( xl = 0; xl < 4; xl++ ){
    for(yl = 0; yl < 4; yl++ ){
      if( font[c][xl][yl] == 1 )
	glCallList(spheredl);
      glTranslatef(4.0f, 0.0f, 0.0f );
    }
    glTranslatef( -16.0f, 4.0f, 0.0f );
  }
  glPopMatrix();
  return;
}

void skybox( void ){

  glBindTexture( GL_TEXTURE_2D, btexture[0] );
  glBegin( GL_QUADS);
  /*
        glNormal3f( 0.0f, 0.0f, 1.0f);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-40.0f, -30.0f,  30.0f); //Front//
        glTexCoord2f(1.0f, 0.0f); glVertex3f( 40.0f, -30.0f,  30.0f);
        glTexCoord2f(1.0f, 1.0f); glVertex3f( 40.0f,  30.0f,  30.0f);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-40.0f,  30.0f,  30.0f);
	*/
    
        glNormal3f( 0.0f, 0.0f,-1.0f);
        glTexCoord2f(1.0f, 0.0f); 
	glVertex3f(-40.0f, -30.0f, -30.0f);/*Back*/
        glTexCoord2f(1.0f, 1.0f); 
	glVertex3f(-40.0f,  30.0f, -30.0f);
        glTexCoord2f(0.0f, 1.0f);
	glVertex3f( 40.0f,  30.0f, -30.0f);
        glTexCoord2f(0.0f, 0.0f); 
	glVertex3f( 40.0f, -30.0f, -30.0f);
   
        glNormal3f( 0.0f, 1.0f, 0.0f);
        glTexCoord2f(0.0f, 1.0f); 
	glVertex3f(-40.0f,  30.0f, -30.0f);/*Top*/
        glTexCoord2f(0.0f, 0.0f); 
	glVertex3f(-40.0f,  30.0f,  30.0f);
        glTexCoord2f(1.0f, 0.0f); 
	glVertex3f( 40.0f,  30.0f,  30.0f);
        glTexCoord2f(1.0f, 1.0f); 
	glVertex3f( 40.0f,  30.0f, -30.0f);
  
        glNormal3f( 0.0f,-1.0f, 0.0f);
        glTexCoord2f(1.0f, 1.0f); 
	glVertex3f(-40.0f, -30.0f, -30.0f);/*Bottom*/
        glTexCoord2f(0.0f, 1.0f); 
	glVertex3f( 40.0f, -30.0f, -30.0f);
        glTexCoord2f(0.0f, 0.0f); 
	glVertex3f( 40.0f, -30.0f,  30.0f);
        glTexCoord2f(1.0f, 0.0f); 
	glVertex3f(-40.0f, -30.0f,  30.0f);
  
        glNormal3f( 1.0f, 0.0f, 0.0f);
        glTexCoord2f(1.0f, 0.0f); 
	glVertex3f( 40.0f, -30.0f, -30.0f);/*Right*/
        glTexCoord2f(1.0f, 1.0f); 
	glVertex3f( 40.0f,  30.0f, -30.0f);
        glTexCoord2f(0.0f, 1.0f); 
	glVertex3f( 40.0f,  30.0f,  30.0f);
        glTexCoord2f(0.0f, 0.0f); 
	glVertex3f( 40.0f, -30.0f,  30.0f);

        glNormal3f(-1.0f, 0.0f, 0.0f);
        glTexCoord2f(0.0f, 0.0f); 
	glVertex3f(-40.0f, -30.0f, -30.0f);/*Left*/
        glTexCoord2f(1.0f, 0.0f); 
	glVertex3f(-40.0f, -30.0f,  30.0f);
        glTexCoord2f(1.0f, 1.0f); 
	glVertex3f(-40.0f,  30.0f,  30.0f); 
        glTexCoord2f(0.0f, 1.0f); 
	glVertex3f(-40.0f,  30.0f, -30.0f);
  glEnd();
  return;
}
  
int chartopint( char ch ){
  int retval;

  retval = (int)ch-32;

  if( retval < 0 || retval >65 )
    return 0;
  else
    return retval;
}

int drawGLScene( int itr ){
  static GLint letter = 0;
  static GLfloat xpos=0.0f;
  static GLfloat x=0.0/*,y=0.0*/;
  int tmp[6], i;
  
  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
  glLoadIdentity();

  glTranslatef( 0.0f, 0.0f, tmpzoom+itr );

  gluLookAt(40*cos(tmpx*PI/180)*cos(tmpy*PI/180),40*sin(tmpx*PI/180),
	    40*cos(tmpx*PI/180)*sin(tmpy*PI/180), 0.0,0.0,0.0, 0.0, 1.0, 0.0 );
    
#ifndef CRAP
  skybox();
#endif


  glRotatef( 180.0f, 1.0f, 0.0f, 0.0f );

  
  xpos -= 0.5;
  if( xpos <= -20.0f){
    letter++;
    if( letter == (strlen( SCROLLTEXT ) -5 )){ /* End of textbuffer */
      return 2;
      letter =0;
    }
    xpos = 0.0f;
    alpha =0.0f;
  }

  
  if( x >= 360 )
    x=0;
  te = SDL_GetTicks();

  for( i = 0; i < 6; i++ ){
    tmp[i] = chartopint( SCROLLTEXT[letter+i] );
  }
  
  if( te <= 15000 ){
    glPushMatrix();
    glTranslatef( 0.0f, -6.0f, 0.0f );
    putb( tmp[0], xpos-40.0f,0.0f );
    putb( tmp[1], xpos-20.0f, 0.0f );
    putb( tmp[2], xpos+0.0f, 0.0f );
    putb( tmp[3], xpos+20.0f, 0.0f );
    putb( tmp[4], xpos+40.0f, 0.0f );
    putb( tmp[5], xpos+60.0f, 0.0f );
    glPopMatrix();
    x = 0;
  } else if( te >= 10000 && te <= 20000 ){
    x+=1;
    glPushMatrix();
    glRotatef( x, 1.0f, 0.0f, 0.0f ); 
    glPushMatrix();
    glTranslatef( 0.0f, -6.0f, 0.0f );
    putb( tmp[0], xpos-40.0f,0.0f );
    putb( tmp[1], xpos-20.0f, 0.0f );
    putb( tmp[2], xpos+0.0f, 0.0f );
    putb( tmp[3], xpos+20.0f, 0.0f );
    putb( tmp[4], xpos+40.0f, 0.0f );
    putb( tmp[5], xpos+60.0f, 0.0f );
    glPopMatrix();
    glPopMatrix();
  } else if( te >= 20000 && te <= 30000 ){
    if( x != 0 ){
      x+=1;
      glRotatef( x, 1.0f, 0.0f, 0.0f );
    }
    glPushMatrix();
    glTranslatef( 0.0f, -6.0f, 0.0f );
    putb( tmp[0], xpos-40.0f,0.0f );
    putb( tmp[1], xpos-20.0f, 0.0f );
    putb( tmp[2], xpos+0.0f, 0.0f );
    putb( tmp[3], xpos+20.0f, 0.0f );
    putb( tmp[4], xpos+40.0f, 0.0f );
    putb( tmp[5], xpos+60.0f, 0.0f );
    glPopMatrix();
  }else if( te >= 30000 && te <= 40000 ){
    x+=1;
    glPushMatrix();
    glTranslatef( 0.0f, -6.0f, 0.0f );
    glRotatef( x, 0.0f, 1.0f, 0.0f);
    putb( tmp[0], xpos-40.0f,0.0f );
    putb( tmp[1], xpos-20.0f, 0.0f );
    putb( tmp[2], xpos+0.0f, 0.0f );
    putb( tmp[3], xpos+20.0f, 0.0f );
    putb( tmp[4], xpos+40.0f, 0.0f );
    putb( tmp[5], xpos+60.0f, 0.0f );
    glPopMatrix();
  } else if( te >= 40000 && te <= 50000 ){
    glPushMatrix();
    glTranslatef( 0.0f, -6.0f, 0.0f );
    if( x != 0 ){
      x += 1;
      glRotatef( x, 0.0f, 1.0f, 0.0f );
    }
    putb( tmp[0], xpos-40.0f,0.0f );
    putb( tmp[1], xpos-20.0f, 0.0f );
    putb( tmp[2], xpos+0.0f, 0.0f );
    putb( tmp[3], xpos+20.0f, 0.0f );
    putb( tmp[4], xpos+40.0f, 0.0f );
    putb( tmp[5], xpos+60.0f, 0.0f );
    glPopMatrix();
  }else if( te >= 50000 && te <= 60000){
    x+=1;
    glPushMatrix();
    glTranslatef( 0.0f, -6.0f, 0.0f );
    glRotatef( x, 0.0f, 0.0f, 1.0f);
    putb( tmp[0], xpos-40.0f,0.0f );
    putb( tmp[1], xpos-20.0f, 0.0f );
    putb( tmp[2], xpos+0.0f, 0.0f );
    putb( tmp[3], xpos+20.0f, 0.0f );
    putb( tmp[4], xpos+40.0f, 0.0f );
    putb( tmp[5], xpos+60.0f, 0.0f );  
    glPopMatrix();
  } else {
    glPushMatrix();
    glTranslatef( 0.0f, -6.0f, 0.0f );
    if( x != 0 ){
      x+= 1;
      glRotatef( x, 0.0f, 0.0f, 1.0f );
    }
    putb( tmp[0], xpos-40.0f,0.0f );
    putb( tmp[1], xpos-20.0f, 0.0f );
    putb( tmp[2], xpos+0.0f, 0.0f );
    putb( tmp[3], xpos+20.0f, 0.0f );
    putb( tmp[4], xpos+40.0f, 0.0f );
    putb( tmp[5], xpos+60.0f, 0.0f );
    glPopMatrix();
  }
					
    
  glPopMatrix();
  
  SDL_GL_SwapBuffers( );

  /*
  frames++;
  t = SDL_GetTicks();
  if (t - t0 >= 5000) {
    seconds = (t - t0) / 1000.0;
    fps = frames / seconds;
    printf("%d frames in %g seconds = %g FPS\n", frames, seconds, fps);
    t0 = t;
    frames = 0;	  
  }
  */
	
  return 0;
}

void intro( int i ){
  GLfloat tx=0.0f, ty =0.0f;
  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
  glLoadIdentity();
  
  glTranslatef( 0.3f, 0.0f, -3.0f-i );

  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
      
  tx = 20.0f;
  ty =  130.0f;
 
  gluLookAt(cos(tx*PI/180)*cos(ty*PI/180),sin(tx*PI/180),
	    cos(tx*PI/180)*sin(ty*PI/180), 0.0,0.0,0.0, 0.0, 1.0, 0.0 );

  drawObject();

  glDisable(GL_COLOR_MATERIAL);
  glDisable(GL_LIGHTING);
  glDisable(GL_LIGHT0);
	
  SDL_GL_SwapBuffers( );
  
  return;
}

void end( int i ){
  int x = 0, y = 0;
  static float tmpr=0.0f;
  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
  glLoadIdentity();

  glTranslatef( 0.0f, 0.0f, -10.0f-i );
  glRotatef( -90.0f, 0.0f, 0.0f, 1.0f );
   
   
  gluLookAt(cos(tmpx*PI/180)*cos(tmpy*PI/180),sin(tmpx*PI/180),
	    cos(tmpx*PI/180)*sin(tmpy*PI/180), 0.0,0.0,0.0, 0.0, 1.0, 0.0 );  
  
  for( x = 0; x <4;x++ ){
    for( y = 0; y <4; y++ ){
      curval = dspspect[(x+1)*(y+1)]*4;
      grid4x4[x][y][2] = curval ;
      if( x*y == 1)
	tmpr += curval;
    }
  }
  
  glBindTexture( GL_TEXTURE_2D, credits[0] );
  glRotatef(tmpr, 1.0f, 0.0f, 0.0f );
  glPushMatrix();
  glTranslatef( 0.0f, 0.0f, 2.0f );
  evaluateGrid();			/* FRONT */
  glTranslatef( 0.0f, 2.0f, -2.0f );
  glRotatef( -90.0f, 1.0f, 0.0f, 0.0f );

  glBindTexture( GL_TEXTURE_2D, credits[1] );
  evaluateGrid();			/* RSIDE */
  glTranslatef( 0.0f, 0.0f, -4.0f );
  glRotatef( 180.0f, 1.0f, 0.0f, 0.0f );
  glBindTexture( GL_TEXTURE_2D, credits[2]);
  evaluateGrid();			/* LSIDE */
  
  glTranslatef(0.0f, -2.0f, -2.0f );
  glRotatef( 90.0f, 1.0f, 0.0f, 0.0f );
  glBindTexture( GL_TEXTURE_2D, credits[3]);
  evaluateGrid();			/* BACK */
  
  glPopMatrix();
  glPushMatrix();
  glTranslatef( 2.0f, 0.0f, 0.0f );
  glRotatef( -90.0f, 1.0f, 0.0f, 0.0f);
  glRotatef( 90.0f, 0.0f, 1.0f, 0.0f );
  glBindTexture(GL_TEXTURE_2D, credits[4] );
  evaluateGrid();			/* BOTTOM */
  
  glTranslatef( 0.0f, 0.0f, -4.0f );
  glRotatef( 180.0f, 0.0f, 1.0f, 0.0f );
  evaluateGrid();			/* TOP */
  
  glPopMatrix();
  
  SDL_GL_SwapBuffers( );
  return;
}

int main( void )
{
  int videoFlags;
  int running = 1;
  SDL_Event event;
  const SDL_VideoInfo *videoInfo;
  int *pvFlags= &videoFlags;
  int i=0, ends =0;
  int scrollt=0/*,tmpasd = 0*/;

  if(! (initSDL(pvFlags, videoInfo )))
     exit(1);
     
  resizeWindow( SCREEN_W, SCREEN_H );
 
  setupMesh();
  /*FMUSIC_PlaySong(tuneptr);*/
  FSOUND_Stream_Play( 0, stream );
  
  while( running ){

    while( SDL_PollEvent( &event )){
      switch( event.type ){
      case SDL_VIDEORESIZE:
	/* handle resize event */
	screen = SDL_SetVideoMode( event.resize.w,
				   event.resize.h,
				   16, videoFlags );		
	if ( !screen ){
	    fprintf( stderr, "Could not get a surface after resize: %s\n", SDL_GetError( ) );
	    exit( 1 );
	  }
	
	resizeWindow( event.resize.w, event.resize.h );
	break;

      case SDL_KEYDOWN:
	handle_key(&event.key.keysym);
	break;
      case SDL_QUIT:
	running = 0;
	break;
      default:
	break;
      }
    }

    te= SDL_GetTicks();
    /* printf("debug: %d\n", te); */
    
    /* Introtext */
    if( te <= 5000 ){
      intro(i);
      if( te >= 4500 && te <= 5000 ){
	i+=5;
      }
    } else if( te <= 150000 && ends != 1){
      /* Rotating text */
      if( (te < 99000 && i > 0)&&scrollt !=2 ){
	i-=5;
      }
      if ( drawGLScene(i) ==2 )
	scrollt = 2;
      
      /* End text */
      if( (te >= 149000 && te <= 150000 ) || (scrollt ==2) ){
	i+=5;
	if( i == 200 )
	  ends=1;
      }
    }else if( te >= 150000 || ends == 1){
      if( (te >= 150000 && i > 0)  || ( ends == 1 && i > 0) ){
	i-=5;
      }
      end(i);
    }
  }
  exit(0);

  return(0);
}
