This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.c
129 lines (109 loc) · 3.5 KB
/
plugin.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdlib.h>
#include <soundpipe.h>
#include <sporth.h>
#include "spigot.h"
#ifndef BUILD_SPORTH_PLUGIN
extern spigot_graphics *global_gfx;
#endif
typedef struct {
spigot_graphics *gfx;
int load;
spigot_state *state;
runt_vm vm;
runt_cell *cells;
unsigned char *mem;
} spigot_stuff;
int sporth_spigot(plumber_data *pd, sporth_stack *stack, void **ud)
{
const char *filename;
SPFLOAT in;
spigot_stuff *stuff;
int rc;
int zoom;
switch(pd->mode) {
case PLUMBER_CREATE:
if(sporth_check_args(stack, "ffs") != SPORTH_OK) {
fprintf(stderr,"Not enough arguments for spigot\n");
stack->error++;
return PLUMBER_NOTOK;
}
filename = sporth_stack_pop_string(stack);
sporth_stack_pop_float(stack);
sporth_stack_pop_float(stack);
stuff = malloc(sizeof(spigot_stuff));
stuff->cells = malloc(1024 * sizeof(runt_cell));
stuff->mem = malloc(5 * RUNT_MEGABYTE * sizeof(unsigned char));
runt_init(&stuff->vm);
runt_cell_pool_set(&stuff->vm, stuff->cells, 1024);
runt_cell_pool_init(&stuff->vm);
runt_memory_pool_set(&stuff->vm, stuff->mem, 5 * RUNT_MEGABYTE);
zoom = 3;
runt_load_stdlib(&stuff->vm);
runt_mark_set(&stuff->vm);
rc = spigot_load(pd, &stuff->vm, &zoom);
rc = spigot_parse(&stuff->vm, filename, &stuff->state);
#ifndef BUILD_SPORTH_PLUGIN
stuff->gfx = global_gfx;
spigot_set_zoom(stuff->gfx, zoom);
#else
stuff->gfx = spigot_gfx_new(zoom);
#endif
spigot_gfx_set_state(stuff->gfx, stuff->state);
*ud = stuff;
if(rc != PLUMBER_OK) {
plumber_print(pd, "Spigot: error reading file %s\n", filename);
stack->error++;
return PLUMBER_NOTOK;
}
break;
case PLUMBER_INIT:
stuff = *ud;
sporth_stack_pop_string(stack);
stuff->load = sporth_stack_pop_float(stack);
sporth_stack_pop_float(stack);
stuff->state->init(stuff->state->ud);
if(stuff->load) {
spigot_start_why_dont_you(stuff->gfx);
#ifdef BUILD_SPORTH_PLUGIN
spigot_gfx_init(stuff->gfx);
spigot_start(stuff->gfx);
#endif
}
break;
case PLUMBER_COMPUTE:
stuff = *ud;
sporth_stack_pop_float(stack);
in = sporth_stack_pop_float(stack);
if(in != 0) {
if(spigot_is_it_happening(stuff->gfx)) {
spigot_gfx_step(stuff->gfx);
}
stuff->state->step(stuff->state->ud);
}
stuff->state->compute(stuff->state->ud, in);
break;
case PLUMBER_DESTROY:
stuff = *ud;
if(runt_is_alive(&stuff->vm) == RUNT_OK) {
stuff->state->free(stuff->state->ud);
}
#ifdef BUILD_SPORTH_PLUGIN
if(spigot_is_it_happening(stuff->gfx)) {
spigot_stop(stuff->gfx);
}
spigot_gfx_free(stuff->gfx);
#endif
runt_close_plugins(&stuff->vm);
free(stuff->mem);
free(stuff->cells);
free(stuff);
break;
}
return PLUMBER_OK;
}
#ifdef BUILD_SPORTH_PLUGIN
plumber_dyn_func sporth_return_ugen()
{
return sporth_spigot;
}
#endif