//////////////////////////////////////////////////////////////////////
//
// Project  : BRender Minimal Z Buffering Sample
//            Copyright (c) 1996 Argonaut Technologies Ltd.
//            All rights reserved.
//
// Author         : Sam Littlewood
// Demo Framework : Steve Williams 
//
// File     : MINIMAL.C
//
// Requires : BRender V1.3
//            Microsoft DirectX 3.0, Win95/NT or DOS
//
// Comments : This is as simple as it gets ...
//
//////////////////////////////////////////////////////////////////////

#include "..\misc\br_demo.h"


// Actors

static br_actor *observer, *world, *a;

// Pixelmaps

static br_pixelmap *back_buffer, *palette;
static br_pixelmap *screen, *depth_buffer;


br_error BrDemo_InitApp( int argc, char **argv )
{
   br_error r ;

   // Setup renderer and screen

   r = BrDemo_Allocate_Pixelmaps( "Minimal", &screen, &back_buffer,
                                  &depth_buffer, &palette, "winstd.pal" );
	
   if ( r != BRE_OK )
      return r ;

   // Set origins

   back_buffer->origin_x = back_buffer->width / 2 ;
   back_buffer->origin_y = back_buffer->height / 2 ;

   depth_buffer->origin_x = depth_buffer->width / 2 ;
   depth_buffer->origin_y = depth_buffer->height / 2 ;
	
   BrZbBegin( screen->type,BR_PMT_DEPTH_16);

   // Build a world

   world = BrActorAllocate(BR_ACTOR_NONE,NULL);
   observer = BrActorAdd(world,BrActorAllocate(BR_ACTOR_CAMERA,NULL));
   BrLightEnable(BrActorAdd(world,BrActorAllocate(BR_ACTOR_LIGHT,NULL)));

   a = BrActorAdd(world,BrActorAllocate(BR_ACTOR_MODEL,NULL));
   a->t.type = BR_TRANSFORM_EULER;
   a->t.t.euler.e.order = BR_EULER_ZXY_S;
   BrVector3Set(&a->t.t.euler.t,BR_SCALAR(0.0),BR_SCALAR(0.0),BR_SCALAR(-5.0));

   // BrDemo_SetRootActor lets the demo framework know which 
   // actor to enumerate when turning on/off actor based properties.
	
   BrDemo_SetRootActor(world);

   return BRE_OK ;

} // End BrDemo_InitApp()



// Tumble the actor around

br_error BrDemo_Render_Loop()
{
	static int i = 0 ;

	// Clear the buffers

    if ( !back_buffer ) 
	   return BRE_FAIL ;

	BrPixelmapFill( back_buffer, 0 );
	BrPixelmapFill( depth_buffer, 0xFFFFFFFF );

    BrZbSceneRender( world, observer, back_buffer, depth_buffer );
    BrPixelmapDoubleBuffer( screen, back_buffer );

	a->t.t.euler.e.a = BR_ANGLE_DEG(i);
	a->t.t.euler.e.b = BR_ANGLE_DEG(i*2);
	a->t.t.euler.e.c = BR_ANGLE_DEG(i*3);

    i++;

    if (DOSKeyTest(SC_Q,0,REPT_FIRST_DOWN))
       return BRE_FAIL;
    else 
      return BRE_OK ;

} // End Render_Loop()


br_error BrDemo_ShutDown()
{
	// Close down

	BrPixelmapFree(depth_buffer);
    BrPixelmapFree(back_buffer);

    depth_buffer = NULL; 
    back_buffer = NULL;

	BrZbEnd();
    
	return BRE_OK ;
}


br_error BrDemo_OnResize()
{
	BrDemo_Refresh_Pointers( &screen, &back_buffer, &depth_buffer );

	// Set origins

	back_buffer->origin_x = back_buffer->width / 2 ;
	back_buffer->origin_y = back_buffer->height / 2 ;

	depth_buffer->origin_x = depth_buffer->width / 2 ;
	depth_buffer->origin_y = depth_buffer->height / 2 ;

	return BRE_OK ;
}    
