Example #1
0
int
main(int argc, char *argv[])
{
	int ch, size;
	struct Options opts = {0};
	struct PNGImage png = {0};
	struct GBImage gb = {0};
	struct Tilemap tilemap = {0};
	char *ext;
	const char *errmsg = "Warning: The PNG's %s setting is not the same as the setting defined on the command line.";

	if (argc == 1) {
		usage();
	}

	opts.mapfile = "";
	opts.palfile = "";
	opts.outfile = "";

	depth = 2;

	while((ch = getopt(argc, argv, "Dd:Ffho:Tt:uPp:Vvx:")) != -1) {
		switch(ch) {
		case 'D':
			opts.debug = true;
			break;
		case 'd':
			depth = strtoul(optarg, NULL, 0);
			break;
		case 'F':
			opts.hardfix = true;
		case 'f':
			opts.fix = true;
			break;
		case 'h':
			opts.horizontal = true;
			break;
		case 'o':
			opts.outfile = optarg;
			break;
		case 'P':
			opts.palout = true;
			break;
		case 'p':
			opts.palfile = optarg;
			break;
		case 'T':
			opts.mapout = true;
			break;
		case 't':
			opts.mapfile = optarg;
			break;
		case 'u':
			opts.unique = true;
			break;
		case 'V':
			printf("rgbgfx %s\n", get_package_version_string());
			exit(0);
		case 'v':
			opts.verbose = true;
			break;
		case 'x':
			opts.trim = strtoul(optarg, NULL, 0);
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc == 0) {
		usage();
	}

	opts.infile = argv[argc - 1];

	if (depth != 1 && depth != 2) {
		errx(1, "Depth option must be either 1 or 2.");
	}
	colors = 1 << depth;

	input_png_file(opts, &png);

	png.mapfile = "";
	png.palfile = "";

	get_text(&png);

	if (png.horizontal != opts.horizontal) {
		if (opts.verbose) {
			warnx(errmsg, "horizontal");
		}
		if (opts.hardfix) {
			png.horizontal = opts.horizontal;
		}
	}
	if (png.horizontal) {
		opts.horizontal = png.horizontal;
	}

	if (png.trim != opts.trim) {
		if (opts.verbose) {
			warnx(errmsg, "trim");
		}
		if (opts.hardfix) {
			png.trim = opts.trim;
		}
	}
	if (png.trim) {
		opts.trim = png.trim;
	}
	if (opts.trim > png.width / 8 - 1) {
		errx(1, "Trim (%i) for input png file '%s' too large (max: %i)", opts.trim, opts.infile, png.width / 8 - 1);
	}

	if (strcmp(png.mapfile, opts.mapfile) != 0) {
		if (opts.verbose) {
			warnx(errmsg, "tilemap file");
		}
		if (opts.hardfix) {
			png.mapfile = opts.mapfile;
		}
	}
	if (!*opts.mapfile) {
		opts.mapfile = png.mapfile;
	}

	if (png.mapout != opts.mapout) {
		if (opts.verbose) {
			warnx(errmsg, "tilemap file");
		}
		if (opts.hardfix) {
			png.mapout = opts.mapout;
		}
	}
	if (png.mapout) {
		opts.mapout = png.mapout;
	}

	if (strcmp(png.palfile, opts.palfile) != 0) {
		if (opts.verbose) {
			warnx(errmsg, "palette file");
		}
		if (opts.hardfix) {
			png.palfile = opts.palfile;
		}
	}
	if (!*opts.palfile) {
		opts.palfile = png.palfile;
	}

	if (png.palout != opts.palout) {
		if (opts.verbose) {
			warnx(errmsg, "palette file");
		}
		if (opts.hardfix) {
			png.palout = opts.palout;
		}
	}
	if (png.palout) {
		opts.palout = png.palout;
	}

	if (!*opts.mapfile && opts.mapout) {
		if ((ext = strrchr(opts.infile, '.')) != NULL) {
			size = ext - opts.infile + 9;
			opts.mapfile = malloc(size);
			strncpy(opts.mapfile, opts.infile, size);
			*strrchr(opts.mapfile, '.') = '\0';
			strcat(opts.mapfile, ".tilemap");
		} else {
			opts.mapfile = malloc(strlen(opts.infile) + 9);
			strcpy(opts.mapfile, opts.infile);
			strcat(opts.mapfile, ".tilemap");
		}
	}

	if (!*opts.palfile && opts.palout) {
		if ((ext = strrchr(opts.infile, '.')) != NULL) {
			size = ext - opts.infile + 5;
			opts.palfile = malloc(size);
			strncpy(opts.palfile, opts.infile, size);
			*strrchr(opts.palfile, '.') = '\0';
			strcat(opts.palfile, ".pal");
		} else {
			opts.palfile = malloc(strlen(opts.infile) + 5);
			strcpy(opts.palfile, opts.infile);
			strcat(opts.palfile, ".pal");
		}
	}

	gb.size = png.width * png.height * depth / 8;
	gb.data = calloc(gb.size, 1);
	gb.trim = opts.trim;
	gb.horizontal = opts.horizontal;

	if (*opts.outfile || *opts.mapfile) {
		png_to_gb(png, &gb);
		create_tilemap(opts, &gb, &tilemap);
	}

	if (*opts.outfile) {
		output_file(opts, gb);
	}

	if (*opts.mapfile) {
		output_tilemap_file(opts, tilemap);
	}

	if (*opts.palfile) {
		output_palette_file(opts, png);
	}

	if (opts.fix || opts.debug) {
		set_text(&png);
		output_png_file(opts, &png);
	}

	free_png_data(&png);
	free(gb.data);

	return 0;
}
Example #2
0
File: main.c Project: bentley/rgbds
int main(int argc, char *argv[])
{
	int ch;
	char *ep;

	if (argc == 1)
		print_usage();

	while ((ch = getopt(argc, argv, "dl:m:n:O:o:p:s:tVw")) != -1) {
		switch (ch) {
		case 'l':
			SetLinkerscriptName(optarg);
			break;
		case 'm':
			SetMapfileName(optarg);
			break;
		case 'n':
			SetSymfileName(optarg);
			break;
		case 'o':
			out_Setname(optarg);
			break;
		case 'O':
			out_SetOverlayname(optarg);
			options |= OPT_OVERLAY;
			break;
		case 'p':
			fillchar = strtoul(optarg, &ep, 0);
			if (optarg[0] == '\0' || *ep != '\0')
				errx(1, "Invalid argument for option 'p'");
			if (fillchar < 0 || fillchar > 0xFF)
				errx(1, "Argument for option 'p' must be between 0 and 0xFF");
			break;
		case 's':
			options |= OPT_SMART_C_LINK;
			smartlinkstartsymbol = optarg;
			break;
		case 't':
			options |= OPT_TINY;
			break;
		case 'd':
			/*
			 * Set to set WRAM as a single continuous block as on
			 * DMG. All WRAM sections must be WRAM0 as bankable WRAM
			 * sections do not exist in this mode. A WRAMX section
			 * will raise an error. VRAM bank 1 can't be used if
			 * this option is enabled either.
			 *
			 * This option implies OPT_CONTWRAM.
			 */
			options |= OPT_DMG_MODE;
			/* FALLTHROUGH */
		case 'w':
			/*
			 * Set to set WRAM as a single continuous block as on
			 * DMG. All WRAM sections must be WRAM0 as bankable WRAM
			 * sections do not exist in this mode. A WRAMX section
			 * will raise an error.
			 */
			options |= OPT_CONTWRAM;
			break;
		case 'V':
			printf("rgblink %s\n", get_package_version_string());
			exit(0);
		default:
			print_usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc == 0)
		print_usage();

	for (int32_t i = 0; i < argc; ++i)
		obj_Readfile(argv[i]);

	AddNeededModules();
	AssignSections();
	CreateSymbolTable();
	Patch();
	Output();
	CloseMapfile();

	return 0;
}