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()
194 for (y = DisplayMinY; y <= DisplayMaxY; y++)
196 for (x = DisplayMinX; x <= DisplayMaxX; x++)
198 int element = LowByte(PlayField16[GetSI(x, y)]);
199 int graphic = GfxGraphic[x][y];
200 int sync_frame = GfxFrame[x][y];
202 boolean redraw = FALSE;
204 boolean redraw = TRUE; // !!! TEST ONLY -- ALWAYS REDRAW !!!
210 if (element != GfxElementLast[x][y] &&
211 graphic == GfxGraphicLast[x][y])
213 /* element changed, but not graphic => disable updating graphic */
215 GfxElementLast[x][y] = element;
216 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
221 if (graphic != GfxGraphicLast[x][y]) // new graphic
225 GfxElementLast[x][y] = element;
226 GfxGraphicLast[x][y] = GfxGraphic[x][y];
227 sync_frame = GfxFrame[x][y] = 0;
229 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
236 int sx = x * StretchWidth;
237 int sy = y * StretchWidth;
240 printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
243 StretchedSprites.BltImg(sx, sy, graphic, sync_frame);
253 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
257 void UpdatePlayfield_TMP()
259 int x1 = mScrollX_last / TILEX - 2;
260 int y1 = mScrollY_last / TILEY - 2;
261 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
262 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
265 for (y = DisplayMinY; y <= DisplayMaxY; y++)
267 for (x = DisplayMinX; x <= DisplayMaxX; x++)
269 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
273 int tsi = GetSI(x, y);
274 long id = ((PlayField16[tsi]) |
275 (PlayField8[tsi] << 16) |
276 (DisPlayField[tsi] << 24));
277 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
280 if (LowByte(PlayField16[tsi]) == fiMurphy)
284 if (redraw_screen_tile)
287 DrawFieldNoAnimated(x, y);
288 DrawFieldAnimated(x, y);
291 ScreenBuffer[sx][sy] = id;
293 redraw[sx][sy] = TRUE;
301 void OLD_UpdatePlayfield()
304 int left = mScrollX / TILEX;
305 int top = mScrollY / TILEY;
307 for (y = top; y < top + MAX_BUF_YSIZE; y++)
309 for (x = left; x < left + MAX_BUF_XSIZE; x++)
311 int sx = x % MAX_BUF_XSIZE;
312 int sy = y % MAX_BUF_YSIZE;
313 int tsi = GetSI(x, y);
314 long id = ((PlayField16[tsi]) |
315 (PlayField8[tsi] << 16) |
316 (DisPlayField[tsi] << 24));
317 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
319 if (redraw_screen_tile)
321 DrawFieldNoAnimated(x, y);
322 DrawFieldAnimated(x, y);
324 ScreenBuffer[sx][sy] = id;
326 redraw[sx][sy] = TRUE;
333 void DDScrollBuffer_Let_DestXOff(long NewVal)
338 long DDScrollBuffer_Get_DestXOff()
342 DestXOff = mDestXOff;
347 void DDScrollBuffer_Let_DestYOff(long NewVal)
352 long DDScrollBuffer_Get_DestYOff()
356 DestYOff = mDestYOff;
361 DirectDrawSurface7 DDScrollBuffer_Get_Surface()
363 DirectDrawSurface7 Surface;
370 long DDScrollBuffer_Get_Width()
379 int DDScrollBuffer_Get_Height()
390 long DDScrollBuffer_CreateAtSize(long Width, long Height, long hWndViewPort)
397 mhWnd = hWndViewPort;
398 // Create ScrollBuffer:
400 SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
401 SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
402 // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
407 // --- On Error Resume Next
408 Buffer = DDraw.CreateSurface(SD);
412 // --- On Error GoTo 0
423 void DDScrollBuffer_Cls(int BackColor)
430 Buffer.BltColorFill(EmptyRect, BackColor);
435 /* copy the entire screen to the window at the scroll position */
437 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
440 int px = 2 * TILEX + mScrollX % TILEX;
441 int py = 2 * TILEY + mScrollY % TILEY;
443 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
444 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
446 int sx, sy, sxsize, sysize;
449 if (mScrollX % TILEX != (mScrollX - mScrollX_last) % TILEX ||
450 mScrollY % TILEY != (mScrollY - mScrollY_last) % TILEY)
451 printf("::: %ld, %ld / %ld, %ld\n",
452 mScrollX, mScrollY, mScrollX_last, mScrollY_last);
458 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
459 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
461 sxsize = (full_xsize < xsize ? full_xsize : xsize);
462 sysize = (full_ysize < ysize ? full_ysize : ysize);
463 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
464 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
469 static int mScrollX_tmp = -1;
470 static int mScrollY_tmp = -1;
472 if (mScrollX != mScrollX_tmp || mScrollY != mScrollY_tmp)
474 printf("::: %ld, %ld\n", mScrollX, mScrollY);
476 mScrollX_tmp = mScrollX;
477 mScrollY_tmp = mScrollY;
484 static boolean x = 0;
488 printf("::: %d, %d (%d, %d) (%d, %d) [%ld, %ld] [%ld, %ld] \n",
489 sx, sy, xsize, ysize, full_xsize, full_ysize,
490 mScrollX, mScrollY, mScrollX_last, mScrollY_last);
498 if (!menBorder.Checked)
506 if (mScrollX >= 0 && mScrollX <= 16)
511 else if (mScrollX >= 352 && mScrollX <= 368)
516 else if (mScrollX >= 16) // && mScrollX <= 352)
523 /* !!! TEST ONLY !!! */
528 BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
531 void OLD_BlitScreenToBitmap_SP(Bitmap *target_bitmap)
533 int x = mScrollX % (MAX_BUF_XSIZE * TILEX);
534 int y = mScrollY % (MAX_BUF_YSIZE * TILEY);
536 if (x < 2 * TILEX && y < 2 * TILEY)
538 BlitBitmap(screenBitmap, target_bitmap, x, y,
539 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
541 else if (x < 2 * TILEX && y >= 2 * TILEY)
543 BlitBitmap(screenBitmap, target_bitmap, x, y,
544 SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
546 BlitBitmap(screenBitmap, target_bitmap, x, 0,
547 SCR_FIELDX * TILEX, y - 2 * TILEY,
548 SX, SY + MAX_BUF_YSIZE * TILEY - y);
550 else if (x >= 2 * TILEX && y < 2 * TILEY)
552 BlitBitmap(screenBitmap, target_bitmap, x, y,
553 MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
555 BlitBitmap(screenBitmap, target_bitmap, 0, y,
556 x - 2 * TILEX, SCR_FIELDY * TILEY,
557 SX + MAX_BUF_XSIZE * TILEX - x, SY);
561 BlitBitmap(screenBitmap, target_bitmap, x, y,
562 MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
564 BlitBitmap(screenBitmap, target_bitmap, 0, y,
565 x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
566 SX + MAX_BUF_XSIZE * TILEX - x, SY);
567 BlitBitmap(screenBitmap, target_bitmap, x, 0,
568 MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
569 SX, SY + MAX_BUF_YSIZE * TILEY - y);
570 BlitBitmap(screenBitmap, target_bitmap, 0, 0,
571 x - 2 * TILEX, y - 2 * TILEY,
572 SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
576 void BackToFront_SP(void)
578 static boolean scrolling_last = FALSE;
579 int left = mScrollX / TILEX;
580 int top = mScrollY / TILEY;
581 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
587 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
590 BlitScreenToBitmap_SP(window);
592 /* blit all (up to four) parts of the scroll buffer to the backbuffer */
593 BlitScreenToBitmap_SP(backbuffer);
595 /* blit the completely updated backbuffer to the window (in one blit) */
596 BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
601 for (x = 0; x < SCR_FIELDX; x++)
603 for (y = 0; y < SCR_FIELDY; y++)
605 int xx = (left + x) % MAX_BUF_XSIZE;
606 int yy = (top + y) % MAX_BUF_YSIZE;
609 BlitBitmap(screenBitmap, window,
610 xx * TILEX, yy * TILEY, TILEX, TILEY,
611 SX + x * TILEX, SY + y * TILEY);
618 for (x = 0; x < MAX_BUF_XSIZE; x++)
619 for (y = 0; y < MAX_BUF_YSIZE; y++)
620 redraw[x][y] = FALSE;
623 scrolling_last = scrolling;
627 void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
645 DR.right = SCR_FIELDX * TILEX;
646 DR.bottom = SCR_FIELDY * TILEY;
648 // --- On Error GoTo BltEH
649 DirectX.GetWindowRect(mhWnd, DR);
650 // --- On Error GoTo 0
654 tX = (DR.right - DR.left) / Stretch;
655 tY = (DR.bottom - DR.top) / Stretch;
659 SR.left = mScrollX + mDestXOff;
660 SR.top = mScrollY + mDestYOff;
662 SR.right = SR.left + tX;
663 SR.bottom = SR.top + tY;
665 // If mWidth < SR.right Then
667 // DR.right = DR.left + Stretch * (SR.right - SR.left)
669 // If mHeight < SR.bottom Then
670 // SR.bottom = mHeight
671 // DR.bottom = DR.top + Stretch * (SR.bottom - SR.top)
673 // If (mScrollX + mDestXOff) < 0 Then
675 // DR.left = DR.left - Stretch * (mScrollX + mDestXOff)
677 // If (mScrollY + mDestYOff) < 0 Then
679 // DR.top = DR.top - Stretch * (mScrollY + mDestYOff)
684 SR.left = (SR.left < 0 ? 0 : SR.left);
685 SR.top = (SR.top < 0 ? 0 : SR.top);
690 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
691 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
692 int sxsize = SCR_FIELDX * TILEX;
693 int sysize = SCR_FIELDY * TILEY;
695 tX = (full_xsize < sxsize ? full_xsize : tX);
696 tY = (full_ysize < sysize ? full_ysize : tY);
697 sX = SX + (full_xsize < sxsize ? (sxsize - full_xsize) / 2 : 0);
698 sY = SY + (full_ysize < sysize ? (sysize - full_ysize) / 2 : 0);
703 if (!menBorder.Checked)
705 SR.left += TILEX / 2;
713 printf("::: DDScrollBuffer.c: DDScrollBuffer_Blt(): blit from %d, %d [%ld, %ld] [%ld, %ld] [%ld, %ld]\n",
714 SR.left, SR.top, mScrollX, mScrollY, mDestXOff, mDestYOff, tX, tY);
718 /* !!! quick and dirty -- FIX THIS !!! */
719 if (tape.playing && tape.fast_forward &&
720 target_bitmap == window &&
721 (FrameCounter % 2) != 0)
722 printf("::: FrameCounter == %d\n", FrameCounter);
730 BlitBitmap(screenBitmap, target_bitmap,
731 SR.left, SR.top, tX, tY, sX, sY);
733 BlitBitmap(screenBitmap, target_bitmap,
735 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
746 // DDraw.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0
747 if (IS_NOTHING(&Buffer, sizeof(Buffer)))
751 if (IS_NOTHING(&PrimarySurface, sizeof(PrimarySurface)))
757 L = PrimarySurface.Blt(DR, &Buffer, SR, DDBLT_WAIT);
767 case DDERR_INVALIDCLIPLIST:
771 case DDERR_INVALIDOBJECT:
775 case DDERR_INVALIDPARAMS:
779 case DDERR_INVALIDRECT:
783 case DDERR_NOALPHAHW:
791 case DDERR_NOCLIPLIST:
795 case DDERR_NODDROPSHW:
799 case DDERR_NOMIRRORHW:
803 case DDERR_NORASTEROPHW:
807 case DDERR_NOROTATIONHW:
811 case DDERR_NOSTRETCHHW:
815 case DDERR_NOZBUFFERHW:
819 case DDERR_SURFACEBUSY:
825 case DDERR_SURFACELOST:
826 DDraw.RestoreAllSurfaces();
827 if (! PrimarySurface.isLost())
833 // RestorePrimarySurface
839 case DDERR_UNSUPPORTED:
843 case DDERR_WASSTILLDRAWING:
857 // Buffer.UpdateOverlay SR, PrimarySurface, DR, DDOVER_SHOW
859 FMark.RefreshMarker();
865 void DDScrollBuffer_Blt()
872 /* !!! TEST ONLY !!! */
873 BlitBitmap(screenBitmap, window,
874 0, 0, SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
878 DDScrollBuffer_Blt_Ext(window);
882 void DDScrollBuffer_ScrollTo(int X, int Y)
895 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTo(): mScroll: %ld, %ld [%d, %d]\n",
896 mScrollX, mScrollY, X, Y);
900 ScrollPlayfieldIfNeeded();
904 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
912 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (1) mScroll: %ld, %ld [%d, %d, %f, %f]\n",
913 mScrollX, mScrollY, X, Y, Step, Stretch);
920 r = Sqr(dx * dx + dY * dY);
921 if (r == 0) // we are there already
929 mScrollX = mScrollX + dx * r;
930 mScrollY = mScrollY + dY * r;
935 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (2) mScroll: %ld, %ld [%d, %d, %f]\n",
936 mScrollX, mScrollY, X, Y, Step);
940 ScrollPlayfieldIfNeeded();
944 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
948 TickCountObject Tick;
952 long oldX, oldY, maxD;
953 static boolean AlreadyRunning = False;
956 printf(":a: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
958 mScrollX_last, mScrollY_last,
959 ScreenScrollXPos, ScreenScrollYPos,
971 AlreadyRunning = True;
976 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
977 StepCount = FPS * (TimeMS / (double)1000);
978 if (StepCount > maxD)
985 tStep = (double)1 / StepCount;
988 // R = Sqr(dX * dX + dY * dY)
989 // If R = 0 Then Exit Sub 'we are there already
990 for (T = (double)tStep; T <= (double)1; T += tStep)
995 // If Claim Then Exit For
998 Tick.DelayMS(dT, False);
1001 mScrollX = oldX + T * dx;
1002 mScrollY = oldY + T * dY;
1012 printf(":x: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
1014 mScrollX_last, mScrollY_last,
1015 ScreenScrollXPos, ScreenScrollYPos,
1023 Tick.DelayMS(dT, False);
1036 AlreadyRunning = False;
1039 printf("::: DDScrollBuffer.c: DDScrollBuffer_SoftScrollTo(): mScroll: %ld, %ld\n",
1040 mScrollX, mScrollY);
1044 printf(":y: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
1046 mScrollX_last, mScrollY_last,
1047 ScreenScrollXPos, ScreenScrollYPos,
1052 ScrollPlayfieldIfNeeded();
1056 printf(":z: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
1058 mScrollX_last, mScrollY_last,
1059 ScreenScrollXPos, ScreenScrollYPos,