Nice work so far! I'm sure you know this, but I'm just throwing it out there. If you have a texture dimension that is a power of two (2^n), logical anding the u and v values with (2^n - 1) will give you texture wrapping. So your loop would look something this (this isn't real code, I just wrote it as an example):
[spoiler]
[/spoiler]
[spoiler]
Code Select
typedef struct {
int x;
fp16x16 u, v; // 16.16 fixed point
fp16x16 z;
} ScanlineValue;
typedef struct {
unsigned char size;
unsigned char* texels;
} Texture;
void draw_scanline(ScanlineValue* left, ScanlineValue* right, Texture* tex, int y, short* zbuf) {
int dx = (right->x - left->x != 0 ? right->x - left->x : 1);
fp16x16 du = (right->u - left->u) / dx;
fp16x16 dv = (right->v - left->v) / dx;
fp16x16 dz = (right->z - left->z) / dx;
fp16x16 u = left->u;
fp16x16 v = left->v;
fp16x16 z = left->z;
for(int x = left->x; x <= right->x; ++x) {
int zz = z >> 16;
if(zz < zbuf[y * SCREEN_WIDTH + x]) {
int uu = (u >> 16) & (tex->size - 1);
int vv = (v >> 16) & (tex->size - 1);
screen[y * SCREEN_WIDTH + x] = tex->texels[vv * tex->size + uu];
zbuf[y * SCREEN_WIDTH + x] = zz;
}
u += du;
v += dv;
z += dz;
}
}
[/spoiler]