-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitle.c
203 lines (174 loc) · 4.51 KB
/
title.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
/* Display the title screen, and handle the main menu...
* vim:set cin sm ts=8 sw=4 sts=4: - Sec <[email protected]>
* $Id: title.c,v 1.14 2005/12/08 11:11:18 sec Exp $
*/
#include "brillion.h"
#include <SDL_image.h>
#define MENU_LINE 40
#define MENU_ENTRIES 5 /* XXXXXXXXXXXXXXXXXXXXXXXX!!!! */
SDL_Surface * create_title(menuentry * entries){
SDL_Surface *title,*s,*bkg;
int x;
int xk,yk;
s=play->g->display;
title=IMG_Load("title.png");
assert(title!=NULL);
/* sleep(1); */ /* I'd like to have a cool intro here ;-) */
/* To make it more readable, perhaps?
black=SDL_CreateRGBSurface(SDL_HWSURFACE, s->w, s->h,
s->format->BitsPerPixel, s->format->Rmask, s->format->Gmask,
s->format->Bmask, s->format->Amask);
SDL_FillRect(black, NULL, SDL_MapRGB(s->format,0,0,0));
SDL_SetAlpha(black, SDL_SRCALPHA, 64);
SDL_BlitSurface(black, NULL, title, NULL);
*/
bkg=SDL_ConvertSurface(title, s->format, SDL_SWSURFACE);
SDL_FreeSurface(title);
xk=100;yk=260; /* XXX: Main_menu only */
for(x=0;entries[x].name;x++){
render_font_to(xk,yk,entries[x].name,bkg);
yk+=MENU_LINE;
};
/* Display it, for good measure */
SDL_BlitSurface(bkg,NULL,s,NULL);
SDL_Flip(s);
return bkg; /* Caller must SDL_FreeSurface(bkg) eventually. */
}
void cb_sound(SDL_Surface *title,int dwim){
SDL_Rect sndr;
graphic * g=play->g;
if(dwim){
if(play->m){
uninit_music();
}else{
play->m=init_music();
};
}
sndr.x=240;sndr.w=100; sndr.y=300;sndr.h=40; /* XXX: Why there? */
SDL_BlitSurface(title,&sndr,play->g->display,&sndr);
#ifdef SOUND
if(play->m){
render_font(sndr.x,sndr.y,"On");
}else{
render_font(sndr.x,sndr.y,"Off");
};
#else
render_font(sndr.x,sndr.y,"n/a");
#endif
UPDATE(sndr);
}
void cb_lvl(SDL_Surface *title,int dwim){
SDL_Rect lvlr;
graphic * g=play->g;
char lvlnum[50];
if(dwim){
play->level+=4;
if(play->level>20)play->level=0;
}
lvlr.x=240;lvlr.w=100; lvlr.y=340;lvlr.h=50; /* XXX: Why there? */
sprintf(lvlnum,"%2d",play->level+1);
SDL_BlitSurface(title,&lvlr,play->g->display,&lvlr);
render_font(lvlr.x,lvlr.y,lvlnum);
UPDATE(lvlr);
}
signed int do_menu(menuentry *entries){
SDL_Event event;
SDL_Rect r;
signed int menu,omenu=1,done=0;
graphic * g=play->g;
SDL_Surface *title;
r.x=0;r.y=0;r.w=16;r.h=16; /* XXX: QUAD/2? :) */
title=create_title(entries);
for(menu=0;entries[menu].name;menu++){
if (entries[menu].callback)
(entries[menu].callback)(title,0);
};
menu=0;
while(!done){
while( !done && SDL_PollEvent( &event ) ){
/* We are only interested in SDL_KEYDOWN and SDL_KEYUP events */
switch( event.type ){
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_ESCAPE:
case SDLK_q:
omenu=menu=-1; /* Quit menu */
done=1;
break;
case SDLK_UP:
if (--menu<0) menu+=MENU_ENTRIES;
break;
case SDLK_DOWN:
if (++menu>=MENU_ENTRIES) menu-=MENU_ENTRIES;
break;
case SDLK_RETURN:
case SDLK_SPACE:
if (entries[menu].callback){
entries[menu].callback (title,1);
}else
done=1;
break;
default: /* perhaps beep? */
break;
};
break;
case SDL_KEYUP:
break;
case SDL_QUIT: /* SDL_QUIT event (window close) */
exit(-1); /* XXX */
break;
default:
break;
};
};
if(omenu!=menu){
SDL_BlitSurface(title,&r,play->g->display,&r);
UPDATE(r);
r.x=100-30;r.y=260+MENU_LINE*menu+6; /* XXX! */
SDL_BlitSurface(play->g->ball[RED],NULL,play->g->display,&r);
UPDATE(r);
omenu=menu;
};
if(!done){
/* While No events are pending, we can do funky idle things */
SDL_Delay(100);
SDL_BlitSurface(title,&r,play->g->display,&r);
UPDATE(r);
r.x=100-30;r.y=260+MENU_LINE*menu+6; /* XXX! */
switch(rand()&7){
case 0:
r.x++;r.y++;break;
case 1:
r.x++; break;
case 2:
r.x++;r.y--;break;
case 3:
r.y++;break;
case 4:
r.y--;break;
case 5:
r.x--;r.y++;break;
case 6:
r.x--; break;
case 7:
r.x--;r.y--;break;
};
SDL_BlitSurface(play->g->ball[RED],NULL,play->g->display,&r);
UPDATE(r);
};
DISPLAY;
}; /* while(!done) */
SDL_FreeSurface(title);
return(menu);
}
signed int title_main(){
menuentry me[MENU_ENTRIES+1] = {
{"PLAY", NULL},
{"SOUND", &cb_sound},
{"LEVEL", &cb_lvl},
{"SCORES", NULL},
{"QUIT", NULL},
{NULL, NULL}
};
return do_menu(me);
}