Remove insidious F11 fullscreen toggle. That will never work under Windows.
[neverball] / ball / main.c
index e2ccc61..f9fb625 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 "game.h"
 #include "gui.h"
 #include "set.h"
+#include "text.h"
 
 #include "st_conf.h"
 #include "st_title.h"
 #include "st_demo.h"
 #include "st_level.h"
+#include "st_pause.h"
 
 #define TITLE "Neverball"
 
@@ -55,10 +45,9 @@ static void shot(void)
     static char filename[MAXSTR];
     static int  num = 0;
 
-    sprintf(filename, _("screen%02d.png"), num++);
+    sprintf(filename, "screen%02d.png", num++);
 
-    image_snap(filename, config_get_d(CONFIG_WIDTH),
-               config_get_d(CONFIG_HEIGHT));
+    image_snap(filename);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -83,225 +72,251 @@ static void toggle_wire(void)
     }
 }
 
-static void toggle_fullscreen(void)
-{
-    int x, y;
-
-    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);
-}
-
 /*---------------------------------------------------------------------------*/
 
 static int loop(void)
 {
     SDL_Event e;
     int d = 1;
+    int c;
 
     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 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:
+
+            c = e.key.keysym.sym;
+
+            if (config_tst_d(CONFIG_KEY_FORWARD, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), -JOY_MAX);
+
+            else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
+
+            else if (config_tst_d(CONFIG_KEY_LEFT, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), -JOY_MAX);
+
+            else if (config_tst_d(CONFIG_KEY_RIGHT, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), +JOY_MAX);
+
+            else switch (c)
             {
-            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);
+            case SDLK_F7:
+                if (config_cheat())
+                    toggle_wire();
                 break;
 
-            case SDL_MOUSEBUTTONDOWN:
-                d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 1);
+            case SDLK_RETURN:
+                d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
                 break;
-
-            case SDL_MOUSEBUTTONUP:
-                d = st_click((e.button.button == SDL_BUTTON_LEFT) ? -1 : 1, 0);
+            case SDLK_ESCAPE:
+                d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT), 1);
                 break;
 
-            case SDL_KEYDOWN:
-
-                switch (e.key.keysym.sym)
-                {
-
-                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;
+            default:
+                if (SDL_EnableUNICODE(-1))
+                    d = st_keybd(e.key.keysym.unicode, 1);
+                else
+                    d = st_keybd(e.key.keysym.sym, 1);
+            }
 
-            case SDL_KEYUP:
-
-                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);
-                }
+            break;
 
-                break;
+        case SDL_KEYUP:
 
-            case SDL_ACTIVEEVENT:
-                if (e.active.state == SDL_APPINPUTFOCUS)
-                    if (e.active.gain == 0 && config_get_grab())
-                        config_set_pause();
-                break;
+            c = e.key.keysym.sym;
 
-            case SDL_JOYAXISMOTION:
-                st_stick(e.jaxis.axis, e.jaxis.value);
-                break;
+            if (config_tst_d(CONFIG_KEY_FORWARD, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
 
-            case SDL_JOYBUTTONDOWN:
-                d = st_buttn(e.jbutton.button, 1);
-                break;
+            else if (config_tst_d(CONFIG_KEY_BACKWARD, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), 1);
+
+            else if (config_tst_d(CONFIG_KEY_LEFT, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
 
-            case SDL_JOYBUTTONUP:
-                d = st_buttn(e.jbutton.button, 0);
+            else if (config_tst_d(CONFIG_KEY_RIGHT, c))
+                st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_X), 1);
+
+            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);
             }
+
+        case SDL_ACTIVEEVENT:
+            if (e.active.state == SDL_APPINPUTFOCUS)
+                if (e.active.gain == 0 && config_get_grab())
+                    goto_pause();
+            break;
+
+        case SDL_JOYAXISMOTION:
+            st_stick(e.jaxis.axis, 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;
+        }
     }
     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 */
+static char *data_path = NULL;
+static char *demo_path = NULL;
+
+static unsigned int display_info = 0;
+static unsigned int replay_demo  = 0;
+
+#define 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"           \
+        "  -i, --info                display info about a replay.\n"      \
+    )
+
+#define argument_error(option) { \
+    fprintf(stderr, L_("Option '%s' requires an argument.\n"),  option); \
+}
 
 static void parse_args(int argc, char **argv)
 {
-    char *exec = *(argv++);
-    int missing; /* Argument is missing. */
-
-    const char *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"
-    );
-
-#define CASE(x) (strcmp(*argv, (x)) == 0)       /* Check current option */
-#define MAND    !(missing = (argv[1] == NULL))  /* Argument is mandatory */
-
-    while (*argv != NULL)
+    int i;
+
+    /* Scan argument list. */
+
+    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(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)
+            {
+                argument_error(argv[i]);
+                exit(EXIT_FAILURE);
+            }
+            data_path = 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)
+            {
+                argument_error(argv[i]);
+                exit(EXIT_FAILURE);
+            }
+            demo_path = argv[++i];
+            continue;
+        }
+
+        if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--info")    == 0)
+        {
+            display_info = 1;
+            continue;
         }
