File: ux_pic.c

package info (click to toggle)
frotz 2.40-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 468 kB
  • ctags: 891
  • sloc: ansic: 6,774; makefile: 143; sh: 63
file content (344 lines) | stat: -rw-r--r-- 9,413 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * ux_pic.c - Unix interface, picture outline functions
 *
 * This file is part of Frotz.
 *
 * Frotz is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Frotz is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#define __UNIX_PORT_FILE

#include <stdlib.h>
#include <string.h>

#ifdef USE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif

#include "frotz.h"
#include "ux_frotz.h"

#define PIC_FILE_HEADER_FLAGS 1
#define PIC_FILE_HEADER_NUM_IMAGES 4
#define PIC_FILE_HEADER_ENTRY_SIZE 8
#define PIC_FILE_HEADER_VERSION 14

#define PIC_HEADER_NUMBER 0
#define PIC_HEADER_WIDTH 2
#define PIC_HEADER_HEIGHT 4

static void safe_mvaddch(int, int, int);
static void save_scrnset(int, int, int, int);

static struct {
  int z_num;
  int width;
  int height;
  int orig_width;
  int orig_height;
} *pict_info;
static int num_pictures = 0;

static unsigned char lookupb(unsigned char *p, int n)
{
  return p[n];
}

static unsigned short lookupw(unsigned char *p, int n)
{
  return (p[n + 1] << 8) | p[n];
}

/*
 * Do a rounding division, rounding to even if fraction part is 1/2.
 * We assume x and y are nonnegative.
 *
 */
static int round_div(int x, int y)
{
	int quotient = x / y;
	int dblremain = (x % y) << 1;

	if ((dblremain > y) || (dblremain == y) && (quotient & 1))
		quotient++;
	return quotient;
}


bool unix_init_pictures (void)
{
  FILE *file = NULL;
  int success = FALSE;
  unsigned char gheader[16];
  unsigned char *raw_info = NULL;

  char *filename;
  const char *basename, *dotpos;
  int namelen;

  if ((filename = malloc(2 * strlen(story_name) + 10)) == NULL)
    return FALSE;

  basename = strrchr(story_name, '/');
  if (basename) basename++; else basename = story_name;
  dotpos = strrchr(basename, '.');
  namelen = (dotpos ? dotpos - basename : strlen(basename));
  sprintf(filename, "%.*sgraphics/%.*s.mg1",
          basename - story_name, story_name, namelen, basename);

  do {
    int i, entry_size, flags, x_scale, y_scale;

    if (((file = fopen (filename, "rb")) == NULL)
	|| (fread(&gheader, sizeof (gheader), 1, file) != 1))
      break;

    num_pictures = lookupw(gheader, PIC_FILE_HEADER_NUM_IMAGES);
    entry_size = lookupb(gheader, PIC_FILE_HEADER_ENTRY_SIZE);
    flags = lookupb(gheader, PIC_FILE_HEADER_FLAGS);

    raw_info = malloc(num_pictures * entry_size);

    if (fread(raw_info, num_pictures * entry_size, 1, file) != 1)
      break;

    pict_info = malloc((num_pictures + 1) * sizeof(*pict_info));
    pict_info[0].z_num = 0;
    pict_info[0].height = num_pictures;
    pict_info[0].width = lookupw(gheader, PIC_FILE_HEADER_VERSION);

    y_scale = 200;
    x_scale = (flags & 0x08) ? 640 : 320;

    /* Copy and scale.  */
    for (i = 1; i <= num_pictures; i++) {
      unsigned char *p = raw_info + entry_size * (i - 1);
      int height, width;
      pict_info[i].z_num = lookupw(p, PIC_HEADER_NUMBER);
      pict_info[i].orig_height = lookupw(p, PIC_HEADER_HEIGHT);
      pict_info[i].orig_width = lookupw(p, PIC_HEADER_WIDTH);

      pict_info[i].height = round_div(pict_info[i].orig_height *
		h_screen_rows, y_scale);
      pict_info[i].width = round_div(pict_info[i].orig_width *
		h_screen_cols, x_scale);
    }
    success = TRUE;
  } while (0);
  if (file)
    fclose(file);
  if (raw_info)
    free(raw_info);
  return success;
}

/* Convert a Z picture number to an index into pict_info.  */
static int z_num_to_index(int n)
{
  int i;
  for (i = 0; i <= num_pictures; i++)
    if (pict_info[i].z_num == n)
      return i;
  return -1;
}

/*
 * os_picture_data
 *
 * Return true if the given picture is available. If so, write the
 * width and height of the picture into the appropriate variables.
 * Only when picture 0 is asked for, write the number of available
 * pictures and the release number instead.
 *
 */
int os_picture_data(int num, int *height, int *width)
{
  int index;

  *height = 0;
  *width = 0;

  if (!pict_info)
    return FALSE;

  if ((index = z_num_to_index(num)) == -1)
    return FALSE;

  *height = pict_info[index].height;
  *width = pict_info[index].width;

  return TRUE;
}

/* 
 * Do a mvaddch if the coordinates aren't too large.
 *
 */
static void safe_mvaddch(int y, int x, int ch)
{
	if ((y < h_screen_rows) && (x < h_screen_cols))
		mvaddch(y, x, ch);
}

/*
 * Set n chars starting at (x, y), doing bounds checking.
 *
 */
