Show Posts
Pages: 1 [2] 3 4 ... 9
16  Forum 2005-2010 (read only) / Interfacing / Re: Wireless communication between arduino and LCD on: June 29, 2010, 08:42:23 am
http://www.sparkfun.com/commerce/product_info.php?products_id=8946

There are receivers and transmitters, just get a couple of each, very cheap. What serial data you put on the transmitter is seen on the receiver, just make the link slow enough and do some sort of error correction or checksum.

Not sure if that's all you need for communicating with the LCD, or whether you need a full ardunio on the lcd end, but there you go.
17  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: June 29, 2010, 03:14:37 pm
I wonder if you couldn't squeeze more room for bitmaps by using some simple compression. What about recording the starting colour (1 or 0) in the top left and then just recording the lengths of lines of that colour, wrapping around until the image is finished?

So for a simple example a black screen of 128X128 would be (0, 16384) and (1, 8192, 8192) would be half white and half black. Obviously the more busy the image is the less it would compress but that might be doable in not a lot of code?
18  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: June 28, 2010, 04:13:50 pm
Hah! Now that you point out the diodes, I see them. I didn't put any diodes in mine, and it still works. Are there any safety issues with having no diodes, for tv or arduino?
19  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: June 28, 2010, 03:15:16 pm
I'm no expert but I do know it's nothing to do with the actual code of the line drawing. It's either a problem with your hardware - TV or arduino, or you've used the wrong code somehow by cobbling bits together that shouldn't be there.

I'd suggest downloading the library again, and starting fresh. Once you get the standard example running, then adjust to your H drawing and go from there.
20  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: June 10, 2010, 04:11:05 pm
I'm going to have to get sound up and running for myself soon, I'm loving the fast progress this library is making.

In the recent 5.9, was a thrill to see my additions to someone elses project, but when you get a minute can you change the name to crook, not cook? smiley

I'm going to start making pong in a bit, I just cant resist having a go smiley
21  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 30, 2010, 06:52:19 pm
Well, it's your library so set it up as you like, if you can get a very basic structure in place I can edit and add the stuff you don't have time for or are uninterested in working on like graphics functions.
22  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 30, 2010, 04:32:39 pm
I would also say ditch the first font and replace with the second one. As I was adding to it and seeing the bytes creep up I think space saving is a useful priority.

More menu interface items in an extension seems like a good idea. I'd only consider myself a novice C coder, so wouldn't know exactly how to go about doing this - are you thinking a graphics.cpp would hold it or something? If you've got the time and want graphics functions, could you make a simple version of it and include a simple example, like sp(1,1,1) in it? I can usually take a structure and build on it from there. Oh, and suggest what graphics functions should be in it. I can think of polygons, radio buttons, vector drawings based on a set of x,y coords, even rudimentary 3D maybe?

@gijs - I'm not sure how useful a flood fill function is - the screen isn't so big as it is to put information on and a processing heavy function like a flood fill might be overkill for this library. What's the smallest memory use algorithm for flood filling out there? I can only see one working using stacks which might get big for some screens. I'd be interested to see one working though, see how fast it works.
23  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 30, 2010, 02:37:13 pm
Draw a progressbar:

Code:

/* draw a progressbar
 * x1,y1 to x2,y2
 * with color 1 = white, 0=black
 * with 1 = solid, 2 or more = sectioned
 * percentage value for bar, 0 - 100
 * safe draw or not 1 = safe
 */
