added dpoly() rendering method

This commit is contained in:
Sylvain PILLOT 2024-02-29 21:34:42 +01:00
parent f51752eef3
commit ddecd84081
2 changed files with 48 additions and 83 deletions

View File

@ -15,6 +15,9 @@
extern "C" {
#endif
#include <stdint.h>
#include "attributes.h"
/* gint_call_arg_t: All types of arguments allowed in an indirect call
Because a function call cannot be easily pieced together, there are

View File

@ -1,105 +1,67 @@
#include <gint/defs/util.h>
#include <gint/display.h>
void dpoly(int *x, int *y, int nb_vertices, int fill_color, int border_color) {
void dpoly(int *polyX, int *polyY, int polyCorners, int fill_color, int border_color){
int i, ymin, ymax, xmin, xmax, *xmint, *xmaxt;
char *emptyt;
if (polyCorners<=2) return;
if (fill_color == C_NONE && border_color == C_NONE)
return;
if (fill_color != C_NONE) {
int i, ymin, ymax, xmin2, xmax2, *xmin, *xmax;
char *empty;
float *nodeX, pixelX, pixelY, swap ;
int i, j, nodes;
nodeX = malloc( polyCorners * sizeof(float));
// Loop through the rows of the image.
for (pixelY=0; pixelY<DHEIGHT; pixelY++) {
// Build a list of nodes.
nodes=0; j=polyCorners-1;
for (i=0; i<polyCorners; i++) {
if ((polyY[i]<(float) pixelY && polyY[j]>=(float) pixelY)
|| (polyY[j]<(float) pixelY && polyY[i]>=(float) pixelY)) {
nodeX[nodes++]=(int) (polyX[i]+(pixelY-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i]));
}
j=i;
}
if (nb_vertices < 3)
return;
ymin = ymax = y[0];
xmin2 = xmax2 = x[0];
for (i = 0; i < nb_vertices; i++) {
if (y[i] < ymin)
ymin = y[i];
if (y[i] > ymax)
ymax = y[i];
if (x[i] < xmin2)
xmin2 = x[i];
if (x[i] > xmax2)
xmax2 = x[i];
}
if (xmax2 < 0 || xmin2 > DWIDTH - 1 || ymax < 0 || ymin > DHEIGHT - 1)
return;
xmin = malloc((ymax - ymin + 1) * sizeof(int));
xmax = malloc((ymax - ymin + 1) * sizeof(int));
empty = malloc(ymax - ymin + 1);
if (xmin && xmax && empty) {
for (i = 0; i < ymax - ymin + 1; i++)
empty[i] = 1;
for (i = 0; i < nb_vertices; i++) {
int j, px, py, dx, dy, sx, sy, cumul;
px = x[i];
py = y[i];
dx = x[(i + 1) % nb_vertices] - px;
dy = y[(i + 1) % nb_vertices] - py;
sx = (dx > 0) ? 1 : -1;
sy = (dy > 0) ? 1 : -1;
dx = (dx > 0) ? dx : -dx;
dy = (dy > 0) ? dy : -dy;
if (empty[py - ymin])
xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0;
else
xmax[py - ymin] = px;
if (dx > dy) {
cumul = dx >> 1;
for (j = 1; j < dx; j++) {
px += sx;
cumul += dy;
if (cumul > dx)
cumul -= dx, py += sy;
if (empty[py - ymin])
xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0;
else
xmax[py - ymin] = px;
}
} else {
cumul = dy >> 1;
for (j = 1; j < dy; j++) {
py += sy;
cumul += dx;
if (cumul > dy)
cumul -= dy, px += sx;
if (empty[py - ymin])
xmax[py - ymin] = xmin[py - ymin] = px, empty[py - ymin] = 0;
else
xmax[py - ymin] = px;
}
// Sort the nodes, via a simple “Bubble” sort.
i=0;
while (i<nodes-1) {
if (nodeX[i]>nodeX[i+1]) {
swap=nodeX[i]; nodeX[i]=nodeX[i+1]; nodeX[i+1]=swap; if (i) i--;
}
else {
i++;
}
}
for (i = 0; i < ymax - ymin + 1; i++)
dline(xmin[i], ymin + i, xmax[i], ymin + i, fill_color);
}
// Fill the pixels between node pairs.
for (i=0; i<nodes; i+=2) {
if (nodeX[i ]>=DWIDTH) break;
if (nodeX[i+1]<= 0 ) break;
if (nodeX[i ]< 0 ) nodeX[i ]=0 ;
if (nodeX[i+1]> DWIDTH) nodeX[i+1]=DWIDTH;
//for (pixelX=nodeX[i]; pixelX<nodeX[i+1]; pixelX++) fillPixel(pixelX,pixelY);
dline(nodeX[i], pixelY, nodeX[i+1], pixelY, fill_color);
}
}
free(xmin);
free(xmax);
free(empty);
free(nodeX);
}
if (border_color != C_NONE) {
for (int i = 0; i < nb_vertices; i++) {
int px = x[i];
int py = y[i];
int px2 = x[(i + 1) % nb_vertices];
int py2 = y[(i + 1) % nb_vertices];
for (int i = 0; i < polyCorners; i++) {
int px = polyX[i];
int py = polyY[i];
int px2 = polyX[(i + 1) % polyCorners];
int py2 = polyY[(i + 1) % polyCorners];
dline(px, py, px2, py2, border_color);
}
}
return;
}