1 // ----------------------------------------------------------------------------
3 // ----------------------------------------------------------------------------
5 #include "DDScrollBuffer.h"
10 // --- VERSION 1.0 CLASS
12 // --- MultiUse = -1 'True // True
13 // --- Persistable = 0 'NotPersistable // NotPersistable
14 // --- DataBindingBehavior = 0 'vbNone // vbNone
15 // --- DataSourceBehavior = 0 'vbNone // vbNone
16 // --- MTSTransactionMode = 0 'NotAnMTSObject // NotAnMTSObject
19 // static char *VB_Name = "DDScrollBuffer";
20 // static boolean VB_GlobalNameSpace = False;
21 // static boolean VB_Creatable = True;
22 // static boolean VB_PredeclaredId = False;
23 // static boolean VB_Exposed = False;
25 // --- Option Explicit
27 // needs reference to: DirectX7 for Visual Basic Type Library
29 DirectDrawSurface7 Buffer;
30 DirectDrawSurface7 mPrimary;
33 long mScrollX, mScrollY;
34 long mScrollX_last, mScrollY_last;
35 long mDestXOff, mDestYOff;
37 long ScreenBuffer[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
38 boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
43 static void ScrollPlayfield(int dx, int dy)
45 int x1 = mScrollX_last / TILEX - 2;
46 int y1 = mScrollY_last / TILEY - 2;
47 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
48 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
51 BlitBitmap(screenBitmap, screenBitmap,
54 (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
55 (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
59 /* when scrolling the whole playfield, do not redraw single tiles */
60 for (x = 0; x < MAX_BUF_XSIZE; x++)
61 for (y = 0; y < MAX_BUF_YSIZE; y++)
69 for (y = DisplayMinY; y <= DisplayMaxY; y++)
71 for (x = DisplayMinX; x <= DisplayMaxX; x++)
73 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
77 int tsi = GetSI(x, y);
78 long id = ((PlayField16[tsi]) |
79 (PlayField8[tsi] << 16) |
80 (DisPlayField[tsi] << 24));
84 printf("::: [%d] %d [%d, %d] [%d]\n", dx, sx, x, y, buf_xsize);
86 if (sx == 0 || sx == MAX_BUF_XSIZE - 1)
87 printf("::: %d, %d\n", dx, sx);
91 if ((dx == -1 && x == x2) ||
92 (dx == +1 && x == x1) ||
93 (dy == -1 && y == y2) ||
94 (dy == +1 && y == y1))
97 printf("::: %d, %d\n", sx, sy);
102 DrawFieldNoAnimated(x, y);
103 DrawFieldAnimated(x, y);
108 ScreenBuffer[sx][sy] = id;
114 static void ScrollPlayfieldIfNeededExt(boolean reset)
124 if (mScrollX_last == -1 || mScrollY_last == -1)
126 mScrollX_last = mScrollX;
127 mScrollY_last = mScrollY;
134 /* check if scrolling the playfield requires redrawing the viewport bitmap */
135 if ((mScrollX != mScrollX_last ||
136 mScrollY != mScrollY_last) &&
137 (ABS(mScrollX - mScrollX_last) >= TILEX ||
138 ABS(mScrollY - mScrollY_last) >= TILEY))
140 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
141 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
142 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
143 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
145 mScrollX_last -= dx * TILEX;
146 mScrollY_last -= dy * TILEY;
148 ScrollPlayfield(dx, dy);
151 printf("::: %ld, %ld\n", mScrollX, mScrollY);
157 /* check if scrolling the playfield reached the destination tile position */
158 if ((mScrollX != mScrollX_last || mScrollY != mScrollY_last) &&
159 mScrollX % TILEX == 0 && mScrollY % TILEY == 0)
161 int dx = (mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
162 int dy = (mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
164 mScrollX_last = mScrollX;
165 mScrollY_last = mScrollY;
167 ScrollPlayfield(dx, dy);
170 printf("::: %ld, %ld\n", mScrollX, mScrollY);
177 static void ScrollPlayfieldIfNeeded()
179 ScrollPlayfieldIfNeededExt(FALSE);
182 void InitScrollPlayfield()
184 ScrollPlayfieldIfNeededExt(TRUE);
187 void UpdatePlayfield()
189 int x1 = mScrollX_last / TILEX - 2;
190 int y1 = mScrollY_last / TILEY - 2;
191 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
192 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
195 for (y = DisplayMinY; y <= DisplayMaxY; y++)
197 for (x = DisplayMinX; x <= DisplayMaxX; x++)
199 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
203 int tsi = GetSI(x, y);
204 long id = ((PlayField16[tsi]) |
205 (PlayField8[tsi] << 16) |
206 (DisPlayField[tsi] << 24));
207 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
210 if (LowByte(PlayField16[tsi]) == fiMurphy)
214 if (redraw_screen_tile)
217 DrawFieldNoAnimated(x, y);
218 DrawFieldAnimated(x, y);
221 ScreenBuffer[sx][sy] = id;
223 redraw[sx][sy] = TRUE;
231 void OLD_UpdatePlayfield()
234 int left = mScrollX / TILEX;
235 int top = mScrollY / TILEY;
237 for (y = top; y < top + MAX_BUF_YSIZE; y++)
239 for (x = left; x < left + MAX_BUF_XSIZE; x++)
241 int sx = x % MAX_BUF_XSIZE;
242 int sy = y % MAX_BUF_YSIZE;
243 int tsi = GetSI(x, y);
244 long id = ((PlayField16[tsi]) |
245 (PlayField8[tsi] << 16) |
246 (DisPlayField[tsi] << 24));
247 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
249 if (redraw_screen_tile)
251 DrawFieldNoAnimated(x, y);
252 DrawFieldAnimated(x, y);
254 ScreenBuffer[sx][sy] = id;
256 redraw[sx][sy] = TRUE;
263 void DDScrollBuffer_Let_DestXOff(long NewVal)
268 long DDScrollBuffer_Get_DestXOff()
272 DestXOff = mDestXOff;
277 void DDScrollBuffer_Let_DestYOff(long NewVal)
282 long DDScrollBuffer_Get_DestYOff()
286 DestYOff = mDestYOff;
291 DirectDrawSurface7 DDScrollBuffer_Get_Surface()
293 DirectDrawSurface7 Surface;
300 long DDScrollBuffer_Get_Width()
309 int DDScrollBuffer_Get_Height()
320 long DDScrollBuffer_CreateAtSize(long Width, long Height, long hWndViewPort)
327 mhWnd = hWndViewPort;
328 // Create ScrollBuffer:
330 SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
331 SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
332 // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
337 // --- On Error Resume Next
338 Buffer = DDraw.CreateSurface(SD);
342 // --- On Error GoTo 0
353 void DDScrollBuffer_Cls(int BackColor)
360 Buffer.BltColorFill(EmptyRect, BackColor);
365 /* copy the entire screen to the window at the scroll position */
367 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
370 int px = 2 * TILEX + mScrollX % TILEX;
371 int py = 2 * TILEY + mScrollY % TILEY;
373 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
374 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
376 int sx, sy, sxsize, sysize;
379 if (mScrollX % TILEX != (mScrollX - mScrollX_last) % TILEX ||
380 mScrollY % TILEY != (mScrollY - mScrollY_last) % TILEY)
381 printf("::: %ld, %ld / %ld, %ld\n",
382 mScrollX, mScrollY, mScrollX_last, mScrollY_last);
388 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
389 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
391 sxsize = (full_xsize < xsize ? full_xsize : xsize);
392 sysize = (full_ysize < ysize ? full_ysize : ysize);
393 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
394 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
399 static int mScrollX_tmp = -1;
400 static int mScrollY_tmp = -1;
402 if (mScrollX != mScrollX_tmp || mScrollY != mScrollY_tmp)
404 printf("::: %ld, %ld\n", mScrollX, mScrollY);
406 mScrollX_tmp = mScrollX;
407 mScrollY_tmp = mScrollY;
414 static boolean x = 0;
418 printf("::: %d, %d (%d, %d) (%d, %d) [%ld, %ld] [%ld, %ld] \n",
419 sx, sy, xsize, ysize, full_xsize, full_ysize,
420 mScrollX, mScrollY, mScrollX_last, mScrollY_last);
428 if (!menBorder.Checked)
436 if (mScrollX >= 0 && mScrollX <= 16)
441 else if (mScrollX >= 352 && mScrollX <= 368)
446 else if (mScrollX >= 16) // && mScrollX <= 352)
453 /* !!! TEST ONLY !!! */
458 BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
461 void OLD_BlitScreenToBitmap_SP(Bitmap *target_bitmap)
463 int x = mScrollX % (MAX_BUF_XSIZE * TILEX);
464 int y = mScrollY % (MAX_BUF_YSIZE * TILEY);
466 if (x < 2 * TILEX && y < 2 * TILEY)
468 BlitBitmap(screenBitmap, target_bitmap, x, y,
469 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
471 else if (x < 2 * TILEX && y >= 2 * TILEY)
473 BlitBitmap(screenBitmap, target_bitmap, x, y,
474 SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
476 BlitBitmap(screenBitmap, target_bitmap, x, 0,
477 SCR_FIELDX * TILEX, y - 2 * TILEY,
478 SX, SY + MAX_BUF_YSIZE * TILEY - y);
480 else if (x >= 2 * TILEX && y < 2 * TILEY)
482 BlitBitmap(screenBitmap, target_bitmap, x, y,
483 MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
485 BlitBitmap(screenBitmap, target_bitmap, 0, y,
486 x - 2 * TILEX, SCR_FIELDY * TILEY,
487 SX + MAX_BUF_XSIZE * TILEX - x, SY);
491 BlitBitmap(screenBitmap, target_bitmap, x, y,
492 MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
494 BlitBitmap(screenBitmap, target_bitmap, 0, y,
495 x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
496 SX + MAX_BUF_XSIZE * TILEX - x, SY);
497 BlitBitmap(screenBitmap, target_bitmap, x, 0,
498 MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
499 SX, SY + MAX_BUF_YSIZE * TILEY - y);
500 BlitBitmap(screenBitmap, target_bitmap, 0, 0,
501 x - 2 * TILEX, y - 2 * TILEY,
502 SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
506 void BackToFront_SP(void)
508 static boolean scrolling_last = FALSE;
509 int left = mScrollX / TILEX;
510 int top = mScrollY / TILEY;
511 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
517 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
520 BlitScreenToBitmap_SP(window);
522 /* blit all (up to four) parts of the scroll buffer to the backbuffer */
523 BlitScreenToBitmap_SP(backbuffer);
525 /* blit the completely updated backbuffer to the window (in one blit) */
526 BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
531 for (x = 0; x < SCR_FIELDX; x++)
533 for (y = 0; y < SCR_FIELDY; y++)
535 int xx = (left + x) % MAX_BUF_XSIZE;
536 int yy = (top + y) % MAX_BUF_YSIZE;
539 BlitBitmap(screenBitmap, window,
540 xx * TILEX, yy * TILEY, TILEX, TILEY,
541 SX + x * TILEX, SY + y * TILEY);
548 for (x = 0; x < MAX_BUF_XSIZE; x++)
549 for (y = 0; y < MAX_BUF_YSIZE; y++)
550 redraw[x][y] = FALSE;
553 scrolling_last = scrolling;
557 void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
575 DR.right = SCR_FIELDX * TILEX;
576 DR.bottom = SCR_FIELDY * TILEY;
578 // --- On Error GoTo BltEH
579 DirectX.GetWindowRect(mhWnd, DR);
580 // --- On Error GoTo 0
584 tX = (DR.right - DR.left) / Stretch;
585 tY = (DR.bottom - DR.top) / Stretch;
589 SR.left = mScrollX + mDestXOff;
590 SR.top = mScrollY + mDestYOff;
592 SR.right = SR.left + tX;
593 SR.bottom = SR.top + tY;
595 // If mWidth < SR.right Then
597 // DR.right = DR.left + Stretch * (SR.right - SR.left)
599 // If mHeight < SR.bottom Then
600 // SR.bottom = mHeight
601 // DR.bottom = DR.top + Stretch * (SR.bottom - SR.top)
603 // If (mScrollX + mDestXOff) < 0 Then
605 // DR.left = DR.left - Stretch * (mScrollX + mDestXOff)
607 // If (mScrollY + mDestYOff) < 0 Then
609 // DR.top = DR.top - Stretch * (mScrollY + mDestYOff)
614 SR.left = (SR.left < 0 ? 0 : SR.left);
615 SR.top = (SR.top < 0 ? 0 : SR.top);
620 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
621 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
622 int sxsize = SCR_FIELDX * TILEX;
623 int sysize = SCR_FIELDY * TILEY;
625 tX = (full_xsize < sxsize ? full_xsize : tX);
626 tY = (full_ysize < sysize ? full_ysize : tY);
627 sX = SX + (full_xsize < sxsize ? (sxsize - full_xsize) / 2 : 0);
628 sY = SY + (full_ysize < sysize ? (sysize - full_ysize) / 2 : 0);
633 if (!menBorder.Checked)
635 SR.left += TILEX / 2;
643 printf("::: DDScrollBuffer.c: DDScrollBuffer_Blt(): blit from %d, %d [%ld, %ld] [%ld, %ld] [%ld, %ld]\n",
644 SR.left, SR.top, mScrollX, mScrollY, mDestXOff, mDestYOff, tX, tY);
648 /* !!! quick and dirty -- FIX THIS !!! */
649 if (tape.playing && tape.fast_forward &&
650 target_bitmap == window &&
651 (FrameCounter % 2) != 0)
652 printf("::: FrameCounter == %d\n", FrameCounter);
660 BlitBitmap(screenBitmap, target_bitmap,
661 SR.left, SR.top, tX, tY, sX, sY);
663 BlitBitmap(screenBitmap, target_bitmap,
665 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
676 // DDraw.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0
677 if (IS_NOTHING(&Buffer, sizeof(Buffer)))
681 if (IS_NOTHING(&PrimarySurface, sizeof(PrimarySurface)))
687 L = PrimarySurface.Blt(DR, &Buffer, SR, DDBLT_WAIT);
697 case DDERR_INVALIDCLIPLIST:
701 case DDERR_INVALIDOBJECT:
705 case DDERR_INVALIDPARAMS:
709 case DDERR_INVALIDRECT:
713 case DDERR_NOALPHAHW:
721 case DDERR_NOCLIPLIST:
725 case DDERR_NODDROPSHW:
729 case DDERR_NOMIRRORHW:
733 case DDERR_NORASTEROPHW:
737 case DDERR_NOROTATIONHW:
741 case DDERR_NOSTRETCHHW:
745 case DDERR_NOZBUFFERHW:
749 case DDERR_SURFACEBUSY:
755 case DDERR_SURFACELOST:
756 DDraw.RestoreAllSurfaces();
757 if (! PrimarySurface.isLost())
763 // RestorePrimarySurface
769 case DDERR_UNSUPPORTED:
773 case DDERR_WASSTILLDRAWING:
787 // Buffer.UpdateOverlay SR, PrimarySurface, DR, DDOVER_SHOW
789 FMark.RefreshMarker();
795 void DDScrollBuffer_Blt()
802 /* !!! TEST ONLY !!! */
803 BlitBitmap(screenBitmap, window,
804 0, 0, SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
808 DDScrollBuffer_Blt_Ext(window);
812 void DDScrollBuffer_ScrollTo(int X, int Y)
825 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTo(): mScroll: %ld, %ld [%d, %d]\n",
826 mScrollX, mScrollY, X, Y);
830 ScrollPlayfieldIfNeeded();
834 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
842 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (1) mScroll: %ld, %ld [%d, %d, %f, %f]\n",
843 mScrollX, mScrollY, X, Y, Step, Stretch);
850 r = Sqr(dx * dx + dY * dY);
851 if (r == 0) // we are there already
859 mScrollX = mScrollX + dx * r;
860 mScrollY = mScrollY + dY * r;
865 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (2) mScroll: %ld, %ld [%d, %d, %f]\n",
866 mScrollX, mScrollY, X, Y, Step);
870 ScrollPlayfieldIfNeeded();
874 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
878 TickCountObject Tick;
882 long oldX, oldY, maxD;
883 static boolean AlreadyRunning = False;
893 AlreadyRunning = True;
898 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
899 StepCount = FPS * (TimeMS / (double)1000);
900 if (StepCount > maxD)
907 tStep = (double)1 / StepCount;
910 // R = Sqr(dX * dX + dY * dY)
911 // If R = 0 Then Exit Sub 'we are there already
912 for (T = (double)tStep; T <= (double)1; T += tStep)
917 // If Claim Then Exit For
920 Tick.DelayMS(dT, False);
923 mScrollX = oldX + T * dx;
924 mScrollY = oldY + T * dY;
934 Tick.DelayMS(dT, False);
944 AlreadyRunning = False;
947 printf("::: DDScrollBuffer.c: DDScrollBuffer_SoftScrollTo(): mScroll: %ld, %ld\n",
952 ScrollPlayfieldIfNeeded();