// This is test.cc CVS version: $Id: test.cc,v 1.4 2000/02/28 21:49:58 andreaha Exp $
#include "eng.h"
#include <SDL/SDL.h>
#include <SDL/SDL_video.h>

int main(int argc, char **argv) {

  SDL_Init(SDL_INIT_VIDEO);

  World::Material *m[10];
  World::Object   *o[50];
  World::Light    *l[10];
  World::Camera   *c[20];
  World *w = new World;
  int *tmp_buffer;

  char filename[256];
  int frame = 0;
  float n = 0.0, nn = 0.0, mm = 0.0;
  int Quit = 0;
  int Focus;
  int x, y;
  int rex, rey;
  int dep;
  int switch_ = 0;

  float pi = 3.1415927;
  float topi = pi * 2.0;

  if (argc < 3) {
    rex = 320;
    rey = 200;
  } else {
    rex = atoi(argv[1]);
    rey = atoi(argv[2]);
  }

  dep = 16;

  tmp_buffer = new int[rex * rey * (dep >> 3)];
   
  SDL_Surface *screen = SDL_SetVideoMode(rex, rey, dep, SDL_HWSURFACE | SDL_HWACCEL | SDL_ASYNCBLIT);
  char *mem = (char *)(screen->pixels);

  m[0] = w->newMaterial("gfx/spot.pcx");
  m[1] = w->newMaterial("gfx/txtmarb4.pcx");
  m[2] = w->newMaterial("gfx/red.pcx");
  m[3] = w->newMaterial("gfx/green.pcx");
  m[4] = w->newMaterial("gfx/blue.pcx");

  o[0] = w->newObject("mesh/floor.asc");
  o[0]->setMaterial(m[0]);
  o[0]->setFillType(TEXTURE | GOURAUD);
  o[0]->scaleTo(70);
  o[0]->pos.set(0, 40, 0);
  o[0]->rot.set(pi/2, 0, 0);
   
  o[1] = w->newObject("mesh/bogga3_2.asc", 1, 1000);
  o[1]->setMaterial(m[1]);
  o[1]->setFillType(TEXTURE | GOURAUD);
  o[1]->scaleTo(30);
  o[1]->pos.set(0, -20, 0);
  o[1]->rot.set(0, 0, 0);

  o[2] = w->newObject("mesh/box.asc");
  o[2]->setMaterial(m[2]);
  o[2]->setFillType(TEXTURE);
  o[2]->scaleTo(5);
  o[2]->rot.set(0, 0, 0);

  o[3] = w->newObject("mesh/box.asc");
  o[3]->setMaterial(m[3]);
  o[3]->setFillType(TEXTURE);
  o[3]->scaleTo(5);
  o[3]->rot.set(0, 0, 0);

  o[4] = w->newObject("mesh/box.asc");
  o[4]->setMaterial(m[4]);
  o[4]->setFillType(TEXTURE);
  o[4]->scaleTo(5);
  o[4]->rot.set(0, 0, 0);

  l[0] = w->newLight(1000, 1, 0, 0);
  l[1] = w->newLight(1000, 0, 1, 0);
  l[2] = w->newLight(1000, 0, 0, 1);

  c[0] = w->newCamera();
  c[0]->target.set(0, 0, 0);
//  c[0]->setRoll(pi);
//  c[0]->setFocus(20,3);

  w->setAntiAlias(1);
  w->setRend(rex, rey, dep);
  w->hmul = 0.8;

  nn = 3;
  int fr = 1000000;

  l[0]->pos.set(cos(-nn / 3.0) * 40, -30, sin(-nn / 3.0) * 40);
  l[1]->pos.set(cos(-nn / 4.0) * 40, -30, sin(-nn / 2.0) * 40);
  l[2]->pos.set(cos(-nn / 5.0) * 40, -30, sin(-nn / 1.0) * 40);

  o[2]->pos.set(cos(-nn / 3.0) * 60, -30, sin(-nn / 3.0) * 60);


  while (!Quit && (fr > 0)) {

    SDL_LockSurface(screen);

    c[0]->pos.set(sin(n)*100, -70, cos(n) * 100);
    c[0]->render(tmp_buffer);

    l[0]->pos.set(cos(-nn / 3.0) * 40, -30, sin(-nn / 3.0) * 40);
    l[1]->pos.set(cos(-nn / 4.0) * 40, -30, sin(-nn / 4.0) * 40);
    l[2]->pos.set(cos(-nn / 5.0) * 40, -30, sin(-nn / 5.0) * 40);

    o[2]->pos.set(cos(-nn / 3.0) * 60, -30, sin(-nn / 3.0) * 60);
    o[3]->pos.set(cos(-nn / 4.0) * 60, -30, sin(-nn / 4.0) * 60);
    o[4]->pos.set(cos(-nn / 5.0) * 60, -30, sin(-nn / 5.0) * 60);

    memcpy(mem, tmp_buffer, rex * rey * (dep >> 3));

    SDL_UnlockSurface(screen);

    SDL_UpdateRect(screen, 0, 0, 0, 0);

    //    Graph->Blit();
    n  += 0.01;
    nn += 0.3;

//    sprintf(filename, "dump%04i.ppm", frame++); 
//    w->saveBufAsPPM24(filename);
    fr--;
  }

  delete w;

  SDL_Quit();
  return 0;
}

