~ubuntu-branches/ubuntu/wily/gargoyle-free/wily-proposed

« back to all changes in this revision

Viewing changes to terps/magnetic/Generic/gfxlink.c

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Beucler
  • Date: 2009-09-11 20:09:43 UTC
  • Revision ID: james.westby@ubuntu.com-20090911200943-idgzoyupq6650zpn
Tags: upstream-2009-08-25
ImportĀ upstreamĀ versionĀ 2009-08-25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#define PRGNAME "gfxlink"
 
2
#define PRGVERSION "1.5"
 
3
 
 
4
/*
 
5
   gfxlink.c
 
6
 
 
7
   - combines Magnetic Scrolls picture files (Amiga format)
 
8
     to gfxfile (compatible with the MAG interpreter)
 
9
   - does not currently work with Collection & Wonderland pictures
 
10
 
 
11
   Author: Paul David Doherty <h0142kdd@rz.hu-berlin.de>
 
12
 
 
13
   v1.0:  12 Feb 1997
 
14
   v1.1:  17 Apr 1997  Amigafied & cleverised
 
15
   v1.2:  19 Apr 1997  itoa() replaced
 
16
   v1.3:  22 Apr 1997  cleaned up; name changed to 'gfxlink'
 
17
   v1.4:   8 May 1997  might even work under UNIX now
 
18
   v1.5:  25 Nov 1997  UNIX fixes supplied by Robert Bihlmeyer
 
19
 */
 
20
 
 
21
#if defined(AZTEC_C) || defined(LATTICE)
 
22
#define AMIGA
 
23
#endif
 
24
 
 
25
#define FILENAMELENGTH 256
 
26
#define MAXPICTURES 35
 
27
 
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <fcntl.h>
 
32
#include <ctype.h>
 
33
#ifdef __TURBOC__
 
34
#include <io.h>
 
35
#include <sys\stat.h>
 
36
#define S_IRUSR S_IREAD
 
37
#define S_IWUSR S_IWRITE
 
38
#endif
 
39
#ifdef __GNUC__
 
40
#include <sys/stat.h>
 
41
#endif
 
42
#ifndef S_IRUSR
 
43
#define S_IRUSR 0400
 
44
#define S_IWUSR 0200
 
45
#endif
 
46
 
 
47
#if defined(__TURBOC__) || defined(__GNUC__) && !defined(__unix__)
 
48
#define O_SCRIVERE O_WRONLY|O_BINARY
 
49
#define O_LEGGERE O_RDONLY|O_BINARY
 
50
#else
 
51
#define O_SCRIVERE O_WRONLY
 
52
#define O_LEGGERE O_RDONLY
 
53
#endif
 
54
 
 
55
#define TRUE 1
 
56
#define FALSE 0
 
57
 
 
58
#ifdef __TURBOC__
 
59
#define DIRSEP "\\"
 
60
#else
 
61
#define DIRSEP "/"
 
62
#endif
 
63
 
 
64
#include <limits.h>
 
65
 
 
66
#if UCHAR_MAX==0xff
 
67
typedef unsigned char type8;
 
68
#else
 
69
#error "Can't find an 8-bit integer type"
 
70
#endif
 
71
 
 
72
#if SHRT_MAX==0x7fff
 
73
typedef unsigned short type16;
 
74
#elif INT_MAX==0x7fff
 
75
typedef unsigned int type16;
 
76
#else
 
77
#error "Can't find a 16-bit integer type"
 
78
#endif
 
79
 
 
80
#if INT_MAX==0x7fffffff
 
81
typedef unsigned int type32;
 
82
#elif LONG_MAX==0x7fffffff
 
83
typedef unsigned long type32;
 
84
#else
 
85
#error "Can't find a 32-bit integer type"
 
86
#endif
 
87
 
 
88
int fdi, fdo_temp, fdo_gfx;
 
89
char infilemask[FILENAMELENGTH];
 
90
char infile[FILENAMELENGTH];
 
91
char gfxfile[FILENAMELENGTH];
 
92
char picnum[5];
 
93
 
 
94
#ifdef AMIGA
 
95
#define TEMPFILE "ram:pics.tmp"
 
96
#else
 
97
#define TEMPFILE "pics.tmp"
 
98
#endif
 
99
 
 
100
char *magic = "MaPi";
 
101
 
 
102
type8 inputbuf[256];
 
