rnd-20060207-1-src
[rocksndiamonds.git] / src / game_em / input.c
index f550494ceeaac64d6d30ca5a2010df304f2705bd..ae3e1d554d0f93c50fa5f3a1bea9a84848817ffc 100644 (file)
@@ -3,32 +3,15 @@
  * handle input from x11 and keyboard and joystick
  */
 
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
-#include <X11/Xatom.h>
-#include <X11/keysym.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-
 #include "global.h"
 #include "display.h"
 #include "level.h"
 
 
-#if defined(TARGET_X11)
-
-unsigned long Random;
+unsigned long RandomEM;
 
-struct PLAYER ply1;
-struct PLAYER ply2;
 struct LEVEL lev;
-
-struct LevelInfo_EM native_em_level;
+struct PLAYER ply[MAX_PLAYERS];
 
 unsigned short **Boom;
 unsigned short **Cave;
@@ -38,83 +21,14 @@ unsigned short **Draw;
 static unsigned short *Index[4][HEIGHT];
 static unsigned short Array[4][HEIGHT][WIDTH];
 
-static int input_die;
-static int input_pause;
-static int input_refresh;
-static int input_esc;
-
-static struct timeval tv1;
-static struct timeval tv2;
-
-static int player_mode;
-static char player_name[32];
-static int player_level;
-
-static void drawmenu(int pos)
-{
-  char buffer[256];
-
-  switch(pos)
-  {
-    case 0:
-      switch(player_mode)
-      {
-        case 0:
-         strcpy(buffer, "quit emerald mine");
-         break;
-
-        case 1:
-         strcpy(buffer, "single player");
-         break;
-
-        case 2:
-         strcpy(buffer, "teamwork");
-         break;
-      }
-
-      title_string(4, 0, 40, buffer);
-      break;
-
-    case 1:
-      sprintf(buffer, "name: %s", player_name);
-      title_string(5, 0, 40, buffer);
-      break;
-
-    case 2:
-      sprintf(buffer, "level: %d", player_level);
-      title_string(6, 0, 40, buffer);
-      break;
-
-    case 3:
-      title_string(7, 0, 40, "highscores");
-      break;
-
-    case 4:
-      title_string(8, 0, 40, "start");
-      break;
-
-    case 5:
-      title_string(10, 3, 13, "played");
-      title_string(10, 14, 26, "score");
-      title_string(10, 27, 37, "won");
-      break;
-
-    case 6:
-      sprintf(buffer, "%d", 0);
-      title_string(11, 3, 13, buffer);
-      sprintf(buffer, "%d", 0);
-      title_string(11, 14, 26, buffer);
-      sprintf(buffer, "%d", 0);
-      title_string(11, 27, 37, buffer);
-      break;
-  }
-}
+extern unsigned int screen_x;
+extern unsigned int screen_y;
 
 void game_init_vars(void)
 {
   int x, y;
 
-  Random = 1684108901;
+  RandomEM = 1684108901;
 
   for (y = 0; y < HEIGHT; y++)
     for (x = 0; x < WIDTH; x++)
@@ -142,225 +56,180 @@ void game_init_vars(void)
   Next = Index[1];
   Draw = Index[2];
   Boom = Index[3];
-
-  player_mode = 1;                     /* start off as single player */
-  strcpy(player_name, "dave");
-  player_level = 0;
-
-  input_pause = 1    * 0;
 }
 
-int game_play_init(int level_nr, char *filename)
+void InitGameEngine_EM()
 {
-#if 0
-  if (filename != NULL)
-  {
-#endif
+  prepare_em_level();
 
-    player_level = level_nr;
+  game_initscreen();
+  game_animscreen();
+}
+
+#if 1
 
+void GameActions_EM(byte action[MAX_PLAYERS])
+{
+  static unsigned long game_frame_delay = 0;
 #if 1
-    prepare_em_level();
+  unsigned long game_frame_delay_value = getGameFrameDelay_EM(20);
 #else
-    if (!LoadNativeLevel_EM(filename))
-      return 1;
+  unsigned long game_frame_delay_value = getGameFrameDelay_EM(25);
 #endif
+  int i;
 
 #if 0
-  }
-  else /* !!! SOON OBSOLETE !!! */
-  {
-    char name[MAXNAME+2];
+  /* this is done in screens.c/HandleGameActions() by calling BackToFront() */
+  XSync(display, False);       /* block until all graphics are drawn */
+#endif
 
-    name[MAXNAME] = 0;
-    snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
+  WaitUntilDelayReached(&game_frame_delay, game_frame_delay_value);
 
-    if (name[MAXNAME])
-      snprintf_overflow("read a level in cave/");
+  game_animscreen();
 
-    if (cave_convert(name) != 0)
-      return 1;
-  }
-#endif
+  RandomEM = RandomEM * 129 + 1;
 
-  game_initscreen();
-  game_blitscore();
-  game_animscreen();
+  frame = (frame - 1) & 7;
 
-  return 0;
-}
+  for (i = 0; i < MAX_PLAYERS; i++)
+    readjoy(action[i], &ply[i]);
 
