/***************************************************************************/
/*
*/
/* Drawing a simple line with an XBOX
*/
/*
*/
/***************************************************************************/
#include
<math.h>
#define
SCREEN_WIDTH 640
#define
SCREEN_HEIGHT 480
// Point in memory where we should start writing to.
// int framebuffer = 0xf0040000;
int
framebuffer = 0xf0010000 + 640*320 + 80;
// Remember that at an address in memory..(for the xbox its 0xf0040000), our
// pixels for the screen are arranged as BGR|BGR|BGR|BGR... etc.. 3 bytes
// each.
void
DrawPixel(int x,
int y, unsigned
int dwPixelColourRGB)
// AARRGGBB
{
// Always remember the colours for or memory is
BlueGreenRed :)
unsigned char
red = (dwPixelColourRGB & 0x00ff0000) >> 16;
unsigned char
blue = (dwPixelColourRGB & 0x000000ff);
unsigned char
green = (dwPixelColourRGB & 0x0000ff00) >> 8;
((unsigned
char*)framebuffer)[(x + y*SCREEN_WIDTH)*4+0]
= blue;
((unsigned
char*)framebuffer)[(x + y*SCREEN_WIDTH)*4+1]
= green;
((unsigned
char*)framebuffer)[(x + y*SCREEN_WIDTH)*4+2]
= red;
}
// FIRST AND SIMPLE METHOD OF DRAWING A LINE USING Y=MX+C
// This is our super cool line drawing function - of course I'm neglecting
// boundary checking (e.g. clipping), but I'm sure you can add that in later
:)
void
DrawLine( int x0,
int y0, int x1,
int y1, unsigned
int dwColour )
{
y1
= (SCREEN_HEIGHT)-y1;
y0
= (SCREEN_HEIGHT)-y0;
int deltax = x1-x0;
int deltay = y1-y0;
double m = (double)deltay/(double)deltax;
for( int
x=0; x<=deltax; x++ )
{
double y=m*(double)x;
DrawPixel( x0+x, y0+y, dwColour);
}
}
// EFFICENT ALGORITHM FOR DRAWING A LINE
// based on the "Bresenham Line" principle of drawing a line with
// only integers...no decimals etc
// 8 types of lines
// deltax >=0 deltax <0 deltax>=0 deltax<0
// deltay >=0 deltay>=0 deltay <0 deltay<0
//
// deltax >= deltay deltax< deltay
void
LineR ( int x0,
int y0,
int x1,
int y1,
unsigned
int colour)
{
y1
= (SCREEN_HEIGHT)-y1;
y0
= (SCREEN_HEIGHT)-y0;
int deltax = x1-x0;
int deltay = y1-y0;
int y=0;
int x=0;
int sdx = (deltax < 0) ? -1 : 1;
// sign of deltax (e.g. +ve or -ve)
int sdy = (deltay < 0) ? -1 : 1;
// sdx is the line direction... for x or y... e.g.
if the x value is going left to
// right or right to left... e.g
// if deltax >0 ...then of course sdx=1
// so our line is going from left to right
------>x +ve
// and alternatively
// if deltax <0 ... we get sdx= -1;
// and our line is x<------- -ve
// We only want our delta's to be positive....keep
with positive numbers of
// incrment...so using our sdx/sdy value we set
our delta values to +ve numbers
deltax = sdx * deltax + 1;
deltay = sdy * deltay + 1; // (-1*-6)+1) = 7
int px = x0; //
starting point (x0,y0)
int py = y0;
//float ynum = deltax/2; // num stands for the
numerator starting value
if( deltax >= deltay )
{
for (x = 0; x < deltax; x++)
{
DrawPixel( px, py, colour);
y += deltay; // y=mx... but
if y starts at y0
// then we can
do y+=m
if (y >= deltax)
// m=deltax/deltay and py+=m
{ // if the
numberator is greator than the denomiator
y -= deltax; // we
increment, and subract from the numerator.
py += sdy;
}
px += sdx; // x is going from x0
to x1... we just increment as we
// move along
}
}
else
{
for( y=0; y<deltay; y++)
{
DrawPixel(px, py, colour);
x+=deltax;
if( x >= deltay )
{
x-= deltay;
px+=sdx;
}
py+=sdy;
}
}
}
// Simple entry point.
void
main()
{
unsigned int
c=0;
while(true)
{
c+= 10;
SimpleLine( 0,90, 300, 50, c ); //
AARRGGBB
SimpleLine( 0,150, 639, 150, 0x0000ff00 );
SimpleLine( 0,350, 300, 350, 0x000000ff );
SimpleLine( 0,450, 300, 450, 0x00ffffff );
SimpleLine( 0,479, 300, 479, 0x0000ffff );
SimpleLine( 50,100, 200, 400, 0x00ffffff );
DrawLine( 0,0, 639, 479, c );
DrawLine( 639,0,0 ,479 , c );
}
}
|