demo_header_write() now writes the header. :-P
[neverball] / ball / main.c
index 1c0073d..ff97ead 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
@@ -47,7 +47,6 @@
 #include "st_level.h"
 
 #define TITLE "Neverball"
-#define VERSION "1.4.1svn"
 
 /*---------------------------------------------------------------------------*/
 
@@ -58,8 +57,7 @@ static void shot(void)
 
     sprintf(filename, _("screen%02d.png"), num++);
 
-    image_snap(filename, config_get_d(CONFIG_WIDTH),
-               config_get_d(CONFIG_HEIGHT));
+    image_snap(filename);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -132,16 +130,16 @@ 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_RETURN:
                     d = st_buttn(config_get_d(CONFIG_JOYSTICK_BUTTON_A), 1);
                     break;
@@ -160,9 +158,9 @@ static int loop(void)
                 case SDLK_DOWN:
                     st_stick(config_get_d(CONFIG_JOYSTICK_AXIS_Y), +JOY_MAX);
                     break;
-                             
+
                 default:
-                    if (SDL_EnableUNICODE(-1)) 
+                    if (SDL_EnableUNICODE(-1))
                         d = st_keybd(e.key.keysym.unicode, 1);
                     else
                         d = st_keybd(e.key.keysym.sym, 1);
@@ -219,6 +217,7 @@ static int loop(void)
 /*---------------------------------------------------------------------------*/
 
 /* Option values */
+
 static char *data_path    = NULL;
 static char *replay_path  = NULL;
 static char *level_path   = NULL;
@@ -226,33 +225,35 @@ 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. */
 
+    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)
     {
         missing = 0;
         if (CASE("-h") || CASE("-?") || CASE("--help"))
         {
-            printf(USAGE, exec);
+            printf(usage, exec);
             exit(0);
         }
         else if (CASE("-v") || CASE("--version"))
         {
-            printf(_("%s: %s version %s\n"), exec, TITLE, VERSION);
+            printf("%s %s\n", TITLE, VERSION);
             exit(0);
         }
         else if (CASE("--data") && MAND)
@@ -266,14 +267,14 @@ static void parse_args(int argc, char **argv)
         else if (!missing)
         {
             fprintf(stderr, _("%s: unknown option %s\n"), exec, *argv);
-            fprintf(stderr, USAGE, exec);
+            fprintf(stderr, usage, exec);
             exit(1);
         }
         else
         {
             fprintf(stderr, _("%s: option %s requires an argument\n"), exec,
                     *argv);
-            fprintf(stderr, USAGE, exec);
+            fprintf(stderr, usage, exec);
             exit(1);
         }
         argv++;
@@ -284,51 +285,49 @@ static void parse_args(int argc, char **argv)
 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);
 
     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)
+    /* Prepare run without SDL */
+
+    if (replay_path)
     {
         if (!level_replay(replay_path))
         {
-            fprintf(stderr, _("Replay file '%s': "), replay_path);
-            if (errno)
-                perror(NULL);
-            else
-                fprintf(stderr, _("Not a replay file\n"));
+            fprintf(stderr, _("Replay file '%s': %s\n"), replay_path,
+                    errno ? strerror(errno) : _("Not a replay file"));
             return 1;
         }
         else if (display_info)
             demo_replay_dump_info();
     }
-    
+
     if (level_path != NULL)
     {
         struct level l;
@@ -348,9 +347,9 @@ int main(int argc, char *argv[])
         }
         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());
@@ -396,6 +395,16 @@ int main(int argc, char *argv[])
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16);
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 
+    /* Set the WM icon */
+
+    icon = IMG_Load(config_data("icon/neverball.png"));
+
+    if (icon)
+    {
+        SDL_WM_SetIcon(icon, NULL);
+        SDL_FreeSurface(icon);
+    }
+
     /* Initialize the video. */
 
     if (!config_mode(config_get_d(CONFIG_FULLSCREEN),
@@ -404,20 +413,18 @@ int main(int argc, char *argv[])
         fprintf(stderr, "%s\n", SDL_GetError());
         return 1;
     }
-   
-    /* Set the WM icon */ 
-    
-    icon = IMG_Load(config_data("icon/neverball.png"));
-    SDL_WM_SetIcon(icon, NULL);
-    SDL_WM_SetCaption(TITLE, TITLE); 
+
+    SDL_WM_SetCaption(TITLE, TITLE);
 
     /* Initialize the run state. */
-    
+
     init_state(&st_null);
-    if (replay_path != NULL)
+
+    if (replay_path)
     {
         level_replay(replay_path);
-        goto_demo_play(1);
+        demo_play_goto(1);
+        goto_state(&st_demo_play);
     }
     else if (level_path != NULL)
     {
@@ -437,7 +444,7 @@ int main(int argc, char *argv[])
             {
                 st_paint();
                 gui_blank();
-                SDL_Delay(10); /* Be nice! */
+                SDL_Delay(10);
             }
             else
             {
@@ -458,9 +465,9 @@ int main(int argc, char *argv[])
         SDL_JoystickClose(joy);
 
     SDL_Quit();
-    
+
     config_save();
-    
+
     return 0;
 }