POG
This commit is contained in:
parent
43b9598a16
commit
e56131c3f9
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
texmap
|
texmap
|
||||||
*.png
|
*.png
|
||||||
*.ppm
|
*.ppm
|
||||||
|
*.swp
|
||||||
wget-log
|
wget-log
|
||||||
|
91
main.c
91
main.c
@ -2,6 +2,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// You need to generate this with textoc.h
|
||||||
|
#include "texture.h"
|
||||||
|
|
||||||
typedef uint8_t u8;
|
typedef uint8_t u8;
|
||||||
|
|
||||||
@ -10,32 +14,93 @@ typedef uint8_t u8;
|
|||||||
#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); }
|
||||||
|
|
||||||
|
// 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
|
// 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
|
// to stdout, which you can pipe to a file or some shit idk
|
||||||
void dump_ppm(u8 * fb, int width, int height) {
|
void dump_ppm(struct texture fb) {
|
||||||
log("Dumping framebuffer %dx%d", width, height);
|
log("Dumping framebuffer %dx%d", fb.width, fb.height);
|
||||||
printf("P3\n%d %d\n%d\n# Image start\n", width, height, CDEPTH);
|
printf("P3\n%d %d\n%d\n# Image start\n", fb.width, fb.height, CDEPTH);
|
||||||
for(int i = 0; i < width * height * 3; i+=3)
|
for(int i = 0; i < fb.width * fb.height * 3; i+=3)
|
||||||
printf("%d %d %d\n", fb[i], fb[i+1], fb[i+2]);
|
printf("%d %d %d\n", fb.data[i], fb.data[i+1], fb.data[i+2]);
|
||||||
printf("\n");
|
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() {
|
int main() {
|
||||||
log("Program start");
|
log("Program start");
|
||||||
|
|
||||||
const int width = 512;
|
const int width = 512;
|
||||||
const int height = 512;
|
const int height = 512;
|
||||||
const int fbsize = width * height * 3 * sizeof(u8);
|
const int fbsize = width * height * 3;
|
||||||
|
|
||||||
u8 * fb = malloc(fbsize);
|
struct texture fb = {
|
||||||
if(!fb) {
|
width, height,
|
||||||
panic("Can't allocate framebuffer");
|
calloc(fbsize, 1)
|
||||||
}
|
};
|
||||||
memset(fb, 0, fbsize);
|
|
||||||
|
|
||||||
// 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");
|
log("Program end");
|
||||||
}
|
}
|
||||||
|
2
run.sh
2
run.sh
@ -1,5 +1,7 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Yeah you need furnace.png lol
|
||||||
|
./textoc.sh furnace.png
|
||||||
./build.sh
|
./build.sh
|
||||||
./texmap > image.ppm
|
./texmap > image.ppm
|
||||||
convert image.ppm image.png
|
convert image.ppm image.png
|
||||||
|
6
textoc.sh
Normal file → Executable file
6
textoc.sh
Normal file → Executable file
@ -2,9 +2,13 @@
|
|||||||
|
|
||||||
if [ $# -lt 1 ]
|
if [ $# -lt 1 ]
|
||||||
then
|
then
|
||||||
echo "You must pass in the texture!"
|
echo "You must pass in the texture! It should be 16x16 lol"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
convert $1 -compress none $1.ppm
|
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
|
||||||
|
|
||||||
|
20
texture.h
Normal file
20
texture.h
Normal file
@ -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,
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user