Mirai's Miscellaneous Misadventures
M18 / engines / mfb.c
1
2
3
4#include <stdlib.h>
5
6#include <MiniFB.h>
7
8#include <mimimi/engines.h>
9#include <mimimi/assets.h>
10#include <mimimi/behaviors.h>
11#include <mimimi/geometry.h>
12#include <mimimi/chapters.h>
13
14#include <mimimi/engines/mfb.h>
15
16static struct mimimi_size mimimi_mfb_size = {512, 256};
17
18static void mimimi_mfb_stamp(void *data, int x0, int y0, struct mimimi_image *image)
19{
20 static unsigned char xterm[] = {0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF};
21
22 uint32_t *buffer = data;
23
24 for (int y = 0 ; y < image->height ; y++)
25 for (int x = 0 ; x < image->width ; x++)
26 {
27 int x1 = x + x0;
28 int y1 = y + y0;
29
30 if (x1 < 0) continue;
31 if (y1 < 0) continue;
32 if (x1 >= mimimi_mfb_size.width) continue;
33 if (y1 >= mimimi_mfb_size.height) continue;
34
35 int offset = x1 + y1 * mimimi_mfb_size.width;
36
37 unsigned char color = image->colors[x + y * image->width];
38 if (color == 0) continue;
39
40 color -= 16;
41 unsigned char r, g, b;
42 r = xterm[(color / 36) % 6];
43 g = xterm[(color / 6) % 6];
44 b = xterm[color % 6];
45
46 buffer[offset + 0] = MFB_RGB(r, g, b);
47 }
48}
49
50void mimimi_mfb(struct mimimi_chapter *chapter)
51{
52 struct mfb_window *window = mfb_open_ex("Mirai's Miscellaneous Misadventures", mimimi_mfb_size.width, mimimi_mfb_size.height, WF_RESIZABLE);
53 if (window == NULL) exit(1);
54
55 uint32_t *buffer = malloc(mimimi_mfb_size.width * mimimi_mfb_size.height * 4);
56 if (buffer == NULL) exit(1);
57
58 chapter->engine->data = buffer;
59 chapter->engine->stamp = &mimimi_mfb_stamp;
60 chapter->engine->size = &mimimi_mfb_size;
61
62 struct mimimi_behavior *behavior = chapter->behavior;
63
64 for (;;)
65 {
66 for (int y = 0 ; y < mimimi_mfb_size.height ; y++)
67 for (int x = 0 ; x < mimimi_mfb_size.width ; x++)
68 {
69 int offset = x + y * mimimi_mfb_size.width;
70 buffer[offset] = MFB_RGB(0xEE, 0xEE, 0xEE);
71 }
72
73 unsigned char *keys = mfb_get_key_buffer(window);
74 if (keys[KB_KEY_Q]) break;
75
76 chapter->left = keys[KB_KEY_LEFT];
77 chapter->right = keys[KB_KEY_RIGHT];
78
79 (*behavior->behave)(behavior->data);
80
81 if(mfb_update_ex(window, buffer, mimimi_mfb_size.width, mimimi_mfb_size.height) < 0)
82 break;
83 }
84
85 mfb_close(window);
86 (*behavior->finish)(behavior->data);
87 free(buffer);
88}