// imagifier: Convert an ASCII representation of a roguelike map into an image // so it can be marked up in Photoshop. #include #include #include #include typedef struct { unsigned width, height; unsigned char *data; } framebuffer; int get_pixel(const framebuffer *fb, unsigned x, unsigned y) { assert(fb && xwidth && yheight); return fb->data[y*fb->width + x]; } void set_pixel(framebuffer *fb, unsigned x, unsigned y, int value) { assert(fb && xwidth && yheight); fb->data[y*fb->width + x] = value; } framebuffer *framebuffer_new(unsigned width, unsigned height) { framebuffer *ret = (framebuffer*)malloc(sizeof(framebuffer)); ret->width = width; ret->height = height; ret->data = (char*)malloc(width*height); return ret; } void blit(framebuffer *dest, framebuffer *source, int dest_x, int dest_y, int width, int height) { int ii, jj; for(ii=0; iileft && bottom>top); assert(fb); assert(fout); // Collect pixel values from the framebuffer for(x=left; x>8, height, height>>8, 8, 0x20 }; fwrite(&tgaHead, sizeof tgaHead, 1, fout); fwrite(pixbuf, bufsiz, 1, fout); free(pixbuf); } const int tilesize_x = 5, tilesize_y = 5; framebuffer tile_wall = { 5, 5, "#####" "#####" "#####" "#####" "#####" }; framebuffer tile_space = { 5, 5, "=====" "=====" "=====" "=====" "=====" }; framebuffer tile_dot = { 5, 5, " " " " " " " - " " " }; framebuffer tile_plus = { 5, 5, " " " # " " ### " " # " " " }; framebuffer tile_question = { 5, 5, " ## " " # " " # " " " " # " }; void init_tile(framebuffer *tile) { int ii, jj, v; for(ii=0; iiheight; ii++) for(jj=0; jjwidth; jj++) { v = get_pixel(tile, jj, ii); switch(v) { case ' ': v=255; break; case '-': v=128; break; case '#': v=0; break; case '=': v=95; break; default: v=128; break; } set_pixel(tile, jj, ii, v); } } void init_all_tiles(void) { init_tile(&tile_wall); init_tile(&tile_space); init_tile(&tile_dot); init_tile(&tile_plus); init_tile(&tile_question); } framebuffer *get_tile(char tile) { switch(tile) { case '#': return &tile_wall; case ' ': return &tile_space; case '.': return &tile_dot; case '+': return &tile_plus; default: return &tile_question; } } int main(int argc, char **argv) { const char *out_filename = NULL; int size_x=0, size_y=0; int ii, jj; char line_inbuf[512]; char **tilebuf; framebuffer *fb; FILE *fout; if(argc < 2) { fprintf(stderr, "Usage: %s [-o filename] size_x size_y\n", argv[0]); return 1; } if(!strcmp(argv[1], "-o")) { if(argc < 5) { fprintf(stderr, "Not enough arguments.\n"); return 1; } out_filename = argv[2]; size_x = atoi(argv[3]); size_y = atoi(argv[4]); } else { size_x = atoi(argv[1]); size_y = atoi(argv[2]); } assert(size_x < 512); tilebuf = (char**)malloc( sizeof(char*) * size_y ); for(ii=0; iiwidth, fb->height); fclose(fout); return 0; }