diff --git a/onefile/data/output-demo/pixelfont34.c b/onefile/data/output-demo/pixelfont34.c new file mode 100644 index 0000000..cd53a89 --- /dev/null +++ b/onefile/data/output-demo/pixelfont34.c @@ -0,0 +1,14 @@ +unsigned short font3x4_data[] = { + 0x000, 0x412, 0x02d, 0xa28, 0x79e, 0xaa1, 0xddb, 0x012, + 0x894, 0x291, 0x080, 0x5d0, 0x280, 0x1c0, 0x400, 0x2a0, + 0x56a, 0xe9a, 0xea3, 0x713, 0x9ed, 0x71f, 0xdce, 0x2a7, + 0xfdb, 0x73b, 0x410, 0x282, 0x8a0, 0xe38, 0x288, 0x427, + 0xc7e, 0xbea, 0xfdb, 0xc4e, 0x76b, 0xe5b, 0x2cf, 0xd4e, + 0xbed, 0xe97, 0x297, 0xaed, 0xe49, 0xffb, 0xb6b, 0x56a, + 0x3ef, 0x9ef, 0xaeb, 0x70e, 0x497, 0xd6d, 0x76d, 0x7ff, + 0xaad, 0x73d, 0xe67, 0xc96, 0x888, 0x693, 0x02a, 0xe00, + 0x011, 0xd50, 0x6c9, 0x450, 0x6d2, 0xc50, 0x2ca, 0x6ca, + 0xac9, 0x482, 0x682, 0xae9, 0x492, 0xfd8, 0xb58, 0x550, + 0x2d8, 0x4d8, 0x270, 0x714, 0x459, 0xd68, 0x768, 0xdf8, + 0xaa8, 0x2a8, 0xeb8, 0x8d4, 0x492, 0x391, 0x033, 0x000, +}; diff --git a/onefile/data/output-demo/pixelfont34.map b/onefile/data/output-demo/pixelfont34.map new file mode 100644 index 0000000..453f756 --- /dev/null +++ b/onefile/data/output-demo/pixelfont34.map @@ -0,0 +1,41 @@ + + # # # ## # ## # # # + # # # # # ## # ## # # # # + ## # ### # # # ### + # # # ## # # ## # # # + + # # ## ## # # ### ## ### + # # # ## # # # # ## # # + # ### # # # # # # ### # ### # + # # # # ### ### ## # ## ## # + + ## ## # ### ## # ## ## + ## ### # # ### # # ### # # ## # + ### # # # # # ### ### # + ### ## # # # ### # # ## # # ### ## + + ## ## ### ## # # ### ### # # # ## ## # + # # ## # # # # # # # # # ### # # # # + # # # ## # # ### # # ## # ### # # # # + ## ### # ## # # ### # # # ### ### # # # + + ### ### ## ## ### # # # # ### # # # # ### ## + # # # # # # # # # # # # ### # # ### # # + ### ### ## # # # # # # ### # # # # + # # # # ## # ## ## ## # # ## ### ## + + ## # # # # # # + # # # # # # # # # # # # + # # # # ## # ## # ## ## + # ## ### ## ## # ## ## # ## + + # # # # # # + # # # # ## ## # ## ## ## # + ## # # ## # ### # # # # ## ## # # + # # # ## # # # ### # # # # # # ## + + # # # # ## + ## # # # # ### # # # # ### # # # ## + # # # # # ### # # # ## # ## + # ## ## ## # # # ### # # # + diff --git a/onefile/data/output-demo/pixelfont34.png b/onefile/data/output-demo/pixelfont34.png new file mode 100644 index 0000000..1c2cb25 Binary files /dev/null and b/onefile/data/output-demo/pixelfont34.png differ diff --git a/onefile/data/output-demo/pixelfont45.c b/onefile/data/output-demo/pixelfont45.c new file mode 100644 index 0000000..6d7a444 --- /dev/null +++ b/onefile/data/output-demo/pixelfont45.c @@ -0,0 +1,14 @@ +unsigned int font3x4_data[] = { + 0x00000, 0x20222, 0x00055, 0xafafa, 0x7a65e, 0x92490, 0xe1652, 0x00024, + 0x42224, 0x24442, 0x05250, 0x02720, 0x24000, 0x00700, 0x02000, 0x12480, + 0x69996, 0x44464, 0xf2496, 0x78687, 0x88f99, 0x7871f, 0x69716, 0x1248f, + 0x69696, 0x68e96, 0x02020, 0x24040, 0x42124, 0x0f0f0, 0x24842, 0x20487, + 0xe1dd6, 0x9f996, 0x79797, 0xe111e, 0x79997, 0xf171f, 0x1171f, 0xed196, + 0x99f99, 0x72227, 0x12227, 0x95359, 0xf1111, 0xddff7, 0x99db9, 0x69996, + 0x17997, 0xed996, 0x97997, 0x7861e, 0x22227, 0xe9999, 0x35999, 0xeffbb, + 0x99699, 0x68e99, 0xf168f, 0x62226, 0x84210, 0x64446, 0x00052, 0xf0000, + 0x00042, 0x9f960, 0x79971, 0x61160, 0xe99e8, 0xe1760, 0x2272c, 0xed160, + 0x99971, 0x44604, 0x64404, 0x99791, 0x44446, 0xdff70, 0x99970, 0x69960, + 0x17970, 0xed960, 0x11960, 0x342c0, 0xc2272, 0xe9990, 0x35990, 0xeffb0, + 0x99690, 0x68e90, 0xf24f0, 0xc232c, 0x22222, 0x34c43, 0x0005a, 0x00000, +}; diff --git a/onefile/data/output-demo/pixelfont45.map b/onefile/data/output-demo/pixelfont45.map new file mode 100644 index 0000000..4ab66a3 --- /dev/null +++ b/onefile/data/output-demo/pixelfont45.map @@ -0,0 +1,49 @@ + + # # # # # ### # # # # + # # # #### # # # # # # # # # # # # + # # # ## # ## # # # ### + #### # # # # # # # # # + # # # ### # # ### # # + + ## # ## ### # # #### ## #### + # # # ## # # # # # # # # + ### # # # # # ## #### ### ### # + # # # # # # # # # # # # # + # # ## # #### ### # ### ## # + + ## ## # # ### ## ## ### ### + # # # # # # # #### # # # ## # # # # # + ## ### # # # # ## # # ### # + # # # # # # #### # # #### # # # + ## ## # # # # ### # # ### ### + + ### #### #### ## # # ### ### # # # ### # # ## + # # # # # # # # # # # # # #### ## # # # + # # ### ### # #### # # ## # #### # ## # # + # # # # # ## # # # # # # # # ## # # # # + ### #### # ### # # ### # # # #### # ## # # ## + + ### ## ### ### ### # # # # ## # # # # # #### ## + # # # # # # # # # # # # ## # # # # # # # + # # # # # # ## # # # # # #### ## ### ## # + ### # ## ### # # # # # # #### # # # # # + # ### # # ### # ### ## ### # # ## #### ## + + ## # # # # ## + # # # # # ## ### ## ### ## # ## + # # # # # # # # # ### ### # + # # #### # # # # # # # # ## + # ## #### # # ### ## ### ### # ### + + # # # # ## + ### # # # ### ### ## ### ## ## ## + # # ## # ### # #### # # # # # # # # # # # + # # # # # # # #### # # # # ### # ## # # + # # # ## # # # # ## # # ## # ### # ## + + # ## # ## # # + ### # # # # ## # # # # # #### # # # # # + # # # # # #### ## ### # ## # ## + # # # # # #### # # # # # # # + ## ### ## ### # # ## #### ## # ## + diff --git a/onefile/data/output-demo/pixelfont45.png b/onefile/data/output-demo/pixelfont45.png new file mode 100644 index 0000000..4e1256b Binary files /dev/null and b/onefile/data/output-demo/pixelfont45.png differ diff --git a/onefile/pixelfont.c b/onefile/pixelfont.c new file mode 100644 index 0000000..e6bdaf9 --- /dev/null +++ b/onefile/pixelfont.c @@ -0,0 +1,242 @@ + +#include +#include + + +#define CW 4 +#define CH 5 + +#define MAXW 1024 +#define MAXH 1024 +#define BORDER 1 + +#define ROWS 8 +#define COLS 12 + +#define WIDTH (COLS*(CW + BORDER) + BORDER) +#define HEIGHT (ROWS*(CH + BORDER) + BORDER) +#define CS ((MAXW/WIDTH) < (MAXH/HEIGHT) ? (MAXW/WIDTH) : (MAXH/HEIGHT)) + +#define IW 512 +#define IH 512 +#define IB 16 + +#define BCOLOR COLOR_BLACK +#define FCOLOR COLOR_WHITE +#define SCOLOR COLOR_YELLOW + +#define QUOTES2(x) #x +#define QUOTES(x) QUOTES2(x) + +#define PREFIX "data/output/pixelfont" QUOTES(CW) QUOTES(CH) +#define MAPFILE PREFIX ".map" +#define IMGFILE PREFIX ".png" +#define CFILE PREFIX ".c" + + +int map[HEIGHT][WIDTH]; +unsigned int img[IH][IW]; +Animation imgAnim; +int k = 1; + +const char demoText[] = + "THE QUICK BROWN FOX JUMPS OVER A LAZY DOG.\n" + "the quick brown fox jumps over a lazy dog.\n" + "\n" + "`~!@#$%^&*()-_+=\\|[]{};:'\",.<>/?\n" + "\n" + "\"Free software\" means software that respects users' freedom and community.\n" + "Roughly, it means that the users have the freedom to run, copy, distribute,\n" + "study, change and improve the software. Thus, \"free software\" is a matter\n" + "of liberty, not price. To understand the concept, you should think of \"free\"\n" + "as in \"free speech\", not as in \"free beer\". We sometimes call it \"libre\n" + "software,\" borrowing the French or Spanish word for \"free\" as in freedom,\n" + "to show we do not mean the software is gratis.\n" + "\n" + "12+(34-56)*67/890\n" + "\n" + "2^3=8\n" + "3/100=3%\n" + "\n" + "e-mail of Richard Stallman: rms@gnu.org\n" + "\n" + "`reverse apostrophe`, ~tilda, array[12], {scope}\n" + "price is $100\n" + "\n" + "#include \n" + "int x = IB, y = IB;\n" + "for(const char *c = demoText; *c; ++c) {\n" + " if (*c == '\\n') { x = IB, y += CH+BORDER; continue; }\n" + " if (*c >= 32 && *c < 128) {\n" + " int mx = ((int)*c - 32)%COLS*(CW + BORDER) + BORDER;\n" + " int my = ((int)*c - 32)/COLS*(CH + BORDER) + BORDER;\n" + " for(int yy = 0; yy < CH; ++yy)\n" + " for(int xx = 0; xx < CW; ++xx)\n" + " if (map[my + yy][mx + xx])\n" + " img[y + yy][x + xx] = FCOLOR;\n" + " }\n" + " x += CW+BORDER;\n" + "}\n"; + + +void buildDemoImage() { + for(int y = 0; y < IH; ++y) + for(int x = 0; x < IW; ++x) + imageSetPixel(IW, IH, img, x, y, BCOLOR); + + int x = IB, y = IB; + for(const char *c = demoText; *c; ++c) { + if (*c == '\n') { x = IB, y += CH+BORDER; continue; } + if (*c >= 32 && *c < 128) { + int mx = ((int)*c - 32)%COLS*(CW + BORDER) + BORDER; + int my = ((int)*c - 32)/COLS*(CH + BORDER) + BORDER; + for(int yy = 0; yy < CH; ++yy) + for(int xx = 0; xx < CW; ++xx) + if (map[my + yy][mx + xx]) + imageSetPixel(IW, IH, img, x + xx, y + yy, FCOLOR); + } + x += CW+BORDER; + } + + if (imgAnim) animationDestroy(imgAnim); + imgAnim = createAnimationFromImageEx(IW, IH, img, FALSE, FALSE, FALSE, FALSE); +} + + +void save() { + FILE *f = fopen(MAPFILE, "w"); + if (!f) + { printf("cannot write to file: %s\n", MAPFILE); return; } + for(int y = 0; y < HEIGHT; ++y) { + for(int x = 0; x < WIDTH; ++x) + fputc(map[y][x] ? '#' : ' ', f); + fputc('\n', f); + } + fclose(f); + + imageSave(IMGFILE, IW, IH, img); + + f = fopen(CFILE, "w"); + if (!f) + { printf("cannot write to file: %s\n", CFILE); return; } + + int bits = CW*CH; + char format[] = " 0x%00llx,"; + format[5] = '0' + (bits-1)/4 + 1; + unsigned long long bit = 1ull << (bits-1); + const char *itype = bits <= 8 ? "char" + : bits <= 16 ? "short" + : bits <= 32 ? "int" + : "long long"; + + fprintf(f, "unsigned %s font3x4_data[] = {\n", itype); + for(int r = 0; r < COLS; ++r) { + fprintf(f, " "); + for(int c = 0; c < ROWS; ++c) { + int i = r*ROWS + c; + int x = i%COLS*(CW+BORDER) + BORDER; + int y = i/COLS*(CH+BORDER) + BORDER; + unsigned long long d = 0; + for(int yy = 0; yy < CH; ++yy) + for(int xx = 0; xx < CW; ++xx) + d = (d >> 1) | (map[y + yy][x + xx] ? bit : 0); + fprintf(f, format, d); + } + fprintf(f, "\n"); + } + fprintf(f, "};\n"); + fclose(f); +} + + +void load() { + for(int y = 0; y < HEIGHT; ++y) + for(int x = 0; x < WIDTH; ++x) + map[y][x] = 0; + + FILE *f = fopen(MAPFILE, "r"); + if (!f) + { printf("cannot read from file: %s\n", MAPFILE); return; } + int x = 0, y = 0; + while(!feof(f)) { + int c = fgetc(f); + if (c == '\n') { x = 0; ++y; continue; } + if (c == '#' && x < WIDTH && y < HEIGHT) + map[y][x] = 1; + ++x; + } +} + + +void init() { + background(BCOLOR); + load(); + buildDemoImage(); +} + + +void draw() { + saveState(); + + int imx = (int)(mouseX()/CS); + int imy = (int)(mouseY()/CS); + + double o = CS/2.0; + stroke(colorWithAlpha(FCOLOR, 0.25)); + strokeWidth(1); + for(int r = 0; r <= ROWS; ++r) + line(o, o + r*(CH+BORDER)*CS, CS*WIDTH - o, o + r*(CH+BORDER)*CS); + for(int c = 0; c <= COLS; ++c) + line(o + c*(CW+BORDER)*CS, o, o + c*(CW+BORDER)*CS, CS*HEIGHT - o); + + noFill(); + textAlign(HALIGN_CENTER, VALIGN_CENTER); + textSize(CH*CS*0.5); + for(int i = 0; i < 96; ++i) { + double x = ((i%COLS + 0.5)*(CW+BORDER) + BORDER)*CS; + double y = ((i/COLS + 0.5)*(CH+BORDER) + BORDER)*CS; + char t[2] = { i+32, 0 }; + text(x, y, t); + } + + fill(COLOR_WHITE); + rectTextured(imgAnim, WIDTH*CS + o, o, IW*k, IH*k); + + noStroke(); + fill(FCOLOR); + for(int y = 0; y < HEIGHT; ++y) + for(int x = 0; x < WIDTH; ++x) + if (map[y][x]) rect(x*CS, y*CS, CS, CS); + + if ( imx >= BORDER && imx < WIDTH + && imy >= BORDER && imy < HEIGHT + && (imx-BORDER)%(CW+BORDER) < CW + && (imy-BORDER)%(CH+BORDER) < CH ) + { + fill(colorWithAlpha(SCOLOR, 0.25)); + rect(imx*CS, imy*CS, CS, CS); + if (mouseDown("left")) { map[imy][imx] = 1; buildDemoImage(); } + if (mouseDown("right")) { map[imy][imx] = 0; buildDemoImage(); } + } + + if (keyWentDown("s")) { + printf("saving\n"); + save(); + } + + if (keyWentDown("any +")) { + k = k == 1 ? 2 : 1; + } + + restoreState(); +} + + +int main() { + windowSetResizable(TRUE); + windowSetVariableFrameRate(); + windowSetInit(&init); + windowSetDraw(&draw); + windowRun(); + return 0; +}