win_vid.c

来自「经典的老游戏digger的源代码.提起digger相信很多人会回忆起曾经为了它挑」· C语言 代码 · 共 1,254 行 · 第 1/3 页

C
1,254
字号
  ddsd.dwWidth=640;
  ddsd.dwFlags=DDSD_WIDTH;
  hRet=IDirectDraw_EnumDisplayModes(g_pDD, (DWORD) NULL, /*NULL*/ &ddsd, NULL, &EnumModesCallback);
}

void attach_clipper()
{
  LPDIRECTDRAWCLIPPER pClipper;
  HRESULT hRet;

  /* add a clipper to the primary surface */
  hRet = IDirectDraw_CreateClipper(g_pDD, 0, &pClipper, NULL);
  if (hRet != DD_OK)
    fatal_error(hRet, "attach_clipper(): CreateClipper FAILED");
  hRet = IDirectDrawClipper_SetHWnd(pClipper, 0, hWnd);
  if (hRet != DD_OK)
    fatal_error(hRet, "attach_clipper(): SetHWnd FAILED");
  hRet = IDirectDrawSurface_SetClipper(g_pDDSPrimary, pClipper);
  if (hRet != DD_OK)
    fatal_error(hRet, "attach_clipper(): SetClipper FAILED");
  IDirectDrawClipper_Release(pClipper);
  pClipper = NULL;
}

void release_clipper()
{
  HRESULT hRet;
  hRet = IDirectDrawSurface_SetClipper(g_pDDSPrimary, (LPDIRECTDRAWCLIPPER) NULL);
  if (hRet != DD_OK)
    fatal_error(hRet, "release_clipper(): SetClipper FAILED");
}

void vgaputim2(Sint4 x,Sint4 y,Sint4 ch,Sint4 w,Sint4 h)
{
/* TO DO: convert the vgagraphics to a more appropriate format,
          rewrite this routine(load the sprites onto the DirectDraw
          Surface beforehand). */

  Uint5 scrn_width;
  Uint5 scrn_height;
  int y_loop_end;
  int y_loop_count;
  int x_loop_end;
  int x_loop_count;
  unsigned char* cur_src_mask_ptr;
  unsigned char* cur_src_ptr;
  unsigned char* cur_dest_ptr;
  unsigned char* scrn_max_ptr;
  Uint5 i;
  Uint5 plane;
  int color;
  Uint5 dest_next_row_offset;
  Uint5 src_plane_offset;
  RECT rect;
  DDSURFACEDESC ddsd;
  DDBLTFX ddbltfx;
  HRESULT hRet;

  scrn_width=640;
  scrn_height=400;
  scrn_max_ptr = back_bitmap_bits + (Uint5) scrn_width*scrn_height;

  cur_src_mask_ptr=vgatable[ch*2+1];
  cur_src_ptr=vgatable[ch*2];
  cur_dest_ptr=&(back_bitmap_bits[(Uint5) (y*2l * scrn_width + x*2l)]);
  dest_next_row_offset = scrn_width - w*8l;

  src_plane_offset = w*h*2;

  y_loop_end=h*2;
  x_loop_end=w*8;

  if (ch>111) return;

  for (y_loop_count=0;y_loop_count<y_loop_end;y_loop_count++)
  {
    for (x_loop_count=0;x_loop_count<x_loop_end;x_loop_count++)
    {
      color=*(sprite_array+ch*1024/2+y_loop_count*32+x_loop_count);
      if (cur_dest_ptr < scrn_max_ptr)
        *cur_dest_ptr = color;
      cur_dest_ptr++;
    }
    cur_dest_ptr+=dest_next_row_offset;
  }

  if (y*2 + h*2 < 400)
    blit_rect_to_window(x*2, y*2, w*8, h*2);
  else
    blit_rect_to_window(x*2, y*2, w*8, h*2 - (y*2 + h*2) + 400);
}

