Mercurial > molko
comparison libmlk-rpg/mlk/rpg/tileset-loader-file.c @ 631:bb67f935a93f
rpg: rework a little tileset/map loaders
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 27 Aug 2023 11:28:35 +0200 |
parents | fee7bd30725c |
children |
comparison
equal
deleted
inserted
replaced
630:8d8fe99b357c | 631:bb67f935a93f |
---|---|
1 /* | 1 /* |
2 * tileset-file.c -- tileset file loader implementation | 2 * tileset-loader-file.c -- tileset file loader implementation |
3 * | 3 * |
4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr> | 4 * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr> |
5 * | 5 * |
6 * Permission to use, copy, modify, and/or distribute this software for any | 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 | 7 * purpose with or without fee is hereby granted, provided that the above |
32 #include "tileset.h" | 32 #include "tileset.h" |
33 | 33 |
34 #define THIS(loader) \ | 34 #define THIS(loader) \ |
35 MLK_CONTAINER_OF(loader, struct mlk_tileset_loader_file, iface) | 35 MLK_CONTAINER_OF(loader, struct mlk_tileset_loader_file, iface) |
36 | 36 |
37 static void | |
38 trash(struct mlk_tileset_loader_file *file) | |
39 { | |
40 mlk__loader_file_clear(file->lf); | |
41 | |
42 mlk_alloc_free(file->tilecollisions); | |
43 mlk_alloc_free(file->tileanimations); | |
44 | |
45 file->tilecollisions = NULL; | |
46 file->tileanimations = NULL; | |
47 } | |
48 | |
37 static void * | 49 static void * |
38 expand(void **array, size_t n, size_t w) | 50 expand(void **array, size_t n, size_t w) |
39 { | 51 { |
40 void *ptr; | 52 void *ptr; |
41 | 53 |
49 | 61 |
50 return ptr; | 62 return ptr; |
51 } | 63 } |
52 | 64 |
53 static struct mlk_texture * | 65 static struct mlk_texture * |
54 new_texture(struct mlk_tileset_loader *loader, struct mlk_tileset *tileset, const char *ident) | 66 new_texture(struct mlk_tileset_loader *self, struct mlk_tileset *tileset, const char *ident) |
55 { | 67 { |
56 (void)tileset; | 68 (void)tileset; |
57 | 69 |
58 struct mlk_tileset_loader_file *self = THIS(loader); | 70 struct mlk_tileset_loader_file *file = THIS(self); |
59 | 71 |
60 return mlk__loader_file_texture_open(self->lf, ident); | 72 return mlk__loader_file_texture_open(file->lf, ident); |
61 } | 73 } |
62 | 74 |
63 static struct mlk_sprite * | 75 static struct mlk_sprite * |
64 new_sprite(struct mlk_tileset_loader *loader, struct mlk_tileset *tileset) | 76 new_sprite(struct mlk_tileset_loader *self, struct mlk_tileset *tileset) |
65 { | 77 { |
66 (void)tileset; | 78 (void)tileset; |
67 | 79 |
68 struct mlk_tileset_loader_file *self = THIS(loader); | 80 struct mlk_tileset_loader_file *file = THIS(self); |
69 | 81 |
70 return mlk__loader_file_sprite_new(self->lf); | 82 return mlk__loader_file_sprite_new(file->lf); |
71 } | 83 } |
72 | 84 |
73 static struct mlk_animation * | 85 static struct mlk_animation * |
74 new_animation(struct mlk_tileset_loader *loader, struct mlk_tileset *tileset) | 86 new_animation(struct mlk_tileset_loader *self, struct mlk_tileset *tileset) |
75 { | 87 { |
76 (void)tileset; | 88 (void)tileset; |
77 | 89 |
78 struct mlk_tileset_loader_file *self = THIS(loader); | 90 struct mlk_tileset_loader_file *file = THIS(self); |
79 | 91 |
80 return mlk__loader_file_animation_new(self->lf); | 92 return mlk__loader_file_animation_new(file->lf); |
81 } | 93 } |
82 | 94 |
83 struct mlk_tileset_collision * | 95 struct mlk_tileset_collision * |
84 expand_collisions(struct mlk_tileset_loader *loader, | 96 expand_collisions(struct mlk_tileset_loader *self, |
85 struct mlk_tileset *tileset, | 97 struct mlk_tileset *tileset, |
86 struct mlk_tileset_collision *array, | 98 struct mlk_tileset_collision *array, |
87 size_t arraysz) | 99 size_t arraysz) |
88 { | 100 { |
89 (void)tileset; | 101 (void)tileset; |
90 (void)array; | 102 (void)array; |
91 | 103 |
92 struct mlk_tileset_loader_file *self = THIS(loader); | 104 struct mlk_tileset_loader_file *file = THIS(self); |
93 | 105 |
94 return expand((void **)&self->tilecollisions, arraysz, sizeof (struct mlk_tileset_collision)); | 106 return expand((void **)&file->tilecollisions, arraysz, sizeof (struct mlk_tileset_collision)); |
95 } | 107 } |
96 | 108 |
97 struct mlk_tileset_animation * | 109 struct mlk_tileset_animation * |
98 expand_animations(struct mlk_tileset_loader *loader, | 110 expand_animations(struct mlk_tileset_loader *self, |
99 struct mlk_tileset *tileset, | 111 struct mlk_tileset *tileset, |
100 struct mlk_tileset_animation *array, | 112 struct mlk_tileset_animation *array, |
101 size_t arraysz) | 113 size_t arraysz) |
102 { | 114 { |
103 (void)tileset; | 115 (void)tileset; |
104 (void)array; | 116 (void)array; |
105 | 117 |
106 struct mlk_tileset_loader_file *self = THIS(loader); | 118 struct mlk_tileset_loader_file *file = THIS(self); |
107 | 119 |
108 return expand((void **)&self->tileanimations, arraysz, sizeof (struct mlk_tileset_animation)); | 120 return expand((void **)&file->tileanimations, arraysz, sizeof (struct mlk_tileset_animation)); |
121 } | |
122 | |
123 static void | |
124 clear(struct mlk_tileset_loader *self, struct mlk_tileset *tileset) | |
125 { | |
126 (void)tileset; | |
127 | |
128 struct mlk_tileset_loader_file *file = THIS(self); | |
129 | |
130 trash(file); | |
131 } | |
132 | |
133 static void | |
134 finish(struct mlk_tileset_loader *self) | |
135 { | |
136 struct mlk_tileset_loader_file *file = THIS(self); | |
137 | |
138 trash(file); | |
139 mlk__loader_file_free(file->lf); | |
140 | |
141 file->lf = NULL; | |
109 } | 142 } |
110 | 143 |
111 int | 144 int |
112 mlk_tileset_loader_file_init(struct mlk_tileset_loader_file *file, const char *filename) | 145 mlk_tileset_loader_file_init(struct mlk_tileset_loader_file *file, const char *filename) |
113 { | 146 { |
122 file->iface.new_texture = new_texture; | 155 file->iface.new_texture = new_texture; |
123 file->iface.new_sprite = new_sprite; | 156 file->iface.new_sprite = new_sprite; |
124 file->iface.new_animation = new_animation; | 157 file->iface.new_animation = new_animation; |
125 file->iface.expand_collisions = expand_collisions; | 158 file->iface.expand_collisions = expand_collisions; |
126 file->iface.expand_animations = expand_animations; | 159 file->iface.expand_animations = expand_animations; |
160 file->iface.clear = clear; | |
161 file->iface.finish = finish; | |
127 | 162 |
128 return 0; | 163 return 0; |
129 } | 164 } |
130 | |
131 void | |
132 mlk_tileset_loader_file_finish(struct mlk_tileset_loader_file *file) | |
133 { | |
134 assert(file); | |
135 | |
136 mlk__loader_file_free(file->lf); | |
137 | |
138 mlk_alloc_free(file->tilecollisions); | |
139 mlk_alloc_free(file->tileanimations); | |
140 | |
141 memset(file, 0, sizeof (*file)); | |
142 } |