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;
132 /* check if scrolling the playfield reached the destination tile position */
133 if ((mScrollX != mScrollX_last || mScrollY != mScrollY_last) &&
134 mScrollX % TILEX == 0 && mScrollY % TILEY == 0)
136 int dx = (mScrollX < mScrollX_last ? 1 : mScrollX > mScrollX_last ? -1 : 0);
137 int dy = (mScrollY < mScrollY_last ? 1 : mScrollY > mScrollY_last ? -1 : 0);
139 mScrollX_last = mScrollX;
140 mScrollY_last = mScrollY;
142 ScrollPlayfield(dx, dy);
146 static void ScrollPlayfieldIfNeeded()
148 ScrollPlayfieldIfNeededExt(FALSE);
151 void InitScrollPlayfield()
153 ScrollPlayfieldIfNeededExt(TRUE);
156 void UpdatePlayfield()
158 int x1 = mScrollX_last / TILEX - 2;
159 int y1 = mScrollY_last / TILEY - 2;
160 int x2 = mScrollX_last / TILEX + (SCR_FIELDX - 1) + 2;
161 int y2 = mScrollY_last / TILEY + (SCR_FIELDY - 1) + 2;
164 for (y = DisplayMinY; y <= DisplayMaxY; y++)
166 for (x = DisplayMinX; x <= DisplayMaxX; x++)
168 if (x >= x1 && x <= x2 && y >= y1 && y <= y2)
172 int tsi = GetSI(x, y);
173 long id = ((PlayField16[tsi]) |
174 (PlayField8[tsi] << 16) |
175 (DisPlayField[tsi] << 24));
176 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
179 if (LowByte(PlayField16[tsi]) == fiMurphy)
183 if (redraw_screen_tile)
186 DrawFieldNoAnimated(x, y);
187 DrawFieldAnimated(x, y);
190 ScreenBuffer[sx][sy] = id;
192 redraw[sx][sy] = TRUE;
200 void OLD_UpdatePlayfield()
203 int left = mScrollX / TILEX;
204 int top = mScrollY / TILEY;
206 for (y = top; y < top + MAX_BUF_YSIZE; y++)
208 for (x = left; x < left + MAX_BUF_XSIZE; x++)
210 int sx = x % MAX_BUF_XSIZE;
211 int sy = y % MAX_BUF_YSIZE;
212 int tsi = GetSI(x, y);
213 long id = ((PlayField16[tsi]) |
214 (PlayField8[tsi] << 16) |
215 (DisPlayField[tsi] << 24));
216 boolean redraw_screen_tile = (ScreenBuffer[sx][sy] != id);
218 if (redraw_screen_tile)
220 DrawFieldNoAnimated(x, y);
221 DrawFieldAnimated(x, y);
223 ScreenBuffer[sx][sy] = id;
225 redraw[sx][sy] = TRUE;
232 void DDScrollBuffer_Let_DestXOff(long NewVal)
237 long DDScrollBuffer_Get_DestXOff()
241 DestXOff = mDestXOff;
246 void DDScrollBuffer_Let_DestYOff(long NewVal)
251 long DDScrollBuffer_Get_DestYOff()
255 DestYOff = mDestYOff;
260 DirectDrawSurface7 DDScrollBuffer_Get_Surface()
262 DirectDrawSurface7 Surface;
269 long DDScrollBuffer_Get_Width()
278 int DDScrollBuffer_Get_Height()
287 long DDScrollBuffer_CreateAtSize(long Width, long Height, long hWndViewPort)
294 mhWnd = hWndViewPort;
295 // Create ScrollBuffer:
297 SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
298 SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
299 // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
304 // --- On Error Resume Next
305 Buffer = DDraw.CreateSurface(SD);
309 // --- On Error GoTo 0
320 void DDScrollBuffer_Cls(int BackColor)
327 Buffer.BltColorFill(EmptyRect, BackColor);
331 /* copy the entire screen to the window at the scroll position */
333 void BlitScreenToBitmap_SP(Bitmap *target_bitmap)
335 int px = 2 * TILEX + (mScrollX - mScrollX_last) % TILEX;
336 int py = 2 * TILEY + (mScrollY - mScrollY_last) % TILEY;
337 int sx, sy, sxsize, sysize;
342 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
343 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
345 sxsize = (full_xsize < xsize ? full_xsize : xsize);
346 sysize = (full_ysize < ysize ? full_ysize : ysize);
347 sx = SX + (full_xsize < xsize ? (xsize - full_xsize) / 2 : 0);
348 sy = SY + (full_ysize < ysize ? (ysize - full_ysize) / 2 : 0);
353 static int mScrollX_tmp = -1;
354 static int mScrollY_tmp = -1;
356 if (mScrollX != mScrollX_tmp || mScrollY != mScrollY_tmp)
358 printf("::: %ld, %ld\n", mScrollX, mScrollY);
360 mScrollX_tmp = mScrollX;
361 mScrollY_tmp = mScrollY;
368 static boolean x = 0;
372 printf("::: %d, %d (%d, %d) (%d, %d) [%ld, %ld] [%ld, %ld] \n",
373 sx, sy, xsize, ysize, full_xsize, full_ysize,
374 mScrollX, mScrollY, mScrollX_last, mScrollY_last);
382 if (!menBorder.Checked)
390 if (mScrollX >= 0 && mScrollX <= 16)
395 else if (mScrollX >= 352 && mScrollX <= 368)
400 else if (mScrollX >= 16) // && mScrollX <= 352)
407 /* !!! TEST ONLY !!! */
412 BlitBitmap(screenBitmap, target_bitmap, px, py, sxsize, sysize, sx, sy);
415 void OLD_BlitScreenToBitmap_SP(Bitmap *target_bitmap)
417 int x = mScrollX % (MAX_BUF_XSIZE * TILEX);
418 int y = mScrollY % (MAX_BUF_YSIZE * TILEY);
420 if (x < 2 * TILEX && y < 2 * TILEY)
422 BlitBitmap(screenBitmap, target_bitmap, x, y,
423 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
425 else if (x < 2 * TILEX && y >= 2 * TILEY)
427 BlitBitmap(screenBitmap, target_bitmap, x, y,
428 SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
430 BlitBitmap(screenBitmap, target_bitmap, x, 0,
431 SCR_FIELDX * TILEX, y - 2 * TILEY,
432 SX, SY + MAX_BUF_YSIZE * TILEY - y);
434 else if (x >= 2 * TILEX && y < 2 * TILEY)
436 BlitBitmap(screenBitmap, target_bitmap, x, y,
437 MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
439 BlitBitmap(screenBitmap, target_bitmap, 0, y,
440 x - 2 * TILEX, SCR_FIELDY * TILEY,
441 SX + MAX_BUF_XSIZE * TILEX - x, SY);
445 BlitBitmap(screenBitmap, target_bitmap, x, y,
446 MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
448 BlitBitmap(screenBitmap, target_bitmap, 0, y,
449 x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
450 SX + MAX_BUF_XSIZE * TILEX - x, SY);
451 BlitBitmap(screenBitmap, target_bitmap, x, 0,
452 MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
453 SX, SY + MAX_BUF_YSIZE * TILEY - y);
454 BlitBitmap(screenBitmap, target_bitmap, 0, 0,
455 x - 2 * TILEX, y - 2 * TILEY,
456 SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
460 void BackToFront_SP(void)
462 static boolean scrolling_last = FALSE;
463 int left = mScrollX / TILEX;
464 int top = mScrollY / TILEY;
465 boolean scrolling = (mScrollX % TILEX != 0 || mScrollY % TILEY != 0);
471 redraw_tiles > REDRAWTILES_THRESHOLD || scrolling || scrolling_last)
474 BlitScreenToBitmap_SP(window);
476 /* blit all (up to four) parts of the scroll buffer to the backbuffer */
477 BlitScreenToBitmap_SP(backbuffer);
479 /* blit the completely updated backbuffer to the window (in one blit) */
480 BlitBitmap(backbuffer, window, SX, SY, SXSIZE, SYSIZE, SX, SY);
485 for (x = 0; x < SCR_FIELDX; x++)
487 for (y = 0; y < SCR_FIELDY; y++)
489 int xx = (left + x) % MAX_BUF_XSIZE;
490 int yy = (top + y) % MAX_BUF_YSIZE;
493 BlitBitmap(screenBitmap, window,
494 xx * TILEX, yy * TILEY, TILEX, TILEY,
495 SX + x * TILEX, SY + y * TILEY);
502 for (x = 0; x < MAX_BUF_XSIZE; x++)
503 for (y = 0; y < MAX_BUF_YSIZE; y++)
504 redraw[x][y] = FALSE;
507 scrolling_last = scrolling;
511 void DDScrollBuffer_Blt_Ext(Bitmap *target_bitmap)
525 DR.right = SCR_FIELDX * TILEX;
526 DR.bottom = SCR_FIELDY * TILEY;
528 // --- On Error GoTo BltEH
529 DirectX.GetWindowRect(mhWnd, DR);
530 // --- On Error GoTo 0
534 tX = (DR.right - DR.left) / Stretch;
535 tY = (DR.bottom - DR.top) / Stretch;
539 SR.left = mScrollX + mDestXOff;
540 SR.top = mScrollY + mDestYOff;
542 SR.right = SR.left + tX;
543 SR.bottom = SR.top + tY;
545 // If mWidth < SR.right Then
547 // DR.right = DR.left + Stretch * (SR.right - SR.left)
549 // If mHeight < SR.bottom Then
550 // SR.bottom = mHeight
551 // DR.bottom = DR.top + Stretch * (SR.bottom - SR.top)
553 // If (mScrollX + mDestXOff) < 0 Then
555 // DR.left = DR.left - Stretch * (mScrollX + mDestXOff)
557 // If (mScrollY + mDestYOff) < 0 Then
559 // DR.top = DR.top - Stretch * (mScrollY + mDestYOff)
564 SR.left = (SR.left < 0 ? 0 : SR.left);
565 SR.top = (SR.top < 0 ? 0 : SR.top);
570 int full_xsize = (FieldWidth - (menBorder.Checked ? 0 : 1)) * TILEX;
571 int full_ysize = (FieldHeight - (menBorder.Checked ? 0 : 1)) * TILEY;
572 int sxsize = SCR_FIELDX * TILEX;
573 int sysize = SCR_FIELDY * TILEY;
575 tX = (full_xsize < sxsize ? full_xsize : tX);
576 tY = (full_ysize < sysize ? full_ysize : tY);
577 sX = SX + (full_xsize < sxsize ? (sxsize - full_xsize) / 2 : 0);
578 sY = SY + (full_ysize < sysize ? (sysize - full_ysize) / 2 : 0);
583 if (!menBorder.Checked)
585 SR.left += TILEX / 2;
593 printf("::: DDScrollBuffer.c: DDScrollBuffer_Blt(): blit from %d, %d [%ld, %ld] [%ld, %ld] [%ld, %ld]\n",
594 SR.left, SR.top, mScrollX, mScrollY, mDestXOff, mDestYOff, tX, tY);
598 /* !!! quick and dirty -- FIX THIS !!! */
599 if (tape.playing && tape.fast_forward &&
600 target_bitmap == window &&
601 (FrameCounter % 2) != 0)
602 printf("::: FrameCounter == %d\n", FrameCounter);
610 BlitBitmap(screenBitmap, target_bitmap,
611 SR.left, SR.top, tX, tY, sX, sY);
613 BlitBitmap(screenBitmap, target_bitmap,
615 SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
626 // DDraw.WaitForVerticalBlank DDWAITVB_BLOCKBEGIN, 0
627 if (IS_NOTHING(&Buffer, sizeof(Buffer)))
630 if (IS_NOTHING(&PrimarySurface, sizeof(PrimarySurface)))
633 L = PrimarySurface.Blt(DR, &Buffer, SR, DDBLT_WAIT);
643 case DDERR_INVALIDCLIPLIST:
647 case DDERR_INVALIDOBJECT:
651 case DDERR_INVALIDPARAMS:
655 case DDERR_INVALIDRECT:
659 case DDERR_NOALPHAHW:
667 case DDERR_NOCLIPLIST:
671 case DDERR_NODDROPSHW:
675 case DDERR_NOMIRRORHW:
679 case DDERR_NORASTEROPHW:
683 case DDERR_NOROTATIONHW:
687 case DDERR_NOSTRETCHHW:
691 case DDERR_NOZBUFFERHW:
695 case DDERR_SURFACEBUSY:
700 case DDERR_SURFACELOST:
701 DDraw.RestoreAllSurfaces();
702 if (! PrimarySurface.isLost())
708 // RestorePrimarySurface
713 case DDERR_UNSUPPORTED:
717 case DDERR_WASSTILLDRAWING:
729 // Buffer.UpdateOverlay SR, PrimarySurface, DR, DDOVER_SHOW
731 FMark.RefreshMarker();
737 void DDScrollBuffer_Blt()
744 /* !!! TEST ONLY !!! */
745 BlitBitmap(screenBitmap, window,
746 0, 0, SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
750 DDScrollBuffer_Blt_Ext(window);
754 void DDScrollBuffer_ScrollTo(int X, int Y)
767 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTo(): mScroll: %ld, %ld [%d, %d]\n",
768 mScrollX, mScrollY, X, Y);
772 ScrollPlayfieldIfNeeded();
776 void DDScrollBuffer_ScrollTowards(int X, int Y, double Step)
784 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (1) mScroll: %ld, %ld [%d, %d, %f]\n",
785 mScrollX, mScrollY, X, Y, Step);
792 r = Sqr(dx * dx + dY * dY);
793 if (r == 0) // we are there already
801 mScrollX = mScrollX + dx * r;
802 mScrollY = mScrollY + dY * r;
807 printf("::: DDScrollBuffer.c: DDScrollBuffer_ScrollTowards(): (2) mScroll: %ld, %ld [%d, %d, %f]\n",
808 mScrollX, mScrollY, X, Y, Step);
812 ScrollPlayfieldIfNeeded();
816 void DDScrollBuffer_SoftScrollTo(int X, int Y, long TimeMS, int FPS)
820 TickCountObject Tick;
824 long oldX, oldY, maxD;
825 static boolean AlreadyRunning = False;
835 AlreadyRunning = True;
840 maxD = (Abs(dx) < Abs(dY) ? Abs(dY) : Abs(dx));
841 StepCount = FPS * (TimeMS / (double)1000);
842 if (StepCount > maxD)
849 tStep = (double)1 / StepCount;
852 // R = Sqr(dX * dX + dY * dY)
853 // If R = 0 Then Exit Sub 'we are there already
854 for (T = (double)tStep; T <= (double)1; T += tStep)
859 // If Claim Then Exit For
862 Tick.DelayMS(dT, False);
865 mScrollX = oldX + T * dx;
866 mScrollY = oldY + T * dY;
876 Tick.DelayMS(dT, False);
886 AlreadyRunning = False;
889 printf("::: DDScrollBuffer.c: DDScrollBuffer_SoftScrollTo(): mScroll: %ld, %ld\n",
894 ScrollPlayfieldIfNeeded();