/*==========================================================================*
 * welcome.h - Include file for Welcome Disk                                *
 * Copyright (c) 1990 by David Joiner (Talin)                               *
 *==========================================================================*/

#ifdef __PRECOMPILED
#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfxmacros.h>
#include <graphics/gfxbase.h>
#include <functions20.h>
#include <xfunctions.h>
#endif

#include <exec/interrupts.h>
#include <exec/execbase.h>
#include <hardware/dmabits.h>
#include <hardware/intbits.h>
#include <hardware/blit.h>
#include <hardware/custom.h>
#include <graphics/gfx.h>
#include <graphics/display.h>
#include <graphics/text.h>
#include <graphics/sprite.h>
#include <graphics/view.h>
#include <libraries/dosextens.h>
/* #include <libraries/translator.h> */
#include <devices/narrator.h>
#include <devices/input.h>
#include <devices/inputevent.h>
#include <devices/audio.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

#include "xlib.h"

/* #define __NO_PRAGMAS */

int strlen(char *);

	/* double buffering control structure */

struct fpage {									/* structure for one flip-page	*/
	struct RasInfo	*ri_page;					/* rasinfo for this page		*/
	char			old_fig_x,					/* x position of character		*/
					old_hgt,					/* old y position of character	*/
					old_len;					/* old clipped height of char	*/
};

	/* Used by the input handler */

struct	in_work {
	short	laydown,pickup;
	short	timeout;
	char	newdisk,joydown;
	char	keybuf[128];
	char	xjoy,yjoy;
};

#define BUTTON_PRESSED	(1<<0)
#define BUTTON_WAS_UP	(1<<1)

#define JOY_FIRE		(1<<7)

#define TIME_TO_WAIT	300

	/* defines for handle_selection */

#define HELP_SELECT		10
#define ESCAPE_SELECT	11

	/* important character constants */

#define RETURN			'\r'
#define BACKSPACE		'\b'
#define NEWLINE			'\n'
#define BACKSLASH		'\\'
#define SLASH			'/'
#define ESCAPE			0x1b

	/* errors we may want to report to user, so he can remedy */
enum {
	DERR_NONE=0,
	DERR_NO_LIBRARY,						/* unrecoverable, I would think			*/
	DERR_CANT_LOCK,							/* from CLI, can't find "Discovery:"	*/
	DERR_NO_DEVICE,							/* internal error						*/
	DERR_NO_INTERRUPT,						/* internal error						*/
	DERR_NO_SPRITE,							/* internal error						*/
	DERR_NO_TRANSLATE,						/* Could not find Translator Library	*/
	DERR_NO_NARRATE,						/* Could not find Narrator Device		*/
	DERR_MUSIC_ON,							/* internal error						*/
	DERR_NO_MEMORY							/* MemAlert ???							*/
};

struct IFFPic {
	struct MinNode		node;					/* for tracking					*/
	UWORD				width,
						height;
	struct BitMap		bmap;
};


	/* this is a general facility for animating color palettes */

struct ColorEntry {
	struct ColorEntry	*next_entry;			/* pointer to next range		*/
	void				(*func)();				/* animation function			*/
	short				range_start,			/* starting color to animate	*/
						range_size;				/* number of colors in range	*/
	UWORD				*source_colors;			/* colors to use in animating	*/
	UWORD				source_length;			/* number of colors in source	*/
	UWORD				rate,
						count;
};

typedef struct ColorEntry CEntry;

#define fade_level		rate
#define fade_color		count

struct ColorAnimation {
	struct ColorAnimation *next;				/* next palette to animate		*/
	struct ColorEntry	*entry_list;			/* list of palette ranges to animate */
	UWORD				*result_palette;		/* pointer to palette			*/
	struct ColorMap		*result_colormap;		/* pointer to color map			*/
	struct ViewPort 	*result_viewport;		/* pointer to viewport to affect */
};

typedef struct {
	WORD			x,y;
} XY;

struct RTClock {
	UWORD			second_units,	pad00,
					second_tens, 	pad01,
					minute_units,	pad02,
					minute_tens,	pad03,
					hour_units,		pad04,
					hour_tens,		pad05,			/* also AM/PM					*/
					day_units,		pad06,
					day_tens,		pad07,
					month_units,	pad08,
					month_tens,		pad09,
					year_units,		pad0A,
					year_tens,		pad0B,
					weekday,		pad0C,
					creg_D,			pad0D,
					creg_E,			pad0E,
					creg_F,			pad0F;
};

struct AsyncFile
{	struct FileHandle		*Handle;		/* a CPTR */
	struct StandardPacket	Packet;
	struct MsgPort			Port;
	char					*Buffer[2];
	char					*LastSent;
	char					*CurPos;
	ULONG					Size;
	ULONG					Avail;
	BYTE					Which;
	BYTE					SentOnce;
	BYTE					Pending;
	BYTE					End;
};

struct WireFrame {
	LONG	size;
	BYTE	*buffer;
	LONG	left;
	BYTE	*at;
};

struct AnimFrame {
	struct AnimFrame	*next;
	struct AnimFrame	*prev;
	struct BitMap		bm;
};

struct AnimHandle {
	struct AnimFrame	*first;
	struct AnimFrame	*current;
	UWORD				colors[32];
	ULONG				bits;
	UWORD				num;
	UWORD				count;
	UWORD				width;
	UBYTE				interleave;
	UBYTE				mode;
	UBYTE				dir;
	UBYTE				pad;
};

