win_sys.c

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

C
1,185
字号

LRESULT CALLBACK sound_settings_dialog_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  LONG cur_volume;
  LONG new_val;
  BOOL success;
  HWND control;

  switch (uMsg)
  {
  case WM_INITDIALOG:
    cur_dialog_box=hDlg;
    cur_volume=get_sound_volume();
  control = GetDlgItem(hDlg, IDC_SCROLLBAR_SOUND_VOLUME);
    SetScrollRange(control, SB_CTL, 0, 100, TRUE);
    SetScrollPos(control, SB_CTL, (100 - cur_volume), TRUE);
    EnableScrollBar(control, SB_CTL, ESB_ENABLE_BOTH);
    CheckDlgButton(hDlg, IDC_CHECK_PLAY_SOUNDS, soundflag ? 1 : 0);
    CheckDlgButton(hDlg, IDC_CHECK_PLAY_MUSIC, musicflag ? 1 : 0);
    SetDlgItemInt(hDlg, IDC_EDIT_BUFFER_SIZE, (int) (size / 2), FALSE);
    SetDlgItemInt(hDlg, IDC_EDIT_SAMPLE_RATE, get_sound_freq(), FALSE);
    return TRUE;

  case WM_SYSCOMMAND:
    switch (wParam)
    {
    case SC_CLOSE:
      EndDialog(hDlg, FALSE);
      return TRUE;
    }
    break;

  case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDCANCEL:
      EndDialog(hDlg, FALSE);
      return TRUE;
    case IDOK:
      new_val = GetScrollPos(GetDlgItem(hDlg, IDC_SCROLLBAR_SOUND_VOLUME), SB_CTL);
      set_sound_volume(100-new_val);

      soundflag = IsDlgButtonChecked(hDlg, IDC_CHECK_PLAY_SOUNDS);
      musicflag = IsDlgButtonChecked(hDlg, IDC_CHECK_PLAY_MUSIC);

      soundlevdoneflag=FALSE;   /* prevent game from locking up if you toggle sound (using menu/dialog box) while the 'level-done' tune is playing */

      new_val=GetDlgItemInt(hDlg, IDC_EDIT_BUFFER_SIZE, &success, FALSE);
      if (new_val>0)
        WriteINIInt(INI_SOUND_SETTINGS, "BufferSize", new_val, ININAME);
      new_val=GetDlgItemInt(hDlg, IDC_EDIT_SAMPLE_RATE, &success, FALSE);
      if (new_val>0)
        WriteINIInt(INI_SOUND_SETTINGS, "Rate", new_val, ININAME);
      EndDialog(hDlg, TRUE);
      return TRUE;
    }
    break;

  case WM_VSCROLL:
    control = (HWND) lParam;
    switch ((int) LOWORD(wParam))
    {
    case SB_THUMBPOSITION:
      SetScrollPos(control, SB_CTL, (short int) HIWORD(wParam), TRUE);
      break;
    case SB_LINEUP:
      new_val=GetScrollPos(control, SB_CTL) - 1;
      SetScrollPos(control, SB_CTL,new_val, TRUE);
      break;
    case SB_LINEDOWN:
      new_val=GetScrollPos(control, SB_CTL) + 1;
      SetScrollPos(control, SB_CTL,new_val, TRUE);
      break;
    case SB_PAGEUP:
      new_val=GetScrollPos(control, SB_CTL) - 10;
      SetScrollPos(control, SB_CTL,new_val, TRUE);
      break;
    case SB_PAGEDOWN:
      new_val=GetScrollPos(control, SB_CTL) + 10;
      SetScrollPos(control, SB_CTL,new_val, TRUE);
      break;
    case SB_TOP:
      SetScrollPos(control, SB_CTL,0, TRUE);
      break;
    case SB_BOTTOM:
      SetScrollPos(control, SB_CTL,100, TRUE);
      break;
    }
    return TRUE;
  }
  return FALSE;
}

LRESULT CALLBACK help_about_dialog_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  char info_string[200];
  switch (uMsg)
  {
  case WM_INITDIALOG:
    cur_dialog_box=hDlg;
    strcpy(info_string, DIGGER_VERSION);
    strcat(info_string, "\n");
    strcat(info_string, "Win32 version (DirectX optional)");
    SetDlgItemText(hDlg, IDC_STATIC_VERSION, info_string);
    if (g_bWindowed)
    {
      strcpy(info_string, "Running in Windowed Mode\n");
      strcat(info_string,palettized_desktop ? "(256 Colors/Using Palette)\n" : "(HighColor/TrueColor)\n");
    }
    else
    {
      strcpy(info_string, "Running in Full Screen Mode\n");
    }
    strcat(info_string, hDirectDrawInstance ? "DirectDraw detected\n" : "DirectDraw NOT detected\n");
    if (!use_direct_draw)
      strcat(info_string, "Not using DirectDraw");
    else if (use_direct_draw)
      strcat(info_string, "Using DirectDraw");
    SetDlgItemText(hDlg, IDC_GRAPHICS_INFO, info_string);
    strcpy(info_string, wave_device_available ? "Available\n" : "Unavailable\n");
    strcat(info_string, hDirectSoundInstance ? "DirectSound detected" : "DirectSound NOT detected");
    SetDlgItemText(hDlg, IDC_SOUND_INFO, info_string);
    return TRUE;

  case WM_SYSCOMMAND:
    switch (wParam)
    {
    case SC_CLOSE:
      EndDialog(hDlg, FALSE);
      return TRUE;
    }
    break;

  case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDOK:
      EndDialog(hDlg, TRUE);
      return TRUE;
    }
    break;
  }
  return FALSE;
}

