filename | test/bin2c.c |
changeset | 561:533f6b478071 |
prev | 185:6755a04c447f |
author | nkeynes |
date | Mon Jan 17 21:22:06 2011 +1000 (13 years ago) |
permissions | -rw-r--r-- |
last change | Avoid rebinding textures unnecessarily (actually saves a surprising amount of runtime) |
view | annotate | diff | log | raw |
1 /* Converts a binary file into a C integer array (for inclusion in
2 a source file)
4 (c)2000 Dan Potter
6 $Id$
7 Note: Licensed under the new BSD license, see README.KOS -NK
8 */
10 #include <stdio.h>
12 void convert(char *ifn, char *ofn, char *prefix) {
13 FILE *i, *o;
14 unsigned char buffer[2048];
15 int red, left, lc, q;
16 char buf[BUFSIZ];
18 i = fopen(ifn, "rb");
19 o = fopen(ofn, "w");
20 if (!i || !o) {
21 printf("error: can't open input or output file\n");
22 return;
23 }
25 fseek(i, 0, SEEK_END); left = ftell(i); fseek(i, 0, SEEK_SET);
26 setbuf(o, buf);
28 fprintf(o, "const int %s_size = %d;\n", prefix, left);
29 fprintf(o, "const unsigned char %s_data[%d] =", prefix, left);
30 fprintf(o, "{\n\t");
32 lc = 0;
33 while(left > 0) {
34 red = fread(buffer, 1, 2048, i);
35 left -= red;
36 for (q=0; q<red; q++) {
37 fprintf(o, "0x%02x, ", buffer[q]);
38 if ((++lc) >= 8) {
39 lc = 0;
40 fprintf(o, "\n\t");
41 }
42 }
43 }
45 fprintf(o, "\n};\n");
46 fclose(i); fclose(o);
47 }
49 int main(int argc, char **argv) {
51 char *prefix;
53 argc--;
54 if (argc != 2 && argc != 3) {
55 printf("usage: bin2c <input> <output> [prefix]\n");
56 return 0;
57 }
59 prefix = (argc == 3) ? argv[3] : "file";
60 convert(argv[1], argv[2], prefix);
61 return 0;
62 }
.