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

« back to all changes in this revision

Viewing changes to terps/scare/os_ansi.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
/* vi: set ts=2 shiftwidth=2 expandtab:
 
2
 *
 
3
 * Copyright (C) 2003-2008  Simon Baldwin and Mark J. Tilford
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of version 2 of the GNU General Public License
 
7
 * as published by the Free Software Foundation.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 
17
 * USA
 
18
 */
 
19
 
 
20
/*
 
21
 * Module notes:
 
22
 *
 
23
 * o This module represents just about the simplest platform input/output
 
24
 *   code possible for SCARE.  Actually, it could be simplified still further
 
25
 *   by abandoning attempts to word wrap at 78 columns of text, and by
 
26
 *   ignoring all tags altogether, though this may stop some games playing
 
27
 *   quite like they should.
 
28
 *
 
29
 * o Feel free to use this code as a starting point for a platform port.
 
30
 */
 
31
 
 
32
#include <assert.h>
 
33
#include <ctype.h>
 
34
#include <errno.h>
 
35
#include <stdio.h>
 
36
#include <stdlib.h>
 
37
#include <string.h>
 
38
 
 
39
#include "scare.h"
 
40
 
 
41
enum { FALSE = 0, TRUE = !FALSE };
 
42
 
 
43
static sc_char line_buffer[79];
 
44
static sc_int line_length = 0;
 
45
 
 
46
static const sc_char *game_file;
 
47
static sc_game game;
 
48
 
 
49
 
 
50
/*
 
51
 * full_flush()
 
52
 * partial_flush()
 
53
 * append_character()
 
54
 */
 
55
static void
 
56
full_flush (void)
 
57
{
 
58
  if (line_length > 0)
 
59
    {
 
60
      fwrite (line_buffer, 1, line_length, stdout);
 
61
      line_length = 0;
 
62
    }
 
63
  fflush (stdout);
 
64
}
 
65
 
 
66
static void
 
67
partial_flush (void)
 
68
{
 
69
  if (line_length > 0)
 
70
    {
 
71
      const sc_char *line_break;
 
72
 
 
73
      line_buffer[line_length] = '\0';
 
74
      line_break = strrchr (line_buffer, ' ');
 
75
      if (line_break)
 
76
        {
 
77
          fwrite (line_buffer, 1, line_break - line_buffer, stdout);
 
78
          memmove (line_buffer, line_break + 1, strlen (line_break) + 1);
 
79
          line_length = strlen (line_buffer);
 
80
        }
 
81
      else
 
82
        full_flush ();
 
83
    }
 
84
  fflush (stdout);
 
85
}
 
86
 
 
87
static void
 
88
append_character (sc_char c)
 
89
{
 
90
  if (c == '\n')
 
91
    {
 
92
      full_flush ();
 
93
      putchar ('\n');
 
94
    }
 
95
  else
 
96
    {
 
97
      line_buffer[line_length++] = c;
 
98
      if (line_length >= (sc_int) sizeof (line_buffer) - 1)
 
99
        {
 
100
          partial_flush ();
 
101
          putchar ('\n');
 
102
        }
 
103
    }
 
104
}
 
105
 
 
106
 
 
107
/*
 
108
 * os_print_tag()
 
109
 * os_print_string()
 
110
 * os_print_string_debug()
 
111
 */
 
112
void
 
113
os_print_tag (sc_int tag, const sc_char *argument)
 
114
{
 
115
  sc_int index_;
 
116
  const sc_char *unused;
 
117
  unused = argument;
 
118
 
 
119
  switch (tag)
 
120
    {
 
121
    case SC_TAG_CLS:
 
122
      for (index_ = 0; index_ < 25; index_++)
 
123
        append_character ('\n');
 
124
      break;
 
125
 
 
126
    case SC_TAG_CENTER:
 
127
    case SC_TAG_RIGHT:
 
128
    case SC_TAG_ENDCENTER:
 
129
    case SC_TAG_ENDRIGHT:
 
130
      if (line_length > 0)
 
131
        append_character ('\n');
 
132
      break;
 
133
 
 
134
    case SC_TAG_WAITKEY:
 
135
      {
 
136
        sc_char dummy[256];
 
137
        full_flush ();
 
138
        if (!feof (stdin))
 
139
          fgets (dummy, sizeof (dummy), stdin);
 
140
        break;
 
141
      }
 
142
    }
 
143
}
 
144
 
 
145
void
 
146
os_print_string (const sc_char *string)
 
147
{
 
148
  sc_int index_;
 
149
 
 
150
  for (index_ = 0; string[index_] != '\0'; index_++)
 
151
    {
 
152
      if (string[index_] == '\t')
 
153
        os_print_string ("        ");
 
154
      else
 
155
        append_character (string[index_]);
 
156
    }
 
157
}
 
158
 
 
159
void
 
160
os_print_string_debug (const sc_char *string)
 
161
{
 
162
  os_print_string (string);
 
163
}
 
