1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include "PositionCalculator.h"
#include "Puzzle.h"
#include <ClanLib/display.h>
//--------------------------------------------------------------------------
PositionCalculator::PositionCalculator(Puzzle const & puzzle, CL_Size const & sprite_size, int step)
: sprite_size(sprite_size)
, step(step)
, field_size(
sprite_size.width * puzzle.Width() + step * (puzzle.Width() - 1),
sprite_size.height * puzzle.Height() + step * (puzzle.Height() - 1))
, left_top(
(CL_Display::get_width() - field_size.width) / 2,
(CL_Display::get_height() - field_size.height) / 2)
{
}
//--------------------------------------------------------------------------
PositionCalculator::~PositionCalculator()
{
}
//--------------------------------------------------------------------------
CL_Point PositionCalculator::ScreenToField(CL_Point const & screen_coord) const
{
CL_Point bad_point(-1, -1);
if ( screen_coord.x < left_top.x
|| screen_coord.x > (left_top.x + field_size.width)
|| screen_coord.y < left_top.y
|| screen_coord.y > (left_top.y + field_size.height) )
{
return bad_point;
}
int tmp_x = (screen_coord.x - left_top.x) % (sprite_size.width + step);
int tmp_y = (screen_coord.y - left_top.y) % (sprite_size.height + step);
if (tmp_x < sprite_size.width && tmp_y < sprite_size.height)
{
return CL_Point(
(screen_coord.x - left_top.x) / (sprite_size.width + step),
(screen_coord.y - left_top.y) / (sprite_size.height + step));
}
return bad_point;
}
//--------------------------------------------------------------------------
CL_Point PositionCalculator::FieldToScreen(CL_Point const & field_coord) const
{
return CL_Point(
left_top.x + field_coord.x * (sprite_size.width + step),
left_top.y + field_coord.y * (sprite_size.height + step));
}
|