Mirai's Miscellaneous Misadventures

M45 / chapters / loader / setup.c

1/* license: AGPLv3 or later */
2/* copyright 2023 zamfofex */
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	{
54		loader->started = 1;
55	}
56	
57	chapter = &loader->chapter.data;
58	
59	if (loader->started == 1)
60	{
61		if (left == 0 && right == 0)
62		{
63			(*chapter->engine->invalidate)(chapter->engine->data, chapter->background);
64			for (i = 0 ; i < count ; i++)
65				(*chapter->engine->invalidate)(chapter->engine->data, chapter->textures[i]);
66			
67			chapter_data = &loader->chapter;
68			loader->started = 2;
69			(*mimimi_loader_chapters[loader->index].start)(chapter_data, loader->chapter.data.engine);
70			(*mimimi_loader_chapters[loader->index].tick)(chapter_data, left, right);
71			
72			return;
73		}
74	}
75	else
76	{
77		if (left == 0)
78		if (chapter->previous_direction == -1)
79		if (loader->index > 0)
80			loader->index--;
81		
82		if (right == 0)
83		if (chapter->previous_direction == 1)
84		if (loader->index < count - 1)
85			loader->index++;
86		
87		chapter->previous_direction = 0;
88		if (left != 0) chapter->previous_direction = -1;
89		if (right != 0) chapter->previous_direction = 1;
90	}
91	
92	width = chapter->images[0].width;
93	height = chapter->images[0].height;
94	
95	chapter->x *= 3;
96	chapter->x += (width + 8) * loader->index;
97	chapter->x /= 4;
98	
99	(*chapter->engine->stamp)(chapter->engine->data, 0, 0, chapter->background);
100	for (i = 0 ; i < count ; i++)
101	{
102		x = chapter->engine->size.width / 2 - width / 2;
103		y = chapter->engine->size.height / 2 - height / 2;
104		x += (width + 8) * i - chapter->x;
105		(*chapter->engine->stamp)(chapter->engine->data, x, y, chapter->textures[i]);
106	}
107}
108
109void mimimi_loader(void *data, struct mimimi_engine *engine)
110{
111	static int count = sizeof mimimi_loader_chapters / sizeof *mimimi_loader_chapters;
112	static unsigned char background_colors[512 * 256];
113	static char title[] = "select chapter";
114	static char note[] = "press [left] and [right] at the same time to select";
115	
116	struct mimimi_loader *loader;
117	struct mimimi_loader_data *chapter;
118	struct mimimi_image background;
119	int i;
120	unsigned char *colors;
121	struct mimimi_image *image;
122	struct mimimi_loader_chapter_info *info;
123	int y;
124	int width;
125	
126	loader = data;
127	chapter = &loader->chapter.data;
128	
129	loader->index = 0;
130	loader->started = 0;
131	chapter->engine = engine;
132	chapter->previous_direction = 0;
133	chapter->x = 0;
134	
135	background.colors = background_colors;
136	background.width = 512;
137	background.height = 256;
138	mimimi_loader_fill(&background, 0x16);
139	
140	width = mimimi_measure_paragraph(mimimi_font, title);
141	mimimi_draw_paragraph(mimimi_font, title, &background, background.width / 2 - width / 2, 32, 0xD);
142	
143	width = mimimi_measure_paragraph(mimimi_font, note);
144	mimimi_draw_paragraph(mimimi_font, note, &background, background.width / 2 - width / 2, background.height - 32 - 16, 0xD);
145	
146	chapter->background = (*chapter->engine->texture)(chapter->engine->data, &background);
147	
148	colors = chapter->colors;
149	
150	for (i = 0 ; i < count ; i++)
151	{
152		info = mimimi_loader_chapters + i;
153		image = chapter->images + i;
154		image->width = 192;
155		image->height = 96;
156		image->colors = colors;
157		colors += image->width * image->height;
158		mimimi_loader_fill(image, 0xD);
159		y = mimimi_draw_text(mimimi_font, info->name, 172, 18, image, 8, 2, 4);
160		mimimi_draw_text(mimimi_font, info->description, 172, 18, image, 8, y + 4, 4);
161		chapter->textures[i] = (*engine->texture)(engine->data, image);
162	}
163}