-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
250 lines (189 loc) · 6.36 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "targa.h"
#include "wave.h"
#include "wavefront.h"
struct {
GLuint iformat;
GLenum format;
GLsizei width;
GLsizei height;
uint8_t *data;
} tga;
struct {
ALenum format;
ALsizei frequency;
ALsizei size;
uint8_t *data;
} wave;
struct {
float *vertices;
int vertices_num;
unsigned short *indices;
int indices_num;
} obj;
void save_targa_to_header( const char *filename, const char *name ) {
FILE *fp = fopen( filename, "w" );
fprintf( fp, "#pragma once\n" );
char s[10] = {};
if ( tga.format == GL_BGR )
strcpy( s, "GL_BGR" );
if ( tga.format == GL_BGRA )
strcpy( s, "GL_BGR" );
fprintf( fp, "const GLenum %s_format = %s;\n", name, s );
if ( tga.iformat == GL_RGB8 )
strcpy( s, "GL_RGB8" );
if ( tga.format == GL_RGBA8 )
strcpy( s, "GL_RGBA8" );
fprintf( fp, "const GLuint %s_internal_format = %s;\n", name, s );
fprintf( fp, "const GLsizei %s_width = %d;\n", name, tga.width );
fprintf( fp, "const GLsizei %s_height = %d;\n", name, tga.height );
int bpp = 3;
if ( tga.iformat == GL_RGBA8 )
bpp = 4;
fprintf( fp, "const unsigned char %s_data[] = {\n", name );
for ( unsigned i = 0; i < tga.width * tga.height * bpp; i++ ) {
if ( ( i != 0 ) && ( ( i % bpp ) == 0 ) )
fprintf( fp, "\n" );
fprintf( fp, "%#x, ", tga.data[i] );
}
fprintf( fp, "};\n" );
fclose( fp );
}
void save_wave_to_header( const char *filename, const char *name ) {
FILE *fp = fopen( filename, "w" );
fprintf( fp, "#pragma once\n" );
char s[40] = {};
int depth = 0;
switch ( wave.format ) {
case AL_FORMAT_MONO8:
strcpy( s, "AL_FORMAT_MONO8" );
depth = 1;
break;
case AL_FORMAT_MONO16:
strcpy( s, "AL_FORMAT_MONO16" );
depth = 2;
break;
case AL_FORMAT_STEREO8:
strcpy( s, "AL_FORMAT_STEREO8" );
depth = 1;
break;
case AL_FORMAT_STEREO16:
strcpy( s, "AL_FORMAT_STEREO16" );
depth = 2;
break;
}
fprintf( fp, "const ALenum %s_format = %s;\n", name, s );
fprintf( fp, "const ALsizei %s_frequency = %d;\n", name, wave.frequency );
fprintf( fp, "const ALsizei %s_size = %d;\n", name, wave.size );
fprintf( fp, "const unsigned char %s_data[] = {\n", name );
for ( int i = 0; i < wave.size; i++ ) {
if ( ( i != 0 ) && ( i % 10 ) == 0 )
fprintf( fp, "\n" );
fprintf( fp, "%#x, ", wave.data[i] );
}
fprintf( fp, "};\n" );
fclose( fp );
}
void save_obj_to_header( const char *filename, const char *name ) {
FILE *fp = fopen( filename, "w" );
fprintf( fp, "#pragma once\n" );
fprintf( fp, "const float %s_vertices[] = {\n", name );
for ( int i = 0; i < obj.vertices_num * 3; i++ ) {
if ( ( i != 0 ) && ( i % 3 ) == 0 )
fprintf( fp, "\n" );
fprintf( fp, "%4.6f, ", obj.vertices[i] );
}
fprintf( fp, "};\n" );
fprintf( fp, "const unsigned short %s_indices[] = {\n", name );
for ( int i = 0; i < obj.indices_num; i++ ) {
if ( ( i != 0 ) && ( i % 3 ) == 0 )
fprintf( fp, "\n" );
fprintf( fp, "%4hd, ", obj.indices[i] );
}
fprintf( fp, "};\n" );
fprintf( fp, "const int %s_vertices_num = %d;\n", name, obj.vertices_num );
fprintf( fp, "const int %s_indices_num = %d;\n", name, obj.indices_num );
fclose( fp );
}
_Noreturn static void print_usage_and_exit( const char *app ) {
char *name = strrchr( app, '/' );
if ( name ) {
fprintf( stderr, "Usage:\n\t%s <-tga|-wave|-obj> [input file] <output file>\n", name + 1 );
}
exit( EXIT_FAILURE );
}
typedef enum { OPT_NONE, OPT_TGA, OPT_WAVE, OPT_OBJ } Options;
int main( int argc, char *argv[] ) {
Options options = OPT_NONE;
char outname[260] = {};
char inname[260] = {};
char varname[260] = {};
int opt_index = 0;
bool first_name = true;
for ( opt_index = 1; opt_index < argc; opt_index++ ) {
if ( argv[opt_index][0] == '-' ) {
if ( strcmp( &argv[opt_index][1], "tga" ) == 0 ) {
options = OPT_TGA;
} else if ( strcmp( &argv[opt_index][1], "wave" ) == 0 ) {
options = OPT_WAVE;
} else if ( strcmp( &argv[opt_index][1], "obj" ) == 0 ) {
options = OPT_OBJ;
} else {
print_usage_and_exit( argv[0] );
}
} else {
if ( first_name ) {
strcpy( inname, argv[opt_index] );
first_name = false;
} else {
strcpy( outname, argv[opt_index] );
}
}
}
if ( inname[0] == 0 )
print_usage_and_exit( argv[0] );
if ( inname[0] != 0 && options == OPT_NONE ) {
const char *ext = strrchr( inname, '.' );
if ( !ext )
exit( EXIT_FAILURE );
if ( strcmp( ext, ".tga" ) == 0 ) {
options = OPT_TGA;
} else if ( strcmp( ext, ".wave" ) == 0 || strcmp( ext, ".wav" ) ) {
options = OPT_WAVE;
} else if ( strcmp( ext, ".obj" ) == 0 ) {
options = OPT_OBJ;
}
}
if ( options == OPT_NONE )
print_usage_and_exit( argv[0] );
if ( outname[0] == 0 ) {
const char *name = strrchr( inname, '/' );
if ( !name )
exit( EXIT_FAILURE );
const char *ext = strrchr( name, '.' );
if ( !ext )
exit( EXIT_FAILURE );
strncpy( outname, name + 1, ( size_t )( ext - name - 1 ) );
strcpy( varname, outname );
strcat( outname, ".h" );
}
if ( options == OPT_TGA ) {
tga.data = (uint8_t *)load_targa( inname, &tga.iformat, &tga.format, &tga.width, &tga.height );
save_targa_to_header( outname, varname );
free( tga.data );
} else if ( options == OPT_WAVE ) {
wave.data = (uint8_t *)load_wave( inname, &wave.format, &wave.frequency, &wave.size );
save_wave_to_header( outname, varname );
free( wave.data );
} else {
load_wavefront( inname, &obj.vertices, &obj.vertices_num, &obj.indices, &obj.indices_num );
save_obj_to_header( outname, varname );
free( obj.vertices );
free( obj.indices );
}
return EXIT_SUCCESS;
}