static void safe_scrnset(int y, int x, int ch, int n)
{
	if ((y < h_screen_rows) && (x < h_screen_cols)) {
		move(y, x);
		if (x + n > h_screen_cols)
			n = h_screen_cols - x;
		while (n--)
			addch(ch);
	}
}

/*
 * os_draw_picture
 *
 * Display a picture at the given coordinates. Top left is (1,1).
 *
 */

/* TODO: handle truncation correctly.  Spec 8.8.3 says all graphics should
 * be clipped to the current window.  To do that, we should probably
 * modify z_draw_picture in the frotz core to pass some extra parameters.
 */

void os_draw_picture (int num, int row, int col)
{
  int width, height, r, c;
  int saved_x, saved_y;
  static int plus, ltee, rtee, ttee, btee, hline, vline, ckboard;
  static int urcorner, ulcorner, llcorner, lrcorner;
  static bool acs_initialized = FALSE;

  if (!acs_initialized) {
    plus     = unix_plain_ascii ? '+'  : ACS_PLUS;
    ltee     = unix_plain_ascii ? '<'  : ACS_LTEE;
    rtee     = unix_plain_ascii ? '>'  : ACS_RTEE;
    ttee     = unix_plain_ascii ? '^'  : ACS_TTEE;
    btee     = unix_plain_ascii ? 'v'  : ACS_BTEE;
    hline    = unix_plain_ascii ? '-'  : ACS_HLINE;
    vline    = unix_plain_ascii ? '|'  : ACS_VLINE;
    ckboard  = unix_plain_ascii ? ':'  : ACS_CKBOARD;
    urcorner = unix_plain_ascii ? '\\' : ACS_URCORNER;
    ulcorner = unix_plain_ascii ? '/'  : ACS_ULCORNER;
    llcorner = unix_plain_ascii ? '\\' : ACS_LLCORNER;
    lrcorner = unix_plain_ascii ? '/'  : ACS_LRCORNER;
    acs_initialized = TRUE;
  }

  if (!os_picture_data(num, &height, &width) || !width || !height)
    return;
  col--, row--;

  getyx(stdscr, saved_y, saved_x);

  /* General case looks like:     
   *                            /----\
   *                            |::::|
   *                            |::42|
   *                            \----/
   *
   * Special cases are:  1 x n:   n x 1:   1 x 1:
   *
   *                                ^
   *                                |
   *                     <----->    |        +
   *                                |
   *                                v
   */






  if ((height == 1) && (width == 1))
    safe_mvaddch(row, col, plus);
  else if (height == 1) {
    safe_mvaddch(row, col, ltee);
    safe_scrnset(row, col + 1, hline, width - 2);
    safe_mvaddch(row, col + width - 1, rtee);
  } else if (width == 1) {
    safe_mvaddch(row, col, ttee);
    for (r = row + 1; r < row + height - 1; r++)
      safe_mvaddch(r, col, vline);
    safe_mvaddch(row + height - 1, col, btee);
  } else {
    safe_mvaddch(row, col, ulcorner);
    safe_scrnset(row, col + 1, hline, width - 2);
    safe_mvaddch(row, col + width - 1, urcorner);
    for (r = row + 1; r < row + height - 1; r++) {
      safe_mvaddch(r, col, vline);
      safe_scrnset(r, col + 1, ckboard, width - 2);
      safe_mvaddch(r, col + width - 1, vline);
    }
    safe_mvaddch(row + height - 1, col, llcorner);
    safe_scrnset(row + height - 1, col + 1, hline, width - 2);
    safe_mvaddch(row + height - 1, col + width - 1, lrcorner);
  }

  /* Picture number.  */
  if (height > 2) {
    for (c = col + width - 2; c > col && num > 0; c--, (num /= 10))
      safe_mvaddch(row + height - 2, c, '0' + num % 10);
  }

  move(saved_y, saved_x);
}

/*
 * os_peek_colour
 *
 * Return the colour of the pixel below the cursor. This is used
 * by V6 games to print text on top of pictures. The coulor need
 * not be in the standard set of Z-machine colours. To handle
 * this situation, Frotz extends the colour scheme: Values above
 * 15 (and below 256) may be used by the interface to refer to
 * non-standard colours. Of course, os_set_colour must be able to
 * deal with these colours. Interfaces which refer to characters
 * instead of pixels might return the current background colour
 * instead.
 *
 */

int os_peek_colour (void)
{
  if (color_enabled) {
#ifdef COLOR_SUPPORT
    short fg, bg;
    pair_content(PAIR_NUMBER(inch() & A_COLOR), &fg, &bg);
    switch(bg) {
          case COLOR_BLACK: return BLACK_COLOUR;
          case COLOR_RED: return RED_COLOUR;
          case COLOR_GREEN: return GREEN_COLOUR;
          case COLOR_YELLOW: return YELLOW_COLOUR;
          case COLOR_BLUE: return BLUE_COLOUR;
          case COLOR_MAGENTA: return MAGENTA_COLOUR;
          case COLOR_CYAN: return CYAN_COLOUR;
          case COLOR_WHITE: return WHITE_COLOUR;
    }
    return 0;
#endif /* COLOR_SUPPORT */
  } else {
    return (inch() & A_REVERSE) ? h_default_foreground : h_default_background;
  }	
}