]> icculus.org git repositories - btb/d2x.git/blob - arch/win32/init.c
use the orientation parameter of g3_draw_bitmap
[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 #include "resource.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.hIcon         = LoadIcon(hInst, MAKEINTRESOURCE(IDI_MAIN_ICON));
137         wcDescentClass.lpszMenuName  = NULL;
138         wcDescentClass.hbrBackground = NULL;
139         wcDescentClass.style         = CS_HREDRAW | CS_VREDRAW;
140         wcDescentClass.cbClsExtra    = 0;
141         wcDescentClass.cbWndExtra    = 0;
142
143         // Register the class
144         RegisterClass(&wcDescentClass);
145         g_hWnd = CreateWindowEx(0,
146                                   "WinD1X",
147                                   "Descent",
148                                   WS_OVERLAPPED | WS_BORDER,
149                                   0, 0,
150                                   GetSystemMetrics(SM_CXSCREEN),
151                                   GetSystemMetrics(SM_CYSCREEN),
152                                   NULL,
153                                   NULL,
154                                   hInst,
155                                   NULL
156                                   );
157
158         if (!g_hWnd) return; // CRAP!
159         ShowWindow(g_hWnd,SW_SHOWNORMAL);
160         UpdateWindow(g_hWnd);
161
162
163         ddrval=DirectDrawCreate(NULL,&lpDD,NULL);
164
165         if(ddrval!=DD_OK)
166         {
167                 fprintf(stderr,"DirectDrawCreate() failed!\n");
168                 abort();
169         }
170
171         if (FindArg("-semiwin"))
172         ddrval=IDirectDraw_SetCooperativeLevel(lpDD,g_hWnd,DDSCL_NORMAL);
173         else
174         {
175                 #ifndef NDEBUG
176                 ddrval=IDirectDraw_SetCooperativeLevel(lpDD,g_hWnd,
177                         DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);
178 #else
179                 ddrval=IDirectDraw_SetCooperativeLevel(lpDD,g_hWnd,
180                         DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);
181 #endif
182         }
183         
184         if (ddrval!=DD_OK)
185         {
186           fprintf(stderr,"SetCooperativeLevel() failed\n");
187           abort();
188         }
189
190         ShowCursor(FALSE);
191         mouse_hidden = 1;
192
193         SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
194
195         key_init();
196         mouse_init();
197 //added/changed 3/7/99 Owen Evans (next line)
198         joy_init(JOYSTICKID1);
199         printf("arch_init successfully completed\n");
200 }