File: Game.cpp

package info (click to toggle)
clanlib 1.0~svn3827-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,600 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (110 lines) | stat: -rw-r--r-- 3,142 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "Game.h"
#include "SpriteManager.h"

#include <ClanLib/display.h>

//--------------------------------------------------------------------------
Game::Game(int width, int height, SpriteManager const & sprites)
	: puzzle(width, height)
	, sprites(sprites)
	, position_calc(puzzle, sprites.HandleSize())
{
	on_key_down = CL_Mouse::sig_key_down().connect(this, &Game::OnKeyDown);
}

//--------------------------------------------------------------------------
Game::Game(Game const & copy)
	: puzzle(copy.puzzle)
	, sprites(copy.sprites)
	, position_calc(copy.position_calc)
{
	on_key_down = CL_Mouse::sig_key_down().connect(this, &Game::OnKeyDown);
}

//--------------------------------------------------------------------------
Game & Game::operator=(Game const & copy)
{
	if (this == &copy)
		return *this;

	puzzle = copy.puzzle;
	sprites = copy.sprites;
	position_calc = copy.position_calc;
	
	return *this;
}

//--------------------------------------------------------------------------
Game::~Game()
{
}

//--------------------------------------------------------------------------
void Game::Draw(DrawStatus draw_status)
{
	DrawPuzzle(draw_status);
	DrawMouse(CL_Point(CL_Mouse::get_x(), CL_Mouse::get_y()));
}

//--------------------------------------------------------------------------
void Game::DrawPuzzle(DrawStatus draw_status)
{
	// draw main field
	for(int y = 0; y < puzzle.Height(); ++y)
		for(int x = 0; x < puzzle.Width(); ++x)
		{
			CL_Sprite & handle = puzzle(x, y) ? sprites(SpriteManager::HANDLE_ON) : sprites(SpriteManager::HANDLE_OFF);
			CL_Point p(position_calc.FieldToScreen(CL_Point(x, y)));
			handle.draw(p.x, p.y);
		}

	if (draw_status == SHOW_HINTS)
	{
		// draw hints
		Puzzle::HintVectorType const & hints = puzzle.Hints();
		for(int i = 0; i < (int)hints.size(); ++i)
		{
			int x = hints[i].first;
			int y = hints[i].second;

			CL_Sprite & handle = puzzle(x, y) ? sprites(SpriteManager::HANDLE_ON_HINT) : sprites(SpriteManager::HANDLE_OFF_HINT);
			CL_Point p(position_calc.FieldToScreen(CL_Point(x, y)));
			handle.draw(p.x, p.y);
		}
	}
}

//--------------------------------------------------------------------------
void Game::DrawMouse(CL_Point const & coord)
{
	CL_Point bad_point(-1, -1);
	CL_Point field(position_calc.ScreenToField(coord));
	if (field != bad_point)
	{
		CL_Sprite & handle = puzzle(field.x, field.y) ? sprites(SpriteManager::HANDLE_ON_HIGHLIGHT) : sprites(SpriteManager::HANDLE_OFF_HIGHLIGHT);
		CL_Point p(position_calc.FieldToScreen(field));
		handle.draw(p.x, p.y);
	}
}

//--------------------------------------------------------------------------
void Game::OnKeyDown(CL_InputEvent const & key)
{
	if (key.id == CL_MOUSE_LEFT)
		MouseClick(key.mouse_pos);
}

//--------------------------------------------------------------------------
void Game::MouseClick(CL_Point const & coord)
{
	CL_Point bad_point(-1, -1);
	CL_Point field(position_calc.ScreenToField(coord));
	if (field != bad_point)
		puzzle.FlipAt(field.x, field.y);
}

//--------------------------------------------------------------------------
bool Game::IsSolved() const
{
	return puzzle.IsSolved();
}