-int game_menu_loop(boolean init, byte action)
-{
-  static int temp = -1;
-  static int pos = -1;
+  UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
 
-  if (init)
+  if (frame == 7)
   {
-    temp = 1;
-    pos = 4;
-
-    return 0;
+    synchro_1();
+    synchro_2();
   }
 
-  input_eventloop();
-
-  if (input_die)
-    return 1;
-
-  if (input_refresh)
-    blitscreen();
-
-  if (!input_pause)
+  if (frame == 6)
   {
-    title_blitscore();
-    title_animscreen();
-
-    ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
-
-    readjoy(action);
-
-    if (temp == 0)
-    {
-      if (ply1.joy_fire)
-       return 2;
-
-      if (ply1.joy_e && player_level < 99)
-      {
-       player_level++;
-       drawmenu(2);
-      }
-
-      if (ply1.joy_w && player_level > 0)
-      {
-       player_level--;
-       drawmenu(2);
-      }
-
-      if (ply1.joy_n && pos > 0)
-      {
-       drawmenu(pos);
-       pos--;
-      }
-
-      if (ply1.joy_s && pos < 4)
-      {
-       drawmenu(pos);
-       pos++;
-      }
-    }
-
-    temp = (ply1.joy_n || ply1.joy_e || ply1.joy_s || ply1.joy_w ||
-           ply1.joy_fire);
+    synchro_3();
+    sound_play();
+
+    if (game_frame_delay_value > 0)    /* do not redraw values in warp mode */
+      DrawGameDoorValues_EM();
   }
 
-  return 0;
+#if 0
+  if (lev.time_initial == 0)
+    lev.time++;
+  else if (lev.time > 0)
+    lev.time--;
+#endif
+
+#if 0
+  if (lev.time_initial > 0 &&
+      lev.time > 0 && lev.time <= 50 && lev.time % 5 == 0 && setup.time_limit)
+    play_sound(-1, -1, SAMPLE_time);
+#endif
 }
 
-int game_play_loop(byte action)
+#else
+
+void GameActions_EM(byte action)
 {
-  input_eventloop();
+  static unsigned long game_frame_delay = 0;
+#if 1
+  unsigned long game_frame_delay_value = getGameFrameDelay_EM(20);
+#else
+  unsigned long game_frame_delay_value = getGameFrameDelay_EM(25);
+#endif
 
-  if (input_die || input_esc)
-    return 1;
+#if 0
+  /* this is done in screens.c/HandleGameActions() by calling BackToFront() */
+  XSync(display, False);       /* block until all graphics are drawn */
+#endif
 
-  if (input_refresh)
-    blitscreen();
+  WaitUntilDelayReached(&game_frame_delay, game_frame_delay_value);
 
-  if (!input_pause)
-  {
-    game_animscreen();
+  game_animscreen();
 
-    frame = (frame - 1) & 7;
+  RandomEM = RandomEM * 129 + 1;
 
-    readjoy(action);
+  frame = (frame - 1) & 7;
 
-#if 1
-    if (input_esc)
-      return 1;
-#endif
+  readjoy(action);
+
+  UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
 
-    if (frame == 7)
-    {
-      synchro_1();
-      synchro_2();
-    }
-
-    if (frame == 6)
-    {
-      synchro_3();
-      sound_play();
-      game_blitscore();
-    }
+  if (frame == 7)
+  {
+    synchro_1();
+    synchro_2();
   }
 
-  return 0;
-}
+  if (frame == 6)
+  {
+    synchro_3();
+    sound_play();
 
-void game_menu_init(void)
-{
-  int i;
+    if (game_frame_delay_value > 0)    /* do not redraw values in warp mode */
+      DrawGameDoorValues_EM();
+  }
 
-  title_initscreen();
+#if 0
+  if (lev.time_initial == 0)
+    lev.time++;
+  else if (lev.time > 0)
+    lev.time--;
+#endif
 
-  for (i = 0; i < 7; i++)
-    drawmenu(i);                       /* display all lines */
+#if 0
+  if (lev.time_initial > 0 &&
+      lev.time > 0 && lev.time <= 50 && lev.time % 5 == 0 && setup.time_limit)
+    play_sound(-1, -1, SAMPLE_time);
+#endif
+}
 
-  title_blitscore();
-  title_animscreen();
+#endif
 
-  game_menu_loop(1, 0);
-}
 
