1 // Windows video functions.
10 #include <Windows32/Errors.h>
19 //added 10/05/98 by Matt Mueller - make fullscreen mode optional
22 //removed 07/11/99 by adb - now option
23 ////added 02/20/99 by adb - put descent in a window, sort of. Needed for debugging
24 ////(needs a 256 color mode to be useful)
30 char *backbuffer = NULL;
32 int vid_installed = 0;
34 // Min without sideeffects.
36 #define inline __inline
40 inline static int min(int x, int y) { return x < y ? x : y; }
46 LPDIRECTDRAWSURFACE lpDDSPrimary;
47 LPDIRECTDRAWSURFACE lpDDSOne;
48 LPDIRECTDRAWPALETTE lpDDPal;
51 //added 02/20/99 by adb - put descent in a window, sort of. Needed for debugging
52 //(needs a 256 color mode to be useful)
55 void gr_palette_clear(); // Function prototype for vid_init;
58 static char *DDerror(int code)
62 /* case DDERR_GENERIC:
63 error = "Undefined error!";
66 error = "Exception encountered";
68 case DDERR_INVALIDOBJECT:
69 error = "Invalid object";
71 /* case DDERR_INVALIDPARAMS:
72 error = "Invalid parameters";
75 error = "Object not found";
77 case DDERR_INVALIDRECT:
78 error = "Invalid rectangle";
80 case DDERR_INVALIDCAPS:
81 error = "Invalid caps member";
83 case DDERR_INVALIDPIXELFORMAT:
84 error = "Invalid pixel format";
86 /* case DDERR_OUTOFMEMORY:
87 error = "Out of memory";
89 case DDERR_OUTOFVIDEOMEMORY:
90 error = "Out of video memory";
92 case DDERR_SURFACEBUSY:
93 error = "Surface busy";
95 case DDERR_SURFACELOST:
96 error = "Surface was lost";
98 case DDERR_WASSTILLDRAWING:
99 error = "DirectDraw is still drawing";
101 case DDERR_INVALIDSURFACETYPE:
102 error = "Invalid surface type";
104 case DDERR_NOEXCLUSIVEMODE:
105 error = "Not in exclusive access mode";
107 case DDERR_NOPALETTEATTACHED:
108 error = "No palette attached";
110 case DDERR_NOPALETTEHW:
111 error = "No palette hardware";
113 case DDERR_NOT8BITCOLOR:
114 error = "Not 8-bit color";
116 case DDERR_EXCLUSIVEMODEALREADYSET:
117 error = "Exclusive mode was already set";
119 case DDERR_HWNDALREADYSET:
120 error = "Window handle already set";
122 case DDERR_HWNDSUBCLASSED:
123 error = "Window handle is subclassed";
126 error = "No blit hardware";
128 case DDERR_IMPLICITLYCREATED:
129 error = "Surface was implicitly created";
131 case DDERR_INCOMPATIBLEPRIMARY:
132 error = "Incompatible primary surface";
134 case DDERR_NOCOOPERATIVELEVELSET:
135 error = "No cooperative level set";
137 case DDERR_NODIRECTDRAWHW:
138 error = "No DirectDraw hardware";
140 case DDERR_NOEMULATION:
141 error = "No emulation available";
144 error = "No flip hardware";
146 case DDERR_NOTFLIPPABLE:
147 error = "Surface not flippable";
149 case DDERR_PRIMARYSURFACEALREADYEXISTS:
150 error = "Primary surface already exists";
152 case DDERR_UNSUPPORTEDMODE:
153 error = "Unsupported mode";
155 case DDERR_WRONGMODE:
156 error = "Surface created in different mode";
158 /* case DDERR_UNSUPPORTED:
159 error = "Operation not supported";
178 ddsd.dwSize=sizeof(ddsd);
179 ddrval=IDirectDrawSurface_Lock(lpDDSPrimary,NULL,&ddsd,0,NULL);
181 printf("lock failed, %s\n", DDerror(ddrval));
182 Assert(ddrval==DD_OK);
185 j=backbuffer; k=ddsd.lpSurface;
186 h=grd_curscreen->sc_canvas.cv_bitmap.bm_h;
187 w=grd_curscreen->sc_canvas.cv_bitmap.bm_w;
188 for (i=0; i<h; i++) {
190 j+=grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize;
192 #ifdef NONAMELESSUNION
198 IDirectDrawSurface_Unlock(lpDDSPrimary,NULL);
201 ddrval=IDirectDrawSurface_Flip(lpDDSPrimary,NULL,0);
204 printf("Flip was successful\n");
207 if (ddrval == DDERR_SURFACELOST)
209 printf("surface was lost\n");
210 ddrval=IDirectDrawSurface_Restore(lpDDSPrimary);
213 printf("restore failed\n");
217 if (ddrval == DDERR_WASSTILLDRAWING )
219 printf("was still drawing\n");
226 int vid_set_mode(uint32_t mode)
229 // DDSURFACEDESC DDSDesc;
240 if(lpDDSPrimary!=NULL)
242 IDirectDrawSurface_Release(lpDDSPrimary);
246 if (backbuffer) free(backbuffer);
249 //changed 07/11/99 by adb - nonfullscreen mode now option
250 if (!FindArg("-semiwin"))
252 ddrval=IDirectDraw_SetDisplayMode(lpDD,w,h,8);
256 fprintf(stderr, "Hmmm... I had a problem changing screen modes... is %ix%ix8 supported? If so, try again... :-( %s\n",w,h, DDerror(ddrval));
257 return -3; // This is **not** good...
261 ddsd.dwSize=sizeof(ddsd);
262 ddsd.dwFlags=DDSD_CAPS /*| DDSD_BACKBUFFERCOUNT*/;
263 ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE /*| DDSCAPS_FLIP | DDSCAPS_COMPLEX*/;
264 //ddsd.dwBackBufferCount=1;
266 ddrval=IDirectDraw_CreateSurface(lpDD,&ddsd,&lpDDSPrimary,NULL);
273 memset(&ddcaps,0,sizeof(ddcaps));
274 // ddcaps.dwSize=sizeof(ddcaps);
275 ddcaps.dwCaps=DDSCAPS_BACKBUFFER;
277 ddrval=IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary,&ddcaps,&lpDDSOne);
278 Assert(ddrval==DD_OK);
285 ddrval=IDirectDraw_CreatePalette(lpDD,DDPCAPS_8BIT | DDPCAPS_INITIALIZE | DDPCAPS_ALLOW256,pe,&lpDDPal,NULL);
286 Assert(ddrval==DD_OK);
292 IDirectDrawSurface_SetPalette(lpDDSPrimary,lpDDPal);
293 // IDirectDrawSurface_SetPalette(lpDDSOne,lpDDPal);
297 backbuffer = malloc(w*h);
298 memset(backbuffer, 0, w*h);
299 gr_init_screen(BM_LINEAR, w, h, 0 0, w, (unsigned char *)backbuffer);
301 ddsd.dwSize=sizeof(ddsd);
302 ddrval=IDirectDrawSurface_Lock(lpDDSPrimary,NULL,&ddsd,0,NULL);
308 memset(ddsd.lpSurface,0,w*h); // Black the canvas out to stop nasty kludgy display
309 IDirectDrawSurface_Unlock(lpDDSPrimary,NULL);
311 gamefont_choose_game_font(w,h);
313 printf("Successfully completed set_mode\n");
319 void Win32_DoSetPalette (PALETTEENTRY *rgpe)
321 IDirectDraw_WaitForVerticalBlank(lpDD,DDWAITVB_BLOCKBEGIN,NULL);
322 IDirectDrawPalette_SetEntries(lpDDPal,0,0,256,rgpe);
325 void Win32_DoGetPalette (PALETTEENTRY *rgpe)
327 IDirectDrawPalette_GetEntries(lpDDPal,0,0,256,rgpe);
330 //added 07/11/99 by adb for d3d
331 void Win32_MakePalVisible(void)
334 //end additions - adb
337 void vid_close(void);
340 int vid_init(int mode)
343 // Only do this function once!
344 if (vid_installed == 1)
348 if ((retcode = vid_set_mode(mode))) {
353 // added on 980913 by adb to add cleanup
355 // end changes by adb
363 if (vid_installed == 1)