Corrected collition with bag of money
[supertux.git] / src / gameloop.c
index f8ed6e5..28a9956 100644 (file)
@@ -3,18 +3,19 @@
   
   Super Tux - Game Loop!
   
-  by Bill Kendrick
+  by Bill Kendrick & Tobias Glaesser
   bill@newbreedsoftware.com
   http://www.newbreedsoftware.com/supertux/
   
-  April 11, 2000 - December 9, 2003
-*/
+  April 11, 2000 - February 1st, 2004
+ */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#include <time.h>
 #include <SDL.h>
 
 #ifdef LINUX
 #include "globals.h"
 #include "gameloop.h"
 #include "screen.h"
-#include "sound.h"
 #include "setup.h"
 #include "high_scores.h"
 #include "menu.h"
 #include "badguy.h"
 #include "world.h"
+#include "special.h"
 #include "player.h"
+#include "level.h"
+#include "scene.h"
+#include "collision.h"
 
-/* Sound files: */
-
-enum {
-  SND_JUMP,
-  SND_BIGJUMP,
-  SND_SKID,
-  SND_DISTRO,
-  SND_HERRING,
-  SND_BRICK,
-  SND_HURT,
-  SND_SQUISH,
-  SND_FALL,
-  SND_RICOCHET,
-  SND_BUMP_UPGRADE,
-  SND_UPGRADE,
-  SND_EXCELLENT,
-  SND_COFFEE,
-  SND_SHOOT,
-  SND_LIFEUP
-};
-
-
-char * soundfilenames[NUM_SOUNDS] = {
-                                      DATA_PREFIX "/sounds/jump.wav",
-                                      DATA_PREFIX "/sounds/bigjump.wav",
-                                      DATA_PREFIX "/sounds/skid.wav",
-                                      DATA_PREFIX "/sounds/distro.wav",
-                                      DATA_PREFIX "/sounds/herring.wav",
-                                      DATA_PREFIX "/sounds/brick.wav",
-                                      DATA_PREFIX "/sounds/hurt.wav",
-                                      DATA_PREFIX "/sounds/squish.wav",
-                                      DATA_PREFIX "/sounds/fall.wav",
-                                      DATA_PREFIX "/sounds/ricochet.wav",
-                                      DATA_PREFIX "/sounds/bump-upgrade.wav",
-                                      DATA_PREFIX "/sounds/upgrade.wav",
-                                      DATA_PREFIX "/sounds/excellent.wav",
-                                      DATA_PREFIX "/sounds/coffee.wav",
-                                      DATA_PREFIX "/sounds/shoot.wav",
-                                      DATA_PREFIX "/sounds/lifeup.wav"
-                                    };
+/* extern variables */
 
+extern char* soundfilenames[NUM_SOUNDS];
+st_level current_level;
 
 /* Local variables: */
 
-int score, highscore, distros, level, lives, scroll_x, next_level, game_pause,
-done, quit, tux_dir, tux_size, tux_duck, tux_x, tux_xm, tux_y, tux_ym,
-tux_dying, tux_safe, jumping, jump_counter, frame, score_multiplier,
-tux_frame_main, tux_frame, tux_got_coffee, tux_skidding,
-super_bkgd_time, time_left, tux_invincible_time, endpos,
-counting_distros, distro_counter;
-int bkgd_red, bkgd_green, bkgd_blue, level_width;
-int left, right, up, down, fire, old_fire;
-SDL_Surface * img_brick[2], * img_solid[4], * img_distro[4],
-* img_waves[3], * img_water, * img_pole, * img_poletop, * img_flag[2];
-SDL_Surface * img_bkgd[2][4];
-SDL_Surface * img_golden_herring;
-SDL_Surface * img_bsod_left[4], * img_bsod_right[4],
-* img_laptop_left[3], * img_laptop_right[3],
-* img_money_left[2], * img_money_right[2];
-SDL_Surface * img_bsod_squished_left, * img_bsod_squished_right,
-* img_bsod_falling_left, * img_bsod_falling_right,
-* img_laptop_flat_left, * img_laptop_flat_right,
-* img_laptop_falling_left, * img_laptop_falling_right;
-SDL_Surface * img_box_full, * img_box_empty, * img_mints, * img_coffee,
-* img_super_bkgd, * img_bullet, * img_red_glow;
-SDL_Surface * img_cloud[2][4];
-SDL_Surface * tux_right[3], * tux_left[3],
-* bigtux_right[3], * bigtux_left[3],
-* bigtux_right_jump, * bigtux_left_jump,
-* cape_right[2], * cape_left[2],
-* bigcape_right[2], * bigcape_left[2],
-* ducktux_right, * ducktux_left,
-* skidtux_right, * skidtux_left, * tux_life;
+texture_type img_waves[3], img_water, img_pole, img_poletop, img_flag[2];
+texture_type img_cloud[2][4];
 SDL_Event event;
-SDL_Rect src, dest;
 SDLKey key;
-unsigned char * tiles[15];
-bouncy_distro_type bouncy_distros[NUM_BOUNCY_DISTROS];
-broken_brick_type broken_bricks[NUM_BROKEN_BRICKS];
-bouncy_brick_type bouncy_bricks[NUM_BOUNCY_BRICKS];
-bad_guy_type bad_guys[NUM_BAD_GUYS];
-floating_score_type floating_scores[NUM_FLOATING_SCORES];
-upgrade_type upgrades[NUM_UPGRADES];
-bullet_type bullets[NUM_BULLETS];
-char song_title[20];
-char levelname[20];
-char leveltheme[20];
-char str[10];
+char level_subset[100];
+char str[60];
+float fps_fps;
+int st_gl_mode;
+unsigned int last_update_time;
+unsigned int update_time;
+int pause_menu_frame;
 
 /* Local function prototypes: */
 
+void levelintro(void);
 void initgame(void);
-void loadlevel(void);
-void loadlevelgfx(void);
-void loadlevelsong(void);
-void unloadlevelgfx(void);
-void unloadlevelsong(void);
 void loadshared(void);
 void unloadshared(void);
-void drawshape(int x, int y, unsigned char c);
-void savegame(void);
-unsigned char shape(int x, int y, int sx);
-int issolid(int x, int y, int sx);
-int isbrick(int x, int y, int sx);
-int isice(int x, int y, int sx);
-int isfullbox(int x, int y, int sx);
-void change(int x, int y, int sx, unsigned char c);
-void trybreakbrick(int x, int y, int sx);
-void bumpbrick(int x, int y, int sx);
-void tryemptybox(int x, int y, int sx);
-void trygrabdistro(int x, int y, int sx, int bounciness);
-void add_bouncy_distro(int x, int y);
-void add_broken_brick(int x, int y);
-void add_broken_brick_piece(int x, int y, int xm, int ym);
-void add_bouncy_brick(int x, int y);
-void add_bad_guy(int x, int y, int kind);
-void add_score(int x, int y, int s);
-void trybumpbadguy(int x, int y, int sx);
-void add_upgrade(int x, int y, int kind);
-void killtux(int mode);
-void add_bullet(int x, int y, int dir, int xm);
+void drawstatus(void);
 void drawendscreen(void);
 void drawresultscreen(void);
 