void TVout::draw_progressbar(unsigned char x0, unsigned char y0,
                             unsigned char x1, unsigned char y1, char c, char d, char e, char h)
{
    if (h == 0)
    {
        if (x0 >= g_hres*8 || y0 >= g_vres ||
                x0 + x1 >= g_hres*8 || y0 + y1 >= g_vres || e > 100)
            return;
    }
    unsigned char f,x,y,progress;

    if (c > 1)
    {
        c=1;
    }

    if (c == 1)
    {
        f=0;
    }

    if (c == 0)
    {
        f=1;
    }

    //  draw the main box


    for (y = y0; y < y0+y1; y++)
    {
        // First do the left hand line
        sp_safe(x0,y,c,h);

        // Now do the right hand line
        sp_safe(x0+x1,y,c,h);

    }

    for (x = x0+1; x < x0 + x1; x++)
    {
        // First do the top line
        sp_safe(x,y0,c,h);

        // Now do the bottom line
        sp_safe(x,y0+y1-1,c,h);

    }





// now draw an inverted box within


    for (y = y0+1; y < y0+y1-1; y++)
    {
        // First do the left hand line
        sp_safe(x0+1,y,f,h);

        // Now do the right hand line
        sp_safe(x0+x1-2,y,f,h);

    }

    for (x = x0+2; x < x0 + x1-2; x++)
    {
        // First do the top line
        sp_safe(x,y0+1,f,h);

        // Now do the bottom line
        sp_safe(x,y0+y1-2,f,h);

    }


// now draw the progressbar within
    progress = (((x1-3)*e)/100)+1;

    if (e == 100)
    {
        progress = x1-2;
    }


    if (e > 0)
    {
        for (x = x0+2; x <= x0+x1-2; x++)
        {
            for (y = y0+2; y < y0+y1-2; y++)
            {
                if (x < x0+2+progress)
                {
                    sp_safe(x,y,c,h);
                }
                else
                {
                    if (e<100)
                    {
                        sp_safe(x,y,f,h);
                    }
                }
            }
        }
    }

}





Draw a circle:

Code:

/* draw a circle
 * x0,y0 around radius
 * with color 1 = white, 0=black, 2=invert
 * with fill 0 = black fill, 1 = white fill, 2 = invert fill, 3 = no fill
 * safe draw or not 1 = safe
 */
void TVout::draw_circle(unsigned char x0, unsigned char y0,
                        unsigned char radius, char c, char d, char h)
{


    if (h == 0)
    {
        if (x0+radius >= g_hres*8 || y0+radius >= g_vres || radius >= x0 || radius >= y0)
            return;
    }
    int f = 1 - radius;
    int dx = 1;
    int dy = -2 * radius;
    int x = 0;
    int y = radius;
    int sty1 = 0;

// draw circle

    f = 1 - radius;
    dx = 1;
    dy = -2 * radius;
    x = 0;
    y = radius;

    if (d<3)
    {
// if filled, use endpoints to draw lines to center x
        for (sty1=0; sty1<radius; sty1++)
        {
            sp_safe(x0,y0+sty1,d,h);
            sp_safe(x0,y0-sty1,d,h);
            sp_safe(x0+sty1,y0,d,h);
            sp_safe(x0-sty1,y0,d,h);
        }
    }
    sp_safe(x0, y0 + radius,c,h);
    sp_safe(x0, y0 - radius,c,h);
    sp_safe(x0 + radius, y0,c,h);
    sp_safe(x0 - radius, y0,c,h);
    while(x < y)
    {
        if(f >= 0)
        {
            y--;
            dy += 2;
            f += dy;
        }
        x++;
        dx += 2;
        f += dx;
        if (d<3)
        {
// if filled, use endpoints to draw lines to center x
            for (sty1=0; sty1<y; sty1++)
            {
                sp_safe(x0+x,y0+sty1,d,h);
                sp_safe(x0-x,y0+sty1,d,h);
                sp_safe(x0+x,y0-sty1,d,h);
                sp_safe(x0-x,y0-sty1,d,h);
                sp_safe(x0+sty1,y0+x,d,h);
                sp_safe(x0-sty1,y0+x,d,h);
                sp_safe(x0+sty1,y0-x,d,h);
                sp_safe(x0-sty1,y0-x,d,h);
            }
        }
        sp_safe(x0 + x, y0 + y,c,h);
        sp_safe(x0 - x, y0 + y,c,h);
        sp_safe(x0 + x, y0 - y,c,h);
        sp_safe(x0 - x, y0 - y,c,h);
        sp_safe(x0 + y, y0 + x,c,h);
        sp_safe(x0 - y, y0 + x,c,h);
        sp_safe(x0 + y, y0 - x,c,h);
        sp_safe(x0 - y, y0 - x,c,h);
    }
    if (d<3)  // redraw circle
    {
// draw circle
        f = 1 - radius;
        dx = 1;
        dy = -2 * radius;
        x = 0;
        y = radius;

        sp_safe(x0, y0 + radius,c,h);
        sp_safe(x0, y0 - radius,c,h);
        sp_safe(x0 + radius, y0,c,h);
        sp_safe(x0 - radius, y0,c,h);

        while(x < y)
        {

            if(f >= 0)
            {
                y--;
                dy += 2;
                f += dy;
            }
            x++;
            dx += 2;
            f += dx;
            sp_safe(x0 + x, y0 + y,c,h);
            sp_safe(x0 - x, y0 + y,c,h);
            sp_safe(x0 + x, y0 - y,c,h);
            sp_safe(x0 - x, y0 - y,c,h);
            sp_safe(x0 + y, y0 + x,c,h);
            sp_safe(x0 - y, y0 + x,c,h);
            sp_safe(x0 + y, y0 - x,c,h);
            sp_safe(x0 - y, y0 - x,c,h);
        }


    }



}


