Mirai's Miscellaneous Misadventures
M19 / engines / mfb.c
#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 rch[] = {0x11, 0x44, 0x77, 0x99, 0xCC, 0xFF};
static unsigned char gch[] = {0x11, 0x33, 0x55, 0x77, 0x99, 0xBB, 0xDD, 0xFF};
static unsigned char bch[] = {0x22, 0x55, 0x88, 0xBB, 0xEE};
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;
if (color < 0x10)
{
unsigned char ch = color * 0x11;
buffer[offset] = MFB_RGB(ch, ch, ch);
}
else
{
color -= 0x10;
unsigned char r, g, b;
r = rch[(color / 40) % 6];
g = gch[(color / 5) % 8];
b = bch[(color / 1) % 5];
buffer[offset] = 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 (;;)
{
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);
}