Mercurial > molko
comparison examples/example-battle/registry.c @ 209:23a844fdc911
examples: move all into subdirectories, closes #2513
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 11 Nov 2020 17:10:40 +0100 |
parents | examples/battle/registry.c@4ad7420ab678 |
children | 86b71e1f9dd5 |
comparison
equal
deleted
inserted
replaced
208:c0e0d4accae8 | 209:23a844fdc911 |
---|---|
1 /* | |
2 * registry.h -- registry of resources | |
3 * | |
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <stddef.h> | |
20 | |
21 #include <core/image.h> | |
22 #include <core/panic.h> | |
23 #include <core/util.h> | |
24 | |
25 #include <adventure/assets/sprites/john.h> | |
26 | |
27 #include <assets/images/haunted-wood.h> | |
28 #include <assets/images/black-cat.h> | |
29 | |
30 #include <assets/sprites/cursor.h> | |
31 #include <assets/sprites/explosion.h> | |
32 | |
33 #include <assets/sounds/fire.h> | |
34 | |
35 #include "registry.h" | |
36 | |
37 struct texture registry_textures[REGISTRY_TEXTURE_NUM]; | |
38 struct sprite registry_sprites[REGISTRY_TEXTURE_NUM]; | |
39 struct sound registry_sounds[REGISTRY_SOUND_NUM]; | |
40 | |
41 #define REGISTRY_TEXTURE(s, ptr, cw, ch) \ | |
42 { (s), (ptr), sizeof ((ptr)), (cw), (ch) } | |
43 | |
44 static const struct { | |
45 enum registry_texture index; | |
46 const void *data; | |
47 size_t datasz; | |
48 unsigned int cellw; | |
49 unsigned int cellh; | |
50 } textures[] = { | |
51 REGISTRY_TEXTURE(REGISTRY_TEXTURE_CURSOR, sprites_cursor, 24, 24), | |
52 REGISTRY_TEXTURE(REGISTRY_TEXTURE_EXPLOSION, sprites_explosion, 256, 256), | |
53 REGISTRY_TEXTURE(REGISTRY_TEXTURE_JOHN, sprites_john, 48, 48), | |
54 REGISTRY_TEXTURE(REGISTRY_TEXTURE_HAUNTED_WOOD, images_haunted_wood, 0, 0), | |
55 REGISTRY_TEXTURE(REGISTRY_TEXTURE_BLACK_CAT, images_black_cat, 0, 0) | |
56 }; | |
57 | |
58 #define REGISTRY_SOUND(s, ptr) \ | |
59 { (s), (ptr), sizeof ((ptr)) } | |
60 | |
61 static const struct { | |
62 enum registry_sound index; | |
63 const void *data; | |
64 size_t datasz; | |
65 } sounds[] = { | |
66 REGISTRY_SOUND(REGISTRY_SOUND_FIRE, sounds_fire) | |
67 }; | |
68 | |
69 static void | |
70 load_textures_and_sprites(void) | |
71 { | |
72 for (size_t i = 0; i < NELEM(textures); ++i) { | |
73 struct texture *texture = ®istry_textures[textures[i].index]; | |
74 struct sprite *sprite = ®istry_sprites[textures[i].index]; | |
75 | |
76 if (!image_openmem(texture, textures[i].data, textures[i].datasz)) | |
77 panic(); | |
78 | |
79 if (textures[i].cellw == 0 || textures[i].cellh == 0) | |
80 sprite_init(sprite, texture, texture->w, texture->h); | |
81 else | |
82 sprite_init(sprite, texture, textures[i].cellw, textures[i].cellh); | |
83 } | |
84 } | |
85 | |
86 static void | |
87 load_sounds(void) | |
88 { | |
89 for (size_t i = 0; i < NELEM(sounds); ++i) { | |
90 struct sound *sound = ®istry_sounds[sounds[i].index]; | |
91 | |
92 if (!sound_openmem(sound, sounds[i].data, sounds[i].datasz)) | |
93 panic(); | |
94 } | |
95 } | |
96 | |
97 void | |
98 registry_init(void) | |
99 { | |
100 load_textures_and_sprites(); | |
101 load_sounds(); | |
102 } | |
103 | |
104 void | |
105 registry_finish(void) | |
106 { | |
107 for (size_t i = 0; i < NELEM(registry_textures); ++i) | |
108 texture_finish(®istry_textures[i]); | |
109 for (size_t i = 0; i < NELEM(registry_sounds); ++i) | |
110 sound_finish(®istry_sounds[i]); | |
111 } |