// end of edit by Andy Crook 2010






And some code to go in the example sketch:

Code:

  TV.delay_frame(50);
  TV.clear_screen();
  TV.print_str(0,0,"draw progressbars");
  TV.delay_frame(50);
  for (char pr = 0; pr < 101 ; pr++)
  {
    TV.print_str(0,0,"draw progressbars");
    // usage is TV.draw_progressbar(x start, y start, x width, y width, colour 1 = white 2 = black, sectioned bar, percentage for bar 0 - 100, safe draw mode 1 = on)
    TV.draw_progressbar(5,10,100,10,1,0,pr,1);
    TV.draw_progressbar(40,30,40,15,1,0,100,0);
    TV.draw_progressbar(20,50,60,15,1,0,100-pr,1);
    TV.draw_progressbar(40,70,40,8,0,0,50,1);
    TV.draw_progressbar(40,80,40,5,1,0,0,1);
    delay(20);
  }
  
  
  TV.delay_frame(50);
  TV.clear_screen();
  TV.print_str(0,0,"draw some boxes");
  TV.delay_frame(50);
  for (char f = 0; f < 8; f++)
  {
  TV.clear_screen();
  TV.print_str(0,0,"draw some boxes");
  // usage is TV.draw_box(x start, y start, x width, y width, colour 1 = white 2 = black, fill 1 = white 2 = black, 3 = no fill, radius > 0 = rounded rectangle, safe draw 1 = on)
  TV.draw_box(15,20,20,15,1,3,f,1);
  TV.draw_box(25,30,15,25,1,1,f,1);
  TV.draw_box(100+(f*2),60+f,35,35,1,1,f,1);
  delay(50);
  }
  
  TV.delay_frame(50);
  TV.clear_screen();
  TV.print_str(0,0,"draw some circles");
  TV.delay_frame(50);
  for (char f = 0; f < 20; f++)
  {
  TV.clear_screen();
  TV.print_str(0,0,"draw some circles");
  // usage is TV.draw_circle(x start, y start, radius, colour 1 = white 2 = black, fill 1 = white 0 = black 3 = no fill, safe draw 1 = on)
  TV.draw_circle(35,25,5+f,1,1,1);
  TV.draw_circle(15,5,5+f,1,3,1);
  TV.draw_circle(115,75,15+f,1,1,1);
  delay(50);
  }




And of course the inline sp_safe():

Code:
// Inline version of set_pixel that does perform a bounds check

