-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.c
270 lines (208 loc) · 6.7 KB
/
menu.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "menu.h"
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <SDL/SDL.h>
#include "input.h"
#include "snge.h"
#include "defs.h"
#include "message.h"
#include "mainloop.h"
#define STAR_COUNT 30
#define M_SPD 1000.0
#define R_SPD (M_PI * 1.5)
static enum
{
ME_500M,
ME_1000M,
ME_HOF,
ME_SETTINGS,
ME_EXIT,
ME_CREDITS,
ME_LAST
} menuEntry;
#define _ICON_WIDTH 64.0
#define _ICON_HEIGHT 64.0
#define _MENU_RADIUS 125.0
#define _MENU_X (400.0 - _ICON_WIDTH / 2.0)
#define _MENU_Y (260.0 - _ICON_HEIGHT / 2.0)
#define _ARROW_MAX_SHIFT 20.0
#define _ARROW_Y (_MENU_Y - _MENU_RADIUS - _ARROW_MAX_SHIFT - _ICON_HEIGHT)
#define _ARROW_SPEED 60.0
#define _ARROW_SFACTOR 1.5
#define _ARROW_RISE_SPEED 300.0
#define _ROT_SPEED 100.0
#define _LABEL_FONT "font:base"
#define _LABEL_FADE_SPEED 2.0
static Sprite *menuEntries[ME_LAST];
static Sprite *menuLabels[ME_LAST];
static Sprite *stars[STAR_COUNT];
static Sprite *arrow;
static int entry = ME_1000M;
static int nextEntry = ME_1000M;
static float angle;
static float angleShift;
static float arrowShift;
static float arrowSpeed;
static float angleSpacing;
static int rotDir;
static bool arrowFalling;
static int starw, starh;
static struct
{
float spx;
float spy;
float spr;
} starp[STAR_COUNT];
static void procStars(float lag)
{
int i;
for(i = 0; i < STAR_COUNT; ++i)
{
stars[i]->x += starp[i].spx * lag;
stars[i]->y += starp[i].spy * lag;
stars[i]->angle += starp[i].spr * lag;
if(stars[i]->x >= _SCREEN_WIDTH || stars[i]->y >= _SCREEN_HEIGHT || (stars[i]->x + starw) < 0 || (stars[i]->y + starh) < 0)
{
stars[i]->x = rand() % _SCREEN_WIDTH;
stars[i]->y = -starh;
starp[i].spx = (float)(rand() % 200) - 100.0;
starp[i].spy = 50.0;
starp[i].spr = (float)(rand() % 720) - 360.0;
}
}
}
static void procMenu(float lag)
{
arrowShift += lag * arrowSpeed;
if(arrowShift < 0.0 && !arrowFalling)
{
arrowShift = -arrowShift;
arrowSpeed = _ARROW_SFACTOR * _ARROW_SPEED;
}
if(arrowShift >= _ARROW_MAX_SHIFT)
{
arrowShift = _ARROW_MAX_SHIFT;
arrowSpeed = -_ARROW_SPEED;
arrowFalling = false;
}
if(arrowShift < -100.0 && arrowFalling)
{
arrowSpeed = _ARROW_RISE_SPEED;
arrowShift = -100.0;
}
arrow->y = (float)_ARROW_Y + arrowShift;
if(input_IsDirPressed(DIR_LEFT) && rotDir == 0)
{
rotDir = -1;
nextEntry = entry + 1;
if(nextEntry == ME_LAST)
nextEntry = 0;
arrowSpeed = -_ARROW_RISE_SPEED;
arrowFalling = true;
}
if(input_IsDirPressed(DIR_RIGHT) && rotDir == 0)
{
rotDir = 1;
nextEntry = entry - 1;
if(nextEntry == -1)
nextEntry = ME_LAST - 1;
arrowSpeed = -_ARROW_RISE_SPEED;
arrowFalling = true;
}
angleShift += lag * _ROT_SPEED * (float)rotDir;
if((rotDir == 1 && angleShift >= angleSpacing) || (rotDir == -1 && angleShift <= -angleSpacing))
{
menuLabels[entry]->opacity = 0.0;
menuLabels[nextEntry]->opacity = 1.0;
angleShift = 0.0;
rotDir = 0;
entry = nextEntry;
}
if(rotDir != 0)
{
menuLabels[nextEntry]->opacity += _LABEL_FADE_SPEED * lag;
menuLabels[entry]->opacity -= _LABEL_FADE_SPEED * lag;
if(menuLabels[entry]->opacity < 0.0)
menuLabels[entry]->opacity = 0.0;
if(menuLabels[nextEntry]->opacity > 1.0)
menuLabels[nextEntry]->opacity = 1.0;
}
angle = -(float)entry * angleSpacing + angleShift - 90.0;
int i;
for(i = 0; i < ME_LAST; ++i)
{
float fangle = angle + (float)i * angleSpacing;
menuEntries[i]->x = cosf(DEG_TO_RAD(fangle)) * _MENU_RADIUS + _MENU_X;
menuEntries[i]->y = sinf(DEG_TO_RAD(fangle)) * _MENU_RADIUS + _MENU_Y;
}
}
static void center(Sprite *s)
{
IntPoint sz = snge_GetTextSize(s);
s->x = (float)(_SCREEN_WIDTH - sz.x) / 2.0;
s->y = (float)(_SCREEN_HEIGHT - sz.y) / 2.0;
}
void menu_Init(void *data)
{
sprites_LoadFromCfg("sprites/menu/menu.spr", "");
sprites_LoadFontsFromCfg("fonts/fonts.desc");
menuEntry = ME_EXIT;
srand(time(NULL));
//graphics_SetBackground(FT_FLAT, color(0.3, 0.59, 1.0, 1), color(0, 0, 0, 0), GT_NONE);
// snge_AddSprite(sprites_GetIdByName("menu_logo"), point(100, 25), 5);
SpriteClassId starc = sprites_GetIdByName("star");
int i;
//for(i = 0; i < STAR_COUNT; ++i)
// stars[i] = snge_AddSprite(starc, point(-500, -500), -1);
sprites_GetDimensions(starc, &starw, &starh);
menuEntries[ME_500M] = snge_AddSprite(sprites_GetIdByName("start1"), point(0,0), 10);
menuEntries[ME_1000M] = snge_AddSprite(sprites_GetIdByName("start2"), point(0,0), 10);
menuEntries[ME_SETTINGS] = snge_AddSprite(sprites_GetIdByName("settings"), point(0,0), 10);
menuEntries[ME_HOF] = snge_AddSprite(sprites_GetIdByName("highscores"), point(0,0), 10);
menuEntries[ME_EXIT] = snge_AddSprite(sprites_GetIdByName("exit"), point(0,0), 10);
menuEntries[ME_CREDITS] = snge_AddSprite(sprites_GetIdByName("credits"), point(0,0), 10);
menuLabels[ME_500M] = snge_AddFontSprite(sprites_GetIdByName(_LABEL_FONT), point(0,0), 9, "500M");
menuLabels[ME_1000M] = snge_AddFontSprite(sprites_GetIdByName(_LABEL_FONT), point(0,0), 9, "1000M");
menuLabels[ME_SETTINGS] = snge_AddFontSprite(sprites_GetIdByName(_LABEL_FONT), point(0,0), 9, "SETTINGS");
menuLabels[ME_HOF] = snge_AddFontSprite(sprites_GetIdByName(_LABEL_FONT), point(0,0), 9, "HALL OF FAME");
menuLabels[ME_EXIT] = snge_AddFontSprite(sprites_GetIdByName(_LABEL_FONT), point(0,0), 9, "EXIT");
menuLabels[ME_CREDITS] = snge_AddFontSprite(sprites_GetIdByName(_LABEL_FONT), point(0,0), 9, "CREDITS!");
for(i = 0; i < ME_LAST; ++i)
{
center(menuLabels[i]);
menuLabels[i]->opacity = 0.0;
}
arrow = snge_AddSprite(sprites_GetIdByName("arrow"), point(_MENU_X, _ARROW_Y), 10);
angleSpacing = 360.0 / (float)ME_LAST;
angle = 0.0;
angleShift = 0.0;
arrowShift = 0.0;
arrowSpeed = _ARROW_SFACTOR * _ARROW_SPEED;
rotDir = 0;
arrowFalling = false;
menuLabels[entry]->opacity = 1.0;
}
int menu_Frame(float lag)
{
//procStars(lag);
procMenu(lag);
if(input_IsKeyPressed(KEY_DRILL))
{
switch(entry)
{
case ME_1000M:
mainloop_ChangeScrWithFade(SCR_GAME, NULL, 1.0);
break;
case ME_SETTINGS:
mainloop_ChangeScrWithFade(SCR_SETTINGS, NULL, 1.0);
break;
}
}
return 0;
}
void menu_Cleanup()
{
snge_FreeSprites();
sprites_FreeAll();
}