view mlk-client/main.cpp @ 212:e50f51702df4

client: add basic button prototype, closes #908 @1h
author David Demelier <markand@malikania.fr>
date Fri, 04 Jan 2019 18:26:04 +0100
parents ac99f440ee44
children 61580ff3138a
line wrap: on
line source

/*
 * main.cpp -- main client file
 *
 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <chrono>
#include <iostream>
#include <thread>

#include <malikania/client/color.hpp>
#include <malikania/client/button.hpp>
#include <malikania/client/window.hpp>
#include <malikania/client/painter.hpp>

using namespace std::chrono_literals;

int main(int argc, char** argv)
{
	mlk::client::window win(1920/2, 1080/2);
	mlk::client::painter painter(win);
	mlk::client::button button("click me");

	button.set_size({200, 64});
	button.set_position({50, 50});
	button.set_on_press([] {
		std::cout << "je suis clické" << std::endl;
	});

	for (;;) {
		while (auto ev = win.poll()) {
			if (std::get_if<mlk::client::quit_event>(&ev))
				goto end;
			else
				button.handle_event(ev);
		}

		painter.set_drawing_color(mlk::client::color::from_hex(0xffffffff));
		painter.clear();
		button.draw(painter);
		painter.present();

		std::this_thread::sleep_for(50ms);
	}

end:
	return 0;
}

#if 0

#include <cerrno>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>

#include <malikania/client/js/context.hpp>

// Simple javascript startup file. Don't delete.

int main(int argc, char** argv)
{
	-- argc;
	++ argv;

	if (argc == 0) {
		std::cerr << "usage: mlk-client main.js" << std::endl;
		return 1;
	}

	try {
		mlk::client::js::context ctx;
		std::ifstream input(argv[0]);
		std::string script(
			std::istreambuf_iterator<char>(input.rdbuf()),
			std::istreambuf_iterator<char>()
		);

		if (!input)
			throw std::runtime_error(std::strerror(errno));
		if (duk_peval_string(ctx, script.c_str()))
			throw mlk::js::duk::get_stack(ctx, -1);
	} catch (const mlk::js::duk::stack_info& ex) {
		std::cerr << argv[0] << ":" << ex.get_line_number() << ": " << ex.what() << std::endl;
		std::cerr << ex.get_stack() << std::endl;
		return 1;
	} catch (const std::exception& ex) {
		std::cerr << "abort: " << ex.what() << std::endl;
		return 1;
	}

	return 0;
}

#endif