-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaggy_scheme.c
109 lines (94 loc) · 2.78 KB
/
shaggy_scheme.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
#include "engine.h"
#include "scheme_helper.h"
/* -------- API WRAPPER ---------- */
#if 0
static void my_free(void *data)
{
printf("cleanup %s\n",data);
free(data);
}
static pointer scheme_test_obj(scheme *sc, pointer args)
{
user_type obj = {
.free = my_free,
.data = strdup("testtest"),
.type = 1,
};
return mk_user_type(sc, obj);
}
#endif
/* -------- GAME CTX ------------- */
static void tinytest_load(void **data_p);
static void tinytest_init(void **data_p);
static void tinytest_update(void* data, float delta);
static void tinytest_draw(void *data);
static void tiny_mouse_motion(int x, int y, void *data);
static struct game_ctx ctx = {
.game_load = tinytest_load,
.game_init = tinytest_init,
.game_update = tinytest_update,
.game_draw = tinytest_draw,
.game_on_mouse_motion = tiny_mouse_motion,
};
ENGINE_MAIN(&ctx);
void scheme_init_api(scheme *sc);
static void tinytest_load(void **data_p)
{
scheme *sc = scheme_init_new();
scheme_init_api(sc);
scheme_set_output_port_file(sc, stdout);
*data_p = sc;
FILE *f = fopen("game.scm", "r");
scheme_load_named_file(sc, f, "game.scm");
fclose(f);
pointer width=scheme_easy_eval(sc, "game-width");
pointer height=scheme_easy_eval(sc, "game-height");
pointer scale=scheme_easy_eval(sc, "game-scale");
if (sc->vptr->is_number(width) &&
sc->vptr->is_number(height)) {
ctx.screen_width = sc->vptr->ivalue(width);
ctx.screen_height = sc->vptr->ivalue(height);
}
if (sc->vptr->is_number(scale)) {
ctx.screen_scale = sc->vptr->ivalue(scale);
}
}
static void tinytest_init(void **data_p)
{
scheme *sc = *data_p;
scheme_easy_call(sc, "game-init", NULL);
scheme_flush_sink(sc);
}
static void tinytest_update(void* data, float delta)
{
scheme *sc = data;
scheme_easy_call(sc, "game-update", mk_real(sc, delta), NULL);
scheme_flush_sink(sc);
}
static void tinytest_draw(void *data)
{
scheme *sc = data;
scheme_easy_call(sc, "game-draw", NULL);
scheme_flush_sink(sc);
}
static void tiny_mouse_motion(int x, int y, void *data)
{
scheme *sc = data;
scheme_easy_call(sc, "game-mouse-motion", mk_integer(sc, x), mk_integer(sc, y), NULL);
scheme_flush_sink(sc);
}
#if 0
int main()
{
scheme *sc = scheme_init_new();
//sc->vptr->scheme_define(sc, sc->global_env, sc->vptr->mk_symbol(sc, "c_function"), sc->vptr->mk_foreign_func(sc, &c_function));
sc->vptr->scheme_define(sc, sc->global_env, sc->vptr->mk_symbol(sc, "my_test"), sc->vptr->mk_foreign_func(sc, &s_my_test));
sc->gc_verbose = 1;
scheme_set_output_port_file(sc, stdout);
scheme_load_string(sc, "(define value 0) (define (count) (set! value (+ value 1)) (display value) (newline) value)");
while(1) {
scheme_easy_call(sc, "my_test", mk_integer(sc, 3), mk_string(sc, "bla"), NULL);
scheme_flush_sink(sc);
}
}
#endif