/*		   ACK-3D ( Animation Construction Kit 3D )		      */
/* Goal routine	      */
/* Author: Lary Myers */

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <sys/stat.h>
#include "ack3d.h"
#include "ackext.h"

extern unsigned char colordat[];

/****************************************************************************
**									   **
****************************************************************************/
int ShowScreen(char *sName)
{
int    handle;
unsigned char *sBuffer,*Video;
unsigned char *pBuffer;

	pBuffer = malloc(768);
	if (pBuffer == NULL)
    		return(-1);

	if (!strcasecmp(GetExtent(sName),"LBM")) {
    		sBuffer = Readiff(sName);
    		memmove(sBuffer,&sBuffer[4],64000);
    		memmove(pBuffer,colordat,768);
    	} else {
    		handle = open(sName,O_RDWR);
    		if (handle < 1) {
			free(pBuffer);
			return(-2);
		}

    		sBuffer = malloc(64000);
    		if (sBuffer) {
			read(handle,sBuffer,4);
			read(handle,sBuffer,64000);
		}

    		close(handle);
	
    		if (*GoalPalFile) {
			handle = open(GoalPalFile,O_RDWR);
			if (handle > 0) {
			    	read(handle,pBuffer,768);
	    			close(handle);
	    		} else {
	    			free(pBuffer);
	    			pBuffer = NULL;
	    		}
		} else {
			free(pBuffer);
			pBuffer = NULL;
		}
    	}

	if (sBuffer == NULL) {
    		if (pBuffer)
			free(pBuffer);
    		return(-3);
    	}

	if (pBuffer) {
    		SetPalette(pBuffer);
    		free(pBuffer);
    	}

	Video = graph_mem;
	memmove(Video,sBuffer,64000);

	free(sBuffer);

	return(0);
}



/****************************************************************************
** Here is where everything finishes up. If a screen was specified in the  **
** master description file then it is displayed, otherwise a simple	   **
** YOU WON! is shown.							   **
****************************************************************************/
void DoGoalScreen(void)
{
int result;

	SetVGAmode();

	result = 1;

	if (*GoalFile)
    		result = ShowScreen(GoalFile);

	if (result) {
    		printf("\tY O U  W O N ! !");
    	}

	vga_getch();
}

