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;
46 static texture_type logo;
47 static texture_type img_choose_subset;
50 static Player titletux;
51 static timer_type random_timer;
53 static SDL_Event event;
56 static unsigned int last_update_time;
57 static unsigned int update_time;
59 void display_credits();
61 void draw_background()
63 /* Draw the title background: */
65 texture_draw_bg(&bkg_title);
71 /* update particle systems */
72 std::vector<ParticleSystem*>::iterator p;
73 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
75 (*p)->simulate(frame_ratio);
78 /* Draw particle systems (background) */
79 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
81 (*p)->draw(scroll_x, 0, 0);
84 /* Draw interactive tiles: */
86 for (int y = 0; y < 15; ++y)
88 for (int x = 0; x < 21; ++x)
90 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
91 current_level.ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
95 global_frame_counter++;
96 titletux.key_event(SDLK_RIGHT,DOWN);
98 if(timer_check(&random_timer))
101 titletux.key_event(SDLK_UP,UP);
103 titletux.key_event(SDLK_UP,DOWN);
107 timer_start(&random_timer, rand() % 3000 + 3000);
111 if(current_level.width * 32 - 320 < titletux.base.x)
113 titletux.base.x = 160;
123 /* --- TITLE SCREEN --- */
128 string_list_type level_subsets;
130 level_subsets = dsubdirs("/levels", "info");
131 timer_init(&random_timer, true);
136 st_pause_ticks_init();
138 level_load(¤t_level, (datadir + "/levels/misc/menu.stl").c_str());
140 activate_particle_systems();
141 /* Lower the gravity that tux doesn't jump to hectically through the demo */
144 /* Reset menu variables */
146 Menu::set_current(main_menu);
148 clearscreen(0, 0, 0);
153 texture_load(&bkg_title,datadir + "/images/title/background.jpg", IGNORE_ALPHA);
154 texture_load(&logo,datadir + "/images/title/logo.png", USE_ALPHA);
155 texture_load(&img_choose_subset,datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
157 /* --- Main title loop: --- */
164 /* Draw the title background: */
165 texture_draw_bg(&bkg_title);
168 update_time = st_get_ticks();
169 timer_start(&random_timer, rand() % 2000 + 2000);
171 while (!done && !quit)
174 /* Calculate the movement-factor */
175 frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
176 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
177 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
178 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
183 while (SDL_PollEvent(&event))
186 if (event.type == SDL_QUIT)
188 /* Quit event - quit: */
191 else if (event.type == SDL_KEYDOWN)
195 key = event.key.keysym.sym;
197 /* Check for menu events */
200 if (key == SDLK_ESCAPE)
209 /* Draw the background: */
212 texture_draw(&logo, 160, 30);
213 text_draw(&white_small_text,
214 " SuperTux " VERSION "\n"
215 "Copyright (c) 2003 SuperTux Devel Team\n"
216 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
217 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
221 /* Draw the high score: */
223 sprintf(str, "High score: %d", hs_score);
224 text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1);
225 sprintf(str, "by %s", hs_name);
226 text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1);
229 /* Don't draw menu, if quit is true */
230 if(show_menu && !quit)
231 menu_process_current();
233 if(current_menu == main_menu)
235 switch (main_menu->check())
240 if(level_subsets.num_items != 0)
242 subset.load(level_subsets.item[0]);
245 texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0);
246 if(level_subsets.num_items != 0)
248 texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78);
249 if(level_subsets.num_items > 1)
252 texture_draw(&arrow_left,(screen->w / 2) - ((subset.title.length()+2)*16)/2,20);
253 if(i < level_subsets.num_items-1)
254 texture_draw(&arrow_right,(screen->w / 2) + ((subset.description.length())*16)/2,20);
256 text_drawf(&gold_text, subset.title.c_str(), 0, 20, A_HMIDDLE, A_TOP, 1);
257 text_drawf(&gold_text, subset.description.c_str(), 20, -20, A_HMIDDLE, A_BOTTOM, 1);
261 while(SDL_PollEvent(&event) && !done)
269 case SDL_KEYDOWN: // key pressed
272 key = event.key.keysym.sym;
280 subset.load(level_subsets.item[i]);
283 else if(key == SDLK_RIGHT)
285 if(i < level_subsets.num_items -1)
289 subset.load(level_subsets.item[i]);
292 else if(key == SDLK_SPACE || key == SDLK_RETURN)
295 quit = gameloop(subset.name.c_str(),1,ST_GL_PLAY);
298 else if(key == SDLK_ESCAPE)
311 titletux.level_begin();
312 update_time = st_get_ticks();
315 update_load_save_game_menu(load_game_menu, true);
319 quit = leveleditor(1);
329 else if(current_menu == options_menu)
331 process_options_menu();
333 else if(current_menu == load_game_menu)
335 process_save_load_game_menu(false);
338 mouse_cursor->draw();
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);
355 string_list_free(&level_subsets);
357 /* Return to main! */
366 void display_credits()
375 string_list_type names;
377 string_list_init(&names);
378 sprintf(filename,"%s/CREDITS", datadir.c_str());
379 if((fi = fopen(filename,"r")) != NULL)
381 while(fgets(temp, sizeof(temp), fi) != NULL)
383 temp[strlen(temp)-1]='\0';
384 string_list_add_item(&names,temp);
390 string_list_add_item(&names,"Credits were not found!");
391 string_list_add_item(&names,"Shame on the guy, who");
392 string_list_add_item(&names,"forgot to include them");
393 string_list_add_item(&names,"in your SuperTux distribution.");
397 timer_init(&timer, SDL_GetTicks());
398 timer_start(&timer, 50);
406 length = names.num_items;
408 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
412 /* in case of input, exit */
413 while(SDL_PollEvent(&event))
417 switch(event.key.keysym.sym)
446 else if(speed < -MAX_VEL)
449 /* draw the credits */
453 text_drawf(&white_big_text, "- Credits -", 0, screen->h-scroll, A_HMIDDLE, A_TOP, 2);
455 for(i = 0, n = 0, d = 0; i < length; i++,n++,d++)
457 if(names.item[i] == "")
461 if(names.item[i][0] == ' ')
462 text_drawf(&white_small_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll-10, A_HMIDDLE, A_TOP, 1);
463 else if(names.item[i][0] == ' ')
464 text_drawf(&white_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
465 else if(names.item[i+1][0] == '-' || names.item[i][0] == '-')
466 text_drawf(&white_big_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 3);
468 text_drawf(&blue_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
474 if(60+screen->h+(n*18)+(d*18)-scroll < 0 && 20+60+screen->h+(n*18)+(d*18)-scroll < 0)
483 if(timer_get_left(&timer) < 0)
486 timer_start(&timer, 50);
489 string_list_free(&names);
491 SDL_EnableKeyRepeat(0, 0); // disables key repeating
493 Menu::set_current(main_menu);