// Renders a falling "block"..

use tksdl;
use tkopengl;

float y0  = 1;
float y   = y0;
float acc = 5*0.001;
float v0  = 0;
float vel = v0;

float time_since_coll = 0;

function Calc(float t) {
   // Also see <http://en.wikipedia.org/wiki/Uniform_acceleration>
   float lastY = 0.5 * acc * time_since_coll*time_since_coll + v0*time_since_coll + y0;
   time_since_coll += t;
   float currY = 0.5 * acc * time_since_coll*time_since_coll + v0*time_since_coll + y0;
   float deltaY = (currY - lastY);
   y += deltaY;
}

function onDraw() {
   float dt = FPS.precision;
   Calc(dt);

   glClearColor(0, 0, 0.2, 1);
   glClear(GL_COLOR_BUFFER_BIT);
   zglInitOrtho(-10, 10);
   glColor3f(0.9,0.9,0.9);
   glTranslatef(0, y-10, 0);
   glBegin(GL_QUADS);
   glVertex2f(-1, -1);
   glVertex2f( 1, -1);
   glVertex2f( 1,  1);
   glVertex2f(-1,  1);
   glEnd();
}

function onKeyboard(Key _k) {
   switch(_k.pressed)
   {
      case VKEY_ESCAPE:
         SDL.exitEventLoop();
         break;

      case VKEY_SPACE:
         y = y0;
         time_since_coll = 0;
         break;
   }
}

function main() {
    Viewport.openWindow(640, 480);
    use callbacks;
    FPS.tickInterval=1000.0/60;
    Viewport.swapInterval(1);
    SDL.eventLoop();
}