Mirai's Miscellaneous Misadventures

M18 / engines / mfb.c

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

#include <stdlib.h>

#include <MiniFB.h>

#include <mimimi/engines.h>
#include <mimimi/assets.h>
#include <mimimi/behaviors.h>
#include <mimimi/geometry.h>
#include <mimimi/chapters.h>

#include <mimimi/engines/mfb.h>

static struct mimimi_size mimimi_mfb_size = {512, 256};

static void mimimi_mfb_stamp(void *data, int x0, int y0, struct mimimi_image *image)
{
	static unsigned char xterm[] = {0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF};
	
	uint32_t *buffer = data;
	
	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 >= mimimi_mfb_size.width) continue;
		if (y1 >= mimimi_mfb_size.height) continue;
		
		int offset = x1 + y1 * mimimi_mfb_size.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);
	}
}

void mimimi_mfb(struct mimimi_chapter *chapter)
{
	struct mfb_window *window = mfb_open_ex("Mirai's Miscellaneous Misadventures", mimimi_mfb_size.width, mimimi_mfb_size.height, WF_RESIZABLE);
	if (window == NULL) exit(1);
	
	uint32_t *buffer = malloc(mimimi_mfb_size.width * mimimi_mfb_size.height * 4);
	if (buffer == NULL) exit(1);
	
	chapter->engine->data = buffer;
	chapter->engine->stamp = &mimimi_mfb_stamp;
	chapter->engine->size = &mimimi_mfb_size;
	
	struct mimimi_behavior *behavior = chapter->behavior;
	
	for (;;)
	{
		for (int y = 0 ; y < mimimi_mfb_size.height ; y++)
		for (int x = 0 ; x < mimimi_mfb_size.width ; x++)
		{
			int offset = x + y * mimimi_mfb_size.width;
			buffer[offset] = MFB_RGB(0xEE, 0xEE, 0xEE);
		}
		
		unsigned char *keys = mfb_get_key_buffer(window);
		if (keys[KB_KEY_Q]) break;
		
		chapter->left = keys[KB_KEY_LEFT];
		chapter->right = keys[KB_KEY_RIGHT];
		
		(*behavior->behave)(behavior->data);
		
		if(mfb_update_ex(window, buffer, mimimi_mfb_size.width, mimimi_mfb_size.height) < 0) 
			break;
	}
	
	mfb_close(window);
	(*behavior->finish)(behavior->data);
	free(buffer);
}