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];
42 void RestorePlayfield()
44 int x1 = mScrollX / TILEX - 2;
45 int y1 = mScrollY / TILEY - 2;
46 int x2 = mScrollX / TILEX + (SCR_FIELDX - 1) + 2;
47 int y2 = mScrollY / TILEY + (SCR_FIELDY - 1) + 2;
52 for (y = DisplayMinY; y <= DisplayMaxY; y++)
54 for (x = DisplayMinX; x <= DisplayMaxX; x++)
56 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
58 DrawFieldNoAnimated(x, y);
59 DrawFieldAnimated(x, y);
65 static void ScrollPlayfield(int dx, int dy)
67 int x1 = mScrollX_last / TILEX - 2;
68 int y1 = mScrollY_last / TILEY - 2;
69 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
70 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
73 BlitBitmap(screenBitmap, screenBitmap,
76 (MAX_BUF_XSIZE * TILEX) - TILEX * (dx != 0),
77 (MAX_BUF_YSIZE * TILEY) - TILEY * (dy != 0),
81 /* when scrolling the whole playfield, do not redraw single tiles */
82 for (x = 0; x < MAX_BUF_XSIZE; x++)
83 for (y = 0; y < MAX_BUF_YSIZE; y++)
91 for (y = DisplayMinY; y <= DisplayMaxY; y++)
93 for (x = DisplayMinX; x <= DisplayMaxX; x++)
95 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
99 int tsi = GetSI(x, y);
100 long id = ((PlayField16[tsi]) |
101 (PlayField8[tsi] << 16) |
102 (DisPlayField[tsi] << 24));
106 printf("::: [%d] %d [%d, %d] [%d]\n", dx, sx, x, y, buf_xsize);
108 if (sx == 0 || sx == MAX_BUF_XSIZE - 1)
109 printf("::: %d, %d\n", dx, sx);
113 if ((dx == -1 && x == x2) ||
114 (dx == +1 && x == x1) ||
115 (dy == -1 && y == y2) ||
116 (dy == +1 && y == y1))
119 printf("::: %d, %d\n", sx, sy);
124 DrawFieldNoAnimated(x, y);
125 DrawFieldAnimated(x, y);
130 ScreenBuffer[sx][sy] = id;
136 static void ScrollPlayfieldIfNeededExt(boolean reset)
146 if (mScrollX_last == -1 || mScrollY_last == -1)
148 mScrollX_last = mScrollX;
149 mScrollY_last = mScrollY;
156 /* check if scrolling the playfield requires redrawing the viewport bitmap */
157 if ((mScrollX != mScrollX_last ||
158 mScrollY != mScrollY_last) &&
159 (ABS(mScrollX - mScrollX_last) >= TILEX ||
160 ABS(mScrollY - mScrollY_last) >= TILEY))
162 int dx = (ABS(mScrollX - mScrollX_last) < TILEX ? 0 :
163 mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
164 int dy = (ABS(mScrollY - mScrollY_last) < TILEY ? 0 :
165 mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
167 mScrollX_last -= dx * TILEX;
168 mScrollY_last -= dy * TILEY;
170 ScrollPlayfield(dx, dy);
173 printf("::: %ld, %ld\n", mScrollX, mScrollY);
179 /* check if scrolling the playfield reached the destination tile position */
180 if ((mScrollX != mScrollX_last || mScrollY != mScrollY_last) &&
181 mScrollX % TILEX == 0 && mScrollY % TILEY == 0)
183 int dx = (mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
184 int dy = (mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
186 mScrollX_last = mScrollX;
187 mScrollY_last = mScrollY;
189 ScrollPlayfield(dx, dy);
192 printf("::: %ld, %ld\n", mScrollX, mScrollY);
199 static void ScrollPlayfieldIfNeeded()
201 ScrollPlayfieldIfNeededExt(FALSE);
204 void InitScrollPlayfield()
206 ScrollPlayfieldIfNeededExt(TRUE);
209 void UpdatePlayfield(boolean force_redraw)
216 for (y = DisplayMinY; y <= DisplayMaxY; y++)
218 for (x = DisplayMinX; x <= DisplayMaxX; x++)
220 int element = LowByte(PlayField16[GetSI(x, y)]);
221 int graphic = GfxGraphic[x][y];
222 int sync_frame = GfxFrame[x][y];
223 boolean redraw = force_redraw;
226 redraw = TRUE; // !!! TEST ONLY -- ALWAYS REDRAW !!!
232 if (element != GfxElementLast[x][y] &&
233 graphic == GfxGraphicLast[x][y])
235 /* element changed, but not graphic => disable updating graphic */
237 GfxElementLast[x][y] = element;
238 GfxGraphicLast[x][y] = GfxGraphic[x][y] = -1;
243 if (graphic != GfxGraphicLast[x][y]) // new graphic
247 GfxElementLast[x][y] = element;
248 GfxGraphicLast[x][y] = GfxGraphic[x][y];
249 sync_frame = GfxFrame[x][y] = 0;
251 else if (isNextAnimationFrame_SP(graphic, sync_frame)) // new frame
258 int sx = x * StretchWidth;
259 int sy = y * StretchWidth;
262 printf("::: REDRAW (%d, %d): %d, %d\n", x, y, graphic, sync_frame);
265 StretchedSprites.BltImg(sx, sy, graphic, sync_frame);
275 printf("::: FRAME %d: %d redrawn\n", FrameCounter, num_redrawn);
279 void UpdatePlayfield_TMP()
281 int x1 = mScrollX_last / TILEX - 2;
282 int y1 = mScrollY_last / TILEY - 2;
283 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
284 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
287 for (y = DisplayMinY; y <= DisplayMaxY; y++)
289 for (x = DisplayMinX; x <= DisplayMaxX; x++)
291 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
295 int tsi = GetSI(x, y);
296 long id = ((PlayField16[tsi]) |
297 (PlayField8[tsi] << 16) |
298 (DisPlayField[tsi] << 24));
299 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
302 if (LowByte(PlayField16[tsi]) == fiMurphy)
306 if (redraw_screen_tile)
309 DrawFieldNoAnimated(x, y);
310 DrawFieldAnimated(x, y);
313 ScreenBuffer[sx][sy] = id;
315 redraw[sx][sy] = TRUE;
323 void OLD_UpdatePlayfield()
326 int left = mScrollX / TILEX;
327 int top = mScrollY / TILEY;
329 for (y = top; y < top + MAX_BUF_YSIZE; y++)
331 for (x = left; x < left + MAX_BUF_XSIZE; x++)
333 int sx = x % MAX_BUF_XSIZE;
334 int sy = y % MAX_BUF_YSIZE;
335 int tsi = GetSI(x, y);
336 long id = ((PlayField16[tsi]) |
337 (PlayField8[tsi] << 16) |
338 (DisPlayField[tsi] << 24));
339 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
341 if (redraw_screen_tile)
343 DrawFieldNoAnimated(x, y);
344 DrawFieldAnimated(x, y);
346 ScreenBuffer[sx][sy] = id;
348 redraw[sx][sy] = TRUE;
355 void DDScrollBuffer_Let_DestXOff(long NewVal)
360 long DDScrollBuffer_Get_DestXOff()
364 DestXOff = mDestXOff;
369 void DDScrollBuffer_Let_DestYOff(long NewVal)
374 long DDScrollBuffer_Get_DestYOff()
378 DestYOff = mDestYOff;
383 DirectDrawSurface7 DDScrollBuffer_Get_Surface()
385 DirectDrawSurface7 Surface;
392 long DDScrollBuffer_Get_Width()
401 int DDScrollBuffer_Get_Height()
412 long DDScrollBuffer_CreateAtSize(long Width, long Height, long hWndViewPort)
419 mhWnd = hWndViewPort;
420 // Create ScrollBuffer:
422 SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
423 SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
424 // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
429 // --- On Error Resume Next
430 Buffer = DDraw.CreateSurface(SD);
434 // --- On Error GoTo 0
445 void DDScrollBuffer_Cls(int BackColor)
452 Buffer.BltColorFill(EmptyRect, BackColor);
457 /* copy the entire screen to the window at the scroll position */
459 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
462 int px = 2 * TILEX + mScrollX % TILEX;
463 int py = 2 * TILEY + mScrollY % TILEY;
465 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
466 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
468 int sx, sy, sxsize, sysize;
471 if (mScrollX % TILEX != (mScrollX - mScrollX_last) % TILEX ||
472 mScrollY % TILEY != (mScrollY - mScrollY_last) % TILEY)
473 printf("::: %ld, %ld / %ld, %ld\n",
474 mScrollX, mScrollY, mScrollX_last, mScrollY_last);
480 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
481 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
483 sxsize = (full_xsize < xsize ? full_xsize : xsize);
484 sysize = (full_ysize < ysize ? full_ysize : ysize);
485 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
486 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
491 static int mScrollX_tmp = -1;
492 static int mScrollY_tmp = -1;
494 if (mScrollX != mScrollX_tmp || mScrollY != mScrollY_tmp)
496 printf("::: %ld, %ld\n", mScrollX, mScrollY);
498 mScrollX_tmp = mScrollX;
499 mScrollY_tmp = mScrollY;
506 static boolean x = 0;
510 printf("::: %d, %d (%d, %d) (%d, %d) [%ld, %ld] [%ld, %ld] \n",
511 sx, sy, xsize, ysize, full_xsize, full_ysize,
512 mScrollX, mScrollY, mScrollX_last, mScrollY_last);
520 if (!menBorder.Checked)
528 if (mScrollX >= 0 && mScrollX <= 16)
533 else if (mScrollX >= 352 && mScrollX <= 368)
538 else if (mScrollX >= 16) // && mScrollX <= 352)
545 /* !!! TEST ONLY !!! */
550 BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
553 void OLD_BlitScreenToBitmap_SP(Bitmap *target_bitmap)
555 int x = mScrollX % (MAX_BUF_XSIZE * TILEX);
556 int y = mScrollY % (MAX_BUF_YSIZE * TILEY);
558 if (x < 2 * TILEX && y < 2 * TILEY)
560 BlitBitmap(screenBitmap, target_bitmap, x, y,
561 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
563 else if (x < 2 * TILEX && y >= 2 * TILEY)
565 BlitBitmap(screenBitmap, target_bitmap, x, y,
566 SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
568 BlitBitmap(screenBitmap, target_bitmap, x, 0,
569 SCR_FIELDX * TILEX, y - 2 * TILEY,
570 SX, SY + MAX_BUF_YSIZE * TILEY - y);
572 else if (x >= 2 * TILEX && y < 2 * TILEY)
574 BlitBitmap(screenBitmap, target_bitmap, x, y,
575 MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
577 BlitBitmap(screenBitmap, target_bitmap, 0, y,
578 x - 2 * TILEX, SCR_FIELDY * TILEY,
579 SX + MAX_BUF_XSIZE * TILEX - x, SY);
583 BlitBitmap(screenBitmap, target_bitmap, x, y,
584 MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
586 BlitBitmap(screenBitmap, target_bitmap, 0, y,
587 x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
588 SX + MAX_BUF_XSIZE * TILEX - x, SY);
589 BlitBitmap(screenBitmap, target_bitmap, x, 0,
590 MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
591 SX, SY + MAX_BUF_YSIZE * TILEY - y);
592 BlitBitmap(screenBitmap, target_bitmap, 0, 0,
593 x - 2 * TILEX, y - 2 * TILEY,
594 SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
598 void BackToFront_SP(void)
600 static boolean scrolling_last = FALSE;
601 int left = mScrollX / TILEX;
602 int top = mScrollY / TILEY;
603 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
609 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
612 BlitScreenToBitmap_SP(window);
614 /* blit all (up to four) parts of the scroll buffer to the backbuffer */
615 BlitScreenToBitmap_SP(backbuffer);
617 /* blit the completely updated backbuffer to the window (in one blit) */
618 BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
623 for (x = 0; x < SCR_FIELDX; x++)
625 for (y = 0; y < SCR_FIELDY; y++)
627 int xx = (left + x) % MAX_BUF_XSIZE;
628 int yy = (top + y) % MAX_BUF_YSIZE;
631 BlitBitmap(screenBitmap, window,
632 xx * TILEX, yy * TILEY, TILEX, TILEY,
633 SX + x * TILEX, SY + y * TILEY);
640 for (x = 0; x < MAX_BUF_XSIZE; x++)
641 for (y = 0; y < MAX_BUF_YSIZE; y++)
642 redraw[x][y] = FALSE;
645 scrolling_last = scrolling;
649 void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
667 DR.right = SCR_FIELDX * TILEX;
668 DR.bottom = SCR_FIELDY * TILEY;
670 // --- On Error GoTo BltEH
671 DirectX.GetWindowRect(mhWnd, DR);
672 // --- On Error GoTo 0
676 tX = (DR.right - DR.left) / Stretch;
677 tY = (DR.bottom - DR.top) / Stretch;
681 SR.left = mScrollX + mDestXOff;
682 SR.top = mScrollY + mDestYOff;
684 SR.right = SR.left + tX;
685 SR.bottom = SR.top + tY;
687 // If mWidth < SR.right Then
689 // DR.right = DR.left + Stretch * (SR.right - SR.left)
691 // If mHeight < SR.bottom Then
692 // SR.bottom = mHeight
693 // DR.bottom = DR.top + Stretch * (SR.bottom - SR.top)
695 // If (mScrollX + mDestXOff) < 0 Then
697 // DR.left = DR.left - Stretch * (mScrollX + mDestXOff)
699 // If (mScrollY + mDestYOff) < 0 Then
701 // DR.top = DR.top - Stretch * (mScrollY + mDestYOff)
706 SR.left = (SR.left < 0 ? 0 : SR.left);
707 SR.top = (SR.top < 0 ? 0 : SR.top);
712 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
713 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
714 int sxsize = SCR_FIELDX * TILEX;
715 int sysize = SCR_FIELDY * TILEY;
717 tX = (full_xsize < sxsize ? full_xsize : tX);
718 tY = (full_ysize < sysize ? full_ysize : tY);
719 sX = SX + (full_xsize < sxsize ? (sxsize - full_xsize) / 2 : 0);
720 sY = SY + (full_ysize < sysize ? (sysize - full_ysize) / 2 : 0);
725 if (!menBorder.Checked)
727 SR.left += TILEX / 2;
735 printf("::: DDScrollBuffer.c: DDScrollBuffer_Blt(): blit from %d, %d [%ld, %ld] [%ld, %ld] [%ld, %ld]\n",
736 SR.left, SR.top, mScrollX, mScrollY, mDestXOff, mDestYOff, tX, tY);
740 /* !!! quick and dirty -- FIX THIS !!! */
741 if (tape.playing && tape.fast_forward &&
742 target_bitmap == window &&
743 (FrameCounter % 2) != 0)
744 printf("::: FrameCounter == %d\n", FrameCounter);
752 BlitBitmap(screenBitmap, target_bitmap,
753 SR.left, SR.top, tX, tY, sX, sY);
755 BlitBitmap(screenBitmap, target_bitmap,
757 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
768 // DDraw.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0
769 if (IS_NOTHING(&Buffer, sizeof(Buffer)))
773 if (IS_NOTHING(&PrimarySurface, sizeof(PrimarySurface)))
779 L = PrimarySurface.Blt(DR, &Buffer, SR, DDBLT_WAIT);
789 case DDERR_INVALIDCLIPLIST:
793 case DDERR_INVALIDOBJECT:
797 case DDERR_INVALIDPARAMS:
801 case DDERR_INVALIDRECT:
805 case DDERR_NOALPHAHW:
813 case DDERR_NOCLIPLIST:
817 case DDERR_NODDROPSHW:
821 case DDERR_NOMIRRORHW:
825 case DDERR_NORASTEROPHW:
829 case DDERR_NOROTATIONHW:
833 case DDERR_NOSTRETCHHW:
837 case DDERR_NOZBUFFERHW:
841 case DDERR_SURFACEBUSY:
847 case DDERR_SURFACELOST:
848 DDraw.RestoreAllSurfaces();
849 if (! PrimarySurface.isLost())
855 // RestorePrimarySurface
861 case DDERR_UNSUPPORTED:
865 case DDERR_WASSTILLDRAWING:
879 // Buffer.UpdateOverlay SR, PrimarySurface, DR, DDOVER_SHOW
881 FMark.RefreshMarker();
887 void DDScrollBuffer_Blt()
894 /* !!! TEST ONLY !!! */
895 BlitBitmap(screenBitmap, window,
896 0, 0, SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
900 DDScrollBuffer_Blt_Ext(window);
904 void DDScrollBuffer_ScrollTo(int X, int Y)
917 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTo(): mScroll: %ld, %ld [%d, %d]\n",
918 mScrollX, mScrollY, X, Y);
922 ScrollPlayfieldIfNeeded();
926 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
934 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (1) mScroll: %ld, %ld [%d, %d, %f, %f]\n",
935 mScrollX, mScrollY, X, Y, Step, Stretch);
942 r = Sqr(dx * dx + dY * dY);
943 if (r == 0) // we are there already
951 mScrollX = mScrollX + dx * r;
952 mScrollY = mScrollY + dY * r;
957 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (2) mScroll: %ld, %ld [%d, %d, %f]\n",
958 mScrollX, mScrollY, X, Y, Step);
962 ScrollPlayfieldIfNeeded();
966 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
970 TickCountObject Tick;
974 long oldX, oldY, maxD;
975 static boolean AlreadyRunning = False;
978 printf(":a: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
980 mScrollX_last, mScrollY_last,
981 ScreenScrollXPos, ScreenScrollYPos,
993 AlreadyRunning = True;
998 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
999 StepCount = FPS * (TimeMS / (double)1000);
1000 if (StepCount > maxD)
1007 tStep = (double)1 / StepCount;
1010 // R = Sqr(dX * dX + dY * dY)
1011 // If R = 0 Then Exit Sub 'we are there already
1012 for (T = (double)tStep; T <= (double)1; T += tStep)
1017 // If Claim Then Exit For
1020 Tick.DelayMS(dT, False);
1023 mScrollX = oldX + T * dx;
1024 mScrollY = oldY + T * dY;
1034 printf(":x: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
1036 mScrollX_last, mScrollY_last,
1037 ScreenScrollXPos, ScreenScrollYPos,
1045 Tick.DelayMS(dT, False);
1058 AlreadyRunning = False;
1061 printf("::: DDScrollBuffer.c: DDScrollBuffer_SoftScrollTo(): mScroll: %ld, %ld\n",
1062 mScrollX, mScrollY);
1066 printf(":y: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
1068 mScrollX_last, mScrollY_last,
1069 ScreenScrollXPos, ScreenScrollYPos,
1074 ScrollPlayfieldIfNeeded();
1078 printf(":z: %d, %d [%d, %d] [%d, %d] [%d, %d]\n",
1080 mScrollX_last, mScrollY_last,
1081 ScreenScrollXPos, ScreenScrollYPos,