DEADSOFTWARE

sound: move sound/music configuration to sound driver
[flatwaifu.git] / src / menu.c
index 4a15ede8a4cce10306f652f8ed3a719d32ca08c7..7286a45375a11e4a271996357ad3b9150b93f9d6 100644 (file)
@@ -39,6 +39,8 @@
 #include "input.h"
 #include "system.h"
 
+#include "save.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -236,78 +238,27 @@ static const menu_t save_game_menu = {
   NULL, &save_game_menu_handler
 };
 
-static int sound_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
-  static int cur;
-  enum { VOLUME, __NUM__ };
-  static const simple_menu_t sm = {
-    GM_BIG, "Sound", NULL,
-    {
-      { "Volume", NULL },
-    }
-  };
-  if (i == VOLUME) {
-    switch (msg->type) {
-      case GM_GETENTRY: return GM_init_int0(msg, GM_SCROLLER, 0, 0, 0);
-      case GM_GETINT: return GM_init_int(msg, snd_vol, 0, 128, 8);
-      case GM_SETINT: S_volume(msg->integer.i); return 1;
-    }
-  }
-  return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
-}
-
-static const menu_t sound_menu = {
-  NULL, &sound_menu_handler
-};
-
-static int music_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
-  static int cur;
-  enum { VOLUME, MUSIC, __NUM__ };
-  static const simple_menu_t sm = {
-    GM_BIG, "Music", NULL,
-    {
-      { "Volume", NULL },
-      { "Music: ", NULL },
-    }
-  };
-  if (i == VOLUME) {
-    switch (msg->type) {
-      case GM_GETENTRY: return GM_init_int0(msg, GM_SCROLLER, 0, 0, 0);
-      case GM_GETINT: return GM_init_int(msg, mus_vol, 0, 128, 8);
-      case GM_SETINT: S_volumemusic(msg->integer.i); return 1;
-    }
-  } else if (i == MUSIC) {
-    switch (msg->type) {
-      case GM_GETSTR: return GM_init_str(msg, g_music, strlen(g_music));
-      case GM_SELECT:
-        F_freemus();
-        F_nextmus(g_music);
-        F_loadmus(g_music);
-        S_startmusic(music_time * 2); // ???
-        return 1;
-    }
-  }
-  return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
-}
-
-static const menu_t music_menu = {
-  NULL, &music_menu_handler
-};
-
 static int options_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
   static int cur;
