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