rnd-20091123-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, 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 boolean DDSpriteBuffer_CreateAtSize(long Width, long Height, long xSprites, long ySprites)
136 {
137   boolean CreateAtSize;
138
139   DDSURFACEDESC2 SD;
140
141   {
142     SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
143     SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
144     // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
145     SD.LWidth = Width;
146     SD.LHeight = Height;
147   }
148
149   // --- On Error GoTo CreateAtSizeEH
150   Buffer = DDraw.CreateSurface(SD);
151   // --- On Error GoTo 0
152
153   mSpriteWidth = Width / xSprites;
154   mSpriteHeight = Height / ySprites;
155   mXSpriteCount = xSprites;
156   mYSpriteCount = ySprites;
157   CreateAtSize = True;
158   return CreateAtSize;
159
160   // CreateAtSizeEH:
161   CreateAtSize = False;
162
163   return CreateAtSize;
164 }
165
166 void DDSpriteBuffer_Cls(int BackColor)
167 {
168   RECT EmptyRect;
169
170   Buffer.BltColorFill(EmptyRect, BackColor);
171 }
172
173 static void Blt(int pX, int pY, int SpriteX, int SpriteY)
174 {
175   RECT DR, SR;
176 #if 0
177   long Tmp;
178 #endif
179
180   int sx1 = mScrollX - TILEX;
181   int sy1 = mScrollY - TILEY;
182   int sx2 = mScrollX + SXSIZE + TILEX;
183   int sy2 = mScrollY + SYSIZE + TILEY;
184   int x1 = sx1 / TILEX;
185   int y1 = sy1 / TILEY;
186 #if 0
187   int x2 = sx2 / TILEX;
188   int y2 = sy2 / TILEY;
189 #endif
190
191   int sx = pX - x1 * TILEX;
192   int sy = pY - y1 * TILEY;
193
194 #if 0
195   printf(":1: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld]\n",
196          pX, pY, mScrollX, mScrollY);
197 #endif
198
199   if (NoDisplayFlag)
200     return;
201
202 #if 1
203   if (TEST_flag)
204   {
205     if (pX < sx1 || pX >= sx2 || pY < sy1 || pY >= sy2)
206       printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%d..%d, %d..%d] (%d, %d) [SKIPPED]\n",
207              pX, pY, sx1, sx2, sy1, sy2, mScrollX, mScrollY);
208     else
209       printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%d..%d, %d..%d] (%d, %d)\n",
210              pX, pY, sx1, sx2, sy1, sy2, mScrollX, mScrollY);
211   }
212 #endif
213
214   /* do not draw fields that are outside the visible screen area */
215   if (pX < sx1 || pX >= sx2 || pY < sy1 || pY >= sy2)
216     return;
217
218 #if 0
219   printf(":2: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld]\n",
220          pX, pY, mScrollX, mScrollY);
221 #endif
222
223   {
224     DR.left = pX + mDestXOff;
225     DR.top = pY + mDestYOff;
226     DR.right = pX + mSpriteWidth + mDestXOff;
227     DR.bottom = pY + mSpriteHeight + mDestYOff;
228   }
229   {
230     SR.left = mSpriteWidth * (SpriteX - 1);
231     SR.top = mSpriteHeight * (SpriteY - 1);
232     SR.right = SR.left + mSpriteWidth;
233     SR.bottom = SR.top + mSpriteHeight;
234   }
235
236 #if 0
237   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", DR.left, DR.top);
238 #endif
239
240 #if 0
241   if (pX == 0 * StretchWidth && pY == 0 * StretchWidth)
242     printf("::: TEST: drawing topleft corner ...\n");
243   if (pX == 59 * StretchWidth && pY == 23 * StretchWidth)
244     printf("::: TEST: drawing bottomright corner ...\n");
245 #endif
246
247 #if 1
248
249 #if 1
250   BlitBitmap(sp_objects, screenBitmap,
251              SR.left, SR.top,
252              mSpriteWidth, mSpriteHeight,
253              sx, sy);
254 #else
255   BlitBitmap(sp_objects, screenBitmap,
256              SR.left, SR.top,
257              mSpriteWidth, mSpriteHeight,
258              DR.left, DR.top);
259 #endif
260
261 #else
262   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
263 #endif
264 }
265
266 #if 0
267
268 static void OLD_Blt(int pX, int pY, int SpriteX, int SpriteY)
269 {
270   RECT DR, SR;
271 #if 0
272   long Tmp;
273 #endif
274
275 #if 1
276   int left = mScrollX;
277   int top  = mScrollY;
278 #else
279   int left = mScrollX / TILEX;
280   int top  = mScrollY / TILEY;
281 #endif
282
283   int sx = pX % (MAX_BUF_XSIZE * TILEX);
284   int sy = pY % (MAX_BUF_YSIZE * TILEY);
285
286 #if 0
287   printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld] [%d, %d]\n",
288          pX, pY, mScrollX, mScrollY, left, top);
289 #endif
290
291   if (NoDisplayFlag)
292     return;
293
294   /* do not draw fields that are outside the visible screen area */
295   if (pX < left || pX >= left + MAX_BUF_XSIZE * TILEX ||
296       pY < top  || pY >= top  + MAX_BUF_YSIZE * TILEY)
297     return;
298
299   {
300     DR.left = pX + mDestXOff;
301     DR.top = pY + mDestYOff;
302     DR.right = pX + mSpriteWidth + mDestXOff;
303     DR.bottom = pY + mSpriteHeight + mDestYOff;
304   }
305   {
306     SR.left = mSpriteWidth * (SpriteX - 1);
307     SR.top = mSpriteHeight * (SpriteY - 1);
308     SR.right = SR.left + mSpriteWidth;
309     SR.bottom = SR.top + mSpriteHeight;
310   }
311
312 #if 0
313   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", DR.left, DR.top);
314 #endif
315
316 #if 0
317   if (pX == 0 * StretchWidth && pY == 0 * StretchWidth)
318     printf("::: TEST: drawing topleft corner ...\n");
319   if (pX == 59 * StretchWidth && pY == 23 * StretchWidth)
320     printf("::: TEST: drawing bottomright corner ...\n");
321 #endif
322
323 #if 1
324
325 #if 1
326   BlitBitmap(sp_objects, screenBitmap,
327              SR.left, SR.top,
328              mSpriteWidth, mSpriteHeight,
329              sx, sy);
330 #else
331   BlitBitmap(sp_objects, screenBitmap,
332              SR.left, SR.top,
333              mSpriteWidth, mSpriteHeight,
334              DR.left, DR.top);
335 #endif
336
337 #else
338   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
339 #endif
340 }
341
342 #endif
343
344 void DDSpriteBuffer_BltEx(int pX, int pY, int SpritePos)
345 {
346   int XPos, YPos;
347
348   if (NoDisplayFlag)
349     return;
350
351   XPos = (SpritePos % mXSpriteCount) + 1;
352   YPos = (SpritePos / mXSpriteCount) + 1;
353
354 #if 0
355   if (TEST_flag)
356     printf("::: DDSpriteBuffer_BltEx(): %d, %d [%d]\n",
357            pX, pY, SpritePos);
358 #endif
359
360   Blt(pX, pY, XPos, YPos);
361 }
362
363 // Public Function GetStretchCopy(Stretch!) As DDSpriteBuffer
364 // Dim SR As RECT, DR As RECT, Y%, X%, pX%, pY%, Tmp&
365 // //  Set GetStretchCopy = New DDSpriteBuffer // (handle this later, if needed)
366 //  If Not GetStretchCopy.CreateAtSize(Stretch * Width, Stretch * Height, mXSpriteCount, mYSpriteCount) Then
367 //    Set GetStretchCopy = Nothing
368 //  Else
369 //    For Y = 0 To mYSpriteCount - 1
370 //      pY = Y * Stretch * mSpriteHeight
371 //      For X = 0 To mXSpriteCount - 1
372 //        pX = X * Stretch * mSpriteWidth
373 //        With DR
374 //          .left = pX
375 //          .top = pY
376 //          .right = pX + mSpriteWidth * Stretch
377 //          .bottom = pY + mSpriteHeight * Stretch
378 //        End With
379 //        With SR
380 //          .left = mSpriteWidth * X
381 //          .top = mSpriteHeight * Y
382 //          .right = .left + mSpriteWidth
383 //          .bottom = .top + mSpriteHeight
384 //        End With
385 //        Tmp = GetStretchCopy.Surface.Blt(DR, Buffer, SR, DDBLT_WAIT)
386 //      Next X
387 //    Next Y
388 //    'GetStretchCopy.Surface.Blt DR, Buffer, DR, DDBLT_WAIT
389 //  End If
390 // End Function
391
392 #if 0
393
394 static void Class_Initialize()
395 {
396   mDestXOff = 0;
397   mDestYOff = 0;
398 }
399
400 #endif