comparison client/main.cpp @ 27:0a1adf7dcca0

Common: update libjs and adapt code
author David Demelier <markand@malikania.fr>
date Tue, 12 Apr 2016 13:53:11 +0200
parents 56cc058200b5
children 80736513d699
comparison
equal deleted inserted replaced
26:56cc058200b5 27:0a1adf7dcca0
23 #include <malikania/client-resources-loader.h> 23 #include <malikania/client-resources-loader.h>
24 #include <malikania/resources-locator.h> 24 #include <malikania/resources-locator.h>
25 25
26 #include <malikania/js-animation.h> 26 #include <malikania/js-animation.h>
27 #include <malikania/js-animator.h> 27 #include <malikania/js-animator.h>
28 #include <malikania/js-client.h>
29 #include <malikania/js-client-target.h>
28 #include <malikania/js-color.h> 30 #include <malikania/js-color.h>
29 #include <malikania/js-font.h> 31 #include <malikania/js-font.h>
30 #include <malikania/js-image.h> 32 #include <malikania/js-image.h>
31 #include <malikania/js-line.h> 33 #include <malikania/js-line.h>
32 #include <malikania/js-point.h> 34 #include <malikania/js-point.h>
62 loadMalikaniaPoint(ctx); 64 loadMalikaniaPoint(ctx);
63 loadMalikaniaRectangle(ctx); 65 loadMalikaniaRectangle(ctx);
64 loadMalikaniaSize(ctx); 66 loadMalikaniaSize(ctx);
65 loadMalikaniaSprite(ctx); 67 loadMalikaniaSprite(ctx);
66 loadMalikaniaWindow(ctx); 68 loadMalikaniaWindow(ctx);
69 loadMalikaniaClient(ctx);
70 loadMalikaniaClientTarget(ctx);
67 71
68 return ctx; 72 return ctx;
69 } 73 }
70 74
71 void start(duk::Context &ctx) 75 void start(duk::Context &ctx, std::shared_ptr<Client> client)
72 { 76 {
73 duk::getGlobal<void>(ctx, "start"); 77 duk::getGlobal<void>(ctx, "start");
74 78
75 if (duk::is<duk::Function>(ctx, -1)) { 79 if (duk::is<duk::Function>(ctx, -1)) {
76 duk::getGlobal<void>(ctx, "\xff""\xff""window"); 80 duk::push(ctx, std::move(client));
77 duk::pcall(ctx, 1); 81 duk::pcall(ctx, 1);
78 duk::pop(ctx); 82 duk::pop(ctx);
79 } else { 83 } else {
80 duk::pop(ctx); 84 duk::pop(ctx);
81 } 85 }
82 } 86 }
83 87
84 void update(duk::Context &ctx) 88 void update(duk::Context &ctx, std::shared_ptr<Client> client)
85 { 89 {
86 duk::getGlobal<void>(ctx, "update"); 90 duk::getGlobal<void>(ctx, "update");
87 91
88 if (duk::is<duk::Function>(ctx, -1)) { 92 if (duk::is<duk::Function>(ctx, -1)) {
89 duk::pcall(ctx, 0); 93 duk::push(ctx, std::move(client));
94 duk::pcall(ctx, 1);
90 duk::pop(ctx); 95 duk::pop(ctx);
91 } else { 96 } else {
92 duk::pop(ctx); 97 duk::pop(ctx);
98 client->update();
93 } 99 }
94 } 100 }
95 101
96 void draw(duk::Context &ctx) 102 void draw(duk::Context &ctx, std::shared_ptr<Client> client)
97 { 103 {
98 duk::getGlobal<void>(ctx, "draw"); 104 duk::getGlobal<void>(ctx, "draw");
99 105
100 if (duk::is<duk::Function>(ctx, -1)) { 106 if (duk::is<duk::Function>(ctx, -1)) {
101 duk::getGlobal<void>(ctx, "\xff""\xff""window"); 107 duk::push(ctx, std::move(client));
102 duk::pcall(ctx, 1); 108 duk::pcall(ctx, 1);
103 duk::pop(ctx); 109 duk::pop(ctx);
104 } else { 110 } else {
105 duk::pop(ctx); 111 duk::pop(ctx);
112 client->draw();
106 } 113 }
107 } 114 }
108 115
109 int run(duk::Context &ctx) 116 int run(duk::Context &ctx)
110 { 117 {
111 bool running = true; 118 auto running = true;
119 auto client = std::make_shared<Client>();
112 120
113 /* js-window use duk::Pointer at the moment so store it from there temporarily */ 121 client->setOnQuit([&] () {
114 duk::putGlobal(ctx, "\xff""\xff""window", duk::Pointer<Window>{new Window});
115
116 Window *window = duk::getGlobal<duk::Pointer<Window>>(ctx, "\xff""\xff""window");
117
118 window->setOnQuit([&] () {
119 running = false; 122 running = false;
120 }); 123 });
121 window->setOnKeyDown([&] (unsigned key) { 124 client->setOnKeyDown([&] (unsigned key) {
122 duk::getGlobal<void>(ctx, "keyDown"); 125 duk::getGlobal<void>(ctx, "keyDown");
123 126
124 if (duk::is<duk::Function>(ctx, -1)) { 127 if (duk::is<duk::Function>(ctx, -1)) {
125 duk::push(ctx, static_cast<int>(key)); 128 duk::push(ctx, static_cast<int>(key));
126 duk::pcall(ctx, 1); 129 duk::pcall(ctx, 1);
127 duk::pop(ctx); 130 duk::pop(ctx);
128 } else { 131 } else {
129 duk::pop(ctx); 132 duk::pop(ctx);
130 } 133 }
131 }); 134 });
132 window->setOnKeyDown([&] (unsigned key) { 135 client->setOnKeyDown([&] (unsigned key) {
133 duk::getGlobal<void>(ctx, "keyUp"); 136 duk::getGlobal<void>(ctx, "keyUp");
134 137
135 if (duk::is<duk::Function>(ctx, -1)) { 138 if (duk::is<duk::Function>(ctx, -1)) {
136 duk::push(ctx, static_cast<int>(key)); 139 duk::push(ctx, static_cast<int>(key));
137 duk::pcall(ctx, 1); 140 duk::pcall(ctx, 1);
139 } else { 142 } else {
140 duk::pop(ctx); 143 duk::pop(ctx);
141 } 144 }
142 }); 145 });
143 146
144 start(ctx); 147 start(ctx, client);
145 148
146 while (running) { 149 while (running) {
147 window->poll(); 150 client->poll();
148 151
149 update(ctx); 152 update(ctx, client);
150 draw(ctx); 153 draw(ctx, client);
151 154
152 // TODO: remove this with an appropriate FPS calculation. 155 // TODO: remove this with an appropriate FPS calculation.
153 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 156 std::this_thread::sleep_for(std::chrono::milliseconds(50));
154 } 157 }
155 158
163 166
164 /* Store the loader */ 167 /* Store the loader */
165 ResourcesLocatorDirectory locator(directory); 168 ResourcesLocatorDirectory locator(directory);
166 ClientResourcesLoader loader(locator); 169 ClientResourcesLoader loader(locator);
167 170
168 duk::putGlobal(ctx, "\xff""\xff""loader", duk::RawPointer<ClientResourcesLoader>{&loader}); 171 duk::putGlobal(ctx, "\xff""\xff""loader", &loader);
169 172
170 if (duk::pevalFile(ctx, path) != 0) { 173 if (duk::pevalFile(ctx, path) != 0) {
171 duk::ErrorInfo info = duk::error(ctx, -1); 174 duk::Exception info = duk::exception(ctx, -1);
172 175
173 std::cerr << info.fileName << ":" << info.lineNumber << ": " << info.stack << std::endl; 176 std::cerr << info.fileName << ":" << info.lineNumber << ": " << info.stack << std::endl;
174 177
175 return 1; 178 return 1;
176 } 179 }