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.
15 #pragma off (unreferenced)
16 static char rcsid[] = "$Id: ddgr.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
17 #pragma on (unreferenced)
22 #define WIN32_LEAN_AND_MEAN
38 // ----------------------------------------------------------------------------
39 int dd_gr_initialized = 0;
41 dd_grs_canvas *dd_grd_screencanv = NULL;
42 dd_grs_canvas *dd_grd_backcanv = NULL;
43 dd_grs_canvas *dd_grd_curcanv = NULL;
45 static int ddgr_atexit_called = 0;
47 extern int _DDLockCounter;
48 extern BOOL _DDSysMemSurfacing;
49 extern int W95OldDisplayMode;
51 extern RECT ViewportRect; // Superhack! (from descentw.c. Too much trouble
55 // dd_gr_create_canvas
56 // ----------------------------------------------------------------------------
57 dd_grs_canvas *dd_gr_create_canvas(int w, int h)
63 ddnew = (dd_grs_canvas *)malloc( sizeof(dd_grs_canvas) );
64 ddnew->xoff = ddnew->yoff = 0;
68 new->cv_bitmap.bm_x = 0;
69 new->cv_bitmap.bm_y = 0;
70 new->cv_bitmap.bm_w = w;
71 new->cv_bitmap.bm_h = h;
72 new->cv_bitmap.bm_flags = 0;
73 new->cv_bitmap.bm_type = BM_LINEAR;
74 new->cv_bitmap.bm_rowsize = 0;
75 new->cv_bitmap.bm_data = NULL;
80 new->cv_font_fg_color = 0;
81 new->cv_font_bg_color = 0;
83 ddnew->lpdds = DDCreateSurface(w, h, 0);
85 Error("dd_gr_create_canvas: Unable to create DD Surface");
86 ddnew->lock_count = 0;
88 IDirectDrawSurface_GetCaps(ddnew->lpdds, &ddsc);
90 if (ddDriverCaps.offscreen.sysmem) ddnew->sram = 1;
97 void dd_gr_init_canvas(dd_grs_canvas *canv, int pixtype, int w, int h)
102 canv->xoff = canv->yoff = 0;
106 new->cv_bitmap.bm_x = 0;
107 new->cv_bitmap.bm_y = 0;
108 new->cv_bitmap.bm_w = w;
109 new->cv_bitmap.bm_h = h;
110 new->cv_bitmap.bm_flags = 0;
111 new->cv_bitmap.bm_type = BM_LINEAR;
112 new->cv_bitmap.bm_rowsize = 0;
113 new->cv_bitmap.bm_data = NULL;
116 new->cv_drawmode = 0;
118 new->cv_font_fg_color = 0;
119 new->cv_font_bg_color = 0;
121 canv->lpdds = DDCreateSurface(w,h,0);
123 Error("dd_gr_create_canvas: Unable to create DD Surface");
125 canv->lock_count = 0;
126 IDirectDrawSurface_GetCaps(canv->lpdds, &ddsc);
128 if (ddsc.dwCaps & DDSCAPS_VIDEOMEMORY) canv->sram = 0;
129 else if (ddDriverCaps.offscreen.sysmem) canv->sram = 1;
135 void dd_gr_reinit_canvas(dd_grs_canvas *canv)
139 LPDIRECTDRAWSURFACE lpdds;
141 ddsd.dwSize = sizeof(ddsd);
142 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT;
143 IDirectDrawSurface_GetSurfaceDesc(canv->lpdds, &ddsd);
147 memset(canv, 0, sizeof(dd_grs_canvas));
150 canv->lock_count = 0;
151 canv->xoff = canv->yoff = 0;
155 new->cv_bitmap.bm_x = 0;
156 new->cv_bitmap.bm_y = 0;
157 new->cv_bitmap.bm_w = ddsd.dwWidth;
158 new->cv_bitmap.bm_h = ddsd.dwHeight;
159 new->cv_bitmap.bm_flags = 0;
160 new->cv_bitmap.bm_type = BM_LINEAR;
161 new->cv_bitmap.bm_rowsize = 0;
162 new->cv_bitmap.bm_data = NULL;
165 new->cv_drawmode = 0;
167 new->cv_font_fg_color = 0;
168 new->cv_font_bg_color = 0;
173 // ----------------------------------------------------------------------------
174 void dd_gr_free_canvas(dd_grs_canvas *canvas)
176 if (canvas == dd_grd_curcanv) { //bad!! freeing current canvas!
178 gr_set_current_canvas(NULL);
181 DDFreeSurface(canvas->lpdds);
186 // dd_gr_create_sub_canvas
187 // ----------------------------------------------------------------------------
188 dd_grs_canvas *dd_gr_create_sub_canvas(dd_grs_canvas *cvs,
189 int x, int y, int w, int h)
191 dd_grs_canvas *ddnew;
197 if (x+w > canv->cv_bitmap.bm_w) {Int3(); w=canv->cv_bitmap.bm_w-x;}
198 if (y+h > canv->cv_bitmap.bm_h) {Int3(); h=canv->cv_bitmap.bm_h-y;}
200 ddnew = (dd_grs_canvas *)malloc( sizeof(dd_grs_canvas) );
201 ddnew->xoff = cvs->xoff;
202 ddnew->yoff = cvs->yoff;
203 ddnew->lock_count = 0;
204 ddnew->lpdds = cvs->lpdds;
205 new = &ddnew->canvas;
207 new->cv_bitmap.bm_x = x+canv->cv_bitmap.bm_x;
208 new->cv_bitmap.bm_y = y+canv->cv_bitmap.bm_y;
209 new->cv_bitmap.bm_w = w;
210 new->cv_bitmap.bm_h = h;
211 new->cv_bitmap.bm_flags = 0;
212 new->cv_bitmap.bm_type = canv->cv_bitmap.bm_type;
213 new->cv_bitmap.bm_rowsize = 0;
215 new->cv_bitmap.bm_data = 0;
217 new->cv_color = canv->cv_color;
218 new->cv_drawmode = canv->cv_drawmode;
219 new->cv_font = canv->cv_font;
220 new->cv_font_fg_color = canv->cv_font_fg_color;
221 new->cv_font_bg_color = canv->cv_font_bg_color;
223 ddnew->sram = cvs->sram;
229 // dd_gr_free_sub_canvas
230 // ----------------------------------------------------------------------------
231 void dd_gr_free_sub_canvas(dd_grs_canvas *cvs)
238 // ----------------------------------------------------------------------------
239 void dd_gr_dup_hack(dd_grs_canvas *hacked, dd_grs_canvas *src)
241 hacked->canvas.cv_bitmap.bm_data = src->canvas.cv_bitmap.bm_data;
242 hacked->canvas.cv_bitmap.bm_rowsize = src->canvas.cv_bitmap.bm_rowsize;
245 void dd_gr_dup_unhack(dd_grs_canvas *hacked)
247 hacked->canvas.cv_bitmap.bm_data = 0;
248 hacked->canvas.cv_bitmap.bm_rowsize = 0;
253 // ----------------------------------------------------------------------------
256 Assert(!dd_gr_initialized);
258 if (!dd_grd_screencanv)
259 dd_grd_screencanv = (dd_grs_canvas *)malloc(sizeof(dd_grs_canvas));
260 if (!dd_grd_backcanv)
261 dd_grd_backcanv = (dd_grs_canvas *)malloc(sizeof(dd_grs_canvas));
264 dd_grd_screencanv->lpdds = NULL;
265 dd_grd_backcanv->lpdds = NULL;
267 if (!ddgr_atexit_called) {
269 ddgr_atexit_called = 1;
271 dd_gr_initialized = 1;
276 // ----------------------------------------------------------------------------
279 Assert(dd_gr_initialized);
281 if (dd_grd_screencanv) free(dd_grd_screencanv);
282 if (dd_grd_backcanv) free(dd_grd_backcanv);
284 dd_grd_screencanv = dd_grd_backcanv = NULL;
285 dd_gr_initialized = 0;
290 // ----------------------------------------------------------------------------
291 void dd_gr_init_screen()
295 // Define screen canvas
296 if (dd_grd_screencanv->lpdds != NULL && !IDirectDrawSurface_IsLost(dd_grd_screencanv->lpdds)) {
298 dd_gr_set_current_canvas(NULL);
299 dd_gr_clear_canvas(BM_XRGB(0,0,0));
300 if (_DDModeList[W95OldDisplayMode].modex) {
302 dd_gr_clear_canvas(BM_XRGB(0,0,0));
307 // Capture the surface's palette.
308 DDSetDisplayMode(W95DisplayMode, 0);
309 grwin_set_winpalette(_lpDD, _lpDDPalette);
312 gr_init_screen(BM_LINEAR,
313 GRMODEINFO(rw), GRMODEINFO(rh),
316 dd_grd_screencanv->lock_count = 0;
317 memcpy(&dd_grd_screencanv->canvas, &grd_curscreen->sc_canvas, sizeof(grs_canvas));
321 // The 'Emulated' Method.
322 // We will define the screen canvas as the Windows equiv to
323 // DOS display memory. This will be our Direct Draw Back canvas.
325 if (GRMODEINFO(emul) || GRMODEINFO(modex)) {
326 dd_grd_screencanv->lpdds = _lpDDSBack;
327 dd_grd_backcanv->lpdds = NULL;
328 dd_grd_curcanv = NULL;
332 // The Page Flipping Full Screen Method
333 // The screen canvas is the actual display
334 // The back canvas is our scratch page
335 // This does not apply to Mode X modes
337 else if (GRMODEINFO(paged) && !GRMODEINFO(modex)) {
338 dd_grd_screencanv->lpdds = _lpDDSPrimary;
340 new = &dd_grd_backcanv->canvas;
342 new->cv_bitmap.bm_x = 0;
343 new->cv_bitmap.bm_y = 0;
344 new->cv_bitmap.bm_w = _DDModeList[W95DisplayMode].rw;
345 new->cv_bitmap.bm_h = _DDModeList[W95DisplayMode].rh;
346 new->cv_bitmap.bm_flags = 0;
347 new->cv_bitmap.bm_type = BM_LINEAR;
348 new->cv_bitmap.bm_rowsize = 0;
349 new->cv_bitmap.bm_data = NULL;
352 new->cv_drawmode = 0;
354 new->cv_font_fg_color = 0;
355 new->cv_font_bg_color = 0;
357 dd_grd_backcanv->lpdds = _lpDDSBack;
358 dd_grd_backcanv->lock_count = 0;
359 dd_grd_curcanv = NULL;
363 // No page flipping (just 1 page), and no use for back surface.
365 else if (GRMODEINFO(dbuf) && !GRMODEINFO(paged)) {
366 dd_grd_screencanv->lpdds = _lpDDSPrimary;
367 dd_grd_backcanv->lpdds = NULL;
368 dd_grd_curcanv = NULL;
374 Int3(); // An illegal hacked graphic mode
377 dd_gr_set_current_canvas(NULL);
378 dd_gr_clear_canvas(BM_XRGB(0,0,0));
379 // if (GRMODEINFO(modex)) {
381 // dd_gr_clear_canvas(BM_XRGB(0,0,0));
387 int dd_gr_restore_canvas(dd_grs_canvas *canvas)
389 if (!DDRestoreSurface(canvas->lpdds)) {
391 DDFreeSurface(canvas->lpdds);
393 canvas->lpdds = DDCreateSysMemSurface(canvas->canvas.cv_bitmap.bm_w,
394 canvas->canvas.cv_bitmap.bm_h);
397 canvas->lpdds = DDCreateSurface(canvas->canvas.cv_bitmap.bm_w,
398 canvas->canvas.cv_bitmap.bm_h, TRUE);
400 if (!canvas->lpdds) return 0;
408 // copies dd_gr_screencanv to grd_curscreen->sc_canvas
409 // ----------------------------------------------------------------------------
410 void dd_gr_screen_lock()
412 if (dd_grd_screencanv->lpdds == _lpDDSPrimary && GRMODEINFO(modex))
413 Int3(); // Can't do this in ModeX!!
414 dd_gr_lock(dd_grd_screencanv);
415 memcpy(&grd_curscreen->sc_canvas, &dd_grd_screencanv->canvas, sizeof(grs_canvas));
419 // dd_gr_screen_unlock
420 // copies grd_curscreen->sc_canvas to dd_gr_screencanv
421 // ----------------------------------------------------------------------------
422 void dd_gr_screen_unlock()
424 memcpy(&dd_grd_screencanv->canvas, &grd_curscreen->sc_canvas, sizeof(grs_canvas));
425 dd_gr_unlock(dd_grd_screencanv);
429 void dd_gr_lock(dd_grs_canvas *canv)
436 if (canv->lock_count == 0)
438 bmp = &canv->canvas.cv_bitmap;
439 SetRect(&rect,bmp->bm_x,bmp->bm_y,bmp->bm_x+bmp->bm_w, bmp->bm_y+bmp->bm_h);
441 if (!dd_gr_restore_canvas(canv))
442 Error("Failed to lock canvas (restore err)!\n");
444 data = DDLockSurface(canv->lpdds, &rect, &rowsize);
445 canv->canvas.cv_bitmap.bm_rowsize = (short)rowsize;
448 Error("Failed to lock canvas! You may need to use the -emul option.\n");
450 canv->canvas.cv_bitmap.bm_data = data;
452 if (canv == dd_grd_curcanv) {
453 gr_set_current_canvas(&canv->canvas);
460 void dd_gr_lock_d(dd_grs_canvas *canv, char *filename, int line)
467 if (canv->lock_count == 0)
469 bmp = &canv->canvas.cv_bitmap;
470 SetRect(&rect,bmp->bm_x,bmp->bm_y,bmp->bm_x+bmp->bm_w, bmp->bm_y+bmp->bm_h);
472 if (!dd_gr_restore_canvas(canv))
474 Error("Failed to lock canvas (restore err) (%s line %d)\n", filename, line);
476 Error("Failed to lock canvas (restore err)!\n");
479 data = DDLockSurface(canv->lpdds, &rect, &rowsize);
480 canv->canvas.cv_bitmap.bm_rowsize = (short)rowsize;
484 Error("Failed to lock canvas (%s line %d)\n", filename, line);
486 Error("Failed to lock canvas! You may ned to use the -emul option.\n");
489 canv->canvas.cv_bitmap.bm_data = data;
491 if (canv == dd_grd_curcanv) {
492 gr_set_current_canvas(&canv->canvas);
500 void dd_gr_unlock(dd_grs_canvas *canv)
502 if (canv->lock_count == 1)
504 DDUnlockSurface(canv->lpdds, canv->canvas.cv_bitmap.bm_data);
510 // dd_gr_set_current_canvas
511 // This function should do this:
512 // set desired canvas to dd_grd_curcanv
513 // call gr_set_current_canvas
515 // If desired canvas is NULL, then set current canvas to screen
516 // set grd_curscreen->sc_canvas.cv_bitmap, etc to dd_grd_screencanv info
517 // call gr_set_current_canvas(NULL)
518 // this will use what is in grd_curscreen->etc to do the proper thing
520 // Note: Must lock desired canvas before calling this.
521 // This causes an address to be delivered to the canvas.
522 // Else address is invalid and will crash.
524 void dd_gr_set_current_canvas(dd_grs_canvas *canvas)
526 if (canvas == NULL) {
527 dd_grd_curcanv = dd_grd_screencanv;
528 gr_set_current_canvas(&dd_grd_screencanv->canvas);
531 dd_grd_curcanv = canvas;
532 gr_set_current_canvas(&dd_grd_curcanv->canvas);
537 // dd_gr_init_sub_canvas
538 // perform gr_init_sub_canvas.
539 // same surface but reset lock count.
540 void dd_gr_init_sub_canvas(dd_grs_canvas *new, dd_grs_canvas *src,
541 int x, int y, int w, int h)
543 gr_init_sub_canvas(&new->canvas, &src->canvas, x, y, w, h);
544 new->xoff = src->xoff;
545 new->yoff = src->yoff;
546 new->lpdds = src->lpdds;
548 new->sram = src->sram;
553 // performs a general 'flip' of canvases.
554 // If we are in a slow display mode, we will copy the screen canvas
556 // If we are in a FullScreen mode, then we will just perform a
560 if (GRMODEINFO(emul)) {
561 dd_gr_blt_display(dd_grd_screencanv,
563 ViewportRect.left, ViewportRect.top,
564 ViewportRect.right-ViewportRect.left,
565 ViewportRect.bottom-ViewportRect.top);
567 else if (_DDFullScreen) {
571 Int3(); // Illegal display mode!
576 // dd_gr_restore_display
577 // blts the screen canvas to the display
578 // (for Slow modes which emulate a DOS display)
579 void dd_gr_restore_display()
581 if (GRMODEINFO(emul)) {
582 // We use a offscreen buffer as grd_screencanv, so just
583 // blt this to the display
585 IDirectDrawSurface_Blt(_lpDDSPrimary,
587 dd_grd_screencanv->lpdds,
592 else if (GRMODEINFO(modex)) {
601 // blts one canvas region to another canvas with scaling and
603 void dd_gr_blt_notrans(dd_grs_canvas *srccanv,
604 int sx, int sy, int swidth, int sheight,
605 dd_grs_canvas *destcanv,
606 int dx, int dy, int dwidth, int dheight)
609 RECT *psrect, *pdrect;
610 grs_bitmap *sbmp, *dbmp;
614 sbmp = &srccanv->canvas.cv_bitmap;
615 dbmp = &destcanv->canvas.cv_bitmap;
617 if (swidth || sheight) {
618 SetRect(psrect, sx+sbmp->bm_x, sy+sbmp->bm_y, sx+sbmp->bm_x+swidth,
619 sy+sbmp->bm_y+sheight);
622 SetRect(psrect, sbmp->bm_x, sbmp->bm_y, sbmp->bm_w+sbmp->bm_x,
623 sbmp->bm_h+sbmp->bm_y);
626 if (dwidth || dheight) {
627 SetRect(pdrect, dx+dbmp->bm_x, dy+dbmp->bm_y, dx+dbmp->bm_x+dwidth,
628 dy+dbmp->bm_y+dheight);
631 SetRect(pdrect, dbmp->bm_x, dbmp->bm_y, dbmp->bm_x+dbmp->bm_w,
632 dbmp->bm_y+dbmp->bm_h);
635 if (_DDFullScreen && !GRMODEINFO(emul)) {
636 IDirectDrawSurface_BltFast(destcanv->lpdds, dx+dbmp->bm_x, dy+dbmp->bm_y,
637 srccanv->lpdds, psrect,
638 DDBLTFAST_NOCOLORKEY | DDBLTFAST_WAIT);
641 IDirectDrawSurface_Blt(destcanv->lpdds,
651 // dd_gr_blt_display (no keying)
652 // blts a canvas region to the display.
653 // for windowed modes, this is the only way to blt
654 // a canvas to the display.
655 void dd_gr_blt_display(dd_grs_canvas *srccanv,
656 int sx, int sy, int swidth, int sheight,
657 int dx, int dy, int dwidth, int dheight)
660 RECT *psrect, *pdrect;
664 if (srccanv->lpdds == _lpDDSPrimary) {
665 Int3(); // This will crash the system
671 sbmp = &srccanv->canvas.cv_bitmap;
673 if (swidth || sheight) {
674 SetRect(psrect, sx+sbmp->bm_x, sy+sbmp->bm_y, sx+sbmp->bm_x+swidth-1,
675 sy+sbmp->bm_y+sheight-1);
678 SetRect(psrect, sbmp->bm_x, sbmp->bm_y, sbmp->bm_w+sbmp->bm_x-1,
679 sbmp->bm_h+sbmp->bm_y-1);
682 if (dwidth || dheight) {
683 SetRect(pdrect, dx, dy, dx+dwidth-1, dy+dheight-1);
687 // We are blting to the display which is _lpDDSPrimary.
688 result = IDirectDrawSurface_Blt(_lpDDSPrimary,
695 Error("DDERR: Blt: (%d)\n", (result & 0x0000ffff));
699 // dd_gr_blt_screen (no keying)
700 // blts canvas to screen canvas
701 void dd_gr_blt_screen(dd_grs_canvas *srccanv,
702 int sx, int sy, int swidth, int sheight,
703 int dx, int dy, int dwidth, int dheight)
705 dd_gr_blt_notrans(srccanv, sx, sy, swidth, sheight,
706 dd_grd_screencanv, dx, dy, dwidth, dheight);
710 // dd_gr_clear_canvas
711 // clears a canvas the 'fast' way.
712 void dd_gr_clear_canvas(int color)
719 bmp = &dd_grd_curcanv->canvas.cv_bitmap;
721 UpdateWindow(GetLibraryWindow());
723 ddbltfx.dwSize = sizeof( ddbltfx );
724 ddbltfx.dwFillColor = (DWORD)color;
726 Assert(_DDLockCounter == 0);
728 SetRect(&drect, bmp->bm_x, bmp->bm_y, bmp->bm_x+bmp->bm_w,
729 bmp->bm_y+bmp->bm_h);
731 ddresult = IDirectDrawSurface_Blt(
732 dd_grd_curcanv->lpdds, // dest surface
736 DDBLT_COLORFILL | DDBLT_WAIT,
738 if (ddresult != DD_OK) {
739 if (ddresult == DDERR_SURFACELOST) {
740 if (!dd_gr_restore_canvas(dd_grd_curcanv))
741 Error("Direct Draw GR library Blt Clear Restore error.");
743 else Error("Direct Draw GR library Blt Clear error: %x", ddresult);
746 if (!_DDFullScreen) {
747 Assert(_DDLockCounter == 0);