/*********************************************************************\
 *                         ScreenSet v1.0                            *
 *                                                                   *
 * Written by Syd L. Bolton  ©1991 Legendary Design Technologies Inc.*
 *                                                                   *
 *           Works in conjunction with ScreenMod v1.0                *
 *                                                                   *
 * This program's executable, source code, and documentation are all *
 * Copyright ©1991 Legendary Design Technologies Inc.                *
 *                 25 Frontenac Avenue                               *
 *                 Brantford, Ontario                                *
 *                 CANADA  N3R 3B7                                   *
 *                 (519)-754-4070  (519)-754-4059 FAX                *
 *                                                                   *
 *              This program is freely distributable.                *
 *                                                                   *
 *       Version: 001   Date: May 9, 1991  Time: 00:22:05            *
\*********************************************************************/

#include <stdio.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

long *IntuitionBase,*GfxBase;
char string[80],origtitle[80];
int origwidth,origheight;
int leftedge,topedge,width,height,detailpen,blockpen,numcolors;
long colors[32];
long modes;
UWORD cl[32];
struct Screen *Screen;

FILE *fp;

main(argc,argv)
int argc;
char *argv[];
{
int n,valid;

if ((argc != 2) || (argv[1][0]=='?')) {
	printf("ScreenSet v1.0 by Syd L. Bolton ©1991 Legendary Design Technologies Inc.\n");
	printf("Usage: %s filename\n",argv[0]);
	exit(1);
	}
	
fp=fopen(argv[1],"r");

if (fp==NULL) {
	puts("Couldn't open file.");
	exit(1);
	}

openlibraries();

fscanf(fp,"%s",&string);
if(strcmp(string,"SM1")) {
	puts("Not a valid ScreenSet/Mod file!");
	fclose(fp);
	exit(1);
	}
getc(fp);
n=0;
while ((origtitle[n]=getc(fp)) != '\n')
	       ++n;
origtitle[n]='\0';
if(!(strcmp(origtitle,"*NN"))) origtitle[0]='\0';  /* it was a blank */
fscanf(fp,"%4d%4d\n",&origwidth,&origheight);

valid=lookforscreen();
if (valid) {
	puts("SCREENSET:  Couldn't find screen!");
	fclose(fp);
	closelibraries();
	exit(1);
	}
fscanf(fp,"%4d%4d%4d%4d%2d%2d\n",&leftedge,&topedge,&width,&height,&detailpen,&blockpen);
n=0;
while ((string[n]=getc(fp)) != '\n')
	++n;
string[n]='\0';
if(!(strcmp(string,"*NN"))) string[0]='\0';  /* it was a blank */
strcpy(Screen->Title,string);
n=0;
while ((string[n]=getc(fp)) != '\n')
	++n;
string[n]='\0';
if(!(strcmp(string,"*NN"))) string[0]='\0';  /* it was a blank */
strcpy(Screen->DefaultTitle,string);
fscanf(fp,"%6d\n",&modes);
fscanf(fp,"%2d\n",&numcolors);
for (n=0; n<numcolors; n++) {
	fscanf(fp,"%4d\n",&colors[n]);
	cl[n]=colors[n];   /* gotta convert to UWORD for LOADRGB4 */
	}
fclose(fp);
Screen->LeftEdge=leftedge; Screen->TopEdge=topedge;
Screen->Width=width; Screen->Height=height;
Screen->DetailPen=detailpen; Screen->BlockPen=blockpen;
Screen->ViewPort.DxOffset=leftedge; Screen->ViewPort.DyOffset=topedge;
Screen->ViewPort.Modes=modes;
RemakeDisplay();
if (Screen->Flags & SHOWTITLE) ShowTitle(Screen,TRUE); /* show title if supposed to */
LoadRGB4(&Screen->ViewPort,&cl[0],numcolors);
closelibraries();
}

lookforscreen()
{
int rv,fallout=0;

Screen=IntuitionBase->FirstScreen;
rv=checkvalid();
if (rv==0) return(0);  /* wants to modify front screen */
do {
	Screen=Screen->NextScreen;
	rv=checkvalid();
	if (rv==0) return(0);
	if (Screen==NULL) fallout=1;
	} while (fallout==0);
return(1);
}

checkvalid()
{
if (strcmp(origtitle,Screen->DefaultTitle)) return(1);
if (origwidth != Screen->Width) return(2);
if (origheight != Screen->Height) return(3);
return(0);
}

openlibraries()
{
if(!(IntuitionBase=OpenLibrary("intuition.library",0L))) {
	puts("Couldn't open Intuition!");
	fclose(fp);
	exit(1);
	}
if(!(GfxBase=OpenLibrary("graphics.library",0L))) {
	puts("Couldn't open graphics!");
	fclose(fp);
	exit(1);
	}
}

closelibraries()
{
if (GfxBase) CloseLibrary(GfxBase);
if (IntuitionBase) CloseLibrary(IntuitionBase);
}	

