/*
** PRIMITIV.C - OP71xx graphics primitives sample program.
*/

// Uncomment exactly one of the following two libraries to select
// landscape (OP71L.LIB) or portrait (OP71P.LIB) display mode.
#use OP71L.LIB
//#use OP71P.LIB
#use OP71HW.LIB

// Size bitmaps to fit nicely in landscape or portrait mode.
#if (OP71XS < OP71YS)
#define BITMAP_XS (OP71XS / 4)
#define BITMAP_YS (OP71YS / 5)
#else
#define BITMAP_XS (OP71XS / 5)
#define BITMAP_YS (OP71YS / 4)
#endif

// This is a generous bitmap size (in bytes) calculation,
// needed to accomodate possibly unused bits in some bytes.
#if (BITMAP_XS < BITMAP_YS)
#define BITMAP_SIZE (((BITMAP_XS + 7) / 8) * BITMAP_YS)
#else
#define BITMAP_SIZE (BITMAP_XS * ((BITMAP_YS + 7) / 8))
#endif

#define NUM_VERTICES 4

// Uncomment to display landscape-mode upside down.
//#define UPSIDE_DOWN

xdata clippedBitMap[BITMAP_SIZE];
xdata myBitMap[BITMAP_SIZE];

main()
{
	int bitMapXS, bitMapYS, clippedMapXS,
		i, j, k,
		maxXYS, minXYS, onOff,
		polyV[NUM_VERTICES * 2];	// array of vertex coordinates
	
	_GLOBAL_INIT();
	glInit();
	glSetBrushType(GL_XOR);
	clippedMapXS = OP71XS % BITMAP_XS;
	maxXYS = (OP71XS > OP71YS) ? OP71XS : OP71YS;	// maximum X or Y size
	minXYS = (OP71XS < OP71YS) ? OP71XS : OP71YS;	// minimum X or Y size
	onOff = 1;	// glInit() sets backlight on
	while(1)
	{
		for(k = 0; k < 2; k++)
		{
			polyV[0] = OP71XS / 4; polyV[1] = OP71YS / 4;
			polyV[2] = 0; polyV[3] = OP71YS / 3;
			polyV[4] = OP71XS / 3; polyV[5] = OP71YS / 3;
			polyV[6] = OP71XS / 3; polyV[7] = 0;
			op71BuffLock();
			for(j = 0; j < minXYS - (minXYS / 3); j++)
			{
				hitwd();
				glPlotVPolygon(NUM_VERTICES, polyV);
				for(i = 0; i < (sizeof(polyV) / sizeof(int)); i++)
				{
					(polyV[i])++;
				}
				if((j % 10) == 0)
				{
					op71BuffUnlock();
					op71BuffLock();
				}
			}
			op71BuffUnlock();
		}
		for(k = 0; k < 2; k++)
		{
			op71BuffLock();
			for(j = minXYS - 1; j >= 0; --j)
			{
				hitwd();
				glPlotDot(j, j);
				if((j % 10) == 0)
				{
					op71BuffUnlock();
					op71BuffLock();
				}
			}
			op71BuffUnlock();
		}
		for(k = 0; k < 2; k++)
		{
			op71BuffLock();
			for(j = minXYS / 2 - 1; j >= 0; --j)
			{
				hitwd();
				glPlotCircle(OP71XS / 2, OP71YS / 2, j);
				if((j % 10) == 0)
				{
					op71BuffUnlock();
					op71BuffLock();
				}
			}
			op71BuffUnlock();
		}
		for(k = 0; k < 2; k++)
		{
			op71BuffLock();
			for(j = 0; j < OP71YS; j++)
			{
				hitwd();
				glPlotLine(0, j, OP71XS - 1, OP71YS - 1 - j);
				if((j % 10) == 0)
				{
					op71BuffUnlock();
					op71BuffLock();
				}
			}
			op71BuffUnlock();
			op71BuffLock();
			for(i = OP71XS - 1; i >= 0; --i)
			{
				hitwd();
				glPlotLine(i, 0, OP71XS - 1 - i, OP71YS - 1);
				if((i % 10) == 0)
				{
					op71BuffUnlock();
					op71BuffLock();
				}
			}
			op71BuffUnlock();
			if(k == 0)
			{
				glXGetBitmap((OP71XS - BITMAP_XS) / 2, 
						(OP71YS - BITMAP_YS) / 2, BITMAP_XS, BITMAP_YS, 
						myBitMap);
				if(clippedMapXS > 0)
				{
					glXGetBitmap((OP71XS - BITMAP_XS) / 2, 
							(OP71YS - BITMAP_YS) / 2, clippedMapXS, BITMAP_YS, 
							clippedBitMap);
				}
				glXPutBitmap((OP71XS - BITMAP_XS) / 2, 
						(OP71YS - BITMAP_YS) / 2, BITMAP_XS, BITMAP_YS, 
						myBitMap);
				for(i = 15000; i; --i)
				{
					hitwd();
				}
				glXPutBitmap((OP71XS - BITMAP_XS) / 2, 
						(OP71YS - BITMAP_YS) / 2, BITMAP_XS, BITMAP_YS, 
						myBitMap);
			}
		}
		for(k = 0; k < 2; k++)
		{
			for(j = 0; j < OP71YS; j += BITMAP_YS)
			{
				for(i = 0; i < OP71XS; i += BITMAP_XS)
				{
					hitwd();
					bitMapXS = (BITMAP_XS < OP71XS - i) ? BITMAP_XS : OP71XS - i;
					bitMapYS = (BITMAP_YS < OP71YS - j) ? BITMAP_YS : OP71YS - j;
					if(bitMapXS < BITMAP_XS)
					{
						glXPutBitmap(i, j, bitMapXS, bitMapYS, clippedBitMap);
					}
					else
					{
						glXPutBitmap(i, j, bitMapXS, bitMapYS, myBitMap);
					}
				}
			}
		}
		onOff ^= 1;
//		onOff = !onOff;
//		printf("onOff = %d\n", onOff);
//		op71BackLight(onOff);
	}
}
