Crossfire Server  1.75.0
image.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
19 #include "global.h"
20 
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "image.h"
28 
29 #include "assets.h"
30 #include "AssetsManager.h"
31 
37 
38 
46 static const char *const colorname[] = {
47  "black", /* 0 */
48  "white", /* 1 */
49  "blue", /* 2 */
50  "red", /* 3 */
51  "orange", /* 4 */
52  "light_blue", /* 5 */
53  "dark_orange", /* 6 */
54  "green", /* 7 */
55  "light_green", /* 8 */
56  "grey", /* 9 */
57  "brown", /* 10 */
58  "yellow", /* 11 */
59  "khaki" /* 12 */
60 };
61 
75 uint8_t find_color(const char *name) {
76  uint8_t i;
77 
78  for (i = 0; i < sizeof(colorname)/sizeof(*colorname); i++)
79  if (!strcmp(name, colorname[i]))
80  return i;
81 
82  LOG(llevError, "Unknown color: %s\n", name);
83  return 0;
84 }
85 
86 const char *get_colorname(uint8_t index) {
87  if (index < sizeof(colorname) / sizeof(*colorname)) {
88  return colorname[index];
89  }
90  return "";
91 }
92 
102 int find_smooth(const Face *face, const Face **smoothed) {
103  (*smoothed) = NULL;
104 
105  if (face && face->smoothface) {
106  (*smoothed) = face->smoothface;
107  return 1;
108  }
109 
110  return 0;
111 }
112 
117 int is_valid_faceset(int fsn) {
118  return find_faceset(fsn) != NULL;
119 }
120 
133 int get_face_fallback(int faceset, uint16_t imageno) {
134  /* faceset 0 is supposed to have every image, so just return. Doing
135  * so also prevents infinite loops in the case if it not having
136  * the face, but in that case, we are likely to crash when we try
137  * to access the data, but that is probably preferable to an infinite
138  * loop.
139  */
140 
141  face_sets *fs = find_faceset(faceset);
142  if (!fs || !fs->prefix) {
143  LOG(llevError, "get_face_fallback called with unused set (%d)?\n", faceset);
144  return 0; /* use default set */
145  }
146  if (imageno < fs->allocated && fs->faces[imageno].data)
147  return faceset;
148 
149  if (!fs->fallback) {
150  return 0;
151  }
152 
153  return get_face_fallback(fs->fallback->id, imageno);
154 }
155 
159 void dump_faces(void) {
160  fprintf(stderr, "id name smooth\n");
161  getManager()->faces()->each([] (const Face *face) {
162  fprintf(stderr, "%5d %50s %50s\n", face->number, face->name, face->smoothface ? face->smoothface->name : "(none)");
163  });
164 }
Error, serious thing.
Definition: logger.h:11
face_info * faces
images in this faceset
Definition: image.h:26
Face * smoothface
Smoothed face for this, NULL for none.
Definition: face.h:18
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
New face structure - this enforces the notion that data is face by face only - you can not change the...
Definition: face.h:14
int id
Definition: image.h:18
int get_face_fallback(int faceset, uint16_t imageno)
This returns the set we will actually use when sending a face.
Definition: image.cpp:133
face_sets * find_faceset(int id)
Definition: assets.cpp:332
void dump_faces(void)
Dump all faces to stderr, for debugging purposes.
Definition: image.cpp:159
Global type definitions and header inclusions.
const char * get_colorname(uint8_t index)
Definition: image.cpp:86
const Face * blank_face
Following can just as easily be pointers, but it is easier to keep them like this.
Definition: image.cpp:36
AssetsManager * getManager()
Definition: assets.cpp:309
Faces * faces()
Get faces.
Definition: AssetsManager.h:39
Image-related structures.
uint8_t * data
Image data.
Definition: image.h:11
char * prefix
Faceset short name, used in pictures names (base, clsc).
Definition: image.h:19
int find_smooth(const Face *face, const Face **smoothed)
Find the smooth face for a given face.
Definition: image.cpp:102
uint16_t number
This is the image unique identifier.
Definition: face.h:15
int is_valid_faceset(int fsn)
Checks specified faceset is valid.
Definition: image.cpp:117
static const char *const colorname[]
The only thing this table is used for now is to translate the colorname in the magicmap field of the ...
Definition: image.cpp:46
const Face * empty_face
Definition: image.cpp:36
struct face_sets * fallback
Faceset to use when an image is not found in this faceset.
Definition: image.h:21
uint8_t find_color(const char *name)
Finds a color by name.
Definition: image.cpp:75
const Face * smooth_face
Definition: image.cpp:36
C function wrappers to interact with assets.
Information about one face set.
Definition: image.h:17
sstring name
Face name, as used by archetypes and such.
Definition: face.h:19
void each(std::function< void(T *)> op)
Apply a function to each asset.