static void inline sp_safe(unsigned char x, unsigned char y, char c, char d) {
      if (d==0) // if d is zero then just do without any bounds check
      {
            if (c==1)
                  g_screen[(x/8) + (y*g_hres)] |= 0x80 >> (x&7);
            else if (c==0)
                  g_screen[(x/8) + (y*g_hres)] &= ~0x80 >> (x&7);
            else
                  g_screen[(x/8) + (y*g_hres)] ^= 0x80 >> (x&7);
      }
      else   // d is 1, so check every pixel for boundary
      {
            if (x < g_hres*8 && y < g_vres)   // if x is in bounds AND if y is in bounds then proceed with pixel write
            {
                  if (c==1)
                        g_screen[(x/8) + (y*g_hres)] |= 0x80 >> (x&7);
                  else if (c==0)
                        g_screen[(x/8) + (y*g_hres)] &= ~0x80 >> (x&7);
                  else
                        g_screen[(x/8) + (y*g_hres)] ^= 0x80 >> (x&7);
            }
      }
}





That's about it so far. Add it or not as you wish. I don't think there's much else to do on a graphical rendering front for it, as the more that the library gets added to, the smaller the space becomes for programming with. There may be some refactoring I can do to cut down the size a little, but not by much I reckon. I think i'll move on from this for a bit into trying to make a pong or asteroids clone maybe, now the library has sound emerging for it.

Any mistakes that anyone spots let me know and I'll get on it, unless you want to fix it yourself.
24  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 30, 2010, 02:35:15 pm
Ok, here are my additions:

First, for tvout.h:

Code:
void draw_circle(unsigned char x0, unsigned char y0, unsigned char radius, char c, char d, char h);
void draw_progressbar(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, char c, char d, char e, char h);
void draw_box(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, char c, char d, char e, char f);


and later in tvout.h:

Code:
static void inline sp_safe(unsigned char x, unsigned char y, char c, char d);

I'll explain this one - sp_safe allows you to render with no boundary check, or for an overhead, check every pixel. It's useful to b e able to set this for example in drawing circles. You can check if the circle is within bounds and draw or not, but what if you wanted to draw slightly off screen? You need a per pixel bounds check, but one that you can choose as the program runs rather than two separate sp() and set_pixel). This allows you to draw safely should you be bothered about going out of bounds, or draw off screen should you wish to display half a circle or rounded rectangle etc.

I wouldn't want to replace sp() with this completely as there is a slight speed loss by calling sp_safe() even when it does no bounds check (as it checks the flag for whether it should do a bounds check, so the if statement will take time to execute), so if someone wanted the fastest render they could always use sp().


Now for drawing boxes:

Everything from here on goes in TVout.cpp


Code:
// Beginning of edit by Andy Crook 2010


/* draw a box
 * x1,y1 to x2,y2
 * with color 1 = white, 0=black, 2=invert
 * with fill 0 = black fill, 1 = white fill, 2 = invert fill, 3 = no fill
 * with radius for rounded box
 * safe draw or not 1 = safe
 */