void init_direct_x()
{
  if (!check_for_direct_x)
    return;

  hDirectDrawInstance = LoadLibrary("ddraw.dll");
  hDirectSoundInstance = LoadLibrary("dsound.dll");
  if (hDirectDrawInstance)
  {
    lpDirectDrawCreate = (long (__stdcall *)(struct _GUID *,struct IDirectDraw ** ,struct IUnknown *)) GetProcAddress(hDirectDrawInstance,"DirectDrawCreate");
    if (lpDirectDrawCreate)
      use_direct_draw=0;
  }
  else
    use_direct_draw=0;

  if (hDirectSoundInstance)
  {
//    lpDirectSoundCreate = (long (__stdcall *)(struct _GUID *,struct IDirectSound ** ,struct IUnknown *)) GetProcAddress(hDirectSoundInstance,"DirectSoundCreate");
  }
}

void release_direct_x()
{
  if (hDirectDrawInstance)
    FreeLibrary(hDirectDrawInstance);
  if (hDirectSoundInstance)
    FreeLibrary(hDirectSoundInstance);
}

HRESULT WINAPI DirectDrawCreate( GUID FAR *lpGUID, LPDIRECTDRAW FAR *lplpDD, IUnknown FAR *pUnkOuter )
{
  return (lpDirectDrawCreate)(lpGUID, lplpDD, pUnkOuter);
}

HRESULT WINAPI DirectSoundCreate(LPGUID lpGUID, LPDIRECTSOUND * lplpDS, LPUNKNOWN pUnkOuter)
{
  return (lpDirectSoundCreate)(lpGUID, lplpDS, pUnkOuter);
}

LRESULT CALLBACK levels_dialog_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  BOOL success;
  UINT rval;
  HWND control;
  int i;
  char str[4];
  char dlf_filename[FILENAME_BUFFER_SIZE];

  switch (uMsg)
  {
  case WM_INITDIALOG:
    cur_dialog_box=hDlg;
    SetDlgItemInt(hDlg, IDC_START_LEVEL, startlev, FALSE);
    control = GetDlgItem(hDlg, IDC_START_LEVEL);
    for (i=1;i<16;i++)
    {
      ComboBox_AddString(control,itoa(i,str,10));
    }
    CheckRadioButton(hDlg,IDC_RADIO_USE_BUILT_IN, IDC_RADIO_USE_EXTERNAL, levfflag ? IDC_RADIO_USE_EXTERNAL : IDC_RADIO_USE_BUILT_IN);
    control = GetDlgItem(hDlg, IDC_EDIT_FILENAME);
    Edit_Enable(control, levfflag);
    Edit_SetText(control, levfname);
    return TRUE;

  case WM_SYSCOMMAND:
    switch (wParam)
    {
    case SC_CLOSE:
      EndDialog(hDlg, FALSE);
      return TRUE;
    }
    break;

  case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDCANCEL:
      EndDialog(hDlg, FALSE);
      return TRUE;
    case IDOK:
      rval=GetDlgItemInt(hDlg, IDC_START_LEVEL, &success, FALSE);
      if (rval>0)
        startlev=rval;
      if (IsDlgButtonChecked(hDlg,IDC_RADIO_USE_EXTERNAL))
      {
        control = GetDlgItem(hDlg, IDC_EDIT_FILENAME);
        Edit_GetText(control,dlf_filename,FILENAME_BUFFER_SIZE);
        load_level_file(dlf_filename);
      }
      else
      {
        restore_original_level_data();
      }
      refresh_screen_info();
      EndDialog(hDlg, TRUE);
      return TRUE;
    case IDC_BROWSE_FILENAME:
      CheckRadioButton(hDlg,IDC_RADIO_USE_BUILT_IN, IDC_RADIO_USE_EXTERNAL, IDC_RADIO_USE_EXTERNAL);
      control = GetDlgItem(hDlg, IDC_EDIT_FILENAME);
      Edit_GetText(control,dlf_filename,FILENAME_BUFFER_SIZE);
      if (get_open_save_filename(OPEN,"Load Extra Levels","Digger Level Files (*.dlf)\0*.DLF\0All Files (*.*)\0*.*\0","DLF", dlf_filename))
        Edit_SetText(control,dlf_filename);
      control = GetDlgItem(hDlg, IDC_EDIT_FILENAME);
      Edit_Enable(control, TRUE);
      SetActiveWindow(hDlg);
      break;
    case IDC_RADIO_USE_EXTERNAL:
      control = GetDlgItem(hDlg, IDC_EDIT_FILENAME);
      Edit_Enable(control, TRUE);
      break;
    case IDC_RADIO_USE_BUILT_IN:
      control = GetDlgItem(hDlg, IDC_EDIT_FILENAME);
      Edit_Enable(control, FALSE);
      break;
    }
    break;
  }
  return FALSE;
}

