rnd-20100224-1-src
[rocksndiamonds.git] / src / game_sp / DDSpriteBuffer.c
1 // ----------------------------------------------------------------------------
2 // DDSpriteBuffer.c
3 // ----------------------------------------------------------------------------
4
5 #include "DDSpriteBuffer.h"
6
7 static void Blt(int pX, int pY, Bitmap *bitmap, int SpriteX, int SpriteY);
8
9 // --- VERSION 1.0 CLASS
10 // --- BEGIN
11 // ---   MultiUse = -1  'True  // True
12 // ---   Persistable = 0  'NotPersistable  // NotPersistable
13 // ---   DataBindingBehavior = 0  'vbNone  // vbNone
14 // ---   DataSourceBehavior  = 0  'vbNone  // vbNone
15 // ---   MTSTransactionMode  = 0  'NotAnMTSObject  // NotAnMTSObject
16 // --- END
17
18 // static char *VB_Name = "DDSpriteBuffer";
19 // static boolean VB_GlobalNameSpace = False;
20 // static boolean VB_Creatable = True;
21 // static boolean VB_PredeclaredId = False;
22 // static boolean VB_Exposed = False;
23 // --- Option Explicit
24
25 // needs reference to: DirectX7 for Visual Basic Type Library
26
27 DirectDrawSurface7 Buffer;
28 DirectDrawSurface7 mDest;
29 long mXSpriteCount, mYSpriteCount;
30 long mSpriteWidth, mSpriteHeight;
31 long mDestXOff, mDestYOff;
32
33 void DDSpriteBuffer_Let_DestXOff(long NewVal)
34 {
35   mDestXOff = NewVal;
36 }
37
38 long DDSpriteBuffer_Get_DestXOff()
39 {
40   long DestXOff;
41
42   DestXOff = mDestXOff;
43
44   return DestXOff;
45 }
46
47 void DDSpriteBuffer_Let_DestYOff(long NewVal)
48 {
49   mDestYOff = NewVal;
50 }
51
52 long DDSpriteBuffer_Get_DestYOff()
53 {
54   long DestYOff;
55
56   DestYOff = mDestYOff;
57
58   return DestYOff;
59 }
60
61 int DDSpriteBuffer_Set_DestinationSurface(DirectDrawSurface7 DSurface)
62 {
63   int DestinationSurface;
64
65   mDest = DSurface;
66
67   return DestinationSurface;
68 }
69
70 DirectDrawSurface7 DDSpriteBuffer_Get_Surface()
71 {
72   DirectDrawSurface7 Surface;
73
74   Surface = Buffer;
75
76   return Surface;
77 }
78
79 long DDSpriteBuffer_Get_Width()
80 {
81   long Width;
82
83   Width = mSpriteWidth * mXSpriteCount;
84
85   return Width;
86 }
87
88 int DDSpriteBuffer_Get_Height()
89 {
90   int Height;
91
92   Height = mSpriteHeight * mYSpriteCount;
93
94   return Height;
95 }
96
97 boolean DDSpriteBuffer_CreateFromFile(char *Path, long xSprites, long ySprites)
98 {
99   boolean CreateFromFile;
100
101   DDSURFACEDESC2 SD;
102
103   {
104     SD.lFlags = DDSD_CAPS; // Or DDSD_WIDTH Or DDSD_HEIGHT
105     SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY; // DDSCAPS_SYSTEMMEMORY 'DDSCAPS_OFFSCREENPLAIN
106   }
107
108   // --- On Error GoTo CreateFromFileEH
109 #if 1
110   SD.LWidth  = 16 * TILEX;
111   SD.LHeight = 16 * TILEY;
112 #else
113   Buffer = DDraw.CreateSurfaceFromFile(Path, SD);
114 #endif
115   // --- On Error GoTo 0
116
117 #if 0
118   Buffer.GetSurfaceDesc(SD);
119 #endif
120
121   mSpriteWidth = SD.LWidth / xSprites;
122   mSpriteHeight = SD.LHeight / ySprites;
123   mXSpriteCount = xSprites;
124   mYSpriteCount = ySprites;
125
126   CreateFromFile = True;
127   return CreateFromFile;
128
129   // CreateFromFileEH:
130   CreateFromFile = False;
131
132   return CreateFromFile;
133 }
134
135 #if 0
136
137 boolean DDSpriteBuffer_CreateAtSize(long Width, long Height, long xSprites, long ySprites)
138 {
139   boolean CreateAtSize;
140
141   DDSURFACEDESC2 SD;
142
143   {
144     SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
145     SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
146     // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
147     SD.LWidth = Width;
148     SD.LHeight = Height;
149   }
150
151   // --- On Error GoTo CreateAtSizeEH
152   Buffer = DDraw.CreateSurface(SD);
153   // --- On Error GoTo 0
154
155   mSpriteWidth = Width / xSprites;
156   mSpriteHeight = Height / ySprites;
157   mXSpriteCount = xSprites;
158   mYSpriteCount = ySprites;
159   CreateAtSize = True;
160   return CreateAtSize;
161
162   // CreateAtSizeEH:
163   CreateAtSize = False;
164
165   return CreateAtSize;
166 }
167
168 #endif
169
170 #if 0
171
172 void DDSpriteBuffer_Cls(int BackColor)
173 {
174   RECT EmptyRect;
175
176   Buffer.BltColorFill(EmptyRect, BackColor);
177 }
178
179 #endif
180
181 static void Blt(int pX, int pY, Bitmap *bitmap, int SpriteX, int SpriteY)
182 {
183   MyRECT DR, SR;
184 #if 0
185   long Tmp;
186 #endif
187
188 #if 1
189
190   int scx = (mScrollX_last < 0 ? 0 : mScrollX_last);
191   int scy = (mScrollY_last < 0 ? 0 : mScrollY_last);
192   int sx1 = scx - 2 * TILEX;
193   int sy1 = scy - 2 * TILEY;
194   int sx2 = scx + SXSIZE + 1 * TILEX;
195   int sy2 = scy + SYSIZE + 1 * TILEY;
196
197   int sx = pX - sx1;
198   int sy = pY - sy1;
199
200 #else
201
202   int sx1 = mScrollX - TILEX;
203   int sy1 = mScrollY - TILEY;
204   int sx2 = mScrollX + SXSIZE + TILEX;
205   int sy2 = mScrollY + SYSIZE + TILEY;
206   int x1 = sx1 / TILEX;
207   int y1 = sy1 / TILEY;
208
209 #if 0
210   int x2 = sx2 / TILEX;
211   int y2 = sy2 / TILEY;
212 #endif
213
214   int sx = pX - x1 * TILEX;
215   int sy = pY - y1 * TILEY;
216
217 #endif
218
219 #if 0
220   printf(":1: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld]\n",
221          pX, pY, mScrollX, mScrollY);
222 #endif
223
224   if (NoDisplayFlag)
225     return;
226
227 #if 0
228   if (TEST_flag)
229   {
230     if (pX < sx1 || pX > sx2 || pY < sy1 || pY > sy2)
231       printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%d..%d, %d..%d] (%ld, %ld) (%ld, %ld) [SKIPPED]\n",
232              pX, pY, sx1, sx2, sy1, sy2, mScrollX, mScrollY, mScrollX_last, mScrollY_last);
233     else
234       printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%d..%d, %d..%d] (%ld, %ld) (%ld, %ld)\n",
235              pX, pY, sx1, sx2, sy1, sy2, mScrollX, mScrollY, mScrollX_last, mScrollY_last);
236   }
237 #endif
238
239   /* do not draw fields that are outside the visible screen area */
240   if (pX < sx1 || pX > sx2 || pY < sy1 || pY > sy2)
241     return;
242
243 #if 0
244   printf(":2: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld]\n",
245          pX, pY, mScrollX, mScrollY);
246 #endif
247
248   {
249     DR.left = pX + mDestXOff;
250     DR.top = pY + mDestYOff;
251     DR.right = pX + mSpriteWidth + mDestXOff;
252     DR.bottom = pY + mSpriteHeight + mDestYOff;
253   }
254   {
255 #if 1
256     SR.left = SpriteX;
257     SR.top = SpriteY;
258 #else
259     SR.left = mSpriteWidth * (SpriteX - 1);
260     SR.top = mSpriteHeight * (SpriteY - 1);
261 #endif
262     SR.right = SR.left + mSpriteWidth;
263     SR.bottom = SR.top + mSpriteHeight;
264   }
265
266 #if 0
267   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", DR.left, DR.top);
268 #endif
269
270 #if 0
271   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", sx, sy);
272 #endif
273
274 #if 0
275   if (pX == 0 * StretchWidth && pY == 0 * StretchWidth)
276     printf("::: TEST: drawing topleft corner ...\n");
277   if (pX == 59 * StretchWidth && pY == 23 * StretchWidth)
278     printf("::: TEST: drawing bottomright corner ...\n");
279 #endif
280
281 #if 1
282
283 #if 1
284   BlitBitmap(bitmap, screenBitmap,
285              SR.left, SR.top,
286              mSpriteWidth, mSpriteHeight,
287              sx, sy);
288 #else
289   BlitBitmap(sp_objects, screenBitmap,
290              SR.left, SR.top,
291              mSpriteWidth, mSpriteHeight,
292              DR.left, DR.top);
293 #endif
294
295 #else
296   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
297 #endif
298 }
299
300 #if 0
301
302 static void OLD_Blt(int pX, int pY, int SpriteX, int SpriteY)
303 {
304   RECT DR, SR;
305 #if 0
306   long Tmp;
307 #endif
308
309 #if 1
310   int left = mScrollX;
311   int top  = mScrollY;
312 #else
313   int left = mScrollX / TILEX;
314   int top  = mScrollY / TILEY;
315 #endif
316
317   int sx = pX % (MAX_BUF_XSIZE * TILEX);
318   int sy = pY % (MAX_BUF_YSIZE * TILEY);
319
320 #if 0
321   printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld] [%d, %d]\n",
322          pX, pY, mScrollX, mScrollY, left, top);
323 #endif
324
325   if (NoDisplayFlag)
326     return;
327
328   /* do not draw fields that are outside the visible screen area */
329   if (pX < left || pX >= left + MAX_BUF_XSIZE * TILEX ||
330       pY < top  || pY >= top  + MAX_BUF_YSIZE * TILEY)
331     return;
332
333   {
334     DR.left = pX + mDestXOff;
335     DR.top = pY + mDestYOff;
336     DR.right = pX + mSpriteWidth + mDestXOff;
337     DR.bottom = pY + mSpriteHeight + mDestYOff;
338   }
339   {
340     SR.left = mSpriteWidth * (SpriteX - 1);
341     SR.top = mSpriteHeight * (SpriteY - 1);
342     SR.right = SR.left + mSpriteWidth;
343     SR.bottom = SR.top + mSpriteHeight;
344   }
345
346 #if 0
347   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", DR.left, DR.top);
348 #endif
349
350 #if 0
351   if (pX == 0 * StretchWidth && pY == 0 * StretchWidth)
352     printf("::: TEST: drawing topleft corner ...\n");
353   if (pX == 59 * StretchWidth && pY == 23 * StretchWidth)
354     printf("::: TEST: drawing bottomright corner ...\n");
355 #endif
356
357 #if 1
358
359 #if 1
360   BlitBitmap(sp_objects, screenBitmap,
361              SR.left, SR.top,
362              mSpriteWidth, mSpriteHeight,
363              sx, sy);
364 #else
365   BlitBitmap(sp_objects, screenBitmap,
366              SR.left, SR.top,
367              mSpriteWidth, mSpriteHeight,
368              DR.left, DR.top);
369 #endif
370
371 #else
372   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
373 #endif
374 }
375
376 #endif
377
378 #if 0
379
380 void DDSpriteBuffer_BltEx(int pX, int pY, int SpritePos)
381 {
382   int XPos, YPos;
383
384   if (NoDisplayFlag)
385     return;
386
387 #if 1
388   XPos = mSpriteWidth  * (SpritePos % mXSpriteCount);
389   YPos = mSpriteHeight * (SpritePos / mXSpriteCount);
390 #else
391   XPos = (SpritePos % mXSpriteCount) + 1;
392   YPos = (SpritePos / mXSpriteCount) + 1;
393 #endif
394
395 #if 0
396   printf("::: BltEx: %d, %d, %d, %d\n", pX, pY, XPos, YPos);
397 #endif
398
399 #if 0
400   if (TEST_flag)
401     printf("::: DDSpriteBuffer_BltEx(): %d, %d [%d]\n",
402            pX, pY, SpritePos);
403 #endif
404
405   Blt(pX, pY, sp_objects, XPos, YPos);
406 }
407
408 #endif
409
410 void DDSpriteBuffer_BltImg(int pX, int pY, int graphic, int sync_frame)
411 {
412   struct GraphicInfo_SP g;
413
414   if (NoDisplayFlag)
415     return;
416
417   getGraphicSource_SP(&g, graphic, sync_frame, -1, -1);
418
419 #if 0
420   printf("::: BltImg: %d, %d, %d, %d\n", pX, pY, g.src_x, g.src_y);
421 #endif
422
423   Blt(pX, pY, g.bitmap, g.src_x, g.src_y);
424 }
425
426 // Public Function GetStretchCopy(Stretch!) As DDSpriteBuffer
427 // Dim SR As RECT, DR As RECT, Y%, X%, pX%, pY%, Tmp&
428 // //  Set GetStretchCopy = New DDSpriteBuffer // (handle this later, if needed)
429 //  If Not GetStretchCopy.CreateAtSize(Stretch * Width, Stretch * Height, mXSpriteCount, mYSpriteCount) Then
430 //    Set GetStretchCopy = Nothing
431 //  Else
432 //    For Y = 0 To mYSpriteCount - 1
433 //      pY = Y * Stretch * mSpriteHeight
434 //      For X = 0 To mXSpriteCount - 1
435 //        pX = X * Stretch * mSpriteWidth
436 //        With DR
437 //          .left = pX
438 //          .top = pY
439 //          .right = pX + mSpriteWidth * Stretch
440 //          .bottom = pY + mSpriteHeight * Stretch
441 //        End With
442 //        With SR
443 //          .left = mSpriteWidth * X
444 //          .top = mSpriteHeight * Y
445 //          .right = .left + mSpriteWidth
446 //          .bottom = .top + mSpriteHeight
447 //        End With
448 //        Tmp = GetStretchCopy.Surface.Blt(DR, Buffer, SR, DDBLT_WAIT)
449 //      Next X
450 //    Next Y
451 //    'GetStretchCopy.Surface.Blt DR, Buffer, DR, DDBLT_WAIT
452 //  End If
453 // End Function
454
455 #if 0
456
457 static void Class_Initialize()
458 {
459   mDestXOff = 0;
460   mDestYOff = 0;
461 }
462
463 #endif