Debian Codegraph @

src/cdogs/draw/draw.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2016, 2018-2022, 2024-2025 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #include <assert.h>
50 #include <math.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "actor_pickup.h"
55 #include "actors.h"
56 #include "algorithms.h"
57 #include "blit.h"
58 #include "config.h"
59 #include "door.h"
60 #include "draw/draw.h"
61 #include "draw/draw_actor.h"
62 #include "draw/drawtools.h"
63 #include "font.h"
64 #include "game_events.h"
65 #include "net_util.h"
66 #include "objs.h"
67 #include "pic_manager.h"
68 #include "pickup.h"
69 #include "pics.h"
70 #include "texture.h"
71
72 // #define DEBUG_DRAW_HITBOXES
73
74 // Three types of tile drawing, based on line of sight:
75 // Unvisited: black
76 // Out of sight: dark, or if fog disabled, black
77 // In sight: full color
78 typedef enum
79 {
80 TILE_LOS_NORMAL,
81 TILE_LOS_FOG,
82 TILE_LOS_NONE
83 } TileLOS;
84 static TileLOS GetTileLOS(const Tile *tile, const bool useFog)
85 {
86 if (!tile->isVisited)
87 {
88 return TILE_LOS_NONE;
89 }
90 if (tile->outOfSight)
91 {
92 return useFog ? TILE_LOS_FOG : TILE_LOS_NONE;
93 }
94 return TILE_LOS_NORMAL;
95 }
96 static color_t GetLOSMask(const Tile *tile, const bool useFog)
97 {
98 switch (GetTileLOS(tile, useFog))
99 {
100 case TILE_LOS_NORMAL:
101 return colorWhite;
102 case TILE_LOS_FOG:
103 return colorFog;
104 case TILE_LOS_NONE:
105 default:
106 // don't draw
107 return colorTransparent;
108 }
109 }
110 static void DrawLOSPic(
111 const Tile *tile, const Pic *pic, const struct vec2i pos,
112 const bool useFog)
113 {
114 const color_t mask = GetLOSMask(tile, useFog);
115 if (!ColorEquals(mask, colorTransparent))
116 {
117 PicRender(
118 pic, gGraphicsDevice.gameWindow.renderer, pos, mask, 0,
119 svec2_one(), SDL_FLIP_NONE, Rect2iZero());
120 }
121 }
122
123 static void DrawThing(
124 DrawBuffer *b, const Thing *t, const struct vec2i offset);
125
126 static void DrawTiles(
127 DrawBuffer *b, const struct vec2i offset,
128 void (*drawTileFunc)(
129 DrawBuffer *, const struct vec2i, const Tile *, const struct vec2i,
130 const bool))
131 {
132 const bool useFog = ConfigGetBool(&gConfig, "Game.Fog");
133 const Tile **tile = DrawBufferGetFirstTile(b);
134 struct vec2i pos;
135 int x, y;
136 for (y = 0, pos.y = b->dy + offset.y; y < Y_TILES;
137 y++, pos.y += TILE_HEIGHT)
138 {
139 CArrayClear(&b->displaylist);
140 for (x = 0, pos.x = b->dx + offset.x; x < b->Size.x;
141 x++, tile++, pos.x += TILE_WIDTH)
142 {
143 if (*tile == NULL)
144 continue;
145 drawTileFunc(b, offset, *tile, pos, useFog);
146 }
147 DrawBufferSortDisplayList(b);
148 CA_FOREACH(const Thing *, tp, b->displaylist)
149 DrawThing(b, *tp, offset);
150 CA_FOREACH_END()
151 tile += X_TILES - b->Size.x;
152 }
153 }
154
155 static void DrawFloor(
156 DrawBuffer *b, const struct vec2i offset, const Tile *t,
157 const struct vec2i pos, const bool useFog);
158 static void DrawThingsBelow(
159 DrawBuffer *b, const struct vec2i offset, const Tile *t,
160 const struct vec2i pos, const bool useFog);
161 static void DrawWallsAndThings(
162 DrawBuffer *b, const struct vec2i offset, const Tile *t,
163 const struct vec2i pos, const bool useFog);
164 static void DrawThingsAbove(
165 DrawBuffer *b, const struct vec2i offset, const Tile *t,
166 const struct vec2i pos, const bool useFog);
167 static void DrawObjectiveHighlights(
168 DrawBuffer *b, const struct vec2i offset, const Tile *t,
169 const struct vec2i pos, const bool useFog);
170 static void DrawChatters(
171 DrawBuffer *b, const struct vec2i offset, const Tile *t,
172 const struct vec2i pos, const bool useFog);
173 static void DrawPickupMenus(
174 DrawBuffer *b, const struct vec2i offset, const Tile *t,
175 const struct vec2i pos, const bool useFog);
176 static void DrawExtra(
177 DrawBuffer *b, struct vec2i offset, const DrawBufferArgs *args);
178
179 void DrawBufferDraw(
180 DrawBuffer *b, struct vec2i offset, const DrawBufferArgs *args)
181 {
182 // First draw the floor tiles (which do not obstruct anything)
183 DrawTiles(b, offset, DrawFloor);
184 // Then draw things that are below everything like debris (wrecks)
185 DrawTiles(b, offset, DrawThingsBelow);
186 // Now draw walls and (non-wreck) things in proper order
187 DrawTiles(b, offset, DrawWallsAndThings);
188 // Draw things that are above everything
189 DrawTiles(b, offset, DrawThingsAbove);
190 if (args->HUD)
191 {
192 // Draw objective highlights, for visible and always-visible objectives
193 DrawTiles(b, offset, DrawObjectiveHighlights);
194 // Draw actor chatter
195 DrawTiles(b, offset, DrawChatters);
196 // Draw actor pickup menus
197 DrawTiles(b, offset, DrawPickupMenus);
198 }
199 // Draw editor-only things
200 DrawExtra(b, offset, args);
201 }
202
203 static void DrawFloor(
204 DrawBuffer *b, const struct vec2i offset, const Tile *t,
205 const struct vec2i pos, const bool useFog)
206 {
207 UNUSED(b);
208 UNUSED(offset);
209 if (t->Class != NULL && t->Class->Pic != NULL &&
210 t->Class->Pic->Data != NULL && t->Class->Type != TILE_CLASS_WALL)
211 {
212 DrawLOSPic(t, t->Class->Pic, pos, useFog);
213 }
214 }
215
216 static void DrawThingsBelow(
217 DrawBuffer *b, const struct vec2i offset, const Tile *t,
218 const struct vec2i pos, const bool useFog)
219 {
220 UNUSED(pos);
221 UNUSED(offset);
222 UNUSED(useFog);
223 if (t->outOfSight)
224 {
225 return;
226 }
227 CA_FOREACH(ThingId, tid, t->things)
228 const Thing *ti = ThingIdGetThing(tid);
229 if (ThingDrawBelow(ti))
230 {
231 CArrayPushBack(&b->displaylist, &ti);
232 }
233 CA_FOREACH_END()
234 }
235
236 static void DrawWallsAndThings(
237 DrawBuffer *b, const struct vec2i offset, const Tile *t,
238 const struct vec2i pos, const bool useFog)
239 {
240 UNUSED(offset);
241 if (t->Class->Type == TILE_CLASS_WALL)
242 {
243 DrawLOSPic(
244 t, t->Class->Pic, svec2i_add(pos, svec2i(0, WALL_OFFSET_Y)),
245 useFog);
246 }
247 else if (t->Class->Type == TILE_CLASS_DOOR)
248 {
249 const color_t mask = GetLOSMask(t, useFog);
250 if (!ColorEquals(mask, colorTransparent))
251 {
252 DoorDraw(&t->Door, pos, mask);
253 }
254 }
255
256 // Draw the items that are in LOS
257 if (t->outOfSight)
258 {
259 return;
260 }
261 CA_FOREACH(ThingId, tid, t->things)
262 const Thing *ti = ThingIdGetThing(tid);
263 if (ThingDrawBelow(ti) || ThingDrawAbove(ti))
264 {
265 continue;
266 }
267 CArrayPushBack(&b->displaylist, &ti);
268 CA_FOREACH_END()
269 }
270
271 static void DrawThingsAbove(
272 DrawBuffer *b, const struct vec2i offset, const Tile *t,
273 const struct vec2i pos, const bool useFog)
274 {
275 UNUSED(offset);
276 UNUSED(pos);
277 UNUSED(useFog);
278 if (t->outOfSight)
279 {
280 return;
281 }
282 CA_FOREACH(ThingId, tid, t->things)
283 const Thing *ti = ThingIdGetThing(tid);
284 if (ThingDrawAbove(ti))
285 {
286 CArrayPushBack(&b->displaylist, &ti);
287 }
288 CA_FOREACH_END()
289 }
290
291 static void DrawObjectiveHighlights(
292 DrawBuffer *b, const struct vec2i offset, const Tile *t,
293 const struct vec2i pos, const bool useFog)
294 {
295 UNUSED(pos);
296 UNUSED(useFog);
297 CA_FOREACH(ThingId, tid, t->things)
298 Thing *ti = ThingIdGetThing(tid);
299 const Pic *pic = NULL;
300 color_t color = colorWhite;
301 struct vec2i drawOffsetExtra = svec2i_zero();
302
303 if (ti->flags & THING_OBJECTIVE)
304 {
305 // Objective
306 const int objective = ObjectiveFromThing(ti->flags);
307 const Objective *o =
308 CArrayGet(&gMission.missionData->Objectives, objective);
309 if (o->Flags & OBJECTIVE_HIDDEN)
310 {
311 continue;
312 }
313 if (!(o->Flags & OBJECTIVE_POSKNOWN) && t->outOfSight)
314 {
315 continue;
316 }
317 switch (o->Type)
318 {
319 case OBJECTIVE_KILL:
320 case OBJECTIVE_DESTROY: // fallthrough
321 pic = PicManagerGetPic(&gPicManager, "hud/objective_kill");
322 break;
323 case OBJECTIVE_RESCUE:
324 case OBJECTIVE_COLLECT: // fallthrough
325 pic = PicManagerGetPic(&gPicManager, "hud/objective_collect");
326 break;
327 default:
328 CASSERT(false, "unexpected objective to draw");
329 continue;
330 }
331 color = o->color;
332 if (ti->kind == KIND_CHARACTER)
333 {
334 drawOffsetExtra.y -= 10;
335 }
336 }
337 else if (ti->kind == KIND_PICKUP)
338 {
339 // Require LOS for non-deathmatch modes
340 if (!IsPVP(gCampaign.Entry.Mode) && t->outOfSight)
341 {
342 continue;
343 }
344 // Gun pickup or keycard
345 const Pickup *p = CArrayGet(&gPickups, ti->id);
346 if (!PickupClassHasKeyEffect(p->class) && !PickupIsManual(NULL, p))
347 {
348 continue;
349 }
350 pic = CPicGetPic(&p->thing.CPic, 0);
351 color = colorDarker;
352 color.a = (Uint8)Pulse256(gMission.time);
353 }
354
355 if (pic != NULL)
356 {
357 const struct vec2i picPos = svec2i_add(
358 svec2i_subtract(svec2i_floor(ti->Pos), svec2i(b->xTop, b->yTop)),
359 offset);
360 color.a = (Uint8)Pulse256(gMission.time);
361 // Centre the drawing
362 const struct vec2i drawOffset = svec2i_scale_divide(pic->size, -2);
363 PicRender(
364 pic, gGraphicsDevice.gameWindow.renderer,
365 svec2i_add(picPos, svec2i_add(drawOffset, drawOffsetExtra)), color,
366 0, svec2_one(), SDL_FLIP_NONE, Rect2iZero());
367 }
368 CA_FOREACH_END()
369 }
370
371 #define ACTOR_HEIGHT 25
372 static void DrawChatters(
373 DrawBuffer *b, const struct vec2i offset, const Tile *t,
374 const struct vec2i pos, const bool useFog)
375 {
376 UNUSED(pos);
377 UNUSED(useFog);
378 CA_FOREACH(ThingId, tid, t->things)
379 // Draw the items that are in LOS
380 if (t->outOfSight)
381 {
382 continue;
383 }
384 const Thing *ti = ThingIdGetThing(tid);
385 if (ti->kind != KIND_CHARACTER)
386 {
387 continue;
388 }
389
390 const TActor *a = CArrayGet(&gActors, ti->id);
391 // Draw character text
392 if (strlen(a->Chatter) > 0)
393 {
394 const struct vec2i textPos = svec2i(
395 (int)a->thing.Pos.x - b->xTop + offset.x -
396 FontStrW(a->Chatter) / 2,
397 (int)a->thing.Pos.y - b->yTop + offset.y - ACTOR_HEIGHT);
398 const color_t mask = GetLOSMask(t, useFog);
399 if (!ColorEquals(mask, colorTransparent))
400 {
401 FontStrMask(a->Chatter, textPos, mask);
402 }
403 }
404 CA_FOREACH_END()
405 }
406
407 static void DrawPickupMenu(
408 DrawBuffer *b, const TActor *a, const struct vec2i offset);
409 static void DrawPickupMenus(
410 DrawBuffer *b, const struct vec2i offset, const Tile *t,
411 const struct vec2i pos, const bool useFog)
412 {
413 UNUSED(pos);
414 CA_FOREACH(ThingId, tid, t->things)
415 // Draw the items that are in LOS
416 if (t->outOfSight || ColorEquals(GetLOSMask(t, useFog), colorTransparent))
417 {
418 continue;
419 }
420 const Thing *ti = ThingIdGetThing(tid);
421 if (ti->kind != KIND_CHARACTER)
422 {
423 continue;
424 }
425
426 const TActor *a = CArrayGet(&gActors, ti->id);
427 // Draw pickup menu
428 if (!a->pickupMenu.pickup || !ActorIsLocalPlayer(a->uid))
429 {
430 continue;
431 }
432
433 DrawPickupMenu(b, a, offset);
434 CA_FOREACH_END()
435 }
436 static void DrawPickupMenu(
437 DrawBuffer *b, const TActor *a, const struct vec2i offset)
438 {
439 // Calculate width and height of menu and draw centered on the actor
440 // TODO: may be expensive to do per-frame; cache somewhere?
441 struct vec2i size = svec2i_zero();
442 struct vec2i ssize = FontStrSize(a->pickupMenu.effect->u.Menu.Text);
443 size.x = MAX(size.x, ssize.x);
444 size.y += ssize.y;
445 size.y += FontH(); // Separator
446 CA_FOREACH(const PickupMenuItem, m, a->pickupMenu.effect->u.Menu.Items)
447 ssize = FontStrSize(m->Text);
448 size.x = MAX(size.x, ssize.x);
449 size.y += ssize.y;
450 CA_FOREACH_END()
451
452 struct vec2i pos = svec2i(
453 (int)a->thing.Pos.x - b->xTop + offset.x - size.x / 2,
454 (int)a->thing.Pos.y - b->yTop + offset.y - size.y / 2);
455 const int startX = pos.x;
456 // Draw box bg with a bit of padding
457 const color_t cbg = {64, 64, 64, 128};
458 DrawRectangle(
459 b->g, svec2i_subtract(pos, svec2i(2, 2)),
460 svec2i_add(size, svec2i(4, 4)), cbg, true);
461 pos = FontStr(a->pickupMenu.effect->u.Menu.Text, pos);
462 pos.x = startX;
463 pos.y += FontH() * 2; // Separator
464 CA_FOREACH(const PickupMenuItem, m, a->pickupMenu.effect->u.Menu.Items)
465 const bool selected = _ca_index == a->pickupMenu.index;
466 if (selected)
467 {
468 // Add 1px padding
469 const struct vec2i bgPos = svec2i_subtract(pos, svec2i_one());
470 const struct vec2i bgSize =
471 svec2i_add(svec2i(size.x, FontH()), svec2i(2, 2));
472 DrawRectangle(b->g, bgPos, bgSize, colorSelectedBG, true);
473 }
474 pos = FontStrMask(m->Text, pos, selected ? colorRed : colorWhite);
475 pos.x = startX;
476 pos.y += FontH();
477 CA_FOREACH_END()
478 }
479
480 static void DrawThing(DrawBuffer *b, const Thing *t, const struct vec2i offset)
481 {
482 const struct vec2i picPos = svec2i_add(
483 svec2i_subtract(
484 svec2i_floor(svec2_add(t->Pos, t->drawShake)),
485 svec2i(b->xTop, b->yTop)),
486 offset);
487
488 if (!svec2i_is_zero(t->ShadowSize))
489 {
490 DrawShadow(
491 &gGraphicsDevice, picPos, svec2_assign_vec2i(t->ShadowSize),
492 colorBlack);
493 }
494
495 if (t->CPicFunc)
496 {
497 t->CPicFunc(b->g, t->id, picPos);
498 }
499 else if (t->kind == KIND_CHARACTER)
500 {
501 const TActor *a = CArrayGet(&gActors, t->id);
502 ActorPics pics = GetCharacterPicsFromActor(a);
503 DrawActorPics(&pics, picPos, Rect2iZero());
504 // Draw weapon indicators
505 DrawLaserSight(&pics, a, picPos);
506 }
507 else
508 {
509 (*(t->drawFunc))(picPos, &t->drawData);
510 }
511
512 #ifdef DEBUG_DRAW_HITBOXES
513 const int pulsePeriod = ConfigGetInt(&gConfig, "Game.FPS");
514 int alphaUnscaled =
515 (gMission.time % pulsePeriod) * 255 / (pulsePeriod / 2);
516 if (alphaUnscaled > 255)
517 {
518 alphaUnscaled = 255 * 2 - alphaUnscaled;
519 }
520 color_t color = colorPurple;
521 color.a = (Uint8)alphaUnscaled;
522 DrawRectangle(
523 &gGraphicsDevice,
524 svec2i_subtract(picPos, svec2i_scale_divide(t->size, 2)), t->size,
525 color, false);
526 #endif
527 }
528
529 static void DrawPickups(
530 DrawBuffer *b, const Map *map, const struct vec2i offset);
531 static void DrawEditorTiles(
532 DrawBuffer *b, const Map *map, const struct vec2i offset);
533 static void DrawGuideImage(
534 const DrawBuffer *b, const Pic *guideImage, const uint8_t alpha);
535 static void DrawObjectNames(DrawBuffer *b, const struct vec2i offset);
536 static void DrawExtra(
537 DrawBuffer *b, struct vec2i offset, const DrawBufferArgs *args)
538 {
539 // Draw guide image
540 if (args->GuideImage && !PicIsNone(args->GuideImage) &&
541 args->GuideImageAlpha > 0)
542 {
543 DrawGuideImage(b, args->GuideImage, args->GuideImageAlpha);
544 }
545 if (args->Editor)
546 {
547 // Draw pickups, in case they are obscured by walls
548 DrawPickups(b, &gMap, offset);
549 DrawEditorTiles(b, &gMap, offset);
550 DrawObjectNames(b, offset);
551 }
552 }
553
554 static void DrawPickups(
555 DrawBuffer *b, const Map *map, const struct vec2i offset)
556 {
557 struct vec2i pos;
558 pos.y = b->dy + offset.y;
559 for (int y = 0; y < Y_TILES; y++, pos.y += TILE_HEIGHT)
560 {
561 pos.x = b->dx + offset.x;
562 for (int x = 0; x < b->Size.x; x++, pos.x += TILE_WIDTH)
563 {
564 const struct vec2i tilePos = svec2i(x + b->xStart, y + b->yStart);
565 if (!MapIsTileIn(map, tilePos))
566 {
567 continue;
568 }
569
570 const Tile *t = MapGetTile(map, tilePos);
571 CA_FOREACH(const ThingId, tid, t->things)
572 const Thing *ti = ThingIdGetThing(tid);
573 if (ti->kind == KIND_PICKUP)
574 {
575 DrawThing(b, ti, offset);
576 }
577 CA_FOREACH_END()
578 }
579 }
580 }
581 static void DrawEditorTiles(
582 DrawBuffer *b, const Map *map, const struct vec2i offset)
583 {
584 const color_t exitColor = colorDarker;
585 struct vec2i pos;
586 pos.y = b->dy + offset.y;
587 for (int y = 0; y < Y_TILES; y++, pos.y += TILE_HEIGHT)
588 {
589 pos.x = b->dx + offset.x;
590 for (int x = 0; x < b->Size.x; x++, pos.x += TILE_WIDTH)
591 {
592 const struct vec2i tilePos = svec2i(x + b->xStart, y + b->yStart);
593 if (!MapIsTileIn(map, tilePos))
594 {
595 continue;
596 }
597
598 // Access highlight
599 const uint16_t al = MapGetAccessLevel(map, tilePos);
600 if (al != 0)
601 {
602 color_t highlight = KeyColor(al);
603 highlight.a = 64;
604 DrawRectangle(
605 b->g, pos, svec2i(TILE_WIDTH, TILE_HEIGHT), highlight,
606 true);
607 }
608
609 // mission start
610 if (!svec2i_is_zero(map->start) &&
611 svec2i_is_equal(map->start, tilePos))
612 {
613 PicRender(
614 PicManagerGetPic(&gPicManager, "editor/start"),
615 b->g->gameWindow.renderer, pos, colorWhite, 0, svec2_one(),
616 SDL_FLIP_NONE, Rect2iZero());
617 }
618
619 // exit tiles
620 CA_FOREACH(const Exit, e, map->exits)
621 Rect2i exitR = e->R;
622 exitR.Size = svec2i_add(exitR.Size, svec2i_one());
623 if (!Rect2iIsInside(exitR, tilePos))
624 {
625 continue;
626 }
627 if (tilePos.x == e->R.Pos.x)
628 {
629 // Left edge
630 DrawLine(
631 pos, svec2i_add(pos, svec2i(0, TILE_HEIGHT - 1)),
632 exitColor);
633 }
634 else if (tilePos.x == e->R.Pos.x + e->R.Size.x)
635 {
636 // Right edge
637 DrawLine(
638 svec2i_add(pos, svec2i(TILE_WIDTH - 1, 0)),
639 svec2i_add(pos, svec2i(TILE_WIDTH - 1, TILE_HEIGHT - 1)),
640 exitColor);
641 }
642 if (tilePos.y == e->R.Pos.y)
643 {
644 // Top edge
645 DrawLine(
646 pos, svec2i_add(pos, svec2i(TILE_WIDTH - 1, 0)),
647 exitColor);
648 }
649 else if (tilePos.y == e->R.Pos.y + e->R.Size.y)
650 {
651 // Bottom edge
652 DrawLine(
653 svec2i_add(pos, svec2i(0, TILE_HEIGHT - 1)),
654 svec2i_add(pos, svec2i(TILE_WIDTH - 1, TILE_HEIGHT - 1)),
655 exitColor);
656 }
657 if (svec2i_is_equal(tilePos, e->R.Pos))
658 {
659 // Label the exit
660 char buf[256];
661 sprintf(
662 buf, "Exit %d\nMission %d\n(%d,%d)\n(%dx%d)", _ca_index,
663 e->Mission + 1, e->R.Pos.x, e->R.Pos.y, e->R.Size.x,
664 e->R.Size.y);
665 FontStrMask(buf, svec2i_add(pos, svec2i(3, 3)), exitColor);
666 }
667 CA_FOREACH_END()
668
669 // Walk-through walls
670 const Tile *tile = MapGetTile(map, tilePos);
671 if (tile->Class->Type == TILE_CLASS_WALL && tile->Class->canWalk)
672 {
673 DrawRectangle(
674 b->g, pos, svec2i(TILE_WIDTH, TILE_HEIGHT), colorGreen,
675 false);
676 }
677 }
678 }
679 }
680
681 static void DrawGuideImage(
682 const DrawBuffer *b, const Pic *guideImage, const uint8_t alpha)
683 {
684 // Scale based on ratio between map size and guide image size,
685 // so that the guide image stretches to the map size
686 float xScale = (float)(gMap.Size.x * TILE_WIDTH) / guideImage->size.x;
687 float yScale = (float)(gMap.Size.y * TILE_HEIGHT) / guideImage->size.y;
688 const struct vec2i pos = svec2i(-b->xTop, -b->yTop);
689 color_t mask = colorWhite;
690 mask.a = alpha;
691 TextureRender(
692 guideImage->Tex, gGraphicsDevice.gameWindow.renderer, Rect2iZero(),
693 Rect2iNew(
694 pos, svec2i(
695 (mint_t)MROUND(guideImage->size.x * xScale),
696 (mint_t)MROUND(guideImage->size.y * yScale))),
697 mask, 0, SDL_FLIP_NONE);
698 }
699
700 // Draw names of objects (objectives, spawners etc.)
701 static void DrawObjectiveName(
702 const Thing *ti, DrawBuffer *b, const struct vec2i offset);
703 static void DrawSpawnerName(
704 const TObject *obj, DrawBuffer *b, const struct vec2i offset,
705 const char *name);
706 static void DrawPickupName(
707 const Pickup *p, DrawBuffer *b, const struct vec2i offset);
708 static void DrawObjectNames(DrawBuffer *b, const struct vec2i offset)
709 {
710 const Tile **tile = DrawBufferGetFirstTile(b);
711 for (int y = 0; y < Y_TILES; y++)
712 {
713 for (int x = 0; x < b->Size.x; x++, tile++)
714 {
715 if (*tile == NULL)
716 continue;
717 CA_FOREACH(ThingId, tid, (*tile)->things)
718 const Thing *ti = ThingIdGetThing(tid);
719 if (ti->flags & THING_OBJECTIVE)
720 {
721 DrawObjectiveName(ti, b, offset);
722 }
723 else if (ti->kind == KIND_OBJECT)
724 {
725 const TObject *obj = CArrayGet(&gObjs, ti->id);
726 switch (obj->Class->Type)
727 {
728 case MAP_OBJECT_TYPE_PICKUP_SPAWNER:
729 DrawSpawnerName(
730 obj, b, offset, obj->Class->u.PickupClass->Name);
731 break;
732 case MAP_OBJECT_TYPE_ACTOR_SPAWNER: {
733 const Character *c = CArrayGet(
734 &gCampaign.Setting.characters.OtherChars,
735 obj->Class->u.Character.CharId);
736 DrawSpawnerName(obj, b, offset, c->Class->Name);
737 }
738 break;
739 default:
740 break;
741 }
742 }
743 else if (ti->kind == KIND_PICKUP)
744 {
745 const Pickup *p = CArrayGet(&gPickups, ti->id);
746 DrawPickupName(p, b, offset);
747 }
748 CA_FOREACH_END()
749 }
750 tile += X_TILES - b->Size.x;
751 }
752 }
753 static void DrawObjectiveName(
754 const Thing *ti, DrawBuffer *b, const struct vec2i offset)
755 {
756 const int objective = ObjectiveFromThing(ti->flags);
757 const Objective *o =
758 CArrayGet(&gMission.missionData->Objectives, objective);
759 const char *typeName = ObjectiveTypeStr(o->Type);
760 const struct vec2i textPos = svec2i(
761 (int)ti->Pos.x - b->xTop + offset.x - FontStrW(typeName) / 2,
762 (int)ti->Pos.y - b->yTop + offset.y);
763 FontStr(typeName, textPos);
764 }
765 static void DrawSpawnerName(
766 const TObject *obj, DrawBuffer *b, const struct vec2i offset,
767 const char *name)
768 {
769 const struct vec2i textPos = svec2i(
770 (int)obj->thing.Pos.x - b->xTop + offset.x - FontStrW(name) / 2,
771 (int)obj->thing.Pos.y - b->yTop + offset.y);
772 FontStr(name, textPos);
773 }
774 static void DrawPickupName(
775 const Pickup *p, DrawBuffer *b, const struct vec2i offset)
776 {
777 CA_FOREACH(const PickupEffect, pe, p->class->Effects)
778 if (pe->Type == PICKUP_KEYCARD)
779 {
780 const char *label = "key";
781 const color_t c = KeyColor(pe->u.Keys);
782 const struct vec2i textPos = svec2i(
783 (int)p->thing.Pos.x - b->xTop + offset.x - FontStrW(label) / 2,
784 (int)p->thing.Pos.y - b->yTop + offset.y);
785 FontStrMask(label, textPos, c);
786 break;
787 }
788 CA_FOREACH_END()
789 }