-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurseur.cpp
172 lines (153 loc) · 3.52 KB
/
curseur.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "curseur.h"
Curseur::Curseur(Image &image) : m_image(image), m_x(0), m_y(0), m_couleur(ROUGE)
{
}
bool Curseur::seek(unsigned int x, unsigned int y, Couleur couleur)
{
if(exist(x, y))
{
m_x = x;
m_y = y;
m_couleur = couleur;
return true;
}
else
{
return false;
}
}
void Curseur::getPosition(unsigned int *x, unsigned int *y, Couleur *couleur) const
{
*x = m_x;
*y = m_y;
if(couleur)
*couleur = m_couleur;
}
void Curseur::readPixel(unsigned char &rouge, unsigned char &vert, unsigned char &bleu, unsigned char &alpha)
{
rouge = m_image.get(m_x, m_y, ROUGE);
vert = m_image.get(m_x, m_y, VERT);
bleu = m_image.get(m_x, m_y, BLEU);
alpha = m_image.get(m_x, m_y, ALPHA);
}
void Curseur::setPixel(unsigned char rouge, unsigned char vert, unsigned char bleu, unsigned char alpha)
{
m_image.set(m_x, m_y, ROUGE, rouge);
m_image.set(m_x, m_y, VERT, vert);
m_image.set(m_x, m_y, BLEU, bleu);
m_image.set(m_x, m_y, ALPHA, alpha);
}
bool Curseur::atEnd() const
{
return (m_x == m_image.getWidth() && m_y == m_image.getHeight() && m_couleur == BLEU);
}
bool Curseur::exist(unsigned int x, unsigned int y) const
{
return (x < m_image.getWidth() && y < m_image.getHeight());
}
bool Curseur::moveToNextPixel(Couleur couleur)
{
unsigned int nextX = m_x, nextY = m_y;
if(m_x+1 < m_image.getWidth())
{
nextX = m_x+1;
}
else
{
if(m_y+1 < m_image.getHeight())
{
nextY = m_y+1;
nextX = 0;
}
else
{
return false;
}
}
if(exist(nextX, nextY))
{
m_y = nextY;
m_x = nextX;
m_couleur = couleur;
return true;
}
else
{
return false;
}
}
bool Curseur::moveToNextColor(bool considererAlpha)
{
unsigned int nextX = m_x, nextY = m_y;
Couleur prochaineCouleur = nextColor(m_couleur, considererAlpha);
if( int(prochaineCouleur) < int(m_couleur) )
{
if(m_x+1 < m_image.getWidth())
{
nextX = m_x+1;
}
else
{
if(m_y+1 < m_image.getHeight())
{
nextY = m_y+1;
nextX = 0;
}
else
{
return false;
}
}
}
/*Couleur prochaineCouleur;
if( (considererAlpha && m_couleur < ALPHA) || ( !considererAlpha && m_couleur < BLEU))
{
prochaineCouleur = nextColor(m_couleur);
}
else
{
if(m_x+1 < m_image.getWidth())
{
nextX = m_x+1;
}
else
{
if(m_y+1 < m_image.getHeight())
{
nextY = m_y+1;
nextX = 0;
}
else
{
return false;
}
}
}*/
if(exist(nextX, nextY))
{
m_y = nextY;
m_x = nextX;
m_couleur = prochaineCouleur;
return true;
}
else
{
return false;
}
}
Couleur Curseur::nextColor(Couleur c, bool considererAlpha)
{
switch(c)
{
case ROUGE : c = VERT; break;
case VERT : c = BLEU; break;
case BLEU :
if(considererAlpha)
c = ALPHA;
else
c = ROUGE;
break;
case ALPHA : c = ROUGE; break;
}
return c;
}