Merge branch 'gles'
[neverball] / ball / main.c
index 832dd1c..98a33d9 100644 (file)
 
 /*---------------------------------------------------------------------------*/
 
-#ifdef WIN32
-#pragma comment(lib, "SDL_ttf.lib")
-#pragma comment(lib, "SDL_mixer.lib")
-#pragma comment(lib, "SDL_image.lib")
-#pragma comment(lib, "SDL.lib")
-#pragma comment(lib, "SDLmain.lib")
-#pragma comment(lib, "opengl32.lib")
-#endif
-
-/*---------------------------------------------------------------------------*/
-
 #include <SDL.h>
-#include <SDL_image.h>
 #include <stdio.h>
 #include <string.h>
-#include <errno.h>
 
 #include "glext.h"
 #include "config.h"
+#include "video.h"
 #include "image.h"
 #include "audio.h"
 #include "demo.h"
-#include "levels.h"
-#include "game.h"
+#include "progress.h"
 #include "gui.h"
 #include "set.h"
+#include "tilt.h"
+#include "fs.h"
+#include "common.h"
 
 #include "st_conf.h"
 #include "st_title.h"
 #include "st_demo.h"
 #include "st_level.h"
+#include "st_pause.h"
 
-#define TITLE "Neverball"
+const char TITLE[] = "Neverball " VERSION;
+const char ICON[] = "icon/neverball.png";
 
 /*---------------------------------------------------------------------------*/
 
-static void shot(void)
+static int shot_pending;
+
+static void shot_prep(void)
 {
-    static char filename[MAXSTR];
-    static int  num = 0;
+    shot_pending = 1;
+}
 
-    sprintf(filename, _("screen%02d.png"), num++);
+static void shot_take(void)
+{
+    static char filename[MAXSTR];
 
-    image_snap(filename, config_get_d(CONFIG_WIDTH),
-               config_get_d(CONFIG_HEIGHT));
+    if (shot_pending)
+    {
+        sprintf(filename, "Screenshots/screen%05d.png", config_screenshot());
+        image_snap(filename);
+        shot_pending = 0;
+    }
 }
 
 /*---------------------------------------------------------------------------*/
 
 static void toggle_wire(void)
 {
+#if !ENABLE_OPENGLES
     static int wire = 0;
 
     if (wire)
@@ -81,274 +82,381 @@ static void toggle_wire(void)
         glDisable(GL_LIGHTING);
         wire = 1;
     }
+#endif
 }
 
