More comments on float to fixed point

This commit is contained in:
Carlos Sanchez 2024-07-22 21:11:03 -04:00
parent 59de3e28ea
commit 22ad42b620

9
main.c
View File

@ -53,7 +53,10 @@ void drawTexInto(struct texture tex, struct texture buffer, struct rect texbound
rect_dims(texbounds, &texwidth, &texheight); rect_dims(texbounds, &texwidth, &texheight);
rect_dims(bufbounds, &outwidth, &outheight); rect_dims(bufbounds, &outwidth, &outheight);
// We precalculate the step through the texture to avoid division and // We precalculate the step through the texture to avoid division and
// multiplication per pixel // multiplication per pixel. We also expand the range so we can use
// integers. By using 256, we are essentially adding 8 bits of "decimal
// point". If you need more bits (or less), just change this to some other
// power of 2.
i32 stepx = 256 * (float)texwidth / outwidth; i32 stepx = 256 * (float)texwidth / outwidth;
i32 stepy = 256 * (float)texheight / outheight; i32 stepy = 256 * (float)texheight / outheight;
i32 texx = 256 * texbounds.x1; i32 texx = 256 * texbounds.x1;
@ -64,6 +67,8 @@ void drawTexInto(struct texture tex, struct texture buffer, struct rect texbound
for(int y = bufbounds.y1; y < bufbounds.y2; y++) { for(int y = bufbounds.y1; y < bufbounds.y2; y++) {
texx = texbounds.x1; texx = texbounds.x1;
for(int x = bufbounds.x1; x < bufbounds.x2; x++) { for(int x = bufbounds.x1; x < bufbounds.x2; x++) {
// And then with the shift, it is log2(256), or whatever you shifted
// by up there. So 512 would be 9, because 2^9 = 512
i32 texi = 3 * ((texx >> 8) + tex.width * (texy >> 8)); i32 texi = 3 * ((texx >> 8) + tex.width * (texy >> 8));
i32 bufi = 3 * (x + y * buffer.width); i32 bufi = 3 * (x + y * buffer.width);
memcpy(buffer.data + bufi, tex.data + texi, 3); memcpy(buffer.data + bufi, tex.data + texi, 3);
@ -94,7 +99,7 @@ int main() {
}; };
// We actually stretch a texture here // We actually stretch a texture here
struct rect texrect = { 0, 0, 16, 16 }; struct rect texrect = { 0, 0, 8, 12 };
struct rect bufrect = { 10, 10, 509, 499 }; struct rect bufrect = { 10, 10, 509, 499 };
clock_t start = clock(); clock_t start = clock();