//---------------------------------------------------//
/*
 *Copyright Bryce Thorup, William Thorup, BitJag 2014
 *
 * Contact: contact@bitjag.com
 *
 *All content developed by BitJag (graphics & Code) is
 *for your personal use only.  Feel free to burn it to a CD, 
 *flash to a SkunkBoard, or use in an emulator.  We 
 *hope you also take the chance to learn from it if needed.
 * 
 *Do not redistribute, duplicate, charge for "handling" 
 *or cost to print, or charge for profit.
 * 
 *If there is someone you know who wants a copy,
 *physical or digital, please direct them to our website.
 *Any other questions about the use of the demo, please
 *shoot us an email.  We will be liberal, promise.
 *
 * http://bitjag.com/
 * 
 *This demo was developed with the Removers Library,
 *which is used under the LGPL licence 2.1, included
 *with the source.  The Removers library is free to 
 *download and use at this location.
 * 
 * http://removers.free.fr/softs/download.php?lang=en
 *
//---------------------------------------------------*/

#include <jagdefs.h>
#include <jagtypes.h> 
#include <stdlib.h>
#include <interrupt.h>
#include <display.h>
#include <sprite.h>
#include <screen.h>
#include <fb2d.h>
#include <sound.h>
#include <joypad.h>
#include <console.h>
#include <string.h>

#include "tran-inout.h"
#include "vert_bounce.h"
#include "bird_motion.h"

#include "bj_text.h"
#include "bj_scroll.h"

//transition grid

extern phrase grid;
extern phrase tran_wipe;

//Intro graphics

extern phrase arcade_cabinent;
extern phrase bitjagGrid;
extern phrase welcomes;
extern phrase youTo;
extern phrase portland;
extern phrase retro_gaming;
extern phrase expo;
extern phrase date_2014;
extern phrase catHead1;
extern animation_chunk cat_animation;
extern phrase arcade_01;
extern animation_chunk arc_animation;
extern phrase brick_8;
extern phrase brick_16;
extern phrase brick_24;

//SCROLLER

extern phrase steel_font;
extern phrase console_scratches;
extern phrase scratches_01;
extern phrase scratches_02;
extern phrase bot_grid_01;
extern phrase bot_grid_02;
extern phrase bot_grid_03;
extern phrase grid_frame;
extern phrase static_01;
extern phrase static_02;

//Art Time

extern phrase loaded;
extern phrase ball;
extern phrase music_credz;
extern phrase scroller_credz;
extern phrase code_credz;
extern phrase graphics_credz;
extern phrase side_grid;


//End Card

extern phrase mcm1;
extern animation_chunk mcfur_animation;
extern phrase qr_code;
extern phrase follow_cat;
extern phrase follow_cat_00;
extern animation_chunk cat_text_animation;

extern char music;  //Reference the our music.

//This function is used ot play the module at the beginning the program, 
//and after it has been stopped by the user.
void vbl_handler() {
  
  play_module();
  
}


//This function is used to track where we are in the demo
int clock(  int cycles_per_second, int *cpuCycles, int *time){
  
  if(*cpuCycles <= cycles_per_second){

  *cpuCycles = (*cpuCycles) + 1;
  
  return *time;
   
 }
 
 else if (*cpuCycles >= cycles_per_second){

  *cpuCycles = 0;

  return (*time) + 1;
   
 }
  
}


//-----------------Font Offsets and Widths------------------//

int FONT_OFFSETS[91]={0,535,0,0,587,0,0,582,0,0,0,0,812,608,808,0,620,628,648,667,685,706,727,748,766,787,0,0,0,0,0,540,560,8,29,50,70,90,109,128,149,169,174,194,214,233,258,279,300,320,341,362,383,402,423,444,469,491,514,0,0,0,0,0,582,8,29,50,70,90,109,128,149,169,174,194,214,233,258,279,300,320,341,362,383,402,423,444,469,491,514};

int FONT_WIDTHS[91]={8,5,0,0,21,0,0,5,0,0,0,0,4,12,4,0,21,8,20,19,18,21,21,18,21,21,0,0,0,0,0,20,22,21,21,20,20,19,19,20,20,5,20,20,19,25,21,21,20,21,21,21,19,21,21,25,22,23,21,0,0,0,0,0,5,21,21,20,20,19,19,20,20,5,20,20,19,25,21,21,20,21,21,21,19,21,21,25,22,23,21};
	
	
//-----------------Intro Card Sprites------------------//

sprite *arcade; //arcade cabinent.

sprite *bitjag_grid; //bitjag grid logo.

sprite *welcomes_title;

sprite *youTo_title;

sprite *portlandGFX; //Portland Retro Gaming Expo Title.por

sprite *retrogamingGFX; 

sprite *expoGFX; 

sprite *date2014;

sprite *cat_head;

sprite *arc_screen;//animated sprite for the arcade screen

sprite *brick8;//backround flying brick

sprite *brick16; // Midground brick

sprite *brick24; // Foreground brick

sprite *brick8_2;//backround flying brick

sprite *brick16_2; // Midground brick

sprite *brick24_2; // Foreground brick


//-----------------scroller sprites------------------//

sprite *consoleScratches;

sprite *scratches01;

sprite *scratches02;

sprite *botGrid01;

sprite *botGrid02;

sprite *botGrid03;

sprite *botGrid04;

sprite *botGrid05;

sprite *botGrid06;

sprite *gridFrame01;

sprite *gridFrame02;

sprite *static01;

sprite *static02;


//-----------------Art Time sprites------------------//

sprite *loadedBG;

sprite *ballGFX;

sprite *musicCredz;

sprite *scrollerCredz;

sprite *codeCredz;

sprite *graphicsCredz;

sprite *sideGrid01;

sprite *sideGrid02;

sprite *sideGrid03;

sprite *sideGrid04;

sprite *sideGrid05;

sprite *sideGrid06;

sprite *sideGrid07;

sprite *sideGrid08;


//-----------------End Card sprites------------------//

sprite *mcfur_middle_1;

sprite *qrCode;

sprite *followCat;

sprite *followCat00;

sprite *tranWipe;


//---------------------MAIN-----------------//

