| 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 |
#include "char_sprites.h" |
| 29 |
|
| 30 |
#include <tinydir/tinydir.h> |
| 31 |
|
| 32 |
#include "c_array.h" |
| 33 |
#include "log.h" |
| 34 |
#include "sys_config.h" |
| 35 |
#include "yajl_utils.h" |
| 36 |
|
| 37 |
#define VERSION 2 |
| 38 |
|
| 39 |
CharSpriteClasses gCharSpriteClasses; |
| 40 |
|
| 41 |
const CharSprites *StrCharSpriteClass(const char *s) |
| 42 |
{ |
| 43 |
CharSprites *c; |
| 44 |
int error = hashmap_get(gCharSpriteClasses.customClasses, s, (any_t *)&c); |
| 45 |
if (error == MAP_OK) |
| 46 |
return c; |
| 47 |
error = hashmap_get(gCharSpriteClasses.classes, s, (any_t *)&c); |
| 48 |
if (error == MAP_OK) |
| 49 |
return c; |
| 50 |
LOG(LM_MAIN, LL_ERROR, "char sprite class %s not found; using default", s); |
| 51 |
return StrCharSpriteClass("base"); |
| 52 |
} |
| 53 |
|
| 54 |
void CharSpriteClassesInit(CharSpriteClasses *c) |
| 55 |
{ |
| 56 |
memset(c, 0, sizeof *c); |
| 57 |
c->classes = hashmap_new(); |
| 58 |
c->customClasses = hashmap_new(); |
| 59 |
char buf[CDOGS_PATH_MAX]; |
| 60 |
GetDataFilePath(buf, ""); |
| 61 |
CharSpriteClassesLoadDir(c->classes, buf); |
| 62 |
} |
| 63 |
|
| 64 |
static CharSprites *CharSpritesLoadJSON(const char *name, const char *path); |
| 65 |
void CharSpriteClassesLoadDir(map_t classes, const char *path) |
| 66 |
{ |
| 67 |
char buf[CDOGS_PATH_MAX]; |
| 68 |
sprintf(buf, "%s/graphics/chars/bodies", path); |
| 69 |
tinydir_dir dir; |
| 70 |
if (tinydir_open(&dir, buf) == -1) |
| 71 |
{ |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
for (; dir.has_next; tinydir_next(&dir)) |
| 76 |
{ |
| 77 |
tinydir_file file; |
| 78 |
if (tinydir_readfile(&dir, &file) == -1) |
| 79 |
{ |
| 80 |
LOG(LM_MAIN, LL_ERROR, "Cannot read file '%s': %s", file.path, |
| 81 |
strerror(errno)); |
| 82 |
goto bail; |
| 83 |
} |
| 84 |
if (!file.is_dir || file.name[0] == '.') |
| 85 |
{ |
| 86 |
continue; |
| 87 |
} |
| 88 |
CharSprites *c = CharSpritesLoadJSON(file.name, file.path); |
| 89 |
if (c == NULL) |
| 90 |
{ |
| 91 |
continue; |
| 92 |
} |
| 93 |
const int error = hashmap_put(classes, file.name, c); |
| 94 |
if (error != MAP_OK) |
| 95 |
{ |
| 96 |
LOG(LM_MAIN, LL_ERROR, "failed to add char sprites %s: %d", |
| 97 |
file.name, error); |
| 98 |
continue; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
bail: |
| 103 |
tinydir_close(&dir); |
| 104 |
} |
| 105 |
static map_t LoadFrameOffsets(yajl_val node, const char *path); |
| 106 |
static void LoadDirOffsets( |
| 107 |
struct vec2 *offsets, yajl_val node, const char *path); |
| 108 |
static CharSprites *CharSpritesLoadJSON(const char *name, const char *path) |
| 109 |
{ |
| 110 |
CharSprites *c = NULL; |
| 111 |
|
| 112 |
tinydir_file dataFile; |
| 113 |
yajl_val node = NULL; |
| 114 |
char buf[CDOGS_PATH_MAX]; |
| 115 |
sprintf(buf, "%s/data.json", path); |
| 116 |
if (tinydir_file_open(&dataFile, buf) != 0) |
| 117 |
{ |
| 118 |
goto bail; |
| 119 |
} |
| 120 |
node = YAJLReadFile(buf); |
| 121 |
if (node == NULL) |
| 122 |
{ |
| 123 |
LOG(LM_MAIN, LL_ERROR, "Error parsing char sprite JSON '%s'", buf); |
| 124 |
goto bail; |
| 125 |
} |
| 126 |
|
| 127 |
CCALLOC(c, sizeof *c); |
| 128 |
CSTRDUP(c->Name, name); |
| 129 |
const yajl_array order = YAJL_GET_ARRAY(YAJLFindNode(node, "Order")); |
| 130 |
for (direction_e d = DIRECTION_UP; d < DIRECTION_COUNT; d++) |
| 131 |
{ |
| 132 |
const yajl_array orderDir = YAJL_GET_ARRAY(order->values[d]); |
| 133 |
int i = 0; |
| 134 |
for (BodyPart bp = BODY_PART_HEAD; bp < BODY_PART_COUNT; bp++) |
| 135 |
{ |
| 136 |
c->Order[d][bp] = |
| 137 |
StrBodyPart(YAJL_GET_STRING(orderDir->values[i])); |
| 138 |
i++; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if (c->Order[d][bp] == BODY_PART_HEAD) |
| 143 |
{ |
| 144 |
c->Order[d][++bp] = BODY_PART_FACEHAIR; |
| 145 |
c->Order[d][++bp] = BODY_PART_GLASSES; |
| 146 |
c->Order[d][++bp] = BODY_PART_HAIR; |
| 147 |
c->Order[d][++bp] = BODY_PART_HAT; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
for (BodyPart bp = BODY_PART_HEAD; bp <= BODY_PART_GLASSES; bp++) |
| 153 |
{ |
| 154 |
c->Offsets.Frame[bp] = LoadFrameOffsets(node, "Offsets/Frame/Head"); |
| 155 |
} |
| 156 |
c->Offsets.Frame[BODY_PART_BODY] = |
| 157 |
LoadFrameOffsets(node, "Offsets/Frame/Body"); |
| 158 |
c->Offsets.Frame[BODY_PART_LEGS] = |
| 159 |
LoadFrameOffsets(node, "Offsets/Frame/Legs"); |
| 160 |
c->Offsets.Frame[BODY_PART_GUN_R] = |
| 161 |
LoadFrameOffsets(node, "Offsets/Frame/Gun"); |
| 162 |
c->Offsets.Frame[BODY_PART_GUN_L] = |
| 163 |
LoadFrameOffsets(node, "Offsets/Frame/Gun"); |
| 164 |
LoadDirOffsets(c->Offsets.Dir[BODY_PART_HEAD], node, "Offsets/Dir/Head"); |
| 165 |
|
| 166 |
for (BodyPart bp = BODY_PART_HEAD + 1; bp <= BODY_PART_GLASSES; bp++) |
| 167 |
{ |
| 168 |
memcpy(&c->Offsets.Dir[bp], &c->Offsets.Dir[BODY_PART_HEAD], sizeof c->Offsets.Dir[bp]); |
| 169 |
} |
| 170 |
LoadDirOffsets(c->Offsets.Dir[BODY_PART_BODY], node, "Offsets/Dir/Body"); |
| 171 |
LoadDirOffsets(c->Offsets.Dir[BODY_PART_LEGS], node, "Offsets/Dir/Legs"); |
| 172 |
LoadDirOffsets(c->Offsets.Dir[BODY_PART_GUN_R], node, "Offsets/Dir/Gun"); |
| 173 |
LoadDirOffsets(c->Offsets.Dir[BODY_PART_GUN_L], node, "Offsets/Dir/Gun"); |
| 174 |
|
| 175 |
bail: |
| 176 |
yajl_tree_free(node); |
| 177 |
return c; |
| 178 |
} |
| 179 |
static map_t LoadFrameOffsets(yajl_val node, const char *path) |
| 180 |
{ |
| 181 |
map_t offsets = hashmap_new(); |
| 182 |
const yajl_object obj = YAJL_GET_OBJECT(YAJLFindNode(node, path)); |
| 183 |
for (int i = 0; i < (int)obj->len; i++) |
| 184 |
{ |
| 185 |
const char *key = obj->keys[i]; |
| 186 |
CArray *offsetVals; |
| 187 |
CMALLOC(offsetVals, sizeof *offsetVals); |
| 188 |
CArrayInit(offsetVals, sizeof(struct vec2i)); |
| 189 |
const yajl_array offsetsArray = YAJL_GET_ARRAY(obj->values[i]); |
| 190 |
for (int j = 0; j < (int)offsetsArray->len; j++) |
| 191 |
{ |
| 192 |
const struct vec2i offset = |
| 193 |
YAJL_GET_VEC2I(offsetsArray->values[j]); |
| 194 |
CArrayPushBack(offsetVals, &offset); |
| 195 |
} |
| 196 |
const int error = hashmap_put(offsets, key, offsetVals); |
| 197 |
if (error != MAP_OK) |
| 198 |
{ |
| 199 |
LOG(LM_MAIN, LL_ERROR, "Failed to add animation offsets: %d", |
| 200 |
error); |
| 201 |
CASSERT(false, "Failed to add animation offsets"); |
| 202 |
} |
| 203 |
} |
| 204 |
return offsets; |
| 205 |
} |
| 206 |
static void LoadDirOffsets( |
| 207 |
struct vec2 *offsets, yajl_val node, const char *path) |
| 208 |
{ |
| 209 |
const yajl_array offsetsArray = YAJL_GET_ARRAY(YAJLFindNode(node, path)); |
| 210 |
if (offsetsArray == NULL) |
| 211 |
{ |
| 212 |
return; |
| 213 |
} |
| 214 |
for (direction_e d = DIRECTION_UP; d < DIRECTION_COUNT; d++) |
| 215 |
{ |
| 216 |
offsets[d] = |
| 217 |
svec2_assign_vec2i(YAJL_GET_VEC2I(offsetsArray->values[d])); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
static void CharSpritesDestroy(any_t data); |
| 222 |
void CharSpriteClassesClear(map_t classes) |
| 223 |
{ |
| 224 |
hashmap_destroy(classes, CharSpritesDestroy); |
| 225 |
} |
| 226 |
static void OffsetFrameDestroy(any_t data); |
| 227 |
static void CharSpritesDestroy(any_t data) |
| 228 |
{ |
| 229 |
CharSprites *c = data; |
| 230 |
CFREE(c->Name); |
| 231 |
for (int i = 0; i < BODY_PART_COUNT + MAX_BARRELS - 1; i++) |
| 232 |
{ |
| 233 |
hashmap_destroy(c->Offsets.Frame[i], OffsetFrameDestroy); |
| 234 |
} |
| 235 |
CFREE(c); |
| 236 |
} |
| 237 |
static void OffsetFrameDestroy(any_t data) |
| 238 |
{ |
| 239 |
CArray *offsets = data; |
| 240 |
CArrayTerminate(offsets); |
| 241 |
} |
| 242 |
void CharSpriteClassesTerminate(CharSpriteClasses *c) |
| 243 |
{ |
| 244 |
CharSpriteClassesClear(c->classes); |
| 245 |
CharSpriteClassesClear(c->customClasses); |
| 246 |
} |
| 247 |
|
| 248 |
struct vec2i CharSpritesGetOffset( |
| 249 |
const map_t offsets, const char *anim, const int frame) |
| 250 |
{ |
| 251 |
CArray *animOffsets; |
| 252 |
int error = hashmap_get(offsets, anim, (any_t *)&animOffsets); |
| 253 |
if (error == MAP_MISSING) |
| 254 |
{ |
| 255 |
|
| 256 |
error = hashmap_get(offsets, "idle", (any_t *)&animOffsets); |
| 257 |
} |
| 258 |
if (error != MAP_OK) |
| 259 |
{ |
| 260 |
CASSERT(false, "animation not found"); |
| 261 |
return svec2i_zero(); |
| 262 |
} |
| 263 |
return *(struct vec2i *)CArrayGet(animOffsets, frame % animOffsets->size); |
| 264 |
} |