-
Notifications
You must be signed in to change notification settings - Fork 12
/
useworker.c
76 lines (67 loc) · 1.77 KB
/
useworker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <string.h>
#include <emscripten.h>
#include "handy.h"
#include "bit_map.h"
#include "acidwarp.h"
#include "worker.h"
#include "display.h"
static int imageFuncList[NUM_IMAGE_FUNCTIONS];
static int imageFuncListIndex=0;
static worker_param wpar;
static worker_handle worker;
static UCHAR *buf_graf;
unsigned int buf_graf_stride;
void draw_init(int draw_flags) {
wpar.flags = draw_flags;
makeShuffledList(imageFuncList, NUM_IMAGE_FUNCTIONS);
wpar.next = (draw_flags & DRAW_LOGO) ? -1 : imageFuncList[imageFuncListIndex];
worker = emscripten_create_worker("worker.js");
/* Could start a worker call here but simpler not to. */
}
void draw_quit(void) {
// FIXME
}
static void worker_callback(char *data, int size, void *arg)
{
if (buf_graf_stride == wpar.width) {
memcpy(buf_graf, data, wpar.width * wpar.height);
} else {
int i;
char *inp = data;
UCHAR *outp = buf_graf;
for (i = 0; i < wpar.height; i++) {
memcpy(outp, inp, wpar.width);
inp += wpar.width;
outp += buf_graf_stride;
}
}
disp_finishUpdate();
disp_swapBuffers();
startloop();
}
void draw_same(void)
{
disp_beginUpdate(&buf_graf, &buf_graf_stride, &wpar.width, &wpar.height);
emscripten_call_worker(worker, "draw", (char *)&wpar, sizeof(wpar),
worker_callback, NULL);
/* Worker callback will restart main loop */
emscripten_cancel_main_loop();
}
static void draw_advance(void)
{
if (wpar.flags & DRAW_LOGO) {
wpar.flags &= ~DRAW_LOGO;
} else {
if (++imageFuncListIndex >= NUM_IMAGE_FUNCTIONS) {
imageFuncListIndex = 0;
makeShuffledList(imageFuncList, NUM_IMAGE_FUNCTIONS);
}
}
}
void draw_next()
{
wpar.want = wpar.next;
draw_advance();
wpar.next = imageFuncList[imageFuncListIndex];
draw_same();
}