Removed remaining SDL_mixer includes
[neverball] / ball / st_demo.c
index d672167..a876053 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
 #include "st_demo.h"
 #include "st_title.h"
 
-extern struct state st_demo_play;
-extern struct state st_demo_end;
-extern struct state st_demo_del;
-
 /*---------------------------------------------------------------------------*/
 
 #define DEMO_LINE 4
@@ -65,27 +61,35 @@ static int demo_action(int i)
         return goto_state(&st_demo);
         break;
 
+    case GUI_NULL:
+        return 1;
+        break;
+
     default:
-        if (level_replay(get_demo(i)->filename))
-            return goto_demo_play(0);
+        if (level_replay(demo_get(i)->filename))
+            demo_play_goto(0);
+            return goto_state(&st_demo_play);
     }
     return 1;
 }
 
+/*---------------------------------------------------------------------------*/
+
 static void demo_replay(int id, int i)
 {
     int w = config_get_d(CONFIG_WIDTH);
     int h = config_get_d(CONFIG_HEIGHT);
     int jd;
+
     char nam[MAXNAM + 3];
 
     if ((jd = gui_vstack(id)))
     {
         gui_space(jd);
+        gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
 
-        gui_image(jd, get_demo(i)->shot, w / 6, h / 6);
         nam[MAXNAM - 1] = '\0';
-        strncpy(nam, get_demo(i)->name, MAXNAM);
+        strncpy(nam, demo_get(i)->name, MAXNAM);
         if (nam[MAXNAM - 1] != '\0')
         {
             nam[MAXNAM - 2] = '.';
@@ -103,17 +107,14 @@ static int name_id;
 static int time_id;
 static int coin_id;
 static int date_id;
-static int level_id;
 static int mode_id;
-static int state_id;
+static int status_id;
 static int player_id;
 
 static int gui_demo_status(int id, const struct demo *d)
-/* Create a layout for some demo info, if d is NULL, try to reserve enough
- * space */
 {
     char noname[MAXNAM];
-    const char *mode, *state;
+    const char *mode, *status;
     int i, j, k;
     int jd, kd, ld, md;
 
@@ -124,35 +125,37 @@ static int gui_demo_status(int id, const struct demo *d)
         noname[MAXNAM - 1] = '\0';
 
         /* Get a long mode */
-        mode = mode_to_str(0);
+        mode = mode_to_str(0, 0);
         j = strlen(mode);
-        for (i = 1; i <= MODE_SINGLE; i++)
+
+        for (i = 1; i <= MODE_COUNT; i++)
         {
-            k = strlen(mode_to_str(i));
+            k = strlen(mode_to_str(i, 0));
+
             if (k > j)
             {
                 j = k;
-                mode = mode_to_str(i);
+                mode = mode_to_str(i, 0);
             }
         }
 
-        /* Get a long state */
-        state = state_to_str(0);
-        j = strlen(state);
+        /* Get a long status */
+        status = status_to_str(0);
+        j = strlen(status);
         for (i = 1; i <= GAME_FALL; i++)
         {
-            k = strlen(state_to_str(i));
+            k = strlen(status_to_str(i));
             if (k > j)
             {
                 j = k;
-                state = state_to_str(i);
+                status = status_to_str(i);
             }
         }
     }
     else
     {
-        mode = mode_to_str(d->mode);
-        state = state_to_str(d->state);
+        mode = mode_to_str(d->mode, 0);
+        status = status_to_str(d->status);
     }
 
     if ((jd = gui_hstack(id)))
@@ -163,103 +166,85 @@ static int gui_demo_status(int id, const struct demo *d)
             {
                 if ((md = gui_vstack(ld)))
                 {
-                    player_id =
-                        gui_label(md, (d ? d->player : noname), GUI_SML,
-                                  GUI_RGT, 0, 0);
-                    coin_id =
-                        gui_count(md, (d ? d->coins : 100), GUI_SML, GUI_RGT);
-                    state_id =
-                        gui_label(md, state, GUI_SML, GUI_RGT, gui_red,
-                                  gui_red);
+                    player_id = gui_label(md, (d ? d->player : noname),
+                                          GUI_SML, GUI_RGT, 0, 0);
+                    coin_id = gui_count(md, (d ? d->coins : 100),
+                                        GUI_SML, GUI_RGT);
+                    status_id = gui_label(md, status, GUI_SML, GUI_RGT,
+                                         gui_red, gui_red);
                 }
                 if ((md = gui_vstack(ld)))
                 {
-                    gui_label(md, _("Player"), GUI_SML, GUI_LFT, gui_wht,
-                              gui_wht);
-                    gui_label(md, _("Coins"), GUI_SML, GUI_LFT, gui_wht,
-                              gui_wht);
-                    gui_label(md, _("State"), GUI_SML, GUI_LFT, gui_wht,
-                              gui_wht);
+                    gui_label(md, _("Player"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
+                    gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
+                    gui_label(md, _("State"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
                 }
                 if ((md = gui_vstack(ld)))
                 {
-                    name_id =
-                        gui_label(md, (d ? d->name : noname), GUI_SML, GUI_RGT,
-                                  0, 0);
-                    time_id =
-                        gui_clock(md, (d ? d->timer : 35000), GUI_SML, GUI_RGT);
+                    name_id = gui_label(md, (d ? d->name : noname),
+                                        GUI_SML, GUI_RGT, 0, 0);
+                    time_id = gui_clock(md, (d ? d->timer : 35000),
+                                        GUI_SML, GUI_RGT);
                     mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
                 }
             }
-            level_id =
-                gui_label(kd, (d ? d->file : "M"), GUI_SML, GUI_RGT, gui_wht,
-                          gui_wht);
-            date_id =
-                gui_label(kd, (d ? date_to_str(d->date) : "M"), GUI_SML,
-                          GUI_RGT, 0, 0);
+            date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
+                                GUI_SML, GUI_RGT, 0, 0);
         }
         if ((kd = gui_vstack(jd)))
         {
             gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Time"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Mode"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Level"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Date"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
         }
-        if (d && (d->state == GAME_GOAL || d->state == GAME_SPEC))
-            gui_set_color(state_id, gui_grn, gui_grn);
+        if (d && d->status == GAME_GOAL)
+            gui_set_color(status_id, gui_grn, gui_grn);
     }
     return jd;
 }
 
 static void gui_demo_update_status(int i)
 {
-    const struct demo *d = get_demo(i);
+    const struct demo *d = demo_get(i);
 
     gui_set_label(name_id,   d->name);
     gui_set_label(date_id,   date_to_str(d->date));
-    gui_set_label(level_id,  d->file);
     gui_set_label(player_id, d->player);
-    gui_set_label(mode_id,   mode_to_str(d->mode));
-    
-    if (d->state == GAME_GOAL || d->state == GAME_SPEC)
-        gui_set_color(state_id, gui_grn, gui_grn);
+    gui_set_label(mode_id,   mode_to_str(d->mode, 0));
+
+    if (d->status == GAME_GOAL)
+        gui_set_color(status_id, gui_grn, gui_grn);
     else
-        gui_set_color(state_id, gui_red, gui_red);
-    
-    gui_set_label(state_id, state_to_str(d->state));
+        gui_set_color(status_id, gui_red, gui_red);
+
+    gui_set_label(status_id, status_to_str(d->status));
     gui_set_count(coin_id, d->coins);
     gui_set_clock(time_id, d->timer);
 }
 
+/*---------------------------------------------------------------------------*/
+
 static int demo_enter(void)
 {
     int i, j;
-    int id, jd, kd, ld;
-
-    total = demo_scan();
+    int id, jd, kd;
 
     id = gui_vstack(0);
-    if (total == 0)
-    {
-        gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0,0);
-        gui_filler(id);
-        gui_multi(id, _("You can save replays of your games.\\"
-                        "Currently, there are no replays saved."),
-                  GUI_SML, GUI_ALL, gui_wht, gui_wht);
-        gui_filler(id);
-        gui_start(id, _("Back"), GUI_SML, GUI_BACK, 0);
-        gui_layout(id, 0, 0);
-    }
-    else
+
+    if ((total = demo_scan()))
     {
         if ((jd = gui_hstack(id)))
         {
 
-            ld = gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
+            gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
             gui_filler(jd);
             gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
         }
+
         if ((jd = gui_varray(id)))
             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
                 if ((kd = gui_harray(jd)))
@@ -275,16 +260,30 @@ static int demo_enter(void)
         gui_layout(id, 0, 0);
         gui_demo_update_status(0);
     }
+    else
+    {
+        gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
+        gui_layout(id, 0, 0);
+    }
 
     audio_music_fade_to(0.5f, "bgm/inter.ogg");
 
     return id;
 }
 
+static void demo_timer(int id, float dt)
+{
+    if (total == 0 && time_state() > 4.0f)
+        goto_state(&st_title);
+
+    gui_timer(id, dt);
+}
+
 static void demo_point(int id, int x, int y, int dx, int dy)
 {
     int jd = shared_point_basic(id, x, y);
     int i  = gui_token(jd);
+
     if (jd && i >= 0)
         gui_demo_update_status(i);
 }
@@ -293,6 +292,7 @@ static void demo_stick(int id, int a, int v)
 {
     int jd = shared_stick_basic(id, a, v);
     int i  = gui_token(jd);
+
     if (jd && i >= 0)
         gui_demo_update_status(i);
 }
@@ -302,7 +302,7 @@ static int demo_buttn(int b, int d)
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
-            return demo_action(gui_token(gui_click()));
+            return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
             return demo_action(GUI_BACK);
     }
@@ -311,18 +311,25 @@ static int demo_buttn(int b, int d)
 
 /*---------------------------------------------------------------------------*/
 
-static int simple_play;
+static int standalone;
+static int demo_paused;
 
-int goto_demo_play(int simple)
+void demo_play_goto(int s)
 {
-    simple_play = simple;
-    return goto_state(&st_demo_play);
+    standalone = s;
 }
 
 static int demo_play_enter(void)
 {
     int id;
 
+    if (demo_paused)
+    {
+        demo_paused = 0;
+        audio_music_fade_in(0.5f);
+        return 0;
+    }
+
     if ((id = gui_vstack(0)))
     {
         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
@@ -355,7 +362,6 @@ static void demo_play_timer(int id, float dt)
 
     game_step_fade(dt);
     gui_timer(id, dt);
-    audio_timer(dt);
 
     global_time += dt;
     hud_timer(dt);
@@ -367,29 +373,48 @@ static void demo_play_timer(int id, float dt)
         {
             replay_time += t;
         }
-        else 
+        else
         {
+            demo_paused = 0;
             goto_state(&st_demo_end);
             break;
         }
 }
 
+static int demo_play_keybd(int c, int d)
+{
+    if (d)
+    {
+        if (config_tst_d(CONFIG_KEY_PAUSE, c))
+        {
+            demo_paused = 1;
+            return goto_state(&st_demo_end);
+        }
+    }
+    return 1;
+}
+
 static int demo_play_buttn(int b, int d)
 {
     if (d)
     {
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
+        {
+            if (!(SDL_GetModState() & KMOD_SHIFT))
+                demo_paused = 1;
             return goto_state(&st_demo_end);
+        }
     }
     return 1;
 }
 
 /*---------------------------------------------------------------------------*/
 
-#define DEMO_KEEP    0
-#define DEMO_DEL     1
-#define DEMO_QUIT    2
-#define DEMO_REPLAY  3
+#define DEMO_KEEP      0
+#define DEMO_DEL       1
+#define DEMO_QUIT      2
+#define DEMO_REPLAY    3
+#define DEMO_CONTINUE  4
 
 static int demo_end_action(int i)
 {
@@ -397,9 +422,11 @@ static int demo_end_action(int i)
 
     switch (i)
     {
-    case DEMO_DEL: 
+    case DEMO_DEL:
+        demo_paused = 0;
         return goto_state(&st_demo_del);
     case DEMO_KEEP:
+        demo_paused = 0;
         demo_replay_stop(0);
         return goto_state(&st_demo);
     case DEMO_QUIT:
@@ -409,6 +436,8 @@ static int demo_end_action(int i)
         demo_replay_stop(0);
         level_replay(curr_demo_replay()->filename);
         return goto_state(&st_demo_play);
+    case DEMO_CONTINUE:
+        return goto_state(&st_demo_play);
     }
     return 1;
 }
@@ -419,48 +448,62 @@ static int demo_end_enter(void)
 
     if ((id = gui_vstack(0)))
     {
-        kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
+        if (demo_paused)
+            kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
+                           gui_gry, gui_red);
+        else
+            kd = gui_label(id, _("Replay Ends"),   GUI_LRG, GUI_ALL,
+                           gui_gry, gui_red);
 
         if ((jd = gui_harray(id)))
         {
-            gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
-            if (simple_play)
-                gui_start(jd, _("OK"),       GUI_SML, DEMO_QUIT,   1);
+            int start_id = 0;
+
+            if (standalone)
+            {
+                start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
+            }
             else
             {
-                gui_start(jd, _("Keep"),     GUI_SML, DEMO_KEEP,   1);
-                gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
+                start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
+                gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
             }
-        }
-
-        gui_filler(id);
 
-        if ((jd = gui_hstack(id)))
-        {
-            gui_filler(jd);
-            gui_demo_status(jd, curr_demo_replay());
-            gui_filler(jd);
+            if (demo_paused)
+            {
+                gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
+                gui_toggle(start_id);
+            }
+            else
+                gui_state(jd, _("Repeat"),   GUI_SML, DEMO_REPLAY,   0);
         }
 
         gui_pulse(kd, 1.2f);
         gui_layout(id, 0, 0);
     }
-    audio_music_fade_out(2.0f);
+
+    audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
 
     return id;
 }
 
