added readme and license
[gba_blender] / src / polyfill.c
index 11e70fd..18294f5 100644 (file)
@@ -41,6 +41,7 @@ void polyfill_flat(struct pvertex *varr, int vnum, unsigned char col)
        int32_t x, y0, y1, dx, dy, slope, fx, fy;
        short *tab, start, len;
        unsigned char *fbptr;
+       uint16_t *pptr, pcol = ((uint16_t)col << 8) | (uint16_t)col;
 
        vlast = varr + vnum - 1;
        top = fbheight;
@@ -92,7 +93,20 @@ void polyfill_flat(struct pvertex *varr, int vnum, unsigned char col)
                len = scantab[1][i] - start;
 
                if(len > 0) {
-                       memset(fbptr + start, col, len);
+                       if(start & 1) {
+                               pptr = (uint16_t*)(fbptr + (start & 0xfffe));
+                               *pptr = (*pptr & 0xff) | ((uint16_t)col << 8);
+                               len--;
+                               start++;
+                       }
+                       pptr = (uint16_t*)(fbptr + start);
+                       while(len > 1) {
+                               *pptr++ = pcol;
+                               len -= 2;
+                       }
+                       if(len) {
+                               *pptr = (*pptr & 0xff00) | col;
+                       }
                }
                fbptr += fbwidth;
        }
@@ -194,9 +208,36 @@ int clip_line(int *x0, int *y0, int *x1, int *y1, int xmin, int ymin, int xmax,
        return 1;
 }
 
+#ifdef ALT_LCLIP
+#define PUTPIXEL(ptr) \
+       do { \
+               if(x0 >= 0 && x0 < fbwidth && y0 >= 0 && y0 < fbheight) { \
+                       uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
+                       if((uint32_t)ptr & 1) { \
+                               *pptr = (*pptr & 0xff) | (color << 8); \
+                       } else { \
+                               *pptr = (*pptr & 0xff00) | color; \
+                       } \
+               } \
+       } while(0)
+#else  /* !ALT_LCLIP */
+#define PUTPIXEL(ptr) \
+       do { \
+               uint16_t *pptr = (uint16_t*)((uint32_t)ptr & 0xfffffffe); \
+               if((uint32_t)ptr & 1) { \
+                       *pptr = (*pptr & 0xff) | (color << 8); \
+               } else { \
+                       *pptr = (*pptr & 0xff00) | color; \
+               } \
+       } while(0)
+#endif
+
 void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
 {
        int i, dx, dy, x_inc, y_inc, error;
+#ifdef ALT_LCLIP
+       int y0inc;
+#endif
        unsigned char *fbptr = fb;
 
        fbptr += y0 * fbwidth + x0;
@@ -212,32 +253,50 @@ void draw_line(int x0, int y0, int x1, int y1, unsigned short color)
        }
        if(dy >= 0) {
                y_inc = fbwidth;
+#ifdef ALT_LCLIP
+               y0inc = 1;
+#endif
        } else {
                y_inc = -fbwidth;
+#ifdef ALT_LCLIP
+               y0inc = -1;
+#endif
                dy = -dy;
        }
 
        if(dx > dy) {
                error = dy * 2 - dx;
                for(i=0; i<=dx; i++) {
-                       *fbptr = color;
+                       PUTPIXEL(fbptr);
                        if(error >= 0) {
                                error -= dx * 2;
                                fbptr += y_inc;
+#ifdef ALT_LCLIP
+                               y0 += y0inc;
+#endif
                        }
                        error += dy * 2;
                        fbptr += x_inc;
+#ifdef ALT_LCLIP
+                       x0 += x_inc;
+#endif
                }
        } else {
                error = dx * 2 - dy;
                for(i=0; i<=dy; i++) {
-                       *fbptr = color;
+                       PUTPIXEL(fbptr);
                        if(error >= 0) {
                                error -= dy * 2;
                                fbptr += x_inc;
+#ifdef ALT_LCLIP
+                               x0 += x_inc;
+#endif
                        }
                        error += dx * 2;
                        fbptr += y_inc;
+#ifdef ALT_LCLIP
+                       y0 += y0inc;
+#endif
                }
        }
 }