/*
*     ___
*    / _ \___ _    _____ ____
*   / ___/ _ \ |/|/ / -_) __/
*  /_/   \___/__,__/\__/_/
*
*
*   Original Code by John Garrison
*   Written June 3-11,18,25 August 5, 1999
*   To be released under the LGPL
*
*   PowerSound 3d audio example
*
*   Mostly taken from the SDL-demo mixer library
*   borrowed by Max Watson <max@blackholesun.com>
*
*   See LICENSE.TXT for more LGPL info
*
*   MIXERLIB:  An audio mixer library based on the SDL library
*   Copyright (C) 1997-1999  Sam Lantinga
*
*   This library is free software; you can redistribute it and/or
*   modify it under the terms of the GNU Library General Public
*   License as published by the Free Software Foundation; either
*   version 2 of the License, or (at your option) any later version.
*
*   This library is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*   Library General Public License for more details.
*
*   You should have received a copy of the GNU Library General Public
*   License along with this library; if not, write to the Free
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*   Sam Lantinga
*   5635-34 Springhouse Dr.
*   Pleasanton, CA 94588 (USA)
*   slouken@devolution.com
*/

#ifdef __WIN32__
#include <windows.h>
#endif

#include <stdlib.h>

#include "PowerPak/powerpak.h"
#include "PowerPak/power3d.h"
#include "PowerPak/gfx3d.h"
#include "PowerPak/render.h"
#include <PowerPak/powersound.h>

P3D_Color color[4];
P3D_Object cube, camera;
P3D_Object wall[5];

static int audio_open = 0;
static PS_Sample *wave = NULL;

float px, py, pz;

void P3D_Display(void)
{
   int i;

   P3D_SetScreen (SCREENWIDTH3D, SCREENHEIGHT3D, 1.0, 40.0, P3D_NOSPLIT, 1);
   P3D_Clear();
   P3D_ShowCamera(camera);
   P3D_ShowObject(cube);
   for(i=0; i<5; i++)
       P3D_ShowObject(wall[i]);

   cube.posx += px;
   cube.posy += py;
   cube.posz += pz;

   if(cube.posx > 9.50 || cube.posx < -9.50)
       px = -px;
   if(cube.posy > 9.50 || cube.posy < -9.50)
       py = -py;
   if(cube.posz > -5.0 || cube.posz < -20.0)
       pz = -pz;

   PS_SampleDepthSet(wave, (int)cube.posz*6);
   PS_SamplePanSet(wave, (int)cube.posx*6);

   if(!PS_ChannelPlaying(0)) {
       PS_SamplePlayOnChannel(0, wave, 0);
   }

   P3D_ShowDoubleBuffer();
}

void P3D_Reshape (int w, int h)
{
   P3D_SetScreen (w, h, 1.0, 40.0, P3D_NOSPLIT, 1);
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}


void CleanUp(void)
{
    if ( audio_open ) {
        PS_AudioClose();
        audio_open = 0;
    }
    if ( wave ) {
        PS_SampleFree(wave);
        wave = NULL;
    }
    PP_Quit();
}

main(int argc, char *argv[])
{
    Uint32 audio_rate;
    Uint16 audio_format;
    Uint8 audio_channels;
    int i=0;

    /* Initialize variables */
    audio_rate = 22050;
    audio_format = AUDIO_S16;
    audio_channels = 2;

    /* Initialize the library */
    PS_Init();
    P3D_Init(argc, argv);


    // Stolen from Power3D example 1
    P3D_SetGFXMode (640, 480, argv[0], 16, 0);
    glutKeyboardFunc(keyboard);
    color[0] = P3D_CreateColor(1.0, .95, 0.20, 0.0);
    color[1] = P3D_CreateColor(1.0, 0.0, 0.20, 0.0);
    color[2] = P3D_CreateColor(0.40, 0.0, 0.50, 0.0);
    color[3] = P3D_CreateColor(0.0, .60, 0.00, 0.0);
    cube = P3D_CreateSolidSphere(1.0, color[0]);
    cube.posz = -10;
    camera = P3D_CreateCamera();
    camera.roty = 0;

    wall[0] = P3D_CreateSolidBox(40.0, 40.0, 1.0, color[1]);
    wall[0].posz = -21;
    wall[1] = P3D_CreateSolidBox(1.0, 40.0, 40.0, color[2]);
    wall[1].posx = -11;
    wall[2] = P3D_CreateSolidBox(1.0, 40.0, 40.0, color[2]);
    wall[2].posx = 11;
    wall[3] = P3D_CreateSolidBox(40.0, 1.0, 40.0, color[3]);
    wall[3].posy = -11;
    wall[4] = P3D_CreateSolidBox(40.0, 1.0, 40.0, color[3]);
    wall[4].posy = 11;

    /* Open the audio device */
    if (PS_AudioOpen(audio_rate, audio_format, audio_channels, 4096) < 0) {
        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
        exit(2);
    } else {
        PS_AudioQuery(&audio_rate, &audio_format, &audio_channels);
        printf("Opened audio at %d Hz %d bit %s\n", audio_rate,
               (audio_format&0xFF),
               (audio_channels > 1) ? "stereo" : "mono");
    }
    audio_open = 1;

    /* Load the requested wave file */
    wave = PS_SampleLoad("sample2.wav");
    if ( wave == NULL ) {
        fprintf(stderr, "Couldn't load %s: %s\n",
                argv[i], SDL_GetError());
        exit(2);
    }

    px = 0.6;
    py = 0.5;
    pz = -0.7;

   P3D_MainLoop(P3D_Display, P3D_Reshape);

   exit(0);

   return 0;
}
