WINMAIN.CPP 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/Tests/MeshTest/WINMAIN.CPP 9 12/10/98 5:53p Greg_h $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando *
  24. * *
  25. * $Archive:: /Commando/Code/Tests/MeshTest/WINMAIN.CPP $*
  26. * *
  27. * $Author:: Greg_h $*
  28. * *
  29. * $Modtime:: 12/07/98 12:11p $*
  30. * *
  31. * $Revision:: 9 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * WinMain -- Win32 Program Entry Point! *
  36. * WIN_resize -- Surrender-required function which resizes the main window *
  37. * WIN_set_fullscreen -- Surrender-required function for toggling full-screen mode *
  38. * Main_Window_Proc -- Windows Proc for the main game window *
  39. * Create_Main_Window -- Creates the main game window *
  40. * Focus_Loss -- this function is called when the application loses focus *
  41. * Focus_Restore -- This function is called when the application gets focus *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #define NOMINMAX
  44. #include "winmain.h"
  45. #include <sr.hpp>
  46. #include "win.h"
  47. #include "wwmouse.h"
  48. #include "init.h"
  49. #include "mainloop.h"
  50. #include "shutdown.h"
  51. #include "_globals.h"
  52. //----------------------------------------------------------------------------
  53. // Globals
  54. //----------------------------------------------------------------------------
  55. extern "C"
  56. {
  57. HWND hWndMain;
  58. bool WIN_fullscreen = true;
  59. }
  60. //----------------------------------------------------------------------------
  61. // Local functions
  62. //----------------------------------------------------------------------------
  63. static BOOL Create_Main_Window(HANDLE hInstance, int nCmdShow);
  64. long FAR PASCAL Main_Window_Proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  65. void Focus_Loss(void);
  66. void Focus_Restore(void);
  67. void Split_Command_Line_Args(HINSTANCE instance, char *path_to_exe, char *command_line);
  68. void Set_Working_Directory(char *old_path, char *new_path);
  69. /***********************************************************************************************
  70. * WinMain -- Win32 Program Entry Point! *
  71. * *
  72. * INPUT: *
  73. * *
  74. * Standard WinMain inputs :-) *
  75. * *
  76. * OUTPUT: *
  77. * *
  78. * Standard WinMain output *
  79. * *
  80. * WARNINGS: *
  81. * *
  82. * HISTORY: *
  83. * 07/18/1997 GH : Created. *
  84. *=============================================================================================*/
  85. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  86. {
  87. LPSTR command;
  88. HANDLE prev;
  89. char path_to_exe[_MAX_PATH];
  90. char oldpath[_MAX_PATH];
  91. command = lpCmdLine;
  92. prev = hPrevInstance;
  93. if (!Create_Main_Window(hInstance, nCmdShow)) return 0;
  94. // Setup the keyboard system
  95. Keyboard = new WWKeyboardClass();
  96. // Setup the mouse system and take over the mouse.
  97. MouseCursor = new WWMouseClass(NULL, MainWindow);
  98. Split_Command_Line_Args(hInstance, &path_to_exe[0], lpCmdLine);
  99. Set_Working_Directory(oldpath, &path_to_exe[0]);
  100. Init();
  101. Main_Loop();
  102. Shutdown();
  103. delete Keyboard;
  104. delete MouseCursor;
  105. return(EXIT_SUCCESS);
  106. }
  107. /***********************************************************************************************
  108. * Main_Window_Proc -- Windows Proc for the main game window *
  109. * *
  110. * INPUT: *
  111. * *
  112. * Standard Windows Proc inputs *
  113. * *
  114. * OUTPUT: *
  115. * *
  116. * Standard Windows Proc output *
  117. * *
  118. * WARNINGS: *
  119. * *
  120. * HISTORY: *
  121. * 07/18/1997 GH : Created. *
  122. *=============================================================================================*/
  123. long FAR PASCAL Main_Window_Proc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
  124. {
  125. PAINTSTRUCT ps;
  126. HDC hdc;
  127. /*
  128. ** Pass this message through to the keyboard handler. If the message
  129. ** was processed and requires no further action, then return with
  130. ** this information.
  131. */
  132. if (Keyboard) {
  133. Keyboard->Message_Handler(hwnd, message, wParam, lParam);
  134. }
  135. switch (message )
  136. {
  137. /*
  138. ** basic management messages
  139. */
  140. case WM_ACTIVATEAPP:
  141. if (WIN_fullscreen) {
  142. GameInFocus = (wParam != 0);
  143. if (!GameInFocus) {
  144. Focus_Loss();
  145. } else {
  146. Focus_Restore();
  147. }
  148. } else {
  149. GameInFocus = true;
  150. if (wParam != 0) {
  151. if (MouseCursor != NULL) MouseCursor->Capture_Mouse();
  152. } else {
  153. if (MouseCursor != NULL) MouseCursor->Release_Mouse();
  154. }
  155. }
  156. return(0);
  157. case WM_SETCURSOR:
  158. SetCursor(NULL);
  159. return 1;
  160. case WM_ERASEBKGND:
  161. return 1;
  162. case WM_PAINT:
  163. hdc = BeginPaint( hwnd, &ps);
  164. EndPaint( hwnd, &ps);
  165. return 1;
  166. /*
  167. ** minimize/maximize
  168. */
  169. case WM_SYSKEYDOWN:
  170. if (wParam == VK_RETURN && ((lParam>>16) & KF_ALTDOWN) && !((lParam>>16) & KF_REPEAT))
  171. {
  172. WIN_fullscreen = !WIN_fullscreen;
  173. }
  174. break;
  175. /*
  176. ** interface open and close
  177. */
  178. case WM_CREATE:
  179. break;
  180. case WM_DESTROY:
  181. ReleaseCapture();
  182. PostQuitMessage( 0);
  183. break;
  184. case WM_SYSCOMMAND:
  185. switch (wParam) {
  186. case SC_CLOSE:
  187. /*
  188. ** Windows sent us a close message. Probably in response to Alt-F4. Ignore it by
  189. ** pretending to handle the message and returning true;
  190. */
  191. return (0);
  192. case SC_SCREENSAVE:
  193. /*
  194. ** Windoze is about to start the screen saver. If we just return without passing
  195. ** this message to DefWindowProc then the screen saver will not be allowed to start.
  196. */
  197. return (0);
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. return DefWindowProc(hwnd, message, wParam, lParam);
  204. }
  205. /***********************************************************************************************
  206. * Create_Main_Window -- Creates the main game window *
  207. * *
  208. * INPUT: *
  209. * *
  210. * hInstance -- Instance handle of the application *
  211. * nCmdShow -- how the window is to be shown *
  212. * *
  213. * OUTPUT: *
  214. * *
  215. * TRUE = success, FALSE = failure *
  216. * *
  217. * WARNINGS: *
  218. * *
  219. * HISTORY: *
  220. * 07/18/1997 GH : Created. *
  221. *=============================================================================================*/
  222. static BOOL Create_Main_Window(HANDLE hInstance, int nCmdShow)
  223. {
  224. WNDCLASS wc;
  225. BOOL rc;
  226. ProgramInstance = (HINSTANCE)hInstance;
  227. wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  228. wc.lpfnWndProc = Main_Window_Proc;
  229. wc.cbClsExtra = 0;
  230. wc.cbWndExtra = 0;
  231. wc.hInstance = (HINSTANCE)hInstance;
  232. wc.hIcon = LoadIcon( NULL, IDI_APPLICATION);
  233. wc.hCursor = LoadCursor( NULL, IDC_ARROW);
  234. wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH);
  235. wc.lpszMenuName = NULL;
  236. wc.lpszClassName = "SRCLASS";
  237. rc = RegisterClass( &wc);
  238. if (!rc ) return FALSE;
  239. MainWindow = hWndMain = CreateWindowEx(
  240. 0, // WS_EX_TOPMOST,
  241. "SRClass",
  242. "Commando",
  243. WS_VISIBLE |
  244. WS_CAPTION |
  245. WS_BORDER |
  246. WS_SYSMENU |
  247. WS_MINIMIZEBOX |
  248. WS_MAXIMIZEBOX |
  249. WS_THICKFRAME,
  250. 0, 0, // top left corner
  251. 640,
  252. 480,
  253. NULL, // no parent handle
  254. NULL, // no menu handle
  255. ProgramInstance, // main program instance
  256. NULL); // creation parameters
  257. if (!MainWindow) {
  258. return FALSE;
  259. }
  260. return TRUE;
  261. }
  262. /***********************************************************************************************
  263. * Focus_Loss -- this function is called when the application loses focus *
  264. * *
  265. * INPUT: Nothing *
  266. * *
  267. * OUTPUT: Nothing *
  268. * *
  269. * WARNINGS: None *
  270. * *
  271. * HISTORY: *
  272. * 07/18/1997 GH : Created. *
  273. *=============================================================================================*/
  274. void Focus_Loss(void)
  275. {
  276. }
  277. /***********************************************************************************************
  278. * Focus_Restore -- This function is called when the application gets focus *
  279. * *
  280. * INPUT: Nothing *
  281. * *
  282. * OUTPUT: Nothing *
  283. * *
  284. * WARNINGS: None *
  285. * *
  286. * HISTORY: *
  287. * 07/18/1997 GH : Created. *
  288. *=============================================================================================*/
  289. void Focus_Restore(void)
  290. {
  291. }
  292. void Prog_End(void)
  293. {
  294. // Sound_End();
  295. MouseCursor->Release_Mouse();
  296. delete MouseCursor;
  297. MouseCursor = NULL;
  298. }
  299. void Split_Command_Line_Args(HINSTANCE instance, char *path_to_exe, char *command_line)
  300. {
  301. // first arguement is the path to the executable including file name
  302. GetModuleFileName (instance, &path_to_exe[0], 132);
  303. Argv[0] = path_to_exe;
  304. char * token = strtok(command_line, " ");
  305. Argc = 1;
  306. while (Argc < ARRAY_SIZE(Argv) && token != NULL) {
  307. Argv[Argc++] = token;
  308. token = strtok(NULL, " ");
  309. }
  310. }
  311. void Set_Working_Directory(char *old_path, char *new_path)
  312. {
  313. char drive[_MAX_DRIVE];
  314. char path[_MAX_PATH];
  315. char dir[_MAX_DIR];
  316. /*
  317. ** Remember the current working directory and drive.
  318. */
  319. GetCurrentDirectory(_MAX_PATH, old_path);
  320. /*
  321. ** Change directory to the where the executable is located. Handle the
  322. ** case where there is no path attached to argv[0].
  323. */
  324. _splitpath(new_path, drive, dir, NULL, NULL);
  325. _makepath(path, drive, dir, NULL, NULL);
  326. SetCurrentDirectory(path);
  327. }