/*====================================================
Piano.cpp

Simple app that simulates a piano keyboard.
====================================================*/

#include "DragonFireSDK.h"

// Defines the position of each of the white keys.
int WhiteKeyY[9] = {0, 55, 110, 165, 220, 275, 330, 385, 440};
// Defines the position of each of the black keys.
// Each is offset from its neighboring white key by 35
// pixels.

int BlackKeyY[6] = { 35, 90, 200, 255, 310, 420};
// Holds handles to each of the white piano keys' sounds.
int WhiteKeySound[9];
// Holds handles to each of the black piano keys' sounds.
int BlackKeySound[6];
// Forward declaration for Key Press Event.
int OnKeyPress(int id);

//====================================================
// AppMain is the first function called by the
// DragonFireSDK. It is resposible
// for initialization of all variables and creating the
// appilcation's objects.

void AppMain()
{
   int i;
   char FileName[255];

   // View iPhone Simulator Landscape
   LandscapeMode();

   // Load a background image.
   ViewAdd(
"Assets/Images/Background.png", 0, 0);

   // Initialize each white key and then set up its corresponding sound.
   // White key sounds and ID's start at 0.

   for (i = 0; i < 9; i++)
   {
      PushButtonAdd(
"Assets/Images/WhiteKey", WhiteKeyY[i], 0, OnKeyPress, i);
      sprintf(FileName,
"Assets/Sounds/WhiteKey%d.wav", i);
      WhiteKeySound[i] = SoundAdd(FileName);
   }

   // Initialize each black key and then set up its corresponding sound.
   // Black key sounds start at zero, their ID's start at 10.

   for (i = 0; i < 6; i++)
   {
      PushButtonAdd(
"Assets/Images/BlackKey", BlackKeyY[i], 0, OnKeyPress, i+10);
      sprintf(FileName,
"Assets/Sounds/BlackKey%d.wav", i);
      BlackKeySound[i] = SoundAdd(FileName);
   }

   // Load an image to hide the "top" of each key.
   ViewAdd(
"Assets/Images/TopBar.png", 0, 0);
}

//====================================================
void OnTimer()
{
   // Do nothing.
}

//====================================================
// OnKeyPress responds to all Key Presses on the piano keyboard.
int OnKeyPress(int id)
{
   // White keys have ID's less than 10. No index adjustment necessary.
   if (id < 10)
   {
      SoundPlay(WhiteKeySound[id]);
   }

   // Black keys have ID's greater than 10.
   //Adjust index by 10 for Black Key range.

   else
   {
      SoundPlay(BlackKeySound[id-10]);
   }

   return 0;
}