1#!/usr/local/bin/perl
2#
3# creates images.c and images.h.  Takes the imagedir as a parameter.
4#
5# Bill Dyess, 10/5/94
6#
7# Bill Dyess, 2/11/95
8#  - Now always compiles in default.x[pb]m.
9#  - Compiles in all image files if -c given as a parameter.
10#
11require "find.pl";
12
13$path = $ARGV[0];
14if ($path eq "-c") {
15  $compile_in = 1;
16  $path = $ARGV[1];
17} else {
18  $compile_in = 0;
19}
20
21die "Usage: $0 [-c] <imagedir>\n  -c    Compile in all images.\n" unless $path;
22
23&find($path);
24
25&init_lookup();
26
27open(C,">images.c") || die "can't open images.c: $!";
28open(H,">images.h") || die "can't open images.h: $!";
29open(SCRIPT_IN,"$0") || die "can't open $0: $!";
30open(SCRIPT_OUT,">$0.out") || die "can't open $0.out: $!";
31while(<SCRIPT_IN>) {
32  print SCRIPT_OUT;
33  last if /^  %lookup = \(/;
34}
35close(SCRIPT_IN);
36
37$help = "/* {width,height,frames,xpm,filename,loaded,alternate,bad,compiled_in,xbmdata,xpmdata,pixmap,clipmask} */\n";
38
39print C <<EOF;
40/* images.c.  Contains the image control data. [BDyess] */
41/* automatically created by scripts/mkimgsrc */
42
43#include <stdlib.h>
44#include "Wlib.h"
45#include "images.h"
46#include "struct.h"
47#include "proto.h"
48
49/* compiled in images in alphabetical order, XBM before XPM. [BDyess] */
50EOF
51
52# sort the filelist
53@filelist = sort keys %table;
54
55# copy in all the xbm and xpm data if $compile_in is set, otherwise just
56# the data for the default image.
57chdir($path) || die "can't chdir() to $path: $!";
58if($compile_in) {
59  for $file (@filelist) {
60    &include_bitmap($file);
61    &include_pixmap($file);
62  }
63} else {
64  &include_bitmap("/default");
65}
66
67# includes the given bitmap in the C source.
68sub include_bitmap {
69  local($file) = @_;
70  $ok = 0;
71  if(open(IN,".$file.xbm")) {
72    while(<IN>) {
73      if (/^#define\s*\S*_width\s*(\d*)/) {
74	$lookup{"${file}_width"} = $1;
75	next;
76      }
77      if (/^#define\s*\S*_height\s*(\d*)/) {
78	$lookup{"${file}_height"} = $1;
79	next;
80      }
81      if (/^static.*char\s\S*_bits\[\]/) {
82        $name = $file;
83	$name =~ s,^/,,;
84	$name =~ s,[/.],_,g;
85        print C "static char ${name}_bits[] = {\n";
86	$lookup{"${file}_xbmdata"} = "${name}_bits";
87	$ok = 1;
88	next;
89      }
90      print C;
91    }
92    print C "\n";
93    if($ok) {
94      $lookup{"${file}_compiled_in"} = 1;
95    } else {
96      print STDERR "Error parsing $file.xbm!\n";
97    }
98  }
99}
100
101# includes the given pixmap in the C source.
102sub include_pixmap {
103  local($file) = @_;
104  $ok = 0;
105  if(open(IN,".$file.xpm")) {
106    while(<IN>) {
107      if (/static.*char[\s\*]*\S*\[\]/) {
108        $name = $file;
109	$name =~ s,^/,,;
110	$name =~ s,[/.],_,g;
111	s/\*\s*\S*\[\]/* ${name}_xpm\[\]/;
112        print C;
113	$lookup{"${file}_xpmdata"} = "${name}_xpm";
114        $ok = 1;
115      } else {
116	print C;
117      }
118    }
119    print C "\n";
120    if($ok) {
121      $lookup{"${file}_compiled_in"} = 1;
122    } else {
123      print STDERR "Error parsing $file.xpm!\n";
124    }
125  }
126}
127
128# start of imagearray
129#$height = $lookup{"/default_height"};
130#$width = $lookup{"/default_width"};
131print C <<EOF;
132/* keep sorted (for bsearch) [BDyess] */
133static W_Image imagearray[] = {
134EOF
135#   {$height, $width, 1, 0, "/default", 1, I_DEFAULT, 0, 1, default_bits, default_xpm, 0, 0},
136
137
138print H <<EOF;
139/* images.h.  Contains the #defines and extern's for all the image management
140   data [BDyess] */
141/* automatically created by scripts/mkimgsrc */
142
143#include "Wlib.h"
144
145W_Image * getImage P((int offset));
146void loadImageByFilename P((char *filename));
147void loadAllImages P((void));
148
149#define I_DEFAULT              0
150EOF
151#
152
153$counter = 0; #1;
154for $file (@filelist) {
155  &print_lookup($file);
156  print C $help if ($counter % 25) == 0;
157  $frames = $lookup{"${file}_frames"};
158  $frames = 0 unless $frames;
159  $alternate = $lookup{"${file}_alternate"};
160  $alternate = "I_DEFAULT" unless $alternate;
161  $width = $lookup{"${file}_width"};
162  $width = 0 unless $width;
163  $height = $lookup{"${file}_height"};
164  $height = 0 unless $height;
165  $is_compiled_in = $lookup{"${file}_compiled_in"};
166  $is_compiled_in = 0 unless $is_compiled_in;
167  $xbmdata = $lookup{"${file}_xbmdata"};
168  $xbmdata = "NULL" unless $xbmdata;
169  $xpmdata = $lookup{"${file}_xpmdata"};
170  if($xpmdata) {
171    $xpmdata = "(char**)$xpmdata";
172  } else {
173    $xpmdata = "NULL" unless $xpmdata;
174  }
175  print C <<EOF;
176   {$width, $height, $frames, 0, "$file", 0, $alternate, 0, $is_compiled_in, $xbmdata, $xpmdata, 0, 0},
177EOF
178  $file =~ s,^/,,;
179  $file =~ s,[./],_,g;
180  $file = "\U$file";
181  $file =~ s/^(WEAPONS|PLANETS|INTRO)_//;
182  printf(H "#define I_%-20s $counter\n",$file);
183  $counter++;
184}
185
186$counter--;
187printf(H "#define I_%-20s $counter\n",LAST);
188
189print C <<EOF;
190};
191
192W_Image *
193getImage(offset)
194  int offset;
195{
196  W_Image * image = &imagearray[offset];
197  if(!image->loaded) {
198    W_LoadImage(image);
199  }
200  return image;
201}
202
203#ifndef __STDC__
204#define const
205#endif /*__STDC__*/
206
207int cmpfilenames(left,right)
208  const void *left, *right;
209{
210  return strcmp((char*)left,((W_Image*)right)->filename);
211}
212
213void
214loadImageByFilename(filename)
215  char *filename;
216{
217  W_Image *image;
218
219  image = bsearch(filename, imagearray, sizeof(imagearray) / sizeof(W_Image),
220                  sizeof(W_Image), cmpfilenames);
221  if(image && !image->loaded) W_LoadImage(image);
222}
223
224void
225loadAllImages()
226{
227  int i;
228  for(i=I_DEFAULT; i<I_LAST; i++)
229    if(!imagearray[i].loaded)
230      W_LoadImage(&imagearray[i]);
231}
232EOF
233
234
235close C;
236close H;
237
238print SCRIPT_OUT ");\n}\n";
239close SCRIPT_OUT;
240unlink("$0.bak");
241rename("$0","$0.bak") || print STDERR "Error renaming $0 to $0.bak: $!";
242rename("$0.out","$0") || print STDERR "Error renaming $0.out to $0: $!";
243chmod 0755, "$0";
244
245sub wanted {
246  return unless /\.x[bp]m$/;
247  $x = $name;
248  $x =~ s/^$path//;
249  $x =~ s/\.x[bp]m$//;
250  return unless -f "$_";
251  $table{$x} = 1;
252}
253
254sub print_lookup {
255  local($name) = @_;
256  local($alternate,$frames);
257  print SCRIPT_OUT ",\n" if $notfirst;
258  $notfirst = 1;
259  $alternate = $lookup{"${name}_alternate"};
260  $alternate = 'I_DEFAULT' unless $alternate;
261  $frames = $lookup{"${name}_frames"};
262  printf(SCRIPT_OUT "%-40s \'$alternate\'","'${file}_alternate',");
263  printf(SCRIPT_OUT ",\n%-40s '$frames'","'${file}_frames',") if $frames;
264}
265
266# lookup is a table to lookup values that are different than 0 or filename.
267# <path>_frames and <path>_alternate are searched for and used.
268#
269# lookup is generated automatically when mkimgsrc is run and saved to this
270# file.  If it's a new image, I_DEFAULT is used.
271sub init_lookup {
272  %lookup = (
273'/al_alternate',                         'I_DEFAULT',
274'/alien1_alternate',                     'I_DEFAULT',
275'/alien10_alternate',                    'I_DEFAULT',
276'/alien11_alternate',                    'I_DEFAULT',
277'/alien12_alternate',                    'I_DEFAULT',
278'/alien13_alternate',                    'I_DEFAULT',
279'/alien14_alternate',                    'I_DEFAULT',
280'/alien15_alternate',                    'I_DEFAULT',
281'/alien16_alternate',                    'I_DEFAULT',
282'/alien17_alternate',                    'I_DEFAULT',
283'/alien2_alternate',                     'I_DEFAULT',
284'/alien3_alternate',                     'I_DEFAULT',
285'/alien4_alternate',                     'I_DEFAULT',
286'/alien5_alternate',                     'I_DEFAULT',
287'/alien6_alternate',                     'I_DEFAULT',
288'/alien7_alternate',                     'I_DEFAULT',
289'/alien8_alternate',                     'I_DEFAULT',
290'/alien9_alternate',                     'I_DEFAULT',
291'/etorp_alternate',                      'I_DEFAULT',
292'/explosion_alternate',                  'I_DEFAULT',
293'/extra_alternate',                      'I_DEFAULT',
294'/miniship_alternate',                   'I_DEFAULT',
295'/mtorp_alternate',                      'I_DEFAULT',
296'/pause_alternate',                      'I_DEFAULT',
297'/player1_alternate',                    'I_DEFAULT',
298'/player2_alternate',                    'I_DEFAULT',
299'/player3_alternate',                    'I_DEFAULT',
300'/pr_blank_alternate',                   'I_DEFAULT',
301'/pr_brain_alternate',                   'I_DEFAULT',
302'/pr_doub_alternate',                    'I_DEFAULT',
303'/pr_extrabullet_alternate',             'I_DEFAULT',
304'/pr_lemon_alternate',                   'I_DEFAULT',
305'/pr_shield_alternate',                  'I_DEFAULT',
306'/pr_sing_alternate',                    'I_DEFAULT',
307'/pr_speed_alternate',                   'I_DEFAULT',
308'/pr_trip_alternate',                    'I_DEFAULT',
309'/s1000_alternate',                      'I_DEFAULT',
310'/s2000_alternate',                      'I_DEFAULT',
311'/s4000_alternate',                      'I_DEFAULT',
312'/s500_alternate',                       'I_DEFAULT',
313'/shield_alternate',                     'I_DEFAULT',
314'/title_alternate',                      'I_DEFAULT');
315}
316