Mirai's Miscellaneous Misadventures

M15 / engines / mfb.c

1// copyright 2022 zamfofex
2// license: AGPLv3 or later
3
4#include <stdlib.h>
5#include <stddef.h>
6
7#include <MiniFB.h>
8
9#include "../mimimi.h"
10
11static void mirai_mfb_stamp(void *opaque, int x0, int y0, struct mirai_image *image)
12{
13	uint32_t *buffer = opaque;
14	
15	static unsigned char xterm[] = {0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF};
16	
17	for (int y = 0 ; y < image->height ; y++)
18	for (int x = 0 ; x < image->width ; x++)
19	{
20		int x1 = x + x0;
21		int y1 = y + y0;
22		
23		if (x1 < 0) continue;
24		if (y1 < 0) continue;
25		if (x1 >= mirai_width) continue;
26		if (y1 >= mirai_height) continue;
27		
28		int offset = x1 + y1 * mirai_width;
29		
30		unsigned char color = image->colors[x + y * image->width];
31		if (color == 0) continue;
32		
33		color -= 16;
34		unsigned char r, g, b;
35		r = xterm[(color / 36) % 6];
36		g = xterm[(color / 6) % 6];
37		b = xterm[color % 6];
38		
39		buffer[offset + 0] = MFB_RGB(r, g, b);
40	}
41}
42
43int main(void)
44{
45	struct mfb_window *window = mfb_open_ex("Mirai's Miscellaneous Misadventures", mirai_width, mirai_height, WF_RESIZABLE);
46	if (window == NULL) return -1;
47	
48	uint32_t *buffer = malloc(mirai_width * mirai_height * 4);
49	if (buffer == NULL) return -1;
50	
51	struct mirai_engine engine = {buffer, &mirai_mfb_stamp};
52	struct mirai_game *game = malloc(mirai_game_size);
53	mirai_start(game, &engine);
54	
55	for (;;)
56	{
57		for (int y = 0 ; y < mirai_height ; y++)
58		for (int x = 0 ; x < mirai_width ; x++)
59		{
60			int offset = x + y * mirai_width;
61			buffer[offset] = MFB_RGB(0xEE, 0xEE, 0xEE);
62		}
63		
64		unsigned char *keys = mfb_get_key_buffer(window);
65		struct mirai_keys input = {keys[KB_KEY_LEFT], keys[KB_KEY_RIGHT]};
66		
67		if (keys[KB_KEY_Q]) break;
68		
69		mirai_step(game, input);
70		
71		if(mfb_update_ex(window, buffer, mirai_width, mirai_height) < 0) 
72			break;
73	}
74	
75	return 0;
76}