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