#include <stdio.h>
#include <fcntl.h>
#include <exec/memory.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>

#include "cdsconst.h"
#include "cdsprefs.h"

#define LOCAL_SIZE	PATHLEN + 1

static char   ext[LOCAL_SIZE] = "--";
static char   dir[LOCAL_SIZE] = "--";
static char   file[LOCAL_SIZE] = "--";

extern BOOL FileRequest();
 		
/* -------------------------------------------------------------- */

BOOL LoadFile(S,P,Bild,Lv,time,sec,x,y)

struct Screen   *S;
struct CDSPrefs	*P;
UBYTE Bild[BildBreite][BildHoehe];
int   *Lv;
int   *time;
int   *sec;
int   *x;
int   *y;

{ char	 buf[200];
  BOOL   Result;
  FILE   *f;
  int    i,j,w;
  
  if (strcmp((char*)dir,"--") == 0)
     sprintf(dir,"%s",P->game);
  if (strcmp((char*)file,"--") == 0)
     sprintf(file,"savedgame");
  if (strcmp((char*)ext,"--") == 0)
     sprintf(ext,"cds");

  Result = FileRequest(S,dir,file,ext);

  if (Result)
   {
    if (dir[strlen(dir)-1] == ':')
       sprintf(buf,"%s%s",dir,file);
    else
       sprintf(buf,"%s/%s",dir,file);
    if (!(f = fopen(buf,"r")))
       return(FALSE);
    w   = getw(f);
    if (w != MAGIC_WORD)
       {
        fclose(f);
        return(TRUE);
       }
    *Lv   = getw(f);
    *time = getw(f);
    *sec  = getw(f);
    *x    = getw(f);
    *y    = getw(f);
    for (i = 0; i < BildBreite; i++)
        for (j = 0; j < BildHoehe; j++)
            Bild[i][j] = getc(f);
    fclose(f);
   }
    
  return(Result);
}