void TVout::draw_box(unsigned char x0, unsigned char y0,
                     unsigned char x1, unsigned char y1, char c, char d, char e, char f)
{
    /* x0,y0 = start position from top left    x1,y1 = width and height
    *  c is box line colour, d is fill colour, e is radius for rounded rectangles (0 radius = straight box)
    *  if the radius is bigger than x0 or y0 then dont render, if the position + length + radius is bigger than the resolution then dont render
    */
    if (f == 0 ) // if safe sp is off
    {
        if (x0 >= g_hres*8 || y0 >= g_vres || x0 + x1 + e >= g_hres*8 || y0 + y1 + e >= g_vres)
            return;
    }

    unsigned char x,y;

    if (e == 0) // ordinary box
    {
        if (d < 3) // fill the box with the appropriate colour if the fill is 0, 1 or 2. if 3 or higher, no fill
        {
            for (y = y0+1; y < y0+y1-1; y++)
            {
                for (x = x0+1; x < x0+x1; x++)
                {
                    sp_safe(x,y,d,f);
                }
            }
        }
        // Now draw the box
        for (y = y0; y < y0+y1; y++)
        {
            sp_safe(x0,y,c,f);   // left hand line
            sp_safe(x0+x1,y,c,f); // right hand line
        }
        for (x = x0+1; x < x0 + x1; x++)
        {
            sp_safe(x,y0,c,f); // top line
            sp_safe(x,y0+y1-1,c,f); // bottom line
        }
    }
    if (e>0)    // rounded rectangle! Split a circle into the 4 corners and draw a box
    {
// subtract the radius of the corners from the straight box we want to draw

        if (e+e > y1) // we cant draw a rounded box with a total radius bigger than the dimensions
            return;
        if (e+e > x1)
            return;

        x0=x0+e;
        y0=y0+e;
        y1=y1-e-e;
        x1=x1-e-e;





// draw the box


        if (d < 3) // fill the box with the appropriate colour if the fill is 0, 1 or 2. if 3 or higher, no fill
        {
            for (y = y0-e; y < y0+y1+e; y++)
            {
                for (x = x0; x < x0+x1+1; x++)
                {
                    sp_safe(x,y,d,f);
                }
            }
            for (y = y0; y < y0+y1+1; y++)
            {
                for (x = x0-e; x < x0+1; x++)
                {
                    sp_safe(x,y,d,f);
                }
                for (x = x0+x1; x < x0+x1+e+1; x++)
                {
                    sp_safe(x,y,d,f);
                }
            }
        }


        for (y = y0; y < y0+y1; y++)
        {
            // First do the left hand line
            sp_safe(x0-e,y+1,c,f);

            // Now do the right hand line
            sp_safe(x0+x1+e,y+1,c,f);

        }

        for (x = x0+1; x < x0 + x1+1; x++)
        {
            // First do the top line
            sp_safe(x,y0-e,c,f);

            // Now do the bottom line
            sp_safe(x,y0+y1+e,c,f);

        }




        int radius = e;
        int ff = 1 - radius;
        int dx = 1;
        int dy = -2 * radius;
        int x = 0;
        int y = radius;
        int sty1 = 0;

// draw  quarter circles at each corner of the box

        ff = 1 - radius;
        dx = 1;
        dy = -2 * radius;
        x = 0;
        y = radius;

        if (d<3)
        {
// if filled, use endpoints to draw lines to center x
            for (sty1=0; sty1<radius; sty1++)
            {
                sp_safe(x0,y0+sty1+y1,d,f);
                sp_safe(x0,y0-sty1,d,f);
                sp_safe(x0+sty1+x1,y0,d,f);
                sp_safe(x0-sty1,y0,d,f);
            }
        }
        sp_safe(x0, y0 + radius+y1,c,f);
        sp_safe(x0, y0 - radius,c,f);
        sp_safe(x0 + radius+x1, y0,c,f);
        sp_safe(x0 - radius, y0,c,f);
        while(x < y)
        {
            if(ff >= 0)
            {
                y--;
                dy += 2;
                ff += dy;
            }
            x++;
            dx += 2;
            ff += dx;
            if (d<3)
            {
// if filled, use endpoints to draw lines to center x
                for (sty1=0; sty1<y; sty1++)
                {
                    sp_safe(x0+x+x1,y0+sty1+y1,d,f);
                    sp_safe(x0-x,y0+sty1+y1,d,f);
                    sp_safe(x0+x+x1,y0-sty1,d,f);
                    sp_safe(x0-x,y0-sty1,d,f);
                    sp_safe(x0+sty1+x1,y0+x+y1,d,f);
                    sp_safe(x0-sty1,y0+x+y1,d,f);
                    sp_safe(x0+sty1+x1,y0-x,d,f);
                    sp_safe(x0-sty1,y0-x,d,f);
                }
            }
            sp_safe(x0 + x+x1, y0 + y+y1,c,f);
            sp_safe(x0 - x, y0 + y+y1,c,f);
            sp_safe(x0 + x+x1, y0 - y,c,f);
            sp_safe(x0 - x, y0 - y,c,f);
            sp_safe(x0 + y+x1, y0 + x+y1,c,f);
            sp_safe(x0 - y, y0 + x+y1,c,f);
            sp_safe(x0 + y+x1, y0 - x,c,f);
            sp_safe(x0 - y, y0 - x,c,f);
        }
        if (d<3)  // redraw circle
        {
// draw circle
            ff = 1 - radius;
            dx = 1;
            dy = -2 * radius;
            x = 0;
            y = radius;

            sp_safe(x0, y0 + radius+y1,c,f);
            sp_safe(x0, y0 - radius,c,f);
            sp_safe(x0 + radius+x1, y0,c,f);
            sp_safe(x0 - radius, y0,c,f);

            while(x < y)
            {

                if(ff >= 0)
                {
                    y--;
                    dy += 2;
                    ff += dy;
                }
                x++;
                dx += 2;
                ff += dx;
                sp_safe(x0 + x+x1, y0 + y+y1,c,f);
                sp_safe(x0 - x, y0 + y+y1,c,f);
                sp_safe(x0 + x+x1, y0 - y,c,f);
                sp_safe(x0 - x, y0 - y,c,f);
                sp_safe(x0 + y+x1, y0 + x+y1,c,f);
                sp_safe(x0 - y, y0 + x+y1,c,f);
                sp_safe(x0 + y+x1, y0 - x,c,f);
                sp_safe(x0 - y, y0 - x,c,f);
            }


        }





    }



}


