RoomFile: preliminary support for roomfiles >= kRoomVersion_350
[rofl0r-agsutils.git] / agsalphahack.c
blobf1c8fbdbedd885c2819e65f94a3b11b7c7d66380
1 #define _GNU_SOURCE
2 #include "DataFile.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include "version.h"
7 #define ADS ":::AGSalphahack " VERSION " by rofl0r:::"
9 static int usage(char *argv0) {
10 dprintf(2, ADS "\nusage:\n%s [-s spriteno] DIR\n"
11 "removes alphachannel flag from specified sprite in game file.\n"
12 "if spriteno is -1 (default), all flags are removed.\n"
13 "gamefile is typically ac2game.dta or game28.dta inside DIR.\n"
14 "this is useful when sprites have been hacked/converted to 16bit.\n"
15 "you should make a backup of the file before doing this.\n"
16 , argv0);
17 return 1;
20 #define SPF_ALPHACHANNEL 0x10
22 int main(int argc, char**argv) {
23 int c, flags = 0, spriteno = -1;
24 while ((c = getopt(argc, argv, "s:")) != EOF) switch(c) {
25 case 's': spriteno = atoi(optarg); break;
26 default: return usage(argv[0]);
28 if(!argv[optind]) return usage(argv[0]);
29 char *dir = argv[optind];
31 ADF a_b, *a = &a_b;
32 char fnbuf[512];
33 if(!ADF_find_datafile(dir, fnbuf, sizeof(fnbuf)))
34 return 1;
35 if(!ADF_open(a, fnbuf)) return 1;
37 off_t off = ADF_get_spriteflagsstart(a);
38 unsigned nsprites = ADF_get_spritecount(a);
39 ADF_close(a);
41 printf("removing alpha of %u out of %u spriteflags.\n", spriteno==-1?nsprites:1, nsprites);
43 FILE *f = fopen(fnbuf, "r+b");
44 if(!f) return 1;
45 fseeko(f, off, SEEK_SET);
46 unsigned char *buf = malloc(nsprites);
47 fread(buf, 1, nsprites, f);
49 size_t i;
50 for(i=0;i<nsprites;++i) {
51 if(spriteno == -1 || spriteno == i)
52 buf[i] = buf[i] & (~SPF_ALPHACHANNEL);
54 fseeko(f, off, SEEK_SET);
55 fwrite(buf, 1, nsprites, f);
56 fclose(f);
57 free(buf);
58 return 0;