rnd-20030404-4-src
[rocksndiamonds.git] / src / libgame / msdos.c
index cc8aedd7a6ab71af5c44b74e907e8fbfe8abbb6e..ec19ef6389c7d639c99f9106ef17826ff53de950 100644 (file)
@@ -1,21 +1,29 @@
 /***********************************************************
-*  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
+* Artsoft Retro-Game Library                               *
 *----------------------------------------------------------*
-*  ©1995 Artsoft Development                               *
-*        Holger Schemel                                    *
-*        33659 Bielefeld-Senne                             *
-*        Telefon: (0521) 493245                            *
-*        eMail: aeglos@valinor.owl.de                      *
-*               aeglos@uni-paderborn.de                    *
-*               q99492@pbhrzx.uni-paderborn.de             *
+* (c) 1994-2002 Artsoft Entertainment                      *
+*               Holger Schemel                             *
+*               Detmolder Strasse 189                      *
+*               33604 Bielefeld                            *
+*               Germany                                    *
+*               e-mail: info@artsoft.org                   *
 *----------------------------------------------------------*
-*  msdos.c                                                 *
+* msdos.c                                                  *
 ***********************************************************/
 
-#include "libgame.h"
+#include "system.h"
+
 
 #if defined(PLATFORM_MSDOS)
 
+#include "sound.h"
+#include "joystick.h"
+#include "misc.h"
+#include "setup.h"
+#include "pcx.h"
+
+#define AllegroDefaultScreen() (display->screens[display->default_screen])
+
 /* allegro driver declarations */
 DECLARE_GFX_DRIVER_LIST(GFX_DRIVER_VBEAF GFX_DRIVER_VESA2L GFX_DRIVER_VESA1)
 DECLARE_COLOR_DEPTH_LIST(COLOR_DEPTH_8)
@@ -46,12 +54,6 @@ static int global_colormap_entries_used = 0;
 
 boolean wait_for_vsync;
 
-/*
-extern int playing_sounds;
-extern struct SoundControl playlist[MAX_SOUNDS_PLAYING];
-extern struct SoundControl emptySoundControl;
-*/
-
 static BITMAP *Read_PCX_to_AllegroBitmap(char *);
 
 static void allegro_init_drivers()
@@ -84,7 +86,7 @@ static void allegro_init_drivers()
 
 static boolean allegro_init_audio()
 {
-  reserve_voices(MAX_SOUNDS_PLAYING, 0);
+  reserve_voices(NUM_MIXER_CHANNELS, 0);
 
   if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL) == -1)
     if (install_sound(DIGI_SB, MIDI_NONE, NULL) == -1)
@@ -232,25 +234,7 @@ static KeySym ScancodeToKeySym(byte scancode)
   }
 }
 
-void XMapWindow(Display *display, Window window)
-{
-  int x, y;
-  unsigned int width, height;
-  boolean mouse_off;
-
-  x = display->screens[display->default_screen].x;
-  y = display->screens[display->default_screen].y;
-  width = display->screens[display->default_screen].width;
-  height = display->screens[display->default_screen].height;
-
-  mouse_off = hide_mouse(display, x, y, width, height);
-  blit((BITMAP *)window, video_bitmap, 0, 0, x, y, width, height);
-
-  if (mouse_off)
-    unhide_mouse(display);
-}
-
-static unsigned long AllocColorCell(int r, int g, int b)
+Pixel AllegroAllocColorCell(int r, int g, int b)
 {
   byte pixel_mapping = 0;
   int i;
@@ -276,30 +260,46 @@ static unsigned long AllocColorCell(int r, int g, int b)
     if (global_colormap_entries_used < MAX_COLORS)
       global_colormap_entries_used++;
 
+    i = global_colormap_entries_used - 1;
+
     global_colormap[i].r = r;
     global_colormap[i].g = g;
     global_colormap[i].b = b;
 
+    set_palette(global_colormap);
+
     pixel_mapping = i;
   }
 
   return pixel_mapping;
 }
 
