Repository navigation
Expand file tree
/
Copy pathresources.c
More file actions
482 lines (457 loc) · 19.3 KB
/
Copy pathresources.c
File metadata and controls
482 lines (457 loc) · 19.3 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#include "allocator.h"
#include "resources.h"
#include <stdlib.h>
#include <string.h>
DxBallSpriteBank dxball_sprite_banks[3];
DxBallInt dxball_sprite_bank, dxball_font_bank;
DxBallDDraw *dxball_direct_draw;
DxBallDDPalette *dxball_direct_palette;
DxBallPaletteEntry dxball_live_palette[256], dxball_saved_palette[256];
/* FUNCTION: DXBALL 0x00403E70 */
void dxball_select_sprite_bank(DxBallInt bank)
{
dxball_sprite_bank = bank;
return;
}
/* FUNCTION: DXBALL 0x00403E90 */
void dxball_select_font_bank(DxBallInt bank)
{
dxball_font_bank = bank;
return;
}
/* FUNCTION: DXBALL 0x00404180 */
void dxball_draw_sprite(DxBallInt sprite, DxBallInt x, DxBallInt y)
{
((DxBallDDSurface *)dxball_active_surface)->vtable->blt_fast(
(DxBallDDSurface *)dxball_active_surface, x, y,
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect, 0x10);
return;
}
/* FUNCTION: DXBALL 0x00404040 */
void dxball_draw_keyed_sprite(DxBallInt sprite, DxBallInt x, DxBallInt y)
{
((DxBallDDSurface *)dxball_active_surface)->vtable->blt_fast(
(DxBallDDSurface *)dxball_active_surface, x, y,
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect, 0x11);
return;
}
/* FUNCTION: DXBALL 0x00403F70 */
void dxball_blt_keyed_sprite(DxBallInt sprite, DxBallInt x, DxBallInt y)
{
DxBallRect destination;
destination.left = x;
destination.top = y;
destination.right = x + dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->width;
destination.bottom = y + dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->height;
((DxBallDDSurface *)dxball_active_surface)->vtable->blt(
(DxBallDDSurface *)dxball_active_surface, &destination,
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect, 0x1008000, NULL);
return;
}
/* FUNCTION: DXBALL 0x004040B0 */
void dxball_blt_sprite(DxBallInt sprite, DxBallInt x, DxBallInt y)
{
DxBallRect destination;
destination.left = x;
destination.top = y;
destination.right = x + dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->width;
destination.bottom = y + dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->height;
((DxBallDDSurface *)dxball_active_surface)->vtable->blt(
(DxBallDDSurface *)dxball_active_surface, &destination,
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect, 0x1000000, NULL);
return;
}
/* FUNCTION: DXBALL 0x004041F0 */
void dxball_stretch_keyed_sprite(DxBallInt sprite, DxBallInt x, DxBallInt y,
DxBallInt width, DxBallInt height)
{
DxBallRect destination;
destination.left = x;
destination.top = y;
destination.right = x + width;
destination.bottom = y + height;
((DxBallDDSurface *)dxball_active_surface)->vtable->blt(
(DxBallDDSurface *)dxball_active_surface, &destination,
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect, 0x1008000, NULL);
return;
}
/* FUNCTION: DXBALL 0x00404BE0 */
void dxball_release_sprite(DxBallInt sprite)
{
if (dxball_sprite_banks[dxball_sprite_bank].sprites[sprite] == NULL) return;
if (dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface != NULL) {
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->release(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface);
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface = NULL;
}
dxball_heap_release(dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]);
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite] = NULL;
return;
}
/* FUNCTION: DXBALL 0x004042B0 */
void dxball_capture_sprite(DxBallInt sprite, DxBallInt x, DxBallInt y,
DxBallInt width, DxBallInt height)
{
DxBallColorKey key;
DxBallRect capture;
dxball_release_sprite(sprite);
{
DxBallSprite *record;
/* The target requests 45 bytes for the 44-byte x86 record. The last byte's
purpose is unclassified; pointer-sized host storage grows naturally. */
record = (DxBallSprite *)dxball_runtime_malloc(sizeof(DxBallSprite) + 1);
if (record == NULL)
exit(1);
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite] = record;
}
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->width = width;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->height = height;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->code = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->baseline = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.top = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.left = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.bottom = height;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.right = width;
{
struct {
DxBallSurfaceDesc description;
DxBallInt result;
} api;
api.description.size = sizeof(api.description);
api.description.flags = 0xf;
api.description.caps = 0x840;
api.description.height = height;
api.description.width = width;
api.result = dxball_direct_draw->vtable->create_surface(
dxball_direct_draw, &api.description,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface, NULL);
if (api.result != 0) return;
key.low = 0;
key.high = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->set_color_key(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface, 8, &key);
api.description.flags = 0x7f9ee;
do {
api.result = dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->get_desc(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface, &api.description);
} while (api.result != 0);
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->pitch = api.description.pitch;
}
capture.left = x;
capture.top = y;
capture.right = width + x;
capture.bottom = height + y;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->blt(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect,
(DxBallDDSurface *)dxball_active_surface, &capture, 0x1000000, NULL);
return;
}
/* FUNCTION: DXBALL 0x00404610 */
void dxball_load_sprite_bank(DxBallInt bank, DxBallInt allocation_mode, const char *path)
{
DxBallInt saved_bank, sprite, count, width, height, baseline;
signed char code;
DxBallUInt pixel_count;
DxBallByte *pixels, *pixel;
DxBallSprite *record;
DxBallColorKey key;
DxBallInt row, column, cursor, pitch;
saved_bank = dxball_sprite_bank;
dxball_select_sprite_bank(bank);
for (sprite = 1; sprite < 254; sprite = sprite + 1)
dxball_release_sprite(sprite);
dxball_board_file = fopen(path, "rb");
if (dxball_board_file == NULL)
exit(1);
fread(&count, 4, 1, dxball_board_file);
for (sprite = 1; sprite < count + 1; sprite = sprite + 1) {
fread(&width, 4, 1, dxball_board_file);
fread(&height, 4, 1, dxball_board_file);
fread(&code, 1, 1, dxball_board_file);
fread(&baseline, 4, 1, dxball_board_file);
pixel_count = width * height;
pixels = (DxBallByte *)dxball_runtime_malloc(pixel_count + 3);
if (pixels == NULL)
exit(1);
record = (DxBallSprite *)dxball_runtime_malloc(sizeof(DxBallSprite) + 1);
if (record == NULL)
exit(1);
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite] = record;
fread(pixels, 1, pixel_count, dxball_board_file);
dxball_sprite_banks[dxball_sprite_bank].count = count;
dxball_sprite_banks[dxball_sprite_bank].allocation_mode = allocation_mode;
strcpy(dxball_sprite_banks[dxball_sprite_bank].filename, path);
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->width = width;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->height = height;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->code = code;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->baseline = baseline;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.top = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.left = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.bottom = height;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->source_rect.right = width;
{
struct {
DxBallSurfaceDesc description;
DxBallInt result;
} api;
api.description.size = sizeof(api.description);
api.description.flags = 0xf;
if (dxball_sprite_banks[dxball_sprite_bank].allocation_mode == 1) {
api.description.caps = 0x40;
} else {
api.description.caps = 0x840;
}
api.description.height = height;
api.description.width = width;
api.result = dxball_direct_draw->vtable->create_surface(
dxball_direct_draw, &api.description,
&dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface, NULL);
/* Creation failure preserves the target's partial owner and leaks;
it does not restore the bank, close the stream or free these buffers. */
if (api.result != 0)
return;
key.low = 0;
key.high = 0;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->set_color_key(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface, 8, &key);
api.description.flags = 0x7f9ee;
do {
api.result = dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->get_desc(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface, &api.description);
} while (api.result != 0);
/* GetSurfaceDesc supplies the row stride before Lock; later API writes
to the descriptor or current record do not replace that snapshot. */
pitch = api.description.pitch;
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->pitch = pitch;
do {
api.result = dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->lock(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface,
NULL, &api.description, 0, NULL);
} while (api.result != 0);
cursor = 0;
for (row = height - 1; row >= 0; --row) {
for (column = 0; column <= width - 1; ++column) {
pixel = row * pitch + column + api.description.pixels;
*pixel = pixels[cursor];
cursor = cursor + 1;
}
}
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface->vtable->unlock(
dxball_sprite_banks[dxball_sprite_bank].sprites[sprite]->surface, NULL);
}
dxball_heap_release(pixels);
}
fclose(dxball_board_file);
dxball_select_sprite_bank(saved_bank);
return;
}
/* FUNCTION: DXBALL 0x00404E40
The count-th slot is intentionally excluded, and an empty bank returns 1. */
DxBallInt dxball_find_glyph(signed char code)
{
DxBallInt sprite = 0;
do {
sprite = sprite + 1;
} while (dxball_sprite_banks[dxball_font_bank].count > sprite &&
dxball_sprite_banks[dxball_font_bank].sprites[sprite]->code != code);
if (dxball_sprite_banks[dxball_font_bank].count == sprite)
sprite = 0;
return sprite;
}
/* FUNCTION: DXBALL 0x00404CF0 */
DxBallInt dxball_draw_glyph(signed char code, DxBallInt x, DxBallInt baseline)
{
DxBallInt sprite;
DxBallRect destination;
sprite = dxball_find_glyph(code);
if (sprite != 0) {
destination.left = x;
destination.top = baseline - dxball_sprite_banks[dxball_font_bank].sprites[sprite]->height
- dxball_sprite_banks[dxball_font_bank].sprites[sprite]->baseline;
destination.right = dxball_sprite_banks[dxball_font_bank].sprites[sprite]->width + x;
destination.bottom = baseline - dxball_sprite_banks[dxball_font_bank].sprites[sprite]->baseline;
((DxBallDDSurface *)dxball_active_surface)->vtable->blt(
(DxBallDDSurface *)dxball_active_surface, &destination,
dxball_sprite_banks[dxball_font_bank].sprites[sprite]->surface,
&dxball_sprite_banks[dxball_font_bank].sprites[sprite]->source_rect, 0x1008000, NULL);
return dxball_sprite_banks[dxball_font_bank].sprites[sprite]->width;
} else {
return 0;
}
}
/* FUNCTION: DXBALL 0x00409790 */
void dxball_load_live_palette(const char *path)
{
FILE *file;
DxBallInt color;
file = fopen(path, "rb");
fseek(file, -768, SEEK_END);
for (color = 0; color < 256; color = color + 1) {
dxball_live_palette[color].red = (DxBallByte)getc(file);
dxball_live_palette[color].green = (DxBallByte)getc(file);
dxball_live_palette[color].blue = (DxBallByte)getc(file);
}
fclose(file);
dxball_direct_palette->vtable->set_entries(dxball_direct_palette, 0, 0, 256,
dxball_live_palette);
return;
}
/* FUNCTION: DXBALL 0x004098F0 */
void dxball_load_saved_palette(const char *path)
{
FILE *file;
DxBallInt color;
file = fopen(path, "rb");
fseek(file, -768, SEEK_END);
for (color = 0; color < 256; color = color + 1) {
dxball_saved_palette[color].red = (DxBallByte)getc(file);
dxball_saved_palette[color].green = (DxBallByte)getc(file);
dxball_saved_palette[color].blue = (DxBallByte)getc(file);
}
fclose(file);
return;
}
/* FUNCTION: DXBALL 0x00409A30 */
void dxball_load_surface_palette(DxBallDDSurface *surface, const char *path)
{
FILE *file;
DxBallInt color, result;
file = fopen(path, "rb");
fseek(file, -768, SEEK_END);
for (color = 0; color < 256; color = color + 1) {
dxball_live_palette[color].red = (DxBallByte)getc(file);
dxball_live_palette[color].green = (DxBallByte)getc(file);
dxball_live_palette[color].blue = (DxBallByte)getc(file);
}
fclose(file);
result = dxball_direct_draw->vtable->create_palette(dxball_direct_draw, 4,
dxball_live_palette, &dxball_direct_palette, NULL);
if (result != 0) {
return;
}
result = surface->vtable->set_palette(surface, dxball_direct_palette);
return;
}
/* Only the two observed signed maxima are interpreted from the disk header. */
typedef struct DxBallPcxHeader {
DxBallByte prefix[8];
signed short xmax, ymax;
DxBallByte remainder[116];
} DxBallPcxHeader;
typedef char DxBallPcxHeaderMustBe128Bytes[(sizeof(DxBallPcxHeader) == 128) ? 1 : -1];
/* Preserve the observed byte-range check and independently updated source row. */
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
/* FUNCTION: DXBALL 0x00409BB0
Preserve the observed inclusive xmax*ymax limit, including truncated last
rows and packet overshoot. This is the game decoder, not a general PCX reader. */
void dxball_load_pcx(DxBallDDSurface *surface, const char *path,
DxBallInt palette_mode, DxBallInt x, DxBallInt y)
{
DxBallSurfaceDesc description;
DxBallPcxHeader header;
DxBallByte *header_bytes, *pixel;
DxBallByte value;
FILE *file;
DxBallInt width, height, pitch, xmax, ymax;
DxBallInt column, row, destination_x, destination_y, decoded, run, result;
DxBallInt header_index, run_index;
description.size = sizeof(description);
description.flags = 0xe;
surface->vtable->get_desc(surface, &description);
width = description.width;
height = description.height;
pitch = description.pitch;
destination_x = x;
destination_y = y;
column = 0;
row = 0;
file = fopen(path, "rb");
header_bytes = (DxBallByte *)&header;
for (header_index = 0; header_index < 128; header_index = header_index + 1)
header_bytes[header_index] = (DxBallByte)getc(file);
xmax = header.xmax;
ymax = header.ymax;
do {
result = surface->vtable->lock(surface, NULL, &description, 0, NULL);
} while (result != 0);
pixel = description.pixels;
decoded = 0;
while (xmax * ymax >= decoded) {
value = (DxBallByte)getc(file);
if ((DxBallInt)value >= 192 && (DxBallInt)value <= 255) {
run = value - 192;
decoded = decoded + run;
value = (DxBallByte)getc(file);
run_index = 0;
while (run_index < run) {
if (column > xmax) {
column = 0;
destination_x = x;
destination_y = destination_y + 1;
row = row + 1;
}
if (destination_x < width && destination_y < height &&
destination_x >= 0 && destination_y >= 0) {
pixel = description.pixels + destination_y * pitch + destination_x;
*pixel = value;
}
destination_x = destination_x + 1;
column = column + 1;
run_index = run_index + 1;
}
} else {
decoded = decoded + 1;
if (column > xmax) {
column = 0;
destination_x = x;
destination_y = destination_y + 1;
row = row + 1;
}
if (destination_x < width && destination_y < height &&
destination_x >= 0 && destination_y >= 0) {
pixel = description.pixels + destination_y * pitch + destination_x;
*pixel = value;
}
destination_x = destination_x + 1;
column = column + 1;
}
}
surface->vtable->unlock(surface, NULL);
fclose(file);
if (palette_mode == 1)
dxball_load_live_palette(path);
if (palette_mode == 2)
dxball_load_saved_palette(path);
return;
}
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
#include "resources_bank.h"
/* FUNCTION: DXBALL 0x00403EB0 */
void dxball_release_sprite_bank(DxBallInt bank)
{
DxBallInt sprite, saved_bank;
saved_bank = dxball_sprite_bank;
dxball_select_sprite_bank(bank);
sprite = 0;
while (sprite <= 254) {
dxball_release_sprite(sprite);
++sprite;
}
dxball_sprite_banks[bank].count = 0;
dxball_sprite_banks[bank].allocation_mode = 0;
strcpy(dxball_sprite_banks[bank].filename, " ");
dxball_select_sprite_bank(saved_bank);
return;
}