103
type8 endfile_reached = FALSE;
 
104
type8 offsetcounter = 1;
 
105
type8 detected;
 
106
type16 buflength;
 
107
type32 offset[MAXPICTURES + 2];
 
108
type32 addoffset;
 
109
type32 partlength;
 
110
type32 alllength;
 
111
 
 
112
struct lookuptab
 
113
  {
 
114
    char *game;
 
115
    char *filemask;
 
116
    type16 par[10];
 
117
  }
 
118
lookup[] =
 
119
{
 
120
  "The Pawn", "Pawn",
 
121
    1, 15, 17, 21, 23, 25, 27, 30, 32, 34,
 
122
    "The Pawn", "pawn",
 
123
    1, 15, 17, 21, 23, 25, 27, 30, 32, 34,
 
124
    "The Pawn", "ThePawn/Pawn",
 
125
    1, 15, 17, 21, 23, 25, 27, 30, 32, 34,
 
126
    "The Guild of Thieves", "guild",
 
127
    4, 32, 0, 0, 0, 0, 0, 0, 0, 0,
 
128
    "The Guild of Thieves", "The Guild Of Thieves/guild",
 
129
    4, 32, 0, 0, 0, 0, 0, 0, 0, 0,
 
130
    "Jinxter", "j",
 
131
    3, 30, 73, 73, 0, 0, 0, 0, 0, 0,
 
132
    "Corruption", "c",
 
133
    16, 37, 41, 41, 38, 40, 0, 0, 0, 0,
 
134
    "Fish!", "f",
 
135
    14, 40, 0, 0, 0, 0, 0, 0, 0, 0,
 
136
    "Myth", "m",
 
137
    16, 19, 0, 0, 0, 0, 0, 0, 0, 0
 
138
};
 
139
 
 
140
void
 
141
write_32 (type32 numb)
 
142
{
 
143
  inputbuf[3] = numb & 0xff;
 
144
  numb >>= 8;
 
145
  inputbuf[2] = numb & 0xff;
 
146
  numb >>= 8;
 
147
  inputbuf[1] = numb & 0xff;
 
148
  numb >>= 8;
 
149
  inputbuf[0] = numb;
 
150
  write (fdo_gfx, inputbuf, 4);
 
151
}
 
152
 
 
153
void
 
154
ex (char *error)
 
155
{
 
156
  fprintf (stderr, PRGNAME ": %s\n", error);
 
157
  close (fdi);
 
158
  close (fdo_temp);
 
159
  close (fdo_gfx);
 
160
  remove (TEMPFILE);
 
161
  exit (1);
 
162
}
 
163
 
 
164
void
 
165
itoa_krns (type16 n, char s[])
 
166
{
 
167
  int k, l, m;
 
168
  k = 0;
 
169
  do
 
170
    {
 
171
      s[k++] = n % 10 + '0';
 
172
    }
 
173
  while ((n /= 10) > 0);
 
174
  s[k] = '\0';
 
175
  for (k = 0, l = strlen (s) - 1; k < l; k++, l--)
 
176
    {
 
177
      m = s[k];
 
178
      s[k] = s[l];
 
179
      s[l] = m;
 
180
    }
 
181
}
 
182
 
 
183
void
 
184
kick (type16 picnumber)
 
185
{
 
186
  itoa_krns (picnumber, picnum);
 
187
  strcpy (infile, infilemask);
 
188
  strcat (infile, picnum);
 
189
 
 
190
  if ((fdi = open (infile, O_LEGGERE, 0)) == -1)
 
191
    {
 
192
      printf ("File not found: %s\n", infile);
 
193
      ex ("picture file(s) missing; can't create gfxfile");
 
194
    }
 
195
  else
 
196
    {
 
197
      endfile_reached = FALSE;
 
198
      partlength = 0;
 
199
      while (endfile_reached == FALSE)
 
200
        {
 
201
          buflength = read (fdi, inputbuf, 256);
 
202
          if (buflength != 256)
 
203
            endfile_reached = TRUE;
 
204
          write (fdo_temp, inputbuf, (int) buflength);
 
205
          partlength = partlength + buflength;
 
206
        }
 
207
      offset[offsetcounter] = offset[offsetcounter - 1] + partlength;
 
208
      offsetcounter++;
 
209
    }
 
210
  close (fdi);
 
211
}
 
212
 
 
213
void
 
214
usage (void)
 
