【正文】
eline(z1,z2)。 32 } } } // Darw a hollow box with side walls. void box_walls(int x1, int y1, int z1, int x2, int y2, int z2) { int iy。 int ix。 argorder(x1, x2, amp。x1, amp。x2)。 argorder(y1, y2, amp。y1, amp。y2)。 argorder(z1, z2, amp。z1, amp。z2)。 for (ix=x1。ix=x2。ix++) { for (iy=y1。iy=y2。iy++) { if (iy == y1 || iy == y2 || ix == x1 || ix == x2) { cube[ix][iy] = byteline(z1,z2)。 } else { cube[ix][iy] |= ((0x01 z1) | (0x01 z2))。 } } } } // Draw a wireframe box. This only draws the corners and edges, // no walls. void box_wireframe(int x1, int y1, int z1, int x2, int y2, int z2) { int iy。 int ix。 argorder(x1, x2, amp。x1, amp。x2)。 argorder(y1, y2, amp。y1, amp。y2)。 argorder(z1, z2, amp。z1, amp。z2)。 // Lines along Z axis cube[x1][y1] = byteline(z1,z2)。 cube[x1][y2] = byteline(z1,z2)。 cube[x2][y1] = byteline(z1,z2)。 cube[x2][y2] = byteline(z1,z2)。 // Lines along Y axis for (iy=y1。iy=y2。iy++) { 33 setvoxel(x1,iy,z1)。 setvoxel(x1,iy,z2)。 setvoxel(x2,iy,z1)。 setvoxel(x2,iy,z2)。 } // Lines along X axis for (ix=x1。ix=x2。ix++) { setvoxel(ix,y1,z1)。 setvoxel(ix,y2,z1)。 setvoxel(ix,y1,z2)。 setvoxel(ix,y2,z2)。 } } // Draw a line between any coordinates in 3d space. // Uses integer values for input, so dont expect smooth animations. void line(int x1, int y1, int z1, int x2, int y2, int z2) { float xy。 // how many voxels do we move on the y axis for each step on the x axis float xz。 // how many voxels do we move on the y axis for each step on the x axis unsigned char x,y,z。 unsigned char lasty,lastz。 // We always want to draw the line from x=0 to x=7. // If x1 is bigget than x2, we need to flip all the values. if (x1x2) { int tmp。 tmp = x2。 x2 = x1。 x1 = tmp。 tmp = y2。 y2 = y1。 y1 = tmp。 tmp = z2。 z2 = z1。 z1 = tmp。 } if (y1y2) { xy = (float)(y1y2)/(float)(x2x1)。 lasty = y2。 } else { xy = (float)(y2y1)/(float)(x2x1)。 lasty = y1。 } if (z1z2) { 34 xz = (float)(z1z2)/(float)(x2x1)。 lastz = z2。 } else { xz = (float)(z2z1)/(float)(x2x1)。 lastz = z1。 } // For each step of x, y increments by: for (x = x1。 x=x2。x++) { y = (xy*(xx1))+y1。 z = (xz*(xx1))+z1。 setvoxel(x,y,z)。 } } void tmp2cube (void) { memcpy(cube, fb, 64)。 // copy the current cube into a buffer. } // Shift the entire contents of the cube along an axis // This is great for effects where you want to draw something // on one side of the cube and have it flow towards the other // side. Like rain flowing down the Z axiz. void shift (char axis, int direction) { int i, x ,y。 int ii, iii。 int state。 for (i = 0。 i CUBE_SIZE。 i++) { if (direction == 1) { ii = i。 } else { ii = (7i)。 } for (x = 0。 x CUBE_SIZE。 x++) { 35 for (y = 0。 y CUBE_SIZE。 y++) { if (direction == 1) { iii = ii+1。 } else { iii = ii1。 } if (axis == AXIS_Z) { state = getvoxel(x,y,iii)。 altervoxel(x,y,ii,state)。 } if (axis == AXIS_Y) { state = getvoxel(x,iii,y)。 altervoxel(x,ii,y,state)。 } if (axis == AXIS_X) { state = getvoxel(iii,y,x)。 altervoxel(ii,y,x,state)。 } } } } if (direction == 1) { i = 7。 } else { i = 0。 } for (x = 0。 x CUBE_SIZE。 x++) { for (y = 0。 y CUBE_SIZE。 y++) { if (axis == AXIS_Z) 36 clrvoxel(x,y,i)。 if (axis == AXIS_Y) clrvoxel(x,i,y)。 if (axis == AXIS_X) clrvoxel(i,y,x)。 } } } if (mode | 0x02) { if ( // We are in one of 8 corner positions (x == 0 amp。amp。 y == 0 amp。amp。 z == 0) || (x == 0 amp。amp。 y == 0 amp。amp。 z == 7) || (x == 0 amp。amp。 y == 7 amp。amp。 z == 0) || (x == 0 amp。amp。 y == 7 amp。amp。 z == 7) || (x == 7 amp。amp。 y == 0 amp。amp。 z == 0) || (x == 7 amp。amp。 y == 0 amp。amp。 z == 7) || (x == 7 amp。amp。 y == 7 amp。amp。 z == 0) || (x == 7 amp。amp。 y == 7 amp。amp。 z == 7) ) { // At this point, the voxel would bounce // back and forth between this corner, // and the exact opposite corner // We don39。t want that! // So we alter the trajectory a bit, // to avoid corner stickyness lol = rand()%3。 if (lol == 0) dx = 0。 if (lol == 1) dy = 0。 if (lol == 2) dz = 0。 } } // one last sanity check if (x == 0 amp。amp。 dx == 1) 37 dx = 1。 if (y == 0 amp。amp。 dy == 1) dy = 1。 if (z == 0 amp。amp。 dz == 1) dz = 1。 if (x == 7 amp。amp。 dx == 1) dx = 1。 if (y == 7 amp。amp。 dy == 1) dy = 1。 if (z == 7 amp。amp。 dz == 1) dz = 1。 // Finally, move the voxel. x = x + dx。 y = y + dy。 z = z + dz。 if (drawmode == 0x01) // show one voxel at time { setvoxel(x,y,z)。 delay_ms(delay)。 clrvoxel(x,y,z)。 } else if (drawmode == 0x02) // flip the voxel in question { flpvoxel(x,y,z)。 delay_ms(delay)。 } if (drawmode == 0x03) // draw a snake { for (i=7。i=0。i) { snake[i][0] = snake[i1][0]。 snake[i][1] = snake[i1][1]。 snake[i][2] = snake[i1][2]。 } snake[0][0] = x。 snake[0][1] = y。 snake[0][2] = z。 for (i=0。i8。i++) 38 { setvoxel(snake[i][0],snake[i][1],snake[i][2])。 } delay_ms(delay)。 for (i=0。i8。i++) { clrvoxel(snake[i][0],snake[i][1],snake[i][2])。 } } iterations。 } } // Set or clear exactly 512 voxels in a random order. void effect_random_filler (int delay, int state) { int x,y,z。 int loop = 0。 if (state == 1) { fill(0x00)。 } else { fill(0xff)。 } while (loop511) { x = rand()%8。 y = rand()%8。 z = rand()%8。 if ((state == 0 amp。amp。 getvoxel(x,y,z) == 0x01) || (state == 1 amp。amp。 getvoxel(x,y,z) == 0x00)) { altervoxel(x,y,z,state)。 delay_ms(delay)。 loop++。 } } } void effect_z_updown (int iterations, int delay) { 39 int i,y,move。 kkk=0。 fill(0x00)。 for (i=0。 i64。 i++) { positions[i] = 4。 destinations[i] = rand()%8。 } for (i=0。 i8。 i++) { effect