-static void toggle_fullscreen(void)
+/*---------------------------------------------------------------------------*/
+
+static int handle_key_dn(SDL_Event *e)
 {
-    int x, y;
+    int d = 1;
+    int c;
+
+    c = e->key.keysym.sym;
+
+    if (config_tst_d(CONFIG_KEY_FORWARD, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -1.0f);
+
+    else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +1.0f);
+
+    else if (config_tst_d(CONFIG_KEY_LEFT, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -1.0f);
+
+    else if (config_tst_d(CONFIG_KEY_RIGHT, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +1.0f);
 
-    SDL_GetMouseState(&x, &y);
-    config_mode(!config_get_d(CONFIG_FULLSCREEN), config_get_d(CONFIG_WIDTH),
-                config_get_d(CONFIG_HEIGHT));
-    SDL_WarpMouse(x, y);
+    else switch (c)
+    {
+    case SDLK_F10:   shot_prep();               break;
+    case SDLK_F9:    config_tgl_d(CONFIG_FPS);  break;
+    case SDLK_F8:    config_tgl_d(CONFIG_NICE); break;
+
+    case SDLK_F7:
+        if (config_cheat())
+            toggle_wire();
+        break;
+    case SDLK_RETURN:
+        d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
+        break;
+    case SDLK_ESCAPE:
+        d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
+        break;
+
+    default:
+        if (SDL_EnableUNICODE(-1))
+            d = st_keybd(e->key.keysym.unicode, 1);
+        else
+            d = st_keybd(e->key.keysym.sym, 1);
+    }
+
+    return d;
 }
 
-/*---------------------------------------------------------------------------*/
+static int handle_key_up(SDL_Event *e)
+{
+    int d = 1;
+    int c;
+
+    c = e->key.keysym.sym;
+
+    if (config_tst_d(CONFIG_KEY_FORWARD, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
+
+    else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 0);
+
+    else if (config_tst_d(CONFIG_KEY_LEFT, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
+
+    else if (config_tst_d(CONFIG_KEY_RIGHT, c))
+        st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 0);
+
+    else switch (c)
+    {
+    case SDLK_RETURN:
+        d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
+        break;
+    case SDLK_ESCAPE:
+        d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
+        break;
+
+    default:
+        d = st_keybd(e->key.keysym.sym, 0);
+    }
+
+    return d;
+}
 
 static int loop(void)
 {
     SDL_Event e;
     int d = 1;
 
+    /* Process SDL events. */
+
     while (d && SDL_PollEvent(&e))
     {
-        if (e.type == SDL_QUIT)
+        switch (e.type)
+        {
+        case SDL_QUIT:
             return 0;
 
-        if (e.type == SDL_KEYDOWN)
-            switch (e.key.keysym.sym)
-            {
-            case SDLK_SPACE: config_tgl_pause();        break;
-            case SDLK_F11:   toggle_fullscreen();       break;
-            case SDLK_F10:   shot();                    break;
-            case SDLK_F9:    config_tgl_d(CONFIG_FPS);  break;
-            case SDLK_F8:    config_tgl_d(CONFIG_NICE); break;
-            case SDLK_F7:    toggle_wire();             break;
-            default: break;
-            }
-
-        if (!config_get_pause())
-            switch (e.type)
-            {
-            case SDL_MOUSEMOTION:
-                st_point(+e.motion.x,
-                         -e.motion.y + config_get_d(CONFIG_HEIGHT),
-                         +e.motion.xrel,
-                         config_get_d(CONFIG_MOUSE_INVERT)
-                         ? +e.motion.yrel : -e.motion.yrel);
-                break;
-
-            case SDL_MOUSEBUTTONDOWN:
-                d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
-                break;
-
-            case SDL_MOUSEBUTTONUP:
-                d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
-                break;
-
-            case SDL_KEYDOWN:
+        case SDL_MOUSEMOTION:
+            st_point(+e.motion.x,
+                     -e.motion.y + config_get_d(CONFIG_HEIGHT),
+                     +e.motion.xrel,
+                     config_get_d(CONFIG_MOUSE_INVERT)
+                     ? +e.motion.yrel : -e.motion.yrel);
+            break;
+
+        case SDL_MOUSEBUTTONDOWN:
+            d = st_click(e.button.button, 1);
+            break;
+
+        case SDL_MOUSEBUTTONUP:
+            d = st_click(e.button.button, 0);
+            break;
+
+        case SDL_KEYDOWN:
+            d = handle_key_dn(&e);
+            break;
+
+        case SDL_KEYUP:
+            d = handle_key_up(&e);
+            break;
+
+        case SDL_ACTIVEEVENT:
+            if (e.active.state == SDL_APPINPUTFOCUS)
+                if (e.active.gain == 0 && video_get_grab())
+                    goto_state(&st_pause);
+            break;
+
+        case SDL_JOYAXISMOTION:
+            st_stick(e.jaxis.axis, JOY_VALUE(e.jaxis.value));
+            break;
+
+        case SDL_JOYBUTTONDOWN:
+            d = st_buttn(e.jbutton.button, 1);
+            break;
+
+        case SDL_JOYBUTTONUP:
+            d = st_buttn(e.jbutton.button, 0);
+            break;
+        }
+    }
 
-                switch (e.key.keysym.sym)
-                {
+    /* Process events via the tilt sensor API. */
 
-                case SDLK_RETURN:
-                    d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
-                    break;
-                case SDLK_ESCAPE:
-                    d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
-                    break;
-                case SDLK_LEFT:
-                    st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
-                    break;
-                case SDLK_RIGHT:
-                    st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
-                    break;
-                case SDLK_UP:
-                    st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
-                    break;
-                case SDLK_DOWN:
-                    st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
-                    break;
-
-                default:
-                    if (SDL_EnableUNICODE(-1))
-                        d = st_keybd(e.key.keysym.unicode, 1);
-                    else
-                        d = st_keybd(e.key.keysym.sym, 1);
-                }
-                break;
+    if (tilt_stat())
+    {
+        int b;
+        int s;
 
-            case SDL_KEYUP:
+        st_angle((int) tilt_get_x(),
+                 (int) tilt_get_z());
 
-                switch (e.key.keysym.sym)
-                {
-                case SDLK_RETURN:
-                    d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 0);
-                    break;
-                case SDLK_ESCAPE:
-                    d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 0);
-                    break;
-                case SDLK_LEFT:
-                case SDLK_RIGHT:
-                    st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
-                    break;
-                case SDLK_DOWN:
-                case SDLK_UP:
-                    st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
-                    break;
-
-                default:
-                    d = st_keybd(e.key.keysym.sym, 0);
-                }
+        while (tilt_get_button(&b, &s))
+        {
+            const int X = config_get_d(CONFIG_JOYSTICK_AXIS_X);
+            const int Y = config_get_d(CONFIG_JOYSTICK_AXIS_Y);
+            const int L = config_get_d(CONFIG_JOYSTICK_DPAD_L);
+            const int R = config_get_d(CONFIG_JOYSTICK_DPAD_R);
+            const int U = config_get_d(CONFIG_JOYSTICK_DPAD_U);
+            const int D = config_get_d(CONFIG_JOYSTICK_DPAD_D);
+
+            if (b == L || b == R || b == U || b == D)
+            {
+                static int pad[4] = { 0, 0, 0, 0 };
 
-                break;
+                /* Track the state of the D-pad buttons. */
 
-            case SDL_ACTIVEEVENT:
-                if (e.active.state == SDL_APPINPUTFOCUS)
-                    if (e.active.gain == 0 && config_get_grab())
-                        config_set_pause();
-                break;
+                if      (b == L) pad[0] = s;
+                else if (b == R) pad[1] = s;
+                else if (b == U) pad[2] = s;
+                else if (b == D) pad[3] = s;
 
-            case SDL_JOYAXISMOTION:
-                st_stick(e.jaxis.axis, e.jaxis.value);
-                break;
+                /* Convert D-pad button events into joystick axis motion. */
 
-            case SDL_JOYBUTTONDOWN:
-                d = st_buttn(e.jbutton.button, 1);
-                break;
+                if      (pad[0] && !pad[1]) st_stick(X, -1.0f);
+                else if (pad[1] && !pad[0]) st_stick(X, +1.0f);
+                else                        st_stick(X,  0.0f);
 
-            case SDL_JOYBUTTONUP:
-                d = st_buttn(e.jbutton.button, 0);
-                break;
+                if      (pad[2] && !pad[3]) st_stick(Y, -1.0f);
+                else if (pad[3] && !pad[2]) st_stick(Y, +1.0f);
+                else                        st_stick(Y,  0.0f);
             }
+            else d = st_buttn(b, s);
+        }
     }
+
     return d;
 }
 
 /*---------------------------------------------------------------------------*/
 
-/* Option values */
-static char *data_path    = NULL;
-static char *replay_path  = NULL;
-static char *level_path   = NULL;
-static int   display_info = 0;
-
-/* Option handling */
-
-#define USAGE  _( \
-        "Usage: %s [options ...]\n" \
-        "-r, --replay file         play the replay 'file'.\n" \
-        "-l, --level file.sol      play the level 'file.sol'.\n" \
-        "-i, --info                display info about level or replay.\n" \
-        "    --data dir            use 'dir' as game data directory.\n" \
-        "-v, --version             show version.\n" \
-        "-h, -?, --help            show this usage message.\n")
-
-static void parse_args(int argc, char **argv)
+static char *opt_data;
+static char *opt_replay;
+static char *opt_level;
+
+#define opt_usage \
+    L_(                                                                   \
+        "Usage: %s [options ...]\n"                                       \
+        "Options:\n"                                                      \
+        "  -h, --help                show this usage message.\n"          \
+        "  -v, --version             show version.\n"                     \
+        "  -d, --data <dir>          use 'dir' as game data directory.\n" \
+        "  -r, --replay <file>       play the replay 'file'.\n"           \
+        "  -l, --level <file>        load the level 'file'\n"             \
+    )
+
+#define opt_error(option) \
+    fprintf(stderr, L_("Option '%s' requires an argument.\n"), option)
+
+static void opt_parse(int argc, char **argv)
 {
-#define CASE(x) (strcmp(*argv, (x)) == 0)       /* Check current option */
-#define MAND    !(missing = (argv[1] == NULL))  /* Argument is mandatory */
-    char *exec = *(argv++);
-    int missing; /* Argument is missing. */
+    int i;
+
+    /* Scan argument list. */
 
-    while (*argv != NULL)
+    for (i = 1; i < argc; i++)
     {
-        missing = 0;
-        if (CASE("-h") || CASE("-?") || CASE("--help"))
+        if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help")    == 0)
         {
-            printf(USAGE, exec);
-            exit(0);
+            printf(opt_usage, argv[0]);
+            exit(EXIT_SUCCESS);
         }
-        else if (CASE("-v") || CASE("--version"))
+
+        if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
         {
-            printf(_("%s: %s version %s\n"), exec, TITLE, VERSION);
-            exit(0);
+            printf("%s\n", VERSION);
+            exit(EXIT_SUCCESS);
         }
-        else if (CASE("--data") && MAND)
-            data_path = *(++argv);
-        else if ((CASE("-r") || CASE("--replay")) && MAND)
-            replay_path = *(++argv);
-        else if ((CASE("-l") || CASE("--level")) && MAND)
-            level_path = *(++argv);
-        else if ((CASE("-i") || CASE("--info")))
-            display_info = 1;
-        else if (!missing)
+
+        if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--data")    == 0)
         {
-            fprintf(stderr, _("%s: unknown option %s\n"), exec, *argv);
-            fprintf(stderr, USAGE, exec);
-            exit(1);
+            if (i + 1 == argc)
+            {
+                opt_error(argv[i]);
+                exit(EXIT_FAILURE);
+            }
+            opt_data = argv[++i];
+            continue;
         }
-        else
+
+        if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--replay")  == 0)
         {
-            fprintf(stderr, _("%s: option %s requires an argument\n"), exec,
-                    *argv);
-            fprintf(stderr, USAGE, exec);
-            exit(1);
+            if (i + 1 == argc)
+            {
+                opt_error(argv[i]);
+                exit(EXIT_FAILURE);
+            }
+            opt_replay = argv[++i];
+            continue;
         }
-        argv++;
-    }
-    return;
-}
 
-int main(int argc, char *argv[])
-{
-    SDL_Joystick *joy = NULL;
-    int t1, t0;               /* ticks */
-    SDL_Surface *icon;        /* WM icon */
+        if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--level")  == 0)
+        {
+            if (i + 1 == argc)
+            {
+                opt_error(argv[i]);
+                exit(EXIT_FAILURE);
+            }
+            opt_level = argv[++i];
+            continue;
+        }
 
-    language_init("neverball", CONFIG_LOCALE);
+        /* Perform magic on a single unrecognized argument. */
 
-    parse_args(argc, argv);
+        if (argc == 2)
+        {
+            size_t len = strlen(argv[i]);
+            int level = 0;
 
-    if (!config_data_path(data_path, SET_FILE))
-    {
-        fprintf(stderr, _("Failure to establish game data directory\n"));
-        return 1;
-    }
+            if (len > 4)
+            {
+                char *ext = argv[i] + len - 4;
 
-    if (!config_user_path(NULL))
-    {
-        fprintf(stderr, _("Failure to establish config directory\n"));
-        return 1;
+                if (strcmp(ext, ".map") == 0)
+                    strncpy(ext, ".sol", 4);
+
+                if (strcmp(ext, ".sol") == 0)
+                    level = 1;
+            }
+
+            if (level)
+                opt_level = argv[i];
+            else
+                opt_replay = argv[i];
+
+            break;
+        }
     }
+}
 
-    /* Intitialize the configuration */
+#undef opt_usage
+#undef opt_error
 
-    config_init();
-    config_load();
+/*---------------------------------------------------------------------------*/
+
+static int is_replay(struct dir_item *item)
+{
+    return str_ends_with(item->path, ".nbr");
+}
 
-    /* Initialize the language */
+static int is_score_file(struct dir_item *item)
+{
+    return str_starts_with(item->path, "neverballhs-");
+}
 
-    language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
+static void make_dirs_and_migrate(void)
+{
+    Array items;
+    int i;
 
-    /* Prepare run without sdl */
+    const char *src;
+    char *dst;
 
-    if (replay_path != NULL)
+    if (fs_mkdir("Replays"))
     {
-        if (!level_replay(replay_path))
+        if ((items = fs_dir_scan("", is_replay)))
         {
-            fprintf(stderr, _("Replay file '%s': "), replay_path);
-            if (errno)
-                perror(NULL);
-            else
-                fprintf(stderr, _("Not a replay file\n"));
-            return 1;
+            for (i = 0; i < array_len(items); i++)
+            {
+                src = DIR_ITEM_GET(items, i)->path;
+                dst = concat_string("Replays/", src, NULL);
+                fs_rename(src, dst);
+                free(dst);
+            }
+
+            fs_dir_free(items);
         }
-        else if (display_info)
-            demo_replay_dump_info();
     }
 
-    if (level_path != NULL)
+    if (fs_mkdir("Scores"))
     {
-        struct level l;
-        if (!level_load(level_path, &l))
-            return 1;
-        else if (display_info)
-            level_dump_info(&l);
+        if ((items = fs_dir_scan("", is_score_file)))
+        {
+            for (i = 0; i < array_len(items); i++)
+            {
+                src = DIR_ITEM_GET(items, i)->path;
+                dst = concat_string("Scores/",
+                                    src + sizeof ("neverballhs-") - 1,
+                                    ".txt",
+                                    NULL);
+                fs_rename(src, dst);
+                free(dst);
+            }
+
+            fs_dir_free(items);
+        }
     }
 
-    if (display_info)
+    fs_mkdir("Screenshots");
+}
+
+/*---------------------------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    SDL_Joystick *joy = NULL;
+    int t1, t0;
+
+    if (!fs_init(argv[0]))
     {
-        if (replay_path == NULL && level_path == NULL)
-        {
-            fprintf(stderr, _("%s: --info requires --replay or --level\n"),
-                    argv[0]);
-            return 1;
-        }
-        return 0;
+        fprintf(stderr, "Failure to initialize virtual file system: %s\n",
+                fs_error());
+        return 1;
     }
 
-    /* Initialize SDL system and subsystems */
+    lang_init("neverball");
+
+    opt_parse(argc, argv);
+
+    config_paths(opt_data);
+    make_dirs_and_migrate();
+
+    /* Initialize SDL. */
 
     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
     {
@@ -356,72 +464,66 @@ int main(int argc, char *argv[])
         return 1;
     }
 
-    /* Initialize the joystick. */
+    /* Intitialize configuration. */
+
+    config_init();
+    config_load();
+
+    /* Initialize joystick. */
 
-    if (SDL_NumJoysticks() > 0)
+    if (config_get_d(CONFIG_JOYSTICK) && SDL_NumJoysticks() > 0)
     {
         joy = SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
         if (joy)
             SDL_JoystickEventState(SDL_ENABLE);
     }
 
-    /* Initialize the audio. */
-
-    audio_bind(AUD_MENU,   3, "snd/menu.wav");
-    audio_bind(AUD_START,  1, "snd/select.ogg");
-    audio_bind(AUD_READY,  1, "snd/ready.ogg");
-    audio_bind(AUD_SET,    1, "snd/set.ogg");
-    audio_bind(AUD_GO,     1, "snd/go.ogg");
-    audio_bind(AUD_BALL,   2, "snd/ball.ogg");
-    audio_bind(AUD_BUMP,   3, "snd/bump.ogg");
-    audio_bind(AUD_COIN,   2, "snd/coin.wav");
-    audio_bind(AUD_TICK,   4, "snd/tick.ogg");
-    audio_bind(AUD_TOCK,   4, "snd/tock.ogg");
-    audio_bind(AUD_SWITCH, 5, "snd/switch.wav");
-    audio_bind(AUD_JUMP,   5, "snd/jump.ogg");
-    audio_bind(AUD_GOAL,   5, "snd/goal.wav");
-    audio_bind(AUD_SCORE,  1, "snd/record.ogg");
-    audio_bind(AUD_FALL,   1, "snd/fall.ogg");
-    audio_bind(AUD_TIME,   1, "snd/time.ogg");
-    audio_bind(AUD_OVER,   1, "snd/over.ogg");
+    /* Initialize audio. */
 
     audio_init();
+    tilt_init();
 
-    /* Require 16-bit double buffer with 16-bit depth buffer. */
+    /* Initialize video. */
 
-    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,     5);
-    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   5);
-    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,    5);
-    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
-    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+    if (!video_init(TITLE, ICON))
+        return 1;
+
+    init_state(&st_null);
 
