Mirai's Miscellaneous Misadventures
M40 / test / chapter.c
1
2
3
4#include <mimimi/test.h>
5#include <mimimi/chapters.h>
6#include <mimimi/characters.h>
7#include <mimimi/appearances.h>
8#include <mimimi/allocators.h>
9#include <mimimi/ground.h>
10#include <mimimi/compound-behaviors.h>
11#include <mimimi/geometry.h>
12#include <mimimi/assets.h>
13#include <mimimi/engines.h>
14#include <mimimi/controls.h>
15#include <mimimi/sprites.h>
16#include <mimimi/cameras.h>
17#include <mimimi/behaviors.h>
18#include <mimimi/font.h>
19#include <mimimi/fonts.h>
20#include <mimimi/animations.h>
21#include <mimimi/collisions.h>
22#include <mimimi/version.h>
23#include <mimimi/text.h>
24
25#include "ground.c"
26
27static void mimimi_test_animation_set(struct mimimi_animation_set *set, struct mimimi_allocator *allocator)
28{
29 int animation_count = 8;
30 int image_count = 8;
31 int width = 48;
32 int height = 48;
33
34 int total_animation_count = 3 + animation_count;
35 int total_image_count = 2 + image_count * animation_count + image_count;
36
37 struct mimimi_animation *animations = mimimi_allocate(allocator, total_animation_count * sizeof *animations);
38 struct mimimi_image *images = mimimi_allocate(allocator, total_image_count * sizeof *images);
39
40 set->standing = animations;
41 set->standing_animation_count = animation_count;
42 animations += animation_count;
43
44 for (int i = 0 ; i < animation_count ; i++)
45 {
46 struct mimimi_animation *animation = set->standing + i;
47 animation->count = image_count;
48 animation->images = images;
49 images += image_count;
50
51 for (int i = 0 ; i < image_count ; i++)
52 {
53 struct mimimi_image *image = animation->images + i;
54 image->width = width;
55 image->height = height;
56 image->colors = mimimi_allocate(allocator, width * height);
57 for (int i = 0 ; i < width * height ; i++) image->colors[i] = 0;
58 }
59 }
60
61 struct mimimi_animation *knocked = animations++;
62 knocked->count = 1;
63 knocked->images = images++;
64 knocked->images[0].width = width;
65 knocked->images[0].height = height;
66 knocked->images[0].colors = mimimi_allocate(allocator, width * height);
67 for (int i = 0 ; i < width * height ; i++) knocked->images[0].colors[i] = 0;
68
69 struct mimimi_animation *falling = animations++;
70 falling->count = 1;
71 falling->images = images++;
72 falling->images[0].width = width;
73 falling->images[0].height = height;
74 falling->images[0].colors = mimimi_allocate(allocator, width * height);
75 for (int i = 0 ; i < width * height ; i++) falling->images[0].colors[i] = 0;
76
77 set->knocked = knocked;
78 set->falling = falling;
79
80 set->jumping = animations++;
81 set->jumping->count = image_count;
82 set->jumping->images = images;
83 images += image_count;
84
85 for (int i = 0 ; i < image_count ; i++)
86 {
87 struct mimimi_image *image = set->jumping->images + i;
88 image->width = width;
89 image->height = height;
90 image->colors = mimimi_allocate(allocator, width * height);
91 for (int i = 0 ; i < width * height ; i++) image->colors[i] = 0;
92 }
93}
94
95static struct mimimi_appearance *mimimi_test_appearance(struct mimimi_allocator *allocator)
96{
97 struct mimimi_appearance *appearance = mimimi_allocate(allocator, sizeof *appearance);
98
99 mimimi_test_animation_set(&appearance->left, allocator);
100 mimimi_test_animation_set(&appearance->right, allocator);
101
102 return appearance;
103}
104
105struct mimimi_test_sprite
106{
107 int x, y;
108 struct mimimi_model *model;
109};
110
111static void mimimi_test_player(struct mimimi_behavior *compound, struct mimimi_chapter *chapter, struct mimimi_sprite *player, struct mimimi_allocator *allocator)
112{
113 struct mimimi_allocator *compound_allocator = mimimi_compound_allocator(compound, allocator);
114
115 struct mimimi_history *history = mimimi_allocate(compound_allocator, sizeof *history);
116 mimimi_compound(compound, mimimi_history(history, chapter, allocator));
117
118 struct mimimi_jump *jump = mimimi_allocate(compound_allocator, sizeof *jump);
119 struct mimimi_behavior *jump_behavior = mimimi_jump(jump, player->physics, compound_allocator);
120
121 struct mimimi_controls *controls = mimimi_allocate(compound_allocator, sizeof *controls);
122 struct mimimi_behavior *controls_behavior = mimimi_controls(controls, player->walk, history, allocator);
123
124 controls->jump = mimimi_choice(&player->physics->airborne, mimimi_noop, jump_behavior, compound_allocator);
125 mimimi_compound(compound, mimimi_choice(&player->awake, controls_behavior, mimimi_noop, allocator));
126}
127
128static struct mimimi_image *mimimi_test_background(struct mimimi_ground *ground, unsigned char *colors, struct mimimi_allocator *allocator)
129{
130 struct mimimi_image *background = mimimi_allocate(allocator, sizeof *background);
131 background->colors = mimimi_allocate(allocator, ground->width * 16 * ground->height * 16);
132
133 background->width = ground->width * 16;
134 background->height = ground->height * 16;
135
136 for (int y = 0 ; y < ground->height * 16 ; y++)
137 for (int x = 0 ; x < ground->width * 16 ; x++)
138 {
139 unsigned char color = colors[mimimi_ground_tile(ground, x / 16, y / 16)];
140 background->colors[x + y * ground->width * 16] = color;
141 }
142
143 return background;
144}
145
146struct mimimi_chapter *mimimi_test(struct mimimi_engine *engine)
147{
148 static struct mimimi_ground *ground = &mimimi_test_ground;
149
150 struct mimimi_behavior *compound = mimimi_compound_behavior(engine->allocator);
151 struct mimimi_allocator *compound_allocator = mimimi_compound_allocator(compound, engine->allocator);
152
153 struct mimimi_chapter *chapter = mimimi_allocate(compound_allocator, sizeof *chapter);
154 chapter->behavior = compound;
155
156 struct mimimi_position *camera = mimimi_allocate(compound_allocator, sizeof *camera);
157
158 struct mimimi_test_sprite infos[] =
159 {
160 {110, 36, mimimi_mango},
161 {105, 36, mimimi_pepper},
162 };
163
164 int count = sizeof infos / sizeof *infos;
165
166 struct mimimi_position **positions = mimimi_allocate(compound_allocator, count * sizeof *positions);
167 mimimi_compound(compound, mimimi_collision(count, positions, 128, 256, 512, 64, engine->allocator));
168
169 int *directions = mimimi_allocate(compound_allocator, count * sizeof *directions);
170
171 struct mimimi_sprite **sprites = mimimi_allocate(engine->allocator, count * sizeof *sprites);
172
173 for (int i = 0 ; i < count ; i++)
174 {
175 struct mimimi_test_sprite *info = infos + i;
176
177 struct mimimi_sprite *sprite = mimimi_sprite(ground, info->x * 128, info->y * 128, 80, 250, engine->allocator);
178 mimimi_compound(compound, sprite->behavior);
179 sprites[i] = sprite;
180
181 if (i == 0)
182 {
183 mimimi_test_player(compound, chapter, sprite, engine->allocator);
184 mimimi_compound(compound, mimimi_camera(camera, sprite->position, 0, -512, engine->allocator));
185 }
186 else
187 {
188 struct mimimi_jump *jump = mimimi_allocate(compound_allocator, sizeof *jump);
189 struct mimimi_behavior *jump_behavior = mimimi_jump(jump, sprite->physics, compound_allocator);
190
191 struct mimimi_path_finding *path_finding = mimimi_allocate(compound_allocator, sizeof *path_finding);
192 mimimi_compound(compound, mimimi_find_path(path_finding, ground, sprites[i - 1]->position, sprite, engine->allocator));
193
194 path_finding->jump = jump;
195 path_finding->jump_behavior = jump_behavior;
196 }
197
198 positions[i] = sprite->position;
199 }
200
201 static unsigned char ground_colors[] = {0x0D, 0x05};
202 struct mimimi_image *background = mimimi_test_background(ground, ground_colors, compound_allocator);
203
204 mimimi_compound(compound, mimimi_background(engine, camera, (*engine->texture)(engine->data, background), 0, 0, 0, engine->allocator));
205
206 for (int i = 0 ; i < count ; i++)
207 {
208 struct mimimi_test_sprite *info = infos + i;
209 struct mimimi_sprite *sprite = sprites[i];
210
211 struct mimimi_appearance *appearance = mimimi_test_appearance(compound_allocator);
212 mimimi_appearance(appearance, info->model, 24, 48, engine->allocator);
213
214 struct mimimi_video_appearance *video_appearance = mimimi_allocate(compound_allocator, sizeof *video_appearance);
215 mimimi_video_appearance(video_appearance, appearance, engine, compound_allocator);
216
217 mimimi_compound(compound, mimimi_display(engine, camera, sprite, video_appearance, directions + i, 24, 48, engine->allocator));
218 }
219
220 mimimi_deallocate(engine->allocator, sprites);
221
222 struct mimimi_image *overlay = mimimi_allocate(compound_allocator, sizeof *overlay);
223 overlay->width = 256;
224 overlay->height = 64;
225 overlay->colors = mimimi_allocate(compound_allocator, overlay->width * overlay->height);
226
227 for (int i = 0 ; i < overlay->width * overlay->height ; i++)
228 overlay->colors[i] = 0;
229
230 struct mimimi_allocator *text_allocator = mimimi_temporary_allocator(engine->allocator);
231
232 unsigned char *name = mimimi_utf8("mimimi game engine ", text_allocator);
233 unsigned char *version = mimimi_utf8(mimimi_version, text_allocator);
234
235 int name_count = mimimi_count_text(name);
236 int version_count = mimimi_count_text(version);
237 int text_count = name_count + version_count;
238
239 unsigned char *text = mimimi_allocate(text_allocator, text_count + 1);
240 for (int i = 0 ; i < name_count ; i++) text[i] = name[i];
241 for (int i = 0 ; i < version_count ; i++) text[name_count + i] = version[i];
242 text[text_count] = 0;
243
244 mimimi_draw_text(overlay, 16, 16, overlay->width - 32, 16, text, mimimi_font, 0x76);
245
246 void *overlay_texture = (*engine->texture)(engine->data, overlay);
247 mimimi_compound(compound, mimimi_overlay(engine, overlay_texture, 0, 0, engine->allocator));
248
249 mimimi_finish_temporary_allocator(text_allocator);
250
251 return chapter;
252}