4 Super Tux - Title Screen
7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - March 15, 2004
19 #include <SDL_image.h>
22 #include <sys/types.h>
30 #include "high_scores.h"
37 #include "leveleditor.h"
42 void loadshared(void);
43 void activate_particle_systems(void);
45 static texture_type bkg_title, img_choose_subset, anim1, anim2;
46 static SDL_Event event;
48 static int frame, pict, i;
49 static unsigned int last_update_time;
50 static unsigned int update_time;
52 void display_credits();
54 void draw_background()
56 /* Draw the title background: */
58 texture_draw_bg(&bkg_title);
60 /* Animate title screen: */
62 pict = (frame / 5) % 3;
65 texture_draw_part(&bkg_title, 560, 270, 560, 270, 80, 75);
67 texture_draw(&anim1, 560, 270);
69 texture_draw(&anim2, 560, 270);
72 /* --- TITLE SCREEN --- */
78 string_list_type level_subsets;
80 level_subsets = dsubdirs("/levels", "info");
81 timer_type random_timer;
82 timer_init(&random_timer, true);
86 st_pause_ticks_init();
87 st_pause_ticks_stop();
89 level_load(¤t_level, (datadir + "/levels/misc/menu.stl").c_str());
91 activate_particle_systems();
92 /* Lower the gravity that tux doesn't jump to hectically through the demo */
95 /* Reset menu variables */
97 Menu::set_current(main_menu);
104 texture_load(&bkg_title,datadir + "/images/title/title.png", IGNORE_ALPHA);
105 texture_load(&anim1,datadir + "/images/title/title-anim2.png", IGNORE_ALPHA);
106 texture_load(&anim2,datadir + "/images/title/title-anim1.png", IGNORE_ALPHA);
107 texture_load(&img_choose_subset,datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
109 /* --- Main title loop: --- */
116 /* Draw the title background: */
117 texture_draw_bg(&bkg_title);
120 update_time = st_get_ticks();
121 timer_start(&random_timer, rand() % 2000 + 2000);
123 while (!done && !quit)
126 /* Calculate the movement-factor */
127 frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
128 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
129 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
130 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
135 while (SDL_PollEvent(&event))
137 if (event.type == SDL_QUIT)
139 /* Quit event - quit: */
142 else if (event.type == SDL_KEYDOWN)
146 key = event.key.keysym.sym;
148 /* Check for menu events */
149 menu_event(&event.key.keysym);
151 if (key == SDLK_ESCAPE)
158 else if (event.type == SDL_JOYAXISMOTION && event.jaxis.axis == JOY_Y)
160 if (event.jaxis.value > 1024)
161 menuaction = MENU_ACTION_DOWN;
162 else if (event.jaxis.value < -1024)
163 menuaction = MENU_ACTION_UP;
165 else if (event.type == SDL_JOYBUTTONDOWN)
167 /* Joystick button: Continue: */
168 menuaction = MENU_ACTION_HIT;
172 /* Draw the background: */
176 /* update particle systems */
177 std::vector<ParticleSystem*>::iterator p;
178 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
180 (*p)->simulate(frame_ratio);
183 /* Draw particle systems (background) */
184 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
186 (*p)->draw(scroll_x, 0, 0);
189 /* Draw interactive tiles: */
191 for (int y = 0; y < 15; ++y)
193 for (int x = 0; x < 21; ++x)
195 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
196 current_level.ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
200 global_frame_counter++;
201 titletux.key_event(SDLK_RIGHT,DOWN);
204 if(timer_check(&random_timer))
207 titletux.key_event(SDLK_UP,UP);
209 titletux.key_event(SDLK_UP,DOWN);
213 timer_start(&random_timer, rand() % 3000 + 3000);
217 if(current_level.width * 32 - 320 < titletux.base.x)
219 titletux.base.x = 160;
228 /* Draw the high score: */
229 sprintf(str, "High score: %d", hs_score);
230 text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1);
231 sprintf(str, "by %s", hs_name);
232 text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1);
235 /* Don't draw menu, if quit is true */
236 if(show_menu && !quit)
237 menu_process_current();
239 if(current_menu == main_menu)
241 switch (main_menu->check())
246 if(level_subsets.num_items != 0)
248 subset.load(level_subsets.item[0]);
251 texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0);
252 if(level_subsets.num_items != 0)
254 texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78);
255 if(level_subsets.num_items > 1)
258 texture_draw(&arrow_left,(screen->w / 2) - ((subset.title.length()+2)*16)/2,20);
259 if(i < level_subsets.num_items-1)
260 texture_draw(&arrow_right,(screen->w / 2) + ((subset.description.length())*16)/2,20);
262 text_drawf(&gold_text, subset.title.c_str(), 0, 20, A_HMIDDLE, A_TOP, 1);
263 text_drawf(&gold_text, subset.description.c_str(), 20, -20, A_HMIDDLE, A_BOTTOM, 1);
267 while(SDL_PollEvent(&event) && !done)
275 case SDL_KEYDOWN: // key pressed
278 key = event.key.keysym.sym;
286 subset.load(level_subsets.item[i]);
289 else if(key == SDLK_RIGHT)
291 if(i < level_subsets.num_items -1)
295 subset.load(level_subsets.item[i]);
298 else if(key == SDLK_SPACE || key == SDLK_RETURN)
301 quit = gameloop(subset.name.c_str(),1,ST_GL_PLAY);
304 else if(key == SDLK_ESCAPE)
317 update_load_save_game_menu(load_game_menu, true);
321 quit = leveleditor(1);
331 else if(current_menu == options_menu)
333 process_options_menu();
335 else if(current_menu == load_game_menu)
337 process_save_load_game_menu(false);
342 /* Set the time of the last update and the time of the current update */
343 last_update_time = update_time;
344 update_time = st_get_ticks();
353 texture_free(&bkg_title);
354 texture_free(&anim1);
355 texture_free(&anim2);
356 string_list_free(&level_subsets);
358 /* Return to main! */
367 void display_credits()
376 string_list_type names;
378 string_list_init(&names);
379 sprintf(filename,"%s/CREDITS", datadir.c_str());
380 if((fi = fopen(filename,"r")) != NULL)
382 while(fgets(temp, sizeof(temp), fi) != NULL)
384 temp[strlen(temp)-1]='\0';
385 string_list_add_item(&names,temp);
391 string_list_add_item(&names,"Credits were not found!");
392 string_list_add_item(&names,"Shame on the guy, who");
393 string_list_add_item(&names,"forgot to include them");
394 string_list_add_item(&names,"in your SuperTux distribution.");
398 timer_init(&timer, SDL_GetTicks());
399 timer_start(&timer, 50);
407 length = names.num_items;
409 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
413 /* in case of input, exit */
414 while(SDL_PollEvent(&event))
418 switch(event.key.keysym.sym)
447 else if(speed < -MAX_VEL)
450 /* draw the credits */
454 text_drawf(&white_big_text, "- Credits -", 0, screen->h-scroll, A_HMIDDLE, A_TOP, 2);
456 for(i = 0, n = 0, d = 0; i < length; i++,n++,d++)
458 if(names.item[i] == "")
462 if(names.item[i][0] == ' ')
463 text_drawf(&white_small_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll-10, A_HMIDDLE, A_TOP, 1);
464 else if(names.item[i][0] == ' ')
465 text_drawf(&white_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
466 else if(names.item[i+1][0] == '-' || names.item[i][0] == '-')
467 text_drawf(&white_big_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 3);
469 text_drawf(&blue_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
474 texture_draw_part(&bkg_title, 0, 0, 0, 0, 640, 130);
478 if(60+screen->h+(n*18)+(d*18)-scroll < 0 && 20+60+screen->h+(n*18)+(d*18)-scroll < 0)
487 if(timer_get_left(&timer) < 0)
490 timer_start(&timer, 50);
493 string_list_free(&names);
495 SDL_EnableKeyRepeat(0, 0); // disables key repeating
497 Menu::set_current(main_menu);