bonusblock can now contain custom MovingObjects, added possibility to execute script...
[supertux.git] / src / title.cpp
index 12c58e2..9afb667 100644 (file)
 #include <ctype.h>
 #endif
 
-#include "app/globals.h"
 #include "title.h"
 #include "video/screen.h"
 #include "video/surface.h"
 #include "gui/menu.h"
 #include "timer.h"
-#include "special/frame_rate.h"
-#include "app/setup.h"
 #include "lisp/lisp.h"
 #include "lisp/parser.h"
 #include "level.h"
 #include "object/camera.h"
 #include "object/player.h"
 #include "resources.h"
-#include "app/gettext.h"
+#include "gettext.h"
 #include "misc.h"
 #include "textscroller.h"
+#include "file_system.h"
 #include "control/joystickkeyboardcontroller.h"
 #include "control/codecontroller.h"
 #include "main.h"
 
 static Surface* bkg_title;
 static Surface* logo;
-static Surface* img_choose_subset;
+//static Surface* img_choose_subset;
 
 static bool walking;
-static Timer2 random_timer;
+static Timer random_timer;
 
 static int frame;
 
@@ -81,8 +79,6 @@ static CodeController* controller;
 static std::vector<LevelSubset*> contrib_subsets;
 static LevelSubset* current_contrib_subset = 0;
 
-static FrameRate frame_rate(100);
-
 /* If the demo was stopped - because game started, level
    editor was excuted, etc - call this when you get back
    to the title code.
@@ -93,7 +89,7 @@ void resume_demo()
   titlesession->get_current_sector()->activate("main");
   titlesession->set_current();
 
-  frame_rate.update();
+  //frame_rate.update();
 }
 
 void update_load_save_game_menu(Menu* menu)
@@ -136,7 +132,7 @@ void generate_contrib_menu()
         delete subset;
         continue;
       }
-      contrib_menu->add_submenu(subset->title, contrib_subset_menu);
+      contrib_menu->add_submenu(subset->title, contrib_subset_menu, i);
       contrib_subsets.push_back(subset);
       ++i;
     }
@@ -189,14 +185,12 @@ void check_levels_contrib_menu()
 
     // TODO: slots should be available for contrib maps
     worldmap.loadgame(user_dir + "/save/" + subset.name + "-slot1.stsg");
-
     worldmap.display();  // run the map
 
     Menu::set_current(main_menu);
     resume_demo();
   } else if (current_subset != index) {
     current_subset = index;
-    // FIXME: This shouln't be busy looping
     LevelSubset& subset = * (contrib_subsets[index]);
 
     current_contrib_subset = &subset;
@@ -225,25 +219,22 @@ void check_levels_contrib_menu()
 void check_contrib_subset_menu()
 {
   int index = contrib_subset_menu->check();
-  if (index != -1)
-    {
-      if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
-        {
-          std::cout << "Starting level: " << index << std::endl;
-          
-          GameSession session(
-              current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
-          session.run();
-          player_status.reset();
-          Menu::set_current(main_menu);
-          resume_demo();
-        }
-    }  
+  if (index != -1) {
+    if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
+      GameSession session(
+          current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
+      session.run();
+      player_status.reset();
+      Menu::set_current(main_menu);
+      resume_demo();
+    }
+  }  
 }
 
 void draw_demo(float elapsed_time)
 {
   static float last_tux_x_pos = -1;
+  static float last_tux_y_pos = -1;
   Sector* sector  = titlesession->get_current_sector();
   Player* tux = sector->player;
 
@@ -252,14 +243,19 @@ void draw_demo(float elapsed_time)
   controller->update();
   controller->press(Controller::RIGHT);
   
-  if(random_timer.check() || (int) last_tux_x_pos == (int) tux->get_pos().x) {
-    random_timer.start(float(rand() % 3000 + 3000) / 1000.);
-    walking = !walking;
+  if(random_timer.check() || 
+      (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
+    walking = false;
   } else {
-      if(!walking)
-        controller->press(Controller::JUMP);
+      if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
+        random_timer.start(float(rand() % 3000 + 3000) / 1000.);
+        walking = true;
+      }
   }
+  if(!walking)
+    controller->press(Controller::JUMP);
   last_tux_x_pos = tux->get_pos().x;
+  last_tux_y_pos = tux->get_pos().y;
 
   // Wrap around at the end of the level back to the beginnig
   if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
@@ -267,7 +263,7 @@ void draw_demo(float elapsed_time)
     sector->camera->reset(tux->get_pos());
   }
 
-  sector->action(elapsed_time);
+  sector->update(elapsed_time);
   sector->draw(*titlesession->context);
 }
 
@@ -275,19 +271,17 @@ void draw_demo(float elapsed_time)
 void title()
 {
   walking = true;
-  LevelEditor* leveleditor;
+  //LevelEditor* leveleditor;
   MusicRef credits_music;
   controller = new CodeController();
 
-  Ticks::pause_init();
-
   titlesession = new GameSession(get_resource_filename("levels/misc/menu.stl"),
       ST_GL_DEMO_GAME);
 
   /* Load images: */
   bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
