Repository navigation
Expand file tree
/
Copy pathpanels.zig
More file actions
607 lines (537 loc) · 28.7 KB
/
Copy pathpanels.zig
File metadata and controls
607 lines (537 loc) · 28.7 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! The loadout's panels as their textures are drawn: the title over the page, with the chosen
//! ship's name, and the info panel with the ship's stats, a missile's, or the ship's guns. The game
//! makes each a texture of `size` by `size` pixels and writes its text into it through a VFX pane
//! (`hud.Pane`), and the scene shows it on one face of a two-sided panel (`Face`).
const std = @import("std");
const Allocator = std.mem.Allocator;
const tga = @import("../../../formats/tga.zig");
const hud = @import("../../game/hud.zig");
const language = @import("../../game/language.zig");
const bars = @import("bars.zig");
const tables = @import("tables.zig");
/// The panels' textures' size in the original, across and down (`0x00444D83`, `0x00444F40`).
pub const size = 256;
/// A panel's pixels: `size` times `multiple` across and down, four bytes a pixel (red, green, blue
/// and alpha), rows from the top.
///
/// **Improvement:** a mod's picture for `fpanels.tga` can be a whole multiple of its size
/// (`matmanager.Sizes.multiples`), and the panels are then drawn as many times as large, with their
/// text from the outline fonts that stand in for theirs (`hud.Pane`). The game's own have a
/// `multiple` of 1.
pub const Image = struct {
pixels: [][4]u8,
/// How many of its pixels across and down stand for each of the original's.
multiple: u32,
/// A clear image `multiple` times the original's size, in `gpa`.
pub fn create(gpa: Allocator, multiple: u32) Allocator.Error!Image {
const pixels = try gpa.alloc([4]u8, @as(usize, size * multiple) * size * multiple);
@memset(pixels, .{ 0, 0, 0, 0 });
return .{ .pixels = pixels, .multiple = multiple };
}
pub fn deinit(image: Image, gpa: Allocator) void {
gpa.free(image.pixels);
}
/// Its pixels across and down.
pub fn side(image: Image) u32 {
return size * image.multiple;
}
/// The pane its text is written through.
fn pane(image: Image) hud.Pane {
return .{ .rgba = image.pixels, .size = @splat(image.side()), .scale = image.multiple };
}
};
/// Which of a panel's two faces a texture is drawn for.
pub const Face = enum {
front,
back,
/// The title's texture: `panel_title_draw` names the front's where its last argument is 7
/// (`0x004EAE7C`), and the back's otherwise (`0x004EAE74`).
pub fn titleTexture(face: Face) []const u8 {
return switch (face) {
.front => "fpanels",
.back => "bpanels",
};
}
/// The info panel's texture: `loadout_draw_stats`, `missile_info_draw` and `guns_draw` name
/// the front's where their last argument is 5 (`0x004EAE94`), and the back's otherwise
/// (`0x004EAE8C`).
pub fn infoTexture(face: Face) []const u8 {
return switch (face) {
.front => "finfo",
.back => "binfo",
};
}
};
/// What the panels are drawn with.
pub const Kit = struct {
/// `ld_handel.fnt` (`0x00523964`), the title's font.
title_font: *const hud.Opened,
/// `handels.fnt` (`0x00523724`), the info panel's.
info_font: *const hud.Opened,
/// VFX's palette while the loadout runs: `palette3.tga`'s colour map (`loadout_palette`,
/// `0x004436B0`).
palette: *const tga.Palette,
strings: *const language.Language,
/// The game's string `id`. The game stops for an id past its strings; OpenReliant draws
/// nothing for it.
fn string(kit: Kit, id: u16) []const u8 {
return kit.strings.string(id) orelse "";
}
/// The game's string `id` in capitals, as the panels write their names, labels and
/// descriptions (`CharUpperBuffA`, `language.upperCase`).
fn capitals(kit: Kit, id: u16) Line {
var line: Line = .{};
line.append(kit.string(id));
language.upperCase(line.slice());
return line;
}
};
/// The info panel's texture as each of its pages begins it (`loadout_draw_stats`,
/// `missile_info_draw`, `guns_draw`): its image cleared, and written through a pane in the info
/// panel's font and colours (`tables.text_remap`).
const InfoPage = struct {
kit: Kit,
pane: hud.Pane,
remap: hud.Remap,
fn begin(image: Image, kit: Kit) InfoPage {
@memset(image.pixels, .{ 0, 0, 0, 0 });
return .{
.kit = kit,
.pane = image.pane(),
.remap = .{ .table = &tables.text_remap, .palette = kit.palette },
};
}
/// `text` from `at`, aligned as `alignment` says (`hud_text`).
fn write(page: InfoPage, at: [2]i32, text: []const u8, alignment: hud.Align) Allocator.Error!void {
_ = try hud.drawTextInto(page.pane, page.kit.info_font, at, text, page.remap, alignment);
}
/// `text` from `at`, broken into lines at most `width` wide, `line_height` apart, at most
/// `max_lines` of them (`hud_text_wrapped`); returns how many it took.
fn wrap(page: InfoPage, at: [2]i32, text: []const u8, width: i32, line_height: i32, max_lines: usize) Allocator.Error!usize {
return hud.drawWrappedInto(page.pane, page.kit.info_font, at, text, page.remap, .left, width, line_height, max_lines);
}
};
/// The title's line, `SHIPS AND LOADOUT`.
const title_string = 0x222;
/// Where the title's line and the ship's name start (`0x00444EBD`, `0x00444EE4`).
const title_at: [2]i32 = .{ 17, 4 };
const name_at: [2]i32 = .{ 17, 28 };
/// `panel_title_draw` (`0x00444D60`): the title panel's texture for the ship of `record`. `panels`, the
/// pixels of `fpanels.tga` from the top row down, which the game keeps as `SR_TGA_allocate_raw`
/// gives them (`0x00523960`), then `SHIPS AND LOADOUT` and the ship's name in capitals, in the
/// title's font and colours (`tables.title_remap`).
pub fn drawTitle(image: Image, panels: Image, kit: Kit, record: tables.Ship) Allocator.Error!void {
@memcpy(image.pixels, panels.pixels);
const pane = image.pane();
const remap: hud.Remap = .{ .table = &tables.title_remap, .palette = kit.palette };
_ = try hud.drawTextInto(pane, kit.title_font, title_at, kit.string(title_string), remap, .left);
var name = kit.capitals(record.name);
_ = try hud.drawTextInto(pane, kit.title_font, name_at, name.slice(), remap, .left);
}
/// The labels before the class and the access, `CLASS : ` and `ACCESS : `.
const class_label = 0x554;
const access_label = 0x555;
/// Where the class and the access stand (`0x004450C2`, `0x00445173`).
const class_at: [2]i32 = .{ 2, 12 };
const access_at: [2]i32 = .{ 2, 32 };
/// Where the rows start and how far apart they stand (`0x0044517F`, `0x004452F4`), and where a
/// row's label and its figure start (`0x004451F9`, `0x004452E2`).
const first_row_y = 57;
const row_step = 15;
const label_x = 2;
const figure_x = 180;
/// Where the specials start, and the width, the line height and the most lines they are wrapped
/// to (`0x00445406` to `0x00445423`).
const specials_at: [2]i32 = .{ 2, 180 };
const specials_width = 252;
const specials_line_height = 15;
const specials_lines = 3;
/// What stands between two specials (`0x004E5A1C`).
const specials_separator = ", ";
/// `loadout_draw_stats` (`0x00444F20`): the info panel's texture for the stats of `record`'s ship, on a
/// clear image, in the info panel's font and colours (`tables.text_remap`). Its class and its
/// access after their labels; its rows (`tables.ship_rows`), each label in capitals and the
/// figure of `figures` beside it, as a number and its suffix or as a bar (`drawBar`); and the
/// specials it has, in capitals, one after another (`specialsLine`).
pub fn drawStats(image: Image, kit: Kit, record: tables.Ship, figures: tables.ShipFigures) Allocator.Error!void {
const page: InfoPage = .begin(image, kit);
var class: Line = .{};
class.append(kit.string(class_label));
class.append(kit.string(record.class.string()));
try page.write(class_at, class.slice(), .left);
var access: Line = .{};
access.append(kit.string(access_label));
access.append(kit.string(record.access.string()));
try page.write(access_at, access.slice(), .left);
var y: i32 = first_row_y;
for (tables.ship_rows, figures) |row, figure| {
try drawRow(page, y, row, figure);
y += row_step;
}
var specials = specialsLine(kit, record.specials);
language.upperCase(specials.slice());
_ = try page.wrap(specials_at, specials.slice(), specials_width, specials_line_height, specials_lines);
}
/// A row of figures at `y` (`loadout_draw_stats`, `0x00445180` to `0x004452F4`;
/// `missile_info_draw`, `0x0044593B` to `0x00445ADE`): its label in capitals, and beside it the
/// figure, as a number and its suffix or as a bar (`drawBar`), or a dash where there is none.
fn drawRow(page: InfoPage, y: i32, row: tables.Row, figure: ?i32) Allocator.Error!void {
const kit = page.kit;
var label = kit.capitals(row.label);
try page.write(.{ label_x, y }, label.slice(), .left);
const shown = figure orelse return page.write(.{ figure_x, y }, no_figure, .left);
switch (row.kind) {
.number => {
var number: Line = .{};
number.print("{d}", .{shown});
number.append(if (row.suffix) |suffix| kit.string(suffix) else "");
try page.write(.{ figure_x, y }, number.slice(), .left);
},
.bar => drawBar(page.pane, kit.palette, .{ figure_x, y }, shown),
}
}
/// What a row shows for a figure its missile lacks (`0x004EAEB4`).
const no_figure = "-";
/// Where a missile's name and its description start, and the width, the line height and the most
/// lines the description is wrapped to (`0x00445839`, `0x004458BA`, `0x004458A4` to
/// `0x004458A8`).
const missile_name_at: [2]i32 = .{ 2, 12 };
const description_at: [2]i32 = .{ 2, 32 };
const description_width = 252;
const description_line_height = 15;
const description_lines = 6;
/// Where a missile's rows start (`0x0044592E`).
const missile_first_row_y = 127;
/// The two lines under a missile's rows, `CLICK MISSILE` and `TO ATTACH TO SHIP`, and where each is
/// centred (`0x004458C6`, `0x004458F6`).
const click_lines = [2]u16{ 0x220, 0x221 };
const click_at = [2][2]i32{ .{ 128, 195 }, .{ 128, 210 } };
/// `missile_info_draw` (`0x004456F0`): the info panel's texture for `missile`, on a clear image,
/// in the info panel's font and colours (`tables.text_remap`): its name and its description in
/// capitals, the description wrapped; `CLICK MISSILE` and `TO ATTACH TO SHIP` centred low down;
/// and its rows (`tables.missile_rows`), each with its figure of `figures`, or a dash for a figure
/// of -1 and for every row of the fuel pod (`0x004459B2`).
pub fn drawMissile(image: Image, kit: Kit, missile: tables.Missile, figures: tables.MissileFigures) Allocator.Error!void {
const page: InfoPage = .begin(image, kit);
const record = missile.record();
var name = kit.capitals(record.name);
try page.write(missile_name_at, name.slice(), .left);
var description = kit.capitals(record.description);
_ = try page.wrap(description_at, description.slice(), description_width, description_line_height, description_lines);
for (click_lines, click_at) |line, at| try page.write(at, kit.string(line), .centre);
var y: i32 = missile_first_row_y;
for (tables.missile_rows, figures) |row, figure| {
const value: ?i32 = if (missile == .fuel_pod) null else if (row.dash_for_none) bars.MissileBars.shown(figure) else figure;
try drawRow(page, y, row, value);
y += row_step;
}
}
/// Where each gun's line starts and how far below it the next thing starts (`0x004455E9`,
/// `0x004455F3`); the width, the line height and the most lines a gun's description is wrapped
/// to, and the room under it (`0x00445677` to `0x0044567B`, `0x0044569B`).
const gun_x = 2;
const gun_step = 20;
const gun_description_width = 252;
const gun_description_line_height = 15;
const gun_description_lines = 20;
const gun_description_gap = 10;
/// What ends the description of a gun that drains no energy (`0x004EAEA8`).
const no_drain = ",";
/// `guns_draw` (`0x00445490`): the info panel's texture for the guns of `record`'s ship, on a clear image,
/// in the info panel's font and colours (`tables.text_remap`), from the top down: for each kind of
/// its guns, its name and how many the ship has in capitals (`%s X %d`, `0x004EAEAC`), and under
/// it, but for a rear turret (`tables.first_rear_turret` on), its description wrapped: its power,
/// kind, range, rate and drain run together (`%s%s%s%s%s`, `0x004EAE9C`), in the strings' own
/// case.
pub fn drawGuns(image: Image, kit: Kit, record: tables.Ship) Allocator.Error!void {
const page: InfoPage = .begin(image, kit);
var y: i32 = 0;
for (record.guns) |mount| {
var line: Line = .{};
line.print("{s} X {d}", .{ kit.string(mount.gun), mount.count });
language.upperCase(line.slice());
try page.write(.{ gun_x, y }, line.slice(), .left);
y += gun_step;
const description = tables.gunDescription(mount.gun) orelse continue;
var sentence: Line = .{};
for ([_]u16{ description.power, description.kind, description.range, description.rate }) |part| sentence.append(kit.string(part));
sentence.append(if (description.drain) |drain| kit.string(drain) else no_drain);
const lines = try page.wrap(.{ gun_x, y }, sentence.slice(), gun_description_width, gun_description_line_height, gun_description_lines);
y += @as(i32, @intCast(lines)) * gun_description_line_height + gun_description_gap;
}
}
/// The names of the specials of `specials`, in their bits' order, `specials_separator` between
/// each and the next (`0x0044530B` to `0x004453DC`).
fn specialsLine(kit: Kit, specials: tables.Specials) Line {
var line: Line = .{};
var first = true;
inline for (tables.Specials.bits, tables.special_names) |bit, name| {
if (@field(specials, bit)) {
if (!first) line.append(specials_separator);
first = false;
line.append(kit.string(name));
}
}
return line;
}
/// The segments of a bar: how many, how far apart from the left of one to the next
/// (`0x0044526E`), and how far a segment's right edge and foot lie from its corner
/// (`0x0044524A`, `0x0044524F`).
const segment_count = 10;
const segment_step = 7;
const segment_right = 4;
const segment_foot = 10;
/// A row's figure as a bar from `at` (`0x00445216` to `0x00445278`): `segment_count` segments,
/// the first `figure` of them filled and the rest outlined (`drawSegment`), in the bars' colour
/// (`tables.bar_colour`).
fn drawBar(pane: hud.Pane, palette: *const tga.Palette, at: [2]i32, figure: i32) void {
const colour = palette[tables.bar_colour];
for (0..segment_count) |index| {
const left = at[0] + @as(i32, @intCast(index)) * segment_step;
const filled = figure >= @as(i32, @intCast(index)) + 1;
drawSegment(pane, colour, .{ left, at[1] }, .{ left + segment_right, at[1] + segment_foot }, filled);
}
}
/// `0x00445B20`: a segment of a bar in `colour`, from the corner `from` to the corner `to`, the
/// columns of both taken, as `VFX_line_draw` draws its lines. Filled, it is a line along each row
/// from `from`'s down to the one above `to`'s; outlined, a line round its four sides, the rows of
/// both corners taken.
fn drawSegment(pane: hud.Pane, colour: [3]u8, from: [2]i32, to: [2]i32, filled: bool) void {
if (filled) {
var y = from[1];
while (y < to[1]) : (y += 1) pane.line(colour, .{ from[0], y }, .{ to[0], y });
return;
}
pane.line(colour, from, .{ to[0], from[1] });
pane.line(colour, .{ to[0], from[1] }, to);
pane.line(colour, to, .{ from[0], to[1] });
pane.line(colour, .{ from[0], to[1] }, from);
}
/// A line of text put together from the game's strings, as the game puts its lines together with
/// `strcpy`, `strcat` and `sprintf`. What does not fit is left off, where the game's buffers
/// overrun; the shipped strings fit several times over.
const Line = struct {
buffer: [room]u8 = undefined,
len: usize = 0,
const room = hud.wrapped_line_room;
fn append(line: *Line, text: []const u8) void {
const kept = text[0..@min(text.len, room - line.len)];
@memcpy(line.buffer[line.len..][0..kept.len], kept);
line.len += kept.len;
}
fn print(line: *Line, comptime format: []const u8, args: anytype) void {
const written = std.mem.print(line.buffer[line.len..], format, args) catch return;
line.len += written.len;
}
fn slice(line: *Line) []u8 {
return line.buffer[0..line.len];
}
};
/// A font for the tests in which every code is the same glyph: two pixels wide and two tall,
/// levels 15 and 1 over 0 and 15.
fn testFont() []const u8 {
const count = 256;
const header: @import("../../../formats/fnt.zig").Header = .{ .version = "2.\x00\x00".*, .count = count, .height = 2, ._unknown_0c = 0 };
const table: [count]u32 = @splat(@sizeOf(@TypeOf(header)) + count * @sizeOf(u32));
const glyph = std.mem.toBytes(@as(u32, 2)) ++ [_]u8{ 15, 1, 0, 15 };
return std.mem.toBytes(header) ++ std.mem.sliceAsBytes(&table) ++ glyph;
}
/// The strings of the tests: those the panels draw, each its own English.
fn testStrings(buffer: *[0x555][]const u8) language.Language {
buffer.* = @splat("");
const strings = [_]struct { u16, []const u8 }{
.{ title_string, "SHIPS AND LOADOUT" }, .{ 0x1F7, "Predator" }, .{ 0x201, "Shroud" },
.{ class_label, "CLASS : " }, .{ access_label, "ACCESS :" }, .{ 0x223, "LIGHT" },
.{ 0x229, "BRONZE" }, .{ 0x21C, " SECS" }, .{ 0x1EF, "Reverse Thrust" },
.{ 0x1F3, "Spectral Shields" }, .{ 0x1F4, "Blind Fire" }, .{ 0x1F5, "Cloaking Device" },
.{ 0x20E, "Max Speed" }, .{ 0x205, "Havok" }, .{ 0x1E7, "A big one" },
.{ 0x220, "CLICK MISSILE" }, .{ 0x218, "Locking Time" }, .{ 0x219, "Speed" },
.{ 0x23B, "Laser" }, .{ 0x54E, "Rear" }, .{ 0x244, "ab" },
.{ 0x24F, "cd" }, .{ 0x247, "e" }, .{ 0x249, "f" },
.{ 0x24D, "g" },
};
for (strings) |entry| buffer[entry[0] - 1] = entry[1];
return .{ .strings = buffer };
}
/// A palette for the tests, entry `i` of which is `(i, 255 - i, 7)`.
fn testPalette() tga.Palette {
var palette: tga.Palette = undefined;
for (&palette, 0..) |*entry, i| entry.* = .{ @intCast(i), @intCast(255 - i), 7 };
return palette;
}
fn colourOf(palette: *const tga.Palette, entry: u8) [4]u8 {
return .{ palette[entry][0], palette[entry][1], palette[entry][2], hud.Pane.opaque_alpha };
}
test drawTitle {
const font = try @import("../../../formats/fnt.zig").Font.parse(comptime testFont());
const opened: hud.Opened = .open(font, null);
const palette = testPalette();
var buffer: [0x555][]const u8 = undefined;
const strings = testStrings(&buffer);
const kit: Kit = .{ .title_font = &opened, .info_font = &opened, .palette = &palette, .strings = &strings };
const gpa = std.testing.allocator;
const panels: Image = try .create(gpa, 1);
defer panels.deinit(gpa);
@memset(panels.pixels, .{ 1, 2, 3, 4 });
const image: Image = try .create(gpa, 1);
defer image.deinit(gpa);
try drawTitle(image, panels, kit, tables.ships[0]);
// The title's letters' tops in black, their edges bright, and the panel's own pixels where
// they are clear.
try std.testing.expectEqual(colourOf(&palette, 0), image.pixels[title_at[1] * size + title_at[0]]);
try std.testing.expectEqual(colourOf(&palette, 0xB8), image.pixels[title_at[1] * size + title_at[0] + 1]);
try std.testing.expectEqual([4]u8{ 1, 2, 3, 4 }, image.pixels[(title_at[1] + 1) * size + title_at[0]]);
// The name, `PREDATOR`: eight letters.
const name_end = name_at[0] + 8 * 2;
try std.testing.expectEqual(colourOf(&palette, 0), image.pixels[name_at[1] * size + name_end - 2]);
try std.testing.expectEqual([4]u8{ 1, 2, 3, 4 }, image.pixels[name_at[1] * size + name_end]);
}
test "a larger picture draws larger panels" {
const font = try @import("../../../formats/fnt.zig").Font.parse(comptime testFont());
const opened: hud.Opened = .open(font, null);
const palette = testPalette();
var buffer: [0x555][]const u8 = undefined;
const strings = testStrings(&buffer);
const kit: Kit = .{ .title_font = &opened, .info_font = &opened, .palette = &palette, .strings = &strings };
const gpa = std.testing.allocator;
const panels: Image = try .create(gpa, 2);
defer panels.deinit(gpa);
@memset(panels.pixels, .{ 1, 2, 3, 4 });
const image: Image = try .create(gpa, 2);
defer image.deinit(gpa);
const side = 2 * size;
try std.testing.expectEqual(side, image.side());
// Each of the original's pixels is a square of two by two: the title's letters' tops in black,
// their edges bright, over the panel's own pixels.
try drawTitle(image, panels, kit, tables.ships[0]);
const corner = 2 * title_at[1] * side + 2 * title_at[0];
for ([_]usize{ 0, 1, side, side + 1 }) |at| {
try std.testing.expectEqual(colourOf(&palette, 0), image.pixels[corner + at]);
try std.testing.expectEqual(colourOf(&palette, 0xB8), image.pixels[corner + 2 + at]);
}
try std.testing.expectEqual([4]u8{ 1, 2, 3, 4 }, image.pixels[corner + 2 * side]);
// So are the bars' segments: the first row's first, filled from its corner to the row above its
// foot.
try drawStats(image, kit, tables.ships[0], .{ 6, 6, 10, 5, 3, 5, 100, 2 });
const bar = colourOf(&palette, tables.bar_colour);
const segment = 2 * first_row_y * side + 2 * figure_x;
try std.testing.expectEqual(bar, image.pixels[segment]);
try std.testing.expectEqual(bar, image.pixels[segment + (2 * segment_foot - 1) * side + 2 * segment_right + 1]);
try std.testing.expectEqual([4]u8{ 0, 0, 0, 0 }, image.pixels[segment + 2 * segment_foot * side]);
}
test drawStats {
const font = try @import("../../../formats/fnt.zig").Font.parse(comptime testFont());
const opened: hud.Opened = .open(font, null);
const palette = testPalette();
var buffer: [0x555][]const u8 = undefined;
const strings = testStrings(&buffer);
const kit: Kit = .{ .title_font = &opened, .info_font = &opened, .palette = &palette, .strings = &strings };
const gpa = std.testing.allocator;
const image: Image = try .create(gpa, 1);
defer image.deinit(gpa);
@memset(image.pixels, .{ 9, 9, 9, 9 });
try drawStats(image, kit, tables.ships[0], .{ 6, 6, 10, 5, 3, 5, 100, 2 });
const top = colourOf(&palette, tables.text_remap[15]);
const bar = colourOf(&palette, tables.bar_colour);
const clear: [4]u8 = .{ 0, 0, 0, 0 };
// The class, `CLASS : LIGHT`, 13 letters from its corner, on a clear image.
try std.testing.expectEqual(top, image.pixels[class_at[1] * size + class_at[0]]);
try std.testing.expectEqual(top, image.pixels[class_at[1] * size + class_at[0] + 12 * 2]);
try std.testing.expectEqual(clear, image.pixels[class_at[1] * size + class_at[0] + 13 * 2]);
// The first row's label, and its bar: six segments filled, the seventh outlined.
try std.testing.expectEqual(top, image.pixels[first_row_y * size + label_x]);
const row = first_row_y * size;
try std.testing.expectEqual(bar, image.pixels[row + figure_x]);
try std.testing.expectEqual(bar, image.pixels[row + 3 * size + figure_x + 5 * segment_step + 2]);
try std.testing.expectEqual(clear, image.pixels[row + segment_foot * size + figure_x + 5 * segment_step + 2]);
try std.testing.expectEqual(bar, image.pixels[row + figure_x + 6 * segment_step]);
try std.testing.expectEqual(clear, image.pixels[row + 3 * size + figure_x + 6 * segment_step + 2]);
try std.testing.expectEqual(bar, image.pixels[row + segment_foot * size + figure_x + 6 * segment_step + 2]);
try std.testing.expectEqual(bar, image.pixels[row + 3 * size + figure_x + 9 * segment_step + segment_right]);
try std.testing.expectEqual(clear, image.pixels[row + figure_x + 9 * segment_step + segment_right + 1]);
// The afterburner fuel as a number and its seconds, `100 SECS`: eight letters.
const fuel = (first_row_y + 6 * row_step) * size + figure_x;
try std.testing.expectEqual(top, image.pixels[fuel + 7 * 2]);
try std.testing.expectEqual(clear, image.pixels[fuel + 8 * 2]);
// The Predator's one special.
try std.testing.expectEqual(top, image.pixels[specials_at[1] * size + specials_at[0] + 9 * 2]);
try std.testing.expectEqual(clear, image.pixels[specials_at[1] * size + specials_at[0] + 10 * 2]);
}
test drawMissile {
const font = try @import("../../../formats/fnt.zig").Font.parse(comptime testFont());
const opened: hud.Opened = .open(font, null);
const palette = testPalette();
var buffer: [0x555][]const u8 = undefined;
const strings = testStrings(&buffer);
const kit: Kit = .{ .title_font = &opened, .info_font = &opened, .palette = &palette, .strings = &strings };
const gpa = std.testing.allocator;
const image: Image = try .create(gpa, 1);
defer image.deinit(gpa);
const top = colourOf(&palette, tables.text_remap[15]);
const bar = colourOf(&palette, tables.bar_colour);
const clear: [4]u8 = .{ 0, 0, 0, 0 };
try drawMissile(image, kit, .havoc, .{ 2, 5, 6, 4 });
// Its name, `HAVOK`, five letters from its corner.
const name = missile_name_at[1] * size + missile_name_at[0];
try std.testing.expectEqual(top, image.pixels[name + 4 * 2]);
try std.testing.expectEqual(clear, image.pixels[name + 5 * 2]);
// `CLICK MISSILE`, thirteen letters, centred.
const click = click_at[0][1] * size + click_at[0][0];
try std.testing.expectEqual(top, image.pixels[click - 13]);
try std.testing.expectEqual(clear, image.pixels[click - 14]);
// The locking time in seconds, `2 SECS`, and the speed's bar, five segments filled.
const locking = missile_first_row_y * size + figure_x;
try std.testing.expectEqual(top, image.pixels[locking + 5 * 2]);
try std.testing.expectEqual(clear, image.pixels[locking + 6 * 2]);
const speed = (missile_first_row_y + row_step + 3) * size + figure_x;
try std.testing.expectEqual(bar, image.pixels[speed + 4 * segment_step + 2]);
try std.testing.expectEqual(clear, image.pixels[speed + 5 * segment_step + 2]);
// The fuel pod's rows are dashes, whatever its figures.
try drawMissile(image, kit, .fuel_pod, .{ 2, 5, 6, 4 });
try std.testing.expectEqual(top, image.pixels[locking]);
try std.testing.expectEqual(clear, image.pixels[locking + 2]);
try std.testing.expectEqual(clear, image.pixels[speed + 2]);
}
test drawGuns {
const font = try @import("../../../formats/fnt.zig").Font.parse(comptime testFont());
const opened: hud.Opened = .open(font, null);
const palette = testPalette();
var buffer: [0x555][]const u8 = undefined;
const strings = testStrings(&buffer);
const kit: Kit = .{ .title_font = &opened, .info_font = &opened, .palette = &palette, .strings = &strings };
const gpa = std.testing.allocator;
const image: Image = try .create(gpa, 1);
defer image.deinit(gpa);
@memset(image.pixels, .{ 9, 9, 9, 9 });
const top = colourOf(&palette, tables.text_remap[15]);
const clear: [4]u8 = .{ 0, 0, 0, 0 };
// The Predator's guns: two lasers, `LASER X 2`, nine letters at the top, on a clear image.
try drawGuns(image, kit, tables.ships[0]);
try std.testing.expectEqual(top, image.pixels[gun_x + 8 * 2]);
try std.testing.expectEqual(clear, image.pixels[gun_x + 9 * 2]);
// Their description, one line of seven letters.
const description = gun_step * size + gun_x;
try std.testing.expectEqual(top, image.pixels[description + 6 * 2]);
try std.testing.expectEqual(clear, image.pixels[description + 7 * 2]);
// Then the rear turret, `REAR X 1`, with no description under it.
const turret = (gun_step + gun_description_line_height + gun_description_gap) * size + gun_x;
try std.testing.expectEqual(top, image.pixels[turret + 7 * 2]);
try std.testing.expectEqual(clear, image.pixels[turret + 8 * 2]);
try std.testing.expectEqual(clear, image.pixels[turret + gun_step * size]);
}
test specialsLine {
var buffer: [0x555][]const u8 = undefined;
const strings = testStrings(&buffer);
const palette = testPalette();
const font = try @import("../../../formats/fnt.zig").Font.parse(comptime testFont());
const opened: hud.Opened = .open(font, null);
const kit: Kit = .{ .title_font = &opened, .info_font = &opened, .palette = &palette, .strings = &strings };
var line = specialsLine(kit, tables.ships[10].specials);
try std.testing.expectEqualStrings("Reverse Thrust, Spectral Shields, Blind Fire, Cloaking Device", line.slice());
var none = specialsLine(kit, tables.ships[2].specials);
try std.testing.expectEqualStrings("", none.slice());
}
test Face {
try std.testing.expectEqualStrings("fpanels", Face.front.titleTexture());
try std.testing.expectEqualStrings("binfo", Face.back.infoTexture());
}