Mirai's Miscellaneous Misadventures

M43 / core / displays.c

1/* license: AGPLv3 or later */
2/* copyright 2023 zamfofex */
3
4#include <mimimi.h>
5
6void mimimi_camera_stamp(struct mimimi_engine *engine, struct mimimi_position *camera, int x, int y, int z, void *texture)
7{
8	z += 8;
9	if (z <= 0) return;
10	
11	x -= camera->x;
12	x /= z;
13	x += engine->size.width / 2;
14	
15	y -= camera->y;
16	y /= z;
17	y += engine->size.height / 2;
18	
19	(*engine->stamp)(engine->data, x, y, texture);
20}
21
22void mimimi_display_tick(struct mimimi_display *display, struct mimimi_engine *engine)
23{
24	struct mimimi_sprite *sprite;
25	struct mimimi_appearance *appearance;
26	struct mimimi_position *position;
27	struct mimimi_physics *physics;
28	struct mimimi_life *life;
29	struct mimimi_walk *walk;
30	int dx, dy;
31	struct mimimi_video_set *videos;
32	struct mimimi_video *video;
33	void *texture;
34	int x, y;
35	
36	sprite = display->sprite;
37	appearance = display->appearance;
38	
39	position = &sprite->position;
40	physics = &sprite->physics;
41	life = &sprite->life;
42	walk = &sprite->walk;
43	
44	x = display->width * -8;
45	y = display->height * -8;
46	
47	if (walk->direction != 0) display->direction = walk->direction;
48	
49	if (display->direction == 1)
50	{
51		dx = -physics->dx;
52		videos = &appearance->left;
53	}
54	else if (display->direction == 2)
55	{
56		dx = physics->dx;
57		videos = &appearance->right;
58	}
59	else
60	{
61		return;
62	}
63	
64	dy = physics->dy;
65	
66	if (dx > 31) dx = 31;
67	if (dx < 0) dx = 0;
68	if (dy < 0) dy = 0;
69	
70	if (life->knocked_time == 0)
71	{
72		if (physics->airborne != 0)
73		{
74			dy = physics->dy;
75			
76			if (dy < -31) dy = -31;
77			if (dy > 31) dy = 31;
78			dy += 31;
79			
80			video = videos->jumping + dx * videos->jumping_video_count / 32;
81			texture = video->textures[dy * video->count / 64];
82			
83			mimimi_camera_stamp(engine, display->camera, position->x + x, position->y + y, 0, texture);
84			return;
85		}
86		
87		if (physics->airborne != 0)
88		{
89			texture = videos->jumping->textures[dx * videos->jumping->count / 32];
90			mimimi_camera_stamp(engine, display->camera, position->x + x, position->y + y, 0, texture);
91			return;
92		}
93		
94		video = videos->standing + dx * videos->standing_video_count / 32;
95		display->animation_time += dx / 4;
96	}
97	else
98	{
99		if (physics->airborne == 0)
100			video = videos->knocked,
101			y += 128;
102		else
103			video = videos->falling;
104		
105		display->animation_time += dy / 8;
106	}
107	
108	/* do not assume 'char' is eight bits */
109	display->animation_time &= 0xFF;
110	
111	texture = video->textures[display->animation_time * video->count / 256];
112	mimimi_camera_stamp(engine, display->camera, position->x + x, position->y + y, 0, texture);
113}
114
115void mimimi_background_tick(struct mimimi_background *background, struct mimimi_engine *engine)
116{
117	mimimi_camera_stamp(engine, background->camera, background->x - 7, background->y - 7, background->z, background->texture);
118}