
/*Ŀ
                                                         
         restore_screen() = save screen to disk file     
                                                         
  */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <kernel.h>
#include "tools.h"
#include "bbc.h"
#include "os.h"



extern union LIMIT XMax, XMin, YMax, YMin, Pval, Qval;

extern int SPRITES,MEM0,FLAG,Power;

extern char *CONTROL;

int restore_screen(char file_name[])
{union LIMIT num;
  _kernel_swi_regs regs;
  _kernel_oserror *kerror;
  int close;
/*    #define graph_out(index,val)  {outp(0x3CE,index);\
                           outp(0x3CF,val);} */
    FILE *fsave;
    unsigned char ch,red,green,blue,color;
    int i,x1,y1,x2,y2;
    char command[256]="<HyperMand$Dir>.Headers.";
    if ((fsave = fopen(strcat(command,file_name),"rb")) == NULL)
    {
        printf("Can't find Header File %s.",file_name);
      return(0);
    }
    else
    {
        ch = fgetc(fsave);
        if (ch != 0x0A)
        {
            printf("%s is not a valid Header file.",
            file_name);
            fclose(fsave);
          return(0);
        }
    }

            for (i=0; i<4; i++)num.c[i]=fgetc(fsave);
    x1 = num.i;
            for (i=0; i<4; i++)num.c[i]=fgetc(fsave);
    y1 = num.i;
            for (i=0; i<4; i++)num.c[i]=fgetc(fsave);
    x2 = num.i;
            for (i=0; i<4; i++)num.c[i]=fgetc(fsave);
    y2 = num.i;
    for (i=12; i<16; i++)
        ch = fgetc(fsave);
        for (i=0; i<4; i++)num.c[i]=fgetc(fsave);
     SPRITES=num.i;
    for (i=0; i<16; i++)
    {
        red = fgetc(fsave);
        green = fgetc(fsave);
        blue = fgetc(fsave);
        color=red & green<<2 & blue<<4;
    }

    for (i=64; i<72; i++)
        ch = fgetc(fsave);
    for (i=0; i<4; i++)
        XMax.c[i] = fgetc(fsave);
    for (i=0; i<4; i++)
        XMin.c[i] = fgetc(fsave);
    for (i=0; i<4; i++)
        YMax.c[i] = fgetc(fsave);
    for (i=0; i<4; i++)
        YMin.c[i] = fgetc(fsave);
    for (i=0; i<4; i++)
        Pval.c[i] = fgetc(fsave);
    for (i=0; i<4; i++)
        Qval.c[i] = fgetc(fsave);
        Power=fgetc(fsave);
        if(Power==' ')Power='2';
    for (i=0; i<21; i++)
        ch = fgetc(fsave);
/*
    graph_out(8,0xFF);
    graph_out(3,0x10);
    for (k=y1; k<y2; k++)
    {
        i = k*80 + (x1/8);
        line_end = k* 80 + (x2/8)+1;
        j = 0;
        while (j < 4)
        {
            ch1 = fgetc(fsave);
            if ((ch1 & 0xC0) != 0xC0)
            {
                display(i, j, ch1);
                i++;
                if (i >= line_end)
                {
                    j++;
                    i = k*80 + (x1/8);
                }
            }
            else
            {
                ch1 &= 0x3F;
                pass = ch1;
                ch = fgetc(fsave);
                for (m=0; m<pass; m++)
                {
                    display(i, j, ch);
                    i++;
                    if (i >= line_end)
                    {
                        j++;
                        i = k*80 + (x1/8);
                    }
                }
            }
        }
    }
    graph_out(3,0);
    graph_out(8,0xFF);
    */
  if(!FLAG){
  if(x2<1279){printf("\n\nThis Fractal \'%s\' is to Power %c.",file_name,Power);
  printf("\n\nIt is indicated that this Sprite is not a complete plot.\n\nThe Sprite will be loaded onto the screen,\n\nAnd the plot will continue until it is complete, or a key is pressed.\n\nIn either case, the Sprite will be saved with the name unchanged.\n\nPress a Key to Continue.");bbc_cursor(0);bbc_get();
  cls(0);bbc_cursor(0);
                       }
  else {printf("\n\nThe Sprite \'%s\' exists as a full plot.",file_name);
           printf("\n\nIt is to Power %c.",Power);
            printf("\n\nYou are now required to choose the area you wish to zoom into,\n\nAn angle caret will appear in the lower left hand corner of the screen,\n\nUse the cursor keys to move the caret to the lower left hand corner of interest.\n\nPress Return (Enter), and another angle caret will appear.\n\nMove this caret to the upper right hand corner of the area of interest.\n\nPress Return (Enter) again and a Plot of the indicated area will be made.\n\nHolding down the 'Control' Key whilst pressing cursor keys speeds up the carets.\n\nPressing any key whilst Plotting will halt it.\n\nPress Key to Continue.");bbc_cursor(0);bbc_get();cls(0);bbc_cursor(0);};
            };
  MEM0=increase2(MEM0);
  regs.r[0]=10+256;
  regs.r[1]=(int)CONTROL;
  regs.r[2]=(int)"<HyperMand$Dir>.Sprites.!SysSprite";
  kerror=_kernel_swi(OS_SpriteOp,&regs,&regs);
  if(kerror){printf("Cannot Load !SysSprite - Consult Leaf Mindcraft");exit(0);}
  regs.r[0]=24+256;
  regs.r[1]=(int)CONTROL;
  regs.r[2]=(int)file_name;
  kerror=_kernel_swi(OS_SpriteOp,&regs,&regs);
  if(kerror){printf("Cannot Find Sprite %s - Consult Leaf Mindcraft",file_name);exit(0);};
    bbc_move(x1,y1);
    regs.r[0]=28+256;
    regs.r[1]=(int)CONTROL;
    regs.r[2]=(int)file_name;
    regs.r[5]=0;
    kerror=_kernel_swi(OS_SpriteOp,&regs,&regs);
    if(kerror){printf("Cannot Plot chosen Sprite - Consult Leaf Mindcraft");exit(0);};
    close=fclose(fsave);
    if(close){printf("Close Error for Header File %s - Consult Leaf Mindcraft",file_name);exit(0);};
    return(x2);
}
