World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
Application.cpp
См. документацию.
1 #include "src/Application.h"
2 
3 #include <Shlwapi.h>
4 #include <Windows.h>
5 #pragma comment(lib, "Shlwapi.lib")
6 #include <Shlobj.h>
7 #pragma comment(lib, "Shell32.lib")
8 
9 #include <string>
10 #include <vector>
11 
12 #include "resource.h"
13 
14 int __stdcall BrowseFolderCallback(HWND hwnd, UINT msg, LPARAM lparam, LPARAM data) {
15  if (msg == BFFM_INITIALIZED) {
16  SendMessageA(hwnd, BFFM_SETSELECTION, true, data);
17  }
18  return 0;
19 }
20 
21 INT_PTR __stdcall DialogProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
22  Application *app = (Application *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
23  switch (msg) {
24  case WM_INITDIALOG: {
25  app = (Application *)lparam;
26  SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)app);
27 
28  CheckRadioButton(
29  hwnd,
30  IDC_RADIO_RENDERER_DIRECTDRAW,
31  IDC_RADIO_RENDERER_OPENGL,
32  IDC_RADIO_RENDERER_DIRECTDRAW);
33  break;
34  }
35 
36  case WM_COMMAND: {
37  if (LOWORD(wparam) == IDC_BUTTON_BROWSE_INSTALL) {
38  char choice[2000];
39  choice[0] = 0;
40  strcpy(choice, app->GetMm7InstallPath().c_str());
41 
42  BROWSEINFO bi;
43  ZeroMemory(&bi, sizeof(bi));
44  bi.lParam = (LPARAM)choice;
45  bi.lpfn = BrowseFolderCallback;
46  bi.ulFlags = BIF_USENEWUI;
47  PIDLIST_ABSOLUTE list = SHBrowseForFolder(&bi);
48 
49  SHGetPathFromIDListA(list, choice);
50  app->SetMm7InstallPath(std::string(choice));
51  SendDlgItemMessageA(hwnd, IDC_EDIT_MM7_INSTALL_DIR, WM_SETTEXT,
52  0, (LPARAM)choice);
53 
54  return TRUE;
55  } else if (LOWORD(wparam) == IDC_BUTTON_LAUNCH) {
56  char mm7_install_dir[2000];
57  GetWindowTextA(GetDlgItem(hwnd, IDC_EDIT_MM7_INSTALL_DIR),
58  mm7_install_dir, 2000);
59 
60  app->SetMm7InstallPath(std::string(mm7_install_dir));
61 
62  if (IsDlgButtonChecked(hwnd, IDC_RADIO_RENDERER_DIRECTDRAW) == BST_CHECKED) {
63  app->SetRenderer(std::string("DirectDraw"));
64  } else if (IsDlgButtonChecked(hwnd, IDC_RADIO_RENDERER_OPENGL) == BST_CHECKED) {
65  app->SetRenderer(std::string("OpenGL"));
66  }
67 
68  std::string config_errors;
69  if (!app->ValidateConfig(config_errors)) {
70  MessageBoxA(hwnd, config_errors.c_str(),
71  "Configuration error", MB_OK);
72  } else {
73  PostMessage(hwnd, WM_QUIT, 0, 0);
74  }
75 
76  return TRUE;
77  }
78  break;
79  }
80  }
81  return FALSE;
82 }
83 
85  this->config = config;
86  return this;
87 }
88 
90  this->config.mm7_install_path = path;
91 }
93  return this->config.mm7_install_path;
94 }
95 
97  char buf[2000];
98  GetModuleFileNameA(GetModuleHandle(0), buf, 2000);
99  return std::string(buf);
100 }
101 
103  auto filename = GetExeFilename();
104  return filename.substr(0, filename.find_last_of("\\/"));
105 }
106 
108  out_errors = "";
109 
110  std::string mm7_exe_path = config.mm7_install_path + "/MM7.exe";
111  if (!PathFileExistsA(mm7_exe_path.c_str())) {
112  out_errors =
113  "Might and Magic VII exe not found in: " + config.mm7_install_path;
114  return false;
115  }
116 
117  return true;
118 }
119 
121  CoInitializeEx(0, COINIT_APARTMENTTHREADED); // SHBrowseForFolder
122 
123  auto module = GetModuleHandleA(nullptr);
124  HWND dialog = CreateDialogParamA(module, MAKEINTRESOURCEA(IDD_FORMVIEW),
125  nullptr, DialogProc, (LPARAM)this);
126  HICON icon =
127  (HICON)LoadImageA(module, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 0, 0,
128  LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
129  SendMessageA(dialog, WM_SETICON, ICON_BIG, (LPARAM)icon);
130  SendDlgItemMessageA(dialog, IDC_EDIT_MM7_INSTALL_DIR, WM_SETTEXT, 0,
131  (LPARAM)config.mm7_install_path.c_str());
132 
133  MSG msg;
134  while (GetMessageA(&msg, dialog, 0, 0)) {
135  TranslateMessage(&msg);
136  DispatchMessageA(&msg);
137  }
138 
139  STARTUPINFOA si;
140  ZeroMemory(&si, sizeof(si));
141  si.cb = sizeof(si);
142 
143  PROCESS_INFORMATION pi;
144  ZeroMemory(&pi, sizeof(pi));
145 
146  std::string womm_filename = GetExePath() + "/" + "World of Might and Magic.exe";
147  std::string command_line = womm_filename + " -window -nointro -nologo -novideo -nomarg -render=" + GetRenderer();
148 
149  std::vector<char> cmd(command_line.begin(), command_line.end());
150  cmd.push_back(0);
151  CreateProcessA(
152  womm_filename.c_str(), cmd.data(), nullptr, nullptr, FALSE,
153  NORMAL_PRIORITY_CLASS, nullptr,
154  config.mm7_install_path.c_str(), &si, &pi);
155 }
Application::ValidateConfig
bool ValidateConfig(std::string &out_errors)
Definition: Application.cpp:107
path
GLsizei const GLchar *const * path
Definition: SDL_opengl_glext.h:3733
Application::SetRenderer
void SetRenderer(const std::string &renderer)
Definition: Application.h:15
GetExeFilename
static std::string GetExeFilename()
Definition: Application.cpp:96
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: SDL_opengl_glext.h:2483
GetExePath
static std::string GetExePath()
Definition: Application.cpp:102
BrowseFolderCallback
int __stdcall BrowseFolderCallback(HWND hwnd, UINT msg, LPARAM lparam, LPARAM data)
Definition: Application.cpp:14
ApplicationConfig
Definition: ApplicationConfig.h:4
resource.h
Application::SetMm7InstallPath
void SetMm7InstallPath(const std::string &path)
Definition: Application.cpp:89
Application.h
Application
Definition: Configuration.h:5
pi
const float pi
Definition: VectorTypes.h:22
Application::GetRenderer
const std::string & GetRenderer() const
Definition: Application.h:16
Application::Run
void Run()
Definition: Application.cpp:120
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
string
GLsizei const GLchar *const * string
Definition: SDL_opengl_glext.h:691
Application::config
ApplicationConfig config
Definition: Application.h:21
DialogProc
INT_PTR __stdcall DialogProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
Definition: Application.cpp:21
Application::GetMm7InstallPath
const std::string & GetMm7InstallPath() const
Definition: Application.cpp:92
Application::Configure
Application * Configure(ApplicationConfig &config)
Definition: Application.cpp:84
config
EGLConfig config
Definition: SDL_egl.h:1149