Mirai's Miscellaneous Misadventures
M49 / chapters / loader / setup.c
1
2
3
4#include <mimimi-chapters.h>
5
6struct mimimi_loader_chapter_info
7{
8 char *name, *description;
9 void (*start)(void *chapter, struct mimimi_engine *engine);
10 void (*tick)(void *chapter, unsigned char left, unsigned char right);
11};
12
13static struct mimimi_loader_chapter_info mimimi_loader_chapters[] =
14{
15 {
16 "test chapter", "This is a chapter to test the foundations of the game and its engine. It features the characters Mango and Pepper!",
17 &mimimi_test, &mimimi_test_tick,
18 },
19 {
20 "chapter I", "This is the first chapter of the game! It is currently still a work-in-progress, but strives to eventually come to tell a story.",
21 &mimimi_chapter_I, &mimimi_chapter_I_tick,
22 },
23};
24
25static void mimimi_loader_fill(struct mimimi_image *image, unsigned char color)
26{
27 int i;
28 for (i = 0 ; i < image->width * image->height ; i++)
29 image->colors[i] = color;
30}
31
32void mimimi_loader_tick(void *data, unsigned char left, unsigned char right)
33{
34 static int count = sizeof mimimi_loader_chapters / sizeof *mimimi_loader_chapters;
35
36 struct mimimi_loader *loader;
37 struct mimimi_loader_data *chapter;
38 void *chapter_data;
39 int i;
40 int x, y;
41 int width, height;
42
43 loader = data;
44
45 if (loader->started == 2)
46 {
47 chapter_data = &loader->chapter;
48 (*mimimi_loader_chapters[loader->index].tick)(chapter_data, left, right);
49 return;
50 }
51
52 if (left != 0 && right != 0)
53 loader->started = 1;
54
55 chapter = &loader->chapter.data;
56
57 if (loader->started == 1)
58 {
59 if (left == 0 && right == 0)
60 {
61 (*chapter->engine->invalidate)(chapter->engine->data, chapter->background);
62 for (i = 0 ; i < count ; i++)
63 (*chapter->engine->invalidate)(chapter->engine->data, chapter->textures[i]);
64
65 chapter_data = &loader->chapter;
66 loader->started = 2;
67 (*mimimi_loader_chapters[loader->index].start)(chapter_data, loader->chapter.data.engine);
68 (*mimimi_loader_chapters[loader->index].tick)(chapter_data, left, right);
69
70 return;
71 }
72 }
73 else
74 {
75 if (left == 0)
76 if (chapter->previous_direction == -1)
77 if (loader->index > 0)
78 loader->index--;
79
80 if (right == 0)
81 if (chapter->previous_direction == 1)
82 if (loader->index < count - 1)
83 loader->index++;
84
85 chapter->previous_direction = 0;
86 if (left != 0) chapter->previous_direction = -1;
87 if (right != 0) chapter->previous_direction = 1;
88 }
89
90 width = chapter->images[0].width;
91 height = chapter->images[0].height;
92
93 chapter->x *= 3;
94 chapter->x += (width + 8) * loader->index;
95 chapter->x /= 4;
96
97 (*chapter->engine->stamp)(chapter->engine->data, 0, 0, chapter->background);
98 for (i = 0 ; i < count ; i++)
99 {
100 x = chapter->engine->size.width / 2 - width / 2;
101 y = chapter->engine->size.height / 2 - height / 2;
102 x += (width + 8) * i - chapter->x;
103 (*chapter->engine->stamp)(chapter->engine->data, x, y, chapter->textures[i]);
104 }
105}
106
107void mimimi_loader(void *data, struct mimimi_engine *engine)
108{
109 static int count = sizeof mimimi_loader_chapters / sizeof *mimimi_loader_chapters;
110 static unsigned char background_colors[512 * 256];
111 static char title[] = "select chapter";
112 static char note[] = "press [left] and [right] at the same time to select";
113
114 struct mimimi_loader *loader;
115 struct mimimi_loader_data *chapter;
116 int i;
117 unsigned char *colors;
118 struct mimimi_image *image;
119 struct mimimi_loader_chapter_info *info;
120 int y;
121 int width;
122
123 loader = data;
124 chapter = &loader->chapter.data;
125
126 loader->index = 0;
127 loader->started = 0;
128 chapter->engine = engine;
129 chapter->previous_direction = 0;
130 chapter->x = 0;
131
132 chapter->background_image.colors = background_colors;
133 chapter->background_image.width = 512;
134 chapter->background_image.height = 256;
135 mimimi_loader_fill(&chapter->background_image, 0x16);
136
137 width = mimimi_measure_paragraph(mimimi_font, title);
138 mimimi_draw_paragraph(mimimi_font, title, &chapter->background_image, chapter->background_image.width / 2 - width / 2, 32, 0xD);
139
140 width = mimimi_measure_paragraph(mimimi_font, note);
141 mimimi_draw_paragraph(mimimi_font, note, &chapter->background_image, chapter->background_image.width / 2 - width / 2, chapter->background_image.height - 32 - 16, 0xD);
142
143 chapter->background = (*chapter->engine->texture)(chapter->engine->data, &chapter->background_image);
144
145 colors = chapter->colors;
146
147 for (i = 0 ; i < count ; i++)
148 {
149 info = mimimi_loader_chapters + i;
150 image = chapter->images + i;
151 image->width = 192;
152 image->height = 96;
153 image->colors = colors;
154 colors += image->width * image->height;
155 mimimi_loader_fill(image, 0xD);
156 y = mimimi_draw_text(mimimi_font, info->name, 172, 18, image, 8, 2, 4);
157 mimimi_draw_text(mimimi_font, info->description, 172, 18, image, 8, y + 4, 4);
158 chapter->textures[i] = (*engine->texture)(engine->data, image);
159 }
160}