-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
475 lines (402 loc) · 11.8 KB
/
common.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Common functions used in this project.
#include "datatypes.h"
/* Parser Functions */
void crocodile_test(){
printf("running test\n");
rstring buf = read_file("parsetest.ated");
ated myentitydef = parse(buf);
printfn("Name: %s",myentitydef.name);
usleep(500000);
initscr();
noecho();
cbreak();
nodelay(stdscr, 1);
curs_set(FALSE);
for(int s = 0 ; s<myentitydef.num_states; s++){
for(int c = 0 ; c<myentitydef.columns; c++){
for(int r = 0 ; r<myentitydef.rows; r++){
mvaddch(r,c,myentitydef.states[s].symbols[c+r*myentitydef.columns]);
}
}
refresh();
sleep(1);
}
endwin();
dealloc_ated(myentitydef);
}
void dealloc_ated(ated object){
//TODO
}
//return string from text file, null terminated
//puts length of file into passed pointer
rstring read_file(char* filename){
rstring text;
text.position = 0;
FILE * f = fopen (filename, "rb");
if (f)
{
fseek (f, 0, SEEK_END);
//length = max index +1
text.length = ftell (f) ;
fseek (f, 0, SEEK_SET);
//note: must free text.text later
//note: text.text is not null terminated
text.text = malloc (text.length);
if (text.text){
fread (text.text, 1, text.length, f);
}
fclose (f);
}
printf(text.text);
return text;
}
char get_byte(rstring* buff){
char c = (*buff).text[(*buff).position];
//printf("%d \n",(*buff).position);
(*buff).position++;
if((*buff).position==(*buff).length){
//loop back
(*buff).position=0;
}
//NOTE: temp removed support for backslash escapes
if(c==92 && false){
//handle backslash escaping
c = (*buff).text[(*buff).position];
//TODO add support for \n \t etc
(*buff).position++;
if((*buff).position==(*buff).length){
//loop back
(*buff).position=0;
}
}else if(c==10){
//ignore newlines unless escaped
c = get_byte(buff);
}
return c;
}
ated parse(rstring text){
printf("testing\n");
char buffer[SEGMENT_BUFFER_LEN];//create a buffer for parts of the file
int len=0; //current length of string in buffer
static ated entityDef; //object to contain parsed file data
int segment = 0;//which segment of the file the scanner is in
int i = 0;
int header_flg = 1; //boolean flag
//TODO what if header never becomes true (currently causes seg faulkt)
for (; header_flg; i++){
char character = get_byte(&text);
//printf("char: %c %d, %d\n",character, len, i);
//TODO handle file to short
if(i>=SEGMENT_BUFFER_LEN){
printf("ERROR: Parser Buffer Overflow.");
break;//TODO error handle
}
else if(character==SEGMENT_DELIMIT){
buffer[len]=0;//null terminate the buffer
len+=1;//leaving room for null terminator, man is it a pain to have to remember that
//TODO look into strncpy and make sure we are really mallocing enough bytes
printf("Header: ");
fwrite(buffer, 1, len, stdout);
printf("\n\n");
switch(segment) {
case 0 :
//everything prior to first delimiter is a comment
break;
case 1:
//name
entityDef.name = malloc(len*sizeof(char));
strncpy(entityDef.name, buffer, len);
break;
case 2:
//display
entityDef.display = malloc(len*sizeof(char));
strncpy(entityDef.display, buffer, len);
break;
case 3:
//description
entityDef.description = malloc(len*sizeof(char));
strncpy(entityDef.description, buffer, len);
break;
case 4:
//id
entityDef.id = strtol(buffer, NULL, 10);
break;
case 5:
//columns
entityDef.columns = strtol(buffer, NULL, 10);
break;
case 6:
//rows
entityDef.rows = strtol(buffer, NULL, 10);
entityDef.block = entityDef.rows * entityDef.columns;
break;
case 7:
//states
entityDef.num_states = strtol(buffer, NULL, 10);
entityDef.states = malloc(entityDef.num_states*sizeof(ated_state));
break;
case 8:
//TODO test this
{
//colormapping
int index = 0;
int num = 1;//color mapping zero is reserved
//TODO this could handle errors better
while(index<len){
//ex "c|255|000|020|"
char id = buffer[index];
color c;
c.r=strtol(buffer+index+2, NULL, 10);
c.g=strtol(buffer+index+6, NULL, 10);
c.b=strtol(buffer+index+10, NULL, 10);
index+=14;
entityDef.colormap[id]=num;
entityDef.colors[num]=c;
num++;
}
break;
}
case 9:
//end of header data, break out of for loop
header_flg = 0;
break;
// TODO: Spaker, please add a default case.
// I'm not really sure what it should be.
}
segment++;
len=0;
}else{
buffer[len]=character;
len++;
}
}
//text.position = i; DO NOT DO THIS, char gotten from text does not correspond to position due to escapes and newlines
//Begin Scanning for symbol, color, and hitbox data
printfn("%d, %d, %d",entityDef.rows, entityDef.columns, entityDef.block);
//printfn("Data:%s", data); doesn't work with rstring
for(int n=0;n<entityDef.num_states;n++){
//fwrite(data + n*entityDef.block,1,entityDef.block,stdout); putchar('\n');
//entityDef.states[n].symbols = substring(data + n*entityDef.block, entityDef.block);
//symbols
entityDef.states[n].symbols = malloc(entityDef.block*sizeof(char));
for(int i=0; i<entityDef.block;i++){
entityDef.states[n].symbols[i]=get_byte(&text);
}
//colormap
entityDef.states[n].colormap = malloc(entityDef.block*sizeof(char));
for(int i=0; i<entityDef.block;i++){
entityDef.states[n].colormap[i]=get_byte(&text);
}
//hitmap
entityDef.states[n].hitmap = malloc(entityDef.block*sizeof(char));
for(int i=0; i<entityDef.block;i++){
entityDef.states[n].hitmap[i]=get_byte(&text);
}
}
return entityDef;
}
void printfn(const char *fmt, ...){
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
putchar('\n');
va_end(args);
}
char* substring(char* src, int len){
char* sub = malloc(len*sizeof(char));
for(int i=0; i<len; i++){
sub[i]= src[i];
}
return sub;
}
/* Sound Functions */
int Callback(const void *input,
void *output,
unsigned long frameCount,
const PaStreamCallbackTimeInfo* paTimeInfo,
PaStreamCallbackFlags statusFlags,
void *userData)
{
adata_t *data = (adata_t *)userData; /* we passed a data structure into the callback so we have something to work with */
int *cursor; /* current pointer into the output */
int *inject_buff;
int *out = (int *)output;
int thisSize = frameCount;
int thisRead;
cursor = out; /* set the output cursor to the beginning */
while (thisSize > 0)
{
/* seek to our current file position */
sf_seek(data->sndFile, data->position, SEEK_SET);
/* are we going to read past the end of the file?*/
if (thisSize > (data->sfInfo.frames - data->position))
{
/*if we are, only read to the end of the file*/
thisRead = data->sfInfo.frames - data->position;
/* and then loop to the beginning of the file */
data->position = 0;
}
else
{
/* otherwise, we'll just fill up the rest of the output buffer */
thisRead = thisSize;
/* and increment the file position */
data->position += thisRead;
}
/* since our output format and channel interleaving is the same as sf_readf_int's requirements */
/* we'll just read straight into the output buffer */
sf_readf_int(data->sndFile, cursor, thisRead);
int i;
for (i = 0; i < thisRead; i++)
{
if (data->volch)
{
if (data->deltavol > 0)
{
data->volume += 0.001;
data->deltavol -= 0.001;
}
if (data->deltavol < 0)
{
data->volume -= 0.001;
data->deltavol += 0.001;
}
if (data->deltavol == 0)
{
data->volch = 0;
}
}
cursor[i] *= data->volume;
}
/* increment the output cursor*/
cursor += thisRead;
/* decrement the number of samples left to process */
thisSize -= thisRead;
}
return paContinue;
}
void handle_sound(adata_t *data)
{
PaStream *stream;
PaError error;
PaStreamParameters outputParameters;
/* initialize our data structure */
data->position = 0;
data->sfInfo.format = SF_FORMAT_OGG;
/* try to open the file */
data->sndFile = sf_open(data->filepath, SFM_READ, &data->sfInfo);
if (!data->sndFile)
{
printf("error opening file\n");
exit(1);
}
/* start portaudio */
Pa_Initialize();
/* set the output parameters */
outputParameters.device = Pa_GetDefaultOutputDevice(); /* use the default device */
outputParameters.channelCount = data->sfInfo.channels; /* use the same number of channels as our sound file */
outputParameters.sampleFormat = paInt32; /* 32bit int format */
outputParameters.suggestedLatency = 0.2; /* 200 ms ought to satisfy even the worst sound card */
outputParameters.hostApiSpecificStreamInfo = 0; /* no api specific data */
/* try to open the output */
error = Pa_OpenStream(&stream, /* stream is a 'token' that we need to save for future portaudio calls */
0, /* no input */
&outputParameters,
data->sfInfo.samplerate, /* use the same sample rate as the sound file */
paFramesPerBufferUnspecified, /* let portaudio choose the buffersize */
paNoFlag, /* no special modes (clip off, dither off) */
Callback, /* callback function defined above */
data ); /* pass in our data structure so the callback knows what's up */
/* if we can't open it, then bail out */
if (error)
{
printf("error opening output, error code = %i\n", error);
Pa_Terminate();
}
/* when we start the stream, the callback starts getting called */
Pa_StartStream(stream);
long ms_length = ((double)data->sfInfo.frames / (double)data->sfInfo.samplerate) * 1000.;
Pa_Sleep(ms_length);
Pa_CloseStream(stream); // stop the stream
(*data).thread_complete = 1;
Pa_Terminate(); // and shut down portaudio
}
void inject_audio(adata_t* adata, char* filepath)
{
adata->injInfo.format = SF_FORMAT_OGG;
adata->injFile = sf_open(filepath, SFM_READ, &adata->injInfo);
adata->inject_audio = 1;
}
/* syscomms functions */
void killer(int dummy){
run=0;
}
/* ncurses helpers */
char *randstring(int length) {
static int mySeed = 25011984;
char *string = "+=O|";
size_t stringLen = strlen(string);
char *randomString = NULL;
srand(time(NULL) * length + ++mySeed);
if (length < 1) {
length = 1;
}
randomString = malloc(sizeof(char) * (length +1));
if (randomString) {
short key = 0;
int n;
for (n = 0;n < length;n++) {
key = rand() % stringLen;
randomString[n] = string[key];
}
randomString[length] = '\0';
return randomString;
}
else {
printf("No memory");
exit(1);
}
}
void genstars(int num){
int i;
for(i=0;i<num;i++){
stars[i].x=random_number(maxx/6 + 2, 5*maxx/6 - 1);
stars[i].y=random_number(0,maxy);
}
}
void handle_winch(int sig)
{
endwin();
// Needs to be called after an endwin() so ncurses will initialize
// itself with the new terminal dimensions.
refresh();
clear();
getmaxyx(stdscr, maxy, maxx);
genstars(STARCOUNT);
}
/* Misc. */
int random_number(int min_num, int max_num)
{
int result=0,low_num=0,hi_num=0;
if(min_num<max_num)
{
low_num=min_num;
hi_num=max_num+1; // this is done to include max_num in output.
}else{
low_num=max_num+1;// this is done to include max_num in output.
hi_num=min_num;
}
//srand(time(NULL));
result = (rand()%(hi_num-low_num))+low_num;
return result;
}
char *getstring(int length){
char *mystring = "";
for(;length>0;length--){
mystring = malloc(strlen(mystring)+1);
char pop = getchar();
typedef struct myStruct someStruct;
strcat(mystring, &pop);
}
return mystring;
}