Fix window name
[attac-man.git] / menu.c
blobe5664b38faf4107b7b789b56dc53321846349c7c
1 /*
2 Pacman Arena
3 Copyright (C) 2003 Nuno Subtil
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 static const char cvsid[] =
21 "$Id: menu.c,v 1.26 2003/11/30 17:43:55 nsubtil Exp $";
23 #ifdef _WIN32
24 #include <windows.h>
25 #endif
27 #include <GL/gl.h>
28 #include <SDL.h>
29 #include <SDL_net.h>
30 #include <stdlib.h>
31 #include <math.h>
33 #include "object.h"
34 #include "game.h"
35 #include "map.h"
36 #include "player.h"
37 #include "screen.h"
38 #include "render.h"
39 #include "gfx.h"
40 #include "audio.h"
41 #include "net.h"
42 #include "input.h"
43 #include "render_map.h"
45 #include "menu.h"
47 #include "level-demo.h"
48 #include "level-classic.h"
49 #include "level-1.h"
51 void menu_setup_map(struct map *map)
53 int x, y;
55 for(y = 0; y < map->height; y++)
56 for(x = 0; x < map->width; x++)
58 if(MAP(map, x, y).content == MAP_CONTENT_FOOD)
60 MAP_FOOD(map, x, y).type = FOOD_TYPE_INTERMITTENT;
61 MAP_FOOD(map, x, y).status = FOOD_STATUS_EATEN;
62 MAP_FOOD(map, x, y).time = sqrt(pow(x, 2) + pow(y, 2)) / 5.0;
63 MAP_FOOD(map, x, y).active_time = 10.0;
64 MAP_FOOD(map, x, y).fade_out = 1.0;
65 MAP_FOOD(map, x, y).inactive_time = 3.0;
66 MAP_FOOD(map, x, y).fade_in = 1.0;
71 void menu_run(void)
73 struct game *game;
74 struct menu_entry main_menu[] =
76 { "gfx/start-game-high.tga", "gfx/start-game-low.tga", NULL, NULL },
77 { "gfx/resolution-high.tga", "gfx/resolution-low.tga", NULL, NULL },
78 { "gfx/high_scores-high.tga", "gfx/high_scores-low.tga", NULL, NULL },
79 { "gfx/exit-high.tga", "gfx/exit-low.tga", NULL, NULL }
81 int main_menu_num = 4;
82 int c, current_selection = 0;
84 for(c = 0; c < main_menu_num; c++)
86 main_menu[c].image_high = gfx_get(main_menu[c].image_name_high);
87 if(main_menu[c].image_high->id == -1)
89 gfx_alpha_from_key(main_menu[c].image_name_high, 0, 0, 0);
90 gfx_upload_texture(main_menu[c].image_name_high);
93 main_menu[c].image_low = gfx_get(main_menu[c].image_name_low);
94 if(main_menu[c].image_low->id == -1)
96 gfx_alpha_from_key(main_menu[c].image_name_low, 0, 0, 0);
97 gfx_upload_texture(main_menu[c].image_name_low);
101 /* preload */
102 game = game_new(GAME_TYPE_DEMO);
103 game->map = map_load_from_ascii((char **)level_demo, NULL,
104 LEVEL_DEMO_WIDTH, LEVEL_DEMO_HEIGHT);
105 menu_setup_map(game->map);
107 game_reset(game);
108 // game_load_level(LEVEL_DEMO);
109 game->demo_camera = (struct camera *)malloc(sizeof(struct camera));
110 game->demo_camera->type = CAMERA_TYPE_DEMO;
111 game_reset_camera(game, -1);
112 game_start(game);
114 audio_play_music("sfx/hummingbird.ogg");
115 screen_set_fadein(0.5);
117 for(;;)
119 struct game *gm;
120 float delta;
121 int last_update, ticks;
123 switch((current_selection = menu(game, main_menu,
124 main_menu_num, current_selection))) {
125 case 0:
127 last_update = SDL_GetTicks();
128 delta = 0.0;
130 audio_fade_music(500);
131 screen_set_fadeout(0.5);
133 for(;;)
135 int diff;
137 ticks = SDL_GetTicks();
138 diff = ticks - last_update;
139 last_update = ticks;
140 delta += (float)diff / 1000;
142 if(delta >= 0.5)
143 break;
145 // screen_update_gamma((float)diff / 1000);
146 render_start_frame();
147 render_setup(game, -1);
148 map_render_opaque_objects(game);
149 game_draw_opaque_objects(game, -1);
150 game_draw_translucent_objects(game, -1);
151 map_render_translucent_objects(game, -1);
154 /* classic single-player */
155 gm = game_new(GAME_TYPE_CLASSIC);
156 gm->map = map_load_from_ascii((char **)level_classic,
157 (char **)level_classic_ghost_map,
158 LEVEL_CLASSIC_WIDTH, LEVEL_CLASSIC_HEIGHT);
159 player_add_new(gm);
160 // player_add_new(gm);
161 // screen_set_num_viewports(2);
162 // screen_reset_gamma();
163 game_run(gm);
165 screen_set_num_viewports(1);
166 audio_play_music("sfx/hummingbird.ogg");
167 break;
169 case 1:
170 menu_resolution(game);
171 break;
173 case 2:
174 menu_high_scores();
175 break;
177 case 3:
178 SDL_Quit();
179 exit(0);
180 break;
182 case -1:
183 SDL_Quit();
184 exit(0);
189 void menu_resolution(struct game *game)
191 struct menu_entry menu_resolution[] =
193 { "gfx/640-480-high.tga", "gfx/640-480-low.tga", NULL, NULL },
194 { "gfx/800-600-high.tga", "gfx/800-600-low.tga", NULL, NULL },
195 { "gfx/1024-768-high.tga", "gfx/1024-768-low.tga", NULL, NULL },
196 { "gfx/1400-1050-high.tga", "gfx/1400-1050-low.tga", NULL, NULL }
198 int menu_resolution_num = 4;
199 int ret, c, width, new_w = 0, height, new_h = 0;
200 int current_selection = 0;
202 for(c = 0; c < menu_resolution_num; c++)
204 menu_resolution[c].image_high = gfx_get(menu_resolution[c].image_name_high);
205 if(menu_resolution[c].image_high->id == -1)
207 gfx_alpha_from_key(menu_resolution[c].image_name_high, 0, 0, 0);
208 gfx_upload_texture(menu_resolution[c].image_name_high);
211 menu_resolution[c].image_low = gfx_get(menu_resolution[c].image_name_low);
212 if(menu_resolution[c].image_low->id == -1)
214 gfx_alpha_from_key(menu_resolution[c].image_name_low, 0, 0, 0);
215 gfx_upload_texture(menu_resolution[c].image_name_low);
219 screen_get_resolution(&width, &height);
220 switch (width) {
221 case 640:
222 current_selection = 0;
223 break;
224 case 800:
225 current_selection = 1;
226 break;
227 case 1024:
228 current_selection = 2;
229 break;
230 case 1400:
231 current_selection = 3;
232 break;
233 default:
234 current_selection = 0;
235 break;
238 while ((ret = menu(game, menu_resolution,
239 menu_resolution_num, current_selection)) != -1) {
240 switch(ret) {
241 case 0:
242 new_w = 640;
243 new_h = 480;
244 break;
246 case 1:
247 new_w = 800;
248 new_h = 600;
249 break;
251 case 2:
252 new_w = 1024;
253 new_h = 768;
254 break;
256 case 3:
257 new_w = 1400;
258 new_h = 1050;
259 break;
261 default:
262 return;
265 current_selection = ret;
266 screen_set_resolution(new_w, new_h);
267 screen_switch_resolution();
271 void menu_high_scores(void)
273 return;
276 int menu(struct game *game, struct menu_entry *m, int n, int cur)
278 int last_update, c;
279 int selected = cur;
280 struct image_rgba32 *banner;
281 struct map *map;
282 struct viewport *vp;
284 map = game->map;
286 banner = gfx_get("gfx/pacman-arena-banner.tga");
287 if(banner->id == -1)
289 gfx_alpha_from_key("gfx/pacman-arena-banner.tga", 0, 0, 0);
290 gfx_upload_texture("gfx/pacman-arena-banner.tga");
294 game_reset();
295 game_load_level(LEVEL_DEMO);
296 camera_type = CAMERA_TYPE_DEMO;
297 game_reset_camera();
298 game_start();
301 vp = screen_get_viewport(0);
303 last_update = SDL_GetTicks();
305 for(;;)
307 int diff, top, left, width, height;
309 input_update();
311 if(input_kstate(SDLK_ESCAPE))
313 input_kclear(SDLK_ESCAPE);
314 return -1;
317 if(input_kstate(SDLK_RETURN))
319 input_kclear(SDLK_RETURN);
320 return selected;
324 if(input_kstate(SDLK_DOWN))
326 input_kclear(SDLK_DOWN);
327 selected = (selected + 1) % n;
331 if(input_kstate(SDLK_UP))
333 input_kclear(SDLK_UP);
334 selected = (selected + n - 1) % n;
337 diff = SDL_GetTicks() - last_update;
338 if(server_running)
339 net_server_update();
341 game_update(game, (float)diff / 1000);
342 last_update = SDL_GetTicks();
344 // screen_update_gamma((float)diff / 1000);
345 render_start_frame();
346 render_setup(game, -1);
347 map_render_opaque_objects(game);
348 game_draw_opaque_objects(game, -1);
349 game_draw_translucent_objects(game, -1);
350 map_render_translucent_objects(game, -1);
352 render_setup_2d(vp);
354 top = vp->height / 2 - vp->height / 5;
356 width = 0;
357 height = 0;
359 for(c = 0; c < n; c++)
361 if(c == selected)
363 if(m[c].image_high->width > width)
364 width = m[c].image_high->width;
366 height += m[c].image_high->height + 20;
368 } else {
369 if(m[c].image_low->width > width)
370 width = m[c].image_low->width;
372 height += m[c].image_low->height + 20;
376 width += 40;
377 left = vp->width / 2 - width / 2;
379 glDisable(GL_TEXTURE_2D);
380 glEnable(GL_BLEND);
381 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
382 glColor4f(0.0, 0.0, 0.0, 0.5);
384 glBegin(GL_QUADS);
385 glVertex2i(left, top);
386 glVertex2i(left + width, top);
387 glVertex2i(left + width, top + height);
388 glVertex2i(left, top + height);
389 glEnd();
391 for(c = 0; c < n; c++)
393 if(c == selected)
395 render_draw_scaled_image(
396 m[c].image_name_high,
397 vp->width / 2 - m[c].image_high->width / 2,
398 top,
399 vp->width / 2 + m[c].image_high->width / 2,
400 top + m[c].image_high->height);
402 top += m[c].image_high->height + 20;
403 height += m[c].image_high->height + 20;
404 } else {
405 render_draw_scaled_image(
406 m[c].image_name_low,
407 vp->width / 2 - m[c].image_low->width / 2,
408 top,
409 vp->width / 2 + m[c].image_low->width / 2,
410 top + m[c].image_low->height);
412 top += m[c].image_low->height + 20;
413 height += m[c].image_high->height + 20;
418 render_draw_scaled_image("gfx/pacman-arena-banner.tga", 0, 0,
419 vp->width, vp->width * banner->height / banner->width);
421 render_finish_frame();