annotate examples/js-window/main.cpp @ 208:263122adef77

client: add texture and painter closes #966 @2h closes #967 @2h
author David Demelier <markand@malikania.fr>
date Wed, 05 Dec 2018 22:24:44 +0100
parents f28cb6d04731
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * main.cpp -- test Window (JavaScript binding)
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <chrono>
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
20 #include <iostream>
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include <thread>
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
22
189
f28cb6d04731 Misc: extreme refactoring
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
23 #include <malikania/client/js/window_js_api.hpp>
208
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
24 #include <malikania/client/js/painter_js_api.hpp>
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
25
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
26 using namespace std::chrono_literals;
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
27
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 using namespace mlk;
189
f28cb6d04731 Misc: extreme refactoring
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
29 using namespace mlk::client::js;
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 using namespace mlk::client;
189
f28cb6d04731 Misc: extreme refactoring
David Demelier <markand@malikania.fr>
parents: 188
diff changeset
31 using namespace mlk::js::duk;
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
32
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
33 void basic(duk_context* ctx)
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 {
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
35 const auto ret = duk_peval_string(ctx,
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 "w = new Malikania.Window();"
208
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
37 "p = new Malikania.Painter(w);"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
38 "p.setDrawingColor('lightskyblue');"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
39 "p.clear();"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
40 "p.present();"
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 );
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
42
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
43 if (ret != 0)
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
44 throw get_stack(ctx, -1);
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
45
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 std::this_thread::sleep_for(3s);
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 }
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
48
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
49 void rect(duk_context* ctx)
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 {
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
51 const auto ret = duk_peval_string(ctx,
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 "w = new Malikania.Window();"
208
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
53 "p = new Malikania.Painter(w);"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
54 "p.setDrawingColor('lightskyblue');"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
55 "p.clear();"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
56 "p.setDrawingColor('white');"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
57 "p.drawRectangle({ x: 10, y: 10, width: 10, height: 10 });"
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
58 "p.present();"
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 );
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
60
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
61 if (ret != 0)
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
62 throw get_stack(ctx, -1);
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
63
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
64 std::this_thread::sleep_for(3s);
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 }
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
66
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 int main()
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 {
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 try {
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
70 context ctx;
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
71
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
72 duk_push_object(ctx);
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 duk_put_global_string(ctx, "Malikania");
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
74
188
0cecdadfb5c4 Misc: rework javascript bindings, closes #916
David Demelier <markand@malikania.fr>
parents: 182
diff changeset
75 load_window_api(ctx);
208
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
76 load_painter_api(ctx);
263122adef77 client: add texture and painter
David Demelier <markand@malikania.fr>
parents: 189
diff changeset
77
182
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 basic(ctx);
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 rect(ctx);
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
80 } catch (const std::exception& ex) {
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 std::cerr << ex.what() << std::endl;
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 return 1;
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 }
3107ce017c3a Misc: switch back to SDL
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 }