]> icculus.org git repositories - btb/d2x.git/blob - arch/win32/win32.c
Rename include/error.h to include/dxxerror.h
[btb/d2x.git] / arch / win32 / win32.c
1 #define WIN32_LEAN_AND_MEAN
2 #include <windows.h>
3 #include <stdio.h>
4
5 #include "gr.h"
6 #include "u_mem.h"
7 #include "timer.h"
8 #include "dxxerror.h"
9 #include "d3dhelp.h"
10 #include "game.h"
11 #include "gauges.h"
12 #include "args.h"
13 #include "gamefont.h"
14
15 #ifndef NDEBUG
16 #ifdef _MSC_VER
17 #include <crtdbg.h>
18 #endif
19 #endif
20
21
22 void gr_linear_rep_movsdm(ubyte *src, ubyte *dest, int num_pixels);
23 void InitMain();
24 void show_dd_error(HRESULT hr, char *loc);
25 void GetDDErrorString(HRESULT hr, char *buf, int size);
26
27 void BlitToPrimary(HDC hSrcDC);
28 void BlitToPrimaryRect(HDC hSrcDC, int x, int y, int w, int h, 
29         unsigned char *dst);
30 extern HWND g_hWnd;
31
32 void key_init(void);
33 void mouse_init(void);
34
35
36 unsigned char *createdib(void);
37
38 static unsigned char *backbuffer, *screenbuffer;
39 static int screensize;
40 static HBITMAP screen_bitmap;
41
42 void arch_init_start()
43 {
44         #ifndef NDEBUG
45         #ifdef _MSC_VER
46         if (FindArg("-memdbg"))
47                 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF | 
48                         /* _CRTDBG_CHECK_CRT_DF | */
49                         _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
50         #endif
51         #endif
52 }
53
54 void arch_init()
55 {
56         //SetPriorityClass (GetCurrentProcess(),HIGH_PRIORITY_CLASS);
57         InitMain ();
58
59         timer_init ();
60         key_init();
61         mouse_init();
62
63         printf("arch_init successfully completed\n");
64 }
65
66
67 int vid_installed = 0;
68
69
70 int vid_check_mode(uint32_t mode)
71 {
72         if (mode == SM(320, 200))
73                 return 0;
74
75         return 11;
76 }
77
78
79 int vid_set_mode(uint32_t mode)
80 {
81         unsigned int w,h,t,r;
82
83         if (mode == SM_ORIGINAL)
84                 return 0;
85
86         switch (mode)
87         {
88         case SM(320,200):
89                 w = 320; r = 320; h = 200; t=BM_LINEAR;//BM_DIRECTX;;
90                 break;
91         default:
92                 return 1;
93         }
94
95         gr_palette_clear();
96
97         if (!(backbuffer = createdib()))
98                 return 1;
99
100         gr_init_screen(BM_LINEAR, w, h, 0, 0, w, backbuffer);
101
102         gamefont_choose_game_font(w,h);
103         
104         return 0;
105 }
106
107
108 int vid_init(int mode)
109 {
110         //int org_gamma;
111         int retcode;
112         //HRESULT hr;
113
114         // Only do this function once!
115         if (vid_installed == 1)
116                 return -1;
117
118         // Set the mode.
119         if ((retcode = vid_set_mode(mode)))
120                 return retcode;
121
122         // Set flags indicating that this is installed.
123         vid_installed = 1;
124
125         return 0;
126 }
127
128 void gr_upixel( int x, int y )
129 {
130         gr_bm_upixel(&grd_curcanv->cv_bitmap, x, y, (unsigned char)COLOR);
131 #if 0
132         grs_bitmap * bm = &grd_curcanv->cv_bitmap;
133         Win32_Rect (
134                 //x + bm->bm_x, y + bm->bm_y,
135                 //x + bm->bm_x, y + bm->bm_y,
136                 x, y, x, y,
137                 bm->bm_data, COLOR);
138 #endif
139 }
140
141 void gr_bm_upixel( grs_bitmap * bm, int x, int y, unsigned char color )
142 {
143         switch (bm->bm_type)
144         {
145         case BM_LINEAR:
146                 bm->bm_data[ bm->bm_rowsize*y+x ] = color;
147                 break;
148
149         case BM_DIRECTX:
150                 {
151                         unsigned char *p = gr_current_pal + color * 3;
152                 Win32_Rect (
153                         x, y, x, y,
154                         //x + bm->bm_x, y + bm->bm_y,
155                         //x + bm->bm_x, y + bm->bm_y,
156                                 (int)bm->bm_data, color);
157                 }
158                 break;
159
160         default:
161                 Assert (FALSE);
162                 break;
163         }
164 }
165
166 RGBQUAD w32lastrgb[256];
167
168
169 void vid_update()
170 {
171         HDC hdc;
172         unsigned char *p;
173         int i;
174
175         p = gr_current_pal;
176         for (i = 0; i < 256; i++) {
177                 w32lastrgb[i].rgbRed = *p++ * 4;
178                 w32lastrgb[i].rgbGreen = *p++ * 4;
179                 w32lastrgb[i].rgbBlue = *p++ * 4;
180         }
181         hdc = CreateCompatibleDC(NULL);
182         SelectObject(hdc, screen_bitmap);
183         SetDIBColorTable(hdc, 0, 256, w32lastrgb);
184
185         BlitToPrimary(hdc);
186         DeleteDC(hdc);
187 }
188
189 void show_dd_error(HRESULT hr, char *loc)
190 {
191         char buf[512], len;
192
193         strcpy(buf, loc);
194         len = strlen(buf);
195         #ifdef NDEBUG
196         sprintf(buf + len, "%x", hr);
197         #else
198         GetDDErrorString(hr, buf + len, sizeof(buf) - len);
199         #endif
200         Error(buf);
201 }
202
203 unsigned char *createdib(void)
204 {
205         BITMAPINFO *bmHdr;
206         HDC hdc;
207         int i;
208         unsigned char *p;
209         unsigned char *buffer;
210         
211         if (!(bmHdr = malloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD))))
212                 return NULL;
213
214         memset(bmHdr, 0, sizeof(*bmHdr));
215         bmHdr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
216         bmHdr->bmiHeader.biWidth = grd_curscreen->sc_canvas.cv_bitmap.bm_w;
217         bmHdr->bmiHeader.biHeight = -grd_curscreen->sc_canvas.cv_bitmap.bm_h;
218         bmHdr->bmiHeader.biPlanes = 1;
219         bmHdr->bmiHeader.biBitCount = 8;
220         bmHdr->bmiHeader.biCompression = BI_RGB;
221
222         p = gr_current_pal;
223         for (i = 0; i < 256; i++) {
224                 #if 0
225                 ((short *)bmHdr->bmiColors)[i] = i;
226                 #else
227                 bmHdr->bmiColors[i].rgbRed = (*p++) << 2;
228                 bmHdr->bmiColors[i].rgbGreen = (*p++) << 2;
229                 bmHdr->bmiColors[i].rgbBlue = (*p++) << 2;
230                 bmHdr->bmiColors[i].rgbReserved = 0;
231                 #endif
232         }
233         hdc = CreateCompatibleDC(NULL);
234         if (!(screen_bitmap = CreateDIBSection(hdc, bmHdr, DIB_RGB_COLORS,
235                 &buffer, NULL, 0))) {
236                         int err = GetLastError();
237                         char buf[256];
238                         sprintf(buf, "CreateDISection():%d", err);
239                         MessageBox(g_hWnd, buf, NULL, MB_OK);
240         }
241         DeleteDC(hdc);
242         free(bmHdr);
243         return buffer;
244 }
245
246 void Win32_BlitLinearToDirectX_bm(grs_bitmap *bm, int sx, int sy, 
247         int w, int h, int dx, int dy, int masked) {
248         HDC hdc;
249         unsigned char *src, *dest, *rle_w;
250         int i;
251
252         dest = backbuffer + dy * 320 + dx;
253         
254         if (bm->bm_flags & BM_FLAG_RLE) {
255                 src = bm->bm_data + 4 + bm->bm_h;
256                 rle_w = bm->bm_data + 4;
257                 while (sy--)
258                         src += (int)*rle_w++;
259                 if (masked) {
260                         for (i = h; i--; ) {
261                                 gr_rle_expand_scanline_masked(dest, src, sx, sx + w - 1);
262                                 src += (int)*rle_w++;
263                                 dest += 320;
264                         }
265                 } else {
266                         for (i = h; i--; ) {
267                                 gr_rle_expand_scanline(dest, src, sx, sx + w - 1);
268                                 src += (int)*rle_w++;
269                                 dest += 320;
270                         }
271                 }
272         } else {
273                 src = bm->bm_data + sy * bm->bm_rowsize + sx;
274                 if (masked) {
275                         for (i = h; i--; ) {
276                                 gr_linear_rep_movsdm(src, dest, w);
277                                 src += bm->bm_rowsize;
278                                 dest += 320;
279                         }
280                 } else {
281                         for (i = h; i--; ) {
282                                 gr_linear_movsd(src, dest, w);
283                                 src += bm->bm_rowsize;
284                                 dest += 320;
285                         }
286                 }
287         }
288         hdc = CreateCompatibleDC(NULL);
289         SelectObject(hdc, screen_bitmap);
290         SetDIBColorTable(hdc, 0, 256, w32lastrgb);
291         BlitToPrimaryRect(hdc, dx, dy, w, h, grd_curcanv->cv_bitmap.bm_data);
292         DeleteDC(hdc);
293 }
294
295 void Win32_MakePalVisible(void) {
296         vid_update();
297 }
298
299 void Win32_InvalidatePages(void) {
300         reset_cockpit();
301         init_gauges();
302 }