int main () {
  
  TOMREGS->vmode = RGB16|CSYNC|BGEN|PWIDTH4|VIDEN;  // Setup the type of diplay we wish to work with.

  init_interrupts(); // Initialize the basic interupts.
  init_display_driver(); // Initalize the display driver.
  skunk_init();

  int replay_freq = 14800;
  replay_freq = 20000;
  replay_freq = init_sound_driver(replay_freq);

   
  display *d = new_display(0); // Create a pointer for the display so we can reference it later.
	d-> x = 19; // set the x position of the display (in relation to the top-left corner).
	d-> y = 7; // set the y position of the display (in relation to the top-left corner).

    
//------------------INITIALIZE FONT STRUCT
Font *font = newFont(24, FONT_WIDTHS, FONT_OFFSETS, 1024, &steel_font);

	font->kerning = 2;
	font->line_spacing = 3;

char text[354] = "Thanks to Eight Bit Fix for letting us occupy their booth at PRGE this year, and the Removers for their awesome libraries.  Greetz to Seb, Matthias Domin, Reboot, Michael Hill, shamus, pouet.net, saturn, JagChris, TXG-MNX, orion-, willyvmm, zerosquare, songbird, Tursi, Jagware, CyranoJ, AtariAge - and everyone else on AA.  Do the Math and keep playing!";
	  
Scroll* scroll = newScroll(font, text, 354);

	scroll->loop_count = 1;

//------------------INITIALIZE MOTION STRUCTS FOR BOUNCING OBJECTS

motion *ballMotion = motion_init();


//------------------SETUP SCREEN BUFFERS FOR TRANSTION

//Our buffered sprite for In/Out Transitions
screen *sprite_scr = new_screen();
set_simple_screen(DEPTH16,20,20,sprite_scr,&grid);

//The screen that we will attach our buffered sprite to
screen *tran_scr = new_screen();
alloc_simple_screen(DEPTH16,340,260,tran_scr);

//The display sprite we use to attach the screen buffers to so they actually show up on the display.
sprite *fb_spr = sprite_of_screen(-10,0,tran_scr);
attach_sprite_to_display_at_layer(fb_spr,d,15);

int spaceCounter = 0; //The number we compare with the total amount of space we want to fill on the screen.
int tranDelay = 0; //Global delay to compare with the speed of the delay
int tranSpeed = 1; //How fast do we want the transition to be.  higher = slower
int tranInOut = 0;  //0 = transition in , 1 = transition out
    
  
//---------CLOCK VARIABLES---------------//

int time = 0; //Used to store how many seconds have passed.  Filled by the clock function.

int cyclesPerSecond = 57; //DONT CHANGE!!!  this is set to match the timeing of the song and the rest of the program relies on it.

int cpucycles = 0; //used to track each cycle of the cpu, within the clock function.

//--------------------------------------//


//---------SONG VARIABLES---------------//

int tempo = 0; //Do we want to control the tempo? 0 = no

init_module(&music,tempo); //Initialize MOD file

int songPlaying = 1; 

//--------------------------------------//


//---------PROGRAM CONTROL---------------//

int demoPos = 9;
//-----Position descriptions-----//
//Pos 0 = Intro
//Pos 1 = Scroller
//Pos 2 = Art Time
//Pos 3 = End Card
//Pos 9 = Reset

//--------------------------------------//

//---------Intro Card Variables---------------//

int main_pan_pos = 1280;// The is the parent variable that controls the pan of all elements in the into card.

//-----FLAGS

int arcadeFlag = 0;

int arcadeScreenFlag = 0;

int catHeadFlag = 0;

int bitJagLogoFlag = 0;

int welcomesFlag = 0;

int youToFlag = 0;

int portlandFlag = 0;

int retroFlag = 0;

int expoFlag = 0;

int dateFlag = 0;

int gridFrame01Flag = 0;

int gridFrame02Flag = 0;

//--------------------------------------//


//----------End Card variables---------//

int flap = 0;  // 0 = no controller input to flap up, 1 = controller input to flap up
	    
int mcfur_alt_limit = 90; // Mcfur cant go any lower than this variable on the title screen.

int mcfur_alt_position = 110; // How much to add to the randomizer to randomly change the altitude limit.

bird_fly *bird_1 = bird_fly_init(); // Create structure for bird motion

int controller = 0; //used to controll the movement of bouncing sprites



clear_screen(tran_scr);// REQUIRED - clean up the screen before we start

show_display(d);  //Turn on the display, so we can actually see it on the screen.

//-------------------Main While Loop-------------------//	  
while (1) {	 

switch( demoPos ){
  
  //--------------------------------------------------------------
  case 0: //Welcome to PRGE graphics
    
  //-----Pull in sprites
		
  //bitjag logo stuff
  
  cat_head = new_sprite( 72 , 88 , 0 , 20 , DEPTH16 , &catHead1);
	  
	  cat_head->animation = &cat_animation;
	  cat_head->animated = 1;
	  cat_head->animation_data.counter = 1;
	  cat_head->animation_data.index = 1;
  
  bitjag_grid = new_sprite( 208 , 48 , 0 , 120 , DEPTH16 , &bitjagGrid);
  
  //arcade cabinent sprites
  arcade = new_sprite(144 , 218 , 0 , 15 , DEPTH16 , &arcade_cabinent);
  
  arc_screen = new_sprite( 88 , 72 , 0 , 70 , DEPTH16 , &arcade_01);
	  
	  arc_screen->animation = &arc_animation;
	  arc_screen->animated = 1;
	  arc_screen->animation_data.counter = 1;
	  arc_screen->animation_data.index = 1;
  
  
  //title sprites
  welcomes_title = new_sprite(156 , 16 , 0 , 49 , DEPTH16 , &welcomes);
  
  youTo_title = new_sprite(152 , 24 , 0 , 120, DEPTH16 , &youTo);
  
  portlandGFX = new_sprite(144 , 20 , 0 , 68 , DEPTH16 , &portland);
  
  retrogamingGFX = new_sprite(148 , 12 , 0 , portlandGFX->y+20+8 , DEPTH16 , &retro_gaming);
  
  expoGFX = new_sprite(88 , 24 , 0 , retrogamingGFX->y+12+8 , DEPTH16 , &expo);
  
  date2014 = new_sprite(44 , 12 , 0 , expoGFX->y+24+8 , DEPTH16 , &date_2014);
  
  gridFrame01 = new_sprite(320 , 8 , 0 , 52 , DEPTH16 , &grid_frame);
  
  gridFrame02 = new_sprite(320 , 8 , 0 , date2014->y+12+9 , DEPTH16 , &grid_frame);
  
  //Flying bricks  (WATCH YOUR HEAD!)
  
  brick8 = new_sprite( 8 , 8 , 0 , 0 , DEPTH16 , &brick_8);
  
  brick16 = new_sprite( 16 , 16 , 0 , 0 , DEPTH16 , &brick_16);
  
  brick24 = new_sprite( 24 , 24 , 0 , 0 , DEPTH16 , &brick_24);
  
  //Flags // 0 = off , 1 = on

  int brick8Flag = 0;
  int brick16Flag = 0;
  int brick24Flag = 0;
  int brick8Flag2 = 0;
  int brick16Flag2 = 0;
  int brick24Flag2 = 0;
  int brickRandom = 0;
  
  
  //Transition Variables, lets make sure that they are correct
	  
  tranInOut = 3;
  
  spaceCounter = 0;
  
  tranDelay = 0;
	      
  //Set our transition state
  tranInOut = 4;
		
    
    while (1) {	    
      
	vsync();

	//Run our clock

	time = clock( cyclesPerSecond, &cpucycles, &time); //pass our clock variables into our clock function to update how many seconds have passed.
	
	//Run the randomizer for the brick
	
	brickRandom = rand()%24 +1;		
	
	//Pan our background
	
	if(main_pan_pos > 0){
	  
	  main_pan_pos -= 1; //move us
	  
	}
	
	//Movement Control for each element
	
	bitjag_grid->x = (main_pan_pos - 890)*2;
	
	cat_head->x = (main_pan_pos - 800);
	
	arcade->x = (main_pan_pos+20)/2;
	
	arc_screen->x = arcade->x+ 23;
	
	welcomes_title->x = cat_head->x + 150;
	
	youTo_title->x = bitjag_grid->x + 850;
	
	portlandGFX->x = arcade->x + 135;
	
	retrogamingGFX->x = portlandGFX->x + ((144/2) - (148/2)) + 1;
	
	expoGFX->x = retrogamingGFX->x + ((148/2) - (88/2));
	
	date2014->x = expoGFX->x + ((88/2) - (44/2));
	
	gridFrame01->x = portlandGFX->x - 20;
	
	gridFrame02->x = portlandGFX->x -  20;
		
		
	//------------------Attach Dettach sprite if they are viewable or not

	//BitJag Logo
	    //activate
	if(bitjag_grid->x < 325 && bitJagLogoFlag == 0){
	
	attach_sprite_to_display_at_layer(bitjag_grid , d , 7);
	
	bitJagLogoFlag = 1;
			  
	}
	    //Deactivate
	if(bitjag_grid->x < -200 && bitJagLogoFlag == 1){

	detach_sprite_from_display(bitjag_grid);
	
	bitJagLogoFlag = 0;
			  
	}
	
	//Cat Head
	    //activate
	if(cat_head->x < 325 && catHeadFlag == 0){
		
	attach_sprite_to_display_at_layer(cat_head , d , 5);
	
	catHeadFlag = 1;
			  
	}
	    //Deactivate		
	if(cat_head->x < -80 && catHeadFlag == 1){

	detach_sprite_from_display(cat_head);
	
	catHeadFlag = 0;
			  
	}
	
	//Welcomes
	    //activate
	if(welcomes_title->x < 325 && welcomesFlag == 0){
	  
	attach_sprite_to_display_at_layer(welcomes_title , d , 7);
	
	welcomesFlag = 1;
			  
	}
	    //Deactivate		
	if(welcomes_title->x < -160 && welcomesFlag == 1){

	detach_sprite_from_display(welcomes_title);
	
	welcomesFlag = 0;
			  
	}
	
	//You To...
	    //activate
	if(youTo_title->x < 325 && youToFlag == 0){
	  
	attach_sprite_to_display_at_layer(youTo_title , d , 7);
	
	youToFlag = 1;
			  
	}
	    //Deactivate		
	if(youTo_title->x < -200 && youToFlag == 1){

	detach_sprite_from_display(youTo_title);
	
	youToFlag = 0;
			  
	}
	
	//Arcade
	    //activate
	if(arcade->x < 325 && arcadeFlag == 0){
	  
	attach_sprite_to_display_at_layer( arcade , d , 5 );
	
	arcadeFlag = 1;
			  
	}
	
	//Arcade Screen
	    //activate
	if(arc_screen->x < 325 && arcadeScreenFlag == 0){
		
	attach_sprite_to_display_at_layer( arc_screen , d , 4);
	
	arcadeScreenFlag = 1;
			  
	}
	
	//Portland Title
	    //activate
	if(portlandGFX->x < 325 && portlandFlag == 0){
	  
	attach_sprite_to_display_at_layer( portlandGFX , d , 5 );
	
	portlandFlag = 1;
			  
	}
	
	//retro gaming Title
	    //activate
	if(retrogamingGFX->x < 325 && retroFlag == 0){
	  
	attach_sprite_to_display_at_layer( retrogamingGFX , d , 5 );
	
	retroFlag = 1;
			  
	}
	
	//expo Title
	    //activate
	if(expoGFX->x < 325 && expoFlag == 0){
	  
	attach_sprite_to_display_at_layer( expoGFX , d , 5 );
	
	expoFlag = 1;
			  
	}
	
	//2014 Date
	    //activate
	if(date2014->x < 325 && dateFlag == 0){
	  
	attach_sprite_to_display_at_layer( date2014 , d , 5 );
	
	dateFlag = 1;
			  
	}
	
	//gridFrame01
	    //activate
	if(gridFrame01->x < 325 && gridFrame01Flag == 0){
	  
	attach_sprite_to_display_at_layer( gridFrame01 , d , 5 );
	
	gridFrame01Flag = 1;
			  
	}
	
	//gridFrame02
	    //activate
	if(gridFrame02->x < 325 && gridFrame02Flag == 0){
	  
	attach_sprite_to_display_at_layer( gridFrame02 , d , 5 );
	
	gridFrame02Flag = 1;
			  
	}
	
	
	//Flying Bricks - All of this needs to be dropped into a function eventually...
	
	
	//brick8_01
	if(brickRandom == 2 && brick8Flag == 0 ){
	
	brick8->y = rand()%240 + 1;
	
	brick8->x = 325;
	
	attach_sprite_to_display_at_layer(brick8 , d , 2);
	
	brick8Flag = 1;
	  
	}
	
	if( brick8Flag == 1){
	  
	brick8->x -= 2; 
	  
	}
	
	if(brick8->x <= -50 && brick8Flag == 1){
	
	brick8Flag = 0; 
	
	detach_sprite_from_display(brick8);
	  
	}


	//brick16
	if(brickRandom == 12 && brick16Flag == 0 ){
	
	brick16->y = rand()%240 + 1;
	
	brick16->x = 325;
	
	attach_sprite_to_display_at_layer(brick16 , d , 4);
	
	brick16Flag = 1;
	  
	}
	
	if( brick16Flag == 1){
	  
	brick16->x -= 3; 
	  
	}
	
	if(brick16->x <= -50 && brick16Flag == 1){
	
	brick16Flag = 0; 
	
	detach_sprite_from_display(brick16);
	  
	}


	//brick24
	if(brickRandom == 24 && brick24Flag == 0 ){
	
	brick24->y = rand()%240 + 1;
	
	brick24->x = 325;
	
	attach_sprite_to_display_at_layer(brick24 , d , 10);
	
	brick24Flag = 1;
	  
	}
	
	if( brick24Flag == 1){
	  
	brick24->x -= 4; 
	  
	}
	
	if(brick24->x <= -50 && brick24Flag == 1){
	
	brick24Flag = 0; 
	
	detach_sprite_from_display(brick24);
	  
	}
			
			
		
    //Run our transition out
    if(time >= 27/*set at least 3 seconds before you break out of the CASE*/){

	tranInOut = 0;
	
    }
      
      
      if(tranInOut == 0){
    
	tranDelay ++;
    
	      if(tranDelay >= tranSpeed){
    
	      spaceCounter += 1;
	  
	      gridInOut( 20 , 20, spaceCounter , 193, 20 , 20 , sprite_scr , tran_scr, tranInOut );
	      
	      tranDelay = 0;
			
	      }
		  
	      if(tran_scr->y == 240 && tran_scr->x == 0){
	      
	      tranDelay = 0;
	      
	      spaceCounter = 0;
	      
	      tranInOut = 4;
	      
	      time = 29;
		
	      }

    }
		
		
    if(time == 29 && tranInOut == 4){
      
    break; // break out of case while loop 
      
    }
	
    }//end of case 0 while loop

    //-----Pull out sprites
    
    detach_sprite_from_display(bitjag_grid);
    
    detach_sprite_from_display(cat_head);
    
    detach_sprite_from_display(welcomes_title);
    
    detach_sprite_from_display(youTo_title);
    
    detach_sprite_from_display(arcade);
	    
	    arcadeFlag = 0;
    
    detach_sprite_from_display(arc_screen);
	    
	    arcadeScreenFlag = 0;
    
    detach_sprite_from_display(portlandGFX);
	    
	    portlandFlag = 0;
    
    detach_sprite_from_display(retrogamingGFX);
	    
	    retroFlag = 0;
    
    detach_sprite_from_display(expoGFX);
	    
	    expoFlag = 0;
    
    detach_sprite_from_display(date2014);
	    
	    dateFlag = 0;
	    
    detach_sprite_from_display(brick8);
    
    detach_sprite_from_display(brick16);
    
    detach_sprite_from_display(brick24);
    
    detach_sprite_from_display(gridFrame01);
    
	    gridFrame01Flag = 0;
    
    detach_sprite_from_display(gridFrame02);
    
	    gridFrame02Flag = 0;
    
    demoPos = 1;//Switch to desired switch/case
    
    //-----Reset variables for the next loop
    
    main_pan_pos = 1280;// The is the parent variable that controls the pan of all elements in the into card.
    
    
    //-------------------------
  
break; // break out of case
    
  //--------------------------------------------------------------
  
  //--------------------------------------------------------------
  case 1: //The Main Scroller
    
  //-----Pull in sprites

  //Jaguar System sprites
  consoleScratches = new_sprite( 92 , 92 , (320/2) - (92/2) , -95 , DEPTH16 , &console_scratches);
  
  scratches01 = new_sprite( 64 , 58 , (320/2) - (64/2) , 20 , DEPTH16 , &scratches_01);
  
  scratches02 = new_sprite( 64 , 58 , (320/2) - (64/2) , 78 , DEPTH16 , &scratches_02);
  
  //Scroller frame grid pieces
  
  gridFrame01 = new_sprite(320 , 8 , 0 , 35 , DEPTH16 , &grid_frame);
  
  attach_sprite_to_display_at_layer( gridFrame01 , d , 2);
  
  gridFrame02 = new_sprite(320 , 8 , 0 , 79 , DEPTH16 , &grid_frame);
  
  attach_sprite_to_display_at_layer( gridFrame02 , d , 2);
  
  
  //Bottom of the screen grid pieces
  
  botGrid01 = new_sprite( 32 , 64 , 0 , 176 , DEPTH16 , &bot_grid_01);
    
  attach_sprite_to_display_at_layer( botGrid01 , d , 2 );
  
  botGrid02 = new_sprite( 32 , 32 , 32 , 208 , DEPTH16 , &bot_grid_02);
    
  attach_sprite_to_display_at_layer( botGrid02 , d , 2 );
  
  botGrid05 = new_sprite( 32 , 32 , 288 , 208 , DEPTH16 , &bot_grid_02);
    
  attach_sprite_to_display_at_layer( botGrid05 , d , 2 );
  
  botGrid05->reflect = 1;
  
  botGrid06 = new_sprite( 32 , 64 , 320 , 184 , DEPTH16 , &bot_grid_01);
    
  attach_sprite_to_display_at_layer( botGrid06 , d , 2 );
  
  botGrid06->reflect = 1;
  
  //static lines
  
  static01 = new_sprite(320 , 4 , 0 , 400 , DEPTH16 , &static_01);
  
  attach_sprite_to_display_at_layer(static01 , d , 0 );
  
  static02 = new_sprite(320 , 4 , 0 , 60 , DEPTH16 , &static_02);
  
  attach_sprite_to_display_at_layer(static02 , d , 0 );
  
  //-----Font stuff
		
  
  attach_sprite_to_display_at_layer(scroll->move_box, d, 5);
  
  initScroll(scroll);
  
	scroll->loop_count = 1;
	
	scroll->loop_done = 0;
	
	scroll->move_box->y = 50;
	      
  
  //Flying bricks  (WATCH YOUR HEAD!)
  
  brick8 = new_sprite( 8 , 8 , 00 , -260 , DEPTH16 , &brick_8);
  
  brick16 = new_sprite( 16 , 16 , 0 , -260 , DEPTH16 , &brick_16);
  
  brick24 = new_sprite( 24 , 24 , 0 , -260 , DEPTH16 , &brick_24);
  
  brick8_2 = new_sprite( 8 , 8 , 00 , -260 , DEPTH16 , &brick_8);
  
  brick16_2 = new_sprite( 16 , 16 , 0 , -260 , DEPTH16 , &brick_16);
  
	      
  //Brick Flags // 0 = off , 1 = on

  brick8Flag = 0;
  brick16Flag = 0;
  brick24Flag = 0;
  brick8Flag2 = 0;
  brick16Flag2 = 0;
  brick24Flag2 = 0;
  brickRandom = 0;
  
  tranInOut = 1; //Make sure we are set for our transition back in.
    
    while (1) {	    
	
    vsync();
	
    //Run our clock

    time = clock( cyclesPerSecond, &cpucycles, &time); //pass our clock variables into our clock function to update how many seconds have passed.
    
    //Run the scroller
    updateScroll(scroll);
		
    //run our transition in
    if(tranInOut == 1){
    
    tranDelay += 1;

	if(tranDelay >= tranSpeed){

	spaceCounter ++;
    
	gridInOut( 20 , 20, spaceCounter , 193, 20 , 20 , sprite_scr , tran_scr, tranInOut );
	
	tranDelay = 0;
		    
	}
	
	    
	if(tran_scr->y == 0 && tran_scr->x == 0){
      
	spaceCounter = 0;
	
	tranDelay = 0;

	spaceCounter = 0;
	
	tranInOut = 4;
	
	clear_screen(tran_scr); //Clean up our buffer so the next transition looks clean.
	  
	}
      
    }
    
    //Scratch the screen
    
    if(time > 30/*tranInOut == 4*/ && consoleScratches->y != 130){
      
    consoleScratches->y += 1; 
	
	if(consoleScratches->y == -92){
	  
	attach_sprite_to_display_at_layer( consoleScratches , d , 4 ); 
	  
	}
	
	if(consoleScratches->y == 20){
		    
	attach_sprite_to_display_at_layer( scratches01 , d , 3 ); 
	  
	}
	
	if(consoleScratches->y == 78){
	  
	attach_sprite_to_display_at_layer( scratches02 , d , 3); 
	  
	}
      
    }
    
    //Run our static lines randomizers
    
    static01->invisible = -100 + rand()%200;
    
    static01->reflect = rand()%2;
    
    static01->x = rand()%160 + 160;
    
    static01->y -= 10%9;
    
    if(static01->y < -200){
      
    static01->y = 280; 
      
    }
    
    static02->invisible = -100 + rand()%200;
    
    static02->reflect = rand()%2;
    
    static02->x = rand()%160 + 160;
    
    static02->y += 2;
    
    if(static02->y > 230){
      
    static02->y = -100; 
      
    }
    
    
    //Run the randomizer for the brick
		
    brickRandom = rand()%24 +1;
		
		
		
//Flying Bricks
		
		
    //brick8_01
    if(brickRandom == 2 && brick8Flag == 0 ){
    
    brick8->x = rand()%320 + 1;
    
    brick8->y = 260;
    
    attach_sprite_to_display_at_layer(brick8 , d , 1);
    
    brick8Flag = 1;
      
    }
    
    if( brick8Flag == 1){
      
    brick8->y -= 2; 
      
    }
    
    if(brick8->y <= -10 && brick8Flag == 1){
    
    brick8Flag = 0; 
    
    detach_sprite_from_display(brick8);
      
    }


    //brick16
    if(brickRandom == 12 && brick16Flag == 0 ){
    
    brick16->x = rand()%320 + 1;
    
    brick16->y = 260;
    
    attach_sprite_to_display_at_layer(brick16 , d , 2);
    
    brick16Flag = 1;
      
    }
    
    if( brick16Flag == 1){
      
    brick16->y -= 3; 
      
    }
    
    if(brick16->y <= -17 && brick16Flag == 1){
    
    brick16Flag = 0; 
    
    detach_sprite_from_display(brick16);
      
    }


    //brick24
    if(brickRandom == 24 && brick24Flag == 0 ){
    
    brick24->x = rand()%320 + 1;
    
    brick24->y = 260;
    
    attach_sprite_to_display_at_layer(brick24 , d , 6);
    
    brick24Flag = 1;
    
    }
    
    if( brick24Flag == 1){
      
    brick24->y -= 4; 
      
    }
    
    if(brick24->y <= -24 && brick24Flag == 1){
    
    brick24Flag = 0; 
    
    detach_sprite_from_display(brick24);
      
    }

    //brick8_2
    if(brickRandom == 4 && brick8Flag2 == 0 ){
    
    brick8_2->x = rand()%320 + 1;
    
    brick8_2->y = 260;
    
    attach_sprite_to_display_at_layer(brick8_2 , d , 1);
    
    brick8Flag2 = 1;
      
    }
    
    if( brick8Flag2 == 1){
      
    brick8_2->y -= 2; 
      
    }
    
    if(brick8_2->y <= -10 && brick8Flag2 == 1){
    
    brick8Flag2 = 0; 
    
    detach_sprite_from_display(brick8_2);
      
    }
		
    
    
		
    //Run our transition out
    if(time >= 151/*set at least 3 seconds before you break out of the CASE*/){

    tranInOut = 0;
	
    }
      
      
    if(tranInOut == 0){
    
    tranDelay ++;

	if(tranDelay >= tranSpeed){

	spaceCounter += 1;
    
	gridInOut( 20 , 20, spaceCounter , 193, 20 , 20 , sprite_scr , tran_scr, tranInOut );
	
	tranDelay = 0;
		    
	}
	    
	if(tran_scr->y == 240 && tran_scr->x == 0){
	
	tranDelay = 0;
	
	spaceCounter = 0;
	
	tranInOut = 4;
	
	time = 155;
	  
	}

    }
    
    if(time == 155 && tranInOut == 4){
      
    break; // break out of case while loop 
      
    }
	
    }//end of case 0 while loop
    
    //-----Pull out sprites
    
    detach_sprite_from_display(consoleScratches);
    
    detach_sprite_from_display(scratches01);
    
    detach_sprite_from_display(scratches02);
    
    detach_sprite_from_display(gridFrame01);
    
    detach_sprite_from_display(gridFrame02);
    
    detach_sprite_from_display(botGrid01);
    
    detach_sprite_from_display(botGrid02);
    
    detach_sprite_from_display(botGrid03);
    
    detach_sprite_from_display(botGrid04);
    
    detach_sprite_from_display(botGrid05);
    
    detach_sprite_from_display(botGrid06);
    
    detach_sprite_from_display(scroll->move_box);
    
    detach_sprite_from_display(brick8);
    
    detach_sprite_from_display(brick16);
    
    detach_sprite_from_display(brick24);
    
    detach_sprite_from_display(brick8_2);
    
    detach_sprite_from_display(static01);
    
    detach_sprite_from_display(static02);
     
    demoPos = 2; //Got to desired switch/case
    
break; // break out of case
    
    
    
  //--------------------------------------------------------------
  
  //--------------------------------------------------------------
  case 2: //Now for some Shameless Advertising and some Awesome Art.
    
  //-----Pull in sprites/
    
  //loaded background sprite
  loadedBG = new_sprite( 180 , 240 , 140 , 0 , DEPTH16 , &loaded);
    
  attach_sprite_to_display_at_layer( loadedBG , d , 2 );

  ballGFX = new_sprite( 32 , 32 , 75, -110 , DEPTH16 , &ball);

  musicCredz = new_sprite( 140 , 104 , ballGFX->x-50 , ballGFX->y , DEPTH16 , &music_credz);
		    
  attach_sprite_to_display_at_layer(musicCredz , d , 1);		

  scrollerCredz = new_sprite( 140 , 104 , ballGFX->x-50 , ballGFX->y , DEPTH16 , &scroller_credz);

  codeCredz = new_sprite( 140 , 104 , ballGFX->x-50 , ballGFX->y , DEPTH16 , &code_credz);

  graphicsCredz = new_sprite( 140 , 104 , ballGFX->x-50 , ballGFX->y , DEPTH16 , &graphics_credz);

  sideGrid01 = new_sprite( 64 , 32 , -42 , 0 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid01 , d , 0 );

  sideGrid02 = new_sprite( 64 , 32 , -34 , 32 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid02 , d , 0 );

  sideGrid03 = new_sprite( 64 , 32 , -26 , 64 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid03 , d , 0 );

  sideGrid04 = new_sprite( 64 , 32 , -10 , 96 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid04 , d , 0 );

  sideGrid05 = new_sprite( 64 , 32 , -18 , 128 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid05 , d , 0 );

  sideGrid06 = new_sprite( 64 , 32 , -18 , 160 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid06 , d , 0 );

  sideGrid07 = new_sprite( 64 , 32 , -26 , 192 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid07 , d , 0 );

  sideGrid08 = new_sprite( 64 , 32 , -10 , 224 , DEPTH16 , &side_grid);

  attach_sprite_to_display_at_layer( sideGrid08 , d , 0 );


  //Flying bricks  (WATCH YOUR HEAD!)

  brick8 = new_sprite( 8 , 8 , 0 , 0 , DEPTH16 , &brick_8);

  brick16 = new_sprite( 16 , 16 , 0 , 0 , DEPTH16 , &brick_16);

  brick24 = new_sprite( 24 , 24 , 0 , 0 , DEPTH16 , &brick_24);

  //Flags // 0 = off , 1 = on

  brick8Flag = 0;
  brick16Flag = 0;
  brick24Flag = 0;
  brick8Flag2 = 0;
  brick16Flag2 = 0;
  brick24Flag2 = 0;
  brickRandom = 0;
    

  
  //-----Set transition state
  
  tranInOut = 1;
  
  //-----Additional variables for the part of the demo.
    
  int floor = 180;//At what postion the object will bounce up.
  
  int bounces = 0; //increments by 1 on each bounce.  bounce 3 will remove the floor and let the object drop.
  
  int credits = 0;  //0 = music , 1 = code , 2 = graphics
		
    
    while (1) {
	
    vsync();
	

    //Run our clock

    time = clock( cyclesPerSecond, &cpucycles, &time); //pass our clock variables into our clock function to update how many seconds have passed.
    
    //Bounce the graphics
    
    musicCredz->y = ballGFX->y-48;
    scrollerCredz->y = ballGFX->y-48;
    codeCredz->y = ballGFX->y-48;
    graphicsCredz->y = ballGFX->y-48;
    
    move_me(ballGFX, controller, ballMotion);//movement function for first bird

    if(ballGFX->y >= floor && bounces <= 8){
      
      controller = 1;
      
    }
    
    if(ballMotion->delay == 1){
      
      bounces += 1; 
      
    }
    
    if( bounces == 8){
      
      floor = 200; 
      
    }
    
    if (ballGFX->y >= 350){
      
      ballGFX->y =  -110;
      
      bounces = 0;
      
      floor = 180;
      
      ballMotion->delay = 0;
      
      credits += 1;
	  
	  if( credits == 4){
	    
	    credits = 0; 
	    
	  }

	  if( credits == 0 ){
	    
	    detach_sprite_from_display(graphicsCredz);
	    
	    attach_sprite_to_display_at_layer(musicCredz , d , 2);
	    
	  }

	  if( credits == 1 ){
	    
	    detach_sprite_from_display(musicCredz);
	    
	    attach_sprite_to_display_at_layer(scrollerCredz , d , 2);
	    
	  }

	  if( credits == 2 ){
	    
	    detach_sprite_from_display(scrollerCredz);
	    
	    attach_sprite_to_display_at_layer(codeCredz , d , 2);
	    
	  }

	  if( credits == 3 ){
	    
	    detach_sprite_from_display(codeCredz);
	    
	    attach_sprite_to_display_at_layer(graphicsCredz , d , 2);
	    
	  }
			  
      }
			
      if (ballGFX->y <= floor){
      
      controller = 0;
	
      }
		
    //run our transition in
    if(tranInOut == 1){
    
    tranDelay += 1;
    
	if(tranDelay >= tranSpeed){

	spaceCounter ++;
    
	gridInOut( 20 , 20, spaceCounter , 193, 20 , 20 , sprite_scr , tran_scr, tranInOut );
	
	tranDelay = 0;
		    
	}
	      
	    
	if(tran_scr->y == 0 && tran_scr->x == 0){
      
	spaceCounter = 0;
	
	tranDelay = 0;

	spaceCounter = 0;
	
	tranInOut = 4;
	
	clear_screen(tran_scr);
	  
	}
      
    }

    //Run the randomizer for the brick
    
    brickRandom = rand()%24 +1;
		
    //Flying Bricks
		

    //brick8_01
    if(brickRandom == 2 && brick8Flag == 0 ){
  
    brick8->y = rand()%240 + 1;
    
    brick8->x = 325;
    
    attach_sprite_to_display_at_layer(brick8 , d , 1);
    
    brick8Flag = 1;
      
    }
    
    if( brick8Flag == 1){
      
    brick8->x -= 2; 
      
    }
    
    if(brick8->x <= -10 && brick8Flag == 1){
      
    brick8Flag = 0; 
    
    detach_sprite_from_display(brick8);
      
    }


    //brick16
    if(brickRandom == 12 && brick16Flag == 0 ){
  
    brick16->y = rand()%240 + 1;
    
    brick16->x = 325;
    
    attach_sprite_to_display_at_layer(brick16 , d , 4);
    
    brick16Flag = 1;
      
    }
    
    if( brick16Flag == 1){
      
    brick16->x -= 3; 
      
    }
    
    if(brick16->x <= -17 && brick16Flag == 1){
      
    brick16Flag = 0; 
    
    detach_sprite_from_display(brick16);
      
    }


    //brick24
    if(brickRandom == 24 && brick24Flag == 0 ){
  
    brick24->y = rand()%240 + 1;
    
    brick24->x = 325;
    
    attach_sprite_to_display_at_layer(brick24 , d , 10);
    
    brick24Flag = 1;
      
    }
    
    if( brick24Flag == 1){
      
    brick24->x -= 4; 
      
    }
    
    if(brick24->x <= -24 && brick24Flag == 1){
      
    brick24Flag = 0; 
    
    detach_sprite_from_display(brick24);
      
    }

    //brick8_2
    if(brickRandom == 4 && brick8Flag2 == 0 ){
  
    brick8_2->y = rand()%240 + 1;
    
    brick8_2->x = 325;
    
    attach_sprite_to_display_at_layer(brick8_2 , d , 0);
    
    brick8Flag2 = 1;
    
    }
    
    if( brick8Flag2 == 1){
      
    brick8_2->x -= 2; 
      
    }
    
    if(brick8_2->x <= -10 && brick8Flag2 == 1){
      
    brick8Flag2 = 0; 
    
    detach_sprite_from_display(brick8_2);
      
    }


    //brick16_2
    if(brickRandom == 8 && brick16Flag2 == 0 ){
  
    brick16_2->y = rand()%240 + 1;
    
    brick16_2->x = 325;
    
    attach_sprite_to_display_at_layer(brick16_2 , d , 2);
    
    brick16Flag2 = 1;
      
    }
    
    if( brick16Flag2 == 1){
      
    brick16_2->x -= 3; 
      
    }
    
    if(brick16_2->x <= -17 && brick16Flag2 == 1){
      
    brick16Flag2 = 0; 
    
    detach_sprite_from_display(brick16_2);
      
    }


    //brick24_2
    if(brickRandom == 20 && brick24Flag2 == 0 ){
  
    brick24_2->y = rand()%240 + 1;
    
    brick24_2->x = 325;
    
    attach_sprite_to_display_at_layer(brick24_2 , d , 6);
    
    brick24Flag2 = 1;
      
    }
    
    if( brick24Flag2 == 1){
      
    brick24_2->x -= 4; 
      
    }
    
    if(brick24_2->x <= -24 && brick24Flag2 == 1){
      
    brick24Flag2 = 0; 
    
    detach_sprite_from_display(brick24_2);
      
    }
		
    //Run our transition out
    
    if(time >= 197/*set at least 3 seconds before you break out of the CASE*/){

    tranInOut = 0;
	
    }
      
      
      if(tranInOut == 0){
    
      tranDelay ++;

	  if(tranDelay >= tranSpeed){

	  spaceCounter += 1;
      
	  gridInOut( 20 , 20, spaceCounter , 193, 20 , 20 , sprite_scr , tran_scr, tranInOut );
	  
	  tranDelay = 0;
		      
	  }
	      
	  if(tran_scr->y == 240 && tran_scr->x == 0){
	  
	  tranDelay = 0;
	  
	  spaceCounter = 0;
	  
	  tranInOut = 4;
	  
	  time = 200;
	    
	  }

      }
      
    if(time == 200 && tranInOut == 4){
    
    demoPos = 3;
    
    break; // break out of case while loop 
      
    }
	
    }//end of case 2 while loop

    //-----Pull out sprites
    
    detach_sprite_from_display(loadedBG);
	    
    detach_sprite_from_display(brick8);
    
    detach_sprite_from_display(brick16);
    
    detach_sprite_from_display(brick24);
	    
    detach_sprite_from_display(brick8_2);
    
    detach_sprite_from_display(brick16_2);
    
    detach_sprite_from_display(brick24_2);
    
    detach_sprite_from_display(ballGFX);
    
    detach_sprite_from_display(musicCredz);
    
    detach_sprite_from_display(scrollerCredz);
    
    detach_sprite_from_display(codeCredz);
    
    detach_sprite_from_display(graphicsCredz);
    
    detach_sprite_from_display(sideGrid01);
    
    detach_sprite_from_display(sideGrid02);
    
    detach_sprite_from_display(sideGrid03);
    
    detach_sprite_from_display(sideGrid04);
    
    detach_sprite_from_display(sideGrid05);
    
    detach_sprite_from_display(sideGrid06);
    
    detach_sprite_from_display(sideGrid07);
    
    detach_sprite_from_display(sideGrid08);
    
    bounces = 0;
    
    floor = 180;
    
    ballMotion->delay = 0;
    
    credits = 0;
    
break; // break out of case
    
  //--------------------------------------------------------------
  
  //--------------------------------------------------------------
  case 3: //End Card
    
  //-----Pull in sprites

  mcfur_middle_1 = new_sprite( 24 , 24 , -100 , 101 , DEPTH16 , &mcm1);

      mcfur_middle_1->animation = &mcfur_animation;
      mcfur_middle_1->animated = 1;
      mcfur_middle_1->animation_data.counter = 1;
      mcfur_middle_1->animation_data.index = 1;

  attach_sprite_to_display_at_layer( mcfur_middle_1 , d , 3); //Attach to display
      
  qrCode = new_sprite( 128 , 128 , (320/2)-(128/2) , (200/2)-(164/2), DEPTH16, &qr_code);

  attach_sprite_to_display_at_layer( qrCode , d , 1);

  followCat = new_sprite( 192 , 12 , (320/2)-(236/2) , qrCode->y+128+35 , DEPTH16 , &follow_cat);

  attach_sprite_to_display_at_layer( followCat , d , 2);
    
  followCat00 = new_sprite( 36 , 12 , followCat->x+192+8 , followCat->y , DEPTH16 , &follow_cat_00);

      followCat00->animation = &cat_text_animation;
      followCat00->animated = 1;
      followCat00->animation_data.counter = 1;
      followCat00->animation_data.index = 1;
		  
  attach_sprite_to_display_at_layer(followCat00 , d , 2);
  
  //Scroller frame grid pieces
  
  gridFrame01 = new_sprite(320 , 8 , 0 , followCat->y -14 , DEPTH16 , &grid_frame);
  
  attach_sprite_to_display_at_layer( gridFrame01 , d , 2);
  
  gridFrame02 = new_sprite(320 , 8 , 0 , followCat->y + 16 , DEPTH16 , &grid_frame);
  
  attach_sprite_to_display_at_layer( gridFrame02 , d , 2);

  tranWipe = new_sprite( 340 , 260 , -10 , -260 , DEPTH16 , &tran_wipe);

      tranWipe->trans = 0;
	  
  //Set our transition state
      
  tranInOut = 1;

    while (1) {
    
    vsync();
    
    //Run our clock

    time = clock( cyclesPerSecond, &cpucycles, &time); //pass our clock variables into our clock function to update how many seconds have passed.
	    
    //run our transition in
    if(tranInOut == 1){
    
    tranDelay += 1;

	if(tranDelay >= tranSpeed){

	spaceCounter ++;
    
	gridInOut( 20 , 20, spaceCounter , 193, 20 , 20 , sprite_scr , tran_scr, tranInOut );
	
	tranDelay = 0;
		  
	}
		    
	if(tran_scr->y == 0 && tran_scr->x == 0){
      
	spaceCounter = 0;
	
	tranDelay = 0;

	spaceCounter = 0;
	
	tranInOut = 3;
	
	clear_screen(tran_scr);
	  
	}
      
    }
			
    //If mcfurs Y goes over the amount specified by mcfur_alt_limit then make him flap


    if(mcfur_middle_1->y > mcfur_alt_limit){
      
    flap = 1; // make him flap
      
    }
    
    if(mcfur_middle_1->x != 148){
      
    mcfur_middle_1->x += 1;
      
    }
    
    if(time > 208 ) {
      
    mcfur_middle_1->x += 1;	  
      
    }
	  
	  

    birdy_move(mcfur_middle_1, bird_1, flap);//movement function for first bird

    mcfur_alt_limit = rand()%31 + mcfur_alt_position; //Randomize his altitued limit

    flap = 0; // turn off flap input    
    
    
    
    if(time >= 212 && tranInOut == 3){
    
    attach_sprite_to_display_at_layer(tranWipe , d , 8); 
    
    tranInOut = 4;
      
    }
    
    
    if(time > 213 && tranWipe->y != -10 ){
      
    tranWipe->y += 2; 
      
    }
    
    		
    if(time > 215  && tranInOut == 4){
    
    demoPos = 9;
      
    break; // break out of case while loop 
      
    }
	
    }//end of case 0 while loop

    //-----Pull out sprites
    
    detach_sprite_from_display(mcfur_middle_1);
    
    detach_sprite_from_display(qrCode);
    
    detach_sprite_from_display(followCat);
    
    detach_sprite_from_display(followCat00);
    
    detach_sprite_from_display(tranWipe);
    
    detach_sprite_from_display(gridFrame01);
    
    detach_sprite_from_display(gridFrame02);
    
    //-----Reset Variables
	    
break; // break out of case
    
  //--------------------------------------------------------------
  
  //--------------------------------------------------------------
  case 9: //Reset and start the demo again.
    
  //-----Reset timing varibles

  time = 0;
  cpucycles = 0;

  //-----Stop the music

  clear_module();  //Stop and deinitialize MOD file
		  
  //-----Clear our screen buffers
	    
  clear_screen(tran_scr);
	  
  //-----Start the music

  init_module(&music,tempo); //Initialize MOD file

  vblQueue[0] = vbl_handler; //Play MOD file.

  demoPos = 0; //Start at the beginning of the demo
    
break; // break out of case
    
  //--------------------------------------------------------------
  
  
     }//end of switch/case statement
	
	
		  
    
  }//End of Main While loop
}
