diff --git a/.gitignore b/.gitignore index ac7f75e..8fe3570 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ texmap *.png *.ppm +*.swp wget-log diff --git a/main.c b/main.c index 74dfe66..b76f808 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,10 @@ #include #include #include +#include + +// You need to generate this with textoc.h +#include "texture.h" typedef uint8_t u8; @@ -10,32 +14,93 @@ typedef uint8_t u8; #define log(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__) #define panic(fmt, ...) { fprintf(stderr, "PANIC: " fmt "\n", ##__VA_ARGS__); exit(EXIT_FAILURE); } +// A simple designation of a rectangle +struct rect { + int x1, y1; // Inclusive + int x2, y2; // Exclusive +}; + +// Compute the dimensions of a rect +void rect_dims(struct rect bounds, int * width, int * height) { + *width = abs(bounds.x2 - bounds.x1); + *height = abs(bounds.y2 - bounds.y1); +} + +// A simple designation of a texture +struct texture { + int width, height; + u8 * data; +}; + + // fb is framebuffer. Thsi dumps a color version (rgb) of a framebuffer // to stdout, which you can pipe to a file or some shit idk -void dump_ppm(u8 * fb, int width, int height) { - log("Dumping framebuffer %dx%d", width, height); - printf("P3\n%d %d\n%d\n# Image start\n", width, height, CDEPTH); - for(int i = 0; i < width * height * 3; i+=3) - printf("%d %d %d\n", fb[i], fb[i+1], fb[i+2]); +void dump_ppm(struct texture fb) { + log("Dumping framebuffer %dx%d", fb.width, fb.height); + printf("P3\n%d %d\n%d\n# Image start\n", fb.width, fb.height, CDEPTH); + for(int i = 0; i < fb.width * fb.height * 3; i+=3) + printf("%d %d %d\n", fb.data[i], fb.data[i+1], fb.data[i+2]); printf("\n"); } +// ------------------------------------------------------ +// * THIS IS THE FUNCTION YOU WANT <3 * +// ------------------------------------------------------ +void drawTexInto(struct texture tex, struct texture buffer, struct rect texbounds, struct rect bufbounds) { + int texwidth, texheight, outwidth, outheight; + rect_dims(texbounds, &texwidth, &texheight); + rect_dims(bufbounds, &outwidth, &outheight); + // We precalculate the step through the texture to avoid division and + // multiplication per pixel + float stepx = (float)texwidth / outwidth; + float stepy = (float)texheight / outheight; + float texx = texbounds.x1; + float texy = 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) + for(int y = bufbounds.y1; y < bufbounds.y2; y++) { + texx = texbounds.x1; + for(int x = bufbounds.x1; x < bufbounds.x2; x++) { + int texi = 3 * (floor(texx) + tex.width * floor(texy)); + int bufi = 3 * (x + buffer.width * y); + // You can get rid of this for loop for more performance, but probably + // not a big deal with -O3, it'll be unrolled + for(int i = 0; i < 3; i++) { + buffer.data[bufi + i] = tex.data[texi + i]; + } + texx += stepx; + } + texy += stepy; + } +} + int main() { log("Program start"); const int width = 512; const int height = 512; - const int fbsize = width * height * 3 * sizeof(u8); + const int fbsize = width * height * 3; - u8 * fb = malloc(fbsize); - if(!fb) { - panic("Can't allocate framebuffer"); - } - memset(fb, 0, fbsize); + struct texture fb = { + width, height, + calloc(fbsize, 1) + }; - // We actually stretch a texture + if(!fb.data) panic("Can't allocate framebuffer"); - dump_ppm(fb, width, height); + // Initialize the texture. It's already defined elsewhere + struct texture tex = { + mytex_width, mytex_height, mytex + }; + + // We actually stretch a texture here + struct rect texrect = { 0, 0, 16, 16 }; + struct rect bufrect = { 100, 100, 502, 192 }; + drawTexInto(tex, fb, texrect, bufrect); + + // Dump the image to the ppm + dump_ppm(fb); log("Program end"); } diff --git a/run.sh b/run.sh index 799ca48..1c3b1a2 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,7 @@ #!/bin/sh +# Yeah you need furnace.png lol +./textoc.sh furnace.png ./build.sh ./texmap > image.ppm convert image.ppm image.png diff --git a/textoc.sh b/textoc.sh old mode 100644 new mode 100755 index 8a14a12..a776060 --- a/textoc.sh +++ b/textoc.sh @@ -2,9 +2,13 @@ if [ $# -lt 1 ] then - echo "You must pass in the texture!" + echo "You must pass in the texture! It should be 16x16 lol" exit 1 fi convert $1 -compress none $1.ppm +echo "int mytex_width = 16, mytex_height = 16;\nuint8_t mytex[] = {\n" > texture.h +cat $1.ppm | sed 1,3d | sed -e 's/ /, /g' >> texture.h +echo "};" >> texture.h + diff --git a/texture.h b/texture.h new file mode 100644 index 0000000..bebc3cd --- /dev/null +++ b/texture.h @@ -0,0 +1,20 @@ +int mytex_width = 16, mytex_height = 16; +uint8_t mytex[] = { + +197, 197, 197, 171, 172, 171, 143, 143, 143, 143, 143, 143, 104, 104, 104, 116, 116, 116, 104, 104, 104, 116, 116, 116, 104, 104, 104, 116, 116, 116, 116, 116, 116, 104, 104, 104, 197, 197, 197, 171, 172, 171, 143, 143, 143, 143, 143, 143, +171, 172, 171, 63, 62, 66, 73, 72, 72, 104, 104, 104, 79, 79, 79, 73, 72, 72, 63, 62, 66, 63, 62, 66, 73, 72, 72, 73, 72, 72, 73, 72, 72, 63, 62, 66, 171, 172, 171, 63, 62, 66, 73, 72, 72, 104, 104, 104, +143, 143, 143, 73, 72, 72, 89, 88, 88, 114, 121, 110, 73, 72, 72, 73, 72, 72, 79, 79, 79, 79, 79, 79, 79, 79, 79, 89, 88, 88, 89, 88, 88, 79, 79, 79, 143, 143, 143, 73, 72, 72, 89, 88, 88, 114, 121, 110, +116, 116, 116, 114, 121, 110, 114, 121, 110, 136, 135, 136, 63, 62, 66, 73, 72, 72, 73, 72, 72, 73, 72, 72, 63, 62, 66, 63, 62, 66, 79, 79, 79, 79, 79, 79, 116, 116, 116, 114, 121, 110, 114, 121, 110, 136, 135, 136, +104, 104, 104, 63, 62, 66, 63, 62, 66, 79, 79, 79, 89, 88, 88, 103, 97, 97, 89, 88, 88, 89, 88, 88, 89, 88, 88, 79, 79, 79, 73, 72, 72, 73, 72, 72, 63, 62, 66, 63, 62, 66, 63, 62, 66, 104, 104, 104, +116, 116, 116, 63, 62, 66, 73, 72, 72, 73, 72, 72, 73, 72, 72, 89, 88, 88, 89, 88, 88, 79, 79, 79, 63, 62, 66, 73, 72, 72, 73, 72, 72, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 116, 116, 116, +116, 116, 116, 73, 72, 72, 79, 79, 79, 136, 135, 136, 136, 135, 136, 136, 135, 136, 136, 135, 136, 136, 135, 136, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 104, 104, 104, 79, 79, 79, 79, 79, 79, 116, 116, 116, +104, 104, 104, 79, 79, 79, 79, 79, 79, 136, 135, 136, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 197, 171, 172, 171, 171, 172, 171, 171, 172, 171, 104, 104, 104, 73, 72, 72, 73, 72, 72, 104, 104, 104, +104, 104, 104, 63, 62, 66, 63, 62, 66, 116, 116, 116, 156, 156, 156, 171, 172, 171, 17, 17, 17, 171, 172, 171, 17, 17, 17, 171, 172, 171, 17, 17, 17, 156, 156, 156, 104, 104, 104, 63, 62, 66, 63, 62, 66, 104, 104, 104, +143, 143, 143, 181, 181, 181, 197, 197, 197, 156, 156, 156, 104, 104, 104, 171, 172, 171, 17, 17, 17, 171, 172, 171, 17, 17, 17, 171, 172, 171, 17, 17, 17, 156, 156, 156, 89, 88, 88, 181, 181, 181, 181, 181, 181, 143, 143, 143, +104, 104, 104, 143, 143, 143, 104, 104, 104, 156, 156, 156, 104, 104, 104, 171, 172, 171, 17, 17, 17, 156, 156, 156, 17, 17, 17, 156, 156, 156, 17, 17, 17, 156, 156, 156, 89, 88, 88, 156, 156, 156, 156, 156, 156, 104, 104, 104, +104, 104, 104, 143, 143, 143, 116, 116, 116, 89, 88, 88, 114, 121, 110, 156, 156, 156, 197, 197, 197, 156, 156, 156, 197, 197, 197, 156, 156, 156, 197, 197, 197, 114, 121, 110, 89, 88, 88, 136, 135, 136, 156, 156, 156, 104, 104, 104, +104, 104, 104, 143, 143, 143, 136, 135, 136, 73, 72, 72, 114, 121, 110, 114, 121, 110, 114, 121, 110, 114, 121, 110, 114, 121, 110, 114, 121, 110, 114, 121, 110, 114, 121, 110, 73, 72, 72, 143, 143, 143, 136, 135, 136, 104, 104, 104, +89, 88, 88, 136, 135, 136, 136, 135, 136, 73, 72, 72, 73, 72, 72, 73, 72, 72, 73, 72, 72, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 136, 135, 136, 127, 127, 127, 89, 88, 88, +89, 88, 88, 127, 127, 127, 136, 135, 136, 127, 127, 127, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 127, 127, 127, 127, 127, 127, 127, 127, 127, 89, 88, 88, +89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, 89, 88, 88, +};