/*
    MegaDemo IV 2 Main
*/

#include "m_main.h"
#include "l_main.h"
#include "g_main.h"
#include "i_main.h"
#include "s_main.h"
#include "script.h"
#include "benchm.h"

#include <stdio.h>

#define _init (void *( *)(void *,cbitmap *,cbitmap *,int ))
#define _doit (void ( *)(void *,cbitmap *,cbitmap *,cbitmap *,int, int ))
#define _exit (void ( *)(void *))

int     systemtime;


void inittext ()
{
  int i=0;

  Stagedat *temp[64];
  for (i=0;i<64;i++)
    temp[i]=Stageline[i];


  for ( i=nbText-16;i<(nbText-12);i++)
  {
    textures[i]=m_allocbitmap (128,128,8);
    textures[i+4]=m_allocbitmap (160,128,8);
    textures[i+8]=m_allocbitmap (128,128,18);
    textures[i+12]=m_allocbitmap (160,128,18);
  }


  i=0;
  while (temp[i])
  {
    while ( temp[i]->inittime!=-1)
    {
      if (temp[i]->s1!=-1)
        if (!textures[temp[i]->s1])
            textures[temp[i]->s1]=m_loadbitmap (textnames[temp[i]->s1]);

      if (temp[i]->s2!=-1)
          if (!textures[temp[i]->s2])
            textures[temp[i]->s2]=m_loadbitmap (textnames[temp[i]->s2]);


        temp[i]++;
      }
      i++;
  }
}


cbitmap *gettext (int i)
{
  if (i==-1)
    return 0;
  if (textures[i])
    return (textures[i]);
    else
    {
      textures[i]=m_loadbitmap (textnames[i]);
      return     textures[i];
    }
}


void exittext ()
{
  int i;
  for ( i=0;i<nbText;i++)
  {
     if (textures[i]) m_freebitmap (textures[i]);
  }
}


int count=0,endtime;


void megademo42 (void)
{
  int stime;
  int key;

  i_init();
  m_init();
  l_init();
  g_init();

#define __SOUND__

#ifdef __SOUND__

  s_init();

#endif

  Init_Tabs();
  inittext ();

#ifdef __SOUND__

  s_start();

#endif

  stime=l_gettime();
  while ((key=i_getkey() )!=i_esc && yet<185*sec)
  {
    int i=0;
    int timer=yet;
    systemtime=timer;

    if ( key==i_space )
      stime-=sec;

    while (Stageline[i])
    {

      if ( Stageline[i]->inittime!=-1)
      {
        if (Stageline[i]->endtime<=timer)
        {
           (_exit Stageline[i]->exit) (Stageline[i]->initdat);
           Stageline[i]++;
        }

        if (Stageline[i]->inittime!=-1) {

            if ( Stageline[i]->inittime<=timer && !Stageline[i]->initdat)
              Stageline[i]->initdat=(_init Stageline[i]->init)
              (Stageline[i]->dat,gettext(Stageline[i]->s1),gettext(Stageline[i]->s2),Stageline[i]->endtime-Stageline[i]->starttime);

            if ( Stageline[i]->starttime<=timer) {

              b_start();
              (_doit Stageline[i]->doit) (Stageline[i]->initdat,gettext(Stageline[i]->s1),gettext(Stageline[i]->s2),
              gettext(Stageline[i]->d), timer-Stageline[i]->starttime, Stageline[i]->endtime-Stageline[i]->starttime);
              b_stop((void*)Stageline[i]->doit);
              }
        }
      }
      i++;
    }
    b_start();
    g_update ((rgb18 *)(gettext(t60a))->data);
    b_stop((void*)1);

    count++;

    s_play(l_gettime());

  }


  endtime=l_gettime();

  exittext ();
#ifdef __SOUND__

  s_shutdown();

#endif
  printf("%i Frames rendered in %f secs.\n",count,(((float)(endtime-stime))/sec));
  printf("%f FPS\n",((float)count)/(((float)(endtime-stime)/sec)));

  l_shutdown();
  m_shutdown();
  i_shutdown();
// g_shutdown();
  b_result();
  while (1==1);

}

