-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
111 lines (90 loc) · 2.44 KB
/
main.cpp
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
#include <iostream>
#include<stdlib.h>
#include<argp.h>
#include"tilemap.h"
#include"output_tilemap.h"
#include"vera_tilemap.h"
#include"collision_tilemap.h"
const char *argp_program_version = "tmx2vera 0.1";
const char *argp_program_bug_address = "https://github.com/jestin/tmx2vera/issues";
static char doc[] = "A tool to convert Tiled maps into files usable by the VERA chip of the Commander X16 computer";
static char args_doc[] = "TMX_FILE OUTPUT_FILE";
static struct argp_option options[] = {
{"layer", 'l', "LAYER_NAME", 0, "The name of the layer to convert"},
{"collision", 'c', 0, 0, "Output a 1 byte per tile collision map instead of 2 byte per tile VERA tile map"},
{"disable-paloffset", 'd', 0, 0, "Do not write a palette offset to the tile data"},
{"use-header", 'u', 0, 0, "Write a 2-byte header to output files"},
{ 0 }
};
struct arguments
{
char *args[2];
char *layer_name;
int collision = 0;
int disable_paloffset = 0;
int use_header = 0;
};
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct arguments *args = (arguments*) state->input;
switch(key)
{
case 'l':
args->layer_name = arg;
break;
case 'c':
args->collision = 1;
break;
case 'd':
args->disable_paloffset = 1;
break;
case 'u':
args->use_header = 1;
break;
case ARGP_KEY_ARG:
if(state->arg_num >= 2)
argp_usage(state);
args->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if(state->arg_num < 2)
argp_usage(state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc };
int main(int argc, char **argv)
{
// set locale to work with Glib::ustring
std::locale::global(std::locale(""));
struct arguments args;
args.layer_name = (char*) "-";
argp_parse(&argp, argc, argv, 0, 0, &args);
std::cout << "Generating " << args.args[1] << " from '" << args.layer_name << "' layer of " << args.args[0] << "...";
OutputTilemap *outputMap;
try {
Tilemap map(args.args[0]);
if(args.collision)
{
outputMap = new CollisionTilemap(&map);
}
else
{
outputMap = new VeraTilemap(&map);
}
outputMap->writeFile(args.args[1], std::string(args.layer_name), args.disable_paloffset, args.use_header);
delete outputMap;
}
catch(const TilemapException& e)
{
std::cerr << "Exception caught: " << e.message << std::endl;
if(outputMap) {
delete outputMap;
}
}
std::cout << "Done" << std::endl;
return 0;
}