//  $Id$
//
//  Pingus - A free Lemmings clone
//  Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program 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 General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include "Soccer.hh"

template<class T>
T mid (T min, T max, T value)
{
  if (value < min) return min;
  if (value > max) return max;
  return value;
}

Ball::Ball (const CL_Vector& arg_pos, CL_ResourceManager* resources)
  : pos (arg_pos),
    ball ("ball", resources),
    shadow ("ballshadow", resources)
{
}

void 
Ball::draw ()
{
  int frame = mid(0, 3, int((pos.z/70.0) * 4));
  shadow.put_screen (int(pos.x), int(pos.y), frame);
  ball.put_screen (int(pos.x), int(pos.y - (pos.z/2) - 4));
}

void 
Ball::update (float delta)
{
  velocity += CL_Vector (0.0, 0.0, -0.03);
  pos += velocity;
  if (pos.z < 0) {
    pos.z = -pos.z;
    if (velocity.z > -0.03) {
      velocity.z = 0.0;
      pos.z = 0.0;
    } else {
      velocity = velocity * -0.9;
    }
  }
}

int 
Soccer::main (int argc, char* argv[])
{
  try 
    {
      CL_SetupCore::init ();
      CL_SetupDisplay::init ();
      CL_SetupPNG::init ();
      
  CL_Display::set_videomode(128, 98, 16, 
			    false, false);

  CL_ResourceManager resources("data/resources.scr", false);

  std::cout << "Soccer Gfx Test" << std::endl;
  background = CL_Surface ("background", &resources);

  Ball ball1 (CL_Vector (10, 40, 50), &resources);
  Ball ball2 (CL_Vector (50, 70, 130), &resources);
  Ball ball3 (CL_Vector (100, 50, 70), &resources);
  while (CL_Keyboard::get_keycode (CL_KEY_ESCAPE) == 0)
    {
      background.put_screen (0, 0);
      ball1.draw ();
      ball2.draw ();
      ball3.draw ();

      ball1.update (0.1f);
      ball2.update (0.1f);
      ball3.update (0.1f);

      CL_Display::flip_display ();
      CL_System::keep_alive ();
      CL_System::sleep (10);
    }

  CL_SetupPNG::deinit ();
  CL_SetupDisplay::deinit ();
  CL_SetupCore::deinit ();
    }
  catch (CL_Error err)
    {
      std::cout << "CL_Error: " << err.message << std::endl;
    }
}

/* EOF */