215
{
 
216
  fprintf (stderr, PRGNAME " v" PRGVERSION ": ");
 
217
  fprintf (stderr, "creates Magnetic Scrolls graphics file.\n");
 
218
  fprintf (stderr, "(c) 1997 by Paul David Doherty <h0142kdd@rz.hu-berlin.de>\n\n");
 
219
  fprintf (stderr, "Usage: " PRGNAME " [path] gfxfile\n");
 
220
  fprintf (stderr, "  e.g. " PRGNAME " df0: pawn.gfx\n");
 
221
  exit (1);
 
222
}
 
223
 
 
224
int
 
225
main (int argc, char **argv)
 
226
{
 
227
  int i, j;
 
228
 
 
229
  if ((argc < 2) || (argc > 3))
 
230
    usage ();
 
231
 
 
232
  strcpy (gfxfile, argv[(argc == 2) ? 1 : 2]);
 
233
  if (argc == 3)
 
234
    {
 
235
      strcpy (infilemask, argv[1]);
 
236
      if ((infilemask[strlen (infilemask) - 1] != ':')
 
237
          && (infilemask[strlen (infilemask) - 1] != '/')
 
238
          && (infilemask[strlen (infilemask) - 1] != '\\'))
 
239
        strcat (infilemask, DIRSEP);
 
240
    }
 
241
 
 
242
/* detection */
 
243
  for (i = 0; i < (sizeof (lookup) / sizeof (struct lookuptab)); i++)
 
244
    {
 
245
      itoa_krns (lookup[i].par[1], picnum);
 
246
      strcpy (infile, infilemask);
 
247
      strcat (infile, lookup[i].filemask);
 
248
      strcat (infile, picnum);
 
249
      if ((fdi = open (infile, O_LEGGERE, 0)) != -1)
 
250
        break;
 
251
    }
 
252
  detected = i;
 
253
  if (detected >= (sizeof (lookup) / sizeof (struct lookuptab)))
 
254
      ex ("no picture files detected");
 
255
  close (fdi);
 
256
  printf ("Game detected: %s\n", lookup[detected].game);
 
257
 
 
258
/* generation of temp file & offsets */
 
259
  if ((fdo_temp = open (TEMPFILE,
 
260
                  O_SCRIVERE | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1)
 
261
    ex ("could not create temp file");
 
262
 
 
263
  offset[0] = 0;
 
264
  strcat (infilemask, lookup[detected].filemask);
 
265
  for (i = 0; i <= 8; i = i + 2)
 
266
    for (j = lookup[detected].par[i]; j <= lookup[detected].par[i + 1]; j++)
 
267
      if (j != 0)
 
268
        kick ((type16) j);
 
269
  close (fdo_temp);
 
270
  offsetcounter--;
 
271
  printf ("Number of pictures: %d\n", offsetcounter);
 
272
 
 
273
/* writing offsets into gfx file */
 
274
  if ((fdo_gfx = open (gfxfile,
 
275
                  O_SCRIVERE | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) == -1)
 
276
    ex ("could not create output file");
 
277
 
 
278
  addoffset = (((offsetcounter + 2) * 4) + strlen (magic));
 
279
  for (i = 0; i < offsetcounter; i++)
 
280
    offset[i] = offset[i] + addoffset;
 
281
  alllength = offset[offsetcounter] + addoffset;
 
282
  offset[offsetcounter] = 0;
 
283
  write (fdo_gfx, magic, strlen (magic));
 
284
  write_32 (alllength);
 
285
  for (i = 0; i <= offsetcounter; i++)
 
286
    write_32 (offset[i]);
 
287
 
 
288
/* copying tempfile into gfx file */
 
289
  if ((fdi = open (TEMPFILE, O_LEGGERE, 0)) == -1)
 
290
    ex ("could not reopen temp file");
 
291
 
 
292
  endfile_reached = FALSE;
 
293
  while (endfile_reached == FALSE)
 
294
    {
 
295
      buflength = read (fdi, inputbuf, 256);
 
296
      if (buflength != 256)
 
297
        endfile_reached = TRUE;
 
298
      write (fdo_gfx, inputbuf, (int) buflength);
 
299
    }
 
300
  close (fdi);
 
301
  close (fdo_gfx);
 
302
  if (remove (TEMPFILE) != 0)
 
303
    ex ("couldn't delete temp file");
 
304
  return 0;
 
305
}