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 // Wrap around at the end of the level back to the beginnig
112 if(current_level.width * 32 - 320 < titletux.base.x)
114 titletux.base.x = titletux.base.x - (current_level.width * 32 - 640);
115 scroll_x = titletux.base.x - 320;
118 float last_tux_x_pos = titletux.base.x;
121 // Jump if tux stays in the same position for one loop, ie. if he is
122 // stuck behind a wall
123 if (last_tux_x_pos == titletux.base.x)
131 /* --- TITLE SCREEN --- */
136 string_list_type level_subsets;
138 level_subsets = dsubdirs("/levels", "info");
139 timer_init(&random_timer, true);
144 st_pause_ticks_init();
146 level_load(¤t_level, (datadir + "/levels/misc/menu.stl").c_str());
148 activate_particle_systems();
149 /* Lower the gravity that tux doesn't jump to hectically through the demo */
152 /* Reset menu variables */
154 Menu::set_current(main_menu);
156 clearscreen(0, 0, 0);
161 texture_load(&bkg_title,datadir + "/images/title/background.jpg", IGNORE_ALPHA);
162 texture_load(&logo,datadir + "/images/title/logo.png", USE_ALPHA);
163 texture_load(&img_choose_subset,datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
165 /* --- Main title loop: --- */
172 /* Draw the title background: */
173 texture_draw_bg(&bkg_title);
176 update_time = st_get_ticks();
177 timer_start(&random_timer, rand() % 2000 + 2000);
179 while (!done && !quit)
182 /* Calculate the movement-factor */
183 frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
184 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
185 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
186 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
191 while (SDL_PollEvent(&event))
194 if (event.type == SDL_QUIT)
196 /* Quit event - quit: */
199 else if (event.type == SDL_KEYDOWN)
203 key = event.key.keysym.sym;
205 /* Check for menu events */
208 if (key == SDLK_ESCAPE)
217 /* Draw the background: */
221 if (current_menu == main_menu)
222 texture_draw(&logo, 160, 30);
224 text_draw(&white_small_text,
225 " SuperTux " VERSION "\n"
226 "Copyright (c) 2003 SuperTux Devel Team\n"
227 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
228 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
232 /* Draw the high score: */
234 sprintf(str, "High score: %d", hs_score);
235 text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1);
236 sprintf(str, "by %s", hs_name);
237 text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1);
240 /* Don't draw menu, if quit is true */
241 if(show_menu && !quit)
242 menu_process_current();
244 if(current_menu == main_menu)
246 switch (main_menu->check())
254 if(level_subsets.num_items != 0)
256 subset.load(level_subsets.item[0]);
259 texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0);
260 if(level_subsets.num_items != 0)
262 texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78);
263 if(level_subsets.num_items > 1)
266 texture_draw(&arrow_left,(screen->w / 2) - ((subset.title.length()+2)*16)/2,20);
267 if(i < level_subsets.num_items-1)
268 texture_draw(&arrow_right,(screen->w / 2) + ((subset.description.length())*16)/2,20);
270 text_drawf(&gold_text, subset.title.c_str(), 0, 20, A_HMIDDLE, A_TOP, 1);
271 text_drawf(&gold_text, subset.description.c_str(), 20, -20, A_HMIDDLE, A_BOTTOM, 1);
275 while(SDL_PollEvent(&event) && !done)
283 case SDL_KEYDOWN: // key pressed
285 key = event.key.keysym.sym;
293 subset.load(level_subsets.item[i]);
296 else if(key == SDLK_RIGHT)
298 if(i < level_subsets.num_items -1)
302 subset.load(level_subsets.item[i]);
305 else if(key == SDLK_SPACE || key == SDLK_RETURN)
308 quit = gameloop(subset.name.c_str(),1,ST_GL_PLAY);
311 else if(key == SDLK_ESCAPE)
324 titletux.level_begin();
325 update_time = st_get_ticks();
329 // Start Game, ie. goto the slots menu
330 update_load_save_game_menu(load_game_menu, true);
337 quit = leveleditor(1);
347 else if(current_menu == options_menu)
349 process_options_menu();
351 else if(current_menu == load_game_menu)
353 if (process_load_game_menu())
357 titletux.level_begin();
358 update_time = st_get_ticks();
361 else if(current_menu == contrib_menu)
366 mouse_cursor->draw();
370 /* Set the time of the last update and the time of the current update */
371 last_update_time = update_time;
372 update_time = st_get_ticks();
381 texture_free(&bkg_title);
383 string_list_free(&level_subsets);
385 /* Return to main! */
394 void display_credits()
403 string_list_type names;
405 string_list_init(&names);
406 sprintf(filename,"%s/CREDITS", datadir.c_str());
407 if((fi = fopen(filename,"r")) != NULL)
409 while(fgets(temp, sizeof(temp), fi) != NULL)
411 temp[strlen(temp)-1]='\0';
412 string_list_add_item(&names,temp);
418 string_list_add_item(&names,"Credits were not found!");
419 string_list_add_item(&names,"Shame on the guy, who");
420 string_list_add_item(&names,"forgot to include them");
421 string_list_add_item(&names,"in your SuperTux distribution.");
425 timer_init(&timer, SDL_GetTicks());
426 timer_start(&timer, 50);
434 length = names.num_items;
436 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
440 /* in case of input, exit */
441 while(SDL_PollEvent(&event))
445 switch(event.key.keysym.sym)
474 else if(speed < -MAX_VEL)
477 /* draw the credits */
481 text_drawf(&white_big_text, "- Credits -", 0, screen->h-scroll, A_HMIDDLE, A_TOP, 2);
483 for(i = 0, n = 0, d = 0; i < length; i++,n++,d++)
485 if(names.item[i] == "")
489 if(names.item[i][0] == ' ')
490 text_drawf(&white_small_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll-10, A_HMIDDLE, A_TOP, 1);
491 else if(names.item[i][0] == ' ')
492 text_drawf(&white_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
493 else if(names.item[i+1][0] == '-' || names.item[i][0] == '-')
494 text_drawf(&white_big_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 3);
496 text_drawf(&blue_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
502 if(60+screen->h+(n*18)+(d*18)-scroll < 0 && 20+60+screen->h+(n*18)+(d*18)-scroll < 0)
511 if(timer_get_left(&timer) < 0)
514 timer_start(&timer, 50);
517 string_list_free(&names);
519 SDL_EnableKeyRepeat(0, 0); // disables key repeating
521 Menu::set_current(main_menu);