⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winmain.cpp

📁 用DirectX编写RPG游戏-Programming.Role.Playing.Games.with.DirectX
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**************************************************
WinMain.cpp
Chapter 16 Master Character List Editor

Programming Role-Playing Games with DirectX
by Jim Adams (01 Jan 2002)
**************************************************/

#include <windows.h>
#include <stdio.h>
#include "resource.h"

#include "MCL.h"

// Application variables ////////////////////////////////////////////
HWND g_hWnd;                   // Window handle
char g_szClass[] = "MCLEDIT";  // Class name

OPENFILENAME g_ofn;            // Open/Save dialog data
char g_MCLFile[MAX_PATH];      // Filename for character file

sCharacterDefinition g_Characters[256]; // Character list
long g_EditCharacter;                   // Character to modify

// Application prototypes ///////////////////////////////////////////
int  PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK ModifyDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

BOOL UpdateEntry(HWND hWnd, long CharNum);
BOOL LoadCharacters(char *Filename);
BOOL NewMCL();
BOOL LoadMCL();
BOOL SaveMCL();

// Application //////////////////////////////////////////////////////
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
  WNDCLASS wc;
  MSG      Msg;

  // Register window class
  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WindowProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = DLGWINDOWEXTRA;
  wc.hInstance     = hInst;
  wc.hIcon         = LoadIcon(hInst, IDI_APPLICATION);
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = g_szClass;
  RegisterClass(&wc);

  // Create the dialog box window and show it
  g_hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_EDIT), 0, NULL);
  UpdateWindow(g_hWnd);
  ShowWindow(g_hWnd, nCmdShow);

  // Force a load of Characters from default.MCL
  LoadCharacters("..\\Data\\Default.MCL");

  // Message loop
  while(GetMessage(&Msg, NULL, 0, 0)) {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
  }

  // Clean up
  UnregisterClass(g_szClass, hInst);

  return 0;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  int Selection;
  char Text[256];

  switch(uMsg) {
    case WM_COMMAND:
      switch(LOWORD(wParam)) {

        // New MCL file
        case IDC_NEW: 
          NewMCL(); 
          break;

        // Load a MCL file
        case IDC_LOAD: 
          LoadMCL();
          break;

        // Save a MCL file
        case IDC_SAVE: 
          SaveMCL();
          break;

        // Clear an entry
        case IDC_CLEAR:
          // See if a character was selected
          if((Selection = SendMessage(GetDlgItem(g_hWnd, IDC_CHARACTERS), LB_GETCURSEL, 0, 0)) == LB_ERR)
            break;

          ZeroMemory(&g_Characters[Selection], sizeof(sCharacterDefinition));
          g_Characters[Selection].Weapon    = -1;
          g_Characters[Selection].Armor     = -1;
          g_Characters[Selection].Shield    = -1;
          g_Characters[Selection].Accessory = -1;
          SendMessage(GetDlgItem(g_hWnd, IDC_CHARACTERS), LB_DELETESTRING, Selection, 0);
          sprintf(Text, "%5lu: %s", Selection, g_Characters[Selection].Name);
          SendMessage(GetDlgItem(g_hWnd, IDC_CHARACTERS), LB_INSERTSTRING, Selection, (LPARAM)Text);
          break;

        // Edit an entry
        case IDC_CHARACTERS:
          if(HIWORD(wParam) != LBN_DBLCLK)
            break;
        case IDC_EDIT:
          // See if an character was selected
          if((Selection = SendMessage(GetDlgItem(g_hWnd, IDC_CHARACTERS), LB_GETCURSEL, 0, 0)) == LB_ERR)
            break;

          // Set the character to edit
          g_EditCharacter = Selection;

          // Call the modify character dialog
          DialogBox(NULL, MAKEINTRESOURCE(IDD_MODIFY), hWnd, ModifyDialogProc);

          // Update character
          SendMessage(GetDlgItem(g_hWnd, IDC_CHARACTERS), LB_DELETESTRING, Selection, 0);
          sprintf(Text, "%5lu: %s", Selection, g_Characters[Selection].Name);
          SendMessage(GetDlgItem(g_hWnd, IDC_CHARACTERS), LB_INSERTSTRING, Selection, (LPARAM)Text);
          break;
      }
      break;
    
    case WM_CREATE:
      // Initialize the save/load dialog box info
      ZeroMemory(&g_ofn, sizeof(OPENFILENAME));
      g_ofn.lStructSize = sizeof(OPENFILENAME);
      g_ofn.nMaxFile = MAX_PATH;
      g_ofn.nMaxFileTitle = MAX_PATH;
      g_ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT;

      // Set default MCL filename
      strcpy(g_MCLFile, "..\\Data\\Default.MCL");

      break;

    case WM_DESTROY:
      PostQuitMessage(0);
      break;

    default: return DefWindowProc(hWnd, uMsg, wParam, lParam);
  }

  return 0;
}