void vgaputim(Sint4 x,Sint4 y,Sint4 ch,Sint4 w,Sint4 h)
{
/* TO DO: convert the vgagraphics to a more appropriate format,
          rewrite this routine(load the sprites onto the DirectDraw
          Surface beforehand). */

  Uint5 scrn_width;
  Uint5 scrn_height;
  int y_loop_end;
  int y_loop_count;
  int x_loop_end;
  int x_loop_count;
  unsigned char* cur_src_mask_ptr;
  unsigned char* cur_src_ptr;
  unsigned char* cur_dest_ptr;
  unsigned char* scrn_max_ptr;
  Uint5 i;
  Uint5 plane;
  int color;
  Uint5 dest_next_row_offset;
  Uint5 src_plane_offset;
  RECT rect;
  DDSURFACEDESC ddsd;
  DDBLTFX ddbltfx;
  HRESULT hRet;

  scrn_width=640;
  scrn_height=400;
  scrn_max_ptr = back_bitmap_bits + (Uint5) scrn_width*scrn_height;

  cur_src_mask_ptr=vgatable[ch*2+1];
  cur_src_ptr=vgatable[ch*2];
  cur_dest_ptr=&(back_bitmap_bits[(Uint5) (y*2l * scrn_width + x*2l)]);
  dest_next_row_offset = scrn_width - w*8l;

  src_plane_offset = w*h*2;

  y_loop_end=h*2;
  x_loop_end=w;

  for (y_loop_count=0;y_loop_count<y_loop_end;y_loop_count++)
  {
    for (x_loop_count=0;x_loop_count<x_loop_end;x_loop_count++)
    {
      for (i=0;i<8;i++)
      {
        if (!((*cur_src_mask_ptr)&(0x80>>i)))
        {
          color=0;
          for (plane=0;plane<4;plane++)
          {
            color|=((((*(cur_src_ptr + (Uint5) (plane*src_plane_offset))) << i) & 0x80 ) >> (4 + plane));
          }
          if (cur_dest_ptr < scrn_max_ptr)
            *cur_dest_ptr = color;

        }
        cur_dest_ptr++;
      }
      cur_src_ptr++;
      cur_src_mask_ptr++;
    }
    cur_dest_ptr+=dest_next_row_offset;
  }

  if (y*2 + h*2 < 400)
    blit_rect_to_window(x*2, y*2, w*8, h*2);
  else
    blit_rect_to_window(x*2, y*2, w*8, h*2 - (y*2 + h*2) + 400);
}

void vgaputi(Sint4 x,Sint4 y,Uint3 *p,Sint4 w,Sint4 h)
{
  Uint5 i;
  for (i=0;i<h*2;i++)
    if (i+y*2 < 400)
      farmemcpy(back_bitmap_bits+(Uint5) ((y*2+i)*640l + x*2), (char far*) (p + (Uint5) (i*w*8l)) , w*8l);
  if (y*2 + h*2 < 400)
    blit_rect_to_window(x*2, y*2, w*8, h*2);
  else
    blit_rect_to_window(x*2, y*2, w*8, h*2 - (y*2 + h*2) + 400);
}

void vgageti(Sint4 x,Sint4 y,Uint3 *p,Sint4 w,Sint4 h)
{
  Uint5 i;
  for (i=0;i<h*2;i++)
    if (i+y*2 < 400)
      farmemcpy( (char far*) (p + (Uint5) i*w*8l), back_bitmap_bits+ (Uint5) ((y*2l+i)*640l + x*2l) , w*8);
}

void cgaputim(Sint4 x,Sint4 y,Sint4 ch,Sint4 w,Sint4 h)
{
/* TO DO: convert the cgagraphics to a more appropriate format,
          rewrite this routine(load the sprites onto the DirectDraw
          Surface beforehand). */

  Uint5 scrn_width;
  Uint5 scrn_height;
  int y_loop_end;
  int y_loop_count;
  int x_loop_end;
  int x_loop_count;
  unsigned char* cur_src_mask_ptr;
  unsigned char* cur_src_ptr;
  unsigned char* cur_dest_ptr;
  unsigned char* scrn_max_ptr;
  Uint5 i;
  int color;
  Uint5 dest_next_row_offset;
  RECT rect;
  DDSURFACEDESC ddsd;
  DDBLTFX ddbltfx;
  HRESULT hRet;

  scrn_width=640;
  scrn_height=400;
  scrn_max_ptr = back_bitmap_bits + (Uint5) scrn_width*scrn_height;

  cur_src_mask_ptr=cgatable[ch*2+1];
  cur_src_ptr=cgatable[ch*2];
  cur_dest_ptr=&(back_bitmap_bits[(Uint5) (y*2 * scrn_width + x*2)]);
  dest_next_row_offset = scrn_width; // - w*4l;

  y_loop_end=h;
  x_loop_end=w;

  for (y_loop_count=0;y_loop_count<y_loop_end;y_loop_count++)
  {
    for (x_loop_count=0;x_loop_count<x_loop_end;x_loop_count++)
    {
      for (i=0;i<4;i++)
      {
        if (!( (*cur_src_mask_ptr)&(0xC0>>(i*2)) ))
        {
          color=((*cur_src_ptr)>>(6-(i*2)))&0x03;
          if (cur_dest_ptr < scrn_max_ptr)
            *cur_dest_ptr = *(cur_dest_ptr+1) = *(cur_dest_ptr+dest_next_row_offset) = *(cur_dest_ptr+dest_next_row_offset+1) = color;
        }
        cur_dest_ptr+=2;
      }
      cur_src_ptr++;
      cur_src_mask_ptr++;
    }
    cur_dest_ptr+=(dest_next_row_offset*2-w*8);
  }
  if (y*2 + h*2 < 400)
    blit_rect_to_window(x*2, y*2, w*8, h*2);
  else
    blit_rect_to_window(x*2, y*2, w*8, h*2 - (y*2 + h*2) + 400);
}