#define ANIM_ONCE		0
#define ANIM_PONG		1
#define ANIM_CONT		2

struct DeltaFrame {
	struct DeltaFrame	*next;
	void				*data;
	long				size;
};

struct CompAnimHandle {
	struct DeltaFrame	*first;
	struct DeltaFrame	*current;
	struct BitMap		bm;
	UWORD				colors[32];
	ULONG				bits;
	UWORD				count;
	UWORD				width;
	UBYTE				interleave;
	UBYTE				pass;
};

struct DiskAnimHandle {
	struct BitMap		bm;
	UWORD				colors[32];
	struct AsyncFile	*file;
	ULONG				filelength;
	ULONG				bits;
	UWORD				width;
	UBYTE				interleave;
	UBYTE				count;
};

/*========================= prototypes =========================*/

AFILE OpenFile(char *name, int mode);
long ReadFile(AFILE fh, void *buffer, long size);
long SeekFile(AFILE fh, long size);

struct SampleBuf;
struct PicBuffer;
struct _BitMapHeader;

/* audio.c */

void play_sample(struct SampleBuf *sb);
void play_async(struct SampleBuf *sb);
void wait_async(struct SampleBuf *sb);
struct SampleBuf *load_sample(char *name);
void unload_sample(struct SampleBuf *sb);

/* copint.c */

int remove_interrupt(void);
int wait_cop(void);
int wait_ncop(int n);
int reset_frames(void);
long get_frames(void);

/* err.c */

int memerror(void);

/* gport.c */

short init_gameport(void);
void gport1_handler(struct in_work *mydata);
void gport0_handler(struct in_work *mydata, struct InputEvent *ev);
void flush_gport(void);
void send_read_request(void);
void wrap_gameport(void);

/* iffsubs.c */

BOOL NextDiskFrame(struct DiskAnimHandle *ahndl,struct BitMap *bm);
void FreeDiskAnim(struct DiskAnimHandle *ahndl);
BOOL LoadDiskAnim(struct DiskAnimHandle *ahndl,char *name);

struct BitMap *ResetAnim(struct AnimHandle *ahndl,UWORD mode);
struct BitMap *NextAnimFrame(struct AnimHandle *ahndl);
void FreeAnim(struct AnimHandle *ahndl);
BOOL LoadAnim(struct AnimHandle *ahndl,char *name);

short SavePic(char *filename, struct BitMap *bm, unsigned short *palette, short count);
int WriteLong(AFILE fh, long value);
int WritePad(AFILE fh);
int WriteBitMap(AFILE fh, struct BitMap *bm);
int WriteBitMapCmp(AFILE fh, struct BitMap *bm);
int CalcBitMapCmp(struct BitMap *bm);
int ReadHeader(void);
int ReadLength(void);
int unpackpic(char *filename, struct BitMap *bitmap, unsigned short *colors);
int loadpic(char *filename, struct BitMap *bitmap, unsigned short *colors, long *mode);
int fade_map(int level);
void fade_palette(struct ViewPort *vp, unsigned short *colors, int count, int level);
int low_fade(int level);
int high_fade(int level);
struct IFFPic *GetPicture(char *filename, UWORD *colors, UWORD maxcolors, struct MinList *track);
void FreePicture(struct IFFPic *pic);
fade_palettes(UWORD *p1, UWORD *p2, int speed);

/* large.c */

struct InputEvent *HandlerInterface(struct InputEvent *ev, struct in_work *mydata);
long myautoreq(struct Window *Window, struct IntuiText *Body, struct IntuiText *PText, struct IntuiText *NText, long PFlag, long NFlag, long W, long H);
int patch_autorequest(void);
int unpatch_autorequest(void);
void CopyColors(UWORD *palette,struct ColorEntry *ce);
void RandomColors(UWORD *palette,struct ColorEntry *ce);
void DimColors(UWORD *palette,struct ColorEntry *ce);
void CycleColors(UWORD *palette,struct ColorEntry *ce);
void ReverseCycle(UWORD *palette,struct ColorEntry *ce);
void FadeTo(UWORD *palette,struct ColorEntry *ce);
void FillColors(UWORD *palette,struct ColorEntry *ce);

/* screen.c */

int init_screen(void);
int _main(long alen, char *aptr);
void CenterText(struct RastPort *rp, short x, short y, char *str);
void close_all(int errnum);
int add_menu(void);
int remove_menu(void);
short build_menus(LOCK lock, short buffers);
int move_towards(int loc, int dest, int step);
void new_lesson(long length);
int rnd(void);
int blank(void);
int tile_no(void);
void actor_outline(short color);
int drawnum(int n);
int drawpage(void);
int free_ucop(void);
int add_view(void);
int new_text_depth(short depth, long mode, unsigned short *hues);
int reset_text_depth(void);
int add_title(void);
int remove_view(void);
int remove_title(void);
int delete_viewport(struct ViewPort *vp);
int delete_view(struct View *v);
int pageflip(int wait);
int scroll_title(int n);
int wrap_device(void);
int getkey(void);
int clearkey(void);
int user_skip(void);
short delayed_skip(unsigned short ticks);
int reset_skip(void);
int testkey(void);
int fadeup(int n);
int smooth_up(int n);
int fadedn(int n);
short highsq(unsigned short ticks);
unsigned long getfilesize(char *filename);
int loadview(struct View *v);
int remove_null(void);
int joyread(void);
