/* test1.c */
#include <stdio.h>
#include <allegro.h>
#include "../inc/ma2d.h"
PALETTE pal;
void
draw_rough (BITMAP * bmp, MA2D_SHAPE * shape,
float pos_x, float pos_y,
float scale_x, float scale_y,
float rot) {
MA2D_VBUFFER * vbuf; MA2D_VERTEX * verts;
MA2D_LBUFFER * lbuf; MA2D_LINESEG * segs;
int points[8];
int s;
vbuf = shape->vbuf;
lbuf = shape->lbuf;
verts = vbuf->verts;
segs = lbuf->segs;
for (s=0; s < lbuf->n_segs; s++) {
if ((segs[s].c1 == -1) || (segs[s].c2 == -1)) { //cubics ignored for now
line( bmp,
verts[segs[s].e1].x * scale_x + pos_x,
verts[segs[s].e1].y * scale_y + pos_y,
verts[segs[s].e2].x * scale_x + pos_x,
verts[segs[s].e2].y * scale_y + pos_y,
makecol(255, 128, 128)
);
} else {
points[0] = verts[segs[s].e1].x * scale_x + pos_x;
points[1] = verts[segs[s].e1].y * scale_y + pos_y;
points[2] = verts[segs[s].c1].x * scale_x + pos_x;
points[3] = verts[segs[s].c1].y * scale_y + pos_y;
points[4] = verts[segs[s].c2].x * scale_x + pos_x;
points[5] = verts[segs[s].c2].y * scale_y + pos_y;
points[6] = verts[segs[s].e2].x * scale_x + pos_x;
points[7] = verts[segs[s].e2].y * scale_y + pos_y;
spline( bmp,
points,
makecol(255, 128, 128)
);
}
}
}
/* scanlines:
* global array of MA2D_SCANLINE for testing
*/
MA2D_SCANLINE scanlines[480];
void init_scanlines() {
int a;
for (a=0; a<480; a++) {
scanlines[a].n_transits = 0;
scanlines[a].transits = NULL;
}
}
void free_all_transits() {
int a;
for (a=0; a<480; a++) {
if (scanlines[a].n_transits >0) free(scanlines[a].transits);
}
}
void render_all_scanlines() {
int a;
for (a=0; a<480; a++) {
int n;
MA2D_SCANTRANSITION * transits = scanlines[a].transits;
n = scanlines[a].n_transits;
if (n>0) {
int i = 0, x=0, r=0;
while (i < n) {
if (x < transits[i].x)
{
if (r) putpixel(screen, x, a, makecol(128, 64, 0));
x++;
} else {
r = transits[i].r;
i++;
}
}
}
}
}
void
add_transition( MA2D_SCANLINE * scanline, int x, int r ) {
MA2D_SCANTRANSITION * transits;
int n, a, i;
i = n = scanline->n_transits;
printf("i=%d ,n=%d\n", i, n);
if (n==0) {
transits = malloc(sizeof (MA2D_SCANTRANSITION));
i=0;
} else {
transits = realloc(scanline->transits, (n+1) *sizeof (MA2D_SCANTRANSITION));
a = 0; i = n;
while (a<n) {
if (transits[a].x > x) {
printf("a=%d ,n=%d\n", a, n);
memmove(&transits[a+1], &transits[a], sizeof(MA2D_SCANTRANSITION) * (n-a));
i = a; a = n;
}
a++;
}
}
transits[i].t = MA2D_TRANSIT_T_SOLID;
transits[i].x = x;
transits[i].r = r;
scanline->transits = transits;
scanline->n_transits = ++n;
}
void
change_transition_i(BITMAP * bmp, int x,int y, int d){
if (d==0) {
putpixel(bmp, x, y, makecol(255,0,0));
} else if (d<0) {
add_transition(&scanlines[y], x, 1);
} else {
add_transition(&scanlines[y], x, 0);
}
}
void
draw_rough_fill (BITMAP * bmp, MA2D_SHAPE * shape,
float pos_x, float pos_y,
float scale_x, float scale_y,
float rot) {
MA2D_VBUFFER * vbuf; MA2D_VERTEX * verts;
MA2D_LBUFFER * lbuf; MA2D_LINESEG * segs;
int points[8];
int s;
vbuf = shape->vbuf;
lbuf = shape->lbuf;
verts = vbuf->verts;
segs = lbuf->segs;
for (s=0; s < lbuf->n_segs; s++) {
if ((segs[s].c1 == -1) || (segs[s].c2 == -1)) { //cubics ignored for now
do_line( bmp,
verts[segs[s].e1].x * scale_x + pos_x,
verts[segs[s].e1].y * scale_y + pos_y,
verts[segs[s].e2].x * scale_x + pos_x,
verts[segs[s].e2].y * scale_y + pos_y,
verts[segs[s].e2].y - verts[segs[s].e1].y ,
&change_transition_i
);
} else {
int a,sx[20],sy[20];
points[0] = verts[segs[s].e1].x * scale_x + pos_x;
points[1] = verts[segs[s].e1].y * scale_y + pos_y;
points[2] = verts[segs[s].c1].x * scale_x + pos_x;
points[3] = verts[segs[s].c1].y * scale_y + pos_y;
points[4] = verts[segs[s].c2].x * scale_x + pos_x;
points[5] = verts[segs[s].c2].y * scale_y + pos_y;
points[6] = verts[segs[s].e2].x * scale_x + pos_x;
points[7] = verts[segs[s].e2].y * scale_y + pos_y;
calc_spline( points, 20, sx,sy);
for (a=0; a<19; a++) {
do_line( bmp, sx[a], sy[a], sx[a+1], sy[a+1],
sy[a+1] - sy[a],
&change_transition_i
);
}
}
}
}
int main()
{
int my_depth=16;
int button=13; //index in the_dlg
#define VV MA2D_VERTEX_F_USED | MA2D_VERTEX_F_VIS
#define VVC MA2D_VERTEX_F_USED | MA2D_VERTEX_F_VIS | MA2D_VERTEX_F_CTL
MA2D_VERTEX test1_verts[17] = {
/* x, y, flags */
{ 0.0, 0.0, VV },
{ 6.0, 0.0, VV },
{10.0, 0.0, VVC},
{11.0, 6.0, VVC},
{ 6.0, 7.0, VV },
{ 9.0,11.0, VV },
{ 7.0,11.0, VV },
{ 4.0, 7.0, VV },
{ 2.0, 7.0, VV },
{ 2.0,11.0, VV },
{ 0.0,11.0, VV },
{ 2.0, 2.0, VV },
{ 5.0, 2.0, VV },
{ 8.0, 2.0, VVC},
{ 8.0, 5.0, VVC},
{ 5.0, 5.0, VV },
{ 2.0, 5.0, VV }
};
MA2D_VBUFFER test1_vbuf = { 0, 17, test1_verts };
MA2D_LINESEG test1_linesegs[13] = {
{ 0, -1, -1, 1},
{ 1, 2, 3, 4},
{ 4, -1, -1, 5},
{ 5, -1, -1, 6},
{ 6, -1, -1, 7},
{ 7, -1, -1, 8},
{ 8, -1, -1, 9},
{ 9, -1, -1, 10},
{10, -1, -1, 0},
{11, -1, -1, 16},
{16, -1, -1, 15},
{15, 14, 13, 12},
{12, -1, -1, 11}
};
MA2D_LBUFFER test1_lbuf = {MA2D_LINESEG_F_USED, 13, test1_linesegs};
int test1_outerpath[9] = {0,1,2,3,4,5,6,7,8};
int test1_innerpath[4] = {9,10,11,12};
MA2D_PATH test1_paths[2] = {
{ 0, 0, 9, test1_outerpath},
{ 11,11, 4, test1_innerpath}
};
MA2D_SHAPE test1_shape = {
MAGESHAPE_F_USED,
&test1_vbuf,
&test1_lbuf,
2, test1_paths
};
allegro_init();
install_keyboard();
install_mouse();
install_timer();
set_color_depth(my_depth);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0) {
printf("Video Error: %s", allegro_error); //screen would still be in text mode on error
return 1;
} else {
clear_to_color(screen,makecol(0,0,0));
draw_rough(screen, &test1_shape, 200,100, 20,20, 0);
textout_centre_ex(screen, font, "Press any key to continue", SCREEN_W/2,
SCREEN_H - 20, makecol(255, 255, 0),makecol(0, 0, 255));
//Wait for a key to be pressed
while (!keypressed()) {}
readkey();
clear_to_color(screen,makecol(0,0,0));
init_scanlines();
draw_rough_fill(screen, &test1_shape, 200,100, 20,20, 0);
render_all_scanlines();
free_all_transits();
textout_centre_ex(screen, font, "Press any key to continue", SCREEN_W/2,
SCREEN_H - 20, makecol(255, 255, 0),makecol(0, 0, 255));
//Wait for a key to be pressed
while (!keypressed()) {}
}
return 0;
}
END_OF_MAIN()