Mirai's Miscellaneous Misadventures

M14 / engines / mfb.c

// copyright 2022 zamfofex
// license: AGPLv3 or later

#include <stdlib.h>
#include <stddef.h>

#include <MiniFB.h>

#include "../mimimi.h"

static void mirai_mfb_stamp(void *opaque, int x0, int y0, struct mirai_image *image)
{
	uint32_t *buffer = opaque;
	
	static unsigned char xterm[] = {0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF};
	
	for (int y = 0 ; y < image->height ; y++)
	for (int x = 0 ; x < image->width ; x++)
	{
		int x1 = x + x0;
		int y1 = y + y0;
		
		if (x1 < 0) continue;
		if (y1 < 0) continue;
		if (x1 >= mirai_width) continue;
		if (y1 >= mirai_height) continue;
		
		int offset = x1 + y1 * mirai_width;
		
		unsigned char color = image->colors[x + y * image->width];
		if (color == 0) continue;
		
		color -= 16;
		unsigned char r, g, b;
		r = xterm[(color / 36) % 6];
		g = xterm[(color / 6) % 6];
		b = xterm[color % 6];
		
		buffer[offset + 0] = MFB_RGB(r, g, b);
	}
}

int main(void)
{
	struct mfb_window *window = mfb_open_ex("Mirai's Miscellaneous Misadventures", mirai_width, mirai_height, WF_RESIZABLE);
	if (window == NULL) return -1;
	
	uint32_t *buffer = malloc(mirai_width * mirai_height * 4);
	if (buffer == NULL) return -1;
	
	struct mirai_engine engine = {buffer, &mirai_mfb_stamp};
	struct mirai_game *game = malloc(mirai_game_size);
	mirai_start(game, &engine);
	
	for (;;)
	{
		for (int y = 0 ; y < mirai_height ; y++)
		for (int x = 0 ; x < mirai_width ; x++)
		{
			int offset = x + y * mirai_width;
			buffer[offset] = MFB_RGB(0xEE, 0xEE, 0xEE);
		}
		
		unsigned char *keys = mfb_get_key_buffer(window);
		struct mirai_keys input = {keys[KB_KEY_LEFT], keys[KB_KEY_RIGHT]};
		
		if (keys[KB_KEY_Q]) break;
		
		mirai_step(game, input);
		
		if(mfb_update_ex(window, buffer, mirai_width, mirai_height) < 0) 
			break;
	}
	
	return 0;
}