//_|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//_|___|___|_B r i c k   B r e a k e r   P r o t o t y p e |___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//_|___|___|___|___|___|___|f o r   M S  X  2__|___|___|___|___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//_|___|___|___|___|Part of the Fusion-c Coding Tutorial___|___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//_|___|___|___|___|___|___|__               __|___|___|___|___|___|___|
//___|___|___|___|___|___|__Eric Boez (c) 2018___|___|___|___|___|___|__
//_|___|___|___|___|___|___|___|           _|___|___|___|___|___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//_|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//_|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//_|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|
//___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|___|__
//                 M a d e   w i t h     F U S I O N - C                        
//

#include "fusion-c/header/msx_fusion.h"
#include "fusion-c/header/vdp_sprites.h"
#include "fusion-c/header/vdp_graph2.h"


// Sprites Definition Patterns
//
static const unsigned char board_pattern[]={
  0b11111111,
  0b11111111,
  0b11111111,
  0b11111111,
  0b11111111,
  0b00000000,
  0b00000000,
  0b00000000
};

static const unsigned char ball_pattern[]={
    0b00111100,
    0b01111110,
    0b11111111,
    0b11111111,
    0b11111111,
    0b11111111,
    0b01111110,
    0b00111100
};

int x;
int y;
int dir_x;
int dir_y;
int px;
int py;

void FT_wait(int cicles)
{
  int i;
  for(i=0;i<cicles;i++) 
    {
      Halt();
    }
}


// Show Sprites on screen, and do graphic job
//
void FT_Srpites(void)
{
         PutSprite(0,1,x,y,10);           // Ball Sprite
         PutSprite(1,0,px,py,10);         // The Paddle is composed of 3 sprites
         PutSprite(2,0,px+8,py,10);
         PutSprite(3,0,px+16,py,10);
}   

void FT_CalcPosition (void)
{
    // If the Ball is hurting left or right border of the screen
    if (x>247 || x<1)
    {
        dir_x *= -1;
    }
    
    // If the Ball is hurting up or bottom border of the screen
    if (y>187 || y<1)
    {
        dir_y *= -1;
    }

    x+=dir_x;
    y+=dir_y;
}

// Joystick control function
//
void FT_Joy()
{
  char joy;
  char trig;

  joy=JoystickRead(0);
  trig=TriggerRead(0);

  // FT_Srpites();

    if (trig!=0)
    {
      Screen(0);
      Exit(0);
    }

    switch (joy)
    {
        case 3:
        if (px<231)
        {
            px++;
        }
        break;

        case 7:
        if (px>0)
        {
          px--;
        }
        break;
    }
}


void main(void)
{
    char BallColors[8]= {15,8,8,8,8,8,8,15};
    char BoardColors[8]={15,15,15,15,15,15,15,15};
    
    x=170;
    y=110;
    dir_x=1;
    dir_y=-1;
    px=170;
    py=185;


    // Preparing Screen
    SetColors(15,0,1);
    Screen(8);
    SetColors(255,0,100);
    Cls();
    KeySound(0);
    VDP60Hz();

    // Check MSX
    if(ReadMSXtype()==3)  // If MSX is Turbo-R Switch CPU to Z80 Mode
    {
        ChangeCPU(0);
    }

    // Creating Sprites
    SpriteReset();
    SpriteSmall();
    SetSpritePattern(0, board_pattern,8);
    SetSpritePattern(1, ball_pattern,8);

    SC8SpriteColors(0,BallColors);
    SC8SpriteColors(1,BoardColors);
    SC8SpriteColors(2,BoardColors);
    SC8SpriteColors(3,BoardColors);

  while (1)
  {  
      FT_Joy();

      FT_CalcPosition();

      FT_Srpites(); 

      FT_wait(1);
  }

}