Mirai's Miscellaneous Misadventures

M51 / core / cameras.c

1/* license: AGPLv3 or later */
2/* copyright 2024 zamfofex */
3
4#include <mimimi.h>
5
6void mimimi_camera_tick(struct mimimi_position *camera, struct mimimi_position *position, int ox, int oy)
7{
8	camera->x *= 3;
9	camera->y *= 3;
10	camera->x += position->x + ox;
11	camera->y += position->y + oy;
12	camera->x /= 4;
13	camera->y /= 4;
14}
15
16void mimimi_clamped_camera_tick(struct mimimi_clamped_camera *camera, int width0, int height0)
17{
18	struct mimimi_position *input;
19	struct mimimi_position *output;
20	int x, y, width, height;
21	int left, right;
22	int top, bottom;
23	
24	input = camera->input;
25	output = &camera->output;
26	
27	x = camera->offset.x;
28	y = camera->offset.y;
29	width = camera->size.width * 128;
30	height = camera->size.height * 128;
31	
32	left = x * 128;
33	right = left + width;
34	
35	top = y * 128;
36	bottom = top + height;
37	
38	width0 *= 8;
39	height0 *= 8;
40	
41	left += width0 / 2;
42	right -= width0 / 2;
43	top += height0 / 2;
44	bottom -= height0 / 2;
45	
46	*output = *input;
47	
48	if (output->x < left) output->x = left;
49	if (output->x > right) output->x = right;
50	if (output->y < top) output->y = top;
51	if (output->y > bottom) output->y = bottom;
52	
53	if (width < width0) output->x = left - (width0 - width) / 2;
54	if (height < height0) output->y = top - (height0 - height) / 2;
55}