-int game_loop(byte action)
+/* read input device for players */
+
+#if 1
+
+void readjoy(byte action, struct PLAYER *ply)
 {
-#if 0
-  printf("::: action == 0x%02x\n", action);
-#endif
+  unsigned int north = 0, east = 0, south = 0, west = 0;
+  unsigned int snap = 0, drop = 0;
 
-  if (em_game_status == EM_GAME_STATUS_MENU)
-  {
-    int result = game_menu_loop(0, action);
+  if (action & JOY_LEFT)
+    west = 1;
+
+  if (action & JOY_RIGHT)
+    east = 1;
 
-    if (result == 1)
-    {
-      /* exit program */
+  if (action & JOY_UP)
+    north = 1;
 
-      return 1;
-    }
+  if (action & JOY_DOWN)
+    south = 1;
 
-    if (result == 2)
-    {
-      /* start playing */
+  if (action & JOY_BUTTON_1)
+    snap = 1;
 
-      em_game_status = EM_GAME_STATUS_PLAY;
-      if (game_play_init(player_level, NULL) != 0)
-       em_game_status = EM_GAME_STATUS_MENU;
-    }
-  }
-  else if (em_game_status == EM_GAME_STATUS_PLAY)
-  {
-    if (game_play_loop(action) != 0)
-    {
-      /* stop playing */
+  if (action & JOY_BUTTON_2)
+    drop = 1;
 
-      if (skip_menu)
-       return 1;
+  ply->joy_snap = snap;
+  ply->joy_drop = drop;
 
-      em_game_status = EM_GAME_STATUS_MENU;
-      game_menu_init();
-    }
+  if (ply->joy_stick || (north | east | south | west))
+  {
+    ply->joy_n = north;
+    ply->joy_e = east;
+    ply->joy_s = south;
+    ply->joy_w = west;
   }
-
-  return 0;
 }
 
-
-/* read input device for players */
+#else
 
 void readjoy(byte action)
 {
-  unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
+  unsigned int north = 0, east = 0, south = 0, west = 0;
+  unsigned int snap = 0, drop = 0;
 
   if (action & JOY_LEFT)
     west = 1;
@@ -375,14 +244,14 @@ void readjoy(byte action)
     south = 1;
 
   if (action & JOY_BUTTON_1)
-    fire = 1;
+    snap = 1;
 
-#if 0
   if (action & JOY_BUTTON_2)
-    input_esc = 1;
-#endif
+    drop = 1;
 
-  ply1.joy_fire = fire;
+#if 1
+  ply1.joy_snap = snap;
+  ply1.joy_drop = drop;
   if (ply1.joy_stick || (north | east | south | west))
   {
     ply1.joy_n = north;
@@ -390,42 +259,19 @@ void readjoy(byte action)
     ply1.joy_s = south;
     ply1.joy_w = west;
   }
-}
-
 
-/* handle events from x windows and block until the next frame */
-
-void input_eventloop(void)
-{
-  unsigned long count;
+#else
 
-  if (!input_pause)
+  ply2.joy_snap = snap;
+  ply2.joy_drop = drop;
+  if (ply2.joy_stick || (north | east | south | west))
   {
-    XSync(display, False); /* block until all graphics are drawn */
-
-    if (gettimeofday(&tv2, 0) == -1)
-      tv2.tv_usec = 0;
-
-    count = tv2.tv_usec + 1000000 - tv1.tv_usec;
-    if (count >= 1000000)
-      count -= 1000000;
-
-    tv1.tv_usec = tv2.tv_usec;
-    if (count < 25000)
-    {
-      tv2.tv_sec = 0;
-      tv2.tv_usec = 25000 - count;
-#if 1
-      select(0, 0, 0, 0, &tv2); /* sleep a bit */
-#else
-      usleep(tv2.tv_usec);
-#endif
-    }
+    ply2.joy_n = north;
+    ply2.joy_e = east;
+    ply2.joy_s = south;
+    ply2.joy_w = west;
   }
-
-  input_esc = 0;
-  input_die = 0;
-  input_refresh = 0;
+#endif
 }
 
 #endif