-static void demo_end_timer(int id, float dt)
+static void demo_end_paint(int id, float st)
 {
-    float gg[3] = { 0.0f,  9.8f, 0.0f };
-    float gf[3] = { 0.0f, -9.8f, 0.0f };
-    int state = curr_demo_replay()->state;
+    game_draw(0, st);
+    gui_paint(id);
 
-    if (time_state() < 2.f && state != GAME_NONE)
-        game_step((state == GAME_GOAL || state == GAME_SPEC) ? gg : gf, dt, NULL);
-                
-    gui_timer(id, dt);
-    audio_timer(dt);
+    if (demo_paused)
+        hud_paint();
+}
+
+static int demo_end_keybd(int c, int d)
+{
+    if (d)
+    {
+        if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
+            return demo_end_action(DEMO_CONTINUE);
+    }
+    return 1;
 }
 
 static int demo_end_buttn(int b, int d)
@@ -470,7 +513,12 @@ static int demo_end_buttn(int b, int d)
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
             return demo_end_action(gui_token(gui_click()));
         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
-            return demo_end_action(simple_play ? DEMO_QUIT : DEMO_KEEP);
+        {
+            if (demo_paused)
+                return demo_end_action(DEMO_CONTINUE);
+            else
+                return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
+        }
     }
     return 1;
 }
@@ -525,13 +573,13 @@ struct state st_demo = {
     demo_enter,
     shared_leave,
     shared_paint,
-    shared_timer,
+    demo_timer,
     demo_point,
     demo_stick,
     shared_click,
     NULL,
     demo_buttn,
-    0
+    1, 0
 };
 
 struct state st_demo_play = {
@@ -542,20 +590,20 @@ struct state st_demo_play = {
     NULL,
     NULL,
     NULL,
-    NULL,
+    demo_play_keybd,
     demo_play_buttn,
-    0
+    1, 0
 };
 
 struct state st_demo_end = {
     demo_end_enter,
     shared_leave,
-    shared_paint,
-    demo_end_timer,
+    demo_end_paint,
+    shared_timer,
     shared_point,
     shared_stick,
     shared_click,
-    NULL,
+    demo_end_keybd,
     demo_end_buttn,
     1, 0
 };