Debian Codegraph @

terps/hugo/heglk/hemedia.c

raw symbols SCIP
1 /*
2 HEMEDIA.C
3
4 Glk/Gargoyle interface functions for the Hugo Engine
5
6 Copyright (c) 1999-2004 by Kent Tessman
7
8 Graphics support added by Simon Baldwin
9 Sound support added by Tor Andersson
10 Gargoyle hacks added by Tor Andersson
11 */
12
13 /* SOUND & GRAPHICS
14
15 The tack taken here is to try to build a blorb set of resources for
16 each Hugo sound and picture available on the first call.
17
18 Thereafter, each resource requested is translated into a blorb
19 resource number, and this one is used to play the sound/show the picture.
20
21 There are two options to do resources for Glk (Gargoyle).
22 A is guaranteed to work. B is not portable, but works for gargoyle
23 and probably most of the other non-PDA glks.
24
25 a) put each resource in a standard named filed (PIC%d or SND%d)
26 b) build an actual BLORB file
27
28 Because Hugo resources live in many files, and there is no way to
29 know which ones they are before it's too late...
30 I go with option A although it does tend to litter a lot of files...
31 A smarter way would be to create the Blorb file, then recreate it every
32 time more resources are discovered...
33
34 I really wish Glk had a smarter resource management facility than Blorb,
35 or that Hugo would use Blorb instead.
36
37 Update as of 2024-02-24:
38
39 There is a new Glk extension GLK_MODULE_GARGLK_FILE_RESOURCES which
40 loads resources from arbitrary files. This allows multimedia to be
41 loaded without littering the filesystem with PIC and SND files. If this
42 extension is available, it will be used; otherwise, option A as
43 described above is used.
44 */
45
46 #define GRAPHICS_SUPPORTED
47 #define SOUND_SUPPORTED
48 #define SETTITLE_SUPPORTED
49
50 #ifdef GLK_MODULE_GARGLK_FILE_RESOURCES
51 #ifdef GLK_MODULE_GARGLKWINSIZE
52 #include <math.h>
53 #endif
54
55 #include "gi_blorb.h"
56 #endif
57
58 void hugo_setgametitle(char *t)
59 {
60 // garglk_set_story_name(t);
61 }
62
63 #define PIC 0
64 #define SND 1
65 #define MAXRES 1024
66
67 static schanid_t mchannel = NULL;
68 static schanid_t schannel = NULL;
69
70 #ifndef GLK_MODULE_GARGLK_FILE_RESOURCES
71 static long resids[2][MAXRES];
72 static int numres[2] = { 0, 0 };
73
74 static int loadres(HUGO_FILE infile, int reslen, int type)
75 {
76 char buf[4096];
77 frefid_t fileref;
78 strid_t stream;
79 long offset;
80 int id;
81 int i, n;
82
83 offset = ftell(infile);
84 for (i = 0; i < numres[type]; i++)
85 if (resids[type][i] == offset)
86 return i;
87
88 /* Too many resources loaded... */
89 if (numres[type] + 1 == MAXRES)
90 return -1;
91
92 id = numres[type]++;
93 sprintf(buf, "%s%d", type == PIC ? "PIC" : "SND", id);
94 resids[type][id] = offset;
95
96 fileref = glk_fileref_create_by_name(fileusage_Data, buf, 0);
97 if (!fileref)
98 {
99 return -1;
100 }
101
102 stream = glk_stream_open_file(fileref, filemode_Write, 0);
103 if (!stream)
104 {
105 glk_fileref_destroy(fileref);
106 return -1;
107 }
108
109 glk_fileref_destroy(fileref);
110
111 while (reslen > 0)
112 {
113 n = fread(buf, 1, reslen < sizeof buf ? reslen : sizeof buf, infile);
114 if (n <= 0)
115 break;
116 glk_put_buffer_stream(stream, buf, n);
117 reslen -= n;
118 }
119
120 glk_stream_close(stream, NULL);
121
122 return id;
123 }
124 #endif
125
126 int hugo_hasgraphics(void)
127 {
128 /* Returns true if the current display is capable of graphics display */
129 return glk_gestalt(gestalt_Graphics, 0)
130 && glk_gestalt(gestalt_DrawImage, glk_window_get_type(mainwin));
131 }
132
133 int hugo_displaypicture(HUGO_FILE infile, long reslen)
134 {
135 #ifndef GLK_MODULE_GARGLK_FILE_RESOURCES
136 int id;
137 #endif
138
139 /* Ignore the call if the current window is set elsewhere. */
140 if (currentwin != NULL && currentwin != mainwin)
141 {
142 fclose(infile);
143 return false;
144 }
145
146 #ifdef GLK_MODULE_GARGLK_FILE_RESOURCES
147 long offset = ftell(infile);
148 fclose(infile);
149
150 glui32 id = garglk_add_resource_from_file(giblorb_ID_Pict, loaded_filename, offset, reslen);
151 if (id == 0) {
152 return false;
153 }
154
155 #ifdef GLK_MODULE_GARGLKWINSIZE
156 glui32 imagewidth, imageheight;
157 if (glk_image_get_info(id, &imagewidth, &imageheight)) {
158 glui32 winwidth, winheight;
159 garglk_window_get_size_pixels(mainwin, &winwidth, &winheight);
160
161 if (imagewidth > winwidth || imageheight > winheight) {
162 double scalex = (double)winwidth / imagewidth;
163 double scaley = (double)winheight / imageheight;
164
165 double scale = scalex < scaley ? scalex : scaley;
166
167 imagewidth = round(imagewidth * scale);
168 imageheight = round(imageheight * scale);
169
170 if (glk_image_draw_scaled(mainwin, id, imagealign_InlineUp, 0, imagewidth, imageheight)) {
171 glk_put_char('\n');
172 }
173
174 return true;
175 }
176 }
177 #endif
178
179 if (glk_image_draw(mainwin, id, imagealign_InlineUp, 0)) {
180 glk_put_char('\n');
181 }
182
183 return true;
184 #else
185 id = loadres(infile, reslen, PIC);
186 if (id < 0)
187 {
188 fclose(infile);
189 return false;
190 }
191
192 #if 0
193 /* Get picture width and height for scaling. */
194 glui32 width, height;
195 if (glk_image_get_info(id, &width, &height))
196 {
197 /* Scale large images. */
198 if (width > PIC_MAX_WIDTH)
199 {
200 height = height * PIC_MAX_WIDTH / width;
201 width = PIC_MAX_WIDTH;
202 }
203 if (height > PIC_MAX_HEIGHT)
204 {
205 width = width * PIC_MAX_HEIGHT / height;
206 height = PIC_MAX_HEIGHT;
207 }
208 }
209 #endif
210
211 fclose(infile);
212
213 /* Draw, then move cursor down to the next line. */
214 glk_image_draw(mainwin, id, imagealign_InlineUp, 0);
215 glk_put_char('\n');
216
217 return true;
218 #endif
219 }
220
221 void initsound()
222 {
223 if (!glk_gestalt(gestalt_Sound, 0))
224 return;
225 schannel = glk_schannel_create(0);
226 }
227
228 void initmusic()
229 {
230 if (!glk_gestalt(gestalt_Sound, 0) || !glk_gestalt(gestalt_SoundMusic, 0))
231 return;
232 mchannel = glk_schannel_create(0);
233 }
234
235 static int hugo_play(HUGO_FILE infile, long reslen, char loop_flag, int is_music)
236 {
237 if (is_music && !mchannel)
238 initmusic();
239
240 schanid_t channel = is_music ? mchannel : schannel;
241 if (channel)
242 {
243 #ifdef GLK_MODULE_GARGLK_FILE_RESOURCES
244 long offset = ftell(infile);
245 glui32 id = garglk_add_resource_from_file(giblorb_ID_Snd, loaded_filename, ftell(infile), reslen);
246 if (id == 0)
247 #else
248 int id = loadres(infile, reslen, SND);
249 if (id < 0)
250 #endif
251 {
252 fclose(infile);
253 return false;
254 }
255 glk_schannel_play_ext(channel, id, loop_flag ? -1 : 1, 0);
256 }
257
258 fclose(infile);
259 return true;
260 }
261
262 int hugo_playmusic(HUGO_FILE infile, long reslen, char loop_flag)
263 {
264 return hugo_play(infile, reslen, loop_flag, true);
265 }
266
267 void hugo_musicvolume(int vol)
268 {
269 if (!mchannel) initmusic();
270 if (!mchannel) return;
271 glk_schannel_set_volume(mchannel, (vol * 0x10000) / 100);
272 }
273
274 void hugo_stopmusic(void)
275 {
276 if (!mchannel) initmusic();
277 if (!mchannel) return;
278 glk_schannel_stop(mchannel);
279 }
280
281 int hugo_playsample(HUGO_FILE infile, long reslen, char loop_flag)
282 {
283 return hugo_play(infile, reslen, loop_flag, false);
284 }
285
286 void hugo_samplevolume(int vol)
287 {
288 if (!schannel) initsound();
289 if (!schannel) return;
290 glk_schannel_set_volume(schannel, (vol * 0x10000) / 100);
291 }
292
293 void hugo_stopsample(void)
294 {
295 if (!schannel) initsound();
296 if (!schannel) return;
297 glk_schannel_stop(schannel);
298 }
299