UINT FAR PASCAL CommonDialogBoxHook(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
/* TODO: This function *should* center the File Open/Save dialog box on the screen*/
/*       Currently, the dialog box always appears at the top-left corner when in full-screen mode.  */
/*       Without this Hook function though, the File Open/Save dialog box often appears off-screen. */

/*
  RECT        rc;
  POINT       pt,pt2;

  if (WM_NOTIFY==iMsg)
  {
    if ( ((LPOFNOTIFY) lParam)->hdr.code == CDN_INITDONE )
    {
      SetWindowPos(hDlg, NULL, 100, 100, 0, 0,
                 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
      return FALSE;
    }
  }
*/
  return FALSE;
}

/* use Windows Common Dialog Box to get a filename for save/open operations */
bool get_open_save_filename(bool save, char* title, char* filter, char* defext, char* filename)
{
  OPENFILENAME ofn;
  char fn[FILENAME_BUFFER_SIZE];
  char dir[FILENAME_BUFFER_SIZE];
  bool result;

  if ( (!g_bWindowed) && (!cur_dialog_box) )
  {
    attach_clipper();
    IDirectDrawSurface_SetPalette(g_pDDSPrimary, NULL);
  }
  SetMenu(hWnd, GetMenu(hWnd));
  show_mouse_cursor();
  strcpy (fn, filename);
  GetCurrentDirectory(FILENAME_BUFFER_SIZE-1,dir);
  ofn.lStructSize       = sizeof (OPENFILENAME);
  ofn.hwndOwner         = hWnd;
  ofn.hInstance         = (HANDLE) g_hInstance;
  ofn.lpstrFilter       = filter;
  ofn.lpstrCustomFilter = (LPTSTR)NULL;
  ofn.nMaxCustFilter    = 0L;
  ofn.nFilterIndex      = 1L;
  ofn.lpstrFile         = fn;
  ofn.nMaxFile          = FILENAME_BUFFER_SIZE-1;
  ofn.lpstrFileTitle    = NULL;
  ofn.nMaxFileTitle     = 0;
  ofn.lpstrInitialDir   = dir;
  ofn.lpstrTitle        = title;
  ofn.nFileOffset       = 0;
  ofn.nFileExtension    = 0;
  ofn.lpstrDefExt       = "drf";
  ofn.lCustData         = 0;
  ofn.lpfnHook=CommonDialogBoxHook;
  if (save)
  {
    ofn.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
    if (!g_bWindowed)
      ofn.Flags|=OFN_ENABLEHOOK;
    result=GetSaveFileName(&ofn);
  }
  else
  {
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
    if (!g_bWindowed)
      ofn.Flags|=OFN_ENABLEHOOK;
    result=GetOpenFileName(&ofn);
  }
  if ( (!g_bWindowed) && (!cur_dialog_box) )
  {
    release_clipper();
    gpal(cur_palette);
  }
  resume_windows_sound_playback();
  InvalidateRect(hWnd, NULL, FALSE);
  UpdateWindow(hWnd);
  if (result)
    strcpy(filename,fn);
  return result;
}

void restore_original_level_data()
{
  memcpy(leveldat,orig_leveldat,8*MHEIGHT*MWIDTH);
  bonusscore=orig_bonusscore;
  strcpy(levfname,"");
  levfflag=FALSE;
}

/* load a DLF file */
void load_level_file(char* fn)
{
  FILE* levf;
  levf=fopen(fn,"rb");
  if (levf==NULL) {
    strcat(fn,".DLF");
    levf=fopen(fn,"rb");
  }
  if (levf==NULL)
  {
    levfflag=FALSE;
    restore_original_level_data();
    strcpy(levfname,"");
  }
  else
  {
    fread(&bonusscore,2,1,levf);
    fread(leveldat,1200,1,levf);
    fclose(levf);
    strcpy(levfname,fn);
    levfflag=TRUE;
  }
}

void init_joystick()
{
    /* use the standard Win32 joystick functions */
    JOYINFO ji;
    if (joyGetNumDevs())
      if (joyGetPos(0,&ji)==JOYERR_NOERROR)
        joyflag=TRUE;
}


⌨️ 快捷键说明

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