Debian Codegraph @

Descent3/hotspotmap.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/hotspotmap.cpp $
21 * $Revision: 19 $
22 * $Date: 4/19/00 5:07p $
23 * $Author: Matt $
24 *
25 * Contains the code for the hotspotmap functions (used in TelCom)
26 *
27 * $Log: /DescentIII/main/hotspotmap.cpp $
28 *
29 * 19 4/19/00 5:07p Matt
30 * From Duane for 1.4
31 * Added checks, asserts, and fixes for bad return values
32 *
33 * 18 4/17/99 6:15p Samir
34 * replaced gr.h with grdefs.h and fixed resulting compile bugs.
35 *
36 * 17 4/14/99 2:50a Jeff
37 * fixed some case mismatched #includes
38 *
39 * 16 1/21/99 11:15p Jeff
40 * pulled out some structs and defines from header files and moved them
41 * into seperate header files so that multiplayer dlls don't require major
42 * game headers, just those new headers. Side effect is a shorter build
43 * time. Also cleaned up some header file #includes that weren't needed.
44 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
45 * manage.h and multi.h
46 *
47 * 15 10/15/98 1:36p Jeff
48 * fixed bug vc6 caught
49 *
50 * 14 10/08/98 4:23p Kevin
51 * Changed code to comply with memory library usage. Always use mem_malloc
52 * , mem_free and mem_strdup
53 *
54 * 13 8/15/98 2:49p Matt
55 * Took out unneeded include
56 *
57 * 12 4/23/98 6:38p Jason
58 * made bitmaps use 1555 format
59 *
60 * 11 3/23/98 9:55a Jeff
61 * Made changes to remove old telcom
62 *
63 * 10 3/18/98 8:00p Jeff
64 * Added some functionality for text and using the monitor borders
65 * correctly in the new Telcom
66 *
67 * 9 3/12/98 3:32p Jeff
68 * Initial changes started for New TelCom
69 *
70 * 8 2/08/98 5:02p Samir
71 * Commented out use of Game_viewport code.
72 *
73 * 7 1/15/98 11:13a Jeff
74 * Got Telcom working again, switched from 2d to 3d
75 *
76 * 6 10/15/97 5:20p Jason
77 * did a HUGE overhaul of the bitmap system
78 *
79 * 5 8/15/97 12:24p Jeff
80 * Changed things around a bit, added code to handle the round corners
81 *
82 * 4 8/13/97 3:13p Jeff
83 * optimized for memory usage (removed the second background from being
84 * stored in memory)
85 *
86 * 3 8/04/97 7:23p Jeff
87 *
88 * 2 8/04/97 6:23p Jeff
89 *
90 * 1 8/04/97 6:23p Jeff
91 *
92 * 2 8/01/97 2:36p Jeff
93 *
94 * 1 8/01/97 2:35p Jeff
95 *
96 * 2 8/01/97 2:35p Jeff
97 *
98 * $NoKeywords: $
99 */
100
101 #include "mono.h"
102 #include "renderer.h"
103 #include "render.h"
104 #include "grdefs.h"
105 #include "ddio.h"
106 #include "descent.h"
107 #include "game.h"
108 #include "cfile.h"
109 #include "application.h"
110 #include <stdlib.h>
111 #include <string.h>
112 #include "hotspotmap.h"
113 #include "mem.h"
114 #include "bitmap.h"
115 #include "manage.h"
116
117 static void makecorner(int corner_bmp, int back_bmp, const char *tmap, int l, int t, int r, int b);
118 static int CreateHotSpotMap(const char *map, int width, int height, hotspotmap_t *hsmap);
119 static void CreateWindowMap(const char *map, int width, int height, windowmap_t *wndmap);
120
121 // just like the old bm_tga_translate_pixel function, but it stores the alpha in the alpha_value parameter
122 uint16_t menutga_translate_pixel(int pixel, char *alpha_value) {
123 int red = ((pixel >> 16) & 0xFF);
124 int green = ((pixel >> 8) & 0xFF);
125 int blue = ((pixel) & 0xFF);
126 int alpha = ((pixel >> 24) & 0xFF);
127
128 *alpha_value = alpha;
129
130 int newred = red >> 3;
131 int newgreen = green >> 3;
132 int newblue = blue >> 3;
133 uint16_t newpix = OPAQUE_FLAG | (newred << 10) | (newgreen << 5) | (newblue);
134 if (alpha == 0)
135 newpix = NEW_TRANSPARENT_COLOR;
136
137 return newpix;
138 }
139
140 // This takes a char array filled with alphavalues from the TGA and fills in a hotspotmap_t struct with the
141 // scanline info
142 // returns the number of windows it found while making the hotspot map
143 #define HOTSPOT_THERE 1
144 #define WINDOW_THERE 2
145 #define NOTHING_THERE 0
146
147 int CreateHotSpotMap(const char *map, int width, int height, hotspotmap_t *hsmap) {
148 // make sure we have a clean struct to work with
149 if (hsmap->hs) {
150 FreeHotSpotMapInternals(hsmap);
151 }
152
153 int curr_sl, x, y, count, num_hs = 0;
154 uint8_t alpha;
155 char whats_there[256];
156 int window_count;
157
158 for (count = 0; count < 256; count++)
159 whats_there[count] = NOTHING_THERE;
160
161 // Get total number of hotspots (based on highest hotspot value)
162 for (y = 0; y < height; y++) {
163 for (x = 0; x < width; x++) {
164 alpha = (unsigned)map[y * width + x];
165 if ((alpha != NO_ALPHA) && (alpha != WRITEABLE_ALPHA) && (whats_there[alpha] == NOTHING_THERE)) {
166 if (alpha < MAX_HOTSPOTS) {
167 whats_there[alpha] = HOTSPOT_THERE;
168 if (alpha >= num_hs)
169 num_hs = alpha + 1;
170 } else {
171 whats_there[alpha] = WINDOW_THERE;
172 }
173 }
174 }
175 }
176
177 hsmap->num_of_hotspots = num_hs;
178 if (!num_hs)
179 return -1;
180
181 hsmap->hs = (hotspot *)mem_malloc(sizeof(hotspot) * num_hs);
182 ASSERT(hsmap->hs);
183 for (count = 0; count < num_hs; count++) {
184 if (whats_there[count] == HOTSPOT_THERE) {
185 hsmap->hs[count].starting_y = MAX_MAP_HEIGHT;
186 hsmap->hs[count].scanlines = 0;
187 } else {
188 hsmap->hs[count].starting_y = 0;
189 hsmap->hs[count].scanlines = 0;
190 }
191 }
192
193 scanline sl_x[MAX_MAP_HEIGHT];
194 int sl_count, last_y = -1;
195
196 // this is to set the values of start and end initially
197 for (count = 0; count < MAX_MAP_HEIGHT; count++) {
198 sl_x[count].start = MAX_MAP_WIDTH;
199 sl_x[count].end = 0;
200 }
201
202 // fill in hotspot info
203 // this is really nasty...MUST MAKE BETTER
204 for (count = 0; count < num_hs; count++) {
205 if (whats_there[count] == HOTSPOT_THERE) {
206 sl_count = 0;
207 // Get start,end, scanlines and starting_y for each hotspot
208 for (y = 0; y < height; y++) {
209 for (x = 0; x < width; x++) {
210 if ((unsigned)map[y * width + x] == (uint8_t)count) {
211 if (last_y != y)
212 sl_count++;
213 if (hsmap->hs[count].starting_y > y)
214 hsmap->hs[count].starting_y = y;
215 if (sl_x[y].start > x)
216 sl_x[y].start = x;
217 if (sl_x[y].end < x)
218 sl_x[y].end = x;
219 last_y = y;
220 }
221 }
222 }
223 // fill in the struct with the start and end values for each scanline
224 hsmap->hs[count].scanlines = sl_count;
225 if (sl_count) {
226 hsmap->hs[count].x = (scanline *)mem_malloc(sizeof(scanline) * sl_count);
227 ASSERT(hsmap->hs[count].x);
228 memset(hsmap->hs[count].x, 0, sizeof(scanline) * sl_count);
229 y = hsmap->hs[count].starting_y;
230 for (curr_sl = 0; curr_sl < sl_count; curr_sl++) {
231 hsmap->hs[count].x[curr_sl].start = sl_x[y].start;
232 hsmap->hs[count].x[curr_sl].end = sl_x[y].end;
233 sl_x[y].start = MAX_MAP_WIDTH;
234 sl_x[y].end = 0;
235 y++;
236 } // end for (curr_sl)
237 } // end if (sl_count)
238 } else {
239 hsmap->hs[count].scanlines = 0;
240 hsmap->hs[count].starting_y = 0;
241 hsmap->hs[count].x = NULL;
242 } // end ifelse (hotspot_there)
243 } // end for (count)
244
245 mprintf(0, "Hunting down empty hotspots....\n");
246 for (count = 0; count < num_hs; count++) {
247 if (whats_there[count] == NOTHING_THERE) {
248 mprintf(0, "Notice: HotSpot %d is empty\n", count);
249 hsmap->hs[count].scanlines = 0;
250 hsmap->hs[count].starting_y = 0;
251 if (hsmap->hs[count].x) {
252 mem_free(hsmap->hs[count].x);
253 }
254 hsmap->hs[count].x = NULL;
255 }
256 }
257
258 mprintf(0, "Finding window count...");
259 window_count = 0;
260 for (count = 0; count < 256; count++) {
261 if (whats_there[count] == WINDOW_THERE)
262 window_count++;
263 }
264 mprintf(0, "%d windows found\n", window_count);
265 return window_count;
266 }
267
268 void CreateWindowMap(const char *map, int width, int height, windowmap_t *wndmap) {
269 mprintf(0, "Processing %d windows\n", wndmap->num_of_windows);
270 int x, y, count;
271 uint8_t alpha;
272 bool newline = true;
273
274 wndmap->wm = (window_box *)mem_malloc(sizeof(window_box) * wndmap->num_of_windows);
275 ASSERT(wndmap->wm);
276
277 for (int index = 0; index < wndmap->num_of_windows; index++) {
278 wndmap->wm[index].lt = NULL;
279 wndmap->wm[index].rt = NULL;
280 wndmap->wm[index].lb = NULL;
281 wndmap->wm[index].rb = NULL;
282 }
283
284 for (count = 0; count < wndmap->num_of_windows; count++) {
285 wndmap->wm[count].x = wndmap->wm[count].y = wndmap->wm[count].width = wndmap->wm[count].height = -1;
286 wndmap->wm[count].t_top_y = wndmap->wm[count].b_top_y = -1;
287 wndmap->wm[count].t_bottom_y = wndmap->wm[count].b_bottom_y = 0;
288 wndmap->wm[count].l_start_x = wndmap->wm[count].r_start_x = MAX_MAP_WIDTH;
289 wndmap->wm[count].l_end_x = wndmap->wm[count].r_end_x = 0;
290 wndmap->wm[count].on_left = wndmap->wm[count].on_top = true;
291 }
292
293 int working_window = -1;
294
295 for (y = 0; y < height; y++) {
296 newline = true;
297 working_window = -1;
298 for (x = 0; x < width; x++) {
299 alpha = (unsigned)map[y * width + x];
300 if (alpha != 255) {
301 if (alpha >= 130)
302 alpha = alpha;
303 }
304 if ((alpha >= MAX_HOTSPOTS) && (alpha != NO_ALPHA)) {
305 // set the working_window value (needed so we know which window to work with for the writeable
306 if (alpha != WRITEABLE_ALPHA) {
307 working_window = alpha - MAX_HOTSPOTS;
308 }
309
310 if (newline) {
311 int count;
312 for (count = 0; count < wndmap->num_of_windows; count++)
313 wndmap->wm[count].on_left = true;
314 newline = false;
315 }
316
317 // Set an initial value for x and y of a window
318 if ((wndmap->wm[working_window].x == -1) && (alpha != WRITEABLE_ALPHA)) {
319 wndmap->wm[working_window].x = x;
320 wndmap->wm[working_window].y = y;
321 }
322
323 // Set an initial value for the write_x and write_y for a window
324 if ((alpha == WRITEABLE_ALPHA) && (working_window >= 0)) {
325 if (wndmap->wm[working_window].t_top_y != -1) {
326 if (wndmap->wm[working_window].l_end_x < x - 1) {
327 wndmap->wm[working_window].on_left = false;
328 } else {
329 wndmap->wm[working_window].on_left = true;
330 }
331
332 if (wndmap->wm[working_window].t_bottom_y < y - 1) {
333 wndmap->wm[working_window].on_top = false;
334 } else {
335 wndmap->wm[working_window].on_top = true;
336 }
337 } else {
338 wndmap->wm[working_window].on_left = true;
339 wndmap->wm[working_window].on_top = true;
340 }
341
342 // first time init
343 if ((wndmap->wm[working_window].t_top_y == -1) && (wndmap->wm[working_window].on_top)) {
344 wndmap->wm[working_window].t_top_y = y;
345 wndmap->wm[working_window].t_bottom_y = y;
346 }
347 if ((wndmap->wm[working_window].b_top_y == -1) && (!wndmap->wm[working_window].on_top)) {
348 wndmap->wm[working_window].b_top_y = y;
349 wndmap->wm[working_window].b_bottom_y = y;
350 }
351
352 // adjust bottom
353 if (wndmap->wm[working_window].on_top) {
354 if (wndmap->wm[working_window].t_bottom_y < y) {
355 wndmap->wm[working_window].t_bottom_y = y;
356 }
357 } else {
358 if (wndmap->wm[working_window].b_bottom_y < y) {
359 wndmap->wm[working_window].b_bottom_y = y;
360 }
361 }
362
363 // adjust edges
364 if (wndmap->wm[working_window].on_left) {
365 if (wndmap->wm[working_window].l_start_x > x) {
366 wndmap->wm[working_window].l_start_x = x;
367 }
368 if (wndmap->wm[working_window].l_end_x < x) {
369 wndmap->wm[working_window].l_end_x = x;
370 }
371 } else {
372 if (wndmap->wm[working_window].r_start_x > x) {
373 wndmap->wm[working_window].r_start_x = x;
374 }
375 if (wndmap->wm[working_window].r_end_x < x) {
376 wndmap->wm[working_window].r_end_x = x;
377 }
378 }
379 }
380
381 if (alpha != WRITEABLE_ALPHA) {
382 // adjust width and height
383 if ((wndmap->wm[working_window].x != -1) && (x > wndmap->wm[working_window].x) &&
384 (x - wndmap->wm[working_window].x > wndmap->wm[working_window].width)) {
385 wndmap->wm[working_window].width = x - wndmap->wm[working_window].x;
386 }
387
388 if ((y - wndmap->wm[working_window].y != -1) && (y > y - wndmap->wm[working_window].y) &&
389 (y - wndmap->wm[working_window].y > wndmap->wm[working_window].height)) {
390 wndmap->wm[working_window].height = y - wndmap->wm[working_window].y;
391 }
392 }
393 } // end if
394 } // end for x
395 } // end for y
396
397 for (count = 0; count < wndmap->num_of_windows; count++) {
398 int h, w;
399 int x, y, real_x, real_y;
400 int *left_x, *right_x, *top_y, *bottom_y;
401 char *array;
402 uint8_t alpha;
403
404 // left top
405 left_x = &wndmap->wm[count].l_start_x;
406 right_x = &wndmap->wm[count].l_end_x;
407 top_y = &wndmap->wm[count].t_top_y;
408 bottom_y = &wndmap->wm[count].t_bottom_y;
409
410 if (*top_y != -1) {
411 w = *right_x - *left_x;
412 h = *bottom_y - *top_y;
413 wndmap->wm[count].lt = (char *)mem_malloc(w * h);
414 array = wndmap->wm[count].lt;
415 for (real_y = *top_y, y = 0; real_y < *bottom_y; real_y++, y++) {
416 for (real_x = *left_x, x = 0; real_x < *right_x; real_x++, x++) {
417 alpha = (unsigned)map[real_y * width + real_x];
418 if (alpha == WRITEABLE_ALPHA) {
419 array[y * w + x] = true;
420 } else {
421 array[y * w + x] = false;
422 }
423 }
424 }
425
426 // right top
427 left_x = &wndmap->wm[count].r_start_x;
428 right_x = &wndmap->wm[count].r_end_x;
429 top_y = &wndmap->wm[count].t_top_y;
430 bottom_y = &wndmap->wm[count].t_bottom_y;
431
432 w = *right_x - *left_x;
433 h = *bottom_y - *top_y;
434 wndmap->wm[count].rt = (char *)mem_malloc(w * h);
435
436 array = wndmap->wm[count].rt;
437 for (real_y = *top_y, y = 0; real_y < *bottom_y; real_y++, y++) {
438 for (real_x = *left_x, x = 0; real_x < *right_x; real_x++, x++) {
439 alpha = (unsigned)map[real_y * width + real_x];
440 if (alpha == WRITEABLE_ALPHA) {
441 array[y * w + x] = true;
442 } else {
443 array[y * w + x] = false;
444 }
445 }
446 }
447 }
448
449 // left bottom
450 left_x = &wndmap->wm[count].l_start_x;
451 right_x = &wndmap->wm[count].l_end_x;
452 top_y = &wndmap->wm[count].b_top_y;
453 bottom_y = &wndmap->wm[count].b_bottom_y;
454
455 if (*top_y != -1) {
456 w = *right_x - *left_x;
457 h = *bottom_y - *top_y;
458 wndmap->wm[count].lb = (char *)mem_malloc(w * h);
459 array = wndmap->wm[count].lb;
460
461 for (real_y = *top_y, y = 0; real_y < *bottom_y; real_y++, y++) {
462 for (real_x = *left_x, x = 0; real_x < *right_x; real_x++, x++) {
463 alpha = (unsigned)map[real_y * width + real_x];
464 if (alpha == WRITEABLE_ALPHA) {
465 array[y * w + x] = true;
466 } else {
467 array[y * w + x] = false;
468 }
469 }
470 }
471
472 // right bottom
473 left_x = &wndmap->wm[count].r_start_x;
474 right_x = &wndmap->wm[count].r_end_x;
475 top_y = &wndmap->wm[count].b_top_y;
476 bottom_y = &wndmap->wm[count].b_bottom_y;
477
478 w = *right_x - *left_x;
479 h = *bottom_y - *top_y;
480 wndmap->wm[count].rb = (char *)mem_malloc(w * h);
481 array = wndmap->wm[count].rb;
482 for (real_y = *top_y, y = 0; real_y < *bottom_y; real_y++, y++) {
483 for (real_x = *left_x, x = 0; real_x < *right_x; real_x++, x++) {
484 alpha = (unsigned)map[real_y * width + real_x];
485 if (alpha == WRITEABLE_ALPHA) {
486 array[y * w + x] = true;
487 } else {
488 array[y * w + x] = false;
489 }
490 }
491 }
492 }
493 }
494
495 for (count = 0; count < wndmap->num_of_windows; count++) {
496 mprintf(0, "Window #%d: Left/Top=(%d,%d) Width=%d Height=%d\n", count, wndmap->wm[count].x, wndmap->wm[count].y,
497 wndmap->wm[count].width, wndmap->wm[count].height);
498 mprintf(0, "---L.T. (%d,%d)->(%d,%d)\n", wndmap->wm[count].l_start_x, wndmap->wm[count].t_top_y,
499 wndmap->wm[count].l_end_x, wndmap->wm[count].t_bottom_y);
500 mprintf(0, "---R.T. (%d,%d)->(%d,%d)\n", wndmap->wm[count].r_start_x, wndmap->wm[count].t_top_y,
501 wndmap->wm[count].r_end_x, wndmap->wm[count].t_bottom_y);
502 mprintf(0, "---L.B. (%d,%d)->(%d,%d)\n", wndmap->wm[count].l_start_x, wndmap->wm[count].b_top_y,
503 wndmap->wm[count].l_end_x, wndmap->wm[count].b_bottom_y);
504 mprintf(0, "---R.B. (%d,%d)->(%d,%d)\n", wndmap->wm[count].r_start_x, wndmap->wm[count].b_top_y,
505 wndmap->wm[count].r_end_x, wndmap->wm[count].b_bottom_y);
506 }
507 }
508
509 // TODO: MTS: only used in this file
510 // Loads a tga or ogf file into a bitmap...returns handle to bm or -1 on error, and fills in the alphamap
511 int menutga_alloc_file(const char *name, char *hsmap[1], int *w, int *h) {
512 uint8_t image_id_len, color_map_type, image_type, pixsize, descriptor;
513 uint8_t upside_down = 0;
514 uint16_t width, height;
515 uint32_t pixel;
516 int i, t, n;
517 char alphavalue;
518 CFILE *infile;
519
520 if (!cfexist(name))
521 return -1;
522
523 infile = (CFILE *)cfopen(name, "rb");
524 if (!infile) {
525 Int3(); // get jeff!
526 }
527
528 image_id_len = cf_ReadByte(infile);
529 color_map_type = cf_ReadByte(infile);
530 image_type = cf_ReadByte(infile);
531
532 if (color_map_type != 0 || (image_type != 2)) {
533 mprintf(0, "menutga: Can't read this type of TGA.\n");
534 return -1;
535 }
536
537 for (i = 0; i < 9; i++) // ingore next 9 bytes
538 cf_ReadByte(infile);
539
540 *w = width = cf_ReadShort(infile);
541 *h = height = cf_ReadShort(infile);
542 pixsize = cf_ReadByte(infile);
543
544 hsmap[0] = (char *)mem_malloc(width * height);
545
546 ASSERT(hsmap);
547
548 if (pixsize != 32) {
549 mprintf(0, "menutga: This file has a pixsize of field of %d, it should be 32. ", pixsize);
550 return -1;
551 }
552
553 descriptor = cf_ReadByte(infile);
554 if ((descriptor & 0x0F) != 8) {
555 mprintf(0, "menutga: Descriptor field & 0x0F must be 8, but this is %d.", descriptor & 0x0F);
556 return -1;
557 }
558
559 for (i = 0; i < image_id_len; i++)
560 cf_ReadByte(infile);
561
562 n = bm_AllocBitmap(width, height, 0);
563
564 if (n < 0) {
565 mprintf(0, "menutga: Failed to allocate memory.\n");
566 Int3();
567 }
568
569 upside_down = (descriptor & 0x20) >> 5;
570 upside_down = 1 - upside_down;
571
572 // Load the actual bitmap data in, converting it from 32 bit to 16 bit, and replacing
573 // that pesky transparency color without our replacement
574
575 for (i = 0; i < height; i++) {
576 for (t = 0; t < width; t++) {
577 pixel = cf_ReadInt(infile);
578
579 uint16_t newpix = menutga_translate_pixel(pixel, &alphavalue);
580
581 if (upside_down) {
582 GameBitmaps[n].data16[((height - 1) - i) * width + t] = newpix;
583 hsmap[0][((height - 1) - i) * width + t] = alphavalue;
584 } else {
585 GameBitmaps[n].data16[i * width + t] = newpix;
586 hsmap[0][i * width + t] = alphavalue;
587 }
588 }
589 }
590
591 cfclose(infile);
592 return (n);
593 }
594
595 // TODO: MTS: only used in this file
596 // Given a filename and a hotspotmap structure, it saves it to disk (.HSM)
597 void menutga_SaveHotSpotMap(const std::filesystem::path &filename, hotspotmap_t *hsmap, windowmap_t *wndmap) {
598 CFILE *file;
599 mprintf(0, "Saving HotSpotMap %s ", filename.u8string().c_str());
600 file = (CFILE *)cfopen(filename, "wb");
601 if (!file) {
602 Int3(); // get jeff!
603 return;
604 }
605
606 int curr_hs, curr_sl;
607 mprintf(0, "Number of HotSpots=%d\n", hsmap->num_of_hotspots);
608 cf_WriteByte(file, hsmap->num_of_hotspots);
609
610 for (curr_hs = 0; curr_hs < hsmap->num_of_hotspots; curr_hs++) {
611 cf_WriteInt(file, hsmap->hs[curr_hs].starting_y);
612 cf_WriteInt(file, hsmap->hs[curr_hs].scanlines);
613 for (curr_sl = 0; curr_sl < hsmap->hs[curr_hs].scanlines; curr_sl++) {
614 cf_WriteInt(file, hsmap->hs[curr_hs].x[curr_sl].start);
615 cf_WriteInt(file, hsmap->hs[curr_hs].x[curr_sl].end);
616 }
617 }
618
619 int count;
620 int index, size;
621
622 cf_WriteInt(file, wndmap->num_of_windows);
623 for (count = 0; count < wndmap->num_of_windows; count++) {
624 cf_WriteInt(file, wndmap->wm[count].x);
625 cf_WriteInt(file, wndmap->wm[count].y);
626 cf_WriteInt(file, wndmap->wm[count].width);
627 cf_WriteInt(file, wndmap->wm[count].height);
628 cf_WriteInt(file, wndmap->wm[count].l_start_x);
629 cf_WriteInt(file, wndmap->wm[count].l_end_x);
630 cf_WriteInt(file, wndmap->wm[count].r_start_x);
631 cf_WriteInt(file, wndmap->wm[count].r_end_x);
632 cf_WriteInt(file, wndmap->wm[count].t_top_y);
633 cf_WriteInt(file, wndmap->wm[count].t_bottom_y);
634 cf_WriteInt(file, wndmap->wm[count].b_top_y);
635 cf_WriteInt(file, wndmap->wm[count].b_bottom_y);
636
637 if (wndmap->wm[count].t_top_y != -1) {
638 // left top
639 size = ((wndmap->wm[count].l_end_x) - (wndmap->wm[count].l_start_x)) *
640 ((wndmap->wm[count].t_bottom_y) - (wndmap->wm[count].t_top_y));
641 for (index = 0; index < size; index++) {
642 cf_WriteByte(file, wndmap->wm[count].lt[index]);
643 }
644 // right top
645 size = ((wndmap->wm[count].r_end_x) - (wndmap->wm[count].r_start_x)) *
646 ((wndmap->wm[count].t_bottom_y) - (wndmap->wm[count].t_top_y));
647 for (index = 0; index < size; index++) {
648 cf_WriteByte(file, wndmap->wm[count].rt[index]);
649 }
650 }
651 if (wndmap->wm[count].b_top_y != -1) {
652 // left bottom
653 size = ((wndmap->wm[count].l_end_x) - (wndmap->wm[count].l_start_x)) *
654 ((wndmap->wm[count].b_bottom_y) - (wndmap->wm[count].b_top_y));
655 for (index = 0; index < size; index++) {
656 cf_WriteByte(file, wndmap->wm[count].lb[index]);
657 }
658 // right bottom
659 size = ((wndmap->wm[count].r_end_x) - (wndmap->wm[count].r_start_x)) *
660 ((wndmap->wm[count].b_bottom_y) - (wndmap->wm[count].b_top_y));
661 for (index = 0; index < size; index++) {
662 cf_WriteByte(file, wndmap->wm[count].rb[index]);
663 }
664 }
665 }
666 cfclose(file);
667 }
668
669 // Given a filename and a hotspotmap structure, it loads the hotspot map (.HSM)
670 void menutga_LoadHotSpotMap(int back_bmp, const char *filename, hotspotmap_t *hsmap, windowmap_t *wndmap) {
671 // start with a clean struct
672 if (hsmap->hs) {
673 FreeHotSpotMapInternals(hsmap);
674 }
675
676 mprintf(0, "Loading hotspotmap %s ", filename);
677
678 CFILE *infile;
679 infile = (CFILE *)cfopen(filename, "rb");
680 if (!infile) {
681 Int3(); // get Jeff
682 return;
683 }
684
685 hsmap->num_of_hotspots = cf_ReadByte(infile);
686
687 mprintf(0, "Contains: (%d hotspots) ", hsmap->num_of_hotspots);
688
689 int curr_hs, curr_sl, num_sl;
690
691 hsmap->hs = (hotspot *)mem_malloc(sizeof(hotspot) * hsmap->num_of_hotspots);
692 memset(hsmap->hs, 0, sizeof(hotspot) * hsmap->num_of_hotspots);
693 for (curr_hs = 0; curr_hs < hsmap->num_of_hotspots; curr_hs++) {
694 hsmap->hs[curr_hs].starting_y = cf_ReadInt(infile);
695 num_sl = hsmap->hs[curr_hs].scanlines = cf_ReadInt(infile);
696 if (num_sl) {
697 hsmap->hs[curr_hs].x = (scanline *)mem_malloc(sizeof(scanline) * hsmap->hs[curr_hs].scanlines);
698 memset(hsmap->hs[curr_hs].x, 0, sizeof(scanline) * hsmap->hs[curr_hs].scanlines);
699 for (curr_sl = 0; curr_sl < hsmap->hs[curr_hs].scanlines; curr_sl++) {
700 hsmap->hs[curr_hs].x[curr_sl].start = cf_ReadInt(infile);
701 hsmap->hs[curr_hs].x[curr_sl].end = cf_ReadInt(infile);
702 }
703 } else {
704 hsmap->hs[curr_hs].x = NULL;
705 }
706 }
707
708 int count, index, size;
709 wndmap->num_of_windows = cf_ReadInt(infile);
710 mprintf(0, "(%d Windows)\n", wndmap->num_of_windows);
711 wndmap->wm = (window_box *)mem_malloc(sizeof(window_box) * wndmap->num_of_windows);
712 for (count = 0; count < wndmap->num_of_windows; count++) {
713 wndmap->wm[count].x = cf_ReadInt(infile);
714 wndmap->wm[count].y = cf_ReadInt(infile);
715 wndmap->wm[count].width = cf_ReadInt(infile);
716 wndmap->wm[count].height = cf_ReadInt(infile);
717 wndmap->wm[count].l_start_x = cf_ReadInt(infile);
718 wndmap->wm[count].l_end_x = cf_ReadInt(infile);
719 wndmap->wm[count].r_start_x = cf_ReadInt(infile);
720 wndmap->wm[count].r_end_x = cf_ReadInt(infile);
721 wndmap->wm[count].t_top_y = cf_ReadInt(infile);
722 wndmap->wm[count].t_bottom_y = cf_ReadInt(infile);
723 wndmap->wm[count].b_top_y = cf_ReadInt(infile);
724 wndmap->wm[count].b_bottom_y = cf_ReadInt(infile);
725
726 if (wndmap->wm[count].t_top_y != -1) {
727 // left top
728 size = ((wndmap->wm[count].l_end_x) - (wndmap->wm[count].l_start_x)) *
729 ((wndmap->wm[count].t_bottom_y) - (wndmap->wm[count].t_top_y));
730 wndmap->wm[count].lt = (char *)mem_malloc(size);
731 for (index = 0; index < size; index++) {
732 wndmap->wm[count].lt[index] = cf_ReadByte(infile);
733 }
734
735 // right top
736 size = ((wndmap->wm[count].r_end_x) - (wndmap->wm[count].r_start_x)) *
737 ((wndmap->wm[count].t_bottom_y) - (wndmap->wm[count].t_top_y));
738 wndmap->wm[count].rt = (char *)mem_malloc(size);
739 for (index = 0; index < size; index++) {
740 wndmap->wm[count].rt[index] = cf_ReadByte(infile);
741 }
742 }
743
744 if (wndmap->wm[count].b_top_y != -1) {
745 // left bottom
746 size = ((wndmap->wm[count].l_end_x) - (wndmap->wm[count].l_start_x)) *
747 ((wndmap->wm[count].b_bottom_y) - (wndmap->wm[count].b_top_y));
748 wndmap->wm[count].lb = (char *)mem_malloc(size);
749 for (index = 0; index < size; index++) {
750 wndmap->wm[count].lb[index] = cf_ReadByte(infile);
751 }
752 // right bottom
753 size = ((wndmap->wm[count].r_end_x) - (wndmap->wm[count].r_start_x)) *
754 ((wndmap->wm[count].b_bottom_y) - (wndmap->wm[count].b_top_y));
755 wndmap->wm[count].rb = (char *)mem_malloc(size);
756 for (index = 0; index < size; index++) {
757 wndmap->wm[count].rb[index] = cf_ReadByte(infile);
758 }
759 }
760 }
761 cfclose(infile);
762
763 // make the corner bitmaps, with transparency
764 for (count = 0; count < wndmap->num_of_windows; count++) {
765 if (wndmap->wm[count].t_top_y != -1) {
766 // left top
767 wndmap->wm[count].lt_bmp = bm_AllocBitmap(wndmap->wm[count].l_end_x - wndmap->wm[count].l_start_x,
768 wndmap->wm[count].t_bottom_y - wndmap->wm[count].t_top_y, 0);
769 makecorner(wndmap->wm[count].lt_bmp, back_bmp, wndmap->wm[count].lt, wndmap->wm[count].l_start_x,
770 wndmap->wm[count].t_top_y, wndmap->wm[count].l_end_x, wndmap->wm[count].t_bottom_y);
771
772 // right top
773 wndmap->wm[count].rt_bmp = bm_AllocBitmap(wndmap->wm[count].r_end_x - wndmap->wm[count].r_start_x,
774 wndmap->wm[count].t_bottom_y - wndmap->wm[count].t_top_y, 0);
775 makecorner(wndmap->wm[count].rt_bmp, back_bmp, wndmap->wm[count].rt, wndmap->wm[count].r_start_x,
776 wndmap->wm[count].t_top_y, wndmap->wm[count].r_end_x, wndmap->wm[count].t_bottom_y);
777 if (wndmap->wm[count].lt)
778 mem_free(wndmap->wm[count].lt);
779 if (wndmap->wm[count].rt)
780 mem_free(wndmap->wm[count].rt);
781 }
782
783 if (wndmap->wm[count].b_top_y != -1) {
784 // left bottom
785 wndmap->wm[count].lb_bmp = bm_AllocBitmap(wndmap->wm[count].l_end_x - wndmap->wm[count].l_start_x,
786 wndmap->wm[count].b_bottom_y - wndmap->wm[count].b_top_y, 0);
787 makecorner(wndmap->wm[count].lb_bmp, back_bmp, wndmap->wm[count].lb, wndmap->wm[count].l_start_x,
788 wndmap->wm[count].b_top_y, wndmap->wm[count].l_end_x, wndmap->wm[count].b_bottom_y);
789
790 // right bottom
791 wndmap->wm[count].rb_bmp = bm_AllocBitmap(wndmap->wm[count].r_end_x - wndmap->wm[count].r_start_x,
792 wndmap->wm[count].b_bottom_y - wndmap->wm[count].b_top_y, 0);
793 makecorner(wndmap->wm[count].rb_bmp, back_bmp, wndmap->wm[count].rb, wndmap->wm[count].r_start_x,
794 wndmap->wm[count].b_top_y, wndmap->wm[count].r_end_x, wndmap->wm[count].b_bottom_y);
795 if (wndmap->wm[count].lb)
796 mem_free(wndmap->wm[count].lb);
797 if (wndmap->wm[count].rb)
798 mem_free(wndmap->wm[count].rb);
799 }
800 }
801 }
802
803 void makecorner(int corner_bmp, int back_bmp, const char *tmap, int l, int t, int r, int b) {
804 int real_x, real_y, awidth, ax, ay;
805 int16_t *backdata, *cornerdata;
806 int back_rowsize, corner_rowsize;
807
808 awidth = r - l;
809 bm_ClearBitmap(corner_bmp);
810 backdata = (int16_t *)bm_data(back_bmp, 0);
811 back_rowsize = bm_rowsize(back_bmp, 0);
812 cornerdata = (int16_t *)bm_data(corner_bmp, 0);
813 corner_rowsize = bm_rowsize(corner_bmp, 0);
814
815 backdata += (t * (back_rowsize / 2));
816
817 for (real_y = t, ay = 0; real_y < b; real_y++, ay++) {
818 for (real_x = l, ax = 0; real_x < r; real_x++, ax++) {
819 if (!tmap[ay * awidth + ax]) {
820 // not transparent
821 cornerdata[ax] = backdata[real_x];
822 }
823 }
824 cornerdata += (corner_rowsize / 2);
825 backdata += (back_rowsize / 2);
826 }
827 }
828
829 // This function frees up the allocated memory within a hotspotmap struct. It does not free up the struct though.
830 void FreeHotSpotMapInternals(hotspotmap_t *hsmap) {
831 mprintf(0, "Freeing HSM internals\n");
832
833 ASSERT(hsmap);
834
835 if (!hsmap->hs)
836 return;
837
838 int curr_hs;
839 int num_hs;
840
841 num_hs = hsmap->num_of_hotspots;
842 for (curr_hs = 0; curr_hs < num_hs; curr_hs++) {
843 if (hsmap->hs[curr_hs].scanlines) {
844 if (hsmap->hs[curr_hs].x) {
845 mem_free(hsmap->hs[curr_hs].x);
846 hsmap->hs[curr_hs].x = NULL;
847 }
848 }
849 }
850 if (hsmap->hs) {
851 mem_free(hsmap->hs);
852 hsmap->hs = NULL;
853 }
854 }
855
856 // This function (given a filename) loads a TGA file, extracts a hotspot map, and saves the hotspot map
857 bool menutga_ConvertTGAtoHSM(const char *fpath) {
858 char path[255], filename[255], ext[8];
859 ddio_SplitPath(fpath, path, filename, ext);
860 mprintf(0, "Extracting hotspots from %s\n", filename);
861
862 // strip file name
863 int index, width = 0, height = 0;
864 char *menu_filename;
865 hotspotmap_t hsmap;
866 windowmap_t wndmap;
867
868 wndmap.num_of_windows = 0;
869 wndmap.wm = NULL;
870 hsmap.hs = NULL;
871 hsmap.num_of_hotspots = 0;
872
873 for (index = (signed)strlen(filename); index >= 0; index--) {
874 if (filename[index] == '.') {
875 filename[index] = '\0';
876 index = -1;
877 }
878 }
879
880 int size = strlen(filename) + 5;
881 menu_filename = (char *)mem_malloc(size);
882 strcpy(menu_filename, filename);
883 strcat(menu_filename, ".HSM"); // Hot Spot Map
884 mprintf(0, "HSM=%s\n", menu_filename);
885
886 std::filesystem::path save_path = LocalManageGraphicsDir / menu_filename;
887
888 // now load up the tga and alloc it, then save it with the alpha hotspots
889 int bm_handle;
890 char *map[1];
891 bm_handle = menutga_alloc_file(fpath, map, &width, &height);
892 if (bm_handle == -1)
893 return false;
894 wndmap.num_of_windows = CreateHotSpotMap(map[0], width, height, &hsmap);
895 if (wndmap.num_of_windows == -1)
896 return false; // DAJ -1FIX
897 CreateWindowMap(map[0], width, height, &wndmap);
898 menutga_SaveHotSpotMap(save_path, &hsmap, &wndmap);
899 ExportHotSpot("C:\\hotspot.txt", &hsmap);
900 mem_free(menu_filename);
901 mem_free(map[0]);
902 FreeHotSpotMapInternals(&hsmap);
903 for (index = 0; index < wndmap.num_of_windows; index++) {
904 if (wndmap.wm[index].lt)
905 mem_free(wndmap.wm[index].lt);
906 if (wndmap.wm[index].rt)
907 mem_free(wndmap.wm[index].rt);
908 if (wndmap.wm[index].lb)
909 mem_free(wndmap.wm[index].lb);
910 if (wndmap.wm[index].rb)
911 mem_free(wndmap.wm[index].rb);
912 }
913 if (wndmap.wm)
914 mem_free(wndmap.wm);
915 return true;
916 }
917
918 // TODO: MTS: Unused?
919 // Given a hotspotmap structure, it draws the pretty hotspot map to the screen (blue areas are the hotspots)
920 void DisplayHotSpots(hotspotmap_t *hsmap, windowmap_t *wndmap) {
921 //@@ grSurface *cursurf;
922 //@@ int count,curr_y,scanline_count;
923 //@@ hotspot *curr_hotspot;
924 //@@
925 //@@
926 //@@ cursurf=Game_viewport->lock();
927 //@@ for(count=0;count<hsmap->num_of_hotspots;count++)
928 //@@ {
929 //@@ curr_hotspot=&hsmap->hs[count];
930 //@@ curr_y=curr_hotspot->starting_y;
931 //@@ for(scanline_count=0;scanline_count<curr_hotspot->scanlines;scanline_count++)
932 //@@ {
933 //@@ Game_viewport->hline(GR_RGB(0,0,255),curr_hotspot->x[scanline_count].start,
934 //@@
935 // curr_hotspot->x[scanline_count].end,curr_y+scanline_count);
936 //@@ }
937 //@@ }
938 //@@
939 //@@ for(count=0;count<wndmap->num_of_windows;count++)
940 //@@ {
941 //@@ for(scanline_count=0;scanline_count<curr_hotspot->scanlines;scanline_count++)
942 //@@ {
943 //@@ Game_viewport->fillrect(GR_RGB(255,0,0), wndmap->wm[count].x,wndmap->wm[count].y,
944 //@@
945 // wndmap->wm[count].x+wndmap->wm[count].width-1,wndmap->wm[count].y+wndmap->wm[count].height-1);
946 //@@ //if there is a writeable area, display it
947 //@@ if(wndmap->wm[count].t_top_y!=-1)
948 //@@ {
949 //@@ Game_viewport->fillrect(GR_RGB(128,128,128),
950 // wndmap->wm[count].l_start_x,wndmap->wm[count].t_top_y,
951 //@@ wndmap->wm[count].l_end_x,wndmap->wm[count].t_bottom_y);
952 //@@ Game_viewport->fillrect(GR_RGB(128,128,128),
953 // wndmap->wm[count].r_start_x,wndmap->wm[count].t_top_y,
954 //@@ wndmap->wm[count].r_end_x,wndmap->wm[count].t_bottom_y);
955 //@@ }
956 //@@ if(wndmap->wm[count].b_top_y!=-1)
957 //@@ {
958 //@@ Game_viewport->fillrect(GR_RGB(128,128,128),
959 // wndmap->wm[count].l_start_x,wndmap->wm[count].b_top_y,
960 //@@ wndmap->wm[count].l_end_x,wndmap->wm[count].b_bottom_y);
961 //@@ Game_viewport->fillrect(GR_RGB(128,128,128),
962 // wndmap->wm[count].r_start_x,wndmap->wm[count].b_top_y,
963 //@@ wndmap->wm[count].r_end_x,wndmap->wm[count].b_bottom_y);
964 //@@ }
965 //@@
966 //@@ }
967 //@@ }
968 //@@
969 //@@ Game_viewport->unlock();
970 //@@ Game_screen->flip();
971 }
972
973 // TODO: MTS: only used in this file
974 // Writes a hotspotmap_t struct to a text file
975 void ExportHotSpot(const char *filename, hotspotmap_t *hsmap) {
976 CFILE *file;
977 file = (CFILE *)cfopen(filename, "wt");
978 ASSERT(file);
979 char buffer[256];
980
981 int i, j;
982 snprintf(buffer, sizeof(buffer), "Number of HotSpots = %d", hsmap->num_of_hotspots);
983 cf_WriteString(file, buffer);
984 for (i = 0; i < hsmap->num_of_hotspots; i++) {
985 snprintf(buffer, sizeof(buffer), "\n-%d-Scanlines = %d", i, hsmap->hs[i].scanlines);
986 cf_WriteString(file, buffer);
987 for (j = 0; j < hsmap->hs[i].scanlines; j++) {
988 snprintf(buffer, sizeof(buffer), "----Y=%d StartX=%d EndX=%d", hsmap->hs[i].starting_y + j,
989 hsmap->hs[i].x[j].start, hsmap->hs[i].x[j].end);
990 cf_WriteString(file, buffer);
991 }
992 }
993
994 cfclose(file);
995 }