void cgageti(Sint4 x,Sint4 y,Uint3 *p,Sint4 w,Sint4 h)
{
  vgageti(x,y,p,w,h);
}

void cgaputi(Sint4 x,Sint4 y,Uint3 *p,Sint4 w,Sint4 h)
{
  vgaputi(x,y,p,w,h);
}

Sint4 cgagetpix(Sint4 x,Sint4 y)
{
  Uint4 xi,yi;
  Sint4 rval;

  rval=0;
  if (x>319||y>199)
  {
    return 0xff;
  }
  for (yi=0;yi<2;yi++)
    for (xi=0;xi<8;xi++)
      if (back_bitmap_bits[(Uint5) ((y*2l+yi)*640l + x*2l + xi)])
        rval |= 0x80 >> xi;

  rval &= 0xee;
  return rval;
}

/*******************************************************/
/* Functions for displaying the CGA data               */
/*******************************************************/
void cgainit(void)
{
  video_mode=VIDEO_MODE_CGA;
  windowed_palette[0] = cga16_pal1_rgbq;
  windowed_palette[1] = cga16_pal1i_rgbq;
  windowed_palette[2] = cga16_pal2_rgbq;
  windowed_palette[3] = cga16_pal2i_rgbq;
  destroy_palettes();
  init_palettes();
}

void cgaclear(void)
{
  vgaclear();
}

void cgapal(Sint4 pal)
{
  vgapal(pal);
}

void cgainten(Sint4 inten)
{
  vgainten(inten);
}

void load_title_bitmaps()
{
  title_bitmap[0]=LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_TITLEBITMAP));
  title_bitmap[1]=LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_CGATITLEBITMAP));
}

void change_graphics_routines(Uint3 newmode)
{
  g_bReady=FALSE;
  ginit=cgainit;
  switch (newmode)
  {
  case VIDEO_MODE_CGA:
    gpal=cgapal;
    ginten=cgainten;
    gclear=cgaclear;
    ggetpix=cgagetpix;
    gputi=cgaputi;
    ggeti=cgageti;
    gputim=cgaputim;
    gwrite=cgawrite;
    gtitle=cgatitle;
    break;
  case VIDEO_MODE_VGA_16:
    ginit=vgainit;
    gpal=vgapal;
    ginten=vgainten;
    gclear=vgaclear;
    ggetpix=vgagetpix;
    gputi=vgaputi;
    ggeti=vgageti;
    gputim=vgaputim;
    gwrite=vgawrite;
    gtitle=vgatitle;
  }

  video_mode=newmode;
  g_bReady=TRUE;
  gclear();
  ginit();
  gpal(0);
  reset_main_menu_screen=TRUE;
}

HRESULT WINAPI EnumModesCallback(LPDDSURFACEDESC lpddsd,LPVOID lpContext)
{
  struct FULLSCREEN_VID_MODE_INFO* vmode;

  if (lpddsd->dwHeight>=400)
  {
    if (supported_video_modes==NULL)
    {
      vmode=supported_video_modes=malloc(sizeof(struct FULLSCREEN_VID_MODE_INFO));
    }
    else
    {
      vmode=supported_video_modes;
      while(vmode->next_mode!=NULL)
      {
        vmode=vmode->next_mode;
      }
      vmode->next_mode=malloc(sizeof(struct FULLSCREEN_VID_MODE_INFO));
      vmode=vmode->next_mode;
    }
    vmode->width=lpddsd->dwWidth;
    vmode->height=lpddsd->dwHeight;
    vmode->bpp=lpddsd->ddpfPixelFormat.dwRGBBitCount;
    vmode->next_mode=NULL;
  }
  return DDENUMRET_OK;
}

void set_preferred_vidmode(int w, int h, int bpp)
{
  preferred_video_mode.width=w;
  preferred_video_mode.height=h;
  preferred_video_mode.bpp=bpp;
  preferred_video_mode.next_mode=NULL;

  // TODO: if we are in fullscreen mode, then switch to new mode
}

BOOL LoadSpriteFile(LPSTR pszFileName)
{
   HANDLE hFile;
   BOOL bSuccess = FALSE;
   int i;


   hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
      OPEN_EXISTING, NULL, NULL);
   if(hFile != INVALID_HANDLE_VALUE)
   {
      DWORD dwFileSize;
      dwFileSize = GetFileSize(hFile, NULL);
      if(dwFileSize != 0xFFFFFFFF)
      {
         sprite_data=GlobalAlloc(GPTR, dwFileSize);
         if(sprite_data != NULL)
         {
            DWORD dwRead;
            ReadFile(hFile, sprite_data, dwFileSize, &dwRead, NULL);
         }
      }
      CloseHandle(hFile);
   }
   /*
   for (i=0;i<200;i++)
   {
     if
     sprite_array
   }
   */
   return bSuccess;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?