+void XMapWindow(Display *display, Window window)
+{
+  int x, y;
+  unsigned int width, height;
+  boolean mouse_off;
+
+  x = AllegroDefaultScreen().x;
+  y = AllegroDefaultScreen().y;
+  width = AllegroDefaultScreen().width;
+  height = AllegroDefaultScreen().height;
+
+  mouse_off = hide_mouse(display, x, y, width, height);
+  blit((BITMAP *)window, video_bitmap, 0, 0, x, y, width, height);
+
+  if (mouse_off)
+    unhide_mouse(display);
+}
+
 Display *XOpenDisplay(char *display_name)
 {
   Screen *screen;
   Display *display;
   BITMAP *mouse_bitmap = NULL;
-  char *filename;
+  char *mouse_filename = getCustomImageFilename(program.msdos_cursor_filename);
 
-  filename = getPath3(options.ro_base_directory, GRAPHICS_DIRECTORY,
-                     MOUSE_FILENAME);
-
-  mouse_bitmap = Read_PCX_to_AllegroBitmap(filename);
-  free(filename);
-
-  if (mouse_bitmap == NULL)
+  if ((mouse_bitmap = Read_PCX_to_AllegroBitmap(mouse_filename)) == NULL)
     return NULL;
 
   screen = malloc(sizeof(Screen));
@@ -307,13 +307,8 @@ Display *XOpenDisplay(char *display_name)
 
   screen[0].cmap = 0;
   screen[0].root = 0;
-#if 0
-  screen[0].white_pixel = 0xFF;
-  screen[0].black_pixel = 0x00;
-#else
-  screen[0].white_pixel = AllocColorCell(0xFFFF, 0xFFFF, 0xFFFF);
-  screen[0].black_pixel = AllocColorCell(0x0000, 0x0000, 0x0000);
-#endif
+  screen[0].white_pixel = AllegroAllocColorCell(0xFFFF, 0xFFFF, 0xFFFF);
+  screen[0].black_pixel = AllegroAllocColorCell(0x0000, 0x0000, 0x0000);
   screen[0].video_bitmap = NULL;
 
   display->default_screen = 0;
@@ -340,11 +335,11 @@ Window XCreateSimpleWindow(Display *display, Window parent, int x, int y,
   video_bitmap = create_video_bitmap(XRES, YRES);
   clear_to_color(video_bitmap, background);
 
-  display->screens[display->default_screen].video_bitmap = video_bitmap;
-  display->screens[display->default_screen].x = x;
-  display->screens[display->default_screen].y = y;
-  display->screens[display->default_screen].width = XRES;
-  display->screens[display->default_screen].height = YRES;
+  AllegroDefaultScreen().video_bitmap = video_bitmap;
+  AllegroDefaultScreen().x = x;
+  AllegroDefaultScreen().y = y;
+  AllegroDefaultScreen().width = XRES;
+  AllegroDefaultScreen().height = YRES;
 
   set_mouse_sprite(display->mouse_ptr);
 
@@ -353,10 +348,10 @@ Window XCreateSimpleWindow(Display *display, Window parent, int x, int y,
 #endif
 
   set_mouse_speed(1, 1);
-  set_mouse_range(display->screens[display->default_screen].x + 1,
-                 display->screens[display->default_screen].y + 1,
-                 display->screens[display->default_screen].x + WIN_XSIZE + 1,
-                 display->screens[display->default_screen].y + WIN_YSIZE + 1);
+  set_mouse_range(AllegroDefaultScreen().x + 1,
+                 AllegroDefaultScreen().y + 1,
+                 AllegroDefaultScreen().x + video.width + 1,
+                 AllegroDefaultScreen().y + video.height + 1);
 
   show_video_bitmap(video_bitmap);
 
@@ -425,8 +420,8 @@ void XFillRectangle(Display *display, Drawable d, GC gc, int x, int y,
 
   if ((BITMAP *)d == video_bitmap)
   {
-    x += display->screens[display->default_screen].x;
-    y += display->screens[display->default_screen].y;
+    x += AllegroDefaultScreen().x;
+    y += AllegroDefaultScreen().y;
     freeze_mouse_flag = TRUE;
     mouse_off = hide_mouse(display, x, y, width, height);
   }
@@ -446,7 +441,7 @@ Pixmap XCreatePixmap(Display *display, Drawable d, unsigned int width,
   BITMAP *bitmap = NULL;
 
   if (gfx_capabilities & GFX_HW_VRAM_BLIT &&
-      width == FXSIZE && height == FYSIZE)
+      width  == gfx.scrollbuffer_width && height == gfx.scrollbuffer_height)
     bitmap = create_video_bitmap(width, height);
 
   if (bitmap == NULL)
@@ -469,14 +464,14 @@ inline void XCopyArea(Display *display, Drawable src, Drawable dest, GC gc,
 
   if ((BITMAP *)src == video_bitmap)
   {
-    src_x += display->screens[display->default_screen].x;
-    src_y += display->screens[display->default_screen].y;
+    src_x += AllegroDefaultScreen().x;
+    src_y += AllegroDefaultScreen().y;
   }
 
   if ((BITMAP *)dest == video_bitmap)
   {
-    dest_x += display->screens[display->default_screen].x;
-    dest_y += display->screens[display->default_screen].y;
+    dest_x += AllegroDefaultScreen().x;
+    dest_y += AllegroDefaultScreen().y;
     freeze_mouse_flag = TRUE;
     mouse_off = hide_mouse(display, dest_x, dest_y, width, height);
   }
@@ -506,12 +501,10 @@ static BITMAP *Image_to_AllegroBitmap(Image *image)
   byte *src_ptr = image->data;
   byte pixel_mapping[MAX_COLORS];
   unsigned int depth = 8;
-
-#if 0
-  int i, j, x, y;
-#else
   int i, x, y;
-#endif
+
+  if (image->type == IMAGETYPE_TRUECOLOR && depth == 8)
+    Error(ERR_EXIT, "cannot handle true-color images on 8-bit display");
 
   /* allocate new allegro bitmap structure */
   if ((bitmap = create_bitmap_ex(depth, image->width, image->height)) == NULL)
@@ -525,48 +518,12 @@ static BITMAP *Image_to_AllegroBitmap(Image *image)
   /* try to use existing colors from the global colormap */
   for (i=0; i<MAX_COLORS; i++)
   {
-
-#if 0
-    int r, g, b;
-#endif
-
     if (!image->rgb.color_used[i])
       continue;
 
-
-#if 0
-    r = image->rgb.red[i] >> 10;
-    g = image->rgb.green[i] >> 10;
-    b = image->rgb.blue[i] >> 10;
-
-    for (j=0; j<global_colormap_entries_used; j++)
-    {
-      if (r == global_colormap[j].r &&
-         g == global_colormap[j].g &&
-         b == global_colormap[j].b)            /* color found */
-      {
-       pixel_mapping[i] = j;
-       break;
-      }
-    }
-
-    if (j == global_colormap_entries_used)     /* color not found */
-    {
-      if (global_colormap_entries_used < MAX_COLORS)
-       global_colormap_entries_used++;
-
-      global_colormap[j].r = r;
-      global_colormap[j].g = g;
-      global_colormap[j].b = b;
-
-      pixel_mapping[i] = j;
-    }
-#else
-    pixel_mapping[i] = AllocColorCell(image->rgb.red[i],
-                                     image->rgb.green[i],
-                                     image->rgb.blue[i]);
-#endif
-
+    pixel_mapping[i] = AllegroAllocColorCell(image->rgb.red[i],
+                                            image->rgb.green[i],
+                                            image->rgb.blue[i]);
   }
 
   /* copy bitmap data */
@@ -604,7 +561,10 @@ int Read_PCX_to_Pixmap(Display *display, Window window, GC gc, char *filename,
     return errno_pcx;
 
   *pixmap = (Pixmap)bitmap;
-  *pixmap_mask = (Pixmap)bitmap;
+
+  /* pixmap_mask will never be used in Allegro (which uses masked_blit()),
+     so use non-NULL dummy pointer to empty Pixmap */
+  *pixmap_mask = (Pixmap)DUMMY_MASK;
 
   return PCX_Success;
 }
@@ -645,6 +605,10 @@ void XFreeGC(Display *display, GC gc)
     free(gcv);
 }
 
+void XUnmapWindow(Display *display, Window window)
+{
+}
+
 void XCloseDisplay(Display *display)
 {
   BITMAP *bitmap = video_bitmap;
@@ -812,8 +776,8 @@ int XPending(Display *display)
     pending_events++;
     xmotion = (XMotionEvent *)&event_buffer[pending_events];
     xmotion->type = MotionNotify;
-    xmotion->x = mouse_x - display->screens[display->default_screen].x;
-    xmotion->y = mouse_y - display->screens[display->default_screen].y;
+    xmotion->x = mouse_x - AllegroDefaultScreen().x;
+    xmotion->y = mouse_y - AllegroDefaultScreen().y;
   }
 
   /* mouse button event */
@@ -831,8 +795,8 @@ int XPending(Display *display)
         xbutton = (XButtonEvent *)&event_buffer[pending_events];
         xbutton->type = (mouse_b & bitmask ? ButtonPress : ButtonRelease);
         xbutton->button = mapping[i];
-       xbutton->x = mouse_x - display->screens[display->default_screen].x;
-       xbutton->y = mouse_y - display->screens[display->default_screen].y;
+       xbutton->x = mouse_x - AllegroDefaultScreen().x;
+       xbutton->y = mouse_y - AllegroDefaultScreen().y;
       }
     }
     last_mouse_b = mouse_b;
@@ -868,10 +832,10 @@ void XDrawLine(Display *display, Drawable d, GC gc,
 
   if ((BITMAP *)d == video_bitmap)
   {
-    x1 += display->screens[display->default_screen].x;
-    y1 += display->screens[display->default_screen].y;
-    x2 += display->screens[display->default_screen].x;
-    y2 += display->screens[display->default_screen].y;
+    x1 += AllegroDefaultScreen().x;
+    y1 += AllegroDefaultScreen().y;
+    x2 += AllegroDefaultScreen().x;
+    y2 += AllegroDefaultScreen().y;
     freeze_mouse_flag = TRUE;
     mouse_off = hide_mouse(display, MIN(x1, x2), MIN(y1, y2),
                           MAX(x1, x2) - MIN(x1, x2),
@@ -890,12 +854,16 @@ void XDestroyImage(XImage *ximage)
 {
 }
 
+void XDestroyWindow(Display *display, Window window)
+{
+}
+
 Bool XQueryPointer(Display *display, Window window,
                   Window *root, Window *child, int *root_x, int *root_y,
                   int *win_x, int *win_y, unsigned int *mask)
 {
-  *win_x = mouse_x - display->screens[display->default_screen].x;
-  *win_y = mouse_y - display->screens[display->default_screen].y;
+  *win_x = mouse_x - AllegroDefaultScreen().x;
+  *win_y = mouse_y - AllegroDefaultScreen().y;
 
   return True;
 }
@@ -911,12 +879,70 @@ void XAutoRepeatOff(Display *display)
   keyboard_auto_repeat = FALSE;
 }
 
-boolean MSDOSOpenAudio(void)
+void AllegroDrawLine(Drawable d, int from_x, int from_y, int to_x, int to_y,
+                    Pixel color)
+{
+  boolean mouse_off = FALSE;
+
+  if ((BITMAP *)d == video_bitmap)
+  {
+    int dx = AllegroDefaultScreen().x;
+    int dy = AllegroDefaultScreen().y;
+    int x1, y1, x2, y2;
+
+    from_x += dx;
+    from_y += dy;
+    to_x += dx;
+    to_y += dy;
+
+    x1 = (from_x < to_x ? from_x : to_x);
+    y1 = (from_y < to_y ? from_y : to_y);
+    x2 = (from_x < to_x ? to_x : from_x);
+    y2 = (from_y < to_y ? to_y : from_y);
+
+    freeze_mouse_flag = TRUE;
+    mouse_off = hide_mouse(display, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
+  }
+
+  line((BITMAP *)d, from_x, from_y, to_x, to_y, color);
+
+  if (mouse_off)
+    unhide_mouse(display);
+
+  freeze_mouse_flag = FALSE;
+}
+
+Pixel AllegroGetPixel(Drawable d, int x, int y)
 {
-  return allegro_init_audio();
+  return getpixel((BITMAP *)d, x, y);
 }
 
-boolean MSDOSCloseAudio(void)
+void AllegroZoomBitmap(Drawable src, Drawable dst,
+                      int src_width, int src_height,
+                      int dst_width, int dst_height)
+{
+  stretch_blit((BITMAP *)src, (BITMAP *)dst,
+              0, 0, src_width, src_height, 0, 0, dst_width, dst_height);
+}
+
+void MSDOSOpenAudio(void)
+{
+  if (allegro_init_audio())
+  {
+    audio.sound_available = TRUE;
+    audio.music_available = TRUE;
+    audio.loops_available = TRUE;
+    audio.sound_enabled = TRUE;
+
+    audio.num_channels = NUM_MIXER_CHANNELS;
+    audio.music_channel = MUSIC_CHANNEL;
+    audio.first_sound_channel = FIRST_SOUND_CHANNEL;
+
+    Mixer_InitChannels();
+  }
+}
+
+void MSDOSCloseAudio(void)
 {
   /* nothing to be done here */
 }
@@ -926,4 +952,57 @@ void NetworkServer(int port, int serveronly)
   Error(ERR_WARN, "networking not supported in DOS version");
 }
 
+
+/* ========================================================================= */
+/* joystick functions                                                        */
+/* ========================================================================= */
+
+void MSDOSInitJoysticks()
+{
+  int i;
+
+  /* start from scratch */
+  remove_joystick();
+
+  /* try to access two joysticks; if that fails, try to access just one */
+  if (install_joystick(JOY_TYPE_2PADS) == 0 ||
+      install_joystick(JOY_TYPE_AUTODETECT) == 0)
+    joystick.status = JOYSTICK_ACTIVATED;
+
+  for (i=0; i<MAX_PLAYERS; i++)
+  {
+    char *device_name = setup.input[i].joy.device_name;
+    int joystick_nr = getJoystickNrFromDeviceName(device_name);
+
+    if (joystick_nr >= num_joysticks)
+      joystick_nr = -1;
+
+    /* misuse joystick file descriptor variable to store joystick number */
+    joystick.fd[i] = joystick_nr;
+  }
+}
+
+boolean MSDOSReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
+{
+  /* the allegro global variable 'num_joysticks' contains the number
+     of joysticks found at initialization under MS-DOS / Windows */
+
+  if (nr < 0 || nr >= num_joysticks)
+    return FALSE;
+
+  poll_joystick();
+
+  if (x != NULL)
+    *x = joy[nr].stick[0].axis[0].pos;
+  if (y != NULL)
+    *y = joy[nr].stick[0].axis[1].pos;
+
+  if (b1 != NULL)
+    *b1 = joy[nr].button[0].b;
+  if (b2 != NULL)
+    *b2 = joy[nr].button[1].b;
+
+  return TRUE;
+}
+
 #endif /* PLATFORM_MSDOS */