view libclient/malikania/client/sprite.cpp @ 98:f4d23ad4aa27

Common: refactoring class mlk::size 1. Make uniform size representation in JSON format: { "width": 100, "height": 200 } The Javascript API was using this form while an array was used in loaders. 2. Create brand new functions in util::json to parse mlk::size objects, Updated util::json::require_(u)int as well. 3. Add new tests for mlk::size object, 4. Get rid of utilities in loader as they are in util::json instead.
author David Demelier <markand@malikania.fr>
date Tue, 04 Jul 2017 13:23:15 +0200
parents ee850a6ab89e
children 119bcc5a727e
line wrap: on
line source

/*
 * sprite.cpp -- image sprite
 *
 * Copyright (c) 2013-2017 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.
 */

#include <cassert>

#include "sprite.hpp"

namespace mlk {

namespace client {

sprite::sprite(mlk::client::image image,
               mlk::size cell,
               mlk::size size,
               mlk::size space,
               mlk::size margin) noexcept
    : m_image(std::move(image))
    , m_cell(std::move(cell))
    , m_margin(std::move(margin))
    , m_space(std::move(space))
    , m_size(std::move(size))
{
    assert(m_cell.width() > 0);
    assert(m_cell.height() > 0);

    // If size is not specified, take from image.
    if (m_size.is_null()) {
        m_size = m_image.size();
    }

    // Compute number of cells.
    m_rows = (m_size.height() - (margin.height() * 2) + m_space.height()) / (m_cell.height() + m_space.height());
    m_columns = (m_size.width() - (m_margin.width() * 2) + m_space.width()) / (m_cell.width() + m_space.width());
}

void sprite::draw(window& window, unsigned cell, const point& point)
{
    assert(cell < m_rows * m_columns);

    // Compute index in the grid.
    unsigned hindex = (cell % m_columns);
    unsigned vindex = (cell / m_columns);

    // Compute the pixel boundaries.
    int x = m_margin.width() + (hindex * m_space.width()) + (hindex * m_cell.width());
    int y = m_margin.height() + (vindex * m_space.height()) + (vindex * m_cell.height());

    rectangle source(x, y, m_cell.width(), m_cell.height());
    rectangle target(point.x(), point.y(), m_cell.width(), m_cell.height());

    m_image.draw(window, source, target);
}

} // !client

} // !mlk