| 1 |
// |
| 2 |
// Copyright(C) 2021 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Sprite |
| 16 |
// |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <string.h> |
| 20 |
#include <ctype.h> |
| 21 |
|
| 22 |
#include "info.h" |
| 23 |
|
| 24 |
#include "sprite.h" |
| 25 |
|
| 26 |
const char** sprnames; |
| 27 |
int num_sprites; |
| 28 |
static int deh_spritenames_size; |
| 29 |
static char** deh_spritenames; |
| 30 |
static byte* sprnames_state; |
| 31 |
|
| 32 |
static void dsda_PrepAllocation(void) { |
| 33 |
static int first_allocation = true; |
| 34 |
|
| 35 |
if (first_allocation) { |
| 36 |
const char** source = sprnames; |
| 37 |
|
| 38 |
first_allocation = false; |
| 39 |
sprnames = malloc(num_sprites * sizeof(*sprnames)); |
| 40 |
memcpy(sprnames, source, num_sprites * sizeof(*sprnames)); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
static void dsda_EnsureCapacity(int limit) { |
| 45 |
while (limit >= num_sprites) { |
| 46 |
int old_num_sprites = num_sprites; |
| 47 |
|
| 48 |
dsda_PrepAllocation(); |
| 49 |
|
| 50 |
num_sprites *= 2; |
| 51 |
|
| 52 |
sprnames = realloc(sprnames, num_sprites * sizeof(*sprnames)); |
| 53 |
memset(sprnames + old_num_sprites, 0, (num_sprites - old_num_sprites) * sizeof(*sprnames)); |
| 54 |
|
| 55 |
sprnames_state = realloc(sprnames_state, num_sprites * sizeof(*sprnames_state)); |
| 56 |
memset(sprnames_state + old_num_sprites, 0, |
| 57 |
(num_sprites - old_num_sprites) * sizeof(*sprnames_state)); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
int dsda_GetDehSpriteIndex(const char* key) { |
| 62 |
int i; |
| 63 |
|
| 64 |
for (i = 0; i < num_sprites; ++i) |
| 65 |
if (sprnames[i] && !strnicmp(sprnames[i], key, 4) && !sprnames_state[i]) { |
| 66 |
sprnames_state[i] = true; // sprite has been edited |
| 67 |
return i; |
| 68 |
} |
| 69 |
|
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
int dsda_GetOriginalSpriteIndex(const char* key) { |
| 74 |
int i; |
| 75 |
const char* c; |
| 76 |
|
| 77 |
for (i = 0; deh_spritenames[i]; ++i) |
| 78 |
if (!strncasecmp(deh_spritenames[i], key, 4)) |
| 79 |
return i; |
| 80 |
|
| 81 |
// is it a number? |
| 82 |
for (c = key; *c; c++) |
| 83 |
if (!isdigit(*c)) |
| 84 |
return -1; |
| 85 |
|
| 86 |
i = atoi(key); |
| 87 |
dsda_EnsureCapacity(i); |
| 88 |
|
| 89 |
return i; |
| 90 |
} |
| 91 |
|
| 92 |
void dsda_InitializeSprites(const char** source, int count) { |
| 93 |
int i; |
| 94 |
extern int raven; |
| 95 |
|
| 96 |
num_sprites = count; |
| 97 |
deh_spritenames_size = num_sprites + 1; |
| 98 |
|
| 99 |
sprnames = source; |
| 100 |
|
| 101 |
if (raven) return; |
| 102 |
|
| 103 |
deh_spritenames = malloc(deh_spritenames_size * sizeof(*deh_spritenames)); |
| 104 |
for (i = 0; i < num_sprites; i++) |
| 105 |
deh_spritenames[i] = strdup(sprnames[i]); |
| 106 |
deh_spritenames[num_sprites] = NULL; |
| 107 |
|
| 108 |
sprnames_state = calloc(num_sprites, sizeof(*sprnames_state)); |
| 109 |
} |
| 110 |
|
| 111 |
void dsda_FreeDehSprites(void) { |
| 112 |
int i; |
| 113 |
|
| 114 |
if (deh_spritenames) |
| 115 |
for (i = 0; i < deh_spritenames_size; i++) |
| 116 |
if (deh_spritenames[i]) |
| 117 |
free(deh_spritenames[i]); |
| 118 |
|
| 119 |
free(deh_spritenames); |
| 120 |
free(sprnames_state); |
| 121 |
} |