-        argv++;
     }
-    return;
+
+    /* Resolve conflicts. */
+
+    if (demo_path)
+        replay_demo = display_info ? 0 : 1;
+    else
+        if (display_info)
+        {
+            /* FIXME, I'm a required option. */
+            fputs(L_("Option '--info' requires '--replay'.\n"), stderr);
+            exit(EXIT_FAILURE);
+        }
 }
 
+#undef usage
+#undef argument_error
+
+/*---------------------------------------------------------------------------*/
+
 int main(int argc, char *argv[])
 {
     SDL_Joystick *joy = NULL;
-    int t1, t0;               /* ticks */
-    SDL_Surface *icon;        /* WM icon */
+    SDL_Surface *icon;
+
+    int t1, t0;
 
-    language_init("neverball", CONFIG_LOCALE);
+    lang_init("neverball", CONFIG_LOCALE);
+
+    text_init();
 
     parse_args(argc, argv);
 
     if (!config_data_path(data_path, SET_FILE))
     {
-        fprintf(stderr, _("Failure to establish game data directory\n"));
+        fputs(L_("Failure to establish game data directory\n"), stderr);
         return 1;
     }
 
     if (!config_user_path(NULL))
     {
-        fprintf(stderr, _("Failure to establish config directory\n"));
+        fputs(L_("Failure to establish config directory\n"), stderr);
+        return 1;
+    }
+
+    /* Initialize SDL system and subsystems */
+
+    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
+    {
+        fprintf(stderr, "%s\n", SDL_GetError());
         return 1;
     }
 
@@ -310,55 +325,20 @@ int main(int argc, char *argv[])
     config_init();
     config_load();
 
-    /* Initialize the language */
-
-    language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
-
-    /* Prepare run without sdl */
-
-    if (replay_path != NULL)
-    {
-        if (!level_replay(replay_path))
-        {
-            fprintf(stderr, _("Replay file '%s': "), replay_path);
-            if (errno)
-                perror(NULL);
-            else
-                fprintf(stderr, _("Not a replay file\n"));
-            return 1;
-        }
-        else if (display_info)
-            demo_replay_dump_info();
-    }
-
-    if (level_path != NULL)
-    {
-        struct level l;
-        if (!level_load(level_path, &l))
-            return 1;
-        else if (display_info)
-            level_dump_info(&l);
-    }
+    /* Dump replay information and exit. */
 
     if (display_info)
     {
-        if (replay_path == NULL && level_path == NULL)
+        if (!level_replay(demo_path))
         {
-            fprintf(stderr, _("%s: --info requires --replay or --level\n"),
-                    argv[0]);
+            fprintf(stderr, L_("Replay file '%s': %s\n"), demo_path,
+                    errno ?  strerror(errno) : L_("Not a replay file"));
             return 1;
         }
+        demo_replay_dump_info();
         return 0;
     }
 
-    /* Initialize SDL system and subsystems */
-
-    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
-    {
-        fprintf(stderr, "%s\n", SDL_GetError());
-        return 1;
-    }
-
     /* Initialize the joystick. */
 
     if (SDL_NumJoysticks() > 0)
@@ -370,24 +350,6 @@ int main(int argc, char *argv[])
 
     /* 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");
-
     audio_init();
 
     /* Require 16-bit double buffer with 16-bit depth buffer. */
@@ -398,6 +360,15 @@ int main(int argc, char *argv[])
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
+#ifndef __APPLE__
+    if ((icon = load_surface("icon/neverball.png")))
+    {
+        SDL_WM_SetIcon(icon, NULL);
+        free(icon->pixels);
+        SDL_FreeSurface(icon);
+    }
+#endif /* __APPLE__ */
+
     /* Initialize the video. */
 
     if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
@@ -407,24 +378,17 @@ int main(int argc, char *argv[])
         return 1;
     }
 
-    /* Set the WM icon */
-
-    icon = IMG_Load(config_data("icon/neverball.png"));
-    SDL_WM_SetIcon(icon, NULL);
     SDL_WM_SetCaption(TITLE, TITLE);
 
-    /* Initialize the run state. */
-
     init_state(&st_null);
-    if (replay_path != NULL)
-    {
-        level_replay(replay_path);
-        goto_demo_play(1);
-    }
-    else if (level_path != NULL)
+
+    /* Initialise demo playback. */
+
+    if (replay_demo)
     {
-        level_play_single(level_path);
-        goto_state(&st_level);
+        level_replay(demo_path);
+        demo_play_goto(1);
+        goto_state(&st_demo_play);
     }
     else
         goto_state(&st_title);
@@ -435,17 +399,8 @@ int main(int argc, char *argv[])
     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();
-            }
+            st_timer((t1 - t0) / 1000.f);
+            st_paint();
             SDL_GL_SwapBuffers();
 
             t0 = t1;
@@ -463,6 +418,8 @@ int main(int argc, char *argv[])
 
     config_save();
 
+    text_quit();
+
     return 0;
 }