/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.03  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Local headers */
#include "ablibs.h"
#include "abimport.h"
#include "proto.h"

// For the purposes of this example, we'll accept only
// 565, 555, or 8888 as possible targets.

typedef union vidptr {
    uint8_t  * volatile ptr8;
    uint16_t * volatile ptr16;
    uint32_t * volatile ptr32;
} VidPtr_t;


PdOffscreenContext_t *buff;
VidPtr_t main_ptr,work_ptr;
uint32_t color,bytespp;
PhRect_t rect;
int i, j, k;

void display()
{
	printf("Create the offscreen context....\n");
// Create the offscreen context
buff=PdCreateOffscreenContext(0,100,100,
                              Pg_OSC_MEM_PAGE_ALIGN);
if (buff==NULL)
{
    // Error code
    printf("error\n");
    return;
}

// figure out which value color should be
switch (buff->format)
{
    case Pg_IMAGE_DIRECT_565 :
        color = 0x0000FFFF; 
        bytespp=2;
    break;
    case Pg_IMAGE_DIRECT_555 :
        color = 0x00007FFF;
        bytespp=2;
    break;
    case Pg_IMAGE_DIRECT_8888 :
        color = 0x00FFFFFF;
        bytespp=4;
    break;
    default:
        //Error code
        return;
}


rect.ul.x=rect.ul.y=0;
rect.lr.x=rect.lr.y=99;

main_ptr.ptr8=(unsigned char *) 
                 PdGetOffscreenContextPtr(buff);
if (main_ptr.ptr8 == NULL)
{
    // Error code
}

// Clear the context to black
PhDCSetCurrent(buff);
PgSetFillColor(Pg_BLACK);
PgDrawRect(&rect,Pg_DRAW_FILL);
PgFlush();

 Ensure that all drawing operations are done before
 writing using software to this context:
PgWaitHWIdle();

 draw the line in the middle:
//work_ptr.ptr8=main_ptr.ptr8 + (49 * bytespp);
//
//for(j = 0; j < 50; j++)
//{
//	for(k = 0; k < 50; k++)
//		memset(main_ptr.ptr8, 0xff, 50*buff->pitch);
//}
for (i=0; i<100; i++, work_ptr.ptr8+=buff->pitch)
{
    switch (bytespp)
    {
        case 2 :
            *work_ptr.ptr16 = color;
        break;
        case 4 :
            *work_ptr.ptr32 = color;
        break;
    }
}

PgContextBlit(buff,&rect,NULL,&rect); 
PgFlush();
printf("color = %d\n", color);
printf("bytespp = %d\n", bytespp); 
}
// You should see a black rectangle with a vertical
// white line in the middle

int
cb_button( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )

	{

	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;
	display();

	return( Pt_CONTINUE );

	}

