Added Grow coin textures, and fixed small grow bug.
[neverball] / ball / main.c
index 6fd8f3d..53b72f8 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
 #include <SDL_image.h>
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 
 #include "glext.h"
 #include "config.h"
 #include "image.h"
 #include "audio.h"
 #include "demo.h"
+#include "levels.h"
 #include "game.h"
 #include "gui.h"
 #include "set.h"
 
 #include "st_conf.h"
 #include "st_title.h"
+#include "st_demo.h"
+#include "st_level.h"
 
-#define TITLE _("Neverball")
+#define TITLE "Neverball"
+#define VERSION "1.4.1svn"
 
 /*---------------------------------------------------------------------------*/
 
@@ -51,9 +56,10 @@ static void shot(void)
     static char filename[MAXSTR];
     static int  num = 0;
 
-    sprintf(filename, _("screen%02d.bmp"), num++);
+    sprintf(filename, _("screen%02d.png"), num++);
 
-    image_snap(filename);
+    image_snap(filename, config_get_d(CONFIG_WIDTH),
+               config_get_d(CONFIG_HEIGHT));
 }
 
 /*---------------------------------------------------------------------------*/
@@ -81,12 +87,13 @@ 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));
+    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)
@@ -99,8 +106,17 @@ static int loop(void)
         if (e.type == SDL_QUIT)
             return 0;
 
-        if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE)
-            config_tgl_pause();
+        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)
@@ -116,24 +132,22 @@ static int loop(void)
             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:
-                
+
                 switch (e.key.keysym.sym)
                 {
-                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;
-                
+
                 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;
@@ -146,9 +160,12 @@ static int loop(void)
                 case SDLK_DOWN:
                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
                     break;
-                             
-                default: 
-                    d = st_keybd(e.key.keysym.sym, 1);
+
+                default:
+                    if (SDL_EnableUNICODE(-1))
+                        d = st_keybd(e.key.keysym.unicode, 1);
+                    else
+                        d = st_keybd(e.key.keysym.sym, 1);
                 }
                 break;
 
@@ -159,6 +176,9 @@ static int loop(void)
                 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);
@@ -196,117 +216,250 @@ static int loop(void)
     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)
+{
+#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. */
+
+    while (*argv != NULL)
+    {
+        missing = 0;
+        if (CASE("-h") || CASE("-?") || CASE("--help"))
+        {
+            printf(USAGE, exec);
+            exit(0);
+        }
+        else if (CASE("-v") || CASE("--version"))
+        {
+            printf(_("%s: %s version %s\n"), exec, TITLE, VERSION);
+            exit(0);
+        }
+        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)
+        {
+            fprintf(stderr, _("%s: unknown option %s\n"), exec, *argv);
+            fprintf(stderr, USAGE, exec);
+            exit(1);
+        }
+        else
+        {
+            fprintf(stderr, _("%s: option %s requires an argument\n"), exec,
+                    *argv);
+            fprintf(stderr, USAGE, exec);
+            exit(1);
+        }
+        argv++;
+    }
+    return;
+}
+
 int main(int argc, char *argv[])
 {
+    SDL_Joystick *joy = NULL;
+    int t1, t0;               /* ticks */
+    SDL_Surface *icon;        /* WM icon */
+
     language_init("neverball", CONFIG_LOCALE);
-    
-    if (config_data_path((argc > 1 ? argv[1] : NULL), SET_FILE))
+
+    parse_args(argc, argv);
+
+    if (!config_data_path(data_path, SET_FILE))
+    {
+        fprintf(stderr, _("Failure to establish game data directory\n"));
+        return 1;
+    }
+
+    if (!config_user_path(NULL))
+    {
+        fprintf(stderr, _("Failure to establish config directory\n"));
+        return 1;
+    }
+
+    /* Intitialize the configuration */
+
+    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 (config_user_path(NULL))
+        if (!level_replay(replay_path))
         {
-            if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK))
-            {
-                SDL_Joystick *joy = NULL;
+            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();
+    }
 