-  logo = new Surface(datadir + "/images/title/logo.png", true);
-  img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
+  logo = new Surface(datadir + "/images/engine/menu/logo.png", true);
+  //img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
 
   titlesession->get_current_sector()->activate("main");
   titlesession->set_current();
@@ -304,7 +298,8 @@ void title()
   
   Menu::set_current(main_menu);
   DrawingContext& context = *titlesession->context;
-  while (Menu::current())
+  bool running = true;
+  while (running)
     {
       // Calculate the movement-factor
       Uint32 ticks = SDL_GetTicks();
@@ -326,7 +321,6 @@ void title()
           Menu::current()->event(event);
         }
         main_controller->process_event(event);
-        // FIXME: QUIT signal should be handled more generic, not locally
         if (event.type == SDL_QUIT)
           throw std::runtime_error("Received window close");
       }
@@ -342,7 +336,7 @@ void title()
               Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
       context.draw_text(white_small_text,
         _(
-"Copyright (c) 2003 SuperTux Devel Team\n"
+"Copyright (c) 2005 SuperTux Devel Team\n"
 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
 "redistribute it under certain conditions; see the file COPYING for details.\n"
         ),
@@ -354,7 +348,7 @@ void title()
       if(menu)
         {
           menu->draw(context);
-          menu->action();
+          menu->update();
          
           if(menu == main_menu)
             {
@@ -366,17 +360,17 @@ void title()
                   break;
                 case MNID_LEVELS_CONTRIB:
                   // Contrib Menu
-                  puts("Entering contrib menu");
                   generate_contrib_menu();
                   break;
 #if 0
-                case MNID_LEVELEDITOR:
-                  leveleditor = new LevelEditor();
+                case MNID_LEVELEDITOR: {
+                  LevelEdtiro* leveleditor = new LevelEditor();
                   leveleditor->run();
                   delete leveleditor;
                   Menu::set_current(main_menu);
                   resume_demo();
                   break;
+                }
 #endif
                 case MNID_CREDITS:
                   fadeout(500);
@@ -388,7 +382,7 @@ void title()
                   Menu::set_current(main_menu);
                   break;
                 case MNID_QUITMAINMENU:
-                  Menu::set_current(0);
+                  running = false;
                   break;
                 }
             }
@@ -431,11 +425,15 @@ void title()
             }
         }
 
-      mouse_cursor->draw(context);
-     
+      // reopen menu of user closed it (so that the app doesn't close when user
+      // accidently hit ESC)
+      if(Menu::current() == 0) {
+        Menu::set_current(main_menu);
+      }
+
       context.do_drawing();
 
-      frame_rate.update();
+      //frame_rate.update();
 
       /* Pause: */
       frame++;
@@ -446,5 +444,5 @@ void title()
   delete titlesession;
   delete bkg_title;
   delete logo;
-  delete img_choose_subset;
+  //delete img_choose_subset;
 }