Skip to content

Commit 8c795d8

Browse files
authoredNov 2, 2016
Add files via upload
1 parent b5dfddd commit 8c795d8

File tree

1 file changed

+337
-0
lines changed

1 file changed

+337
-0
lines changed
 

‎animations/animations.h

+337
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
#include <SDL.h>
2+
#include <vector>
3+
4+
class menuAnimation
5+
{
6+
private:
7+
8+
SDL_Renderer * laytouRender;
9+
void moveWithoutAnimation(int movement);
10+
void moveWithAnimation(int direction);
11+
12+
SDL_Texture * texture;
13+
SDL_Surface * surface;
14+
15+
int mat[3][4];
16+
int voidi;
17+
int voidj;
18+
SDL_Point corner[12];
19+
SDL_Rect parts[3][4];
20+
21+
public:
22+
menuAnimation(SDL_Renderer * laytouRender);
23+
void run();
24+
25+
};
26+
27+
menuAnimation::menuAnimation(SDL_Renderer * a)
28+
{
29+
laytouRender = a;
30+
31+
32+
for(int i=0; i<3; i++)
33+
for(int j=0; j<4; j++)
34+
mat[i][j] = i*4+j+1;
35+
36+
voidi = 2;
37+
voidj = 3;
38+
39+
}
40+
41+
42+
43+
44+
void menuAnimation::run()
45+
{
46+
47+
surface = SDL_LoadBMP("images/principal.bmp");
48+
texture = SDL_CreateTextureFromSurface(laytouRender,surface);
49+
SDL_FreeSurface(surface);
50+
51+
52+
for(int i=0; i<3; i++)
53+
{
54+
for(int j=0; j<4; j++)
55+
{
56+
parts[i][j] = {j*201,i*201,200,200};
57+
}
58+
}
59+
60+
61+
for(int i=0; i<3; i++)
62+
{
63+
for(int j=0; j<4; j++)
64+
{
65+
corner[i*4+j] = {j*200,i*200};
66+
}
67+
}
68+
69+
70+
// scramble the screen
71+
std::vector<int> moves = {1,1,1,-2,-1,-1,-2,-1,2,2,1,1,1,-2,-1,-1,-2,1,1,2,-1,-1,2,1,-2,-1,-2,1,2};
72+
73+
for(int i=0; i<(int)moves.size(); i++)
74+
moveWithoutAnimation(moves[i]);
75+
76+
SDL_SetRenderDrawColor(laytouRender, 255, 255, 255, 255);
77+
SDL_RenderClear(laytouRender);
78+
SDL_Rect cut = {0,0,200,200};
79+
80+
int number;
81+
for(int i=0; i<3; i++)
82+
{
83+
for(int j=0; j<4; j++)
84+
{
85+
number = mat[i][j]-1;
86+
if(number != 11)
87+
{
88+
cut.x = corner[number].x;
89+
cut.y = corner[number].y;
90+
SDL_RenderCopy(laytouRender,texture,&cut,&parts[i][j]);
91+
}
92+
}
93+
}
94+
SDL_RenderPresent(laytouRender);
95+
SDL_Delay(250);
96+
97+
for(int i=moves.size()-1; i>=0; i--)
98+
moveWithAnimation(moves[i]*-1);
99+
100+
101+
/*
102+
int qtdMoves = 0;
103+
bool running = true;
104+
while(running)
105+
{
106+
107+
108+
109+
SDL_Event event;
110+
while(SDL_PollEvent(&event))
111+
{
112+
switch(event.type)
113+
{
114+
case SDL_QUIT:
115+
running = false;
116+
break;
117+
case SDL_KEYDOWN:
118+
{
119+
if(event.key.keysym.scancode == SDL_SCANCODE_DOWN)
120+
{
121+
//count only valid moves
122+
moveWithAnimation(-2);
123+
}
124+
else if(event.key.keysym.scancode == SDL_SCANCODE_UP)
125+
{
126+
moveWithAnimation(2);
127+
}
128+
if(event.key.keysym.scancode == SDL_SCANCODE_RIGHT)
129+
{
130+
moveWithAnimation(1);
131+
}
132+
if(event.key.keysym.scancode == SDL_SCANCODE_LEFT)
133+
{
134+
moveWithAnimation(-1);
135+
}
136+
137+
}
138+
break;
139+
}
140+
}
141+
142+
SDL_SetRenderDrawColor(laytouRender, 255, 255, 255, 255);
143+
SDL_RenderClear(laytouRender);
144+
SDL_Rect cut = {0,0,200,200};
145+
146+
int number;
147+
for(int i=0; i<3; i++)
148+
{
149+
for(int j=0; j<4; j++)
150+
{
151+
number = mat[i][j]-1;
152+
if(number != 11)
153+
{
154+
cut.x = corner[number].x;
155+
cut.y = corner[number].y;
156+
SDL_RenderCopy(laytouRender,texture,&cut,&parts[i][j]);
157+
}
158+
}
159+
}
160+
SDL_RenderPresent(laytouRender);
161+
SDL_Delay(10);
162+
}
163+
*/
164+
165+
}
166+
167+
void menuAnimation::moveWithoutAnimation(int direction)
168+
{
169+
170+
int aux;
171+
switch(direction)
172+
{
173+
case +1:
174+
if(voidj != 0)
175+
{
176+
aux = mat[voidi][voidj];
177+
mat[voidi][voidj] = mat[voidi][voidj - 1];
178+
mat[voidi][voidj - 1] = aux;
179+
voidj--;
180+
}
181+
182+
break;
183+
184+
case -1:
185+
if(voidj != 3)
186+
{
187+
aux = mat[voidi][voidj];
188+
mat[voidi][voidj] = mat[voidi][voidj + 1];
189+
mat[voidi][voidj + 1] = aux;
190+
voidj++;
191+
}
192+
193+
194+
break;
195+
case +2:
196+
if(voidi != 2)
197+
{
198+
aux = mat[voidi][voidj];
199+
mat[voidi][voidj] = mat[voidi+1][voidj];
200+
mat[voidi+1][voidj] = aux;
201+
voidi++;
202+
}
203+
204+
break;
205+
case -2:
206+
if(voidi != 0)
207+
{
208+
aux = mat[voidi][voidj];
209+
mat[voidi][voidj] = mat[voidi-1][voidj];
210+
mat[voidi-1][voidj] = aux;
211+
voidi--;
212+
}
213+
214+
break;
215+
}
216+
217+
}
218+
219+
void menuAnimation::moveWithAnimation(int direction)
220+
{
221+
222+
int fromi;
223+
int fromj;
224+
int toi = voidi;
225+
int toj = voidj;
226+
227+
228+
int aux;
229+
switch(direction)
230+
{
231+
case +1:
232+
if(voidj != 0)
233+
{
234+
aux = mat[voidi][voidj];
235+
mat[voidi][voidj] = mat[voidi][voidj - 1];
236+
mat[voidi][voidj - 1] = aux;
237+
voidj--;
238+
}
239+
240+
break;
241+
242+
case -1:
243+
if(voidj != 3)
244+
{
245+
aux = mat[voidi][voidj];
246+
mat[voidi][voidj] = mat[voidi][voidj + 1];
247+
mat[voidi][voidj + 1] = aux;
248+
voidj++;
249+
}
250+
251+
252+
break;
253+
case +2:
254+
if(voidi != 2)
255+
{
256+
aux = mat[voidi][voidj];
257+
mat[voidi][voidj] = mat[voidi+1][voidj];
258+
mat[voidi+1][voidj] = aux;
259+
voidi++;
260+
}
261+
262+
break;
263+
case -2:
264+
if(voidi != 0)
265+
{
266+
aux = mat[voidi][voidj];
267+
mat[voidi][voidj] = mat[voidi-1][voidj];
268+
mat[voidi-1][voidj] = aux;
269+
voidi--;
270+
}
271+
272+
break;
273+
}
274+
275+
fromi = voidi;
276+
fromj = voidj;
277+
278+
int num = mat[toi][toj];
279+
280+
int di = toi - fromi;
281+
int dj = toj - fromj;
282+
283+
SDL_Rect toMove = {0,0,200,200};
284+
toMove.x = fromj * 201;
285+
toMove.y = fromi * 201;
286+
287+
288+
bool done = false;
289+
while(!done)
290+
{
291+
292+
SDL_SetRenderDrawColor(laytouRender, 255, 255, 255, 255);
293+
SDL_RenderClear(laytouRender);
294+
SDL_Rect cut = {0,0,200,200};
295+
296+
int number;
297+
for(int i=0; i<3; i++)
298+
{
299+
for(int j=0; j<4; j++)
300+
{
301+
number = mat[i][j];
302+
//printf("valor de number = %d\n",number);
303+
//getchar();
304+
if(number != 12 && number!= num)
305+
{
306+
//printf("passou aqui2\n");
307+
cut.x = corner[number-1].x;
308+
cut.y = corner[number-1].y;
309+
SDL_RenderCopy(laytouRender,texture,&cut,&parts[i][j]);
310+
}
311+
else if(number == 12)
312+
{
313+
//printf("passou aqui\n");
314+
cut.x = corner[num-1].x;
315+
cut.y = corner[num-1].y;
316+
SDL_RenderCopy(laytouRender,texture,&cut,&toMove);
317+
toMove.x+=dj*3;
318+
toMove.y+=di*3;
319+
if(toMove.x == parts[toi][toj].x && toMove.y == parts[toi][toj].y)
320+
done = true;
321+
}
322+
}
323+
}
324+
SDL_Delay(1);
325+
SDL_RenderPresent(laytouRender);
326+
327+
}
328+
329+
}
330+
331+
332+
333+
334+
335+
336+
337+

0 commit comments

Comments
 (0)