-                config_init();
-                config_load();
+    if (level_path != NULL)
+    {
+        struct level l;
+        if (!level_load(level_path, &l))
+            return 1;
+        else if (display_info)
+            level_dump_info(&l);
+    }
 
-               /* Initialize the language. */
-               language_set(language_from_code(config_simple_get_s(CONFIG_LANG)));
+    if (display_info)
+    {
+        if (replay_path == NULL && level_path == NULL)
+        {
+            fprintf(stderr, _("%s: --info requires --replay or --level\n"),
+                    argv[0]);
+            return 1;
+        }
+        return 0;
+    }
 
-                /* Initialize the joystick. */
+    /* Initialize SDL system and subsystems */
 
-                if (SDL_NumJoysticks() > 0)
-                {
-                    joy=SDL_JoystickOpen(config_get_d(CONFIG_JOYSTICK_DEVICE));
-                    if (joy)
-                        SDL_JoystickEventState(SDL_ENABLE);
-                }
+    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
+    {
+        fprintf(stderr, "%s\n", SDL_GetError());
+        return 1;
+    }
 
-                /* 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. */
-
-                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);
-
-                /* Initialize the video. */
-
-                if (config_mode(config_get_d(CONFIG_FULLSCREEN),
-                                config_get_d(CONFIG_WIDTH),
-                                config_get_d(CONFIG_HEIGHT)))
-                {
-                    int t1, t0 = SDL_GetTicks();
-
-                   SDL_Surface *icon = IMG_Load(config_data("icon/neverball.png"));
-                   SDL_WM_SetIcon(icon, NULL);
-                    SDL_WM_SetCaption(TITLE, TITLE); 
-
-                    /* Initialize the run state and the title display. */
-
-                    init_state(&st_null);
-                    goto_state(&st_title);
-
-                    /* Run the main game loop. */
-
-                    while (loop())
-                        if ((t1 = SDL_GetTicks()) > t0)
-                        {
-                            if (config_get_pause())
-                            {
-                                st_paint();
-                                gui_blank();
-                            }
-                            else
-                            {
-                                st_timer((t1 - t0) / 1000.f);
-                                st_paint();
-                            }
-                            SDL_GL_SwapBuffers();
-
-                            t0 = t1;
-
-                            if (config_get_d(CONFIG_NICE))
-                                SDL_Delay(1);
-                        }
-                }
-                else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
+    /* Initialize the joystick. */
+
+    if (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");
+
+    audio_init();
+
+    /* Require 16-bit double buffer with 16-bit depth buffer. */
+
+    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);
+
+    /* Initialize the video. */
+
+    if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
+                     config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT)))
+    {
+        fprintf(stderr, "%s\n", SDL_GetError());
+        return 1;
+    }
+
+    /* Set the WM icon */
 
-                config_save();
+    icon = IMG_Load(config_data("icon/neverball.png"));
+    SDL_WM_SetIcon(icon, NULL);
+    SDL_WM_SetCaption(TITLE, TITLE);
 
-                if (SDL_JoystickOpened(0))
-                    SDL_JoystickClose(joy);
+    /* Initialize the run state. */
 
-                SDL_Quit();
+    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);
+    }
+    else
+        goto_state(&st_title);
+
+    /* 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();
             }
-            else fprintf(stderr, "%s: %s\n", argv[0], SDL_GetError());
+            SDL_GL_SwapBuffers();
+
+            t0 = t1;
+
+            if (config_get_d(CONFIG_NICE))
+                SDL_Delay(1);
         }
-        else fprintf(stderr, _("Failure to establish config directory\n"));
-    }
-    else fprintf(stderr, _("Failure to establish game data directory\n"));
+
+    /* Gracefully close the game */
+
+    if (SDL_JoystickOpened(0))
+        SDL_JoystickClose(joy);
+
+    SDL_Quit();
+
+    config_save();
 
     return 0;
 }