implement -nomusic for linux hmiplay (d1x r1.8)
[btb/d2x.git] / arch / win32 / init.c
1 /* $Id: init.c,v 1.2 2004-05-19 02:39:21 btb Exp $ */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <windows.h>
6 #include <ddraw.h>
7 #include <mmsystem.h>
8 #include "args.h"
9
10 #ifndef NDEBUG
11 #ifdef _MSC_VER
12 #include <crtdbg.h>
13 #endif
14 #endif
15
16 extern HINSTANCE hInst;
17 extern HWND g_hWnd;
18 extern LPDIRECTDRAW            lpDD;
19 extern LPDIRECTDRAWSURFACE     lpDDSPrimary;
20 extern LPDIRECTDRAWSURFACE     lpDDSOne;
21 extern LPDIRECTDRAWPALETTE     lpDDPal;
22
23 extern int Inferno_verbose;
24
25 static int mouse_hidden=0;
26
27 static void finiObjects()
28 {
29         if(lpDD!=NULL)
30         {
31                 if(lpDDSPrimary!=NULL)
32                 {           
33                         IDirectDrawSurface_Release(lpDDSPrimary);
34                         lpDDSPrimary=NULL;
35                 }
36                 if(lpDDSOne!=NULL)
37                 {
38                         IDirectDrawSurface_Unlock(lpDDSOne,NULL);
39                         IDirectDrawSurface_Release(lpDDSOne);
40                         lpDDSOne=NULL;
41                 }
42                 if(lpDDPal!=NULL)
43                 {
44                         IDirectDrawSurface_Release(lpDDPal);
45                         lpDDPal=NULL;
46                 }
47                 IDirectDrawSurface_Release(lpDD);
48                 lpDD=NULL;
49         }
50    if(mouse_hidden)
51     ShowCursor(TRUE);
52
53
54 //extern unsigned int key_wparam, key_lparam, key_msg;
55 void keyboard_handler();
56 extern int WMKey_Handler_Ready;
57
58 void PumpMessages(void)
59 {
60   MSG msg;
61
62   while (PeekMessage(&msg,NULL,0,0,PM_REMOVE|PM_NOYIELD))
63   {
64         TranslateMessage(&msg);
65         DispatchMessage(&msg);
66   }
67 }
68
69 long PASCAL DescentWndProc(HWND hWnd,UINT message,
70                                                    WPARAM wParam,LPARAM lParam )
71 {
72   switch(message)
73   {
74
75    case WM_KEYDOWN:
76    case WM_KEYUP:
77         if (WMKey_Handler_Ready) {
78 //      key_wparam=wParam; key_lparam=lParam; key_msg=message;
79           keyboard_handler();
80         }
81         break;
82    case WM_MOUSEMOVE:
83    case WM_LBUTTONDOWN:
84    case WM_LBUTTONUP:
85    case WM_RBUTTONDOWN:
86    case WM_RBUTTONUP:
87    case WM_NCMOUSEMOVE:
88    case WM_NCLBUTTONDOWN:
89    case WM_NCLBUTTONUP:
90    case WM_NCRBUTTONDOWN:
91    case WM_NCRBUTTONUP:
92          break;
93    case WM_PALETTECHANGED:
94    case WM_PALETTEISCHANGING:
95    return 0;
96    case WM_ACTIVATEAPP:
97 //     Win32_Key_Hook(wParam);
98 // DPH: This doesn't work... no idea why not...
99          break;
100    case WM_DESTROY:
101          finiObjects();
102          PostQuitMessage(0);
103          break;
104   }
105   return DefWindowProc(hWnd,message,wParam,lParam);
106 }
107
108 void arch_init_start()
109 {
110         #ifndef NDEBUG
111         #ifdef _MSC_VER
112         if (FindArg("-memdbg"))
113                 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | /* _CRTDBG_CHECK_ALWAYS_DF | */
114                         /*_CRTDBG_CHECK_CRT_DF |*/
115                         _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
116         #endif
117         #endif
118 }
119
120 extern void key_init(void);
121 extern void mouse_init(void);
122 //added/changed 3/7/99 Owen Evans (next line)
123 extern void joy_init(int joyid);
124
125 void arch_init()
126 {
127         HRESULT             ddrval;
128
129         WNDCLASS wcDescentClass;
130
131         wcDescentClass.lpszClassName = "WinD1X";
132         wcDescentClass.hInstance     = hInst;
133         wcDescentClass.lpfnWndProc   = DescentWndProc;
134         wcDescentClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
135         wcDescentClass.hIcon         = LoadIcon(NULL, IDI_WINLOGO);
136         wcDescentClass.lpszMenuName  = NULL;
137         wcDescentClass.hbrBackground = NULL;
138         wcDescentClass.style         = CS_HREDRAW | CS_VREDRAW;
139         wcDescentClass.cbClsExtra    = 0;
140         wcDescentClass.cbWndExtra    = 0;
141
142         // Register the class
143         RegisterClass(&wcDescentClass);
144         g_hWnd = CreateWindowEx(0,
145                                   "WinD1X",
146                                   "Descent",
147                                   WS_OVERLAPPED | WS_BORDER,
148                                   0, 0,
149                                   GetSystemMetrics(SM_CXSCREEN),
150                                   GetSystemMetrics(SM_CYSCREEN),
151                                   NULL,
152                                   NULL,
153                                   hInst,
154                                   NULL
155                                   );
156
157         if (!g_hWnd) return; // CRAP!
158         ShowWindow(g_hWnd,SW_SHOWNORMAL);
159         UpdateWindow(g_hWnd);
160
161
162         ddrval=DirectDrawCreate(NULL,&lpDD,NULL);
163
164         if(ddrval!=DD_OK)
165         {
166                 fprintf(stderr,"DirectDrawCreate() failed!\n");
167                 abort();
168         }
169
170         if (FindArg("-semiwin"))
171         ddrval=IDirectDraw_SetCooperativeLevel(lpDD,g_hWnd,DDSCL_NORMAL);
172         else
173         {
174                 #ifndef NDEBUG
175                 ddrval=IDirectDraw_SetCooperativeLevel(lpDD,g_hWnd,
176                         DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);
177 #else
178                 ddrval=IDirectDraw_SetCooperativeLevel(lpDD,g_hWnd,
179                         DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);
180 #endif
181         }
182         
183         if (ddrval!=DD_OK)
184         {
185           fprintf(stderr,"SetCooperativeLevel() failed\n");
186           abort();
187         }
188
189         ShowCursor(FALSE);
190         mouse_hidden = 1;
191
192         SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
193
194         key_init();
195         mouse_init();
196 //added/changed 3/7/99 Owen Evans (next line)
197         joy_init(JOYSTICKID1);
198         printf("arch_init successfully completed\n");
199 }