Repository navigation
Expand file tree
/
Copy pathsprites.c
More file actions
88 lines (66 loc) · 1.89 KB
/
Copy pathsprites.c
File metadata and controls
88 lines (66 loc) · 1.89 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <raylib.h>
#include "hashmap.h"
#include "hashmap.c"
#include "components.c"
#include "helpers.c"
struct hashmap *map_sprites;
char* sprite_keys[SPRITES_MAX_COUNT] = {};
int sprite_compare(const void *a, const void *b, [[maybe_unused]] void *data) {
const sprite_t *sa = a;
const sprite_t *sb = b;
return strcmp(sa->key, sb->key);
}
uint64_t sprite_hash(const void *item, uint64_t seed0, uint64_t seed1) {
const sprite_t *sprite = item;
return hashmap_sip(sprite->key, strlen(sprite->key), seed0, seed1);
}
size_t sprites_count() {
return hashmap_count(map_sprites);
}
void sprite_new(char *key, char *label, char *file_name, AppSettings *settings) {
char *_path = malloc(500);
char *_key = malloc(strlen(key) + 1);
char *_label = malloc(strlen(label) + 1);
char *_file_name = malloc(strlen(file_name) + 1);
_path[0] = '\0';
_key[0] = '\0';
_label[0] = '\0';
_file_name[0] = '\0';
strcpy(_path, settings->assets_path);
strcpy(_key, key);
strcpy(_label, label);
strcpy(_file_name, file_name);
Texture texture = LoadTexture(strcat(_path, file_name));
sprite_keys[sprites_count()] = _key;
hashmap_set(map_sprites, &(sprite_t) {
.key = _key,
.label = _label,
.file_name = _file_name,
.texture = texture,
.width = texture.width,
.height = texture.height
});
free(_path);
}
void sprites_init() {
map_sprites = hashmap_new(sizeof(sprite_t), 40, 0, 0, sprite_hash, sprite_compare, NULL, NULL);
}
sprite_t* sprite_get(char* key) {
return (sprite_t*)hashmap_get(map_sprites, &(sprite_t) { .key = key });
}
void sprites_destroy() {
size_t iter = 0;
void *item;
while (hashmap_iter(map_sprites, &iter, &item)) {
const sprite_t *sprite = item;
free(sprite->key);
free(sprite->label);
free(sprite->file_name);
UnloadTexture(sprite->texture);
}
hashmap_free(map_sprites);
}