view libclient/malikania/window.hpp @ 43:fabbe1759cec

Misc: switch to mlk namespace, closes #589
author David Demelier <markand@malikania.fr>
date Tue, 29 Nov 2016 22:25:17 +0100
parents a47a4477f347
children 4bc4732fa1dd
line wrap: on
line source

/*
 * window.hpp -- main window and basic drawing
 *
 * Copyright (c) 2013-2016 Malikania Authors
 *
 * 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.
 */

#ifndef MALIKANIA_WINDOW_HPP
#define MALIKANIA_WINDOW_HPP

/**
 * \file window.hpp
 * \brief Window and drawing.
 */

#include <functional>
#include <memory>
#include <vector>
#include <string>

namespace mlk {

class color;
class line;
class font;
class point;
class rectangle;

/**
 * \brief Main window class and drawing.
 */
class window {
private:
    class backend_impl;

    std::unique_ptr<backend_impl> m_backend;

    bool m_is_open{true};

public:
    /**
     * Create a window.
     *
     * \param width the initial width
     * \param height the initial height
     * \param title the optional title
     * \throw std::runtime_error on errors
     */
    window(unsigned width = 640, unsigned height = 480, const std::string& title = "Malikania Engine");

    /**
     * Move constructor defaulted.
     */
    window(window&&) noexcept;

    /**
     * Virtual destructor defaulted.
     */
    virtual ~window() noexcept;

    /**
     * Tells if the window is open.
     *
     * \return true if open
     */
    inline bool is_open() const noexcept
    {
        return m_is_open;
    }

    /**
     * Get the underlying backend.
     *
     * \return the backend
     */
    inline const backend_impl& backend() const noexcept
    {
        return *m_backend;
    }

    /**
     * Overloaded function.
     *
     * \return the backend
     */
    inline backend_impl& backend() noexcept
    {
        return *m_backend;
    }

    /**
     * Poll pending events.
     */
    void poll();

    /**
     * Clear the window content with the current drawing color.
     */
    void clear();

    /**
     * Render the content of the window to the screen.
     */
    void present();

    /**
     * Close the window.
     */
    void close() noexcept;

    /**
     * Get the current drawing color.
     *
     * \return the color
     */
    color drawing_color() const;

    /**
     * Set the drawing color.
     *
     * \param color the color
     */
    void set_drawing_color(const color& color);

    /**
     * Draw a line.
     *
     * \param line the line
     */
    void draw_line(const line& line);

    /**
     * Draw a several lines.
     *
     * \param the lines vertices
     */
    void draw_lines(const std::vector<point>& points);

    /**
     * Draw a point.
     *
     * \param point the point
     */
    void draw_point(const point& point);

    /**
     * Draw a list of points.
     *
     * \param points the points
     */
    void draw_points(const std::vector<point>& points);

    /**
     * Draw a rectangle (only borders).
     *
     * \param rect the rectangle
     * \see fillRectangle
     */
    void draw_rectangle(const rectangle& rect);

    /**
     * Draw a list of rectangles.
     *
     * \param rects the rectangles
     * \see fillRectangles
     */
    void draw_rectangles(const std::vector<rectangle>& rects);

    /**
     * Fill the given rectangle with the current color.
     *
     * \param rect the rectangle
     * \see drawRectangle
     */
    void fill_rectangle(const rectangle& rect);

    /**
     * Fill the list of rectangles with the current color.
     *
     * \param rects the list of rectangles
     * \see drawRectangles
     */
    void fill_rectangles(const std::vector<rectangle>& rects);

    /**
     * Draw some text.
     *
     * This function may stretch the text texture.
     *
     * \param text the text (UTF-8)
     * \param font the font
     * \param rectangle the rectangle target
     */
    void draw_text(const std::string& text, font& font, const rectangle& rectangle);

    /**
     * Overloaded function.
     *
     * Draw the text at the given position.
     *
     * \param text the text (UTF-8)
     * \param font the font
     * \param point the text position
     */
    void draw_text(const std::string& text, font& font, const point& point);

    /**
     * Move assigment operator defaulted.
     *
     * \return this
     */
    window& operator=(window&&) noexcept;
};

} // !mlk

#endif // !MALIKANIA_WINDOW_HPP