Cc65: TGI request: some sort of fill.

Created on 18 Oct 2018  路  6Comments  路  Source: cc65/cc65

I'm looking for algorithms to implement for a flood fill. I figured I would go ahead and file a ticket here, if anyone else has any ideas, in the mean-time. I just need a reasonably fast flood fill for PLATOTerm to implement the "paint" command.

-Thom

question

All 6 comments

Here's the first pass of one. This is a scanline algorithm that I am using in PLATOTerm, that might be beneficial to fold into TGI. I think it just needs some logic to dynamically allocate a stack based on screen size, what do you guys think?

 /**
  * tgi_fill -- scanline_fill
  * possible enhancements, add parameter c for old_color.
  * and allocate stack dynamically based on screen size?
  */
 void tgi_paint(unsigned short x, unsigned short y)
 {
   static unsigned short xStack[320];
   static unsigned char yStack[192];
   unsigned char stackentry = 1;
   unsigned char spanAbove, spanBelow;  

   unsigned char oldColor = tgi_getpixel(x,y); 

   if (oldColor == 1)
     return; 

   do
     {
       while (x > 0 && tgi_getpixel(x-1,y) == oldColor)
         --x; 

       spanAbove = spanBelow = false;
       while(tgi_getpixel(x,y) == oldColor)
         {
           tgi_setpixel(x,y); 

           if (y < (191))
             {
               unsigned char belowColor = tgi_getpixel(x, y+1);
               if (!spanBelow  && belowColor == oldColor)
                 {
                   xStack[stackentry]  = x;
                   yStack[stackentry]  = y+1;
                   ++stackentry;
                   spanBelow = true;
                 }
               else if (spanBelow && belowColor != oldColor)
                 spanBelow = false;
             }

           if (y > 0)
             {
               unsigned char aboveColor = tgi_getpixel(x, y-1);
               if (!spanAbove  && aboveColor == oldColor)
                 {
                   xStack[stackentry]  = x;
                   yStack[stackentry]  = y-1;
                   ++stackentry;
                   spanAbove = true;
                 }
               else if (spanAbove && aboveColor != oldColor)
                 spanAbove = false;
             } 

           ++x;
         }
       --stackentry;
       x = xStack[stackentry];
       y = yStack[stackentry];
     }
   while (stackentry);
 }

Since Github is mangling the comment, gist here:
https://gist.github.com/tschak909/b7f9248f230fabcc8435c8a2d80501ee

-Thom

since github is mangling the comment

I struggled with that before, until I found out that in order to quote a multi-line code snippet, one should use three backticks instead of one at the beginning and the end of the quote.

In case it isn't obvious, I'd like to say that "C" is nice to develop a workling algorithm, but the final version should then be translated into asm. In code like this every cycle counts...

I completely agree. I was trying to provide something to start with. :)

-Thom

Those "backticks" are called a "code fence". You can get syntax highlighting in many languages, by appending the language's name on the starting fence, for example:

```C
// Some C code
```

```python
# Some Python code
```

In the past my idea was to use tgi_bar() somehow and just fill the border :)

There seems to be no more momentum to come up with an answer to this question.

Was this page helpful?
0 / 5 - 0 ratings