Generic bit shift

This commit is contained in:
Carlos Sanchez 2024-07-22 21:23:21 -04:00
parent 22ad42b620
commit b54e5b66f1

15
main.c
View File

@ -12,6 +12,7 @@ typedef uint8_t u8;
typedef int32_t i32; typedef int32_t i32;
#define CDEPTH 255 #define CDEPTH 255
#define FIXEDPOINTDEPTH 8
#define log(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__) #define log(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)
#define panic(fmt, ...) { fprintf(stderr, "PANIC: " fmt "\n", ##__VA_ARGS__); exit(EXIT_FAILURE); } #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 // 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 // point". If you need more bits (or less), just change this to some other
// power of 2. // power of 2.
i32 stepx = 256 * (float)texwidth / outwidth; i32 stepx = (1 << FIXEDPOINTDEPTH) * (float)texwidth / outwidth;
i32 stepy = 256 * (float)texheight / outheight; i32 stepy = (1 << FIXEDPOINTDEPTH) * (float)texheight / outheight;
i32 texx = 256 * texbounds.x1; i32 texx = (1 << FIXEDPOINTDEPTH) * texbounds.x1;
i32 texy = 256 * texbounds.y1; i32 texy = (1 << FIXEDPOINTDEPTH) * texbounds.y1;
//log("%f,%f step %f,%f", texx, texy, stepx, stepy); //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, // Buffer is R G B for each pixel, then across then down. If you want X, Y,
// it's: 3 * (x + y * width) // 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++) { for(int x = bufbounds.x1; x < bufbounds.x2; x++) {
// And then with the shift, it is log2(256), or whatever you shifted // 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 // 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); i32 bufi = 3 * (x + y * buffer.width);
memcpy(buffer.data + bufi, tex.data + texi, 3); memcpy(buffer.data + bufi, tex.data + texi, 3);
texx += stepx; texx += stepx;
@ -99,8 +100,8 @@ int main() {
}; };
// We actually stretch a texture here // We actually stretch a texture here
struct rect texrect = { 0, 0, 8, 12 }; struct rect texrect = { 0, 0, 16, 16 };
struct rect bufrect = { 10, 10, 509, 499 }; struct rect bufrect = { 10, 10, 212, 192};
clock_t start = clock(); clock_t start = clock();
for(int i = 0; i < 50; i++) { //overdraw factor for(int i = 0; i < 50; i++) { //overdraw factor