to be continued
25  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 27, 2010, 07:39:58 am
@gijs, like that line verison, all ints no floats. Clever stuff.
26  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 27, 2010, 06:34:50 am
What does partially mean - it draws some lines or not others? If it's similar behaviour to yours then there must be a problem. Actually, when I was doing circles I tried lots of versions  of the midpoint circle algorithm that didn't work as expected - then I got it to work and honestly I don't know what was wrong. One just did diamonds and the other did circles.

I think it's a problem with the maths somewhere, maybe for arduino code you need to specify maths in brackets to get operations to work in the right order, I dunno.

I'd do some debugging if I were you, print characters on screen for x1,y1, that sort of thing, so see where it's going wrong. I think it'll be either abs() or the floating point maths or something like that, or:

2 * dy - dx being interpreted as 2* (dy-dx) or (2*dy) - dx or something. Go through and be explicit with the maths maybe?
27  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 27, 2010, 03:45:27 am
I can't test it where I am but it looks ok. What does it do at present? some lines but not all, or no lines at all?

You could try this as a replacement to test.


Code:
{
     /* bresenham line code
       *
       *  x1,y1 is start, x2,y2 is end
       *
       */
      int dx, dy, p, end;
      float x1, x2, y1, y2, x, y;
    
      dx = abs(x1 - x2);
      dy = abs(y1 - y2);
      p = 2 * dy - dx;
      if(x1 > x2)
      {
            x = x2;
            y = y2;
            end = x1;
      }
      else
      {
            x = x1;
            y = y1;
            end = x2;
      }
      sp(x, y, c);
      while(x < end)
      {
            x = x + 1;
            if(p < 0)
            {
                  p = p + 2 * dy;
            }
            else
            {
                  y = y + 1;
                  p = p + 2 * (dy - dx);
            }
            sp(x, y, c);
}
28  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 27, 2010, 03:32:11 am
Doesn't TVout::draw_line already do this? And I haven't gone through your code for errors but something that struck me was that in the beginning you check for out of bounds and return, then during the code you check for out of bounds for every pixel write you perform, slowing it down.

I don't know which version of TVOut your editing but the latest one has changed the _RESOLUTION_VERTICAL (and horizontal), so you might want to change them in preparation for blending into the most up to date version as well.
29  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 25, 2010, 09:33:16 am
What does this fade do? Am I reading it right, it takes the pixels on screen and fades them out? Because I thought this was 2 bit mono, just on and off, no greyscale?


Here is a work in progress for boxes and progress bars. This is in no way intended to be placed in there right now, just if someone wants to mess around with it they can. It's not tested since I've been playing with it today - coding without compiling / testing, so don't take it as finished code.

Code:
/* draw a box
 * x1,y1 to x2,y2
 * with color 1 = white, 0=black, 2=invert
 * with fill 0 = black fill, 1 = white fill, 2 = invert fill, 3 = no fill
 * with radius for rounded box
 */
void TVout::draw_box(unsigned char x0, unsigned char y0,
                     unsigned char x1, unsigned char y1, char c, char d, char e)
{
      /* x0,y0 = start position from top left    x1,y1 = width and height
      *  c is box line colour, d is fill colour, e is radius for rounded rectangles (0 radius = straight box)
    *  if the radius is bigger than x0 or y0 then dont render, if the position + length + radius is bigger than the resolution then dont render
    *
    *  It's possible to set a 'render off screen' flag here as well, and use set_pixel(x,y,d) for outside the boundaries and only plot
    *  if it's within the screen. Slower but could allow for more flexibility if desired for partial rounded rectangles. Too many flags for a box draw?
    *
      *  reminder to do some speed tests with set_pixel instead - can we just get away with using that instead?
      */
    if (e >= x0 g_hres || e >= y0 g_vres ||
            x0 + x1 +e >= g_hres || y0 + y1+e >= g_vres)
        return;
    unsigned char x,y;

    if (e == 0) // ordinary box
    {
        if (d < 3) // fill the box with the appropriate colour if the fill is 0, 1 or 2. if 3 or higher, no fill
        {
            for (y = y0+1; y < y0+y1-1; y++)
            {
                for (x = x0+1; x < x0+x1; x++)
                {
                    sp(x,y,d);
                }
            }
        }
        // Now draw the box
        for (y = y0; y < y0+y1; y++)
        {
            sp(x0,y,c);   // left hand line
            sp(x0+x1,y,c); // right hand line
        }
        for (x = x0+1; x < x0 + x1; x++)
        {
            sp(x,y0,c); // top line
            sp(x,y0+y1-1,c); // bottom line
        }
    }
    if (e>0)    // rounded rectangle! Split a circle into the 4 corners and draw a box
    {
// subtract the radius of the corners from the straight box we want to draw
        x0=x0+e;
        y0=y0+e;
        y1=y1-e-e;
        x1=x1-e-e;


// draw the box
        for (y = y0; y < y0+y1; y++)
        {
            // First do the left hand line
            sp(x0-e,y+1,c);

            // Now do the right hand line
            sp(x0+x1+e,y+1,c);

        }

        for (x = x0+1; x < x0 + x1+1; x++)
        {
            // First do the top line
            sp(x,y0-e,c);

            // Now do the bottom line
            sp(x,y0+y1+e,c);

        }



        int radius = e;
        int f = 1 - radius;
        int dx = 1;
        int dy = -2 * radius;
        int x = 0;
        int y = radius;
        int sty1 = 0;

// draw  quarter circles at each corner of the box

        f = 1 - radius;
        dx = 1;
        dy = -2 * radius;
        x = 0;
        y = radius;

        if (d<3)
        {
// if filled, use endpoints to draw lines to center x
            for (sty1=0; sty1<radius; sty1++)
            {
                sp(x0,y0+sty1+y1,d);
                sp(x0,y0-sty1,d);
                sp(x0+sty1+x1,y0,d);
                sp(x0-sty1,y0,d);
            }
        }
        sp(x0, y0 + radius+y1,c);
        sp(x0, y0 - radius,c);
        sp(x0 + radius+x1, y0,c);
        sp(x0 - radius, y0,c);
        while(x < y)
        {
            if(f >= 0)
            {
                y--;
                dy += 2;
                f += dy;
            }
            x++;
            dx += 2;
            f += dx;
            if (d<3)
            {
// if filled, use endpoints to draw lines to center x
                for (sty1=0; sty1<y; sty1++)
                {
                    sp(x0+x+x1,y0+sty1+y1,d);
                    sp(x0-x,y0+sty1+y1,d);
                    sp(x0+x+x1,y0-sty1,d);
                    sp(x0-x,y0-sty1,d);
                    sp(x0+sty1+x1,y0+x+y1,d);
                    sp(x0-sty1,y0+x+y1,d);
                    sp(x0+sty1+x1,y0-x,d);
                    sp(x0-sty1,y0-x,d);
                }
            }
            sp(x0 + x+x1, y0 + y+y1,c);
            sp(x0 - x, y0 + y+y1,c);
            sp(x0 + x+x1, y0 - y,c);
            sp(x0 - x, y0 - y,c);
            sp(x0 + y+x1, y0 + x+y1,c);
            sp(x0 - y, y0 + x+y1,c);
            sp(x0 + y+x1, y0 - x,c);
            sp(x0 - y, y0 - x,c);
        }
        if (d<3)  // redraw circle
        {
// draw circle
            f = 1 - radius;
            dx = 1;
            dy = -2 * radius;
            x = 0;
            y = radius;

            sp(x0, y0 + radius+y1,c);
            sp(x0, y0 - radius,c);
            sp(x0 + radius+x1, y0,c);
            sp(x0 - radius, y0,c);

            while(x < y)
            {

                if(f >= 0)
                {
                    y--;
                    dy += 2;
                    f += dy;
                }
                x++;
                dx += 2;
                f += dx;
                sp(x0 + x+x1, y0 + y+y1,c);
                sp(x0 - x, y0 + y+y1,c);
                sp(x0 + x+x1, y0 - y,c);
                sp(x0 - x, y0 - y,c);
                sp(x0 + y+x1, y0 + x+y1,c);
                sp(x0 - y, y0 + x+y1,c);
                sp(x0 + y+x1, y0 - x,c);
                sp(x0 - y, y0 - x,c);
            }


        }





    }



}



/* draw a progressbar
 * x1,y1 to x2,y2
 * with color 1 = white, 0=black
 * with 1 = solid, 2 or more = sectioned
 * percentage value for bar, 0 - 100
 */
void TVout::draw_progressbar(unsigned char x0, unsigned char y0,
                             unsigned char x1, unsigned char y1, char c, char d, char e)
{

    if (x0 >= g_hres || y0 >= g_vres ||
            x0 + x1 >= g_hres || y0 + y1 >= g_vres || e > 100)
        return;
    unsigned char f,x,y,progress;

    if (c > 1)
    {
        c=1;
    }

    if (c == 1)
    {
        f=0;
    }

    if (c == 0)
    {
        f=1;
    }

    //  draw the main box


    for (y = y0; y < y0+y1; y++)
    {
        // First do the left hand line
        sp(x0,y,c);

        // Now do the right hand line
        sp(x0+x1,y,c);

    }

    for (x = x0+1; x < x0 + x1; x++)
    {
        // First do the top line
        sp(x,y0,c);

        // Now do the bottom line
        sp(x,y0+y1-1,c);

    }







// now draw an inverted box within


    for (y = y0+1; y < y0+y1-1; y++)
    {
        // First do the left hand line
        sp(x0+1,y,f);

        // Now do the right hand line
        sp(x0+x1-2,y,f);

    }

    for (x = x0+2; x < x0 + x1-2; x++)
    {
        // First do the top line
        sp(x,y0+1,f);

        // Now do the bottom line
        sp(x,y0+y1-2,f);

    }


// now draw the progressbar within


    progress = ((x1-4)*e)/100;

    for (x = x0+progress+2+c; x < x0+x1-2; x=x+1)

    {
        for (y = y0+2; y < y0+y1-2; y++)
        {

            sp(x,y,f);


        }
    }

    for (x = x0+2; x < x0+progress+2+c; x=x+d)

    {
        for (y = y0+2; y < y0+y1-2; y++)
        {


            sp(x,y,c);

        }
    }





}
30  Forum 2005-2010 (read only) / Exhibition / Re: NTSC video out library on: May 25, 2010, 04:55:50 am
If I'm reading the library right, replacing set_pixel with sp will give a small boost in speed as it wont check for boundary conditions, and if all you're doing is a scroll then I think this would be ok.

For my shapes stuff I think I'll add a sp_safe() that will check for boundary conditions or not depending on a set flag, so sp_safe(X,Y,C,D)
Pages: 1 [2] 3 4 ... 9