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();
88 level_load(¤t_level, (datadir + "/levels/misc/menu.stl").c_str());
90 activate_particle_systems();
91 /* Lower the gravity that tux doesn't jump to hectically through the demo */
94 /* Reset menu variables */
96 Menu::set_current(main_menu);
103 texture_load(&bkg_title,datadir + "/images/title/title.png", IGNORE_ALPHA);
104 texture_load(&anim1,datadir + "/images/title/title-anim2.png", IGNORE_ALPHA);
105 texture_load(&anim2,datadir + "/images/title/title-anim1.png", IGNORE_ALPHA);
106 texture_load(&img_choose_subset,datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
108 /* --- Main title loop: --- */
115 /* Draw the title background: */
116 texture_draw_bg(&bkg_title);
119 update_time = st_get_ticks();
120 timer_start(&random_timer, rand() % 2000 + 2000);
122 while (!done && !quit)
125 /* Calculate the movement-factor */
126 frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
127 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
128 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
129 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
134 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 */
151 if (key == SDLK_ESCAPE)
160 /* Draw the background: */
164 /* update particle systems */
165 std::vector<ParticleSystem*>::iterator p;
166 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
168 (*p)->simulate(frame_ratio);
171 /* Draw particle systems (background) */
172 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
174 (*p)->draw(scroll_x, 0, 0);
177 /* Draw interactive tiles: */
179 for (int y = 0; y < 15; ++y)
181 for (int x = 0; x < 21; ++x)
183 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
184 current_level.ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
188 global_frame_counter++;
189 titletux.key_event(SDLK_RIGHT,DOWN);
192 if(timer_check(&random_timer))
195 titletux.key_event(SDLK_UP,UP);
197 titletux.key_event(SDLK_UP,DOWN);
201 timer_start(&random_timer, rand() % 3000 + 3000);
205 if(current_level.width * 32 - 320 < titletux.base.x)
207 titletux.base.x = 160;
216 /* Draw the high score: */
217 sprintf(str, "High score: %d", hs_score);
218 text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1);
219 sprintf(str, "by %s", hs_name);
220 text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1);
223 /* Don't draw menu, if quit is true */
224 if(show_menu && !quit)
225 menu_process_current();
227 if(current_menu == main_menu)
229 switch (main_menu->check())
234 if(level_subsets.num_items != 0)
236 subset.load(level_subsets.item[0]);
239 texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0);
240 if(level_subsets.num_items != 0)
242 texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78);
243 if(level_subsets.num_items > 1)
246 texture_draw(&arrow_left,(screen->w / 2) - ((subset.title.length()+2)*16)/2,20);
247 if(i < level_subsets.num_items-1)
248 texture_draw(&arrow_right,(screen->w / 2) + ((subset.description.length())*16)/2,20);
250 text_drawf(&gold_text, subset.title.c_str(), 0, 20, A_HMIDDLE, A_TOP, 1);
251 text_drawf(&gold_text, subset.description.c_str(), 20, -20, A_HMIDDLE, A_BOTTOM, 1);
255 while(SDL_PollEvent(&event) && !done)
263 case SDL_KEYDOWN: // key pressed
266 key = event.key.keysym.sym;
274 subset.load(level_subsets.item[i]);
277 else if(key == SDLK_RIGHT)
279 if(i < level_subsets.num_items -1)
283 subset.load(level_subsets.item[i]);
286 else if(key == SDLK_SPACE || key == SDLK_RETURN)
289 quit = gameloop(subset.name.c_str(),1,ST_GL_PLAY);
292 else if(key == SDLK_ESCAPE)
305 titletux.level_begin();
306 update_time = st_get_ticks();
309 update_load_save_game_menu(load_game_menu, true);
313 quit = leveleditor(1);
323 else if(current_menu == options_menu)
325 process_options_menu();
327 else if(current_menu == load_game_menu)
329 process_save_load_game_menu(false);
332 mouse_cursor->draw();
336 /* Set the time of the last update and the time of the current update */
337 last_update_time = update_time;
338 update_time = st_get_ticks();
347 texture_free(&bkg_title);
348 texture_free(&anim1);
349 texture_free(&anim2);
350 string_list_free(&level_subsets);
352 /* Return to main! */
361 void display_credits()
370 string_list_type names;
372 string_list_init(&names);
373 sprintf(filename,"%s/CREDITS", datadir.c_str());
374 if((fi = fopen(filename,"r")) != NULL)
376 while(fgets(temp, sizeof(temp), fi) != NULL)
378 temp[strlen(temp)-1]='\0';
379 string_list_add_item(&names,temp);
385 string_list_add_item(&names,"Credits were not found!");
386 string_list_add_item(&names,"Shame on the guy, who");
387 string_list_add_item(&names,"forgot to include them");
388 string_list_add_item(&names,"in your SuperTux distribution.");
392 timer_init(&timer, SDL_GetTicks());
393 timer_start(&timer, 50);
401 length = names.num_items;
403 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
407 /* in case of input, exit */
408 while(SDL_PollEvent(&event))
412 switch(event.key.keysym.sym)
441 else if(speed < -MAX_VEL)
444 /* draw the credits */
448 text_drawf(&white_big_text, "- Credits -", 0, screen->h-scroll, A_HMIDDLE, A_TOP, 2);
450 for(i = 0, n = 0, d = 0; i < length; i++,n++,d++)
452 if(names.item[i] == "")
456 if(names.item[i][0] == ' ')
457 text_drawf(&white_small_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll-10, A_HMIDDLE, A_TOP, 1);
458 else if(names.item[i][0] == ' ')
459 text_drawf(&white_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
460 else if(names.item[i+1][0] == '-' || names.item[i][0] == '-')
461 text_drawf(&white_big_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 3);
463 text_drawf(&blue_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
468 texture_draw_part(&bkg_title, 0, 0, 0, 0, 640, 130);
472 if(60+screen->h+(n*18)+(d*18)-scroll < 0 && 20+60+screen->h+(n*18)+(d*18)-scroll < 0)
481 if(timer_get_left(&timer) < 0)
484 timer_start(&timer, 50);
487 string_list_free(&names);
489 SDL_EnableKeyRepeat(0, 0); // disables key repeating
491 Menu::set_current(main_menu);