comparison client/main.cpp @ 42:a47a4477f347

Misc: new style, closes #578
author David Demelier <markand@malikania.fr>
date Tue, 29 Nov 2016 21:21:36 +0100
parents 9af360f34c7d
children 4bc4732fa1dd
comparison
equal deleted inserted replaced
41:3645200f46bf 42:a47a4477f347
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 <chrono> 19 int main()
20 #include <iostream>
21 #include <thread>
22
23 #include <malikania/client-resources-loader.hpp>
24 #include <malikania/resources-locator.hpp>
25
26 #include <malikania/js-animation.hpp>
27 #include <malikania/js-animator.hpp>
28 #include <malikania/js-client.hpp>
29 #include <malikania/js-client-target.hpp>
30 #include <malikania/js-color.hpp>
31 #include <malikania/js-font.hpp>
32 #include <malikania/js-image.hpp>
33 #include <malikania/js-line.hpp>
34 #include <malikania/js-point.hpp>
35 #include <malikania/js-rectangle.hpp>
36 #include <malikania/js-size.hpp>
37 #include <malikania/js-sprite.hpp>
38 #include <malikania/js-window.hpp>
39
40 #if 0
41
42 using namespace malikania;
43
44 namespace {
45
46 int usage()
47 { 20 {
48 std::cerr << "usage: mlk-client directory\n";
49
50 return 1;
51 } 21 }
52 22
53 duk::Context init()
54 {
55 duk::Context ctx;
56
57 /* TODO: Put Malikania global somewhere else */
58 duk::putGlobal(ctx, "Malikania", duk::Object());
59
60 loadMalikaniaAnimation(ctx);
61 loadMalikaniaAnimator(ctx);
62 loadMalikaniaColor(ctx);
63 loadMalikaniaFont(ctx);
64 loadMalikaniaImage(ctx);
65 loadMalikaniaLine(ctx);
66 loadMalikaniaPoint(ctx);
67 loadMalikaniaRectangle(ctx);
68 loadMalikaniaSize(ctx);
69 loadMalikaniaSprite(ctx);
70 loadMalikaniaWindow(ctx);
71 loadMalikaniaClient(ctx);
72 loadMalikaniaClientTarget(ctx);
73
74 return ctx;
75 }
76
77 void start(duk::Context &ctx, std::shared_ptr<Client> client)
78 {
79 duk::getGlobal<void>(ctx, "start");
80
81 if (duk::is<duk::Function>(ctx, -1)) {
82 duk::push(ctx, std::move(client));
83 duk::pcall(ctx, 1);
84 duk::pop(ctx);
85 } else {
86 duk::pop(ctx);
87 }
88 }
89
90 void update(duk::Context &ctx, std::shared_ptr<Client> client)
91 {
92 duk::getGlobal<void>(ctx, "update");
93
94 if (duk::is<duk::Function>(ctx, -1)) {
95 duk::push(ctx, std::move(client));
96 duk::pcall(ctx, 1);
97 duk::pop(ctx);
98 } else {
99 duk::pop(ctx);
100 client->update();
101 }
102 }
103
104 void draw(duk::Context &ctx, std::shared_ptr<Client> client)
105 {
106 duk::getGlobal<void>(ctx, "draw");
107
108 if (duk::is<duk::Function>(ctx, -1)) {
109 duk::push(ctx, std::move(client));
110 duk::pcall(ctx, 1);
111 duk::pop(ctx);
112 } else {
113 duk::pop(ctx);
114 client->draw();
115 }
116 }
117
118 int run(duk::Context &ctx)
119 {
120 auto running = true;
121 auto client = std::make_shared<Client>();
122
123 client->setOnQuit([&] () {
124 running = false;
125 });
126 client->setOnKeyDown([&] (unsigned key) {
127 duk::getGlobal<void>(ctx, "keyDown");
128
129 if (duk::is<duk::Function>(ctx, -1)) {
130 duk::push(ctx, static_cast<int>(key));
131 duk::pcall(ctx, 1);
132 duk::pop(ctx);
133 } else {
134 duk::pop(ctx);
135 }
136 });
137 client->setOnKeyDown([&] (unsigned key) {
138 duk::getGlobal<void>(ctx, "keyUp");
139
140 if (duk::is<duk::Function>(ctx, -1)) {
141 duk::push(ctx, static_cast<int>(key));
142 duk::pcall(ctx, 1);
143 duk::pop(ctx);
144 } else {
145 duk::pop(ctx);
146 }
147 });
148
149 start(ctx, client);
150
151 while (running) {
152 client->poll();
153
154 update(ctx, client);
155 draw(ctx, client);
156
157 // TODO: remove this with an appropriate FPS calculation.
158 std::this_thread::sleep_for(std::chrono::milliseconds(50));
159 }
160
161 return 0;
162 }
163
164 int boot(const std::string &directory)
165 {
166 std::string path = directory + "/client.js";
167 duk::Context ctx = init();
168
169 /* Store the loader */
170 ResourcesLocatorDirectory locator(directory);
171 ClientResourcesLoader loader(locator);
172
173 duk::putGlobal(ctx, "\xff""\xff""loader", &loader);
174
175 if (duk::pevalFile(ctx, path) != 0) {
176 duk::Exception info = duk::exception(ctx, -1);
177
178 std::cerr << info.fileName << ":" << info.lineNumber << ": " << info.stack << std::endl;
179
180 return 1;
181 }
182
183 return run(ctx);
184 }
185
186 } // !namespace
187
188 int main(int argc, char **argv)
189 {
190 #if 0
191 -- argc;
192 ++ argv;
193
194 if (argc < 1) {
195 return usage();
196 }
197
198 return boot(argv[0]);
199 #endif
200 Client client;
201 ElapsedTimer timer;
202
203 while (timer.elapsed() < 5000) {
204 client.poll();
205 client.update();
206 client.draw();
207
208 std::this_thread::sleep_for(std::chrono::milliseconds(50));
209 }
210
211 return 0;
212 }
213
214 #endif
215
216 int main() {}
217