164
 
 
165
 
 
166
/*
 
167
 * os_play_sound()
 
168
 * os_stop_sound()
 
169
 * os_show_graphic()
 
170
 */
 
171
void
 
172
os_play_sound (const sc_char *filepath,
 
173
               sc_int offset, sc_int length, sc_bool is_looping)
 
174
{
 
175
  const sc_char *unused1;
 
176
  sc_int unused2, unused3;
 
177
  sc_bool unused4;
 
178
  unused1 = filepath;
 
179
  unused2 = offset;
 
180
  unused3 = length;
 
181
  unused4 = is_looping;
 
182
}
 
183
 
 
184
void
 
185
os_stop_sound (void)
 
186
{
 
187
}
 
188
 
 
189
void
 
190
os_show_graphic (const sc_char *filepath, sc_int offset, sc_int length)
 
191
{
 
192
#ifdef LINUX_GRAPHICS
 
193
  const sc_char *unused1;
 
194
  unused1 = filepath;
 
195
 
 
196
  if (length > 0 && strlen (game_file) < 768)
 
197
    {
 
198
      sc_char buffer[1024];
 
199
 
 
200
      sprintf (buffer,
 
201
               "dd if=%s ibs=1c skip=%ld count=%ld obs=100k"
 
202
               " of=/tmp/scare.jpg 2>/dev/null", game_file, offset, length);
 
203
      system (buffer);
 
204
      system ("xv /tmp/scare.jpg >/dev/null 2>&1 &");
 
205
      system ("( sleep 10; rm /tmp/scare.jpg ) >/dev/null 2>&1 &");
 
206
    }
 
207
#else
 
208
  const sc_char *unused1;
 
209
  sc_int unused2, unused3;
 
210
  unused1 = filepath;
 
211
  unused2 = offset;
 
212
  unused3 = length;
 
213
#endif
 
214
}
 
215
 
 
216
 
 
217
/*
 
218
 * os_read_line()
 
219
 * os_read_line_debug()
 
220
 */
 
221
sc_bool
 
222
os_read_line (sc_char *buffer, sc_int length)
 
223
{
 
224
  full_flush ();
 
225
  if (feof (stdin))
 
226
    sc_quit_game (game);
 
227
 
 
228
  putchar ('>');
 
229
  fflush (stdout);
 
230
  fgets (buffer, length, stdin);
 
231
  return TRUE;
 
232
}
 
233
 
 
234
sc_bool
 
235
os_read_line_debug (sc_char *buffer, sc_int length)
 
236
{
 
237
  full_flush ();
 
238
  if (feof (stdin))
 
239
    sc_quit_game (game);
 
240
 
 
241
  printf ("[SCARE debug]");
 
242
  return os_read_line (buffer, length);
 
243
}
 
244
 
 
245
 
 
246
/*
 
247
 * os_confirm()
 
248
 */
 
249
sc_bool
 
250
os_confirm (sc_int type)
 
251
{
 
252
  sc_char buffer[256];
 
253
 
 
254
  if (type == SC_CONF_SAVE)
 
255
    return TRUE;
 
256
 
 
257
  full_flush ();
 
258
  if (feof (stdin))
 
259
    return type == SC_CONF_QUIT;
 
260
 
 
261
  do
 
262
    {
 
263
      printf ("Do you really want to ");
 
264
      switch (type)
 
265
        {
 
266
        case SC_CONF_QUIT:
 
267
          printf ("quit");
 
268
          break;
 
269
        case SC_CONF_RESTART:
 
270
          printf ("restart");
 
271
          break;
 
272
        case SC_CONF_RESTORE:
 
273
          printf ("restore");
 
274
          break;
 
275
        case SC_CONF_VIEW_HINTS:
 
276
          printf ("view hints");
 
277
          break;
 
278
        default:
 
279
          printf ("do that");
 
280
          break;
 
281
        }
 
282
      printf ("? [Y/N] ");
 
283
      fflush (stdout);
 
284
      fgets (buffer, sizeof (buffer), stdin);
 
285
    }
 
286
  while (toupper (buffer[0]) != 'Y' && toupper (buffer[0]) != 'N');
 
287
 
 
288
  return toupper (buffer[0]) == 'Y';
 
289
}
 
290
 
 
291
 
 
292
/*
 
293
 * os_open_file()
 
294
 * os_read_file()
 
295
 * os_write_file()
 
296
 * os_close_file()
 
297
 */
 
298
void *
 
299
os_open_file (sc_bool is_save)
 
