File: PositionCalculator.cpp

package info (click to toggle)
clanlib 1.0~svn3827-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,632 kB
  • ctags: 16,580
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (56 lines) | stat: -rw-r--r-- 1,784 bytes parent folder | download | duplicates (7)
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));
}