win32.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #define UNICODE
  2. #define _UNICODE
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <tchar.h>
  5. #include <windows.h>
  6. #include <commctrl.h>
  7. #include <shlobj.h>
  8. #include <string>
  9. #include "resource.h"
  10. #include "../global.h"
  11. #include "../util.h"
  12. #include "../palcfg.h"
  13. #include "../resampler.h"
  14. #define ComboBox_AddString(hwndDlg, idCtrl, lpsz) \
  15. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_ADDSTRING, (WPARAM)(0), (LPARAM)(lpsz))
  16. #define ComboBox_SetCurSel(hwndDlg, idCtrl, index) \
  17. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_SETCURSEL, (WPARAM)(index), (LPARAM)(0))
  18. #define ComboBox_GetCurSel(hwndDlg, idCtrl) \
  19. (int)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), CB_GETCURSEL, (WPARAM)(0), (LPARAM)(0))
  20. #define TrackBar_SetRange(hwndDlg, idCtrl, min, max, redraw) \
  21. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_SETRANGE, (WPARAM)(redraw), (LPARAM)(MAKELONG((min), (max))))
  22. #define TrackBar_SetPos(hwndDlg, idCtrl, pos, redraw) \
  23. (BOOL)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_SETPOS, (WPARAM)(redraw), (LPARAM)(pos))
  24. #define TrackBar_GetPos(hwndDlg, idCtrl) \
  25. (int)SNDMSG(GetDlgItem((hwndDlg), (idCtrl)), TBM_GETPOS, (WPARAM)(0), (LPARAM)(0))
  26. #define EnableDlgItem(hwnd, nIDControl, bEnable) \
  27. EnableWindow(GetDlgItem((hwnd), (nIDControl)), (bEnable))
  28. HINSTANCE g_hInstance;
  29. WORD g_wLanguage;
  30. int WINAPI LoadStringEx(
  31. HINSTANCE hInstance,
  32. UINT uID,
  33. LANGID wLang,
  34. LPTSTR lpBuffer,
  35. int nBufferMax
  36. )
  37. {
  38. auto hrc = FindResourceEx(hInstance, RT_STRING, MAKEINTRESOURCE((uID >> 4) + 1), wLang);
  39. if (nullptr == hrc) return 0;
  40. auto begin = (LPCWSTR)LockResource(LoadResource(hInstance, hrc));
  41. for (int idx = 0; idx < (int)(uID & 0xf); idx++)
  42. begin += *begin + 1;
  43. if (nBufferMax == 0)
  44. {
  45. *((LPCWSTR*)lpBuffer) = begin;
  46. return sizeof(LPCWSTR);
  47. }
  48. else
  49. {
  50. wcsncpy(lpBuffer, begin + 1, min(nBufferMax, *begin));
  51. if (nBufferMax <= *begin)
  52. {
  53. lpBuffer[nBufferMax - 1] = '\0';
  54. return nBufferMax - 1;
  55. }
  56. else
  57. {
  58. lpBuffer[*begin] = '\0';
  59. return *begin;
  60. }
  61. }
  62. }
  63. void SaveSettings(HWND hwndDlg, BOOL fWriteFile)
  64. {
  65. int textLen;
  66. if (IsDlgButtonChecked(hwndDlg, IDC_CHS))
  67. gConfig.uCodePage = CP_GBK;
  68. else
  69. gConfig.uCodePage = CP_BIG5;
  70. if ((textLen = GetWindowTextLengthA(GetDlgItem(hwndDlg, IDC_MSGFILE))) > 0)
  71. {
  72. gConfig.pszMsgFile = (char*)realloc(gConfig.pszMsgFile, textLen + 1);
  73. GetDlgItemTextA(hwndDlg, IDC_MSGFILE, gConfig.pszMsgFile, textLen + 1);
  74. }
  75. else
  76. {
  77. free(gConfig.pszMsgFile); gConfig.pszMsgFile = nullptr;
  78. }
  79. if ((textLen = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_GAMEPATH))) > 0)
  80. {
  81. gConfig.pszGamePath = (char*)realloc(gConfig.pszGamePath, textLen + 1);
  82. GetDlgItemTextA(hwndDlg, IDC_GAMEPATH, gConfig.pszGamePath, textLen + 1);
  83. }
  84. else
  85. {
  86. free(gConfig.pszGamePath); gConfig.pszGamePath = nullptr;
  87. }
  88. gConfig.fFullScreen = IsDlgButtonChecked(hwndDlg, IDC_FULLSCREEN);
  89. gConfig.fUseTouchOverlay = IsDlgButtonChecked(hwndDlg, IDC_TOUCHOVERLAY);
  90. gConfig.fUseEmbeddedFonts = IsDlgButtonChecked(hwndDlg, IDC_EMBEDFONT);
  91. gConfig.fKeepAspectRatio = IsDlgButtonChecked(hwndDlg, IDC_ASPECTRATIO);
  92. gConfig.eCDType = (MUSICTYPE)(ComboBox_GetCurSel(hwndDlg, IDC_CD) + MUSIC_MP3);
  93. gConfig.eMusicType = (MUSICTYPE)ComboBox_GetCurSel(hwndDlg, IDC_BGM);
  94. gConfig.eOPLType = (OPLTYPE)(ComboBox_GetCurSel(hwndDlg, IDC_OPL));
  95. gConfig.iAudioChannels = IsDlgButtonChecked(hwndDlg, IDC_STEREO) ? 2 : 1;
  96. gConfig.iSampleRate = GetDlgItemInt(hwndDlg, IDC_SAMPLERATE, nullptr, FALSE);
  97. gConfig.wAudioBufferSize = GetDlgItemInt(hwndDlg, IDC_AUDIOBUFFER, nullptr, FALSE);
  98. gConfig.iMusicVolume = TrackBar_GetPos(hwndDlg, IDC_MUSICVOLUME);
  99. gConfig.iSoundVolume = TrackBar_GetPos(hwndDlg, IDC_SOUNDVOLUME);
  100. gConfig.iResampleQuality = TrackBar_GetPos(hwndDlg, IDC_QUALITY);
  101. if (gConfig.eMusicType == MUSIC_RIX)
  102. {
  103. gConfig.fUseSurroundOPL = IsDlgButtonChecked(hwndDlg, IDC_SURROUNDOPL);
  104. gConfig.iOPLSampleRate = GetDlgItemInt(hwndDlg, IDC_OPLSR, nullptr, FALSE);
  105. }
  106. if (fWriteFile) PAL_SaveConfig();
  107. }
  108. void ResetControls(HWND hwndDlg)
  109. {
  110. TCHAR buffer[100];
  111. EnableDlgItem(hwndDlg, IDC_OPL, gConfig.eMusicType == MUSIC_RIX);
  112. EnableDlgItem(hwndDlg, IDC_SURROUNDOPL, gConfig.eMusicType == MUSIC_RIX);
  113. EnableDlgItem(hwndDlg, IDC_OPLSR, gConfig.eMusicType == MUSIC_RIX);
  114. CheckRadioButton(hwndDlg, IDC_CHT, IDC_CHS, IDC_CHT + gConfig.uCodePage);
  115. CheckDlgButton(hwndDlg, IDC_FULLSCREEN, gConfig.fFullScreen);
  116. CheckDlgButton(hwndDlg, IDC_TOUCHOVERLAY, gConfig.fUseTouchOverlay);
  117. CheckDlgButton(hwndDlg, IDC_EMBEDFONT, gConfig.fUseEmbeddedFonts);
  118. CheckDlgButton(hwndDlg, IDC_ASPECTRATIO, gConfig.fKeepAspectRatio);
  119. CheckDlgButton(hwndDlg, IDC_SURROUNDOPL, gConfig.fUseSurroundOPL);
  120. CheckDlgButton(hwndDlg, IDC_STEREO, gConfig.iAudioChannels == 2);
  121. ComboBox_SetCurSel(hwndDlg, IDC_CD, gConfig.eCDType - MUSIC_MP3);
  122. ComboBox_SetCurSel(hwndDlg, IDC_BGM, gConfig.eMusicType);
  123. ComboBox_SetCurSel(hwndDlg, IDC_OPL, gConfig.eOPLType);
  124. SetDlgItemText(hwndDlg, IDC_SAMPLERATE, _itot(gConfig.iSampleRate, buffer, 10));
  125. SetDlgItemText(hwndDlg, IDC_OPLSR, _itot(gConfig.iOPLSampleRate, buffer, 10));
  126. SetDlgItemText(hwndDlg, IDC_AUDIOBUFFER, _itot(gConfig.wAudioBufferSize, buffer, 10));
  127. if (gConfig.pszGamePath) SetDlgItemTextA(hwndDlg, IDC_GAMEPATH, gConfig.pszGamePath);
  128. if (gConfig.pszMsgFile) SetDlgItemTextA(hwndDlg, IDC_MSGFILE, gConfig.pszMsgFile);
  129. TrackBar_SetPos(hwndDlg, IDC_QUALITY, gConfig.iResampleQuality, TRUE);
  130. TrackBar_SetPos(hwndDlg, IDC_MUSICVOLUME, gConfig.iMusicVolume, TRUE);
  131. TrackBar_SetPos(hwndDlg, IDC_SOUNDVOLUME, gConfig.iSoundVolume, TRUE);
  132. }
  133. INT_PTR InitProc(HWND hwndDlg, HWND hwndCtrl, LPARAM lParam)
  134. {
  135. InitCommonControls();
  136. ComboBox_AddString(hwndDlg, IDC_CD, TEXT("MP3"));
  137. ComboBox_AddString(hwndDlg, IDC_CD, TEXT("OGG"));
  138. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("MIDI"));
  139. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("RIX"));
  140. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("MP3"));
  141. ComboBox_AddString(hwndDlg, IDC_BGM, TEXT("OGG"));
  142. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("DOSBOX"));
  143. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("MAME"));
  144. ComboBox_AddString(hwndDlg, IDC_OPL, TEXT("DOSBOXNEW"));
  145. TrackBar_SetRange(hwndDlg, IDC_QUALITY, RESAMPLER_QUALITY_MIN, RESAMPLER_QUALITY_MAX, FALSE);
  146. TrackBar_SetRange(hwndDlg, IDC_MUSICVOLUME, 0, PAL_MAX_VOLUME, FALSE);
  147. TrackBar_SetRange(hwndDlg, IDC_SOUNDVOLUME, 0, PAL_MAX_VOLUME, FALSE);
  148. ResetControls(hwndDlg);
  149. WINDOWINFO wi = { sizeof(WINDOWINFO) };
  150. RECT rcWork;
  151. if (SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0) && GetWindowInfo(hwndDlg, &wi))
  152. {
  153. int x = ((rcWork.right - rcWork.left) - (wi.rcWindow.right - wi.rcWindow.left)) / 2;
  154. int y = ((rcWork.bottom - rcWork.top) - (wi.rcWindow.bottom - wi.rcWindow.top)) / 2;
  155. SetWindowPos(hwndDlg, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOOWNERZORDER | SWP_SHOWWINDOW);
  156. }
  157. return TRUE;
  158. }
  159. INT_PTR ButtonProc(HWND hwndDlg, WORD idControl, HWND hwndCtrl)
  160. {
  161. switch (idControl)
  162. {
  163. case IDOK:
  164. gConfig.fLaunchSetting = FALSE;
  165. SaveSettings(hwndDlg, TRUE);
  166. EndDialog(hwndDlg, IDOK);
  167. return TRUE;
  168. case IDCANCEL:
  169. EndDialog(hwndDlg, IDCANCEL);
  170. return TRUE;
  171. case IDC_DEFAULT:
  172. PAL_LoadConfig(FALSE);
  173. ResetControls(hwndDlg);
  174. return TRUE;
  175. case IDC_BRGAME:
  176. {
  177. TCHAR szName[MAX_PATH * 2], szTitle[200];
  178. BROWSEINFO bi = { hwndDlg, nullptr, szName, szTitle, BIF_USENEWUI, nullptr, NULL, 0 };
  179. LoadStringEx(g_hInstance, idControl, g_wLanguage, szTitle, 200);
  180. auto pidl = SHBrowseForFolder(&bi);
  181. if (pidl)
  182. {
  183. SHGetPathFromIDList(pidl, szName);
  184. int n = _tcslen(szName);
  185. if (szName[n - 1] != '\\') _tcscat(szName, L"\\");
  186. SetDlgItemText(hwndDlg, IDC_GAMEPATH, szName);
  187. }
  188. return TRUE;
  189. }
  190. default: return FALSE;
  191. }
  192. }
  193. INT_PTR ComboBoxProc(HWND hwndDlg, WORD idControl, HWND hwndCtrl)
  194. {
  195. switch (idControl)
  196. {
  197. case IDC_BGM:
  198. EnableDlgItem(hwndDlg, IDC_OPL, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  199. EnableDlgItem(hwndDlg, IDC_SURROUNDOPL, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  200. EnableDlgItem(hwndDlg, IDC_OPLSR, ComboBox_GetCurSel(hwndDlg, IDC_BGM) == MUSIC_RIX);
  201. return TRUE;
  202. default:
  203. return FALSE;
  204. }
  205. }
  206. INT_PTR CommandProc(HWND hwndDlg, WORD command, WORD idControl, HWND hwndCtrl)
  207. {
  208. switch (command)
  209. {
  210. case BN_CLICKED: return ButtonProc(hwndDlg, idControl, hwndCtrl);
  211. case CBN_SELCHANGE: return ComboBoxProc(hwndDlg, idControl, hwndCtrl);
  212. default: return FALSE;
  213. }
  214. }
  215. INT_PTR CALLBACK LauncherDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  216. {
  217. switch (uMsg)
  218. {
  219. case WM_INITDIALOG: return InitProc(hwndDlg, (HWND)wParam, lParam);
  220. case WM_COMMAND: return CommandProc(hwndDlg, HIWORD(wParam), LOWORD(wParam), (HWND)lParam);
  221. case WM_CLOSE: return EndDialog(hwndDlg, 0);
  222. default: return FALSE;
  223. }
  224. }
  225. extern "C" int UTIL_Platform_Init(int argc, char* argv[])
  226. {
  227. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION <= 2
  228. putenv("SDL_VIDEODRIVER=directx");
  229. #endif
  230. g_hInstance = GetModuleHandle(nullptr);
  231. #if !defined(__MINGW32__) || _WIN32_WINNT > _WIN32_WINNT_WS03 // compile time switch; use `make CCFLAGS=-D_WIN32_WINNT=_WIN32_WINNT_VISTA` for vista+ only automatic language detection
  232. g_wLanguage = GetThreadUILanguage();
  233. #else // default XP compatible CodePage detection hack.
  234. g_wLanguage = GetSystemDefaultLangID();
  235. #endif
  236. if (PRIMARYLANGID(g_wLanguage) == LANG_CHINESE)
  237. {
  238. if (SUBLANGID(g_wLanguage) == SUBLANG_CHINESE_SIMPLIFIED || SUBLANGID(g_wLanguage) == SUBLANG_CHINESE_SINGAPORE)
  239. g_wLanguage = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
  240. else
  241. g_wLanguage = MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL);
  242. }
  243. else
  244. g_wLanguage = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
  245. auto dlg = (LPCDLGTEMPLATE)LockResource(LoadResource(g_hInstance, FindResourceEx(g_hInstance, RT_DIALOG, MAKEINTRESOURCE(IDD_LAUNCHER), g_wLanguage)));
  246. if (gConfig.fLaunchSetting && DialogBoxIndirect(GetModuleHandle(nullptr), dlg, nullptr, LauncherDialogProc) != IDOK)
  247. return -1;
  248. else
  249. return 0;
  250. }
  251. extern "C" VOID UTIL_Platform_Quit(VOID) {}
  252. extern "C" BOOL
  253. UTIL_GetScreenSize(
  254. DWORD *pdwScreenWidth,
  255. DWORD *pdwScreenHeight
  256. )
  257. {
  258. return (pdwScreenWidth && pdwScreenHeight && *pdwScreenWidth && *pdwScreenHeight);
  259. }
  260. extern "C"
  261. BOOL UTIL_IsAbsolutePath(LPCSTR lpszFileName)
  262. {
  263. char szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFname[_MAX_FNAME], szExt[_MAX_EXT];
  264. #if !defined(__MINGW32__) // MinGW Distro's win32 api lacks this...Anyway, winxp lacks this too
  265. if (_splitpath_s(lpszFileName, szDrive, szDir, szFname, szExt) == 0)
  266. #else
  267. _splitpath(lpszFileName, szDrive, szDir, szFname, szExt);
  268. if ( errno !=EINVAL )
  269. #endif
  270. return (strlen(szDrive) > 0 && (szDir[0] == '\\' || szDir[0] == '/'));
  271. else
  272. return FALSE;
  273. }