aboutsummaryrefslogtreecommitdiff
path: root/convanim.c
blob: 0be693e9da30f9971c107e6eff9d315a5f5949c0 (plain) (blame)
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
#include "rect.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "convcom.c"

#define max_rects 32

const char* next_line(const char* line) {
	const char* c;
	for (c = line; *c != '\n'; c++) {
		if (!*c) return 0;
	}
	return c + 1;
}

int line_len(const char* line) {
	const char* c;
	int r = 0;
	for (c = line; *c && *c != '\n'; c++, r++);
	return r;
}

int main(int argc, char** argv) {
	FILE* infile;
	const char* line;
	char* src;
	int size;
	int speed;
	Rect rects[max_rects];
	Rect r;
	int fc = 0;
	if (argc < 3) {
		fprintf(stderr, "Usage: %s infile outfile.\n", argv[0]);
		return 1;
	}
	infile = fopen(argv[1], "rb");
	if (!infile) {
		fprintf(stderr, "Failed to open %s.\n", argv[1]);
		return 2;
	}
	fseek(infile, 0, SEEK_END);
	size = ftell(infile);
	rewind(infile);
	src = malloc(size + 1);
	fread(src, 1, size, infile);
	src[size] = 0;
	fclose(infile);
	line = src;
	speed = (int)strtol(line, 0, 10);
	line = next_line(line);
	for (; line; line = next_line(line)) {
		int ll = line_len(line);
		char* line2 = malloc(ll + 1);
		line2[ll] = 0; /* lmao */
		memcpy(line2, line, ll);
		if (sscanf(line,
			"{%d,%d,%d,%d}", 
			&r.x,
			&r.y,
			&r.w,
			&r.h
		) != 4) break;
		rects[fc++] = r;
	}
	free(src);
	{
		FILE* outfile = fopen(argv[2], "wb");
		if (!outfile) {
			fprintf(stderr, "Failed to open %s.\n", argv[2]);
			return 2;
		}
		fwrite(&fc,    1,  4,        outfile);
		fwrite(&speed, 1,  4,        outfile);
		fwrite(rects,  fc, sizeof r, outfile);
		pad_file(8 + fc * sizeof r, outfile);
		fclose(outfile);
	}
	return 0;
}