Cygwin support, using SDL.
[btb/d2x.git] / unused / win95 / winapp.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14
15 #pragma off (unreferenced)
16 static char rcsid[] = "$Id: winapp.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
17 #pragma on (unreferenced)
18
19
20 #define _WIN32
21 #define WIN32_LEAN_AND_MEAN
22
23 #include <windows.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include "mono.h"
27 #include "winapp.h"
28 #include "dd.h"
29
30 static HWND hWAppWnd = 0;
31 static HINSTANCE hWAppInstance = 0;
32
33
34 void SetLibraryWinInfo(HWND hWnd, HINSTANCE hInstance)
35 {
36         hWAppWnd = hWnd;
37         hWAppInstance = hInstance;
38 }
39
40
41 HWND GetLibraryWindow(void)
42 {
43         return hWAppWnd;
44 }
45
46
47 HINSTANCE GetLibraryWinInstance(void)
48 {
49         return hWAppInstance;   
50 }
51
52
53 //      Console Functions
54
55 static BOOL ConInit=FALSE;
56 static HANDLE ConStdOut;
57 static HANDLE ConStdIn;
58
59 void cinit()
60 {
61         AllocConsole();
62         ConStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
63         ConStdIn = GetStdHandle(STD_INPUT_HANDLE);
64         ConInit = TRUE;
65 }
66
67 void cclose()
68 {
69         ConInit = FALSE;
70 }
71
72
73 void cprintf(char *text, ...)
74 {
75         char buffer[1000];
76         va_list args;
77         DWORD size;
78
79         if (!ConInit) return;   
80
81         va_start(args, text );
82         vsprintf(buffer,text,args);
83
84         WriteConsole(ConStdOut, buffer, strlen(buffer), &size, NULL);
85 }
86
87
88 int cgetch()
89 {
90         char buffer[3];
91         DWORD chread;
92
93         ReadConsole(ConStdIn, buffer, sizeof(TCHAR), &chread, NULL);
94         return (int)buffer[0];
95 }
96         
97
98 //      Log Functions
99
100 static FILE *LogFile=NULL;
101
102 void loginit(char *name)
103 {
104         if (LogFile) return;
105
106         LogFile = fopen(name, "wt");
107         if (!LogFile) {
108                 MessageBox(NULL, "Unable to create descentw.log!", "Descent II Beta Error", MB_OK);
109                 return;
110         }
111
112         fprintf(LogFile, "%s begins.\n--------------------------------------\n\n",name);
113         fflush(LogFile);
114 }
115
116
117 void logclose()
118 {
119         if (!LogFile) return;
120
121         fprintf(LogFile, "\n-----------------------------------------\nlog ends.\n");
122
123         fclose(LogFile);
124         
125         LogFile = NULL;
126 }       
127
128
129 void logentry(char *format, ...)
130 {
131         char buffer[256];
132         va_list args;
133
134         if (!LogFile) return;   
135
136         va_start(args, format );
137         vsprintf(buffer, format ,args);
138
139         mprintf((0, "%s", buffer));
140         fprintf(LogFile, "%s", buffer);
141         fflush(LogFile);
142 }
143
144
145 //      Disk stuff
146
147 unsigned int GetFreeDiskSpace()
148 {
149         DWORD sec_per_cluster,
150                         bytes_per_sec,
151                         free_clusters,
152                         total_clusters;
153
154         if (!GetDiskFreeSpace(
155                 NULL,
156                 &sec_per_cluster,
157                 &bytes_per_sec,
158                 &free_clusters,
159                 &total_clusters)) return 0x7fffffff;
160
161         return (uint)(free_clusters * sec_per_cluster * bytes_per_sec);
162 }
163
164
165 //      Clipboarding functions
166
167 HBITMAP win95_screen_shot()
168 {
169         HDC hdcscreen;
170         HDC hdccapture;
171         HBITMAP screen_bitmap;
172         HBITMAP old_bitmap;
173
174 //@@    hdcscreen = CreateDC("DISPLAY", NULL, NULL, NULL);
175         IDirectDrawSurface_GetDC(dd_grd_screencanv->lpdds, &hdcscreen);
176         hdccapture = CreateCompatibleDC(hdcscreen);
177         
178         screen_bitmap = CreateCompatibleBitmap(hdcscreen, GRMODEINFO(w), GRMODEINFO(h));
179
180         if (!screen_bitmap) {
181                 DeleteDC(hdccapture);
182                 IDirectDrawSurface_ReleaseDC(dd_grd_screencanv->lpdds, hdcscreen);
183         //@@    DeleteDC(hdcscreen);
184                 return NULL;
185         }
186
187         old_bitmap = SelectObject(hdccapture, screen_bitmap);
188         if (!old_bitmap) {
189                 DeleteObject(screen_bitmap);
190                 DeleteDC(hdccapture);
191                 IDirectDrawSurface_ReleaseDC(dd_grd_screencanv->lpdds, hdcscreen);
192                 //@@    DeleteDC(hdcscreen);
193                 return NULL;
194         }
195         
196         if (!BitBlt(hdccapture, 0,0,GRMODEINFO(w), GRMODEINFO(h), hdcscreen, 0, 0,
197                         SRCCOPY)) {
198                 SelectObject(hdccapture, old_bitmap);
199                 DeleteObject(screen_bitmap);
200                 DeleteDC(hdccapture);
201                 IDirectDrawSurface_ReleaseDC(dd_grd_screencanv->lpdds, hdcscreen);
202         //@@    DeleteDC(hdcscreen);
203                 return NULL;
204         }
205
206         screen_bitmap = SelectObject(hdccapture, old_bitmap);
207         DeleteDC(hdccapture);
208
209         IDirectDrawSurface_ReleaseDC(dd_grd_screencanv->lpdds, hdcscreen);
210 //@@    DeleteDC(hdcscreen);
211
212         return screen_bitmap;
213 }               
214
215
216 HANDLE win95_dib_from_bitmap(HBITMAP hbm)
217 {
218         HANDLE hMem,hX;
219         BITMAP bm;
220         BITMAPINFOHEADER bi;
221         BITMAPINFOHEADER *pbi;
222         RGBQUAD *rgbp;
223         HDC hdc;
224         char grpal[768];
225         int i;
226         HRESULT hRes;
227
228         GetObject(hbm, sizeof(bm), &bm);
229
230         memset(&bi, 0, sizeof(bi));
231         
232         bi.biSize = sizeof(BITMAPINFOHEADER);
233         bi.biWidth = bm.bmWidth;
234         bi.biHeight = bm.bmHeight;
235         bi.biPlanes = 1;
236         bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
237         bi.biCompression = BI_RGB;
238
239         hMem = GlobalAlloc(GHND, bi.biSize + 256 * sizeof(RGBQUAD));
240         if (!hMem) {
241                 mprintf((1, "WINAPP: Unable to allocate memory for DIB.\n"));
242                 return NULL;
243         }
244
245         pbi = (BITMAPINFOHEADER *)GlobalLock(hMem);
246         *pbi = bi;
247
248         hRes = IDirectDrawSurface_GetDC(dd_grd_screencanv->lpdds, &hdc);
249         if (hRes != DD_OK) {
250                 GlobalFree(hMem);
251                 mprintf((1, "WINAPP: Unable to get GDI DC for DIB creation.\n"));
252                 return NULL;
253         }
254         GetDIBits(hdc, hbm, 0, bi.biHeight, NULL, (BITMAPINFO *)pbi, DIB_RGB_COLORS);
255         bi = *pbi;
256         GlobalUnlock(hMem); 
257         
258         if (!bi.biSizeImage) {
259                 bi.biSizeImage = bi.biWidth * bi.biHeight;
260         }               
261         
262         hX = GlobalReAlloc(hMem, bi.biSize + 256*sizeof(RGBQUAD) + bi.biSizeImage,0);
263         if (!hX) {
264                 GlobalFree(hMem);
265                 IDirectDrawSurface_ReleaseDC(dd_grd_screencanv->lpdds, hdc);
266                 mprintf((1, "WINAPP: Unable to reallocate mem for DIB.\n"));
267                 return NULL;
268         }
269         hMem = hX;
270
271         pbi = GlobalLock(hMem);
272         
273         if (!GetDIBits(hdc, hbm, 0, 
274                         pbi->biHeight, 
275                         (LPSTR)pbi + pbi->biSize + 256*sizeof(RGBQUAD),
276                         (BITMAPINFO *)pbi, DIB_RGB_COLORS)) {
277                 GlobalUnlock(hMem);
278                 GlobalFree(hMem);
279                 IDirectDrawSurface_ReleaseDC(dd_grd_screencanv->lpdds, hdc);
280                 mprintf((1, "WINAPP: GetDIBits was unable to get needed info.\n"));
281                 return NULL;
282         }
283         bi = *pbi;
284
285         IDirectDrawSurface_ReleaseDC(dd_grd_screencanv->lpdds, hdc);
286         
287         rgbp = (RGBQUAD *)(((LPSTR)pbi + pbi->biSize));
288
289         gr_palette_read(grpal);
290         for (i = 0; i < 256; i++) 
291         {
292                 rgbp[i].rgbRed = grpal[i*3]<<2;
293                 rgbp[i].rgbGreen = grpal[i*3+1]<<2;
294                 rgbp[i].rgbBlue = grpal[i*3+2]<<2;
295                 rgbp[i].rgbReserved = 0;
296         }                       
297
298         GlobalUnlock(hMem);
299
300         return hMem;
301 }
302
303                 
304