From 22ad42b6202f72034afae46031f79dae23d65a5f Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Mon, 22 Jul 2024 21:11:03 -0400 Subject: [PATCH] More comments on float to fixed point --- main.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 5992f50..a225a08 100644 --- a/main.c +++ b/main.c @@ -53,7 +53,10 @@ void drawTexInto(struct texture tex, struct texture buffer, struct rect texbound rect_dims(texbounds, &texwidth, &texheight); rect_dims(bufbounds, &outwidth, &outheight); // 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 stepy = 256 * (float)texheight / outheight; 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++) { texx = texbounds.x1; 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 bufi = 3 * (x + y * buffer.width); memcpy(buffer.data + bufi, tex.data + texi, 3); @@ -94,7 +99,7 @@ int main() { }; // 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 }; clock_t start = clock();