/*
  This file is a part of SEAL

  (c) Copyright 2001 Sealsystem / Point Mad. All rights reserved.
  (c) Copyright 1999-2000 Michal Stencl. All rights reserved.

  Seal is under the Sealsystem license. Please read
    license.txt in the main directory for more informations.

  For more informations about copyrights, read about.txt
    in the main directory.

*/

#include"screen.c" // Screen functions ...

#include"drivers\savescr.c"

#include"drivers\mouse.c" // Mouse functions ...

#include"drivers\keyboard.c" // Keyboard functions ...

#include"drivers\sound.c" // SFA from Florian Xaver



extern char* _LIBEXPORTTABLE[];

void init_ico ( void );
l_int maxstep = 13;
l_int step = 0;
l_int dstep = 0;
BITMAP *step_ico = NULL;
BITMAP *gstep_ico = NULL;
l_bool lowgrfx = false;
t_rect lowr;

void step_init ( ) {
  if ( lowgrfx ) {
    l_int w = ( screen_width - 10 * maxstep ) / 2;
    lowr = rect_assign(w,screen_height-15,w+10*maxstep,screen_height-5);
    rect(screen,lowr.a.x-2,lowr.a.y-2,lowr.b.x+2,lowr.b.y+2,0);
  } else {
    l_int gstep = 0;
    step_ico = load_image("bmp/step.bmp");
    gstep_ico = load_image("bmp/gstep.bmp");
    if ( gstep_ico ) {
      dstep = ( screen_width - ( IMAGE_WIDTH(gstep_ico) + 10 ) * maxstep ) / 2;

      while ( gstep < maxstep ) {
        if ( step_ico ) draw_sprite(screen, gstep_ico, dstep + ( IMAGE_WIDTH(gstep_ico) + 10 ) * gstep, screen_height-IMAGE_HEIGHT(gstep_ico) - 5 );
        gstep++;
      };
    };
  };
};


void step_icon ( ) {
  if ( lowgrfx ) {
    rectfill(screen,lowr.a.x+step*10,lowr.a.y,lowr.a.x+step*10+10,lowr.b.y, makecol(0,0,255) );
  } else
    if ( step_ico ) draw_sprite(screen, step_ico, dstep + ( IMAGE_WIDTH(step_ico) + 10 ) * step, screen_height-IMAGE_HEIGHT(step_ico) - 5 );
  step++;
};
/*l_ulong lasttime = 0;

void info_init ( l_text info ) {

  step_icon();

  rectfill(screen,0 ,screen_height - 15,screen_width,screen_height,makecol(255,255,255));

  textout_draw_rect(screen, get_font_in_size("ARIAL",10,10), info, -1,
                                        0 ,
                                        screen_height - 15 ,
                                        screen_width,
                                        screen_height,
                                        TX_ALIGN_CENTER, makecol(0,0,0), makecol(255,255,255), 1);

  if ( lasttime ) DEBUG_printf("\n  ... done in %d seconds\n",time(NULL)-lasttime);
  DEBUG_printf("\n  - %s\n",info);
  lasttime = time(NULL);
};

void info_title ( l_text info, l_int id  ) {
  rectfill(screen,0 ,(id*30) ,screen_width,(id*30)+30,makecol(255,255,255));

  textout_draw_rect(screen, get_font_in_size("ARIAL",20,20), info, -1,
                                        0 ,
                                        (id*30),
                                        screen_width,
                                        (id*30)+30,
                                        TX_ALIGN_CENTER, makecol(0,0,0), makecol(255,255,255), 1);

};*/

l_bool  drivers_done ( p_object o )
{
  DEBUG_printf("Done drivers...\n");
  if ( !obj_done((p_object)o) ) return false;
  DEBUG_printf("Done fonts...\n");
  fonts_done();
  DEBUG_printf("Done screen...\n");
  screen_done();
  DEBUG_printf("Done allegro...\n");
  allegro_exit();
  DEBUG_printf("Done drivers : FINISHED\n");
  return true;

};

void drv_done ( void ) {

  drivers_done ( OBJECT(drivers) );

};

void ini_ext_runner ( void );

t_drivers *drivers_init ( t_drivers *o ) {
  BITMAP *ld = NULL;
  if ( !o ) return NULL;

  memset(o, 0, sizeof(t_drivers));

  obj_init(&o->obclass);

  init_ico ( ); // hi hi !!

  screen_init ( );

  lowgrfx = get_depth(screen) < 16;

  clear_to_color(screen, makecol(255,255,255));

  if ( !lowgrfx ) ld = load_image("bmp/load.bmp");

  if ( lowgrfx ) font_smooth = false;

  step_init();
  if ( ld ) draw_sprite(screen, ld, (screen_width-IMAGE_WIDTH(ld))/2, (screen_height-IMAGE_HEIGHT(ld))/2 );

  step_icon ( );

  load_supported_fonts("/system/fonts/");

  step_icon();

  load_txts();

  step_icon();
  DEBUG_printf("   Drivers :: Load the libexportable.\n");
  DLXImport(_LIBEXPORTTABLE);
  step_icon();
  colors_init ( );
  step_icon();
  load_bmps ( );
  step_icon();
  install_int(&aclock, 20);
  step_icon();
  ini_sound ( );
  step_icon();
  OBJECT(o)->insert(OBJECT(o), OBJECT (key_init(_malloc(sizeof(t_keyboard))) ));
  step_icon();
  OBJECT(o)->insert(OBJECT(o), OBJECT( mouse_init(_malloc(sizeof(t_mouse))) ));
  step_icon();
  ini_ext_runner();
  step_icon();
  init_skins();
  step_icon();
  RunAllRegistryEntries("system/startup/drivers",NULL);
  step_icon();

  return o;

};

void drv_init ( void ) {

  drivers = drivers_init ( _malloc(sizeof(t_drivers)) );

};
