race condition with development tools..
[vg.git] / src / fontcomp.c
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 #define VG_TOOLS
4 #include "vg/vg.h"
5
6 #define STB_IMAGE_IMPLEMENTATION
7 #include "stb/stb_image.h"
8
9 // Super basic model compiler
10 int main( int argc, char *argv[] )
11 {
12 if( argc < 3 )
13 {
14 vg_error( "Need input/output files\n" );
15 return 0;
16 }
17
18 FILE *output;
19
20 int x,y,n;
21 unsigned char *data = stbi_load( argv[1], &x, &y, &n, 4 );
22
23 if( data )
24 {
25 output = fopen( argv[2], "w" );
26 if( !output )
27 {
28 vg_error( "couldn't open output for writing\n" );
29 free(data);
30 return 0;
31 }
32
33 fprintf( output, "/* Font buffer generated from source file: '%s' */\n", argv[1] );
34
35 u32 pixel_max = x*y;
36 u32 pixel = 0, chars = 0;
37 while(pixel_max)
38 {
39 u32 buff = 0;
40 for( int b = 31; b >= 0; b-- )
41 {
42 buff |= data[pixel*4]>128?0x1<<b:0;
43 pixel++;
44
45 if( pixel >= pixel_max )
46 {
47 pixel_max = 0;
48 break;
49 }
50 }
51
52 fprintf( output, "%#x,", buff );
53 if( (++chars) % 8 == 0 )
54 fprintf( output, "\n" );
55 }
56
57 free(data);
58 fclose(output);
59
60 vg_success( "Font compiled successfully\n" );
61 }
62 else
63 {
64 vg_error( "Couldn't open source file\n" );
65 return 0;
66 }
67 }