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

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


extern int SPRITES,MEM0,Power;
extern char *CONTROL;


void save_screen(int x1, int y1, int x2, int y2, char *file_name)
{_kernel_swi_regs regs;
  time_t *timer;
  struct tm *timeptr;
  size_t maxsize=25;
  char *timestr;
       extern union LIMIT XMax,XMin,YMax,YMin,Pval,Qval;
       extern unsigned char PALETTE[16];
       int i, line_length, end,
              start_line, end_line, close;
       unsigned char red,green,blue;
       FILE *fsave;
       union LIMIT num;
       _kernel_oserror *error;
       char command[256];
       timer=(time_t *)malloc(sizeof(time_t));
       timeptr=(struct tm *)malloc(sizeof(struct tm));
       timestr=(char *)malloc(22*sizeof(char));
       bbc_sound(1,-7,53,255,-1);
       bbc_soundon();
       while (file_name[6] < 0x3A)
       {strcpy(command,"<HyperMand$Dir>.Headers.");
              if ((fsave = fopen (strcat(command,file_name),"rb")) != NULL)
              {
                     file_name[7]++;
                     if (file_name[7] >= 0x3A)
                     {
                            file_name[7] = 0x30;
                            file_name[6]++;
                     }
                     fclose(fsave);
              }
              else
              {
                     if(fsave!=NULL)fclose(fsave);
                     strcpy(command,"<HyperMand$Dir>.Headers.");
                     fsave = fopen(strcat(command,file_name),"wb");
                     if(fsave==NULL){printf("Header Open Error - Consult Leaf Mindcraft");exit(0);};
                     fputc(0x0A,fsave);
                     num.i=x1;
                     for (i=0; i<4; i++)
               fputc(num.c[i],fsave);
                     num.i=y1;
                     for(i=0;i<4;i++)
               fputc(num.c[i],fsave);
                     num.i=x2;
                     for(i=0;i<4;i++)
               fputc(num.c[i],fsave);
                     num.i=y2;
                     for(i=0;i<4;i++)
               fputc(num.c[i],fsave);
                     num.i=1280;
                     for(i=0;i<4;i++)
               fputc(num.c[i],fsave);
                     num.i=1024;
                     for(i=0;i<4;i++)
               fputc(num.c[i],fsave);
                     for (i=0; i<16; i++)
                     {
                            red = PALETTE[i] & 0x03;
                            green = (PALETTE[i] & 0x0c)>>2;
                            blue = (PALETTE[i] & 0x30)>>4;
                            fputc(red,fsave);
                            fputc(green,fsave);
                            fputc(blue,fsave);
                     }
                     start_line = x1;
                     end_line = x2 + 1;
                     line_length = end_line - start_line;
                     end = start_line + line_length;
                     num.i=line_length;
                     for(i=0;i<4;i++)
                     fputc(num.c[i],fsave);
                     num.i=SPRITES;
                     for(i=0;i<4;i++)
                     fputc(num.c[i],fsave);
                     for (i=0; i<4; i++)
                            fputc(XMax.c[i],fsave);
                     for (i=0; i<4; i++)
                            fputc(XMin.c[i],fsave);
                     for (i=0; i<4; i++)
                            fputc(YMax.c[i],fsave);
                     for (i=0; i<4; i++)
                            fputc(YMin.c[i],fsave);
                     for (i=0; i<4; i++)
                            fputc(Pval.c[i],fsave);
                     for (i=0; i<4; i++)
                            fputc(Qval.c[i],fsave);
                            fputc(Power,fsave);
                     time(timer);
                     timeptr=localtime(timer);
                     strftime(timestr,maxsize,"%b %d %Y %H:%M:%S",timeptr);
                     for(i=0;i<21;i++)
                     fputc(timestr[i],fsave);
                     free(timestr);
/*                     for (k=y1; k<y2; k++)
                     {
                            add1 = 80*k;
                            number = 1;
                            j = 0;
                            add2 = (start_line);
                            old_ch = read_screen(add1 + add2++,0);
                            for (i=add2; i<end; i++)
                            {
                                   if (i == end - 1)
                                          ch = old_ch - 1;
                                   else
                                   {
                                          if ((add2) == end_line)
                                          {
                                                 j++;
                                                 add2 = (start_line);
                                          }
                                          ch = read_screen(add1 + add2,
                                                 j);
                                   }
                                   if ((ch == old_ch) && number < 63)
                                          number++;
                                   else
                                   {
                                          num_out = ((unsigned char)
                                                 number | 0xC0);
                                          if ((number != 1) ||
                                                 ((old_ch & 0xC0) ==
                                                 0xC0))
                                                 fputc(num_out,fsave);
                                          fputc(old_ch,fsave);
                                          old_ch = ch;
                                          number = 1;
                                   }
                                   add2++;
                            }
                     } */
                     MEM0=increase(MEM0);
                     regs.r[0]=10+256;
                     regs.r[1]=(int)CONTROL;
                     regs.r[2]=(int)"<HyperMand$Dir>.Sprites.!SysSprite";
                     error=_kernel_swi(OS_SpriteOp,&regs,&regs);
                   if(error){printf("Cannot Load !SysSprite - Consult Leaf Mindcraft");exit(0);}
                     else {regs.r[0]=16+256;
                              regs.r[1]=(int)CONTROL;
                              regs.r[2]=(int)file_name;
                              regs.r[3]=1;
                              regs.r[4]=x1;
                              regs.r[5]=y1;
                              regs.r[6]=x2;
                              regs.r[7]=y2;
                              error=_kernel_swi(OS_SpriteOp,&regs,&regs);
                              if(error){printf("Cannot Save Screen Image - Consult Leaf Mindcraft");exit(0);}
                              else {regs.r[0]=12+256;
                                       regs.r[1]=(int)CONTROL;
                                       regs.r[2]=(int)"<HyperMand$Dir>.Sprites.!SysSprite";
                              error=_kernel_swi(OS_SpriteOp,&regs,&regs);
                              if(error){printf("Cannot Save !SysSprite - Consult Leaf Mindcraft");exit(0);}
                                }
                             }
                     close=fclose(fsave);
                     if(close){printf("Header Close Error - Consult Leaf Mindcraft");exit(0);}
                     break;
              }
       }
 if(file_name[6]==0x3A){printf("\n\nMaximum Number of Sprites Reached");exit(0);};
}
