]> icculus.org git repositories - btb/d2x.git/blob - unused/win95/ddgfx.c
Rename include/error.h to include/dxxerror.h
[btb/d2x.git] / unused / win95 / ddgfx.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
16 #define WIN95
17 #define _WIN32
18 #define WIN32_LEAN_AND_MEAN
19
20 #include <windows.h>
21 #include <stdlib.h>
22 #include "win\ddraw.h"
23 #include "dd.h"
24
25
26 #include "fix.h"
27 #include "gr.h"
28 #include "rle.h"
29 #include "mono.h"
30 #include "dxxerror.h"
31 #include "args.h"
32 #include "winapp.h"
33
34
35 #define MAX_GFX_BITMAPS         512
36
37
38 // Data!
39
40 static BOOL dd_gfx_initialized = 0;
41 static unsigned short gfxBitmapHandleCur = 1;
42
43 static struct _gfxBitmap {
44         LPDIRECTDRAWSURFACE lpdds;
45         int w;
46         int h;
47 } gfxBitmap[MAX_GFX_BITMAPS];
48
49
50 //      Functions
51
52 int dd_gfx_init()
53 {
54         int i;
55         
56         if (dd_gfx_initialized) return 1;
57
58         for (i = 0; i < MAX_GFX_BITMAPS; i++)
59                 gfxBitmap[i].lpdds = NULL;
60
61         gfxBitmapHandleCur = 1;
62
63         atexit(dd_gfx_close);
64
65
66         if (FindArg("-disallowgfx")) dd_gfx_initialized = 0;
67         else if (FindArg("-forcegfx")) dd_gfx_initialized = 1;
68         else {
69                 if (ddDriverCaps.hwcolorkey) 
70                         logentry("Card supports HW colorkeying.\n");
71
72                 if (ddDriverCaps.hwbltstretch)
73                         logentry("Card supports HW bitmap stretching.\n");
74                 
75                 if (ddDriverCaps.hwcolorkey) dd_gfx_initialized = 1;
76                 else dd_gfx_initialized = 0;
77         }
78
79         return 0;
80 }
81
82
83 int dd_gfx_close()
84 {
85         int i;
86
87         if (!dd_gfx_initialized) return 1;
88
89         for (i = 0; i < MAX_GFX_BITMAPS; i++)
90         {
91                 if (gfxBitmap[i].lpdds)
92                         IDirectDrawSurface_Release(gfxBitmap[i].lpdds);
93         }
94         
95         dd_gfx_initialized =0;
96
97         return 0;
98 }
99
100
101 unsigned short dd_gfx_createbitmap2D(int w, int h)
102 {
103         int i, force;
104         unsigned short handle;
105
106         if (!dd_gfx_initialized) return 0;
107
108         for (i = 0; i < MAX_GFX_BITMAPS; i++)
109                 if (!gfxBitmap[i].lpdds) {
110                         handle = (unsigned short)(i);
111                         break;
112                 }
113
114         if (i == MAX_GFX_BITMAPS) return 0;
115
116 //      Only do this if we can benefit from it.
117 //      if (ddDriverCaps.hwcolorkey && ddDriverCaps.hwbltstretch) 
118
119         if (FindArg("-forcegfx"))       force = 1;
120         else force = 0;
121
122         if (ddDriverCaps.hwcolorkey || force) 
123         {
124                 LPDIRECTDRAWSURFACE lpdds;
125                 DDSURFACEDESC ddsd;
126                 HRESULT ddresult;
127                 DDCOLORKEY ddck;
128
129                 memset(&ddsd, 0, sizeof(ddsd));
130                 ddsd.dwSize = sizeof(ddsd);
131                 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
132                 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
133                 ddsd.dwWidth = w;
134                 ddsd.dwHeight = h;
135
136                 ddresult = IDirectDraw_CreateSurface(_lpDD, &ddsd, &lpdds, NULL);
137
138                 if (ddresult != DD_OK) {
139                         mprintf((0, "DDGFX: Failed to create vidmem 2d bitmap (%x).\n", ddresult));
140                         return 0;
141                 }
142       ddck.dwColorSpaceLowValue = TRANSPARENCY_COLOR;
143       ddck.dwColorSpaceHighValue = TRANSPARENCY_COLOR;
144       IDirectDrawSurface_SetColorKey(lpdds, DDCKEY_SRCBLT, &ddck);
145
146                 gfxBitmap[handle].lpdds = lpdds;
147                 gfxBitmap[handle].w = w;
148                 gfxBitmap[handle].h = h; 
149                 handle++;                                                                       // Make it a valid handle       
150         }
151         else handle = 0;
152
153         return handle;
154 }       
155
156
157 int dd_gfx_loadbitmap2D(unsigned short handle, void *buf, int rleflag)
158 {
159         HRESULT ddresult;
160         DDSURFACEDESC ddsd;
161         char *ptr;
162         int pitch, i;
163         grs_bitmap tbmp;
164
165         Assert(handle != 0);
166
167         if (!dd_gfx_initialized) return 0;
168
169         handle--;                                                                       // Convert to valid handle
170
171 RedoLock:
172
173         memset(&ddsd, 0, sizeof(ddsd));
174         ddsd.dwSize = sizeof(ddsd);
175
176         ddresult = IDirectDrawSurface_Lock(gfxBitmap[handle].lpdds, NULL,       &ddsd, DDLOCK_WAIT, NULL);
177         if (ddresult != DD_OK) {
178                 if (ddresult == DDERR_SURFACELOST) {
179                         ddresult = IDirectDrawSurface_Restore(gfxBitmap[handle].lpdds);
180                         if (ddresult != DD_OK) {
181                                 if (ddresult != DDERR_WRONGMODE) {
182                                         logentry("DDGFX::Restore::Surface err: %x\n", ddresult);                
183                                         return 1;
184                                 }
185                                 else {
186                                         LPDIRECTDRAWSURFACE lpdds;
187                                         DDCOLORKEY ddck;
188
189                                         IDirectDrawSurface_Release(lpdds);
190                                         gfxBitmap[handle].lpdds = NULL;
191
192                                         memset(&ddsd, 0, sizeof(ddsd));
193                                         ddsd.dwSize = sizeof(ddsd);
194                                         ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
195                                         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
196                                         ddsd.dwWidth = gfxBitmap[handle].w;
197                                         ddsd.dwHeight = gfxBitmap[handle].h;
198
199                                         ddresult = IDirectDraw_CreateSurface(_lpDD, &ddsd, &lpdds, NULL);
200
201                                         if (ddresult != DD_OK) {
202                                                 mprintf((0, "DDGFX: Failed to restore vidmem 2d bitmap.\n"));
203                                                 return 1;
204                                         }
205                                 ddck.dwColorSpaceLowValue = TRANSPARENCY_COLOR;
206                                 ddck.dwColorSpaceHighValue = TRANSPARENCY_COLOR;
207                                 IDirectDrawSurface_SetColorKey(lpdds, DDCKEY_SRCBLT, &ddck);
208                                         gfxBitmap[handle].lpdds = lpdds;
209                                 }
210                         }
211                         
212                         goto RedoLock;
213                 }
214                 else {
215                         mprintf((0, "DDGFX:Unable to lock surface!!\n"));
216                         return 1;
217                 }
218         }
219
220 //      Locked!
221         ptr = ddsd.lpSurface;
222         pitch = ddsd.lPitch;    
223
224         gr_init_bitmap(&tbmp, BM_LINEAR, 0,0,gfxBitmap[handle].w, gfxBitmap[handle].h, gfxBitmap[handle].w, buf);
225         if (rleflag) tbmp.bm_flags = BM_FLAG_RLE;
226
227         for(i = 0; i < gfxBitmap[handle].h; i++) 
228         {
229                 extern ubyte scale_rle_data[];
230                 if (rleflag) {
231                         decode_row(&tbmp,i);
232                         memcpy(ptr+(i*pitch), scale_rle_data, gfxBitmap[handle].w);
233                 } 
234                 else memcpy(ptr+(i*pitch), (ubyte*)(buf)+(i*gfxBitmap[handle].w), gfxBitmap[handle].w);
235         }
236         
237         IDirectDrawSurface_Unlock(gfxBitmap[handle].lpdds, ptr);
238
239 //      Unlocked...
240
241         return 0;
242 }
243
244         
245 int dd_gfx_destroybitmap2D(unsigned short handle)
246 {
247         if (!dd_gfx_initialized) return 0;
248
249         if (handle == 0) return 1;
250         handle--;                                                                       // Convert to valid handle
251
252         if (gfxBitmap[handle].lpdds) {
253                 IDirectDrawSurface_Release(gfxBitmap[handle].lpdds);
254                 gfxBitmap[handle].lpdds = NULL;
255                 return 0;
256         }
257         else return 1;
258 }               
259
260
261 int dd_gfx_resetbitmap2Dcache()
262 {
263         int i;
264
265         if (!dd_gfx_initialized) return 0;
266
267         for (i = 0; i < MAX_GFX_BITMAPS; i++)
268                 dd_gfx_destroybitmap2D(i+1);
269
270         return 0;
271 }
272                 
273                 
274 void dd_gfx_blt2D(unsigned short handle, int x, int y, int x2, int y2,
275                                                 fix u0, fix v0, fix u1, fix v1)
276 {
277         RECT drect, srect;
278
279         if (!dd_gfx_initialized) return;
280
281         Assert(handle != 0);
282         handle--;                                                                       // Convert to valid handle
283
284         SetRect(&drect, x,y,x2,y2);
285         SetRect(&srect, f2i(u0), f2i(v0), f2i(u1), f2i(v1));
286
287         IDirectDrawSurface_Blt(dd_grd_curcanv->lpdds, &drect,
288                                         gfxBitmap[handle].lpdds, 
289                                         &srect, DDBLT_WAIT| DDBLT_KEYSRC,
290                                         NULL);
291 }