Debian Codegraph @

src/cdogs/pic_manager.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2020, 2024 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "pic_manager.h"
29
30 #include <tinydir/tinydir.h>
31
32 #include "files.h"
33 #include "log.h"
34
35 #define GRAPHICS_DIR "graphics"
36 #define GRAPHICS_HD_DIR "graphics_hd"
37
38 PicManager gPicManager;
39
40 void PicManagerInit(PicManager *pm)
41 {
42 memset(pm, 0, sizeof *pm);
43 pm->pics = hashmap_new();
44 pm->sprites = hashmap_new();
45 pm->customPics = hashmap_new();
46 pm->customSprites = hashmap_new();
47 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
48 {
49 CArrayInit(&pm->headPartNames[hp], sizeof(char *));
50 }
51 CArrayInit(&pm->wallStyleNames, sizeof(char *));
52 CArrayInit(&pm->tileStyleNames, sizeof(char *));
53 CArrayInit(&pm->exitStyleNames, sizeof(char *));
54 CArrayInit(&pm->doorStyleNames, sizeof(char *));
55 CArrayInit(&pm->keyStyleNames, sizeof(char *));
56 }
57
58 static NamedPic *AddNamedPic(map_t pics, const char *name, const Pic *p);
59 static NamedSprites *AddNamedSprites(map_t sprites, const char *name);
60 static void AfterAdd(PicManager *pm);
61 static void PicManagerAdd(
62 map_t pics, map_t sprites, const char *name, SDL_Surface *imageIn,
63 const bool isHD)
64 {
65 char buf[CDOGS_FILENAME_MAX];
66 const char *dot = strrchr(name, '.');
67 if (dot)
68 {
69 strncpy(buf, name, dot - name);
70 buf[dot - name] = '\0';
71 }
72 else
73 {
74 strcpy(buf, name);
75 }
76 // TODO: check if name already exists
77 // Special case: if the file name is in the form foobar_WxH.ext,
78 // this is a spritesheet where each sprite is W wide by H high
79 // Load multiple images from this single sheet
80 struct vec2i size = svec2i(imageIn->w, imageIn->h);
81 bool isSpritesheet = false;
82 char *underscore = strrchr(buf, '_');
83 const char *x = strrchr(buf, 'x');
84 if (underscore != NULL && x != NULL && underscore + 1 < x &&
85 x + 1 < buf + strlen(buf))
86 {
87 if (sscanf(underscore, "_%dx%d", &size.x, &size.y) != 2)
88 {
89 size = svec2i(imageIn->w, imageIn->h);
90 }
91 else
92 {
93 *underscore = '\0';
94 isSpritesheet = true;
95 }
96 }
97 NamedSprites *nsp = NULL;
98 NamedPic *np = NULL;
99 if (isSpritesheet)
100 {
101 nsp = AddNamedSprites(sprites, buf);
102 }
103 else
104 {
105 np = AddNamedPic(pics, buf, NULL);
106 }
107 // Use 32-bit image
108 SDL_Surface *image =
109 SDL_ConvertSurfaceFormat(imageIn, SDL_PIXELFORMAT_RGBA8888, 0);
110 SDL_LockSurface(image);
111 struct vec2i offset;
112 for (offset.y = 0; offset.y < image->h; offset.y += size.y)
113 {
114 for (offset.x = 0; offset.x < image->w; offset.x += size.x)
115 {
116 Pic *pic;
117 if (isSpritesheet)
118 {
119 Pic p;
120 memset(&p, 0, sizeof p);
121 CArrayPushBack(&nsp->pics, &p);
122 pic = CArrayGet(&nsp->pics, nsp->pics.size - 1);
123 }
124 else
125 {
126 pic = &np->pic;
127 }
128 PicLoad(pic, size, offset, image, isHD);
129
130 if (strncmp("chars/", buf, strlen("chars/")) == 0)
131 {
132 // All head parts use hair color, so determine
133 // which head part we are looking at
134 const char *subfolder = buf + strlen("chars/");
135 CharColorType headPartColor = CHAR_COLOR_HAIR;
136 if (strncmp("facehairs/", subfolder, strlen("facehairs/")) ==
137 0)
138 {
139 headPartColor = CHAR_COLOR_FACEHAIR;
140 }
141 else if (strncmp("hats/", subfolder, strlen("hats/")) == 0)
142 {
143 headPartColor = CHAR_COLOR_HAT;
144 }
145 else if (
146 strncmp("glasses/", subfolder, strlen("glasses/")) == 0)
147 {
148 headPartColor = CHAR_COLOR_GLASSES;
149 }
150 // Convert char pics to multichannel version
151 for (int i = 0; i < pic->size.x * pic->size.y; i++)
152 {
153 color_t c = PIXEL2COLOR(pic->Data[i]);
154 // Don't bother if the alpha has already been modified; it
155 // means we have already processed this pixel
156 if (c.a != 255)
157 {
158 continue;
159 }
160 // Convert character color keyed color to
161 // greyscale + special alpha
162 const CharColorType colorType =
163 CharColorTypeFromColor(c, headPartColor);
164 color_t converted = c;
165 if (colorType != CHAR_COLOR_COUNT)
166 {
167 const uint8_t value = MAX(MAX(c.r, c.g), c.b);
168 converted.r = converted.g = converted.b = value;
169 converted.a = CharColorTypeAlpha(colorType);
170 }
171 pic->Data[i] = COLOR2PIXEL(converted);
172 }
173 }
174 }
175 }
176 SDL_UnlockSurface(image);
177 SDL_FreeSurface(image);
178
179 AfterAdd(&gPicManager);
180 }
181
182 void PicManagerLoadDir(
183 PicManager *pm, const char *path, const char *prefix, map_t pics,
184 map_t sprites, const bool isHD)
185 {
186 tinydir_dir dir;
187 if (tinydir_open(&dir, path) == -1)
188 {
189 if (errno != ENOENT)
190 {
191 LOG(LM_MAIN, LL_ERROR, "Error opening image dir '%s': %s", path,
192 strerror(errno));
193 }
194 goto bail;
195 }
196
197 for (; dir.has_next; tinydir_next(&dir))
198 {
199 tinydir_file file;
200 if (tinydir_readfile(&dir, &file) == -1)
201 {
202 LOG(LM_MAIN, LL_ERROR, "Cannot read file '%s': %s", file.path,
203 strerror(errno));
204 goto bail;
205 }
206 if (file.is_reg && Stricmp(file.extension, "png") == 0)
207 {
208 SDL_Surface *data = LoadImgToSurface(file.path);
209 if (!data)
210 {
211 LOG(LM_MAIN, LL_ERROR, "Cannot load image");
212 }
213 else
214 {
215 char buf[CDOGS_PATH_MAX];
216 if (prefix)
217 {
218 char buf1[CDOGS_PATH_MAX];
219 sprintf(buf1, "%s/%s", prefix, file.name);
220 PathGetWithoutExtension(buf, buf1);
221 }
222 else
223 {
224 PathGetBasenameWithoutExtension(buf, file.name);
225 }
226 PicManagerAdd(pics, sprites, buf, data, isHD);
227 }
228 }
229 else if (file.is_dir && file.name[0] != '.')
230 {
231 if (prefix)
232 {
233 char buf[CDOGS_PATH_MAX];
234 sprintf(buf, "%s/%s", prefix, file.name);
235 PicManagerLoadDir(pm, file.path, buf, pics, sprites, isHD);
236 }
237 else
238 {
239 PicManagerLoadDir(
240 pm, file.path, file.name, pics, sprites, isHD);
241 }
242 }
243 }
244
245 bail:
246 tinydir_close(&dir);
247 }
248 void PicManagerLoad(PicManager *pm)
249 {
250 char buf[CDOGS_PATH_MAX];
251 GetDataFilePath(buf, GRAPHICS_DIR);
252 PicManagerLoadDir(pm, buf, NULL, pm->pics, pm->sprites, false);
253 GetDataFilePath(buf, GRAPHICS_HD_DIR);
254 PicManagerLoadDir(pm, buf, NULL, pm->pics, pm->sprites, true);
255 }
256
257 static void FindStylePics(
258 PicManager *pm, CArray *styleNames, PFany hashmapFunc);
259 static void FindStyleSprites(
260 PicManager *pm, CArray *styleNames, PFany hashmapFunc);
261 static int MaybeAddHairSpriteName(any_t data, any_t item);
262 static int MaybeAddFacehairSpriteName(any_t data, any_t item);
263 static int MaybeAddHatSpriteName(any_t data, any_t item);
264 static int MaybeAddGlassesSpriteName(any_t data, any_t item);
265 static int MaybeAddWallPicName(any_t data, any_t item);
266 static int MaybeAddTilePicName(any_t data, any_t item);
267 static int MaybeAddExitPicName(any_t data, any_t item);
268 static int MaybeAddKeyPicName(any_t data, any_t item);
269 static int MaybeAddDoorPicName(any_t data, any_t item);
270 static void AfterAdd(PicManager *pm)
271 {
272 FindStyleSprites(
273 pm, &pm->headPartNames[HEAD_PART_HAIR], MaybeAddHairSpriteName);
274 FindStyleSprites(
275 pm, &pm->headPartNames[HEAD_PART_FACEHAIR],
276 MaybeAddFacehairSpriteName);
277 FindStyleSprites(
278 pm, &pm->headPartNames[HEAD_PART_HAT], MaybeAddHatSpriteName);
279 FindStyleSprites(
280 pm, &pm->headPartNames[HEAD_PART_GLASSES], MaybeAddGlassesSpriteName);
281 FindStylePics(pm, &pm->wallStyleNames, MaybeAddWallPicName);
282 FindStylePics(pm, &pm->tileStyleNames, MaybeAddTilePicName);
283 FindStylePics(pm, &pm->exitStyleNames, MaybeAddExitPicName);
284 FindStylePics(pm, &pm->doorStyleNames, MaybeAddDoorPicName);
285 FindStylePics(pm, &pm->keyStyleNames, MaybeAddKeyPicName);
286 }
287 static int CompareStyleNames(const void *v1, const void *v2);
288 static void StylesClear(CArray *styles)
289 {
290 CA_FOREACH(char *, styleName, *styles)
291 CFREE(*styleName);
292 CA_FOREACH_END()
293 CArrayClear(styles);
294 }
295 static void FindStylePics(
296 PicManager *pm, CArray *styleNames, PFany hashmapFunc)
297 {
298 // Scan all pics for style pics
299 StylesClear(styleNames);
300 hashmap_iterate(pm->customPics, hashmapFunc, pm);
301 hashmap_iterate(pm->pics, hashmapFunc, pm);
302 // Sort the style names alphabetically
303 // This prevents the list from reordering unpredictably, when the editor
304 // is used and masked pics get added
305 if (styleNames->data != NULL)
306 {
307 qsort(
308 styleNames->data, styleNames->size, styleNames->elemSize,
309 CompareStyleNames);
310 }
311 }
312 static int CompareStyleNames(const void *v1, const void *v2)
313 {
314 const char *const *s1 = v1;
315 const char *const *s2 = v2;
316 return strcmp(*s1, *s2);
317 }
318 static void FindStyleSprites(
319 PicManager *pm, CArray *styleNames, PFany hashmapFunc)
320 {
321 // Scan all pics for style sprites
322 StylesClear(styleNames);
323 hashmap_iterate(pm->customSprites, hashmapFunc, pm);
324 hashmap_iterate(pm->sprites, hashmapFunc, pm);
325 // Sort the style names alphabetically
326 // This prevents the list from reordering unpredictably, when the editor
327 // is used and masked pics get added
328 if (styleNames->data != NULL)
329 {
330 qsort(
331 styleNames->data, styleNames->size, styleNames->elemSize,
332 CompareStyleNames);
333 }
334 }
335 static void MaybeAddStyleName(
336 const char *picName, const char *prefix, CArray *styleNames)
337 {
338 // Look for style names within a full name of the form:
339 // prefix/style/suffix
340 // NOTE: prefix should include trailing slash
341 if (strncmp(picName, prefix, strlen(prefix)) != 0)
342 {
343 return;
344 }
345 const char *nextSlash = strchr(picName + strlen(prefix), '/');
346 if (nextSlash == NULL)
347 {
348 nextSlash = picName + strlen(picName);
349 }
350 char buf[CDOGS_PATH_MAX];
351 const size_t len = nextSlash - picName - strlen(prefix);
352 strncpy(buf, picName + strlen(prefix), len);
353 buf[len] = '\0';
354 // Check if we already have the style name
355 // This can happen if a custom pic uses the same name as a built in one
356 CA_FOREACH(char *, styleName, *styleNames)
357 if (strcmp(*styleName, buf) == 0)
358 {
359 return;
360 }
361 CA_FOREACH_END()
362
363 char *s;
364 CSTRDUP(s, buf);
365 CArrayPushBack(styleNames, &s);
366 }
367 static int MaybeAddHeadPartSpriteName(
368 any_t data, any_t item, const HeadPart hp, const char *path)
369 {
370 PicManager *pm = data;
371 MaybeAddStyleName(
372 ((const NamedSprites *)item)->name, path, &pm->headPartNames[hp]);
373 return MAP_OK;
374 }
375 static int MaybeAddHairSpriteName(any_t data, any_t item)
376 {
377 return MaybeAddHeadPartSpriteName(
378 data, item, HEAD_PART_HAIR, "chars/hairs/");
379 }
380 static int MaybeAddFacehairSpriteName(any_t data, any_t item)
381 {
382 return MaybeAddHeadPartSpriteName(
383 data, item, HEAD_PART_FACEHAIR, "chars/facehairs/");
384 }
385 static int MaybeAddHatSpriteName(any_t data, any_t item)
386 {
387 return MaybeAddHeadPartSpriteName(
388 data, item, HEAD_PART_HAT, "chars/hats/");
389 }
390 static int MaybeAddGlassesSpriteName(any_t data, any_t item)
391 {
392 return MaybeAddHeadPartSpriteName(
393 data, item, HEAD_PART_GLASSES, "chars/glasses/");
394 }
395 static int MaybeAddExitPicName(any_t data, any_t item)
396 {
397 // Exit pics should be like:
398 // exits/style/shadow
399 // where style is the style name to be stored, and
400 // shadow is normal/shadow
401 PicManager *pm = data;
402 MaybeAddStyleName(
403 ((const NamedPic *)item)->name, "exits/", &pm->exitStyleNames);
404 return MAP_OK;
405 }
406 static int MaybeAddDoorPicName(any_t data, any_t item)
407 {
408 // Door pics should be like:
409 // door/style/type
410 // where style is the style name to be stored
411 PicManager *pm = data;
412 MaybeAddStyleName(
413 ((const NamedPic *)item)->name, "door/", &pm->doorStyleNames);
414 return MAP_OK;
415 }
416 static int MaybeAddKeyPicName(any_t data, any_t item)
417 {
418 // Key pics should be like:
419 // keys/style/colour
420 // where style is the style name to be stored, and
421 // colour is yellow/green/blue/red
422 // TODO: more colours
423 PicManager *pm = data;
424 MaybeAddStyleName(
425 ((const NamedPic *)item)->name, "keys/", &pm->keyStyleNames);
426 return MAP_OK;
427 }
428 static int MaybeAddWallPicName(any_t data, any_t item)
429 {
430 // Wall pics should be like:
431 // wall/style/type
432 // where style is the style name to be stored
433 PicManager *pm = data;
434 MaybeAddStyleName(
435 ((const NamedPic *)item)->name, "wall/", &pm->wallStyleNames);
436 return MAP_OK;
437 }
438 static int MaybeAddTilePicName(any_t data, any_t item)
439 {
440 // Tile pics should be like:
441 // tile/style/type
442 // where style is the style name to be stored, and
443 // type is normal/shadow/alt1/alt2
444 PicManager *pm = data;
445 MaybeAddStyleName(
446 ((const NamedPic *)item)->name, "tile/", &pm->tileStyleNames);
447 return MAP_OK;
448 }
449
450 // Need to free the pics and the memory since hashmap stores on heap
451 static void NamedPicDestroy(any_t data);
452 static void NamedSpritesDestroy(any_t data);
453 void PicManagerClearCustom(PicManager *pm)
454 {
455 hashmap_clear(pm->customPics, NamedPicDestroy);
456 hashmap_clear(pm->customSprites, NamedSpritesDestroy);
457 AfterAdd(pm);
458 }
459 static void PicManagerUnload(PicManager *pm)
460 {
461 hashmap_clear(pm->pics, NamedPicDestroy);
462 hashmap_clear(pm->sprites, NamedSpritesDestroy);
463 hashmap_clear(pm->customPics, NamedPicDestroy);
464 hashmap_clear(pm->customSprites, NamedSpritesDestroy);
465 AfterAdd(pm);
466 }
467 static void StyleNamesDestroy(CArray *a)
468 {
469 CA_FOREACH(char, n, *a)
470 CFREE(n);
471 CA_FOREACH_END()
472 CArrayTerminate(a);
473 }
474 void PicManagerTerminate(PicManager *pm)
475 {
476 PicManagerUnload(pm);
477 for (HeadPart hp = HEAD_PART_HAIR; hp < HEAD_PART_COUNT; hp++)
478 {
479 StyleNamesDestroy(&pm->headPartNames[hp]);
480 }
481 StyleNamesDestroy(&pm->wallStyleNames);
482 StyleNamesDestroy(&pm->tileStyleNames);
483 StyleNamesDestroy(&pm->exitStyleNames);
484 StyleNamesDestroy(&pm->doorStyleNames);
485 StyleNamesDestroy(&pm->keyStyleNames);
486 }
487 static void NamedPicDestroy(any_t data)
488 {
489 NamedPic *n = data;
490 NamedPicFree(n);
491 CFREE(n);
492 }
493 static void NamedSpritesDestroy(any_t data)
494 {
495 NamedSprites *n = data;
496 NamedSpritesFree(n);
497 CFREE(n);
498 }
499 static int ReloadTexture(any_t data, any_t item);
500 static int ReloadSpriteTexture(any_t data, any_t item);
501 void PicManagerReloadTextures(PicManager *pm)
502 {
503 hashmap_iterate(pm->pics, ReloadTexture, NULL);
504 hashmap_iterate(pm->customPics, ReloadTexture, NULL);
505 hashmap_iterate(pm->sprites, ReloadSpriteTexture, NULL);
506 hashmap_iterate(pm->customSprites, ReloadSpriteTexture, NULL);
507 }
508 static int ReloadTexture(any_t data, any_t item)
509 {
510 UNUSED(data);
511 NamedPic *n = item;
512 if (!PicTryMakeTex(&n->pic))
513 {
514 LOG(LM_MAIN, LL_ERROR, "failed to reload pic texture");
515 n->pic.Tex = NULL;
516 }
517 return MAP_OK;
518 }
519 static int ReloadSpriteTexture(any_t data, any_t item)
520 {
521 UNUSED(data);
522 NamedSprites *n = item;
523 CA_FOREACH(Pic, op, n->pics)
524 if (!PicTryMakeTex(op))
525 {
526 LOG(LM_MAIN, LL_ERROR, "failed to reload pic texture");
527 op->Tex = NULL;
528 }
529 CA_FOREACH_END()
530 return MAP_OK;
531 }
532
533 NamedPic *PicManagerGetNamedPic(const PicManager *pm, const char *name)
534 {
535 NamedPic *n;
536 int error = hashmap_get(pm->customPics, name, (any_t *)&n);
537 if (error == MAP_OK)
538 {
539 return n;
540 }
541 error = hashmap_get(pm->pics, name, (any_t *)&n);
542 if (error == MAP_OK)
543 {
544 return n;
545 }
546 return NULL;
547 }
548 Pic *PicManagerGetPic(const PicManager *pm, const char *name)
549 {
550 NamedPic *n = PicManagerGetNamedPic(pm, name);
551 if (n != NULL)
552 return &n->pic;
553 return NULL;
554 }
555 const NamedSprites *PicManagerGetSprites(
556 const PicManager *pm, const char *name)
557 {
558 NamedSprites *n;
559 int error = hashmap_get(pm->customSprites, name, (any_t *)&n);
560 if (error == MAP_OK)
561 {
562 return n;
563 }
564 error = hashmap_get(pm->sprites, name, (any_t *)&n);
565 if (error == MAP_OK)
566 {
567 return n;
568 }
569 return NULL;
570 }
571
572 static void GetMaskedName(
573 char *buf, const char *name, const color_t mask, const color_t maskAlt);
574
575 // Get a pic that is colour-masked.
576 // The name of the pic will be <name>/<mask>/<maskAlt>
577 // Used for dynamic map tile pic colours
578 static NamedPic *PicManagerGetMaskedPic(
579 const PicManager *pm, const char *name, const color_t mask,
580 const color_t maskAlt)
581 {
582 char maskedName[256];
583 GetMaskedName(maskedName, name, mask, maskAlt);
584 return PicManagerGetNamedPic(pm, maskedName);
585 }
586 NamedPic *PicManagerGetMaskedStylePic(
587 const PicManager *pm, const char *name, const char *style,
588 const char *type, const color_t mask, const color_t maskAlt)
589 {
590 char buf[256];
591 sprintf(buf, "%s/%s/%s", name, style, type);
592 return PicManagerGetMaskedPic(pm, buf, mask, maskAlt);
593 }
594
595 static void PicManagerGenerateMaskedPic(
596 PicManager *pm, const char *name, const color_t mask,
597 const color_t maskAlt, const bool noAltMask)
598 {
599 char maskedName[256];
600 GetMaskedName(maskedName, name, mask, maskAlt);
601 // Check if the masked pic already exists
602 if (PicManagerGetPic(pm, maskedName) != NULL)
603 return;
604
605 // Check if the original pic is available; if not then it's impossible to
606 // create the masked version
607 Pic *original = PicManagerGetPic(pm, name);
608 CASSERT(original != NULL, "Cannot find original pic for masking");
609
610 // Create the new pic by masking the original pic
611 Pic p = PicCopy(original);
612 for (int i = 0; i < p.size.x * p.size.y; i++)
613 {
614 color_t c = PIXEL2COLOR(original->Data[i]);
615 // Apply mask based on which channel each pixel is
616 if (c.g <= 2 && c.b <= 2 && !noAltMask)
617 {
618 // Restore to white before masking
619 c.g = c.r;
620 c.b = c.r;
621 c = ColorMult(c, maskAlt);
622 }
623 else if (c.r == c.g && c.g == c.b)
624 {
625 c = ColorMult(c, mask);
626 }
627 p.Data[i] = COLOR2PIXEL(c);
628 // TODO: more channels
629 }
630 if (!PicTryMakeTex(&p))
631 {
632 p.Tex = NULL;
633 }
634 AddNamedPic(pm->customPics, maskedName, &p);
635
636 AfterAdd(pm);
637 }
638 void PicManagerGenerateMaskedStylePic(
639 PicManager *pm, const char *name, const char *style, const char *type,
640 const color_t mask, const color_t maskAlt, const bool noAltMask)
641 {
642 char buf[256];
643 sprintf(buf, "%s/%s/%s", name, style, type);
644 PicManagerGenerateMaskedPic(pm, buf, mask, maskAlt, noAltMask);
645 }
646
647 const NamedSprites *PicManagerGetCharSprites(
648 PicManager *pm, const char *name, const CharColors *colors)
649 {
650 char buf[CDOGS_PATH_MAX];
651 CharColorsGetMaskedName(buf, name, colors);
652 // Get or generate masked sprites
653 const NamedSprites *ns = PicManagerGetSprites(&gPicManager, buf);
654 if (ns != NULL)
655 {
656 return ns;
657 }
658 const NamedSprites *ons = PicManagerGetSprites(pm, name);
659 if (ons == NULL)
660 {
661 return NULL;
662 }
663 NamedSprites *nsp = AddNamedSprites(pm->customSprites, buf);
664 CA_FOREACH(Pic, op, ons->pics)
665 Pic p = PicCopy(op);
666 p.Tex = NULL;
667 for (int i = 0; i < p.size.x * p.size.y; i++)
668 {
669 if (op->Data[i] == 0)
670 {
671 continue;
672 }
673 const color_t c = PIXEL2COLOR(op->Data[i]);
674 p.Data[i] =
675 COLOR2PIXEL(ColorMult(c, CharColorsGetChannelMask(colors, c.a)));
676 }
677 if (!PicTryMakeTex(&p))
678 {
679 p.Tex = NULL;
680 }
681 CArrayPushBack(&nsp->pics, &p);
682 CA_FOREACH_END()
683 AfterAdd(pm);
684 return nsp;
685 }
686
687 static void GetMaskedName(
688 char *buf, const char *name, const color_t mask, const color_t maskAlt)
689 {
690 char maskName[COLOR_STR_BUF];
691 ColorStr(maskName, mask);
692 char maskAltName[COLOR_STR_BUF];
693 ColorStr(maskAltName, maskAlt);
694 sprintf(buf, "%s/%s/%s", name, maskName, maskAltName);
695 }
696
697 static NamedPic *AddNamedPic(map_t pics, const char *name, const Pic *p)
698 {
699 NamedPic *n;
700 CMALLOC(n, sizeof *n);
701 if (p != NULL)
702 n->pic = *p;
703 CSTRDUP(n->name, name);
704 const int error = hashmap_put(pics, name, n);
705 if (error != MAP_OK)
706 {
707 LOG(LM_MAIN, LL_ERROR, "failed to add named pic %s: %d", name, error);
708 CFREE(n);
709 return NULL;
710 }
711 return n;
712 }
713 static NamedSprites *AddNamedSprites(map_t sprites, const char *name)
714 {
715 NamedSprites *ns;
716 CMALLOC(ns, sizeof *ns);
717 NamedSpritesInit(ns, name);
718 const int error = hashmap_put(sprites, name, ns);
719 if (error != MAP_OK)
720 {
721 LOG(LM_MAIN, LL_ERROR, "failed to add named sprites %s: %d", name,
722 error);
723 CFREE(ns);
724 return NULL;
725 }
726 return ns;
727 }
728
729 int PicManagerGetWallStyleIndex(PicManager *pm, const char *style)
730 {
731 CA_FOREACH(const char *, styleName, pm->wallStyleNames)
732 if (strcmp(style, *styleName) == 0)
733 {
734 return _ca_index;
735 }
736 CA_FOREACH_END()
737 return 0;
738 }
739 int PicManagerGetTileStyleIndex(PicManager *pm, const char *style)
740 {
741 CA_FOREACH(const char *, styleName, pm->tileStyleNames)
742 if (strcmp(style, *styleName) == 0)
743 {
744 return _ca_index;
745 }
746 CA_FOREACH_END()
747 return 0;
748 }
749 int PicManagerGetExitStyleIndex(PicManager *pm, const char *style)
750 {
751 CA_FOREACH(const char *, styleName, pm->exitStyleNames)
752 if (strcmp(style, *styleName) == 0)
753 {
754 return _ca_index;
755 }
756 CA_FOREACH_END()
757 return 0;
758 }
759 int PicManagerGetDoorStyleIndex(PicManager *pm, const char *style)
760 {
761 CA_FOREACH(const char *, doorStyleName, pm->doorStyleNames)
762 if (strcmp(style, *doorStyleName) == 0)
763 {
764 return _ca_index;
765 }
766 CA_FOREACH_END()
767 return 0;
768 }
769 int PicManagerGetKeyStyleIndex(PicManager *pm, const char *style)
770 {
771 CA_FOREACH(const char *, keyStyleName, pm->keyStyleNames)
772 if (strcmp(style, *keyStyleName) == 0)
773 {
774 return _ca_index;
775 }
776 CA_FOREACH_END()
777 return 0;
778 }