-    /* Initialize the video. */
+    /* Initialize demo playback or load the level. */
 
-    if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
-                     config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
+    if (opt_replay &&
+        fs_add_path(dir_name(opt_replay)) &&
+        progress_replay(base_name(opt_replay)))
     {
-        fprintf(stderr, "%s\n", SDL_GetError());
-        return 1;
+        demo_play_goto(1);
+        goto_state(&st_demo_play);
     }
+    else if (opt_level)
+    {
+        const char *path = fs_resolve(opt_level);
+        int loaded = 0;
 
-    /* Set the WM icon */
+        if (path)
+        {
+            /* HACK: must be around for the duration of the game. */
+            static struct level level;
 
-    icon = IMG_Load(config_data("icon/neverball.png"));
-    SDL_WM_SetIcon(icon, NULL);
-    SDL_WM_SetCaption(TITLE, TITLE);
+            if (level_load(path, &level))
+            {
+                progress_init(MODE_STANDALONE);
 
-    /* Initialize the run state. */
+                if (progress_play(&level))
+                {
+                    goto_state(&st_level);
+                    loaded = 1;
+                }
+            }
+        }
+        else fprintf(stderr, "%s: file is not in game path\n", opt_level);
 
-    init_state(&st_null);
-    if (replay_path != NULL)
-    {
-        level_replay(replay_path);
-        goto_demo_play(1);
-    }
-    else if (level_path != NULL)
-    {
-        level_play_single(level_path);
-        goto_state(&st_level);
+        if (!loaded)
+            goto_state(&st_title);
     }
     else
         goto_state(&st_title);
@@ -429,37 +531,36 @@ int main(int argc, char *argv[])
     /* Run the main game loop. */
 
     t0 = SDL_GetTicks();
+
     while (loop())
+    {
         if ((t1 = SDL_GetTicks()) > t0)
         {
-            if (config_get_pause())
-            {
-                st_paint();
-                gui_blank();
-                SDL_Delay(10); /* Be nice! */
-            }
-            else
-            {
-                st_timer((t1 - t0) / 1000.f);
-                st_paint();
-            }
-            SDL_GL_SwapBuffers();
+            /* Step the game state. */
+
+            st_timer(0.001f * (t1 - t0));
 
             t0 = t1;
 
+            /* Render. */
+
+            st_paint(0.001f * t0);
+            shot_take();
+            video_swap();
+
             if (config_get_d(CONFIG_NICE))
                 SDL_Delay(1);
         }
+    }
 
-    /* Gracefully close the game */
+    config_save();
 
-    if (SDL_JoystickOpened(0))
+    if (joy)
         SDL_JoystickClose(joy);
 
+    tilt_free();
     SDL_Quit();
 
-    config_save();
-
     return 0;
 }