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 static texture_type bkg_title, img_choose_subset, anim1, anim2;
43 static SDL_Event event;
45 static int frame, pict, i;
46 static unsigned int last_update_time;
47 static unsigned int update_time;
49 void display_credits();
51 void draw_background()
53 /* Draw the title background: */
55 texture_draw_bg(&bkg_title);
57 /* Animate title screen: */
59 pict = (frame / 5) % 3;
62 texture_draw_part(&bkg_title, 560, 270, 560, 270, 80, 75);
64 texture_draw(&anim1, 560, 270);
66 texture_draw(&anim2, 560, 270);
69 void loadshared(void);
70 void activate_particle_systems(void);
71 /* --- TITLE SCREEN --- */
77 string_list_type level_subsets;
79 level_subsets = dsubdirs("/levels", "info");
82 st_pause_ticks_init();
83 st_pause_ticks_stop();
85 level_load(¤t_level, (datadir + "/levels/misc/menu.stl").c_str());
87 activate_particle_systems();
88 /* Lower the gravity that tux doesn't jump to hectically through the demo */
91 /* Reset menu variables */
93 Menu::set_current(main_menu);
100 texture_load(&bkg_title,datadir + "/images/title/title.png", IGNORE_ALPHA);
101 texture_load(&anim1,datadir + "/images/title/title-anim2.png", IGNORE_ALPHA);
102 texture_load(&anim2,datadir + "/images/title/title-anim1.png", IGNORE_ALPHA);
103 texture_load(&img_choose_subset,datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
105 /* --- Main title loop: --- */
112 /* Draw the title background: */
113 texture_draw_bg(&bkg_title);
116 update_time = st_get_ticks();
118 while (!done && !quit)
121 /* Calculate the movement-factor */
122 frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
123 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
124 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
125 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
130 while (SDL_PollEvent(&event))
132 if (event.type == SDL_QUIT)
134 /* Quit event - quit: */
137 else if (event.type == SDL_KEYDOWN)
141 key = event.key.keysym.sym;
143 /* Check for menu events */
144 menu_event(&event.key.keysym);
146 if (key == SDLK_ESCAPE)
153 else if (event.type == SDL_JOYAXISMOTION && event.jaxis.axis == JOY_Y)
155 if (event.jaxis.value > 1024)
156 menuaction = MENU_ACTION_DOWN;
157 else if (event.jaxis.value < -1024)
158 menuaction = MENU_ACTION_UP;
160 else if (event.type == SDL_JOYBUTTONDOWN)
162 /* Joystick button: Continue: */
163 menuaction = MENU_ACTION_HIT;
167 /* Draw the background: */
171 /* update particle systems */
172 std::vector<ParticleSystem*>::iterator p;
173 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
175 (*p)->simulate(frame_ratio);
178 /* Draw particle systems (background) */
179 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
181 (*p)->draw(scroll_x, 0, 0);
184 /* Draw interactive tiles: */
186 for (int y = 0; y < 15; ++y)
188 for (int x = 0; x < 21; ++x)
190 drawshape(32*x - fmodf(scroll_x, 32), y * 32,
191 current_level.ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
195 global_frame_counter++;
196 titletux.key_event(SDLK_RIGHT,DOWN);
197 titletux.key_event(SDLK_UP,DOWN);
199 if(current_level.width * 32 - 320 < titletux.base.x)
201 titletux.base.x = 160;
210 /* Draw the high score: */
211 sprintf(str, "High score: %d", hs_score);
212 text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1);
213 sprintf(str, "by %s", hs_name);
214 text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1);
217 /* Don't draw menu, if quit is true */
218 if(show_menu && !quit)
219 menu_process_current();
221 if(current_menu == main_menu)
223 switch (main_menu->check())
228 if(level_subsets.num_items != 0)
230 subset.load(level_subsets.item[0]);
233 texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0);
234 if(level_subsets.num_items != 0)
236 texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78);
237 if(level_subsets.num_items > 1)
240 texture_draw(&arrow_left,(screen->w / 2) - ((subset.title.length()+2)*16)/2,20);
241 if(i < level_subsets.num_items-1)
242 texture_draw(&arrow_right,(screen->w / 2) + ((subset.description.length())*16)/2,20);
244 text_drawf(&gold_text, subset.title.c_str(), 0, 20, A_HMIDDLE, A_TOP, 1);
245 text_drawf(&gold_text, subset.description.c_str(), 20, -20, A_HMIDDLE, A_BOTTOM, 1);
249 while(SDL_PollEvent(&event) && !done)
257 case SDL_KEYDOWN: // key pressed
260 key = event.key.keysym.sym;
268 subset.load(level_subsets.item[i]);
271 else if(key == SDLK_RIGHT)
273 if(i < level_subsets.num_items -1)
277 subset.load(level_subsets.item[i]);
280 else if(key == SDLK_SPACE || key == SDLK_RETURN)
283 quit = gameloop(subset.name.c_str(),1,ST_GL_PLAY);
286 else if(key == SDLK_ESCAPE)
299 update_load_save_game_menu(load_game_menu, true);
303 quit = leveleditor(1);
313 else if(current_menu == options_menu)
315 process_options_menu();
317 else if(current_menu == load_game_menu)
319 process_save_load_game_menu(false);
324 /* Set the time of the last update and the time of the current update */
325 last_update_time = update_time;
326 update_time = st_get_ticks();
335 level_free(¤t_level);
336 texture_free(&bkg_title);
337 texture_free(&anim1);
338 texture_free(&anim2);
339 string_list_free(&level_subsets);
341 /* Return to main! */
350 void display_credits()
359 string_list_type names;
361 string_list_init(&names);
362 sprintf(filename,"%s/CREDITS", datadir.c_str());
363 if((fi = fopen(filename,"r")) != NULL)
365 while(fgets(temp, sizeof(temp), fi) != NULL)
367 temp[strlen(temp)-1]='\0';
368 string_list_add_item(&names,temp);
374 string_list_add_item(&names,"Credits were not found!");
375 string_list_add_item(&names,"Shame on the guy, who");
376 string_list_add_item(&names,"forgot to include them");
377 string_list_add_item(&names,"in your SuperTux distribution.");
381 timer_init(&timer, SDL_GetTicks());
382 timer_start(&timer, 50);
390 length = names.num_items;
392 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
396 /* in case of input, exit */
397 while(SDL_PollEvent(&event))
401 switch(event.key.keysym.sym)
430 else if(speed < -MAX_VEL)
433 /* draw the credits */
437 text_drawf(&white_big_text, "- Credits -", 0, screen->h-scroll, A_HMIDDLE, A_TOP, 2);
439 for(i = 0, n = 0, d = 0; i < length; i++,n++,d++)
441 if(names.item[i] == "")
445 if(names.item[i][0] == ' ')
446 text_drawf(&white_small_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll-10, A_HMIDDLE, A_TOP, 1);
447 else if(names.item[i][0] == ' ')
448 text_drawf(&white_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
449 else if(names.item[i+1][0] == '-' || names.item[i][0] == '-')
450 text_drawf(&white_big_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 3);
452 text_drawf(&blue_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
457 texture_draw_part(&bkg_title, 0, 0, 0, 0, 640, 130);
461 if(60+screen->h+(n*18)+(d*18)-scroll < 0 && 20+60+screen->h+(n*18)+(d*18)-scroll < 0)
470 if(timer_get_left(&timer) < 0)
473 timer_start(&timer, 50);
476 string_list_free(&names);
478 SDL_EnableKeyRepeat(0, 0); // disables key repeating
480 Menu::set_current(main_menu);