+  const menu_t *mm;
   enum { VIDEO, SOUND, MUSIC, __NUM__ };
   static const simple_menu_t sm = {
     GM_BIG, "Options", NULL,
     {
       { "Video", NULL },
-      { "Sound", &sound_menu },
-      { "Music", &music_menu },
+      { "Sound", NULL },
+      { "Music", NULL },
     }
   };
   if (msg->type == GM_SELECT) {
-    if (i == VIDEO) {
-      const menu_t *mm = R_menu();
-      return mm ? GM_push(mm) : 1;
+    switch (i) {
+      case VIDEO: mm = R_menu(); break;
+      case SOUND: mm = S_menu(); break;
+      case MUSIC: mm = MUS_menu(); break;
+      default: mm = NULL;
+    }
+    if (mm != NULL) {
+      return GM_push(mm);
     }
   }
   return simple_menu_handler(msg, i, __NUM__, &sm, &cur);
@@ -332,7 +283,7 @@ static int exit_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int
   } else if (msg->type == GM_SELECT) {
     switch (i) {
       case YES:
-        F_freemus();
+        MUS_free();
         GM_stop();
         Z_sound(S_get(qsnd[myrand(QSND_NUM)]), 255);
         S_wait();
@@ -352,6 +303,7 @@ static const menu_t exit_menu = {
 static int main_menu_handler (menu_msg_t *msg, const menu_t *m, void *data, int i) {
   static int cur;
   enum { NEWGAME, OLDGAME, SAVEGAME, OPTIONS, EXIT, __NUM__ };
+  assert(i >= 0 && i < __NUM__);
   static const simple_menu_t sm = {
     GM_BIG, "Menu", NULL,
     {
@@ -495,7 +447,7 @@ static int state_for_anykey (int x) {
 int GM_act (void) {
   menu_msg_t msg;
   int n, cur, type;
-  const menu_t *m = GM_get ();
+  const menu_t *m = GM_get();
   if (m == NULL) {
     if (lastkey == KEY_ESCAPE || (state_for_anykey(g_st) && lastkey != KEY_UNKNOWN)) {
       GM_push(&main_menu);
@@ -503,93 +455,98 @@ int GM_act (void) {
     }
   } else {
     msg.type = GM_QUERY;
-    assert(GM_send_this(m, &msg));
-    cur = msg.integer.i;
-    n = msg.integer.a;
-    msg.type = GM_GETENTRY;
-    assert(GM_send(m, cur, &msg));
-    type = msg.integer.i;
-    switch (lastkey) {
-      case KEY_ESCAPE:
-        if (type == GM_TEXTFIELD && input) {
-          input = 0;
-          Y_disable_text_input();
-          msg.type = GM_CANCEL;
-          GM_send(m, cur, &msg);
-        } else {
-          GM_pop();
-          Z_sound(msnd4, 128);
-        }
-        break;
-      case KEY_UP:
-      case KEY_DOWN:
-        msg.type = lastkey == KEY_UP ? GM_UP : GM_DOWN;
-        if (GM_send(m, cur, &msg)) {
-          Z_sound(msnd1, 128);
-        }
-        break;
-      case KEY_LEFT:
-      case KEY_RIGHT:
-        if (type == GM_SCROLLER) {
-          msg.integer.type = GM_GETINT;
-          if (GM_send(m, cur, &msg)) {
-            msg.integer.type = GM_SETINT;
-            msg.integer.i += lastkey == KEY_LEFT ? -msg.integer.s : msg.integer.s;
-            msg.integer.i = min(max(msg.integer.i, msg.integer.a), msg.integer.b);
-            if (GM_send(m, cur, &msg)) {
-              Z_sound(lastkey == KEY_LEFT ? msnd5 : msnd6, 255);
+    if (GM_send_this(m, &msg)) {
+      cur = msg.integer.i;
+      n = msg.integer.a;
+      msg.type = GM_GETENTRY;
+      if (GM_send(m, cur, &msg)) {
+        type = msg.integer.i;
+        switch (lastkey) {
+          case KEY_ESCAPE:
+            if (type == GM_TEXTFIELD && input) {
+              input = 0;
+              Y_disable_text_input();
+              msg.type = GM_CANCEL;
+              GM_send(m, cur, &msg);
+            } else {
+              GM_pop();
+              Z_sound(msnd4, 128);
             }
-          }
-        } else if (type == GM_TEXTFIELD) {
-          //if (input) {
-          //  icur += lastkey == KEY_LEFT ? -1 : +1;
-          //  icur = min(max(icur, 0), strnlen(ibuf, imax));
-          //}
-        }
-        break;
-      case KEY_BACKSPACE:
-        if (type == GM_TEXTFIELD) {
-          if (input && icur > 0) {
-            // FIXIT buffers in strncpy must not overlap
-            strncpy(&ibuf[icur - 1], &ibuf[icur], imax - icur);
-            ibuf[imax - 1] = 0;
-            icur -= 1;
-          }
-        }
-        break;
-      case KEY_RETURN:
-        if (type == GM_TEXTFIELD) {
-          if (input) {
-            input = 0;
-            Y_disable_text_input();
-            msg.type = GM_END;
-            msg.string.s = ibuf;
-            msg.string.maxlen = imax;
-            GM_send(m, cur, &msg);
-          } else {
-            msg.type = GM_GETSTR;
-            if (GM_send(m, cur, &msg)) {
-              imax = min(msg.string.maxlen, GM_MAX_INPUT);
-              strncpy(ibuf, msg.string.s, imax);
-              icur = strnlen(ibuf, imax);
+            break;
+          case KEY_UP:
+          case KEY_DOWN:
+            if (input == 0) {
+              msg.type = lastkey == KEY_UP ? GM_UP : GM_DOWN;
+              if (GM_send(m, cur, &msg)) {
+                Z_sound(msnd1, 128);
+              }
+            }
+            break;
+          case KEY_LEFT:
+          case KEY_RIGHT:
+            if (type == GM_SCROLLER) {
+              msg.integer.type = GM_GETINT;
+              if (GM_send(m, cur, &msg)) {
+                msg.integer.type = GM_SETINT;
+                msg.integer.i += lastkey == KEY_LEFT ? -msg.integer.s : msg.integer.s;
+                msg.integer.i = min(max(msg.integer.i, msg.integer.a), msg.integer.b);
+                if (GM_send(m, cur, &msg)) {
+                  Z_sound(lastkey == KEY_LEFT ? msnd5 : msnd6, 255);
+                }
+              }
+            } else if (type == GM_TEXTFIELD) {
+              //if (input) {
+              //  icur += lastkey == KEY_LEFT ? -1 : +1;
+              //  icur = min(max(icur, 0), strnlen(ibuf, imax));
+              //}
+            }
+            break;
+          case KEY_BACKSPACE:
+            if (type == GM_TEXTFIELD) {
+              if (input && icur > 0) {
+                // FIXIT buffers in strncpy must not overlap
+                strncpy(&ibuf[icur - 1], &ibuf[icur], imax - icur);
+                ibuf[imax - 1] = 0;
+                icur -= 1;
+              }
+            }
+            break;
+          case KEY_RETURN:
+            if (type == GM_TEXTFIELD) {
+              if (input) {
+                input = 0;
+                Y_disable_text_input();
+                msg.type = GM_END;
+                msg.string.s = ibuf;
+                msg.string.maxlen = imax;
+                GM_send(m, cur, &msg);
+              } else {
+                msg.type = GM_GETSTR;
+                if (GM_send(m, cur, &msg)) {
+                  imax = min(msg.string.maxlen, GM_MAX_INPUT);
+                  strncpy(ibuf, msg.string.s, imax);
+                  icur = strnlen(ibuf, imax);
+                } else {
+                  memset(ibuf, 0, GM_MAX_INPUT);
+                  imax = GM_MAX_INPUT;
+                  icur = 0;
+                }
+                input = 1;
+                Y_enable_text_input();
+                msg.type = GM_BEGIN;
+                GM_send(m, cur, &msg);
+              }
+              Z_sound(msnd2, 128);
             } else {
-              memset(ibuf, 0, GM_MAX_INPUT);
-              imax = GM_MAX_INPUT;
-              icur = 0;
+              msg.type = GM_SELECT;
+              if (cur < 0) abort();
+              if (GM_send(m, cur, &msg)) {
+                Z_sound(msnd2, 128);
+              }
             }
-            input = 1;
-            Y_enable_text_input();
-            msg.type = GM_BEGIN;
-            GM_send(m, cur, &msg);
-          }
-          Z_sound(msnd2, 128);
-        } else {
-          msg.type = GM_SELECT;
-          if (GM_send(m, cur, &msg)) {
-            Z_sound(msnd2, 128);
-          }
+            break;
         }
-        break;
+      }
     }
   }
   lastkey = KEY_UNKNOWN;
@@ -648,6 +605,6 @@ void GM_init (void) {
   msnd4 = Z_getsnd("SWTCHX");
   msnd5 = Z_getsnd("SUDI");
   msnd6 = Z_getsnd("TUDI");
-  F_loadmus("MENU");
-  S_startmusic(0);
+  MUS_load("MENU");
+  MUS_start(0);
 }