win32.cpp 11 KB

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