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

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

/*
 * The offscreen buffer, and the palette
 */
br_pixelmap *palette, *back_buffer;

/*
 * Pointer to primitive heap
 */
static void *primitives;

/*
 * The actors in the world and the module specific variables
 */
static br_actor *observer, *world;
static br_actor *a;
static br_pixelmap *screen_buffer;
static int i=0;

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

   // Setup renderer and screen

    r = BrDemo_Allocate_Pixelmaps( "Minimal (Z-Sort)",
                                   &screen_buffer,&back_buffer,NULL,
                                   &palette,"winstd.pal");

    if ( r != BRE_OK )
    {
        BR_ERROR0("Could not allocate pixelmaps");
        return r ;
    }

    // Set back buffer origin

    back_buffer->origin_x = back_buffer->width / 2 ;
    back_buffer->origin_y = back_buffer->height / 2 ;
	
    primitives = BrMemAllocate(10000,BR_MEMORY_APPLICATION);
    BrZsBegin(screen_buffer->type,primitives,10000);

    // Build a world

	world = BrActorAllocate(BR_ACTOR_NONE,NULL);

	// BrDemo_SetRootActor lets the demo framework know which 
	// actor to enumerate when turning on/off actor based properties.
	
	BrDemo_SetRootActor(world);
	
	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));

    return BRE_OK ;
} // End Init_App()

	
/*
 * Tumble the actor around
 */

br_error BrDemo_Render_Loop()
{
    if (!back_buffer) 
	   return BRE_OK ;
         
    // Clear the buffers
     
	BrPixelmapFill( back_buffer, 0 );

    BrZsSceneRender(world,observer,back_buffer);
    BrPixelmapDoubleBuffer(screen_buffer,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;
}



br_error BrDemo_ShutDown() 
{
    // Close down

    BrPixelmapFree(back_buffer);
    back_buffer = NULL;
    BrZsEnd();

	return BRE_OK ;
}


br_error BrDemo_OnResize()
{
	BrDemo_Refresh_Pointers( &screen_buffer, &back_buffer, NULL );

    // Set back buffer origin

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

	return BRE_OK ;
}    
