| 1 |
// Copyright (c) 1993-2011 PrBoom developers (see AUTHORS) |
| 2 |
// Licence: GPLv2 or later (see COPYING) |
| 3 |
|
| 4 |
// Main program, parse command line arguments |
| 5 |
|
| 6 |
#include <ctype.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "rd_graphic.h" |
| 12 |
#include "rd_output.h" |
| 13 |
#include "rd_palette.h" |
| 14 |
#include "rd_sound.h" |
| 15 |
#include "rd_util.h" |
| 16 |
|
| 17 |
enum argtype |
| 18 |
{ |
| 19 |
ARG_NONE, |
| 20 |
ARG_OUTPUT, |
| 21 |
ARG_INCLUDE, |
| 22 |
ARG_PALETTE, |
| 23 |
ARG_MARKER, |
| 24 |
ARG_LUMP, |
| 25 |
ARG_GRAPHIC, |
| 26 |
ARG_SOUND, |
| 27 |
ARG_FLAT, |
| 28 |
ARG_SPRITE, |
| 29 |
}; |
| 30 |
|
| 31 |
static void ATTR((noreturn)) usage(int exitcode) |
| 32 |
{ |
| 33 |
FILE *f = exitcode ? stderr : stdout; |
| 34 |
fprintf(f, "Usage: rdatawad <options...>\n" |
| 35 |
" -o <output filename>\n" |
| 36 |
" -I <search directory>\n" |
| 37 |
" -palette <rgbfile>\n" |
| 38 |
" -marker <lumpname>\n" |
| 39 |
" -lumps <file>...\n" |
| 40 |
" -graphics <ppmfile>...\n" |
| 41 |
" -sounds <wavfile>...\n" |
| 42 |
" -flats <ppmfile>...\n" |
| 43 |
" -sprites <x,y,ppmfile>...\n"); |
| 44 |
exit(exitcode); |
| 45 |
} |
| 46 |
|
| 47 |
int main(int argc, char **argv) |
| 48 |
{ |
| 49 |
enum argtype argtype = ARG_NONE; |
| 50 |
const char *output = NULL; |
| 51 |
|
| 52 |
if (argc <= 1) |
| 53 |
usage(0); |
| 54 |
|
| 55 |
while (*(++argv)) |
| 56 |
{ |
| 57 |
char *arg = *argv; |
| 58 |
|
| 59 |
if (*arg == '-') |
| 60 |
{ |
| 61 |
if (*(arg+1) == '-') // allow both -switch and --switch |
| 62 |
arg++; |
| 63 |
|
| 64 |
if (!strcmp(arg, "-o")) |
| 65 |
argtype = ARG_OUTPUT; |
| 66 |
else if (!strcmp(arg, "-I")) |
| 67 |
argtype = ARG_INCLUDE; |
| 68 |
else if (!strcmp(arg, "-palette")) |
| 69 |
argtype = ARG_PALETTE; |
| 70 |
else if (!strcmp(arg, "-marker")) |
| 71 |
argtype = ARG_MARKER; |
| 72 |
else if (!strcmp(arg, "-lumps")) |
| 73 |
argtype = ARG_LUMP; |
| 74 |
else if (!strcmp(arg, "-graphics")) |
| 75 |
argtype = ARG_GRAPHIC; |
| 76 |
else if (!strcmp(arg, "-sounds")) |
| 77 |
argtype = ARG_SOUND; |
| 78 |
else if (!strcmp(arg, "-flats")) |
| 79 |
argtype = ARG_FLAT; |
| 80 |
else if (!strcmp(arg, "-sprites")) |
| 81 |
argtype = ARG_SPRITE; |
| 82 |
else if (!strcmp(arg, "-help") || !strcmp(arg, "-version")) |
| 83 |
usage(0); |
| 84 |
else |
| 85 |
usage(1); |
| 86 |
} |
| 87 |
else |
| 88 |
switch (argtype) |
| 89 |
{ |
| 90 |
case ARG_NONE: |
| 91 |
usage(1); |
| 92 |
break; |
| 93 |
|
| 94 |
case ARG_OUTPUT: |
| 95 |
output = arg; |
| 96 |
argtype = ARG_NONE; |
| 97 |
break; |
| 98 |
|
| 99 |
case ARG_INCLUDE: |
| 100 |
search_path(arg); |
| 101 |
break; |
| 102 |
|
| 103 |
case ARG_PALETTE: |
| 104 |
palette_init(arg); |
| 105 |
argtype = ARG_NONE; |
| 106 |
break; |
| 107 |
|
| 108 |
case ARG_MARKER: |
| 109 |
output_add(arg, NULL, 0); |
| 110 |
break; |
| 111 |
|
| 112 |
case ARG_LUMP: |
| 113 |
{ |
| 114 |
void *data = NULL; |
| 115 |
size_t size = read_or_die(&data, arg); |
| 116 |
output_add(arg, data, size); |
| 117 |
} |
| 118 |
break; |
| 119 |
|
| 120 |
case ARG_GRAPHIC: |
| 121 |
{ |
| 122 |
void *data = NULL; |
| 123 |
size_t size = ppm_to_patch(&data, arg, 0, 0); |
| 124 |
output_add(arg, data, size); |
| 125 |
} |
| 126 |
break; |
| 127 |
|
| 128 |
case ARG_SOUND: |
| 129 |
{ |
| 130 |
void *data = NULL; |
| 131 |
size_t size = wav_to_doom(&data, arg); |
| 132 |
output_add(arg, data, size); |
| 133 |
} |
| 134 |
break; |
| 135 |
|
| 136 |
case ARG_FLAT: |
| 137 |
{ |
| 138 |
void *data = NULL; |
| 139 |
size_t size = ppm_to_bitmap(&data, arg); |
| 140 |
output_add(arg, data, size); |
| 141 |
} |
| 142 |
break; |
| 143 |
|
| 144 |
case ARG_SPRITE: |
| 145 |
{ |
| 146 |
void *data = NULL; |
| 147 |
char *pos = arg; |
| 148 |
size_t size; |
| 149 |
int x, y; |
| 150 |
|
| 151 |
x = strtol(pos, &pos, 0); |
| 152 |
if (*pos == ',') pos++; |
| 153 |
else if (!isspace(*pos) && !isdigit(*pos)) usage(1); |
| 154 |
|
| 155 |
y = strtol(pos, &pos, 0); |
| 156 |
if (*pos == ',') pos++; |
| 157 |
else if (!isspace(*pos) && !isdigit(*pos)) usage(1); |
| 158 |
|
| 159 |
size = ppm_to_patch(&data, pos, x, y); |
| 160 |
output_add(pos, data, size); |
| 161 |
} |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if (output) |
| 167 |
output_write(output); |
| 168 |
else |
| 169 |
die("No output file specified\n"); |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |