Mercurial > molko
comparison src/mlk-run/main.c @ 339:979960e65f76
js: add font bindings
While here, correct int to long in font_render.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 16 Oct 2021 09:01:20 +0200 |
parents | 94828af916bb |
children | 0c18acf4517e |
comparison
equal
deleted
inserted
replaced
338:94828af916bb | 339:979960e65f76 |
---|---|
25 #include <core/vfs-directory.h> | 25 #include <core/vfs-directory.h> |
26 #include <core/vfs-zip.h> | 26 #include <core/vfs-zip.h> |
27 #include <core/vfs.h> | 27 #include <core/vfs.h> |
28 | 28 |
29 #include <core/js-clock.h> | 29 #include <core/js-clock.h> |
30 #include <core/js-core.h> | |
30 #include <core/js-event.h> | 31 #include <core/js-event.h> |
32 #include <core/js-font.h> | |
31 #include <core/js-painter.h> | 33 #include <core/js-painter.h> |
32 #include <core/js-texture.h> | 34 #include <core/js-texture.h> |
33 #include <core/js-window.h> | 35 #include <core/js-window.h> |
34 | 36 |
35 /* VFS loader to support zip and directories when loading game. */ | 37 /* VFS loader to support zip and directories when loading game. */ |
48 | 50 |
49 static void | 51 static void |
50 core_bind(duk_context *ctx) | 52 core_bind(duk_context *ctx) |
51 { | 53 { |
52 js_clock_bind(ctx); | 54 js_clock_bind(ctx); |
55 js_core_bind(ctx, &vfs); | |
56 js_font_bind(ctx); | |
53 js_event_bind(ctx); | 57 js_event_bind(ctx); |
54 js_painter_bind(ctx); | 58 js_painter_bind(ctx); |
55 js_texture_bind(ctx); | 59 js_texture_bind(ctx); |
56 js_window_bind(ctx); | 60 js_window_bind(ctx); |
57 } | 61 } |
72 duk_push_c_function(ctx, print, 1); | 76 duk_push_c_function(ctx, print, 1); |
73 duk_put_prop_string(ctx, -2, "print"); | 77 duk_put_prop_string(ctx, -2, "print"); |
74 duk_pop(ctx); | 78 duk_pop(ctx); |
75 } | 79 } |
76 | 80 |
77 static char * | |
78 extract(struct vfs_file *file) | |
79 { | |
80 FILE *fp; | |
81 char *out, buf[BUFSIZ]; | |
82 size_t len, nr; | |
83 | |
84 if (!(fp = open_memstream(&out, &len))) | |
85 panic(); | |
86 | |
87 while ((nr = vfs_file_read(file, buf, sizeof (buf))) > 0) | |
88 if (fwrite(buf, 1, nr, fp) <= 0) | |
89 panic(); | |
90 | |
91 fclose(fp); | |
92 | |
93 return out; | |
94 } | |
95 | |
96 static void | 81 static void |
97 startup(void) | 82 startup(void) |
98 { | 83 { |
99 struct vfs_file main; | 84 struct vfs_file main; |
100 char *code; | 85 char *code; |
101 | 86 |
102 if (vfs_open(&vfs, &main, "main.js", "r") < 0) | 87 if (vfs_open(&vfs, &main, "main.js", "r") < 0) |
103 panic(); | 88 panic(); |
89 if (!(code = vfs_file_aread(&main, NULL))) | |
90 panic(); | |
104 | 91 |
105 code = extract(&main); | |
106 vfs_file_finish(&main); | 92 vfs_file_finish(&main); |
107 | 93 |
108 if (duk_peval_string(ctx, code)) | 94 if (duk_peval_string(ctx, code)) |
109 panicf("%s", duk_safe_to_string(ctx, -1)); | 95 panicf("%s", duk_safe_to_string(ctx, -1)); |
110 | 96 |