diff 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
line wrap: on
line diff
--- a/client/main.cpp	Fri Apr 08 14:16:47 2016 +0200
+++ b/client/main.cpp	Tue Apr 12 13:53:11 2016 +0200
@@ -25,6 +25,8 @@
 
 #include <malikania/js-animation.h>
 #include <malikania/js-animator.h>
+#include <malikania/js-client.h>
+#include <malikania/js-client-target.h>
 #include <malikania/js-color.h>
 #include <malikania/js-font.h>
 #include <malikania/js-image.h>
@@ -64,16 +66,18 @@
 	loadMalikaniaSize(ctx);
 	loadMalikaniaSprite(ctx);
 	loadMalikaniaWindow(ctx);
+	loadMalikaniaClient(ctx);
+	loadMalikaniaClientTarget(ctx);
 
 	return ctx;
 }
 
-void start(duk::Context &ctx)
+void start(duk::Context &ctx, std::shared_ptr<Client> client)
 {
 	duk::getGlobal<void>(ctx, "start");
 
 	if (duk::is<duk::Function>(ctx, -1)) {
-		duk::getGlobal<void>(ctx, "\xff""\xff""window");
+		duk::push(ctx, std::move(client));
 		duk::pcall(ctx, 1);
 		duk::pop(ctx);
 	} else {
@@ -81,44 +85,43 @@
 	}
 }
 
-void update(duk::Context &ctx)
+void update(duk::Context &ctx, std::shared_ptr<Client> client)
 {
 	duk::getGlobal<void>(ctx, "update");
 
 	if (duk::is<duk::Function>(ctx, -1)) {
-		duk::pcall(ctx, 0);
+		duk::push(ctx, std::move(client));
+		duk::pcall(ctx, 1);
 		duk::pop(ctx);
 	} else {
 		duk::pop(ctx);
+		client->update();
 	}
 }
 
-void draw(duk::Context &ctx)
+void draw(duk::Context &ctx, std::shared_ptr<Client> client)
 {
 	duk::getGlobal<void>(ctx, "draw");
 
 	if (duk::is<duk::Function>(ctx, -1)) {
-		duk::getGlobal<void>(ctx, "\xff""\xff""window");
+		duk::push(ctx, std::move(client));
 		duk::pcall(ctx, 1);
 		duk::pop(ctx);
 	} else {
 		duk::pop(ctx);
+		client->draw();
 	}
 }
 
 int run(duk::Context &ctx)
 {
-	bool running = true;
+	auto running = true;
+	auto client = std::make_shared<Client>();
 
-	/* js-window use duk::Pointer at the moment so store it from there temporarily */
-	duk::putGlobal(ctx, "\xff""\xff""window", duk::Pointer<Window>{new Window});
-
-	Window *window = duk::getGlobal<duk::Pointer<Window>>(ctx, "\xff""\xff""window");
-
-	window->setOnQuit([&] () {
+	client->setOnQuit([&] () {
 		running = false;
 	});
-	window->setOnKeyDown([&] (unsigned key) {
+	client->setOnKeyDown([&] (unsigned key) {
 		duk::getGlobal<void>(ctx, "keyDown");
 
 		if (duk::is<duk::Function>(ctx, -1)) {
@@ -129,7 +132,7 @@
 			duk::pop(ctx);
 		}
 	});
-	window->setOnKeyDown([&] (unsigned key) {
+	client->setOnKeyDown([&] (unsigned key) {
 		duk::getGlobal<void>(ctx, "keyUp");
 
 		if (duk::is<duk::Function>(ctx, -1)) {
@@ -141,13 +144,13 @@
 		}
 	});
 
-	start(ctx);
+	start(ctx, client);
 
 	while (running) {
-		window->poll();
+		client->poll();
 
-		update(ctx);
-		draw(ctx);
+		update(ctx, client);
+		draw(ctx, client);
 
 		// TODO: remove this with an appropriate FPS calculation.
 		std::this_thread::sleep_for(std::chrono::milliseconds(50));
@@ -165,10 +168,10 @@
 	ResourcesLocatorDirectory locator(directory);
 	ClientResourcesLoader loader(locator);
 
-	duk::putGlobal(ctx, "\xff""\xff""loader", duk::RawPointer<ClientResourcesLoader>{&loader});
+	duk::putGlobal(ctx, "\xff""\xff""loader", &loader);
 
 	if (duk::pevalFile(ctx, path) != 0) {
-		duk::ErrorInfo info = duk::error(ctx, -1);
+		duk::Exception info = duk::exception(ctx, -1);
 
 		std::cerr << info.fileName << ":" << info.lineNumber << ": " << info.stack << std::endl;