+void levelintro(void)
+{
+  /* Level Intro: */
+
+  clearscreen(0, 0, 0);
+
+  sprintf(str, "LEVEL %d", level);
+  text_drawf(&blue_text, str, 0, 200, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
+
+  sprintf(str, "%s", current_level.name);
+  text_drawf(&gold_text, str, 0, 224, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
+
+  sprintf(str, "TUX x %d", tux.lives);
+  text_drawf(&white_text, str, 0, 256, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
+
+  flipscreen();
+
+  SDL_Delay(1000);
+}
+
+/* Reset Timers */
+void start_timers(void)
+{
+  timer_start(&time_left,current_level.time_left*1000);
+  st_pause_ticks_init();
+  update_time = st_get_ticks();
+}
+
+void activate_bad_guys(void)
+{
+  int x,y;
+
+  /* Activate bad guys: */
+
+  for (y = 0; y < 15; y++)
+    {
+      for (x = 0; x < current_level.width; x++)
+        {
+          if (current_level.tiles[y][x] >= '0' && current_level.tiles[y][x] <= '9')
+            {
+              add_bad_guy(x * 32, y * 32, current_level.tiles[y][x] - '0');
+              current_level.tiles[y][x] = '.';
+            }
+        }
+    }
+
+}
+
 /* --- GAME EVENT! --- */
 
 void game_event(void)
 {
-
   while (SDL_PollEvent(&event))
     {
-
-      if (event.type == SDL_QUIT)
+      switch(event.type)
         {
-          /* Quit event - quit: */
-
+        case SDL_QUIT:        /* Quit event - quit: */
           quit = 1;
-        }
-      else if (event.type == SDL_KEYDOWN)
-        {
-          /* A keypress! */
-
+          break;
+        case SDL_KEYDOWN:     /* A keypress! */
           key = event.key.keysym.sym;
 
           /* Check for menu-events, if the menu is shown */
           if(show_menu)
-            menu_event(key);
+            menu_event(&event.key.keysym);
 
-          if (key == SDLK_ESCAPE)
+          if(player_key_event(&tux,key,DOWN))
+            break;
+
+          switch(key)
             {
-              /* Escape: Open/Close the menu: */
+            case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
               if(!game_pause)
                 {
-                  if(show_menu)
-                    show_menu = 0;
+                  if(st_gl_mode == ST_GL_TEST)
+                    quit = 1;
+                  else if(show_menu)
+                    {
+                     menu_set_current(&game_menu);
+                      show_menu = 0;
+                      st_pause_ticks_stop();
+                    }
                   else
-                    show_menu = 1;
+                    {
+                      show_menu = 1;
+                      st_pause_ticks_start();
+                    }
                 }
+              break;
+            default:
+              break;
             }
-          else if (key == SDLK_RIGHT)
-            {
-              right = DOWN;
-            }
-          else if (key == SDLK_LEFT)
-            {
-              left = DOWN;
-            }
-          else if (key == SDLK_UP)
-            {
-              up = DOWN;
-            }
-          else if (key == SDLK_DOWN)
-            {
-              down = DOWN;
-            }
-          else if (key == SDLK_LCTRL)
-            {
-              fire = DOWN;
-            }
-        }
-      else if (event.type == SDL_KEYUP)
-        {
-          /* A keyrelease! */
-
+          break;
+        case SDL_KEYUP:      /* A keyrelease! */
           key = event.key.keysym.sym;
 
-          if (key == SDLK_RIGHT)
-            {
-              right = UP;
-            }
-          else if (key == SDLK_LEFT)
-            {
-              left = UP;
-            }
-          else if (key == SDLK_UP)
-            {
-              up = UP;
-            }
-          else if (key == SDLK_DOWN)
-            {
-              down = UP;
-            }
-          else if (key == SDLK_LCTRL)
-            {
-              fire = UP;
-            }
-          else if (key == SDLK_p)
+          if(player_key_event(&tux,key,UP))
+            break;
+
+          switch(key)
             {
+            case SDLK_p:
               if(!show_menu)
                 {
                   if(game_pause)
-                    game_pause = 0;
+                    {
+                      game_pause = 0;
+                      st_pause_ticks_stop();
+                    }
                   else
-                    game_pause = 1;
+                    {
+                      game_pause = 1;
+                      st_pause_ticks_start();
+                    }
                 }
+              break;
+            case SDLK_TAB:
+              if(debug_mode == YES)
+                tux.size = !tux.size;
+              break;
+            case SDLK_END:
+              if(debug_mode == YES)
+                distros += 50;
+              break;
+            case SDLK_SPACE:
+              if(debug_mode == YES)
+                next_level = 1;
+              break;
+            case SDLK_DELETE:
+              if(debug_mode == YES)
+                tux.got_coffee = 1;
+              break;
+            case SDLK_INSERT:
+              if(debug_mode == YES)
+                timer_start(&tux.invincible_timer,TUX_INVINCIBLE_TIME);
+              break;
+            case SDLK_l:
+              if(debug_mode == YES)
+                --tux.lives;
+              break;
+            case SDLK_s:
+              if(debug_mode == YES)
+                score += 1000;
+              break;
+            default:
+              break;
             }
-          else if (key == SDLK_TAB && debug_mode == YES)
-            {
-              tux_size = !tux_size;
-            }
-          else if (key == SDLK_END && debug_mode == YES)
-            {
-              distros += 50;
-            }
-          else if (key == SDLK_SPACE && debug_mode == YES)
-            {
-              next_level = 1;
-            }
-        }
+          break;
 #ifdef JOY_YES
-      else if (event.type == SDL_JOYAXISMOTION)
-        {
-          if (event.jaxis.axis == JOY_X)
-            {
-              if (event.jaxis.value < -256)
-                left = DOWN;
-              else
-                left = UP;
-
-              if (event.jaxis.value > 256)
-                right = DOWN;
-              else
-                right = UP;
-            }
-          else if (event.jaxis.axis == JOY_Y)
+
+        case SDL_JOYAXISMOTION:
+          switch(event.jaxis.axis)
             {
-              if (event.jaxis.value > 256)
-                down = DOWN;
-              else
-                down = UP;
+            case JOY_X:
+              if (event.jaxis.value < -1024)
+                tux.input.left = DOWN;
+              else if (event.jaxis.value > 1024)
+                tux.input.left = UP;
+
+              if (event.jaxis.value > 1024)
+                tux.input.right = DOWN;
+              else if (event.jaxis.value < -1024)
+                tux.input.right = UP;
+              break;
+            case JOY_Y:
+              if (event.jaxis.value > 1024)
+                tux.input.down = DOWN;
+              else if (event.jaxis.value < -1024)
+                tux.input.down = UP;
 
               /* Handle joystick for the menu */
               if(show_menu)
                 {
-                  if(down == DOWN)
+                  if(tux.input.down == DOWN)
                     menuaction = MN_DOWN;
                   else
                     menuaction = MN_UP;
                 }
+              break;
+            default:
+              break;
             }
-        }
-      else if (event.type == SDL_JOYBUTTONDOWN)
-        {
+          break;
+        case SDL_JOYBUTTONDOWN:
           if (event.jbutton.button == JOY_A)
-            up = DOWN;
+            tux.input.up = DOWN;
           else if (event.jbutton.button == JOY_B)
-            fire = DOWN;
-        }
-      else if (event.type == SDL_JOYBUTTONUP)
-        {
+            tux.input.fire = DOWN;
+          break;
+        case SDL_JOYBUTTONUP:
           if (event.jbutton.button == JOY_A)
-            up = UP;
+            tux.input.up = UP;
           else if (event.jbutton.button == JOY_B)
-            fire = UP;
+            tux.input.fire = UP;
 
           if(show_menu)
             menuaction = MN_HIT;
+          break;
+        default:
+          break;
 
         }
 #endif
@@ -324,209 +280,13 @@ void game_event(void)
 
 int game_action(void)
 {
-  int i,j;
-
-  /* --- HANDLE TUX! --- */
-
-  /* Handle key and joystick state: */
-
-  if (!(tux_dying || next_level))
-    {
-      if (right == DOWN && left == UP)
-        {
-          if (jumping == NO)
-            {
-              if (tux_xm < -SKID_XM && !tux_skidding &&
-                  tux_dir == LEFT)
-                {
-                  tux_skidding = SKID_TIME;
-
-                  play_sound(sounds[SND_SKID]);
-
-                }
-              tux_dir = RIGHT;
-            }
-
-          if (tux_xm < 0 && !isice(tux_x, tux_y + 32, scroll_x) &&
-              !tux_skidding)
-            {
-              tux_xm = 0;
-            }
-
-          if (!tux_duck)
-            {
-              if (tux_dir == RIGHT)
-                {
-                  /* Facing the direction we're jumping?  Go full-speed: */
-
-                  if (fire == UP)
-                    {
-                      tux_xm = tux_xm + WALK_SPEED;
-
-                      if (tux_xm > MAX_WALK_XM)
-                        tux_xm = MAX_WALK_XM;
-                    }
-                  else if (fire == DOWN)
-                    {
-                      tux_xm = tux_xm + RUN_SPEED;
-
-                      if (tux_xm > MAX_RUN_XM)
-                        tux_xm = MAX_RUN_XM;
-                    }
-                }
-              else
-                {
-                  /* Not facing the direction we're jumping?
-                  Go half-speed: */
-
-                  tux_xm = tux_xm + WALK_SPEED / 2;
-
-                  if (tux_xm > MAX_WALK_XM / 2)
-                    tux_xm = MAX_WALK_XM / 2;
-                }
-            }
-        }
-      else if (left == DOWN && right == UP)
-        {
-          if (jumping == NO)
-            {
-              if (tux_xm > SKID_XM && !tux_skidding &&
-                  tux_dir == RIGHT)
-                {
-                  tux_skidding = SKID_TIME;
-                  play_sound(sounds[SND_SKID]);
-                }
-              tux_dir = LEFT;
-            }
-
-          if (tux_xm > 0 && !isice(tux_x, tux_y + 32, scroll_x) &&
-              !tux_skidding)
-            {
-              tux_xm = 0;
-            }
-
-          if (!tux_duck)
-            {
-              if (tux_dir == LEFT)
-                {
-                  /* Facing the direction we're jumping?  Go full-speed: */
-
-                  if (fire == UP)
-                    {
-                      tux_xm = tux_xm - WALK_SPEED;
-
-                      if (tux_xm < -MAX_WALK_XM)
-                        tux_xm = -MAX_WALK_XM;
-                    }
-                  else if (fire == DOWN)
-                    {
-                      tux_xm = tux_xm - RUN_SPEED;
-
-                      if (tux_xm < -MAX_RUN_XM)
-                        tux_xm = -MAX_RUN_XM;
-                    }
-                }
-              else
-                {
-                  /* Not facing the direction we're jumping?
-                  Go half-speed: */
-
-                  tux_xm = tux_xm - WALK_SPEED / 2;
-
-                  if (tux_xm < -MAX_WALK_XM / 2)
-                    tux_xm = -MAX_WALK_XM / 2;
-                }
-            }
-        }
-
-
-      /* End of level? */
-
-      if (tux_x >= endpos && endpos != 0)
-        {
-          next_level = 1;
-        }
-
-
-      /* Jump/jumping? */
-
-      if (up == DOWN)
-        {
-          if (jump_counter == 0)
-            {
-              /* Taking off? */
-
-              if (!issolid(tux_x, tux_y + 32, scroll_x) ||
-                  tux_ym != 0)
-                {
-                  /* If they're not on the ground, or are currently moving
-                  vertically, don't jump! */
-
-                  jump_counter = MAX_JUMP_COUNT;
-                }
-              else
-                {
-                  /* Make sure we're not standing back up into a solid! */
-
-                  if (tux_size == SMALL || tux_duck == NO ||
-                      !issolid(tux_x, tux_y, scroll_x))
-                    {
-                      jumping = YES;
-
-                      if (tux_size == SMALL)
-                        play_sound(sounds[SND_JUMP]);
-                      else
-                        play_sound(sounds[SND_BIGJUMP]);
-                    }
-                }
-            }
-
-
-          /* Keep jumping for a while: */
-
-          if (jump_counter < MAX_JUMP_COUNT)
-            {
-              tux_ym = tux_ym - JUMP_SPEED;
-              jump_counter++;
-            }
-        }
-      else
-        jump_counter = 0;
-
-
-      /* Shoot! */
-
-      if (fire == DOWN && old_fire == UP && tux_got_coffee)
-        {
-          add_bullet(tux_x + scroll_x, tux_y, tux_dir, tux_xm);
-        }
-
-
-      /* Duck! */
-
-      if (down == DOWN)
-        {
-          if (tux_size == BIG)
-            tux_duck = YES;
-        }
-      else
-        {
-          if (tux_size == BIG && tux_duck == YES)
-            {
-              /* Make sure we're not standing back up into a solid! */
+  int i;
 
-              if (!issolid(tux_x, tux_y - 32, scroll_x))
-                tux_duck = NO;
-            }
-          else
-            tux_duck = NO;
-        }
-    } /* (tux_dying || next_level) */
-  else
+  /* (tux_dying || next_level) */
+  if (tux.dying || next_level)
     {
       /* Tux either died, or reached the end of a level! */
 
-
       if (playing_music())
         halt_music();
 
@@ -536,1643 +296,239 @@ int game_action(void)
           /* End of a level! */
           level++;
           next_level = 0;
-          drawresultscreen();
+          if(st_gl_mode == ST_GL_PLAY)
+            drawresultscreen();
+          player_level_begin(&tux);
         }
       else
         {
-
-          tux_ym = tux_ym + GRAVITY;
-
-
-
-          /* He died :^( */
-
-          lives--;
+          player_dying(&tux);
 
           /* No more lives!? */
 
-          if (lives < 0)
+          if (tux.lives < 0)
             {
-              drawendscreen();
+              if(st_gl_mode == ST_GL_PLAY)
+                drawendscreen();
 
-              if (score > highscore)
-                save_hs(score);
-
-              unloadlevelgfx();
-              unloadlevelsong();
+              if(st_gl_mode == ST_GL_PLAY)
+                {
+                  if (score > hs_score)
+                    save_hs(score);
+                }
+              level_free_gfx();
+              level_free(&current_level);
+              level_free_song();
               unloadshared();
+              arrays_free();
               return(0);
             } /* if (lives < 0) */
         }
 
       /* Either way, (re-)load the (next) level... */
 
-      loadlevel();
-      unloadlevelgfx();
-      loadlevelgfx();
-      unloadlevelsong();
-      loadlevelsong();
-    }
-
-  /* Move tux: */
-
-  tux_x = tux_x + tux_xm;
-  tux_y = tux_y + tux_ym;
+      player_level_begin(&tux);
+      set_defaults();
+      level_free(&current_level);
+      if(level_load(&current_level,level_subset,level) != 0)
+        exit(1);
+      arrays_free();
+      arrays_init();
+      activate_bad_guys();
+      level_free_gfx();
+      level_load_gfx(&current_level);
+      level_free_song();
+      level_load_song(&current_level);
+      if(st_gl_mode == ST_GL_PLAY)
+        levelintro();
+      start_timers();
+    }
+
+  player_action(&tux);
 
+  /* Handle bouncy distros: */
 
-  /* Keep tux in bounds: */
-  if (tux_x < 0)
-    tux_x = 0;
-    else if (tux_x < 160 && scroll_x > 0 && debug_mode == YES)
+  for (i = 0; i < num_bouncy_distros; i++)
     {
-    scroll_x = scroll_x - ( 160 - tux_x);
-    tux_x = 160;
-    
-    if(scroll_x < 0)
-     scroll_x = 0;
-   
+      bouncy_distro_action(&bouncy_distros[i]);
     }
-    else if (tux_x > 320 && scroll_x < ((level_width * 32) - 640))
-    {
-      /* Scroll the screen in past center: */
 
-      scroll_x = scroll_x + (tux_x - 320);
-      tux_x = 320;
 
-      if (scroll_x > ((level_width * 32) - 640))
-        scroll_x = ((level_width * 32) - 640);
-    }
-  else if (tux_x > 608)
-    {
-      /* ... unless there's no more to scroll! */
+  /* Handle broken bricks: */
 
-      tux_x = 608;
+  for (i = 0; i < num_broken_bricks; i++)
+    {
+      broken_brick_action(&broken_bricks[i]);
     }
 
 
-  /* Land: */
+  /* Handle distro counting: */
 
-  if (!tux_dying)
+  if (counting_distros == YES)
     {
-      if (issolid(tux_x, tux_y + 31, scroll_x) &&
-          !issolid(tux_x - tux_xm, tux_y + 31, scroll_x))
-        {
-          while (issolid(tux_x, tux_y + 31, scroll_x))
-            {
-              if (tux_xm < 0)
-                tux_x++;
-              else if (tux_xm > 0)
-                tux_x--;
-            }
-
-          tux_xm = 0;
-        }
-
-      if (issolid(tux_x, tux_y, scroll_x) &&
-          !issolid(tux_x - tux_xm, tux_y, scroll_x))
-        {
-          while (issolid(tux_x, tux_y, scroll_x))
-            {
-              if (tux_xm < 0)
-                tux_x++;
-              else if (tux_xm > 0)
-                tux_x--;
-            }
+      distro_counter--;
 
-          tux_xm = 0;
-        }
+      if (distro_counter <= 0)
+        counting_distros = -1;
+    }
 
-      if (issolid(tux_x, tux_y + 31, scroll_x))
-        {
-          /* Set down properly: */
 
-          while (issolid(tux_x, tux_y + 31, scroll_x))
-            {
-              if (tux_ym < 0)
-                tux_y++;
-              else if (tux_ym > 0)
-                tux_y--;
-            }
+  /* Handle bouncy bricks: */
 
+  for (i = 0; i < num_bouncy_bricks; i++)
+    {
+      bouncy_brick_action(&bouncy_bricks[i]);
+    }
 
-          /* Reset score multiplier (for mutli-hits): */
 
-          if (tux_ym > 0)
-            score_multiplier = 1;
+  /* Handle floating scores: */
 
+  for (i = 0; i < num_floating_scores; i++)
+    {
+      floating_score_action(&floating_scores[i]);
+    }
 
-          /* Stop jumping! */
 
-          tux_ym = 0;
-          jumping = NO;
-        }
+  /* Handle bullets: */
 
+  for (i = 0; i < num_bullets; ++i)
+    {
+      bullet_action(&bullets[i]);
+    }
 
-      /* Bump into things: */
+  /* Handle upgrades: */
 
-      if (issolid(tux_x, tux_y, scroll_x) ||
-          (tux_size == BIG && !tux_duck &&
-           (issolid(tux_x, tux_y - 32, scroll_x))))
-        {
-          if (!issolid(tux_x - tux_xm, tux_y, scroll_x) &&
-              (tux_size == SMALL || tux_duck ||
-               !issolid(tux_x - tux_xm, tux_y - 32, scroll_x)))
-            {
-              tux_x = tux_x - tux_xm;
-              tux_xm = 0;
-            }
-          else if (!issolid(tux_x, tux_y - tux_ym, scroll_x) &&
-                   (tux_size == SMALL || tux_duck ||
-                    !issolid(tux_x, tux_y - 32 - tux_ym, scroll_x)))
-            {
-              if (tux_ym <= 0)
-                {
-                  /* Jumping up? */
+  for (i = 0; i < num_upgrades; i++)
+    {
+      upgrade_action(&upgrades[i]);
+    }
 
-                  if (tux_size == BIG)
-                    {
-                      /* Break bricks and empty boxes: */
-
-                      if (!tux_duck)
-                        {
-                          if (isbrick(tux_x, tux_y - 32, scroll_x) ||
-                              isfullbox(tux_x, tux_y - 32, scroll_x))
-                            {
-                              trygrabdistro(tux_x, tux_y - 64, scroll_x,
-                                            BOUNCE);
-                              trybumpbadguy(tux_x, tux_y - 96, scroll_x);
-
-                              if (isfullbox(tux_x, tux_y - 32,
-                                            scroll_x))
-                                {
-                                  bumpbrick(tux_x, tux_y - 32,
-                                            scroll_x);
-                                }
-
-                              trybreakbrick(tux_x, tux_y - 32, scroll_x);
-                              tryemptybox(tux_x, tux_y - 32, scroll_x);
-                            }
-
-                          if (isbrick(tux_x + 31, tux_y - 32, scroll_x) ||
-                              isfullbox(tux_x + 31, tux_y - 32, scroll_x))
-                            {
-                              trygrabdistro(tux_x + 31,
-                                            tux_y - 64,
-                                            scroll_x,
-                                            BOUNCE);
-                              trybumpbadguy(tux_x + 31,
-                                            tux_y - 96,
-                                            scroll_x);
-
-                              if (isfullbox(tux_x + 31, tux_y - 32,
-                                            scroll_x))
-                                {
-                                  bumpbrick(tux_x + 31, tux_y - 32,
-                                            scroll_x);
-                                }
-
-                              trybreakbrick(tux_x + 31,
-                                            tux_y - 32,
-                                            scroll_x);
-                              tryemptybox(tux_x + 31,
-                                          tux_y - 32,
-                                          scroll_x);
-                            }
-                        }
-                      else /* ducking */
-                        {
-                          if (isbrick(tux_x, tux_y, scroll_x) ||
-                              isfullbox(tux_x, tux_y, scroll_x))
-                            {
-                              trygrabdistro(tux_x, tux_y - 32, scroll_x,
-                                            BOUNCE);
-                              trybumpbadguy(tux_x, tux_y - 64, scroll_x);
-                              if (isfullbox(tux_x, tux_y, scroll_x))
-                                bumpbrick(tux_x, tux_y, scroll_x);
-                              trybreakbrick(tux_x, tux_y, scroll_x);
-                              tryemptybox(tux_x, tux_y, scroll_x);
-                            }
-
-                          if (isbrick(tux_x + 31, tux_y, scroll_x) ||
-                              isfullbox(tux_x + 31, tux_y, scroll_x))
-                            {
-                              trygrabdistro(tux_x + 31,
-                                            tux_y - 32,
-                                            scroll_x,
-                                            BOUNCE);
-                              trybumpbadguy(tux_x + 31,
-                                            tux_y - 64,
-                                            scroll_x);
-                              if (isfullbox(tux_x + 31, tux_y, scroll_x))
-                                bumpbrick(tux_x + 31, tux_y, scroll_x);
-                              trybreakbrick(tux_x + 31, tux_y, scroll_x);
-                              tryemptybox(tux_x + 31, tux_y, scroll_x);
-                            }
-                        }
-                    }
-                  else
-                    {
-                      /* It's a brick and we're small, make the brick
-                         bounce, and grab any distros above it: */
-
-                      if (isbrick(tux_x, tux_y, scroll_x) ||
-                          isfullbox(tux_x, tux_y, scroll_x))
-                        {
-                          trygrabdistro(tux_x, tux_y - 32, scroll_x,
-                                        BOUNCE);
-                          trybumpbadguy(tux_x, tux_y - 64, scroll_x);
-                          bumpbrick(tux_x, tux_y, scroll_x);
-                          tryemptybox(tux_x, tux_y, scroll_x);
-                        }
-
-                      if (isbrick(tux_x + 31, tux_y, scroll_x) ||
-                          isfullbox(tux_x + 31, tux_y, scroll_x))
-                        {
-                          trygrabdistro(tux_x + 31, tux_y - 32, scroll_x,
-                                        BOUNCE);
-                          trybumpbadguy(tux_x + 31, tux_y - 64, scroll_x);
-                          bumpbrick(tux_x + 31, tux_y, scroll_x);
-                          tryemptybox(tux_x + 31, tux_y, scroll_x);
-                        }
-
-
-                      /* Get a distro from a brick? */
-
-                      if (shape(tux_x, tux_y, scroll_x) == 'x' ||
-                          shape(tux_x, tux_y, scroll_x) == 'y')
-                        {
-                          add_bouncy_distro(((tux_x + scroll_x + 1)
-                                             / 32) * 32,
-                                            (tux_y / 32) * 32);
-
-                          if (counting_distros == NO)
-                            {
-                              counting_distros = YES;
-                              distro_counter = 100;
-                            }
-
-                          if (distro_counter <= 0)
-                            change(tux_x, tux_y, scroll_x, 'a');
-
-                          play_sound(sounds[SND_DISTRO]);
-                          score = score + SCORE_DISTRO;
-                          distros++;
-                        }
-                      else if (shape(tux_x + 31, tux_y, scroll_x) == 'x' ||
-                               shape(tux_x + 31, tux_y, scroll_x) == 'y')
-                        {
-                          add_bouncy_distro(((tux_x + scroll_x + 1 + 31)
-                                             / 32) * 32,
-                                            (tux_y / 32) * 32);
-
-                          if (counting_distros == NO)
-                            {
-                              counting_distros = YES;
-                              distro_counter = 100;
-                            }
-
-                          if (distro_counter <= 0)
-                            change(tux_x + 31, tux_y, scroll_x, 'a');
-
-                          play_sound(sounds[SND_DISTRO]);
-                          score = score + SCORE_DISTRO;
-                          distros++;
-                        }
-                    }
-
-
-                  /* Bump head: */
-
-                  tux_y = (tux_y / 32) * 32 + 30;
-                }
-              else
-                {
-                  /* Land on feet: */
-
-                  tux_y = (tux_y / 32) * 32 - 32;
-                }
-
-              tux_ym = 0;
-              jumping = NO;
-              jump_counter = MAX_JUMP_COUNT;
-            }
-        }
-    }
-
-
-  /* Grab distros: */
-
-  if (!tux_dying)
-    {
-      trygrabdistro(tux_x, tux_y, scroll_x, NO_BOUNCE);
-      trygrabdistro(tux_x + 31, tux_y, scroll_x, NO_BOUNCE);
-
-      if (tux_size == BIG && !tux_duck)
-        {
-          trygrabdistro(tux_x, tux_y - 32, scroll_x, NO_BOUNCE);
-          trygrabdistro(tux_x + 31, tux_y - 32, scroll_x, NO_BOUNCE);
-        }
-    }
-
-
-  /* Enough distros for a One-up? */
-
-  if (distros >= DISTROS_LIFEUP)
-    {
-      distros = distros - DISTROS_LIFEUP;
-      if(lives < MAX_LIVES)
-        lives++;
-      play_sound(sounds[SND_LIFEUP]); /*We want to hear the sound even, if MAX_LIVES is reached*/
-    }
-
-
-  /* Keep in-bounds, vertically: */
-
-  if (tux_y < 0)
-    tux_y = 0;
-  else if (tux_y > 480)
-    {
-      killtux(KILL);
-    }
-
-
-  /* Slow down horizontally: */
-
-  if (!tux_dying)
-    {
-      if (right == UP && left == UP)
-        {
-          if (isice(tux_x, tux_y + 32, scroll_x) ||
-              !issolid(tux_x, tux_y + 32, scroll_x))
-            {
-              /* Slowly on ice or in air: */
-
-              if (tux_xm > 0)
-                tux_xm--;
-              else if (tux_xm < 0)
-                tux_xm++;
-            }
-          else
-            {
-              /* Quickly, otherwise: */
-
-              tux_xm = tux_xm / 2;
-            }
-        }
-
-
-      /* Drop vertically: */
-
-      if (!issolid(tux_x, tux_y + 32, scroll_x))
-        {
-          tux_ym = tux_ym + GRAVITY;
-
-          if (tux_ym > MAX_YM)
-            tux_ym = MAX_YM;
-        }
-    }
-
-
-  if (tux_safe > 0)
-    tux_safe--;
-
-
-  /* ---- DONE HANDLING TUX! --- */
-
-
-  /* Handle bouncy distros: */
-
-  for (i = 0; i < NUM_BOUNCY_DISTROS; i++)
-    {
-      if (bouncy_distros[i].alive)
-        {
-          bouncy_distros[i].y = bouncy_distros[i].y + bouncy_distros[i].ym;
-
-          bouncy_distros[i].ym++;
-
-          if (bouncy_distros[i].ym >= 0)
-            bouncy_distros[i].alive = NO;
-        }
-    }
-
-
-  /* Handle broken bricks: */
-
-  for (i = 0; i < NUM_BROKEN_BRICKS; i++)
-    {
-      if (broken_bricks[i].alive)
-        {
-          broken_bricks[i].x = broken_bricks[i].x + broken_bricks[i].xm;
-          broken_bricks[i].y = broken_bricks[i].y + broken_bricks[i].ym;
-
-          broken_bricks[i].ym++;
-
-          if (broken_bricks[i].ym >= 0)
-            broken_bricks[i].alive = NO;
-        }
-    }
-
-
-  /* Handle distro counting: */
-
-  if (counting_distros == YES)
-    {
-      distro_counter--;
-
-      if (distro_counter <= 0)
-        counting_distros = -1;
-    }
-
-
-  /* Handle bouncy bricks: */
-
-  for (i = 0; i < NUM_BOUNCY_BRICKS; i++)
-    {
-      if (bouncy_bricks[i].alive)
-        {
-          bouncy_bricks[i].offset = (bouncy_bricks[i].offset +
-                                     bouncy_bricks[i].offset_m);
-
-          /* Go back down? */
-
-          if (bouncy_bricks[i].offset < -BOUNCY_BRICK_MAX_OFFSET)
-            bouncy_bricks[i].offset_m = BOUNCY_BRICK_SPEED;
-
-
-          /* Stop bouncing? */
-
-          if (bouncy_bricks[i].offset == 0)
-            bouncy_bricks[i].alive = NO;
-        }
-    }
-
-
-  /* Handle floating scores: */
-
-  for (i = 0; i < NUM_FLOATING_SCORES; i++)
-    {
-      if (floating_scores[i].alive)
-        {
-          floating_scores[i].y = floating_scores[i].y - 2;
-          floating_scores[i].timer--;
-
-          if (floating_scores[i].timer <= 0)
-            floating_scores[i].alive = NO;
-        }
-    }
-
-
-  /* Handle bullets: */
-
-  for (i = 0; i < NUM_BULLETS; i++)
-    {
-      if (bullets[i].alive)
-        {
-          bullets[i].x = bullets[i].x + bullets[i].xm;
-          bullets[i].y = bullets[i].y + bullets[i].ym;
-
-          if (issolid(bullets[i].x, bullets[i].y, 0))
-            {
-              if (issolid(bullets[i].x, bullets[i].y - bullets[i].ym, 0))
-                bullets[i].alive = NO;
-              else
-                {
-                  if (bullets[i].ym >= 0)
-                    {
-                      bullets[i].y = (bullets[i].y / 32) * 32 - 8;
-                    }
-                  bullets[i].ym = -bullets[i].ym;
-                }
-            }
-
-          bullets[i].ym = bullets[i].ym + GRAVITY;
-
-          if (bullets[i].x < scroll_x ||
-              bullets[i].x > scroll_x + 640)
-            {
-              bullets[i].alive = NO;
-            }
-        }
-
-
-      if (bullets[i].alive)
-        {
-          for (j = 0; j < NUM_BAD_GUYS; j++)
-            {
-              if (bad_guys[j].alive && !bad_guys[j].dying)
-                {
-                  if (bullets[i].x >= bad_guys[j].x - 4 &&
-                      bullets[i].x <= bad_guys[j].x + 32 + 4 &&
-                      bullets[i].y >= bad_guys[j].y - 4 &&
-                      bullets[i].y <= bad_guys[j].y + 32 + 4)
-                    {
-                      /* Kill the bad guy! */
-
-                      bullets[i].alive = 0;
-                      bad_guys[j].dying = FALLING;
-                      bad_guys[j].ym = -8;
-
-
-                      /* Gain some points: */
-
-                      if (bad_guys[j].kind == BAD_BSOD)
-                        {
-                          add_score(bad_guys[j].x - scroll_x, bad_guys[j].y,
-                                    50 * score_multiplier);
-                        }
-                      else if (bad_guys[j].kind == BAD_LAPTOP)
-                        {
-                          add_score(bad_guys[j].x - scroll_x, bad_guys[j].y,
-                                    25 * score_multiplier);
-                        }
-
-
-                      /* Play death sound: */
-                      play_sound(sounds[SND_FALL]);
-                    }
-                }
-            }
-        }
-    }
-
-
-  /* Handle background timer: */
-
-  if (super_bkgd_time)
-    super_bkgd_time--;
-
-
-  /* Handle invincibility timer: */
-
-
-  if (tux_invincible_time > 50)
-    {
-      tux_invincible_time--;
-
-
-      if (!playing_music())
-        play_music( herring_song, 1 );
-    }
-  else
-    {
-      if (current_music == HERRING_MUSIC)
-        {
-          /* stop the herring_song, now play the level_song ! */
-          current_music = LEVEL_MUSIC;
-          halt_music();
-        }
-      if (!playing_music())
-        play_music( level_song, 1 );
-      if (tux_invincible_time > 0)
-        tux_invincible_time--;
-    }
-
-
-  /* Handle upgrades: */
-
-  for (i = 0; i < NUM_UPGRADES; i++)
-    {
-      if (upgrades[i].alive)
-        {
-          if (upgrades[i].height < 32)
-            {
-              /* Rise up! */
-
-              upgrades[i].height++;
-            }
-          else
-            {
-              /* Move around? */
-
-              if (upgrades[i].kind == UPGRADE_MINTS ||
-                  upgrades[i].kind == UPGRADE_HERRING)
-                {
-                  upgrades[i].x = upgrades[i].x + upgrades[i].xm;
-                  upgrades[i].y = upgrades[i].y + upgrades[i].ym;
-
-                  if (issolid(upgrades[i].x, upgrades[i].y + 31, 0) ||
-                      issolid(upgrades[i].x + 31, upgrades[i].y + 31, 0))
-                    {
-                      if (upgrades[i].ym > 0)
-                        {
-                          if (upgrades[i].kind == UPGRADE_MINTS)
-                            {
-                              upgrades[i].ym = 0;
-                            }
-                          else if (upgrades[i].kind == UPGRADE_HERRING)
-                            {
-                              upgrades[i].ym = -24;
-                            }
-
-                          upgrades[i].y = (upgrades[i].y / 32) * 32;
-                        }
-                    }
-                  else
-                    upgrades[i].ym = upgrades[i].ym + GRAVITY;
-
-                  if (issolid(upgrades[i].x, upgrades[i].y, 0))
-                    {
-                      upgrades[i].xm = -upgrades[i].xm;
-                    }
-                }
-
-
-              /* Off the screen?  Kill it! */
-
-              if (upgrades[i].x < scroll_x)
-                upgrades[i].alive = NO;
-
-
-              /* Did the player grab it? */
-
-              if (tux_x + scroll_x >= upgrades[i].x - 32 &&
-                  tux_x + scroll_x <= upgrades[i].x + 32 &&
-                  tux_y >= upgrades[i].y - 32 &&
-                  tux_y <= upgrades[i].y + 32)
-                {
-                  /* Remove the upgrade: */
-
-                  upgrades[i].alive = NO;
-
-
-                  /* Affect the player: */
-
-                  if (upgrades[i].kind == UPGRADE_MINTS)
-                    {
-                      play_sound(sounds[SND_EXCELLENT]);
-                      tux_size = BIG;
-                      super_bkgd_time = 8;
-                    }
-                  else if (upgrades[i].kind == UPGRADE_COFFEE)
-                    {
-                      play_sound(sounds[SND_COFFEE]);
-                      tux_got_coffee = YES;
-                      super_bkgd_time = 4;
-                    }
-                  else if (upgrades[i].kind == UPGRADE_HERRING)
-                    {
-                      play_sound(sounds[SND_HERRING]);
-                      tux_invincible_time = TUX_INVINCIBLE_TIME;
-                      super_bkgd_time = 4;
-                      /* play the herring song ^^ */
-                      current_music = HERRING_MUSIC;
-                      if (playing_music())
-                        halt_music();
-                      play_music( herring_song, 1 );
-                    }
-                }
-            }
-        }
-    }
-
-
-  /* Handle bad guys: */
-
-  for (i = 0; i < NUM_BAD_GUYS; i++)
-    {
-      if (bad_guys[i].alive)
-        {
-          if (bad_guys[i].seen)
-            {
-              if (bad_guys[i].kind == BAD_BSOD)
-                {
-                  /* --- BLUE SCREEN OF DEATH MONSTER: --- */
-
-                  /* Move left/right: */
-
-                  if (bad_guys[i].dying == NO ||
-                      bad_guys[i].dying == FALLING)
-                    {
-                      if (bad_guys[i].dir == RIGHT)
-                        bad_guys[i].x = bad_guys[i].x + 4;
-                      else if (bad_guys[i].dir == LEFT)
-                        bad_guys[i].x = bad_guys[i].x - 4;
-                    }
-
-
-                  /* Move vertically: */
-
-                  bad_guys[i].y = bad_guys[i].y + bad_guys[i].ym;
-
-
-                  /* Bump into things horizontally: */
-
-                  if (!bad_guys[i].dying)
-                    {
-                      if (issolid(bad_guys[i].x, bad_guys[i].y, 0))
-                        bad_guys[i].dir = !bad_guys[i].dir;
-                    }
-
-
-                  /* Bump into other bad guys: */
-
-                  for (j = 0; j < NUM_BAD_GUYS; j++)
-                    {
-                      if (j != i && bad_guys[j].alive &&
-                          !bad_guys[j].dying && !bad_guys[i].dying &&
-                          bad_guys[i].x >= bad_guys[j].x - 32 &&
-                          bad_guys[i].x <= bad_guys[j].x + 32 &&
-                          bad_guys[i].y >= bad_guys[j].y - 32 &&
-                          bad_guys[i].y <= bad_guys[j].y + 32)
-                        {
-                          bad_guys[i].dir = !bad_guys[i].dir;
-                        }
-                    }
-
-
-                  /* Fall if we get off the ground: */
-
-                  if (bad_guys[i].dying != FALLING)
-                    {
-                      if (!issolid(bad_guys[i].x, bad_guys[i].y + 32, 0) &&
-                          bad_guys[i].ym < MAX_YM)
-                        {
-                          bad_guys[i].ym = bad_guys[i].ym + GRAVITY;
-                        }
-                      else
-                        {
-                          /* Land: */
-
-                          if (bad_guys[i].ym > 0)
-                            {
-                              bad_guys[i].y = (bad_guys[i].y / 32) * 32;
-                              bad_guys[i].ym = 0;
-                            }
-                        }
-                    }
-                  else
-                    bad_guys[i].ym = bad_guys[i].ym + GRAVITY;
-
-                  if (bad_guys[i].y > 480)
-                    bad_guys[i].alive = NO;
-                }
-              else if (bad_guys[i].kind == BAD_LAPTOP)
-                {
-                  /* --- LAPTOP MONSTER: --- */
-
-                  /* Move left/right: */
-
-                  if (bad_guys[i].mode != FLAT && bad_guys[i].mode != KICK)
-                    {
-                      if (bad_guys[i].dying == NO ||
-                          bad_guys[i].dying == FALLING)
-                        {
-                          if (bad_guys[i].dir == RIGHT)
-                            bad_guys[i].x = bad_guys[i].x + 4;
-                          else if (bad_guys[i].dir == LEFT)
-                            bad_guys[i].x = bad_guys[i].x - 4;
-                        }
-                    }
-                  else if (bad_guys[i].mode == KICK)
-                    {
-                      if (bad_guys[i].dir == RIGHT)
-                        bad_guys[i].x = bad_guys[i].x + 16;
-                      else if (bad_guys[i].dir == LEFT)
-                        bad_guys[i].x = bad_guys[i].x - 16;
-                    }
-
-
-                  /* Move vertically: */
-
-                  bad_guys[i].y = bad_guys[i].y + bad_guys[i].ym;
-
-
-                  /* Bump into things horizontally: */
-
-                  if (!bad_guys[i].dying)
-                    {
-                      if (issolid(bad_guys[i].x, bad_guys[i].y, 0))
-                        {
-                          bad_guys[i].dir = !bad_guys[i].dir;
-
-                          if (bad_guys[i].mode == KICK)
-                            play_sound(sounds[SND_RICOCHET]);
-                        }
-                    }
-
-
-                  /* Bump into other bad guys: */
-
-                  for (j = 0; j < NUM_BAD_GUYS; j++)
-                    {
-                      if (j != i && bad_guys[j].alive &&
-                          !bad_guys[j].dying && !bad_guys[i].dying &&
-                          bad_guys[i].x >= bad_guys[j].x - 32 &&
-                          bad_guys[i].x <= bad_guys[j].x + 32 &&
-                          bad_guys[i].y >= bad_guys[j].y - 32 &&
-                          bad_guys[i].y <= bad_guys[j].y + 32)
-                        {
-                          if (bad_guys[i].mode != KICK)
-                            bad_guys[i].dir = !bad_guys[i].dir;
-                          else
-                            {
-                              /* We're in kick mode, kill the other guy: */
-
-                              bad_guys[j].dying = FALLING;
-                              bad_guys[j].ym = -8;
-                              play_sound(sounds[SND_FALL]);
-
-                              add_score(bad_guys[i].x - scroll_x,
-                                        bad_guys[i].y, 100);
-                            }
-                        }
-                    }
-
-
-                  /* Fall if we get off the ground: */
-
-                  if (bad_guys[i].dying != FALLING)
-                    {
-                      if (!issolid(bad_guys[i].x, bad_guys[i].y + 32, 0) &&
-                          bad_guys[i].ym < MAX_YM)
-                        {
-                          bad_guys[i].ym = bad_guys[i].ym + GRAVITY;
-                        }
-                      else
-                        {
-                          /* Land: */
-
-                          if (bad_guys[i].ym > 0)
-                            {
-                              bad_guys[i].y = (bad_guys[i].y / 32) * 32;
-                              bad_guys[i].ym = 0;
-                            }
-                        }
-                    }
-                  else
-                    bad_guys[i].ym = bad_guys[i].ym + GRAVITY;
-
-                  if (bad_guys[i].y > 480)
-                    bad_guys[i].alive = NO;
-                }
-              else if (bad_guys[i].kind == BAD_MONEY)
-                {
-                  /* --- MONEY BAGS: --- */
-
-
-                  /* Move vertically: */
-
-                  bad_guys[i].y = bad_guys[i].y + bad_guys[i].ym;
-
-
-                  /* Fall if we get off the ground: */
-
-                  if (bad_guys[i].dying != FALLING)
-                    {
-                      if (!issolid(bad_guys[i].x, bad_guys[i].y + 32, 0))
-                        {
-                          if (bad_guys[i].ym < MAX_YM)
-                            {
-                              bad_guys[i].ym = bad_guys[i].ym + GRAVITY;
-                            }
-                        }
-                      else
-                        {
-                          /* Land: */
-
-                          if (bad_guys[i].ym > 0)
-                            {
-                              bad_guys[i].y = (bad_guys[i].y / 32) * 32;
-                              bad_guys[i].ym = -MAX_YM;
-                            }
-                        }
-                    }
-                  else
-                    bad_guys[i].ym = bad_guys[i].ym + GRAVITY;
-
-                  if (bad_guys[i].y > 480)
-                    bad_guys[i].alive = NO;
-                }
-              else if (bad_guys[i].kind == -1)
-              {}
-
-
-              /* Kill it if the player jumped on it: */
-
-              if (!bad_guys[i].dying && !tux_dying && !tux_safe &&
-                  tux_x + scroll_x >= bad_guys[i].x - 32 &&
-                  tux_x + scroll_x <= bad_guys[i].x + 32 &&
-                  tux_y >= bad_guys[i].y - 32 &&
-                  tux_y <= bad_guys[i].y - 8
-                  /* &&
-                  tux_ym >= 0 */)
-                {
-                  if (bad_guys[i].kind == BAD_BSOD)
-                    {
-                      bad_guys[i].dying = SQUISHED;
-                      bad_guys[i].timer = 16;
-                      tux_ym = -KILL_BOUNCE_YM;
-
-                      add_score(bad_guys[i].x - scroll_x, bad_guys[i].y,
-                                50 * score_multiplier);
-
-                      play_sound(sounds[SND_SQUISH]);
-                    }
-                  else if (bad_guys[i].kind == BAD_LAPTOP)
-                    {
-                      if (bad_guys[i].mode != FLAT)
-                        {
-                          /* Flatten! */
-
-                          bad_guys[i].mode = FLAT;
-
-                          bad_guys[i].timer = 64;
-
-                          tux_y = tux_y - 32;
-                        }
-                      else
-                        {
-                          /* Kick! */
-
-                          bad_guys[i].mode = KICK;
-
-                          if (tux_x + scroll_x <= bad_guys[i].x)
-                            bad_guys[i].dir = RIGHT;
-                          else
-                            bad_guys[i].dir = LEFT;
-
-                          bad_guys[i].timer = 8;
-                        }
-
-                      tux_ym = -KILL_BOUNCE_YM;
-
-                      add_score(bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                25 * score_multiplier);
-
-                      /* play_sound(sounds[SND_SQUISH]); */
-                    }
-                  else if (bad_guys[i].kind == -1)
-                  {}
-
-                  score_multiplier++;
-                }
-
-
-              /* Hurt the player if he just touched it: */
-
-              if (!bad_guys[i].dying && !tux_dying &&
-                  !tux_safe &&
-                  tux_x + scroll_x >= bad_guys[i].x - 32 &&
-                  tux_x + scroll_x <= bad_guys[i].x + 32 &&
-                  tux_y >= bad_guys[i].y - 32 &&
-                  tux_y <= bad_guys[i].y + 32)
-                {
-                  if (bad_guys[i].mode == FLAT)
-                    {
-                      /* Kick: */
-
-                      bad_guys[i].mode = KICK;
-
-                      if (tux_x + scroll_x <= bad_guys[i].x)
-                        {
-                          bad_guys[i].dir = RIGHT;
-                          bad_guys[i].x = bad_guys[i].x + 16;
-                        }
-                      else
-                        {
-                          bad_guys[i].dir = LEFT;
-                          bad_guys[i].x = bad_guys[i].x - 16;
-                        }
-
-                      bad_guys[i].timer = 8;
-                    }
-                  else if (bad_guys[i].mode == KICK)
-                    {
-                      if (tux_y < bad_guys[i].y - 16 &&
-                          bad_guys[i].timer == 0)
-                        {
-                          /* Step on (stop being kicked) */
-
-                          bad_guys[i].mode = FLAT;
-                          bad_guys[i].timer = 64;
-                        }
-                      else
-                        {
-                          /* Hurt if you get hit by kicked laptop: */
-
-                          if (bad_guys[i].timer == 0)
-                            {
-                              if (tux_invincible_time == 0)
-                                {
-                                  killtux(SHRINK);
-                                }
-                              else
-                                {
-                                  bad_guys[i].dying = FALLING;
-                                  bad_guys[i].ym = -8;
-                                  play_sound(sounds[SND_FALL]);
-                                }
-                            }
-                        }
-                    }
-                  else
-                    {
-                      if (tux_invincible_time == 0)
-                        {
-                          killtux(SHRINK);
-                        }
-                      else
-                        {
-                          bad_guys[i].dying = FALLING;
-                          bad_guys[i].ym = -8;
-                          play_sound(sounds[SND_FALL]);
-                        }
-                    }
-                }
-
-
-              /* Handle mode timer: */
-
-              if (bad_guys[i].mode == FLAT)
-                {
-                  bad_guys[i].timer--;
-
-                  if (bad_guys[i].timer <= 0)
-                    bad_guys[i].mode = NORMAL;
-                }
-              else if (bad_guys[i].mode == KICK)
-                {
-                  if (bad_guys[i].timer > 0)
-                    bad_guys[i].timer--;
-                }
-
-
-              /* Handle dying timer: */
-
-              if (bad_guys[i].dying == SQUISHED)
-                {
-                  bad_guys[i].timer--;
-
-
-                  /* Remove it if time's up: */
-
-                  if (bad_guys[i].timer <= 0)
-                    bad_guys[i].alive = NO;
-                }
-
-
-              /* Remove if it's far off the screen: */
-
-              if (bad_guys[i].x < scroll_x - OFFSCREEN_DISTANCE)
-                bad_guys[i].alive = NO;
-            }
-          else /* !seen */
-            {
-              /* Once it's on screen, it's activated! */
-
-              if (bad_guys[i].x <= scroll_x + 640 + OFFSCREEN_DISTANCE)
-                bad_guys[i].seen = YES;
-            }
-        }
-    }
-
-
-  /* Handle skidding: */
-
-  if (tux_skidding > 0)
-    {
-      tux_skidding--;
-    }
-
-  return -1;
-}
-
-/* --- GAME DRAW! --- */
-
-void game_draw()
-{
-  int  x, y, i;
-
-  /* Draw screen: */
-
-  if (tux_dying && (frame % 4) == 0)
-    clearscreen(255, 255, 255);
-  else
-    {
-      if (super_bkgd_time == 0)
-        clearscreen(bkgd_red, bkgd_green, bkgd_blue);
-      else
-        drawimage(img_super_bkgd, 0, 0, NO_UPDATE);
-    }
-
-
-  /* Draw background: */
-
-  for (y = 0; y < 15; y++)
-    {
-      for (x = 0; x < 21; x++)
-        {
-          drawshape(x * 32 - (scroll_x % 32), y * 32,
-                    tiles[y][x + (scroll_x / 32)]);
-        }
-    }
-
-
-  /* (Bouncy bricks): */
-
-  for (i = 0; i < NUM_BOUNCY_BRICKS; i++)
-    {
-      if (bouncy_bricks[i].alive)
-        {
-          if (bouncy_bricks[i].x >= scroll_x - 32 &&
-              bouncy_bricks[i].x <= scroll_x + 640)
-            {
-              dest.x = bouncy_bricks[i].x - scroll_x;
-              dest.y = bouncy_bricks[i].y;
-              dest.w = 32;
-              dest.h = 32;
-
-              SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format,
-                                                     bkgd_red,
-                                                     bkgd_green,
-                                                     bkgd_blue));
-
-              drawshape(bouncy_bricks[i].x - scroll_x,
-                        bouncy_bricks[i].y + bouncy_bricks[i].offset,
-                        bouncy_bricks[i].shape);
-            }
-        }
-    }
-
-
-  /* (Bad guys): */
-
-  for (i = 0; i < NUM_BAD_GUYS; i++)
-    {
-      if (bad_guys[i].alive &&
-          bad_guys[i].x > scroll_x - 32 &&
-          bad_guys[i].x < scroll_x + 640)
-        {
-          if (bad_guys[i].kind == BAD_BSOD)
-            {
-              /* --- BLUE SCREEN OF DEATH MONSTER: --- */
-
-              if (bad_guys[i].dying == NO)
-                {
-                  /* Alive: */
-
-                  if (bad_guys[i].dir == LEFT)
-                    {
-                      drawimage(img_bsod_left[(frame / 5) % 4],
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                  else
-                    {
-                      drawimage(img_bsod_right[(frame / 5) % 4],
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                }
-              else if (bad_guys[i].dying == FALLING)
-                {
-                  /* Falling: */
-
-                  if (bad_guys[i].dir == LEFT)
-                    {
-                      drawimage(img_bsod_falling_left,
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                  else
-                    {
-                      drawimage(img_bsod_falling_right,
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                }
-              else if (bad_guys[i].dying == SQUISHED)
-                {
-                  /* Dying - Squished: */
-
-                  if (bad_guys[i].dir == LEFT)
-                    {
-                      drawimage(img_bsod_squished_left,
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y + 24,
-                                NO_UPDATE);
-                    }
-                  else
-                    {
-                      drawimage(img_bsod_squished_right,
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y + 24,
-                                NO_UPDATE);
-                    }
-                }
-            }
-          else if (bad_guys[i].kind == BAD_LAPTOP)
-            {
-              /* --- LAPTOP MONSTER: --- */
-
-              if (bad_guys[i].dying == NO)
-                {
-                  /* Alive: */
-
-                  if (bad_guys[i].mode == NORMAL)
-                    {
-                      /* Not flat: */
-
-                      if (bad_guys[i].dir == LEFT)
-                        {
-                          drawimage(img_laptop_left[(frame / 5) % 3],
-                                    bad_guys[i].x - scroll_x,
-                                    bad_guys[i].y,
-                                    NO_UPDATE);
-                        }
-                      else
-                        {
-                          drawimage(img_laptop_right[(frame / 5) % 3],
-                                    bad_guys[i].x - scroll_x,
-                                    bad_guys[i].y,
-                                    NO_UPDATE);
-                        }
-                    }
-                  else
-                    {
-                      /* Flat: */
-
-                      if (bad_guys[i].dir == LEFT)
-                        {
-                          drawimage(img_laptop_flat_left,
-                                    bad_guys[i].x - scroll_x,
-                                    bad_guys[i].y,
-                                    NO_UPDATE);
-                        }
-                      else
-                        {
-                          drawimage(img_laptop_flat_right,
-                                    bad_guys[i].x - scroll_x,
-                                    bad_guys[i].y,
-                                    NO_UPDATE);
-                        }
-                    }
-                }
-              else if (bad_guys[i].dying == FALLING)
-                {
-                  /* Falling: */
-
-                  if (bad_guys[i].dir == LEFT)
-                    {
-                      drawimage(img_laptop_falling_left,
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                  else
-                    {
-                      drawimage(img_laptop_falling_right,
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                }
-            }
-          else if (bad_guys[i].kind == BAD_MONEY)
-            {
-              if (bad_guys[i].ym > -16)
-                {
-                  if (bad_guys[i].dir == LEFT)
-                    {
-                      drawimage(img_money_left[0],
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                  else
-                    {
-                      drawimage(img_money_right[0],
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                }
-              else
-                {
-                  if (bad_guys[i].dir == LEFT)
-                    {
-                      drawimage(img_money_left[1],
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                  else
-                    {
-                      drawimage(img_money_right[1],
-                                bad_guys[i].x - scroll_x,
-                                bad_guys[i].y,
-                                NO_UPDATE);
-                    }
-                }
-            }
-          else if (bad_guys[i].kind == -1)
-          {}
-        }
-    }
-
-
-  /* (Tux): */
-
-  if (right == UP && left == UP)
-    {
-      tux_frame_main = 1;
-      tux_frame = 1;
-    }
-  else
-    {
-      if ((fire == DOWN && (frame % 2) == 0) ||
-          (frame % 4) == 0)
-        tux_frame_main = (tux_frame_main + 1) % 4;
-
-      tux_frame = tux_frame_main;
-
-      if (tux_frame == 3)
-        tux_frame = 1;
-    }
 
+  /* Handle bad guys: */
 
-  if (tux_got_coffee && (frame % 2) == 1)
+  for (i = 0; i < num_bad_guys; i++)
     {
-      /* Coffee glow: */
-
-      drawimage(img_red_glow, tux_x - 8, tux_y - 32, NO_UPDATE);
+      badguy_action(&bad_guys[i]);
     }
 
+  /* Handle all possible collisions. */
+  collision_handler();
 
-  if (tux_safe == 0 || (frame % 2) == 0)
-    {
-      if (tux_size == SMALL)
-        {
-          if (tux_invincible_time)
-            {
-              /* Draw cape: */
+  return -1;
+}
 
-              if (tux_dir == RIGHT)
-                {
-                  drawimage(cape_right[frame % 2],
-                            tux_x, tux_y,
-                            NO_UPDATE);
-                }
-              else
-                {
-                  drawimage(cape_left[frame % 2],
-                            tux_x, tux_y,
-                            NO_UPDATE);
-                }
-            }
+/* --- GAME DRAW! --- */
 
+void game_draw(void)
+{
+  int  x, y, i, s;
 
-          if (tux_dir == RIGHT)
-            {
-              drawimage(tux_right[tux_frame], tux_x, tux_y, NO_UPDATE);
-            }
-          else
-            {
-              drawimage(tux_left[tux_frame], tux_x, tux_y, NO_UPDATE);
-            }
+  /* Draw screen: */
+
+  if (tux.dying && (frame % 4) == 0)
+    clearscreen(255, 255, 255);
+  else if(timer_check(&super_bkgd_timer))
+    texture_draw(&img_super_bkgd, 0, 0, NO_UPDATE);
+  else
+    {
+      /* Draw the real background */
+      if(current_level.bkgd_image[0] != '\0')
+        {
+          s = (int)scroll_x / 30;
+          texture_draw_part(&img_bkgd,s,0,0,0,img_bkgd.w - s, img_bkgd.h, NO_UPDATE);
+          texture_draw_part(&img_bkgd,0,0,screen->w - s ,0,s,img_bkgd.h, NO_UPDATE);
         }
       else
         {
-          if (tux_invincible_time)
-            {
-              /* Draw cape: */
-
-              if (tux_dir == RIGHT)
-                {
-                  drawimage(bigcape_right[frame % 2],
-                            tux_x - 8 - 16, tux_y - 32,
-                            NO_UPDATE);
-                }
-              else
-                {
-                  drawimage(bigcape_left[frame % 2],
-                            tux_x - 8, tux_y - 32,
-                            NO_UPDATE);
-                }
-            }
-
-          if (!tux_duck)
-            {
-              if (!tux_skidding)
-                {
-                  if (!jumping || tux_ym > 0)
-                    {
-                      if (tux_dir == RIGHT)
-                        {
-                          drawimage(bigtux_right[tux_frame],
-                                    tux_x - 8, tux_y - 32,
-                                    NO_UPDATE);
-                        }
-                      else
-                        {
-                          drawimage(bigtux_left[tux_frame],
-                                    tux_x - 8, tux_y - 32,
-                                    NO_UPDATE);
-                        }
-                    }
-                  else
-                    {
-                      if (tux_dir == RIGHT)
-                        {
-                          drawimage(bigtux_right_jump,
-                                    tux_x - 8, tux_y - 32,
-                                    NO_UPDATE);
-                        }
-                      else
-                        {
-                          drawimage(bigtux_left_jump,
-                                    tux_x - 8, tux_y - 32,
-                                    NO_UPDATE);
-                        }
-                    }
-                }
-              else
-                {
-                  if (tux_dir == RIGHT)
-                    {
-                      drawimage(skidtux_right,
-                                tux_x - 8, tux_y - 32,
-                                NO_UPDATE);
-                    }
-                  else
-                    {
-                      drawimage(skidtux_left,
-                                tux_x - 8, tux_y - 32,
-                                NO_UPDATE);
-                    }
-                }
-            }
-          else
-            {
-              if (tux_dir == RIGHT)
-                {
-                  drawimage(ducktux_right, tux_x - 8, tux_y - 16,
-                            NO_UPDATE);
-                }
-              else
-                {
-                  drawimage(ducktux_left, tux_x - 8, tux_y - 16,
-                            NO_UPDATE);
-                }
-            }
+          clearscreen(current_level.bkgd_red, current_level.bkgd_green, current_level.bkgd_blue);
         }
     }
 
+  /* Draw background: */
 
-  /* (Bullets): */
-
-  for (i = 0; i < NUM_BULLETS; i++)
+  for (y = 0; y < 15; ++y)
     {
-      if (bullets[i].alive &&
-          bullets[i].x >= scroll_x - 4 &&
-          bullets[i].x <= scroll_x + 640)
+      for (x = 0; x < 21; ++x)
         {
-          drawimage(img_bullet, bullets[i].x - scroll_x, bullets[i].y,
-                    NO_UPDATE);
+          drawshape(x * 32 - ((int)scroll_x % 32), y * 32,
+                    current_level.tiles[(int)y][(int)x + (int)(scroll_x / 32)]);
         }
     }
 
 
-  /* (Floating scores): */
+  /* (Bouncy bricks): */
 
-  for (i = 0; i < NUM_FLOATING_SCORES; i++)
+  for (i = 0; i < num_bouncy_bricks; ++i)
     {
-      if (floating_scores[i].alive)
-        {
-          sprintf(str, "%d", floating_scores[i].value);
-          drawtext(str,
-                   floating_scores[i].x + 16 - strlen(str) * 8,
-                   floating_scores[i].y,
-                   letters_gold, NO_UPDATE);
-        }
+      bouncy_brick_draw(&bouncy_bricks[i]);
     }
 
 
-  /* (Upgrades): */
+  /* (Bad guys): */
 
-  for (i = 0; i < NUM_UPGRADES; i++)
+  for (i = 0; i < num_bad_guys; ++i)
     {
-      if (upgrades[i].alive)
-        {
-          if (upgrades[i].height < 32)
-            {
-              /* Rising up... */
-
-              dest.x = upgrades[i].x - scroll_x;
-              dest.y = upgrades[i].y + 32 - upgrades[i].height;
-              dest.w = 32;
-              dest.h = upgrades[i].height;
-
-              src.x = 0;
-              src.y = 0;
-              src.w = 32;
-              src.h = upgrades[i].height;
-
-              if (upgrades[i].kind == UPGRADE_MINTS)
-                SDL_BlitSurface(img_mints, &src, screen, &dest);
-              else if (upgrades[i].kind == UPGRADE_COFFEE)
-                SDL_BlitSurface(img_coffee, &src, screen, &dest);
-              else if (upgrades[i].kind == UPGRADE_HERRING)
-                SDL_BlitSurface(img_golden_herring, &src, screen, &dest);
-            }
-          else
-            {
-              if (upgrades[i].kind == UPGRADE_MINTS)
-                {
-                  drawimage(img_mints,
-                            upgrades[i].x - scroll_x, upgrades[i].y,
-                            NO_UPDATE);
-                }
-              else if (upgrades[i].kind == UPGRADE_COFFEE)
-                {
-                  drawimage(img_coffee,
-                            upgrades[i].x - scroll_x, upgrades[i].y,
-                            NO_UPDATE);
-                }
-              else if (upgrades[i].kind == UPGRADE_HERRING)
-                {
-                  drawimage(img_golden_herring,
-                            upgrades[i].x - scroll_x, upgrades[i].y,
-                            NO_UPDATE);
-                }
-            }
-        }
+      badguy_draw(&bad_guys[i]);
     }
 
+  /* (Tux): */
 
-  /* (Bouncy distros): */
+  player_draw(&tux);
 
-  for (i = 0; i < NUM_BOUNCY_DISTROS; i++)
+  /* (Bullets): */
+
+  for (i = 0; i < num_bullets; ++i)
     {
-      if (bouncy_distros[i].alive)
-        {
-          drawimage(img_distro[0],
-                    bouncy_distros[i].x - scroll_x,
-                    bouncy_distros[i].y,
-                    NO_UPDATE);
-        }
+      bullet_draw(&bullets[i]);
     }
 
+  /* (Floating scores): */
 
-  /* (Broken bricks): */
-
-  for (i = 0; i < NUM_BROKEN_BRICKS; i++)
+  for (i = 0; i < num_floating_scores; ++i)
     {
-      if (broken_bricks[i].alive)
-        {
-          src.x = rand() % 16;
-          src.y = rand() % 16;
-          src.w = 16;
-          src.h = 16;
+      floating_score_draw(&floating_scores[i]);
+    }
 
-          dest.x = broken_bricks[i].x - scroll_x;
-          dest.y = broken_bricks[i].y;
-          dest.w = 16;
-          dest.h = 16;
 
-          SDL_BlitSurface(img_brick[0], &src, screen, &dest);
-        }
+  /* (Upgrades): */
+
+  for (i = 0; i < num_upgrades; ++i)
+    {
+      upgrade_draw(&upgrades[i]);
     }
 
 
-  /* (Status): */
+  /* (Bouncy distros): */
+
+  for (i = 0; i < num_bouncy_distros; ++i)
+    {
+      bouncy_distro_draw(&bouncy_distros[i]);
+    }
 
-  sprintf(str, "%d", score);
-  drawtext("SCORE", 0, 0, letters_blue, NO_UPDATE);
-  drawtext(str, 96, 0, letters_gold, NO_UPDATE);
 
-  sprintf(str, "%d", highscore);
-  drawtext("HIGH", 0, 20, letters_blue, NO_UPDATE);
-  drawtext(str, 96, 20, letters_gold, NO_UPDATE);
+  /* (Broken bricks): */
 
-  if (time_left >= 50 || (frame % 10) < 5)
+  for (i = 0; i < num_broken_bricks; ++i)
     {
-      sprintf(str, "%d", time_left);
-      drawtext("TIME", 224, 0, letters_blue, NO_UPDATE);
-      drawtext(str, 304, 0, letters_gold, NO_UPDATE);
+      broken_brick_draw(&broken_bricks[i]);
     }
 
-  sprintf(str, "%d", distros);
-  drawtext("DISTROS", 480, 0, letters_blue, NO_UPDATE);
-  drawtext(str, 608, 0, letters_gold, NO_UPDATE);
+  drawstatus();
 
-  drawtext("LIVES", 480, 20, letters_blue, NO_UPDATE);
 
-  for(i=0; i < lives; ++i)
+  if(game_pause)
     {
-      drawimage(tux_life,565+(18*i),20,NO_UPDATE);
+      x = screen->h / 20;
+      for(i = 0; i < x; ++i)
+        {
+          fillrect(i % 2 ? (pause_menu_frame * i)%screen->w : -((pause_menu_frame * i)%screen->w) ,(i*20+pause_menu_frame)%screen->h,screen->w,10,20,20,20, rand() % 20 + 1);
+        }
+      fillrect(0,0,screen->w,screen->h,rand() % 50, rand() % 50, rand() % 50, 128);
+      text_drawf(&blue_text, "PAUSE - Press 'P' To Play", 0, 230, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
     }
-    
-  if(game_pause)
-    drawcenteredtext("PAUSE",230,letters_red, NO_UPDATE);
 
   if(show_menu)
-    done = drawmenu();
+    menu_process_current();
 
   /* (Update it all!) */
 
@@ -2183,10 +539,14 @@ void game_draw()
 
 /* --- GAME LOOP! --- */
 
-int gameloop(void)
+int gameloop(char * subset, int levelnb, int mode)
 {
+  int fps_cnt, jump, done;
+  timer_type fps_timer, frame_timer;
 
-  Uint32 last_time, now_time;
+  level = levelnb;
+  st_gl_mode = mode;
+  strcpy(level_subset,subset);
 
   /* Clear screen: */
 
@@ -2195,38 +555,91 @@ int gameloop(void)
 
 
   /* Init the game: */
+  arrays_init();
+
+  menu_reset();
+  menu_set_current(&game_menu);
 
-  initmenu();
   initgame();
   loadshared();
-  loadlevel();
-  loadlevelgfx();
-  loadlevelsong();
-  highscore = load_hs();
+  set_defaults();
+
+  if(level_load(&current_level,level_subset,level) != 0)
+    exit(1);
+  level_load_gfx(&current_level);
+  activate_bad_guys();
+  level_load_song(&current_level);
+  if(st_gl_mode == ST_GL_PLAY)
+    load_hs();
+
+  player_init(&tux);
+
+  if(st_gl_mode == ST_GL_PLAY)
+    levelintro();
 
+  start_timers();
 
   /* --- MAIN GAME LOOP!!! --- */
 
+  jump = NO;
   done = 0;
   quit = 0;
   frame = 0;
-  tux_frame_main = 0;
-  tux_frame = 0;
   game_pause = 0;
+  timer_init(&fps_timer);
+  timer_init(&frame_timer);
+  fps_cnt = 0;
 
+    while (SDL_PollEvent(&event))
+    {}
+  
   game_draw();
   do
     {
-      last_time = SDL_GetTicks();
-      frame++;
+      jump = NO;
+
+      /* Calculate the movement-factor */
+      frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
+
+      if(!timer_check(&frame_timer))
+        {
+          timer_start(&frame_timer,25);
+          ++frame;
+        }
 
 
       /* Handle events: */
 
-      old_fire = fire;
+      tux.input.old_fire = tux.input.fire;
+
+      /*printf("%lf\n",frame_ratio);*/
 
       game_event();
 
+      if(show_menu)
+        {
+          if(current_menu == &game_menu)
+            {
+              switch (menu_check(&game_menu))
+                {
+                case 0:
+                  st_pause_ticks_stop();
+                  break;
+                case 1:
+                  savegame();
+                  break;
+                case 4:
+                  done = 1;
+                  break;
+                }
+            }
+          else if(current_menu == &options_menu)
+            {
+              process_options_menu();
+            }
+        }
+
+
       /* Handle actions: */
 
       if(!game_pause && !show_menu)
@@ -2239,30 +652,23 @@ int gameloop(void)
             }
         }
       else
-        SDL_Delay(50);
+        {
+          ++pause_menu_frame;
+          SDL_Delay(50);
+        }
+
+      if(tux.input.down == DOWN)
+        SDL_Delay(30);
 
       /*Draw the current scene to the screen */
+      /*If the machine running the game is too slow
+        skip the drawing of the frame (so the calculations are more precise and
+      the FPS aren't affected).*/
+      /*if( ! fps_fps < 50.0 )
+      game_draw();
+      else
+      jump = YES;*/ /*FIXME: Implement this tweak right.*/
       game_draw();
-
-      /* Keep playing music: */
-
-
-      if (!playing_music())
-        {
-          switch (current_music)
-            {
-            case LEVEL_MUSIC:
-              play_music(level_song, 1);
-              break;
-            case HERRING_MUSIC:
-              play_music(herring_song, 1);
-              break;
-            case HURRYUP_MUSIC: // keep the compiler happy
-            case NO_MUSIC:      // keep the compiler happy for the moment :-)
-            {}
-              /*default:*/
-            }
-        }
 
       /* Time stops in pause mode */
       if(game_pause || show_menu )
@@ -2270,644 +676,453 @@ int gameloop(void)
           continue;
         }
 
-      /* Pause til next frame: */
+      /* Set the time the last update and the time of the current update */
+      last_update_time = update_time;
+      update_time = st_get_ticks();
+
+
+      /* Pause till next frame, if the machine running the game is too fast: */
+      /* FIXME: Works great for in OpenGl mode, where the CPU doesn't have to do that much. But
+                the results in SDL mode aren't perfect (thought the 100 FPS are reached), even on an AMD2500+. */
+      if(last_update_time >= update_time - 12 && jump != YES )
+        SDL_Delay(10);
+      //if((update_time - last_update_time) < 10)
+      //  SDL_Delay((11 - (update_time - last_update_time))/2);
 
-      now_time = SDL_GetTicks();
-      if (now_time < last_time + FPS)
-       SDL_Delay(last_time + FPS - now_time);
 
 
       /* Handle time: */
 
-      if ((frame % 10) == 0 && time_left > 0)
+      if (timer_check(&time_left))
         {
-          time_left--;
+          /* are we low on time ? */
+          if ((timer_get_left(&time_left) < TIME_WARNING)
+              && (current_music != HURRYUP_MUSIC))
+            {
+              current_music = HURRYUP_MUSIC;
+              /* stop the others music, prepare to play the fast music */
+              if (playing_music())
+                {
+                  halt_music();
+                }
+            }
 
-          if (time_left <= 0)
-            killtux(KILL);
         }
-    }
-  while (!done && !quit);
-
-  if (playing_music())
-    halt_music();
-
-  unloadlevelgfx();
-  unloadlevelsong();
-  unloadshared();
-
-  return(quit);
-}
-
-
-/* Initialize the game stuff: */
-
-void initgame(void)
-{
-  level = 1;
-  score = 0;
-  distros = 0;
-  lives = 3;
-}
-
-
-
-/* Load data for this level: */
-
-void loadlevel(void)
-{
-  int i, x, y;
-  FILE * fi;
-  char * filename;
-  char str[80];
-  char * line;
-
-
-  /* Reset arrays: */
-
-  for (i = 0; i < NUM_BOUNCY_DISTROS; i++)
-    bouncy_distros[i].alive = NO;
-
-  for (i = 0; i < NUM_BROKEN_BRICKS; i++)
-    broken_bricks[i].alive = NO;
-
-  for (i = 0; i < NUM_BOUNCY_BRICKS; i++)
-    bouncy_bricks[i].alive = NO;
-
-  for (i = 0; i < NUM_BAD_GUYS; i++)
-    bad_guys[i].alive = NO;
-
-  for (i = 0; i < NUM_FLOATING_SCORES; i++)
-    floating_scores[i].alive = NO;
-
-  for (i = 0; i < NUM_UPGRADES; i++)
-    upgrades[i].alive = NO;
-
-  for (i = 0; i < NUM_BULLETS; i++)
-    bullets[i].alive = NO;
-
-
-  /* Load data file: */
-
-  filename = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) + 20));
-  sprintf(filename, "%s/levels/level%d.dat", DATA_PREFIX, level);
-  fi = fopen(filename, "r");
-  if (fi == NULL)
-    {
-      perror(filename);
-      st_shutdown();
-      free(filename);
-      exit(-1);
-    }
-  free(filename);
-
-
-  /* Load header info: */
-
-
-  /* (Level title) */
-  fgets(str, 20, fi);
-  strcpy(levelname, str);
-  levelname[strlen(levelname)-1] = '\0';
-
-  /* (Level theme) */
-  fgets(str, 20, fi);
-  strcpy(leveltheme, str);
-  leveltheme[strlen(leveltheme)-1] = '\0';
-
-
-
-  /* (Time to beat level) */
-  fgets(str, 10, fi);
-  time_left = atoi(str);
-
-  /* (Song file for this level) */
-  fgets(str, 20, fi);
-  strcpy(song_title, str);
-  song_title[strlen(song_title)-1] = '\0';
-
-
-
-  /* (Level background color) */
-  fgets(str, 10, fi);
-  bkgd_red = atoi(str);
-  fgets(str, 10, fi);
-  bkgd_green= atoi(str);
-  fgets(str, 10, fi);
-  bkgd_blue = atoi(str);
-
-  /* (Level width) */
-  fgets(str, 10, fi);
-  level_width = atoi(str);
-
-
-  /* Allocate some space for the line-reading! */
-
-  line = (char *) malloc(sizeof(char) * (level_width + 5));
-  if (line == NULL)
-    {
-      fprintf(stderr, "Couldn't allocate space to load level data!");
-      exit(1);
-    }
+      else
+        player_kill(&tux,KILL);
 
 
-  /* Load the level lines: */
+      /* Keep playing the correct music: */
 
-  for (y = 0; y < 15; y++)
-    {
-      if(fgets(line, level_width + 5, fi) == NULL)
+      if (!playing_music())
         {
-          fprintf(stderr, "Level %s isn't complete!\n",levelname);
-          exit(1);
+          play_current_music();
         }
-      line[strlen(line) - 1] = '\0';
-      tiles[y] = strdup(line);
-    }
 
-  fclose(fi);
-
-
-  /* Activate bad guys: */
-
-  for (y = 0; y < 15; y++)
-    {
-      for (x = 0; x < level_width; x++)
+      /* Calculate frames per second */
+      if(show_fps)
         {
-          if (tiles[y][x] >= '0' && tiles[y][x] <= '9')
+          ++fps_cnt;
+          fps_fps = (1000.0 / (float)timer_get_gone(&fps_timer)) * (float)fps_cnt;
+
+          if(!timer_check(&fps_timer))
             {
-              add_bad_guy(x * 32, y * 32, tiles[y][x] - '0');
-              tiles[y][x] = '.';
+              timer_start(&fps_timer,1000);
+              fps_cnt = 0;
             }
         }
-    }
-
-
-  /* Set defaults: */
-
-  tux_x = 0;
-  tux_xm = 0;
-  tux_y = 240;
-  tux_ym = 0;
-  tux_dir = RIGHT;
-  tux_size = SMALL;
-  tux_got_coffee = NO;
-  tux_invincible_time = 0;
-  tux_duck = NO;
-
-  tux_dying = NO;
-  tux_safe = TUX_SAFE_TIME;
-
-  jumping = NO;
-  jump_counter = 0;
-
-  tux_skidding = 0;
-
-  scroll_x = 0;
 
-  right = UP;
-  left = UP;
-  up = UP;
-  down = UP;
-  fire = UP;
-  old_fire = UP;
-
-  score_multiplier = 1;
-  super_bkgd_time = 0;
-
-  counting_distros = NO;
-  distro_counter = 0;
-
-  endpos = 0;
-
-  /* set current song/music */
-  current_music = LEVEL_MUSIC;
-
-  /* Level Intro: */
-
-  clearscreen(0, 0, 0);
-
-  sprintf(str, "LEVEL %d", level);
-  drawcenteredtext(str, 200, letters_red, NO_UPDATE);
-
-  sprintf(str, "%s", levelname);
-  drawcenteredtext(str, 224, letters_gold, NO_UPDATE);
-
-  sprintf(str, "TUX x %d", lives);
-  drawcenteredtext(str, 256, letters_blue, NO_UPDATE);
-
-  SDL_Flip(screen);
-
-  SDL_Delay(1000);
-
-
-}
-
-
-/* Load a level-specific graphic... */
+    }
+  while (!done && !quit);
 
-SDL_Surface * load_level_image(char * file, int use_alpha)
-{
-  char fname[1024];
+  if (playing_music())
+    halt_music();
 
-  snprintf(fname, 1024, "%s/images/%s/%s", DATA_PREFIX, leveltheme, file);
+  level_free_gfx();
+  level_free(&current_level);
+  level_free_song();
+  unloadshared();
+  arrays_free();
 
-  return(load_image(fname, use_alpha));
+  return(quit);
 }
 
 
-/* Load graphics: */
+/* Initialize the game stuff: */
 
-void loadlevelgfx(void)
+void initgame(void)
 {
-  img_brick[0] = load_level_image("brick0.png", IGNORE_ALPHA);
-  img_brick[1] = load_level_image("brick1.png", IGNORE_ALPHA);
-
-  img_solid[0] = load_level_image("solid0.png", USE_ALPHA);
-  img_solid[1] = load_level_image("solid1.png", USE_ALPHA);
-  img_solid[2] = load_level_image("solid2.png", USE_ALPHA);
-  img_solid[3] = load_level_image("solid3.png", USE_ALPHA);
-
-  img_bkgd[0][0] = load_level_image("bkgd-00.png", USE_ALPHA);
-  img_bkgd[0][1] = load_level_image("bkgd-01.png", USE_ALPHA);
-  img_bkgd[0][2] = load_level_image("bkgd-02.png", USE_ALPHA);
-  img_bkgd[0][3] = load_level_image("bkgd-03.png", USE_ALPHA);
-
-  img_bkgd[1][0] = load_level_image("bkgd-10.png", USE_ALPHA);
-  img_bkgd[1][1] = load_level_image("bkgd-11.png", USE_ALPHA);
-  img_bkgd[1][2] = load_level_image("bkgd-12.png", USE_ALPHA);
-  img_bkgd[1][3] = load_level_image("bkgd-13.png", USE_ALPHA);
+  score = 0;
+  distros = 0;
 }
 
+/* Load graphics/sounds shared between all levels: */
 
-/* Load music: */
-
-void loadlevelsong(void)
+void loadshared(void)
 {
+  int i;
+  char * herring_song_path; /* for loading herring song*/
 
-  char * song_path;
+  /* Tuxes: */
 
-  song_path = (char *) malloc(sizeof(char) * (strlen(DATA_PREFIX) +
-                              strlen(song_title) + 8));
+  texture_load(&tux_right[0],DATA_PREFIX "/images/shared/tux-right-0.png", USE_ALPHA);
+  texture_load(&tux_right[1],DATA_PREFIX "/images/shared/tux-right-1.png", USE_ALPHA);
+  texture_load(&tux_right[2],DATA_PREFIX "/images/shared/tux-right-2.png", USE_ALPHA);
 
-  sprintf(song_path, "%s/music/%s", DATA_PREFIX, song_title);
+  texture_load(&tux_left[0],DATA_PREFIX "/images/shared/tux-left-0.png", USE_ALPHA);
+  texture_load(&tux_left[1],DATA_PREFIX "/images/shared/tux-left-1.png", USE_ALPHA);
+  texture_load(&tux_left[2],DATA_PREFIX "/images/shared/tux-left-2.png", USE_ALPHA);
 
-  level_song = load_song(song_path);
+  texture_load(&firetux_right[0],DATA_PREFIX "/images/shared/firetux-right-0.png", USE_ALPHA);
+  texture_load(&firetux_right[1],DATA_PREFIX "/images/shared/firetux-right-1.png", USE_ALPHA);
+  texture_load(&firetux_right[2],DATA_PREFIX "/images/shared/firetux-right-2.png", USE_ALPHA);
 
-  free(song_path);
-}
+  texture_load(&firetux_left[0],DATA_PREFIX "/images/shared/firetux-left-0.png", USE_ALPHA);
+  texture_load(&firetux_left[1],DATA_PREFIX "/images/shared/firetux-left-1.png", USE_ALPHA);
+  texture_load(&firetux_left[2],DATA_PREFIX "/images/shared/firetux-left-2.png", USE_ALPHA);
 
 
-/* Free graphics data for this level: */
+  texture_load(&cape_right[0] ,DATA_PREFIX "/images/shared/cape-right-0.png",
+               USE_ALPHA);
 
-void unloadlevelgfx(void)
-{
-  int i;
+  texture_load(&cape_right[1] ,DATA_PREFIX "/images/shared/cape-right-1.png",
+               USE_ALPHA);
 
-  for (i = 0; i < 2; i++)
-    {
-      SDL_FreeSurface(img_brick[i]);
-    }
-  for (i = 0; i < 4; i++)
-    {
-      SDL_FreeSurface(img_solid[i]);
-      SDL_FreeSurface(img_bkgd[0][i]);
-      SDL_FreeSurface(img_bkgd[1][i]);
-    }
-}
+  texture_load(&cape_left[0] ,DATA_PREFIX "/images/shared/cape-left-0.png",
+               USE_ALPHA);
 
+  texture_load(&cape_left[1] ,DATA_PREFIX "/images/shared/cape-left-1.png",
+               USE_ALPHA);
 
-/* Free music data for this level: */
+  texture_load(&bigtux_right[0] ,DATA_PREFIX "/images/shared/bigtux-right-0.png",
+               USE_ALPHA);
 
-void unloadlevelsong(void)
-{
-  free_music(level_song);
-}
+  texture_load(&bigtux_right[1] ,DATA_PREFIX "/images/shared/bigtux-right-1.png",
+               USE_ALPHA);
 
+  texture_load(&bigtux_right[2] ,DATA_PREFIX "/images/shared/bigtux-right-2.png",
+               USE_ALPHA);
 
-/* Load graphics/sounds shared between all levels: */
+  texture_load(&bigtux_right_jump ,DATA_PREFIX "/images/shared/bigtux-right-jump.png", USE_ALPHA);
 
-void loadshared(void)
-{
-  int i;
-  char * herring_song_path; /* for loading herring song*/
+  texture_load(&bigtux_left[0] ,DATA_PREFIX "/images/shared/bigtux-left-0.png",
+               USE_ALPHA);
 
-  /* Tuxes: */
+  texture_load(&bigtux_left[1] ,DATA_PREFIX "/images/shared/bigtux-left-1.png",
+               USE_ALPHA);
 
-  tux_right[0] = load_image(DATA_PREFIX "/images/shared/tux-right-0.png",
-                            USE_ALPHA);
+  texture_load(&bigtux_left[2] ,DATA_PREFIX "/images/shared/bigtux-left-2.png",
+               USE_ALPHA);
+
+  texture_load(&bigtux_left_jump ,DATA_PREFIX "/images/shared/bigtux-left-jump.png", USE_ALPHA);
 
-  tux_right[1] = load_image(DATA_PREFIX "/images/shared/tux-right-1.png",
-                            USE_ALPHA);
+  texture_load(&bigcape_right[0] ,DATA_PREFIX "/images/shared/bigcape-right-0.png",
+               USE_ALPHA);
 
-  tux_right[2] = load_image(DATA_PREFIX "/images/shared/tux-right-2.png",
-                            USE_ALPHA);
+  texture_load(&bigcape_right[1] ,DATA_PREFIX "/images/shared/bigcape-right-1.png",
+               USE_ALPHA);
 
-  tux_left[0] = load_image(DATA_PREFIX "/images/shared/tux-left-0.png",
-                           USE_ALPHA);
+  texture_load(&bigcape_left[0] ,DATA_PREFIX "/images/shared/bigcape-left-0.png",
+               USE_ALPHA);
 
-  tux_left[1] = load_image(DATA_PREFIX "/images/shared/tux-left-1.png",
-                           USE_ALPHA);
+  texture_load(&bigcape_left[1] ,DATA_PREFIX "/images/shared/bigcape-left-1.png",
+               USE_ALPHA);
 
-  tux_left[2] = load_image(DATA_PREFIX "/images/shared/tux-left-2.png",
-                           USE_ALPHA);
+  texture_load(&bigfiretux_right[0] ,DATA_PREFIX "/images/shared/bigfiretux-right-0.png",
+               USE_ALPHA);
 
-  cape_right[0] = load_image(DATA_PREFIX "/images/shared/cape-right-0.png",
-                             USE_ALPHA);
+  texture_load(&bigfiretux_right[1] ,DATA_PREFIX "/images/shared/bigfiretux-right-1.png",
+               USE_ALPHA);
 
-  cape_right[1] = load_image(DATA_PREFIX "/images/shared/cape-right-1.png",
-                             USE_ALPHA);
+  texture_load(&bigfiretux_right[2] ,DATA_PREFIX "/images/shared/bigfiretux-right-2.png",
+               USE_ALPHA);
 
-  cape_left[0] = load_image(DATA_PREFIX "/images/shared/cape-left-0.png",
-                            USE_ALPHA);
+  texture_load(&bigfiretux_right_jump ,DATA_PREFIX "/images/shared/bigfiretux-right-jump.png", USE_ALPHA);
 
-  cape_left[1] = load_image(DATA_PREFIX "/images/shared/cape-left-1.png",
-                            USE_ALPHA);
+  texture_load(&bigfiretux_left[0] ,DATA_PREFIX "/images/shared/bigfiretux-left-0.png",
+               USE_ALPHA);
 
-  bigtux_right[0] = load_image(DATA_PREFIX "/images/shared/bigtux-right-0.png",
-                               USE_ALPHA);
+  texture_load(&bigfiretux_left[1] ,DATA_PREFIX "/images/shared/bigfiretux-left-1.png",
+               USE_ALPHA);
 
-  bigtux_right[1] = load_image(DATA_PREFIX "/images/shared/bigtux-right-1.png",
-                               USE_ALPHA);
+  texture_load(&bigfiretux_left[2] ,DATA_PREFIX "/images/shared/bigfiretux-left-2.png",
+               USE_ALPHA);
 
-  bigtux_right[2] = load_image(DATA_PREFIX "/images/shared/bigtux-right-2.png",
-                               USE_ALPHA);
+  texture_load(&bigfiretux_left_jump ,DATA_PREFIX "/images/shared/bigfiretux-left-jump.png", USE_ALPHA);
 
-  bigtux_right_jump =
-    load_image(DATA_PREFIX "/images/shared/bigtux-right-jump.png", USE_ALPHA);
+  texture_load(&bigcape_right[0] ,DATA_PREFIX "/images/shared/bigcape-right-0.png",
+               USE_ALPHA);
 
-  bigtux_left[0] = load_image(DATA_PREFIX "/images/shared/bigtux-left-0.png",
-                              USE_ALPHA);
+  texture_load(&bigcape_right[1] ,DATA_PREFIX "/images/shared/bigcape-right-1.png",
+               USE_ALPHA);
 
-  bigtux_left[1] = load_image(DATA_PREFIX "/images/shared/bigtux-left-1.png",
-                              USE_ALPHA);
+  texture_load(&bigcape_left[0] ,DATA_PREFIX "/images/shared/bigcape-left-0.png",
+               USE_ALPHA);
 
-  bigtux_left[2] = load_image(DATA_PREFIX "/images/shared/bigtux-left-2.png",
-                              USE_ALPHA);
+  texture_load(&bigcape_left[1] ,DATA_PREFIX "/images/shared/bigcape-left-1.png",
+               USE_ALPHA);
 
-  bigtux_left_jump =
-    load_image(DATA_PREFIX "/images/shared/bigtux-left-jump.png", USE_ALPHA);
 
-  bigcape_right[0] =
-    load_image(DATA_PREFIX "/images/shared/bigcape-right-0.png",
+  texture_load(&ducktux_right ,DATA_PREFIX
+               "/images/shared/ducktux-right.png",
                USE_ALPHA);
 
-  bigcape_right[1] =
-    load_image(DATA_PREFIX "/images/shared/bigcape-right-1.png",
+  texture_load(&ducktux_left ,DATA_PREFIX
+               "/images/shared/ducktux-left.png",
                USE_ALPHA);
 
-  bigcape_left[0] =
-    load_image(DATA_PREFIX "/images/shared/bigcape-left-0.png",
+  texture_load(&skidtux_right ,DATA_PREFIX
+               "/images/shared/skidtux-right.png",
                USE_ALPHA);
 
-  bigcape_left[1] =
-    load_image(DATA_PREFIX "/images/shared/bigcape-left-1.png",
+  texture_load(&skidtux_left ,DATA_PREFIX
+               "/images/shared/skidtux-left.png",
                USE_ALPHA);
 
-  ducktux_right = load_image(DATA_PREFIX
-                             "/images/shared/ducktux-right.png",
-                             USE_ALPHA);
+  texture_load(&duckfiretux_right ,DATA_PREFIX
+               "/images/shared/duckfiretux-right.png",
+               USE_ALPHA);
 
-  ducktux_left = load_image(DATA_PREFIX
-                            "/images/shared/ducktux-left.png",
-                            USE_ALPHA);
+  texture_load(&duckfiretux_left ,DATA_PREFIX
+               "/images/shared/duckfiretux-left.png",
+               USE_ALPHA);
 
-  skidtux_right = load_image(DATA_PREFIX
-                             "/images/shared/skidtux-right.png",
-                             USE_ALPHA);
+  texture_load(&skidfiretux_right ,DATA_PREFIX
+               "/images/shared/skidfiretux-right.png",
+               USE_ALPHA);
 
-  skidtux_left = load_image(DATA_PREFIX
-                            "/images/shared/skidtux-left.png",
-                            USE_ALPHA);
+  texture_load(&skidfiretux_left ,DATA_PREFIX
+               "/images/shared/skidfiretux-left.png",
+               USE_ALPHA);
 
 
   /* Boxes: */
 
-  img_box_full = load_image(DATA_PREFIX "/images/shared/box-full.png",
-                            IGNORE_ALPHA);
-  img_box_empty = load_image(DATA_PREFIX "/images/shared/box-empty.png",
-                             IGNORE_ALPHA);
+  texture_load(&img_box_full ,DATA_PREFIX "/images/shared/box-full.png",
+               IGNORE_ALPHA);
+  texture_load(&img_box_empty ,DATA_PREFIX "/images/shared/box-empty.png",
+               IGNORE_ALPHA);
 
 
   /* Water: */
 
 
-  img_water = load_image(DATA_PREFIX "/images/shared/water.png", IGNORE_ALPHA);
+  texture_load(&img_water ,DATA_PREFIX "/images/shared/water.png", IGNORE_ALPHA);
 
-  img_waves[0] = load_image(DATA_PREFIX "/images/shared/waves-0.png",
-                            USE_ALPHA);
+  texture_load(&img_waves[0] ,DATA_PREFIX "/images/shared/waves-0.png",
+               USE_ALPHA);
 
-  img_waves[1] = load_image(DATA_PREFIX "/images/shared/waves-1.png",
-                            USE_ALPHA);
+  texture_load(&img_waves[1] ,DATA_PREFIX "/images/shared/waves-1.png",
+               USE_ALPHA);
 
-  img_waves[2] = load_image(DATA_PREFIX "/images/shared/waves-2.png",
-                            USE_ALPHA);
+  texture_load(&img_waves[2] ,DATA_PREFIX "/images/shared/waves-2.png",
+               USE_ALPHA);
 
 
   /* Pole: */
 
-  img_pole = load_image(DATA_PREFIX "/images/shared/pole.png", USE_ALPHA);
-  img_poletop = load_image(DATA_PREFIX "/images/shared/poletop.png",
-                           USE_ALPHA);
+  texture_load(&img_pole ,DATA_PREFIX "/images/shared/pole.png", USE_ALPHA);
+  texture_load(&img_poletop ,DATA_PREFIX "/images/shared/poletop.png",
+               USE_ALPHA);
 
 
   /* Flag: */
 
-  img_flag[0] = load_image(DATA_PREFIX "/images/shared/flag-0.png",
-                           USE_ALPHA);
-  img_flag[1] = load_image(DATA_PREFIX "/images/shared/flag-1.png",
-                           USE_ALPHA);
+  texture_load(&img_flag[0] ,DATA_PREFIX "/images/shared/flag-0.png",
+               USE_ALPHA);
+  texture_load(&img_flag[1] ,DATA_PREFIX "/images/shared/flag-1.png",
+               USE_ALPHA);
 
 
   /* Cloud: */
 
-  img_cloud[0][0] = load_image(DATA_PREFIX "/images/shared/cloud-00.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[0][0] ,DATA_PREFIX "/images/shared/cloud-00.png",
+               USE_ALPHA);
 
-  img_cloud[0][1] = load_image(DATA_PREFIX "/images/shared/cloud-01.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[0][1] ,DATA_PREFIX "/images/shared/cloud-01.png",
+               USE_ALPHA);
 
-  img_cloud[0][2] = load_image(DATA_PREFIX "/images/shared/cloud-02.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[0][2] ,DATA_PREFIX "/images/shared/cloud-02.png",
+               USE_ALPHA);
 
-  img_cloud[0][3] = load_image(DATA_PREFIX "/images/shared/cloud-03.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[0][3] ,DATA_PREFIX "/images/shared/cloud-03.png",
+               USE_ALPHA);
 
 
-  img_cloud[1][0] = load_image(DATA_PREFIX "/images/shared/cloud-10.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[1][0] ,DATA_PREFIX "/images/shared/cloud-10.png",
+               USE_ALPHA);
 
-  img_cloud[1][1] = load_image(DATA_PREFIX "/images/shared/cloud-11.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[1][1] ,DATA_PREFIX "/images/shared/cloud-11.png",
+               USE_ALPHA);
 
-  img_cloud[1][2] = load_image(DATA_PREFIX "/images/shared/cloud-12.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[1][2] ,DATA_PREFIX "/images/shared/cloud-12.png",
+               USE_ALPHA);
 
-  img_cloud[1][3] = load_image(DATA_PREFIX "/images/shared/cloud-13.png",
-                               USE_ALPHA);
+  texture_load(&img_cloud[1][3] ,DATA_PREFIX "/images/shared/cloud-13.png",
+               USE_ALPHA);
 
 
   /* Bad guys: */
 
   /* (BSOD) */
 
-  img_bsod_left[0] = load_image(DATA_PREFIX
-                                "/images/shared/bsod-left-0.png",
-                                USE_ALPHA);
+  texture_load(&img_bsod_left[0] ,DATA_PREFIX
+               "/images/shared/bsod-left-0.png",
+               USE_ALPHA);
 
-  img_bsod_left[1] = load_image(DATA_PREFIX
-                                "/images/shared/bsod-left-1.png",
-                                USE_ALPHA);
+  texture_load(&img_bsod_left[1] ,DATA_PREFIX
+               "/images/shared/bsod-left-1.png",
+               USE_ALPHA);
 
-  img_bsod_left[2] = load_image(DATA_PREFIX
-                                "/images/shared/bsod-left-2.png",
-                                USE_ALPHA);
+  texture_load(&img_bsod_left[2] ,DATA_PREFIX
+               "/images/shared/bsod-left-2.png",
+               USE_ALPHA);
 
-  img_bsod_left[3] = load_image(DATA_PREFIX
-                                "/images/shared/bsod-left-3.png",
-                                USE_ALPHA);
+  texture_load(&img_bsod_left[3] ,DATA_PREFIX
+               "/images/shared/bsod-left-3.png",
+               USE_ALPHA);
 
-  img_bsod_right[0] = load_image(DATA_PREFIX
-                                 "/images/shared/bsod-right-0.png",
-                                 USE_ALPHA);
+  texture_load(&img_bsod_right[0] ,DATA_PREFIX
+               "/images/shared/bsod-right-0.png",
+               USE_ALPHA);
 
-  img_bsod_right[1] = load_image(DATA_PREFIX
-                                 "/images/shared/bsod-right-1.png",
-                                 USE_ALPHA);
+  texture_load(&img_bsod_right[1] ,DATA_PREFIX
+               "/images/shared/bsod-right-1.png",
+               USE_ALPHA);
 
-  img_bsod_right[2] = load_image(DATA_PREFIX
-                                 "/images/shared/bsod-right-2.png",
-                                 USE_ALPHA);
+  texture_load(&img_bsod_right[2] ,DATA_PREFIX
+               "/images/shared/bsod-right-2.png",
+               USE_ALPHA);
 
-  img_bsod_right[3] = load_image(DATA_PREFIX
-                                 "/images/shared/bsod-right-3.png",
-                                 USE_ALPHA);
+  texture_load(&img_bsod_right[3] ,DATA_PREFIX
+               "/images/shared/bsod-right-3.png",
+               USE_ALPHA);
 
-  img_bsod_squished_left = load_image(DATA_PREFIX
-                                      "/images/shared/bsod-squished-left.png",
-                                      USE_ALPHA);
+  texture_load(&img_bsod_squished_left ,DATA_PREFIX
+               "/images/shared/bsod-squished-left.png",
+               USE_ALPHA);
 
-  img_bsod_squished_right = load_image(DATA_PREFIX
-                                       "/images/shared/bsod-squished-right.png",
-                                       USE_ALPHA);
+  texture_load(&img_bsod_squished_right ,DATA_PREFIX
+               "/images/shared/bsod-squished-right.png",
+               USE_ALPHA);
 
-  img_bsod_falling_left = load_image(DATA_PREFIX
-                                     "/images/shared/bsod-falling-left.png",
-                                     USE_ALPHA);
+  texture_load(&img_bsod_falling_left ,DATA_PREFIX
+               "/images/shared/bsod-falling-left.png",
+               USE_ALPHA);
 
-  img_bsod_falling_right = load_image(DATA_PREFIX
-                                      "/images/shared/bsod-falling-right.png",
-                                      USE_ALPHA);
+  texture_load(&img_bsod_falling_right ,DATA_PREFIX
+               "/images/shared/bsod-falling-right.png",
+               USE_ALPHA);
 
 
   /* (Laptop) */
 
-  img_laptop_left[0] = load_image(DATA_PREFIX
-                                  "/images/shared/laptop-left-0.png",
-                                  USE_ALPHA);
+  texture_load(&img_laptop_left[0] ,DATA_PREFIX
+               "/images/shared/laptop-left-0.png",
+               USE_ALPHA);
 
-  img_laptop_left[1] = load_image(DATA_PREFIX
-                                  "/images/shared/laptop-left-1.png",
-                                  USE_ALPHA);
+  texture_load(&img_laptop_left[1] ,DATA_PREFIX
+               "/images/shared/laptop-left-1.png",
+               USE_ALPHA);
 
-  img_laptop_left[2] = load_image(DATA_PREFIX
-                                  "/images/shared/laptop-left-2.png",
-                                  USE_ALPHA);
+  texture_load(&img_laptop_left[2] ,DATA_PREFIX
+               "/images/shared/laptop-left-2.png",
+               USE_ALPHA);
 
-  img_laptop_right[0] = load_image(DATA_PREFIX
-                                   "/images/shared/laptop-right-0.png",
-                                   USE_ALPHA);
+  texture_load(&img_laptop_right[0] ,DATA_PREFIX
+               "/images/shared/laptop-right-0.png",
+               USE_ALPHA);
 
-  img_laptop_right[1] = load_image(DATA_PREFIX
-                                   "/images/shared/laptop-right-1.png",
-                                   USE_ALPHA);
+  texture_load(&img_laptop_right[1] ,DATA_PREFIX
+               "/images/shared/laptop-right-1.png",
+               USE_ALPHA);
 
-  img_laptop_right[2] = load_image(DATA_PREFIX
-                                   "/images/shared/laptop-right-2.png",
-                                   USE_ALPHA);
+  texture_load(&img_laptop_right[2] ,DATA_PREFIX
+               "/images/shared/laptop-right-2.png",
+               USE_ALPHA);
 
-  img_laptop_flat_left = load_image(DATA_PREFIX
-                                    "/images/shared/laptop-flat-left.png",
-                                    USE_ALPHA);
+  texture_load(&img_laptop_flat_left ,DATA_PREFIX
+               "/images/shared/laptop-flat-left.png",
+               USE_ALPHA);
 
-  img_laptop_flat_right = load_image(DATA_PREFIX
-                                     "/images/shared/laptop-flat-right.png",
-                                     USE_ALPHA);
+  texture_load(&img_laptop_flat_right ,DATA_PREFIX
+               "/images/shared/laptop-flat-right.png",
+               USE_ALPHA);
 
-  img_laptop_falling_left =
-    load_image(DATA_PREFIX
+  texture_load(&img_laptop_falling_left ,DATA_PREFIX
                "/images/shared/laptop-falling-left.png",
                USE_ALPHA);
 
-  img_laptop_falling_right =
-    load_image(DATA_PREFIX
+  texture_load(&img_laptop_falling_right ,DATA_PREFIX
                "/images/shared/laptop-falling-right.png",
                USE_ALPHA);
 
 
   /* (Money) */
 
-  img_money_left[0] = load_image(DATA_PREFIX
-                                 "/images/shared/bag-left-0.png",
-                                 USE_ALPHA);
+  texture_load(&img_money_left[0] ,DATA_PREFIX
+               "/images/shared/bag-left-0.png",
+               USE_ALPHA);
 
-  img_money_left[1] = load_image(DATA_PREFIX
-                                 "/images/shared/bag-left-1.png",
-                                 USE_ALPHA);
+  texture_load(&img_money_left[1] ,DATA_PREFIX
+               "/images/shared/bag-left-1.png",
+               USE_ALPHA);
 
-  img_money_right[0] = load_image(DATA_PREFIX
-                                  "/images/shared/bag-right-0.png",
-                                  USE_ALPHA);
+  texture_load(&img_money_right[0] ,DATA_PREFIX
+               "/images/shared/bag-right-0.png",
+               USE_ALPHA);
 
-  img_money_right[1] = load_image(DATA_PREFIX
-                                  "/images/shared/bag-right-1.png",
-                                  USE_ALPHA);
+  texture_load(&img_money_right[1] ,DATA_PREFIX
+               "/images/shared/bag-right-1.png",
+               USE_ALPHA);
 
 
 
   /* Upgrades: */
 
-  img_mints = load_image(DATA_PREFIX "/images/shared/mints.png", USE_ALPHA);
-  img_coffee = load_image(DATA_PREFIX "/images/shared/coffee.png", USE_ALPHA);
+  texture_load(&img_mints ,DATA_PREFIX "/images/shared/mints.png", USE_ALPHA);
+  texture_load(&img_coffee ,DATA_PREFIX "/images/shared/coffee.png", USE_ALPHA);
 
 
   /* Weapons: */
 
-  img_bullet = load_image(DATA_PREFIX "/images/shared/bullet.png", USE_ALPHA);
+  texture_load(&img_bullet ,DATA_PREFIX "/images/shared/bullet.png", USE_ALPHA);
+
+  texture_load(&img_red_glow ,DATA_PREFIX "/images/shared/red-glow.png",
+               USE_ALPHA);
 
-  img_red_glow = load_image(DATA_PREFIX "/images/shared/red-glow.png",
-                            USE_ALPHA);
 
 
   /* Distros: */
 
-  img_distro[0] = load_image(DATA_PREFIX "/images/shared/distro-0.png",
-                             USE_ALPHA);
+  texture_load(&img_distro[0] ,DATA_PREFIX "/images/shared/distro-0.png",
+               USE_ALPHA);
+
+  texture_load(&img_distro[1] ,DATA_PREFIX "/images/shared/distro-1.png",
+               USE_ALPHA);
 
-  img_distro[1] = load_image(DATA_PREFIX "/images/shared/distro-1.png",
-                             USE_ALPHA);
+  texture_load(&img_distro[2] ,DATA_PREFIX "/images/shared/distro-2.png",
+               USE_ALPHA);
 
-  img_distro[2] = load_image(DATA_PREFIX "/images/shared/distro-2.png",
-                             USE_ALPHA);
+  texture_load(&img_distro[3] ,DATA_PREFIX "/images/shared/distro-3.png",
+               USE_ALPHA);
 
-  img_distro[3] = load_image(DATA_PREFIX "/images/shared/distro-3.png",
-                             USE_ALPHA);
 
   /* Tux life: */
 
-  tux_life = load_image(DATA_PREFIX "/images/shared/tux-life.png",
-                        USE_ALPHA);
+  texture_load(&tux_life ,DATA_PREFIX "/images/shared/tux-life.png",
+               USE_ALPHA);
 
   /* Herring: */
 
-  img_golden_herring =
-    load_image(DATA_PREFIX "/images/shared/golden-herring.png",
+  texture_load(&img_golden_herring, DATA_PREFIX "/images/shared/golden-herring.png",
                USE_ALPHA);
 
 
   /* Super background: */
 
-  img_super_bkgd = load_image(DATA_PREFIX "/images/shared/super-bkgd.png",
-                              IGNORE_ALPHA);
+  texture_load(&img_super_bkgd ,DATA_PREFIX "/images/shared/super-bkgd.png",
+               IGNORE_ALPHA);
 
 
   /* Sound effects: */
@@ -2943,83 +1158,83 @@ void unloadshared(void)
 
   for (i = 0; i < 3; i++)
     {
-      SDL_FreeSurface(tux_right[i]);
-      SDL_FreeSurface(tux_left[i]);
-      SDL_FreeSurface(bigtux_right[i]);
-      SDL_FreeSurface(bigtux_left[i]);
+      texture_free(&tux_right[i]);
+      texture_free(&tux_left[i]);
+      texture_free(&bigtux_right[i]);
+      texture_free(&bigtux_left[i]);
     }
 
-  SDL_FreeSurface(bigtux_right_jump);
-  SDL_FreeSurface(bigtux_left_jump);
+  texture_free(&bigtux_right_jump);
+  texture_free(&bigtux_left_jump);
 
   for (i = 0; i < 2; i++)
     {
-      SDL_FreeSurface(cape_right[i]);
-      SDL_FreeSurface(cape_left[i]);
-      SDL_FreeSurface(bigcape_right[i]);
-      SDL_FreeSurface(bigcape_left[i]);
+      texture_free(&cape_right[i]);
+      texture_free(&cape_left[i]);
+      texture_free(&bigcape_right[i]);
+      texture_free(&bigcape_left[i]);
     }
 
-  SDL_FreeSurface(ducktux_left);
-  SDL_FreeSurface(ducktux_right);
+  texture_free(&ducktux_left);
+  texture_free(&ducktux_right);
 
-  SDL_FreeSurface(skidtux_left);
-  SDL_FreeSurface(skidtux_right);
+  texture_free(&skidtux_left);
+  texture_free(&skidtux_right);
 
   for (i = 0; i < 4; i++)
     {
-      SDL_FreeSurface(img_bsod_left[i]);
-      SDL_FreeSurface(img_bsod_right[i]);
+      texture_free(&img_bsod_left[i]);
+      texture_free(&img_bsod_right[i]);
     }
 
-  SDL_FreeSurface(img_bsod_squished_left);
-  SDL_FreeSurface(img_bsod_squished_right);
+  texture_free(&img_bsod_squished_left);
+  texture_free(&img_bsod_squished_right);
 
-  SDL_FreeSurface(img_bsod_falling_left);
-  SDL_FreeSurface(img_bsod_falling_right);
+  texture_free(&img_bsod_falling_left);
+  texture_free(&img_bsod_falling_right);
 
   for (i = 0; i < 3; i++)
     {
-      SDL_FreeSurface(img_laptop_left[i]);
-      SDL_FreeSurface(img_laptop_right[i]);
+      texture_free(&img_laptop_left[i]);
+      texture_free(&img_laptop_right[i]);
     }
 
-  SDL_FreeSurface(img_laptop_flat_left);
-  SDL_FreeSurface(img_laptop_flat_right);
+  texture_free(&img_laptop_flat_left);
+  texture_free(&img_laptop_flat_right);
 
-  SDL_FreeSurface(img_laptop_falling_left);
-  SDL_FreeSurface(img_laptop_falling_right);
+  texture_free(&img_laptop_falling_left);
+  texture_free(&img_laptop_falling_right);
 
   for (i = 0; i < 2; i++)
     {
-      SDL_FreeSurface(img_money_left[i]);
-      SDL_FreeSurface(img_money_right[i]);
+      texture_free(&img_money_left[i]);
+      texture_free(&img_money_right[i]);
     }
 
-  SDL_FreeSurface(img_box_full);
-  SDL_FreeSurface(img_box_empty);
+  texture_free(&img_box_full);
+  texture_free(&img_box_empty);
 
-  SDL_FreeSurface(img_water);
+  texture_free(&img_water);
   for (i = 0; i < 3; i++)
-    SDL_FreeSurface(img_waves[i]);
+    texture_free(&img_waves[i]);
 
-  SDL_FreeSurface(img_pole);
-  SDL_FreeSurface(img_poletop);
+  texture_free(&img_pole);
+  texture_free(&img_poletop);
 
   for (i = 0; i < 2; i++)
-    SDL_FreeSurface(img_flag[i]);
+    texture_free(&img_flag[i]);
 
-  SDL_FreeSurface(img_mints);
-  SDL_FreeSurface(img_coffee);
+  texture_free(&img_mints);
+  texture_free(&img_coffee);
 
   for (i = 0; i < 4; i++)
     {
-      SDL_FreeSurface(img_distro[i]);
-      SDL_FreeSurface(img_cloud[0][i]);
-      SDL_FreeSurface(img_cloud[1][i]);
+      texture_free(&img_distro[i]);
+      texture_free(&img_cloud[0][i]);
+      texture_free(&img_cloud[1][i]);
     }
 
-  SDL_FreeSurface(img_golden_herring);
+  texture_free(&img_golden_herring);
 
   for (i = 0; i < NUM_SOUNDS; i++)
     free_chunk(sounds[i]);
@@ -3031,207 +1246,199 @@ void unloadshared(void)
 
 /* Draw a tile on the screen: */
 
-void drawshape(int x, int y, unsigned char c)
+void drawshape(float x, float y, unsigned char c)
 {
   int z;
 
   if (c == 'X' || c == 'x')
-    drawimage(img_brick[0], x, y, NO_UPDATE);
+    texture_draw(&img_brick[0], x, y, NO_UPDATE);
   else if (c == 'Y' || c == 'y')
-    drawimage(img_brick[1], x, y, NO_UPDATE);
+    texture_draw(&img_brick[1], x, y, NO_UPDATE);
   else if (c == 'A' || c =='B' || c == '!')
-    drawimage(img_box_full, x, y, NO_UPDATE);
+    texture_draw(&img_box_full, x, y, NO_UPDATE);
   else if (c == 'a')
-    drawimage(img_box_empty, x, y, NO_UPDATE);
+    texture_draw(&img_box_empty, x, y, NO_UPDATE);
   else if (c >= 'C' && c <= 'F')
-    drawimage(img_cloud[0][c - 'C'], x, y, NO_UPDATE);
+    texture_draw(&img_cloud[0][c - 'C'], x, y, NO_UPDATE);
   else if (c >= 'c' && c <= 'f')
-    drawimage(img_cloud[1][c - 'c'], x, y, NO_UPDATE);
+    texture_draw(&img_cloud[1][c - 'c'], x, y, NO_UPDATE);
   else if (c >= 'G' && c <= 'J')
-    drawimage(img_bkgd[0][c - 'G'], x, y, NO_UPDATE);
+    texture_draw(&img_bkgd_tile[0][c - 'G'], x, y, NO_UPDATE);
   else if (c >= 'g' && c <= 'j')
-    drawimage(img_bkgd[1][c - 'g'], x, y, NO_UPDATE);
+    texture_draw(&img_bkgd_tile[1][c - 'g'], x, y, NO_UPDATE);
   else if (c == '#')
-    drawimage(img_solid[0], x, y, NO_UPDATE);
+    texture_draw(&img_solid[0], x, y, NO_UPDATE);
   else if (c == '[')
-    drawimage(img_solid[1], x, y, NO_UPDATE);
+    texture_draw(&img_solid[1], x, y, NO_UPDATE);
   else if (c == '=')
-    drawimage(img_solid[2], x, y, NO_UPDATE);
+    texture_draw(&img_solid[2], x, y, NO_UPDATE);
   else if (c == ']')
-    drawimage(img_solid[3], x, y, NO_UPDATE);
+    texture_draw(&img_solid[3], x, y, NO_UPDATE);
   else if (c == '$')
     {
+
       z = (frame / 2) % 6;
 
       if (z < 4)
-        drawimage(img_distro[z], x, y, NO_UPDATE);
+        texture_draw(&img_distro[z], x, y, NO_UPDATE);
       else if (z == 4)
-        drawimage(img_distro[2], x, y, NO_UPDATE);
+        texture_draw(&img_distro[2], x, y, NO_UPDATE);
       else if (z == 5)
-        drawimage(img_distro[1], x, y, NO_UPDATE);
+        texture_draw(&img_distro[1], x, y, NO_UPDATE);
     }
   else if (c == '^')
     {
       z = (frame / 3) % 3;
 
-      drawimage(img_waves[z], x, y, NO_UPDATE);
+      texture_draw(&img_waves[z], x, y, NO_UPDATE);
     }
   else if (c == '*')
-    drawimage(img_poletop, x, y, NO_UPDATE);
+    texture_draw(&img_poletop, x, y, NO_UPDATE);
   else if (c == '|')
     {
-      drawimage(img_pole, x, y, NO_UPDATE);
+      texture_draw(&img_pole, x, y, NO_UPDATE);
 
       /* Mark this as the end position of the level! */
 
-      endpos = x;
+      endpos = (int)x;
     }
   else if (c == '\\')
     {
       z = (frame / 3) % 2;
 
-      drawimage(img_flag[z], x + 16, y, NO_UPDATE);
+      texture_draw(&img_flag[z], x + 16, y, NO_UPDATE);
     }
   else if (c == '&')
-    drawimage(img_water, x, y, NO_UPDATE);
+    texture_draw(&img_water, x, y, NO_UPDATE);
 }
 
 
 /* What shape is at some position? */
 
-unsigned char shape(int x, int y, int sx)
+unsigned char shape(float x, float y)
 {
+
   int xx, yy;
   unsigned char c;
 
-  yy = (y / 32);
-  xx = ((x + sx) / 32);
+  yy = ((int)y / 32);
+  xx = ((int)x / 32);
 
-  if (yy >= 0 && yy <= 15 && xx >= 0 && xx <= level_width)
-    c = tiles[yy][xx];
+  if (yy >= 0 && yy < 15 && xx >= 0 && xx <= current_level.width)
+    {
+      c = current_level.tiles[yy][xx];
+    }
   else
     c = '.';
 
   return(c);
 }
 
-
 /* Is is ground? */
 
-int issolid(int x, int y, int sx)
+
+int issolid(float x, float y)
 {
-  int v;
-
-  v = 0;
-
-  if (isbrick(x, y, sx) ||
-      isbrick(x + 31, y, sx) ||
-      isice(x, y, sx) ||
-      isice(x + 31, y, sx) ||
-      (shape(x, y, sx) == '[' ||
-       shape(x + 31, y, sx) == '[') ||
-      (shape(x, y, sx) == '=' ||
-       shape(x + 31, y, sx) == '=') ||
-      (shape(x, y, sx) == ']' ||
-       shape(x + 31, y, sx) == ']') ||
-      (shape(x, y, sx) == 'A' ||
-       shape(x + 31, y, sx) == 'A') ||
-      (shape(x, y, sx) == 'B' ||
-       shape(x + 31, y, sx) == 'B') ||
-      (shape(x, y, sx) == '!' ||
-       shape(x + 31, y, sx) == '!') ||
-      (shape(x, y, sx) == 'a' ||
-       shape(x + 31, y, sx) == 'a'))
+  if (isbrick(x, y) ||
+      isice(x, y) ||
+      (shape(x, y) == '[') ||
+      (shape(x, y) == '=') ||
+      (shape(x, y) == ']') ||
+      (shape(x, y) == 'A') ||
+      (shape(x, y) == 'B') ||
+      (shape(x, y) == '!') ||
+      (shape(x, y) == 'a'))
     {
-      v = 1;
+      return YES;
     }
 
-  return(v);
+  return NO;
 }
 
+/*
+int issolid(float x, float y)
+{
+  if (isbrick(x, y) ||
+      isbrick(x + 31, y) ||
+      isice(x, y) ||
+      isice(x + 31, y) ||
+      (shape(x, y) == '[' ||
+       shape(x + 31, y) == '[') ||
+      (shape(x, y) == '=' ||
+       shape(x + 31, y) == '=') ||
+      (shape(x, y) == ']' ||
+       shape(x + 31, y) == ']') ||
+      (shape(x, y) == 'A' ||
+       shape(x + 31, y) == 'A') ||
+      (shape(x, y) == 'B' ||
+       shape(x + 31, y) == 'B') ||
+      (shape(x, y) == '!' ||
+       shape(x + 31, y) == '!') ||
+      (shape(x, y) == 'a' ||
+       shape(x + 31, y) == 'a'))
+    {
+      return YES;
+    }
+
+  return NO;
+}*/
+
 
 /* Is it a brick? */
 
-int isbrick(int x, int y, int sx)
+int isbrick(float x, float y)
 {
-  int v;
-
-  v = 0;
-
-  if (shape(x, y, sx) == 'X' ||
-      shape(x, y, sx) == 'x' ||
-      shape(x, y, sx) == 'Y' ||
-      shape(x, y, sx) == 'y')
+  if (shape(x, y) == 'X' ||
+      shape(x, y) == 'x' ||
+      shape(x, y) == 'Y' ||
+      shape(x, y) == 'y')
     {
-      v = 1;
+      return YES;
     }
 
-  return(v);
+  return NO;
 }
 
 
 /* Is it ice? */
 
-int isice(int x, int y, int sx)
+int isice(float x, float y)
 {
-  int v;
-
-  v = 0;
-
-  if (shape(x, y, sx) == '#')
+  if (shape(x, y) == '#')
     {
-      v = 1;
+      return YES;
     }
 
-  return(v);
+  return NO;
 }
 
 
 /* Is it a full box? */
 
-int isfullbox(int x, int y, int sx)
+int isfullbox(float x, float y)
 {
-  int v;
-
-  v = 0;
-
-  if (shape(x, y, sx) == 'A' ||
-      shape(x, y, sx) == 'B' ||
-      shape(x, y, sx) == '!')
+  if (shape(x, y) == 'A' ||
+      shape(x, y) == 'B' ||
+      shape(x, y) == '!')
     {
-      v = 1;
+      return YES;
     }
 
-  return(v);
-}
-
-
-/* Edit a piece of the map! */
-
-void change(int x, int y, int sx, unsigned char c)
-{
-  int xx, yy;
-
-  yy = (y / 32);
-  xx = ((x + sx) / 32);
-
-  if (yy >= 0 && yy <= 15 && xx >= 0 && xx <= level_width)
-    tiles[yy][xx] = c;
+  return NO;
 }
 
-
 /* Break a brick: */
 
-void trybreakbrick(int x, int y, int sx)
+void trybreakbrick(float x, float y)
 {
-  if (isbrick(x, y, sx))
+  if (isbrick(x, y))
     {
-      if (shape(x, y, sx) == 'x' || shape(x, y, sx) == 'y')
+      if (shape(x, y) == 'x' || shape(x, y) == 'y')
         {
           /* Get a distro from it: */
 
-          add_bouncy_distro(((x + sx + 1) / 32) * 32,
-                            (y / 32) * 32);
+          add_bouncy_distro(((x + 1) / 32) * 32,
+                            (int)(y / 32) * 32);
 
           if (counting_distros == NO)
             {
@@ -3240,9 +1447,9 @@ void trybreakbrick(int x, int y, int sx)
             }
 
           if (distro_counter <= 0)
-            change(x, y, sx, 'a');
+            level_change(&current_level,x, y, 'a');
 
-          play_sound(sounds[SND_DISTRO]);
+          play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
           score = score + SCORE_DISTRO;
           distros++;
         }
@@ -3250,19 +1457,19 @@ void trybreakbrick(int x, int y, int sx)
         {
           /* Get rid of it: */
 
-          change(x, y, sx, '.');
+          level_change(&current_level,x, y,'.');
         }
 
 
       /* Replace it with broken bits: */
 
-      add_broken_brick(((x + sx + 1) / 32) * 32,
-                       (y / 32) * 32);
+      add_broken_brick(((x + 1) / 32) * 32,
+                       (int)(y / 32) * 32);
 
 
       /* Get some score: */
 
-      play_sound(sounds[SND_BRICK]);
+      play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
       score = score + SCORE_BRICK;
     }
 }
@@ -3270,84 +1477,87 @@ void trybreakbrick(int x, int y, int sx)
 
 /* Bounce a brick: */
 
-void bumpbrick(int x, int y, int sx)
+void bumpbrick(float x, float y)
 {
-  add_bouncy_brick(((x + sx + 1) / 32) * 32,
-                   (y / 32) * 32);
+  add_bouncy_brick(((int)(x + 1) / 32) * 32,
+                   (int)(y / 32) * 32);
 
-  play_sound(sounds[SND_BRICK]);
+  play_sound(sounds[SND_BRICK], SOUND_CENTER_SPEAKER);
 }
 
 
 /* Empty a box: */
 
-void tryemptybox(int x, int y, int sx)
+void tryemptybox(float x, float y)
 {
-  if (isfullbox(x, y, sx))
+  if (isfullbox(x, y))
     {
-      if (shape(x, y, sx) == 'A')
+      if (shape(x, y) == 'A')
         {
+       
+       DEBUG_MSG("Here I am");
+       
           /* Box with a distro! */
 
-          add_bouncy_distro(((x + sx + 1) / 32) * 32,
-                            (y / 32) * 32 - 32);
+          add_bouncy_distro(((x + 1) / 32) * 32,
+                            (int)(y / 32) * 32 - 32);
 
-          play_sound(sounds[SND_DISTRO]);
+          play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
           score = score + SCORE_DISTRO;
           distros++;
         }
-      else if (shape(x, y, sx) == 'B')
+      else if (shape(x, y) == 'B')
         {
           /* Add an upgrade! */
 
-          if (tux_size == SMALL)
+          if (tux.size == SMALL)
             {
               /* Tux is small, add mints! */
 
-              add_upgrade(((x + sx + 1) / 32) * 32,
-                          (y / 32) * 32 - 32,
+              add_upgrade(((x + 1) / 32) * 32,
+                          (int)(y / 32) * 32 - 32,
                           UPGRADE_MINTS);
             }
           else
             {
               /* Tux is big, add coffee: */
 
-              add_upgrade(((x + sx + 1) / 32) * 32,
-                          (y / 32) * 32 - 32,
+              add_upgrade(((x + 1) / 32) * 32,
+                          (int)(y / 32) * 32 - 32,
                           UPGRADE_COFFEE);
             }
 
-          play_sound(sounds[SND_UPGRADE]);
+          play_sound(sounds[SND_UPGRADE], SOUND_CENTER_SPEAKER);
         }
-      else if (shape(x, y, sx) == '!')
+      else if (shape(x, y) == '!')
         {
           /* Add a golden herring */
 
-          add_upgrade(((x + sx + 1) / 32) * 32,
-                      (y / 32) * 32 - 32,
+          add_upgrade(((x + 1) / 32) * 32,
+                      (int)(y / 32) * 32 - 32,
                       UPGRADE_HERRING);
         }
 
       /* Empty the box: */
 
-      change(x, y, sx, 'a');
+      level_change(&current_level,x, y, 'a');
     }
 }
 
 
 /* Try to grab a distro: */
 
-void trygrabdistro(int x, int y, int sx, int bounciness)
+void trygrabdistro(float x, float y, int bounciness)
 {
-  if (shape(x, y, sx) == '$')
+  if (shape(x, y) == '$')
     {
-      change(x, y, sx, '.');
-      play_sound(sounds[SND_DISTRO]);
+      level_change(&current_level,x, y, '.');
+      play_sound(sounds[SND_DISTRO], SOUND_CENTER_SPEAKER);
 
       if (bounciness == BOUNCE)
         {
-          add_bouncy_distro(((x + sx + 1) / 32) * 32,
-                            (y / 32) * 32);
+          add_bouncy_distro(((x + 1) / 32) * 32,
+                            (int)(y / 32) * 32);
         }
 
       score = score + SCORE_DISTRO;
@@ -3355,180 +1565,27 @@ void trygrabdistro(int x, int y, int sx, int bounciness)
     }
 }
 
-
-/* Add a bouncy distro: */
-
-void add_bouncy_distro(int x, int y)
-{
-  int i, found;
-
-  found = -1;
-
-  for (i = 0; i < NUM_BOUNCY_DISTROS && found == -1; i++)
-    {
-      if (!bouncy_distros[i].alive)
-        found = i;
-    }
-
-  if (found != -1)
-    {
-      bouncy_distros[found].alive = YES;
-      bouncy_distros[found].x = x;
-      bouncy_distros[found].y = y;
-      bouncy_distros[found].ym = -6;
-    }
-}
-
-
-/* Add broken brick pieces: */
-
-void add_broken_brick(int x, int y)
-{
-  add_broken_brick_piece(x, y, -4, -16);
-  add_broken_brick_piece(x, y + 16, -6, -12);
-
-  add_broken_brick_piece(x + 16, y, 4, -16);
-  add_broken_brick_piece(x + 16, y + 16, 6, -12);
-}
-
-
-/* Add a broken brick piece: */
-
-void add_broken_brick_piece(int x, int y, int xm, int ym)
-{
-  int i, found;
-
-  found = -1;
-
-  for (i = 0; i < NUM_BROKEN_BRICKS && found == -1; i++)
-    {
-      if (!broken_bricks[i].alive)
-        found = i;
-    }
-
-  if (found != -1)
-    {
-      broken_bricks[found].alive = YES;
-      broken_bricks[found].x = x;
-      broken_bricks[found].y = y;
-      broken_bricks[found].xm = xm;
-      broken_bricks[found].ym = ym;
-    }
-}
-
-
-/* Add a bouncy brick piece: */
-
-void add_bouncy_brick(int x, int y)
-{
-  int i, found;
-
-  found = -1;
-
-  for (i = 0; i < NUM_BOUNCY_BRICKS && found == -1; i++)
-    {
-      if (!bouncy_bricks[i].alive)
-        found = i;
-    }
-
-  if (found != -1)
-    {
-      bouncy_bricks[found].alive = YES;
-      bouncy_bricks[found].x = x;
-      bouncy_bricks[found].y = y;
-      bouncy_bricks[found].offset = 0;
-      bouncy_bricks[found].offset_m = -BOUNCY_BRICK_SPEED;
-      bouncy_bricks[found].shape = shape(x, y, 0);
-    }
-}
-
-
-/* Add a bad guy: */
-
-void add_bad_guy(int x, int y, int kind)
-{
-  int i, found;
-
-  found = -1;
-
-  for (i = 0; i < NUM_BAD_GUYS && found == -1; i++)
-    {
-      if (!bad_guys[i].alive)
-        found = i;
-    }
-
-  if (found != -1)
-    {
-      bad_guys[found].alive = YES;
-      bad_guys[found].mode = NORMAL;
-      bad_guys[found].dying = NO;
-      bad_guys[found].timer = 0;
-      bad_guys[found].kind = kind;
-      bad_guys[found].x = x;
-      bad_guys[found].y = y;
-      bad_guys[found].xm = 0;
-      bad_guys[found].ym = 0;
-      bad_guys[found].dir = LEFT;
-      bad_guys[found].seen = NO;
-    }
-}
-
-
-/* Add score: */
-
-void add_score(int x, int y, int s)
-{
-  int i, found;
-
-
-  /* Add the score: */
-
-  score = score + s;
-
-
-  /* Add a floating score thing to the game: */
-
-  found = -1;
-
-  for (i = 0; i < NUM_FLOATING_SCORES && found == -1; i++)
-    {
-      if (!floating_scores[i].alive)
-        found = i;
-    }
-
-
-  if (found != -1)
-    {
-      floating_scores[found].alive = YES;
-      floating_scores[found].x = x;
-      floating_scores[found].y = y - 16;
-      floating_scores[found].timer = 8;
-      floating_scores[found].value = s;
-    }
-}
-
-
 /* Try to bump a bad guy from below: */
 
-void trybumpbadguy(int x, int y, int sx)
+void trybumpbadguy(float x, float y)
 {
   int i;
 
 
   /* Bad guys: */
 
-  for (i = 0; i < NUM_BAD_GUYS; i++)
+  for (i = 0; i < num_bad_guys; i++)
     {
-      if (bad_guys[i].alive &&
-          bad_guys[i].x >= x + sx - 32 && bad_guys[i].x <= x + sx + 32 &&
-          bad_guys[i].y >= y - 16 && bad_guys[i].y <= y + 16)
+      if (bad_guys[i].base.alive &&
+          bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
+          bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
         {
           if (bad_guys[i].kind == BAD_BSOD ||
               bad_guys[i].kind == BAD_LAPTOP)
             {
               bad_guys[i].dying = FALLING;
-              bad_guys[i].ym = -8;
-              play_sound(sounds[SND_FALL]);
+              bad_guys[i].base.ym = -8;
+              play_sound(sounds[SND_FALL], SOUND_CENTER_SPEAKER);
             }
         }
     }
@@ -3536,109 +1593,62 @@ void trybumpbadguy(int x, int y, int sx)
 
   /* Upgrades: */
 
-  for (i = 0; i < NUM_UPGRADES; i++)
+  for (i = 0; i < num_upgrades; i++)
     {
-      if (upgrades[i].alive && upgrades[i].height == 32 &&
-          upgrades[i].x >= x + sx - 32 && upgrades[i].x <= x + sx + 32 &&
-          upgrades[i].y >= y - 16 && upgrades[i].y <= y + 16)
+      if (upgrades[i].base.alive && upgrades[i].base.height == 32 &&
+          upgrades[i].base.x >= x - 32 && upgrades[i].base.x <= x + 32 &&
+          upgrades[i].base.y >= y - 16 && upgrades[i].base.y <= y + 16)
         {
-          upgrades[i].xm = -upgrades[i].xm;
-          upgrades[i].ym = -8;
-          play_sound(sounds[SND_BUMP_UPGRADE]);
+          upgrades[i].base.xm = -upgrades[i].base.xm;
+          upgrades[i].base.ym = -8;
+          play_sound(sounds[SND_BUMP_UPGRADE], SOUND_CENTER_SPEAKER);
         }
     }
 }
 
-
-/* Add an upgrade: */
-
-void add_upgrade(int x, int y, int kind)
+/* (Status): */
+void drawstatus(void)
 {
-  int i, found;
+  int i;
 
-  found = -1;
+  sprintf(str, "%d", score);
+  text_draw(&white_text, "SCORE", 0, 0, 1, NO_UPDATE);
+  text_draw(&gold_text, str, 96, 0, 1, NO_UPDATE);
 
-  for (i = 0; i < NUM_UPGRADES && found == -1; i++)
+  if(st_gl_mode == ST_GL_PLAY)
     {
-      if (!upgrades[i].alive)
-        found = i;
+      sprintf(str, "%d", hs_score);
+      text_draw(&white_text, "HIGH", 0, 20, 1, NO_UPDATE);
+      text_draw(&gold_text, str, 96, 20, 1, NO_UPDATE);
     }
-
-  if (found != -1)
+  else if(st_gl_mode == ST_GL_TEST)
     {
-      upgrades[found].alive = YES;
-      upgrades[found].kind = kind;
-      upgrades[found].x = x;
-      upgrades[found].y = y;
-      upgrades[found].xm = 4;
-      upgrades[found].ym = -4;
-      upgrades[found].height = 0;
+      text_draw(&white_text,"Press ESC To Return",0,20,1, NO_UPDATE);
     }
-}
-
-
-/* Kill tux! */
-
-void killtux(int mode)
-{
-  tux_ym = -16;
-
-  play_sound(sounds[SND_HURT]);
-
-  if (tux_dir == RIGHT)
-    tux_xm = -8;
-  else if (tux_dir == LEFT)
-    tux_xm = 8;
-
-  if (mode == SHRINK && tux_size == BIG)
-    {
-      if (tux_got_coffee)
-        tux_got_coffee = NO;
-
-      tux_size = SMALL;
 
-      tux_safe = TUX_SAFE_TIME;
-    }
-  else
+  if (timer_get_left(&time_left) > TIME_WARNING || (frame % 10) < 5)
     {
-      tux_dying = 1;
+      sprintf(str, "%d", timer_get_left(&time_left) / 1000 );
+      text_draw(&white_text, "TIME", 224, 0, 1, NO_UPDATE);
+      text_draw(&gold_text, str, 304, 0, 1, NO_UPDATE);
     }
-}
-
 
-/* Add a bullet: */
-
-void add_bullet(int x, int y, int dir, int xm)
-{
-  int i, found;
+  sprintf(str, "%d", distros);
+  text_draw(&white_text, "DISTROS", screen->h, 0, 1, NO_UPDATE);
+  text_draw(&gold_text, str, 608, 0, 1, NO_UPDATE);
 
-  found = -1;
+  text_draw(&white_text, "LIVES", screen->h, 20, 1, NO_UPDATE);
 
-  for (i = 0; i < NUM_BULLETS && found == -1; i++)
+  if(show_fps)
     {
-      if (!bullets[i].alive)
-        found = i;
+      sprintf(str, "%2.1f", fps_fps);
+      text_draw(&white_text, "FPS", screen->h, 40, 1, NO_UPDATE);
+      text_draw(&gold_text, str, screen->h + 60, 40, 1, NO_UPDATE);
     }
 
-  if (found != -1)
+  for(i=0; i < tux.lives; ++i)
     {
-      bullets[found].alive = YES;
-
-      if (dir == RIGHT)
-        {
-          bullets[found].x = x + 32;
-          bullets[found].xm = BULLET_XM + xm;
-        }
-      else
-        {
-          bullets[found].x = x;
-          bullets[found].xm = -BULLET_XM + xm;
-        }
-
-      bullets[found].y = y;
-      bullets[found].ym = BULLET_STARTING_YM;
-
-      play_sound(sounds[SND_SHOOT]);
+      texture_draw(&tux_life,565+(18*i),20,NO_UPDATE);
     }
 }
 
@@ -3649,15 +1659,15 @@ void drawendscreen(void)
 
   clearscreen(0, 0, 0);
 
-  drawcenteredtext("GAMEOVER", 200, letters_red, NO_UPDATE);
+  text_drawf(&blue_text, "GAMEOVER", 0, 200, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
 
   sprintf(str, "SCORE: %d", score);
-  drawcenteredtext(str, 224, letters_gold, NO_UPDATE);
+  text_drawf(&gold_text, str, 0, 224, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
 
   sprintf(str, "DISTROS: %d", distros);
-  drawcenteredtext(str, 256, letters_blue, NO_UPDATE);
+  text_drawf(&gold_text, str, 0, 256, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
 
-  SDL_Flip(screen);
+  flipscreen();
   SDL_Delay(2000);
 }
 
@@ -3667,18 +1677,95 @@ void drawresultscreen(void)
 
   clearscreen(0, 0, 0);
 
-  drawcenteredtext("Result:", 200, letters_red, NO_UPDATE);
+  text_drawf(&blue_text, "Result:", 0, 200, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
 
   sprintf(str, "SCORE: %d", score);
-  drawcenteredtext(str, 224, letters_gold, NO_UPDATE);
+  text_drawf(&gold_text, str, 0, 224, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
 
   sprintf(str, "DISTROS: %d", distros);
-  drawcenteredtext(str, 256, letters_blue, NO_UPDATE);
+  text_drawf(&gold_text, str, 0, 256, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
 
-  SDL_Flip(screen);
-  /*SDL_Delay(2000);*/
-  sleep(2);
+  flipscreen();
+  SDL_Delay(2000);
 }
 
 void savegame(void)
-{}
+{
+  char savefile[300];
+  time_t current_time = time(NULL);
+  struct tm* time_struct;
+  FILE* fi;
+
+  time_struct = localtime(&current_time);
+  sprintf(savefile,"%s/%d-%d-%d-%d.save",st_save_dir,time_struct->tm_year+1900,time_struct->tm_mon,time_struct->tm_mday,time_struct->tm_hour);
+  printf("%s",savefile);
+
+
+  fi = fopen(savefile, "wb");
+
+  if (fi == NULL)
+    {
+      fprintf(stderr, "Warning: I could not open the high score file ");
+
+    }
+  else
+    {
+      fwrite(&level,4,1,fi);
+      fwrite(&score,4,1,fi);
+      fwrite(&distros,4,1,fi);
+      fwrite(&tux.base.x,4,1,fi);
+      fwrite(&tux.base.y,4,1,fi);
+      fwrite(&scroll_x,4,1,fi);
+      fwrite(&current_level.time_left,4,1,fi);
+    }
+  fclose(fi);
+
+}
+
+void loadgame(char* filename)
+{
+  char savefile[300];
+  FILE* fi;
+  time_t current_time = time(NULL);
+  struct tm* time_struct;
+
+  time_struct = localtime(&current_time);
+  sprintf(savefile,"%s/%d-%d-%d-%d.save",st_save_dir,time_struct->tm_year+1900,time_struct->tm_mon,time_struct->tm_mday,time_struct->tm_hour);
+  printf("%s",savefile);
+
+
+  fi = fopen(savefile, "rb");
+
+  if (fi == NULL)
+    {
+      fprintf(stderr, "Warning: I could not open the high score file ");
+
+    }
+  else
+    {
+      player_level_begin(&tux);
+      set_defaults();
+      level_free(&current_level);
+      if(level_load(&current_level,level_subset,level) != 0)
+        exit(1);
+      arrays_free();
+      arrays_init();
+      activate_bad_guys();
+      level_free_gfx();
+      level_load_gfx(&current_level);
+      level_free_song();
+      level_load_song(&current_level);
+      levelintro();
+      start_timers();
+
+      fread(&level,4,1,fi);
+      fread(&score,4,1,fi);
+      fread(&distros,4,1,fi);
+      fread(&tux.base.x,4,1,fi);
+      fread(&tux.base.y,4,1,fi);
+      fread(&scroll_x,4,1,fi);
+      fread(&current_level.time_left,4,1,fi);
+      fclose(fi);
+    }
+
+}