-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
139 lines (130 loc) · 3.3 KB
/
Menu.cpp
File metadata and controls
139 lines (130 loc) · 3.3 KB
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
#include "Menu.h"
Menu::Menu(SDL_Surface* sc)
{
seleccion = 1;
background = IMG_Load("background.png");
botones = IMG_Load("menu.png");
instrucciones = IMG_Load("instrucciones.png");
font = TTF_OpenFont("Frijole-Regular.ttf", 32);
screen = sc;
creador = new CreaMapas(screen);
terminar = false;
instr = false;
score = false;
int cont = 0;
for(int j = 0; j<2; j++)
for(int i = 0; i<4 ; i++)
{
clips[cont].x = (j * 202);
clips[cont].y = (i * 52);
clips[cont].h = 52;
clips[cont++].w = 202;
}
leerScore();
}
Menu::~Menu()
{
SDL_FreeSurface(background);
SDL_FreeSurface(botones);
SDL_FreeSurface(instrucciones);
TTF_CloseFont(font);
}
void Menu::eventos(SDL_Event* evento)
{
switch(evento->type)
{
case SDL_KEYDOWN :
switch(evento->key.keysym.sym)
{
case SDLK_UP :
if(seleccion>1 && !instr && !score)
seleccion--;
break;
case SDLK_DOWN :
if(seleccion<4 && !instr && !score)
seleccion++;
break;
case SDLK_RETURN :
if(seleccion == 1)
terminar = true;
else if(seleccion == 2)
instr = !instr;
else if(seleccion == 3)
score = !score;
else if(seleccion == 4)
{
creador->loop();
}
break;
default :;
}
break;
case SDL_QUIT :
terminar = true;
break;
}
}
void Menu::render()
{
if(score)
{
applySurface(0,0,background);
SDL_Color textColor = { 0, 0, 0 };
for(int i = 0; i<3; i++)
{
SDL_Surface* mensaje = TTF_RenderText_Solid(font, names[i].c_str(), textColor);
applySurface(200, 200+(i*50), mensaje);
SDL_FreeSurface(mensaje);
}
for(int i = 0; i<3; i++)
{
char c[10];
sprintf(c, "%d", scores[i]);
SDL_Surface* mensaje = TTF_RenderText_Solid(font, c, textColor);
applySurface(400, 200+(i*50), mensaje);
SDL_FreeSurface(mensaje);
}
}else if(instr)
{
applySurface(0,0,instrucciones);
}else
{
applySurface(0,0,background);
if(seleccion == 1)
applySurface(200, 178, botones, &clips[4]);
else
applySurface(200, 178, botones, &clips[0]);
if(seleccion == 2)
applySurface(200, 250, botones, &clips[5]);
else
applySurface(200, 250, botones, &clips[1]);
if(seleccion == 3)
applySurface(200, 322, botones, &clips[6]);
else
applySurface(200, 322, botones, &clips[2]);
if(seleccion == 4)
applySurface(200, 394, botones, &clips[7]);
else
applySurface(200, 394, botones, &clips[3]);
}
SDL_Flip(screen);
}
void Menu::applySurface(int x, int y, SDL_Surface* imagen, SDL_Rect* clip)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(imagen, clip, screen, &offset);
}
void Menu::leerScore()
{
ifstream in("Scores.txt");
if(in == NULL)
return;
int cont = 0;
while(in>>names[cont] && in>>scores[cont])
{
cont++;
}
in.close();
}