Mercurial > malikania
view examples/font/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 | 0a285d62ace7 |
children |
line wrap: on
line source
/* * main.cpp -- test Font * * 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/loader.hpp> #include <malikania/client/color.hpp> #include <malikania/client/font.hpp> #include <malikania/client/painter.hpp> #include <malikania/client/window.hpp> #include <malikania/locator.hpp> #include <malikania/point.hpp> #include <malikania/size.hpp> using namespace std::chrono_literals; void topleft(mlk::client::painter& painter, mlk::client::font& font) { painter.set_drawing_color(mlk::client::color::from_name("black")); painter.clear(); painter.set_drawing_color(mlk::client::color::from_name("white")); font.draw(painter, "top left", {10, 10}); painter.present(); std::this_thread::sleep_for(1s); } void topright(mlk::client::painter& painter, mlk::client::font& font) { auto dim = font.clip("top right"); painter.set_drawing_color(mlk::client::color::from_name("black")); painter.clear(); painter.set_drawing_color(mlk::client::color::from_name("white")); font.draw(painter, "top right", { static_cast<int>(400 - dim.width - 10), static_cast<int>(10) }); painter.present(); std::this_thread::sleep_for(1s); } void bottomleft(mlk::client::painter& painter, mlk::client::font& font) { auto dim = font.clip("bottom left"); painter.set_drawing_color(mlk::client::color::from_name("black")); painter.clear(); painter.set_drawing_color(mlk::client::color::from_name("white")); font.draw(painter, "bottom left", { static_cast<int>(10), static_cast<int>(400 - dim.height - 10) }); painter.present(); std::this_thread::sleep_for(1s); } void bottomright(mlk::client::painter& painter, mlk::client::font& font) { auto dim = font.clip("bottom right"); painter.set_drawing_color(mlk::client::color::from_name("black")); painter.clear(); painter.set_drawing_color(mlk::client::color::from_name("white")); font.draw(painter, "bottom right", { static_cast<int>(400 - dim.width - 10), static_cast<int>(400 - dim.height - 10) }); painter.present(); std::this_thread::sleep_for(1s); } void center(mlk::client::painter& painter, mlk::client::font& font) { auto dim = font.clip("center"); painter.set_drawing_color(mlk::client::color::from_name("black")); painter.clear(); painter.set_drawing_color(mlk::client::color::from_name("white")); font.draw(painter, "center", { static_cast<int>(200 - (dim.width / 2)), static_cast<int>(200 - (dim.height -2)) }); painter.present(); std::this_thread::sleep_for(1s); } void center2(mlk::client::painter& painter, mlk::client::font& font) { auto dim = font.clip("The world is Malikania."); painter.set_drawing_color(mlk::client::color::from_name("black")); painter.clear(); painter.set_drawing_color(mlk::client::color::from_name("white")); font.draw(painter, "The world is Malikania.", { static_cast<int>(200 - (dim.width / 2)), static_cast<int>(200 - (dim.height -2)) }); painter.present(); std::this_thread::sleep_for(3s); } int main(int, char**) { try { mlk::directory_locator locator(CMAKE_CURRENT_SOURCE_DIR "/resources"); mlk::client::window window(400, 400); mlk::client::painter painter(window); mlk::client::loader loader(locator); mlk::client::font font = loader.load_font("DejaVuSans.ttf", 10); topleft(painter, font); topright(painter, font); bottomleft(painter, font); bottomright(painter, font); center(painter, font); center2(painter, font); } catch (const std::exception& ex) { std::cerr << ex.what() << std::endl; return 1; } return 0; }