Mirai's Miscellaneous Misadventures
M27 / 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#include <mimimi/engines/mfb.h>
14#include <mimimi/malloc.h>
15
16static void mimimi_mfb_stamp(void *data, int x1, int y1, struct mimimi_image *image)
17{
18 static unsigned char rch[] = {0x11, 0x44, 0x77, 0x99, 0xCC, 0xFF};
19 static unsigned char gch[] = {0x11, 0x33, 0x55, 0x77, 0x99, 0xBB, 0xDD, 0xFF};
20 static unsigned char bch[] = {0x22, 0x55, 0x88, 0xBB, 0xEE};
21
22 uint32_t *buffer = data;
23
24 int x0 = 0;
25 int y0 = 0;
26
27 if (x1 < 0) x0 = -x1;
28 if (y1 < 0) y0 = -y1;
29
30 int width = image->width;
31 int height = image->height;
32
33 if (width + x1 > 512) width = 512 - x1;
34 if (height + y1 > 256) height = 256 - y1;
35
36 for (int y = y0 ; y < height ; y++)
37 for (int x = x0 ; x < width ; x++)
38 {
39 unsigned char color = image->colors[x + y * image->width];
40 if (color == 0) continue;
41
42 int x2 = x + x1;
43 int y2 = y + y1;
44
45 int offset = x2 + y2 * 512;
46
47 if (color < 0x10)
48 {
49 unsigned char ch = color * 0x11;
50 buffer[offset] = MFB_RGB(ch, ch, ch);
51 }
52 else
53 {
54 color -= 0x10;
55 unsigned char r, g, b;
56 r = rch[(color / 40) % 6];
57 g = gch[(color / 5) % 8];
58 b = bch[(color / 1) % 5];
59
60 buffer[offset] = MFB_RGB(r, g, b);
61 }
62 }
63}
64
65void mimimi_mfb(struct mimimi_chapter *(*start)(struct mimimi_engine *engine))
66{
67 static struct mimimi_size size = {512, 256};
68
69 struct mfb_window *window = mfb_open_ex("Mirai's Miscellaneous Misadventures", size.width, size.height, WF_RESIZABLE);
70 if (window == NULL) exit(1);
71
72 uint32_t *buffer = malloc(size.width * size.height * 4);
73 if (buffer == NULL) exit(1);
74
75 struct mimimi_engine engine;
76 engine.data = buffer;
77 engine.stamp = &mimimi_mfb_stamp;
78 engine.size = &size;
79 engine.allocator = mimimi_malloc;
80 struct mimimi_chapter *chapter = (*start)(&engine);
81 struct mimimi_behavior *behavior = chapter->behavior;
82
83 for (;;)
84 {
85 unsigned char *keys = (void *) mfb_get_key_buffer(window);
86 if (keys[KB_KEY_Q]) break;
87
88 chapter->left = keys[KB_KEY_LEFT];
89 chapter->right = keys[KB_KEY_RIGHT];
90
91 (*behavior->behave)(behavior->data);
92
93 if(mfb_update_ex(window, buffer, size.width, size.height) < 0)
94 break;
95 }
96
97 mfb_close(window);
98 (*behavior->finish)(behavior->data);
99 free(buffer);
100}