300
{
 
301
  sc_char path[256];
 
302
  FILE *stream;
 
303
 
 
304
  full_flush ();
 
305
  if (feof (stdin))
 
306
    return NULL;
 
307
 
 
308
  printf ("Enter saved game to %s: ", is_save ? "save" : "load");
 
309
  fflush (stdout);
 
310
  fgets (path, sizeof (path), stdin);
 
311
  if (path[strlen (path) - 1] == '\n')
 
312
    path[strlen (path) - 1] = '\0';
 
313
 
 
314
  if (is_save)
 
315
    {
 
316
      stream = fopen (path, "rb");
 
317
      if (stream)
 
318
        {
 
319
          fclose (stream);
 
320
          printf ("File already exists.\n");
 
321
          return NULL;
 
322
        }
 
323
      stream = fopen (path, "wb");
 
324
    }
 
325
  else
 
326
    stream = fopen (path, "rb");
 
327
 
 
328
  if (!stream)
 
329
    {
 
330
      printf ("Error opening file.\n");
 
331
      return NULL;
 
332
    }
 
333
  return stream;
 
334
}
 
335
 
 
336
sc_int
 
337
os_read_file (void *opaque, sc_byte *buffer, sc_int length)
 
338
{
 
339
  FILE *stream = (FILE *) opaque;
 
340
  sc_int bytes;
 
341
 
 
342
  bytes = fread (buffer, 1, length, stream);
 
343
  if (ferror (stream))
 
344
    fprintf (stderr, "Read error: %s\n", strerror (errno));
 
345
 
 
346
  return bytes;
 
347
}
 
348
 
 
349
void
 
350
os_write_file (void *opaque, const sc_byte *buffer, sc_int length)
 
351
{
 
352
  FILE *stream = (FILE *) opaque;
 
353
 
 
354
  fwrite (buffer, 1, length, stream);
 
355
  if (ferror (stream))
 
356
    fprintf (stderr, "Write error: %s\n", strerror (errno));
 
357
}
 
358
 
 
359
void
 
360
os_close_file (void *opaque)
 
361
{
 
362
  FILE *stream = (FILE *) opaque;
 
363
 
 
364
  fclose (stream);
 
365
}
 
366
 
 
367
 
 
368
/*
 
369
 * os_display_hints()
 
370
 */
 
371
void
 
372
os_display_hints (sc_game game_)
 
373
{
 
374
  sc_game_hint hint;
 
375
  assert (game_ == game);
 
376
 
 
377
  full_flush ();
 
378
  for (hint = sc_get_first_game_hint (game);
 
379
       hint; hint = sc_get_next_game_hint (game, hint))
 
380
    {
 
381
      const sc_char *hint_text;
 
382
 
 
383
      printf ("%s\n", sc_get_game_hint_question (game, hint));
 
384
 
 
385
      hint_text = sc_get_game_subtle_hint (game, hint);
 
386
      if (hint_text)
 
387
        printf ("- %s\n", hint_text);
 
388
 
 
389
      hint_text = sc_get_game_unsubtle_hint (game, hint);
 
390
      if (hint_text)
 
391
        printf ("- %s\n", hint_text);
 
392
    }
 
393
}
 
394
 
 
395
 
 
396
/*
 
397
 * main()
 
398
 */
 
399
int
 
400
main (int argc, const char *argv[])
 
401
{
 
402
  FILE *stream;
 
403
  const char *trace_flags, *locale;
 
404
  assert (argc > 0 && argv);
 
405
 
 
406
  if (argc != 2)
 
407
    {
 
408
      fprintf (stderr, "Usage: %s taf_file\n", argv[0]);
 
409
      return EXIT_FAILURE;
 
410
    }
 
411
 
 
412
  stream = fopen (argv[1], "rb");
 
413
  if (!stream)
 
414
    {
 
415
      fprintf (stderr, "%s: %s: %s\n", argv[0], argv[1], strerror (errno));
 
416
      return EXIT_FAILURE;
 
417
    }
 
418
 
 
419
  trace_flags = getenv ("SC_TRACE_FLAGS");
 
420
  if (trace_flags)
 
421
    sc_set_trace_flags (strtoul (trace_flags, NULL, 0));
 
422
 
 
423
  locale = getenv ("SC_LOCALE");
 
424
  if (locale)
 
425
    sc_set_locale (locale);
 
426
 
 
427
  printf ("Loading game...\n");
 
428
  game = sc_game_from_stream (stream);
 
429
  if (!game)
 
430
    {
 
431
      fprintf (stderr,
 
432
               "%s: %s: Not a loadable Adrift game\n", argv[0], argv[1]);
 
433
      fclose (stream);
 
434
      return EXIT_FAILURE;
 
435
    }
 
436
  fclose (stream);
 
437
 
 
438
  if (getenv ("SC_DEBUGGER_ENABLED"))
 
439
    sc_set_game_debugger_enabled (game, TRUE);
 
440
  if (getenv ("SC_STABLE_RANDOM_ENABLED"))
 
441
    {
 
442
      sc_set_portable_random (TRUE);
 
443
      sc_reseed_random_sequence (1);
 
444
    }
 
445
 
 
446
  game_file = argv[1];
 
447
 
 
448
  sc_interpret_game (game);
 
449
  sc_free_game (game);
 
450
  return EXIT_SUCCESS;
 
451
}