comparison mlk-client/main.cpp @ 210:1599919b5de6

client: allow starting a Javascript file
author David Demelier <markand@malikania.fr>
date Fri, 07 Dec 2018 19:31:10 +0100
parents 263122adef77
children ac99f440ee44
comparison
equal deleted inserted replaced
209:b788b6a20eea 210:1599919b5de6
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <cerrno>
20 #include <cstring>
21 #include <fstream>
19 #include <iostream> 22 #include <iostream>
20 #include <chrono> 23 #include <iterator>
21 #include <thread>
22 24
23 #include <malikania/client/color.hpp> 25 #include <malikania/js/elapsed_timer_js_api.hpp>
24 #include <malikania/client/button.hpp> 26 #include <malikania/js/line_js_api.hpp>
25 #include <malikania/client/window.hpp> 27 #if 0
26 #include <malikania/client/sprite.hpp> 28 #include <malikania/js/loader_js_api.hpp>
27 #include <malikania/client/theme.hpp> 29 #endif
28 #include <malikania/client/painter.hpp> 30 #include <malikania/js/point_js_api.hpp>
31 #include <malikania/js/rectangle_js_api.hpp>
32 #include <malikania/js/size_js_api.hpp>
29 33
30 #include <assets/ui.hpp> 34 #include <malikania/client/js/animation_js_api.hpp>
35 #include <malikania/client/js/animator_js_api.hpp>
36 #include <malikania/client/js/color_js_api.hpp>
37 #include <malikania/client/js/font_js_api.hpp>
38 #include <malikania/client/js/image_js_api.hpp>
39 #if 0
40 #include <malikania/client/js/loader_js_api.hpp>
41 #endif
42 #include <malikania/client/js/painter_js_api.hpp>
43 #include <malikania/client/js/sprite_js_api.hpp>
44 #include <malikania/client/js/texture_js_api.hpp>
45 #include <malikania/client/js/window_js_api.hpp>
31 46
32 using namespace std::chrono_literals; 47 int main(int argc, char** argv)
48 {
49 -- argc;
50 ++ argv;
33 51
34 int main(int, char**) 52 if (argc == 0) {
35 { 53 std::cerr << "usage: mlk-client main.js" << std::endl;
36 mlk::client::window win(1920/2, 1080/2, "Hello"); 54 return 1;
37 mlk::client::image image(std::string(ui, sizeof (ui))); 55 }
38 mlk::client::sprite sprite(std::move(image), {16, 16});
39 mlk::client::painter painter(win);
40 56
41 for (;;) { 57 try {
42 while (auto ev = win.poll()) { 58 mlk::js::duk::context ctx;
43 if (std::holds_alternative<mlk::client::quit_event>(ev))
44 return 0;
45 }
46 59
47 painter.clear(); 60 mlk::js::load_elapsed_timer_api(ctx);
48 painter.set_drawing_color(mlk::client::color::from_hex(0xffffffff)); 61 mlk::js::load_line_api(ctx);
62 mlk::js::load_point_api(ctx);
63 mlk::js::load_rectangle_api(ctx);
64 mlk::js::load_size_api(ctx);
49 65
50 const auto width = 200; 66 mlk::client::js::load_animation_api(ctx);
51 const auto height = 150; 67 mlk::client::js::load_animator_api(ctx);
68 mlk::client::js::load_color_api(ctx);
69 mlk::client::js::load_image_api(ctx);
70 mlk::client::js::load_painter_api(ctx);
71 mlk::client::js::load_sprite_api(ctx);
72 mlk::client::js::load_texture_api(ctx);
73 mlk::client::js::load_window_api(ctx);
52 74
53 // top 75 std::ifstream input(argv[0]);
54 sprite.draw(painter, 0, mlk::point{10, 10}); 76 std::string script(
55 sprite.draw(painter, 1, mlk::rectangle{10 + 16, 10, width, 16}); 77 std::istreambuf_iterator<char>(input.rdbuf()),
56 sprite.draw(painter, 2, mlk::point{10 + 16 + width, 10}); 78 std::istreambuf_iterator<char>()
79 );
57 80
58 // middle 81 if (!input)
59 sprite.draw(painter, 32, mlk::rectangle{10, 10 + 16, 16, height}); 82 throw std::runtime_error(std::strerror(errno));
60 sprite.draw(painter, 33, mlk::rectangle{10 + 16, 10 + 16, width, height}); 83 if (duk_peval_string(ctx, script.c_str()))
61 sprite.draw(painter, 34, mlk::rectangle{10 + 16 + width, 10 + 16, 16, height}); 84 throw mlk::js::duk::get_stack(ctx, -1);
62 85 } catch (const mlk::js::duk::stack_info& ex) {
63 // bottom 86 std::cerr << argv[0] << ":" << ex.get_line_number() << ": " << ex.what() << std::endl;
64 sprite.draw(painter, 64, mlk::point{10, 10 + 16 + height}); 87 std::cerr << ex.get_stack() << std::endl;
65 sprite.draw(painter, 65, mlk::rectangle{10 + 16, 10 + 16 + height, width, 16}); 88 return 1;
66 sprite.draw(painter, 66, mlk::point{10 + 16 + width, 10 + 16 + height}); 89 } catch (const std::exception& ex) {
67 90 std::cerr << "abort: " << ex.what() << std::endl;
68 // input 91 return 1;
69 sprite.draw(painter, 3, mlk::point{10 + 8, 10 + 8});
70 sprite.draw(painter, 4, mlk::rectangle{10 + 8 + 16, 10 + 8, width - 16, 16});
71 sprite.draw(painter, 5, mlk::point{10 + 8 + width, 10 + 8});
72 sprite.draw(painter, 35, mlk::point{10 + 8, 10 + 8 + 16});
73 sprite.draw(painter, 36, mlk::rectangle{10 + 8 + 16, 10 + 8 + 16, width - 16, 16});
74 sprite.draw(painter, 37, mlk::point{10 + 8 + width, 10 + 8 + 16});
75
76 painter.present();
77 } 92 }
78 93
79 return 0; 94 return 0;
80 } 95 }