1 #include <string.h>
2 #include <stdlib.h>
3 #include "resources/palette.h"
4 #include "resources/ids.h"
5 #include "resources/pathmanager.h"
6 #include "utils/log.h"
7 
8 sd_altpal_file *altpals = NULL;
9 
altpals_init()10 int altpals_init() {
11     // Get filename
12     const char *filename = pm_get_resource_path(DAT_ALTPALS);
13 
14     altpals = malloc(sizeof(sd_altpal_file));
15     if(sd_altpal_create(altpals) != SD_SUCCESS) {
16         goto error_0;
17     }
18     if(sd_altpals_load(altpals, filename) != SD_SUCCESS) {
19         PERROR("Unable to load altpals file '%s'!", filename);
20         goto error_1;
21     }
22     INFO("Loaded altpals file '%s'.", filename);
23     return 0;
24 
25 error_1:
26     sd_altpal_free(altpals);
27 error_0:
28     free(altpals);
29     altpals = NULL;
30     return 1;
31 }
32 
altpals_close()33 void altpals_close() {
34     if(altpals != NULL) {
35         sd_altpal_free(altpals);
36         free(altpals);
37         altpals = NULL;
38     }
39 }
40 
palette_set_player_color(palette * palette,int player,int srccolor,int dstcolor)41 void palette_set_player_color(palette *palette, int player, int srccolor, int dstcolor) {
42     int dst = dstcolor * 16 + player * 48;
43     int src = srccolor * 16;
44     char iz[3];
45     memcpy(iz, palette->data, 3);
46     memcpy(palette->data + dst, altpals->palettes[0].data + src, 16 * 3);
47     memcpy(palette->data, iz, 3);
48 }
49 
palette_copy(palette * src)50 palette* palette_copy(palette *src) {
51     palette *new = malloc(sizeof(palette));
52     memcpy(new->data, src->data, 256*3);
53     memcpy(new->remaps, src->remaps, 19*256);
54     return new;
55 }
56