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 #include "resources.h"
44 static texture_type bkg_title;
45 static texture_type logo;
46 static texture_type img_choose_subset;
49 static timer_type random_timer;
51 static SDL_Event event;
54 static unsigned int last_update_time;
55 static unsigned int update_time;
57 void display_credits();
59 void draw_background()
61 /* Draw the title background: */
63 texture_draw_bg(&bkg_title);
66 void draw_demo(GameSession* session)
68 World::set_current(session->get_world());
69 //World* world = session->get_world();
70 Level* plevel = session->get_level();
71 Player* tux = session->get_world()->get_tux();
74 // update particle systems
75 std::vector<ParticleSystem*>::iterator p;
76 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
78 (*p)->simulate(frame_ratio);
81 // Draw particle systems (background)
82 for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
84 (*p)->draw(scroll_x, 0, 0);
88 // Draw interactive tiles:
89 for (int y = 0; y < 15; ++y)
91 for (int x = 0; x < 21; ++x)
93 Tile::draw(32*x - fmodf(scroll_x, 32), y * 32,
94 plevel->ia_tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
98 global_frame_counter++;
99 tux->key_event(SDLK_RIGHT,DOWN);
101 if(timer_check(&random_timer))
104 tux->key_event(SDLK_UP,UP);
106 tux->key_event(SDLK_UP,DOWN);
110 timer_start(&random_timer, rand() % 3000 + 3000);
114 // Wrap around at the end of the level back to the beginnig
115 if(plevel->width * 32 - 320 < tux->base.x)
117 tux->base.x = tux->base.x - (plevel->width * 32 - 640);
118 scroll_x = tux->base.x - 320;
121 float last_tux_x_pos = tux->base.x;
124 // Jump if tux stays in the same position for one loop, ie. if he is
125 // stuck behind a wall
126 if (last_tux_x_pos == tux->base.x)
134 /* --- TITLE SCREEN --- */
137 string_list_type level_subsets;
139 level_subsets = dsubdirs("/levels", "info");
140 timer_init(&random_timer, true);
144 st_pause_ticks_init();
146 GameSession session(datadir + "/levels/misc/menu.stl");
149 //FIXME:activate_particle_systems();
151 /* Reset menu variables */
153 Menu::set_current(main_menu);
155 clearscreen(0, 0, 0);
160 texture_load(&bkg_title,datadir + "/images/title/background.jpg", IGNORE_ALPHA);
161 texture_load(&logo,datadir + "/images/title/logo.png", USE_ALPHA);
162 texture_load(&img_choose_subset,datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
164 /* --- Main title loop: --- */
169 /* Draw the title background: */
170 texture_draw_bg(&bkg_title);
173 update_time = st_get_ticks();
174 timer_start(&random_timer, rand() % 2000 + 2000);
178 /* Calculate the movement-factor */
179 frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
180 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
181 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
182 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
187 while (SDL_PollEvent(&event))
190 if (event.type == SDL_QUIT)
194 else if (event.type == SDL_KEYDOWN)
197 key = event.key.keysym.sym;
199 /* Check for menu events */
202 if (key == SDLK_ESCAPE)
210 /* Draw the background: */
214 if (current_menu == main_menu)
215 texture_draw(&logo, 160, 30);
217 text_draw(&white_small_text,
218 " SuperTux " VERSION "\n"
219 "Copyright (c) 2003 SuperTux Devel Team\n"
220 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
221 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
225 /* Draw the high score: */
227 sprintf(str, "High score: %d", hs_score);
228 text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1);
229 sprintf(str, "by %s", hs_name);
230 text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1);
233 /* Don't draw menu, if quit is true */
234 if(show_menu && !done)
235 menu_process_current();
237 if(current_menu == main_menu)
239 switch (main_menu->check())
247 if(level_subsets.num_items != 0)
249 subset.load(level_subsets.item[0]);
252 texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0);
253 if(level_subsets.num_items != 0)
255 texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78);
256 if(level_subsets.num_items > 1)
259 texture_draw(&arrow_left,(screen->w / 2) - ((subset.title.length()+2)*16)/2,20);
260 if(i < level_subsets.num_items-1)
261 texture_draw(&arrow_right,(screen->w / 2) + ((subset.description.length())*16)/2,20);
263 text_drawf(&gold_text, subset.title.c_str(), 0, 20, A_HMIDDLE, A_TOP, 1);
264 text_drawf(&gold_text, subset.description.c_str(), 20, -20, A_HMIDDLE, A_BOTTOM, 1);
268 while(SDL_PollEvent(&event) && !done)
275 case SDL_KEYDOWN: // key pressed
277 key = event.key.keysym.sym;
285 subset.load(level_subsets.item[i]);
288 else if(key == SDLK_RIGHT)
290 if(i < level_subsets.num_items -1)
294 subset.load(level_subsets.item[i]);
297 else if(key == SDLK_SPACE || key == SDLK_RETURN)
300 quit = gameloop(subset.name.c_str(),1,ST_GL_PLAY);
303 else if(key == SDLK_ESCAPE)
316 titletux.level_begin();
317 update_time = st_get_ticks();
321 // Start Game, ie. goto the slots menu
322 update_load_save_game_menu(load_game_menu, true);
329 done = leveleditor(1);
339 else if(current_menu == options_menu)
341 process_options_menu();
343 else if(current_menu == load_game_menu)
345 if (process_load_game_menu())
347 // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
350 //titletux.level_begin();
351 update_time = st_get_ticks();
354 else if(current_menu == contrib_menu)
359 mouse_cursor->draw();
363 /* Set the time of the last update and the time of the current update */
364 last_update_time = update_time;
365 update_time = st_get_ticks();
374 texture_free(&bkg_title);
376 string_list_free(&level_subsets);
378 /* Return to main! */
386 void display_credits()
395 string_list_type names;
397 string_list_init(&names);
398 sprintf(filename,"%s/CREDITS", datadir.c_str());
399 if((fi = fopen(filename,"r")) != NULL)
401 while(fgets(temp, sizeof(temp), fi) != NULL)
403 temp[strlen(temp)-1]='\0';
404 string_list_add_item(&names,temp);
410 string_list_add_item(&names,"Credits were not found!");
411 string_list_add_item(&names,"Shame on the guy, who");
412 string_list_add_item(&names,"forgot to include them");
413 string_list_add_item(&names,"in your SuperTux distribution.");
417 timer_init(&timer, SDL_GetTicks());
418 timer_start(&timer, 50);
426 length = names.num_items;
428 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
432 /* in case of input, exit */
433 while(SDL_PollEvent(&event))
437 switch(event.key.keysym.sym)
466 else if(speed < -MAX_VEL)
469 /* draw the credits */
473 text_drawf(&white_big_text, "- Credits -", 0, screen->h-scroll, A_HMIDDLE, A_TOP, 2);
475 for(i = 0, n = 0, d = 0; i < length; i++,n++,d++)
477 if(names.item[i] == "")
481 if(names.item[i][0] == ' ')
482 text_drawf(&white_small_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll-10, A_HMIDDLE, A_TOP, 1);
483 else if(names.item[i][0] == ' ')
484 text_drawf(&white_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
485 else if(names.item[i+1][0] == '-' || names.item[i][0] == '-')
486 text_drawf(&white_big_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 3);
488 text_drawf(&blue_text, names.item[i], 0, 60+screen->h+(n*18)+(d*18)-scroll, A_HMIDDLE, A_TOP, 1);
494 if(60+screen->h+(n*18)+(d*18)-scroll < 0 && 20+60+screen->h+(n*18)+(d*18)-scroll < 0)
503 if(timer_get_left(&timer) < 0)
506 timer_start(&timer, 50);
509 string_list_free(&names);
511 SDL_EnableKeyRepeat(0, 0); // disables key repeating
513 Menu::set_current(main_menu);