From b54e5b66f1656bb99c6d246420c162bdc2be1c51 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Mon, 22 Jul 2024 21:23:21 -0400 Subject: [PATCH] Generic bit shift --- main.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index a225a08..897ceb1 100644 --- a/main.c +++ b/main.c @@ -12,6 +12,7 @@ typedef uint8_t u8; typedef int32_t i32; #define CDEPTH 255 +#define FIXEDPOINTDEPTH 8 #define log(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__) #define panic(fmt, ...) { fprintf(stderr, "PANIC: " fmt "\n", ##__VA_ARGS__); exit(EXIT_FAILURE); } @@ -57,10 +58,10 @@ void drawTexInto(struct texture tex, struct texture buffer, struct rect texbound // 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; - i32 texy = 256 * texbounds.y1; + i32 stepx = (1 << FIXEDPOINTDEPTH) * (float)texwidth / outwidth; + i32 stepy = (1 << FIXEDPOINTDEPTH) * (float)texheight / outheight; + i32 texx = (1 << FIXEDPOINTDEPTH) * texbounds.x1; + i32 texy = (1 << FIXEDPOINTDEPTH) * texbounds.y1; //log("%f,%f step %f,%f", texx, texy, stepx, stepy); // Buffer is R G B for each pixel, then across then down. If you want X, Y, // it's: 3 * (x + y * width) @@ -69,7 +70,7 @@ void drawTexInto(struct texture tex, struct texture buffer, struct rect texbound 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 >> FIXEDPOINTDEPTH) + tex.width * (texy >> FIXEDPOINTDEPTH)); i32 bufi = 3 * (x + y * buffer.width); memcpy(buffer.data + bufi, tex.data + texi, 3); texx += stepx; @@ -99,8 +100,8 @@ int main() { }; // We actually stretch a texture here - struct rect texrect = { 0, 0, 8, 12 }; - struct rect bufrect = { 10, 10, 509, 499 }; + struct rect texrect = { 0, 0, 16, 16 }; + struct rect bufrect = { 10, 10, 212, 192}; clock_t start = clock(); for(int i = 0; i < 50; i++) { //overdraw factor