Mirai's Miscellaneous Misadventures
M31 / 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>
#include <mimimi/malloc.h>
static void *mimimi_mfb_texture(void *data, struct mimimi_image *image)
{
(void) data;
return image;
}
static void mimimi_mfb_invalidate(void *data, void *texture)
{
(void) data;
(void) texture;
}
static void mimimi_mfb_stamp(void *data, int x1, int y1, void *texture)
{
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};
struct mimimi_image *image = texture;
uint32_t *buffer = data;
int x0 = 0;
int y0 = 0;
if (x1 < 0) x0 = -x1;
if (y1 < 0) y0 = -y1;
int width = image->width;
int height = image->height;
if (width + x1 > 512) width = 512 - x1;
if (height + y1 > 256) height = 256 - y1;
for (int y = y0 ; y < height ; y++)
for (int x = x0 ; x < width ; x++)
{
unsigned char color = image->colors[x + y * image->width];
if (color == 0) continue;
int x2 = x + x1;
int y2 = y + y1;
int offset = x2 + y2 * 512;
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 *(*start)(struct mimimi_engine *engine))
{
static struct mimimi_size size = {512, 256};
struct mfb_window *window = mfb_open_ex("Mirai's Miscellaneous Misadventures", size.width, size.height, WF_RESIZABLE);
if (window == NULL) exit(1);
uint32_t *buffer = malloc(size.width * size.height * 4);
if (buffer == NULL) exit(1);
struct mimimi_engine engine;
engine.data = buffer;
engine.texture = &mimimi_mfb_texture;
engine.invalidate = &mimimi_mfb_invalidate;
engine.stamp = &mimimi_mfb_stamp;
engine.size = &size;
engine.allocator = mimimi_malloc;
struct mimimi_chapter *chapter = (*start)(&engine);
struct mimimi_behavior *behavior = chapter->behavior;
for (;;)
{
unsigned char *keys = (void *) 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, size.width, size.height) < 0)
break;
}
mfb_close(window);
(*behavior->finish)(behavior->data);
free(buffer);
}