BOOL CALLBACK ModifyDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  char Text[32];
  long i;

  switch(uMsg) {
    case WM_INITDIALOG:
      // Return an error if there's no character to modify
      if(g_EditCharacter >= 256) {
        EndDialog(hWnd, FALSE);
        return FALSE;
      }

      // Display character number
      sprintf(Text, "%lu", g_EditCharacter);
      SetWindowText(GetDlgItem(hWnd, IDC_NUM), Text);

      // Display text entries
      SetWindowText(GetDlgItem(hWnd, IDC_NAME), g_Characters[g_EditCharacter].Name);
      SetWindowText(GetDlgItem(hWnd, IDC_ITEM), g_Characters[g_EditCharacter].ItemFilename);

      // Display numerical entries
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Class);
      SetWindowText(GetDlgItem(hWnd, IDC_CLASS), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].HealthPoints);
      SetWindowText(GetDlgItem(hWnd, IDC_HEALTH), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].ManaPoints);
      SetWindowText(GetDlgItem(hWnd, IDC_MANA), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Level);
      SetWindowText(GetDlgItem(hWnd, IDC_LEVEL), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Experience);
      SetWindowText(GetDlgItem(hWnd, IDC_EXPERIENCE), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Money);
      SetWindowText(GetDlgItem(hWnd, IDC_MONEY), Text);

      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Attack);
      SetWindowText(GetDlgItem(hWnd, IDC_ATTACK), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Defense);
      SetWindowText(GetDlgItem(hWnd, IDC_DEFENSE), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Agility);
      SetWindowText(GetDlgItem(hWnd, IDC_AGILITY), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Resistance);
      SetWindowText(GetDlgItem(hWnd, IDC_RESISTANCE), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Mental);
      SetWindowText(GetDlgItem(hWnd, IDC_MENTAL), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].ToHit);
      SetWindowText(GetDlgItem(hWnd, IDC_TOHIT), Text);
      sprintf(Text, "%lf", g_Characters[g_EditCharacter].Speed);
      SetWindowText(GetDlgItem(hWnd, IDC_SPEED), Text);

      sprintf(Text, "%lf", g_Characters[g_EditCharacter].Range);
      SetWindowText(GetDlgItem(hWnd, IDC_RANGE), Text);
      sprintf(Text, "%lf", g_Characters[g_EditCharacter].ChargeRate);
      SetWindowText(GetDlgItem(hWnd, IDC_CHARGE), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].ToAttack);
      SetWindowText(GetDlgItem(hWnd, IDC_TOATTACK), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].ToMagic);
      SetWindowText(GetDlgItem(hWnd, IDC_TOMAGIC), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].DropItem);
      SetWindowText(GetDlgItem(hWnd, IDC_DROPITEM), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].DropChance);
      SetWindowText(GetDlgItem(hWnd, IDC_TODROP), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].EffectChance);
      SetWindowText(GetDlgItem(hWnd, IDC_TOEFFECT), Text);
      sprintf(Text, "%lu", g_Characters[g_EditCharacter].Effects);
      SetWindowText(GetDlgItem(hWnd, IDC_EFFECT), Text);

      sprintf(Text, "%lu", g_Characters[g_EditCharacter].MeshNum);
      SetWindowText(GetDlgItem(hWnd, IDC_MESH), Text);
      sprintf(Text, "%ld", g_Characters[g_EditCharacter].Weapon);
      SetWindowText(GetDlgItem(hWnd, IDC_WEAPON), Text);
      sprintf(Text, "%ld", g_Characters[g_EditCharacter].Armor);
      SetWindowText(GetDlgItem(hWnd, IDC_ARMOR), Text);
      sprintf(Text, "%ld", g_Characters[g_EditCharacter].Shield);
      SetWindowText(GetDlgItem(hWnd, IDC_SHIELD), Text);
      sprintf(Text, "%ld", g_Characters[g_EditCharacter].Accessory);
      SetWindowText(GetDlgItem(hWnd, IDC_ACCESSORY), Text);

⌨️ 快捷键说明

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