rnd-20091124-2-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 #if 1
181
182   int scx = (mScrollX_last < 0 ? 0 : mScrollX_last);
183   int scy = (mScrollY_last < 0 ? 0 : mScrollY_last);
184   int sx1 = scx - 2 * TILEX;
185   int sy1 = scy - 2 * TILEY;
186   int sx2 = scx + SXSIZE + 1 * TILEX;
187   int sy2 = scy + SYSIZE + 1 * TILEY;
188
189   int sx = pX - sx1;
190   int sy = pY - sy1;
191
192 #else
193
194   int sx1 = mScrollX - TILEX;
195   int sy1 = mScrollY - TILEY;
196   int sx2 = mScrollX + SXSIZE + TILEX;
197   int sy2 = mScrollY + SYSIZE + TILEY;
198   int x1 = sx1 / TILEX;
199   int y1 = sy1 / TILEY;
200
201 #if 0
202   int x2 = sx2 / TILEX;
203   int y2 = sy2 / TILEY;
204 #endif
205
206   int sx = pX - x1 * TILEX;
207   int sy = pY - y1 * TILEY;
208
209 #endif
210
211 #if 0
212   printf(":1: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld]\n",
213          pX, pY, mScrollX, mScrollY);
214 #endif
215
216   if (NoDisplayFlag)
217     return;
218
219 #if 0
220   if (TEST_flag)
221   {
222     if (pX < sx1 || pX > sx2 || pY < sy1 || pY > sy2)
223       printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%d..%d, %d..%d] (%ld, %ld) (%ld, %ld) [SKIPPED]\n",
224              pX, pY, sx1, sx2, sy1, sy2, mScrollX, mScrollY, mScrollX_last, mScrollY_last);
225     else
226       printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%d..%d, %d..%d] (%ld, %ld) (%ld, %ld)\n",
227              pX, pY, sx1, sx2, sy1, sy2, mScrollX, mScrollY, mScrollX_last, mScrollY_last);
228   }
229 #endif
230
231   /* do not draw fields that are outside the visible screen area */
232   if (pX < sx1 || pX > sx2 || pY < sy1 || pY > sy2)
233     return;
234
235 #if 0
236   printf(":2: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld]\n",
237          pX, pY, mScrollX, mScrollY);
238 #endif
239
240   {
241     DR.left = pX + mDestXOff;
242     DR.top = pY + mDestYOff;
243     DR.right = pX + mSpriteWidth + mDestXOff;
244     DR.bottom = pY + mSpriteHeight + mDestYOff;
245   }
246   {
247     SR.left = mSpriteWidth * (SpriteX - 1);
248     SR.top = mSpriteHeight * (SpriteY - 1);
249     SR.right = SR.left + mSpriteWidth;
250     SR.bottom = SR.top + mSpriteHeight;
251   }
252
253 #if 0
254   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", DR.left, DR.top);
255 #endif
256
257 #if 0
258   if (pX == 0 * StretchWidth && pY == 0 * StretchWidth)
259     printf("::: TEST: drawing topleft corner ...\n");
260   if (pX == 59 * StretchWidth && pY == 23 * StretchWidth)
261     printf("::: TEST: drawing bottomright corner ...\n");
262 #endif
263
264 #if 1
265
266 #if 1
267   BlitBitmap(sp_objects, screenBitmap,
268              SR.left, SR.top,
269              mSpriteWidth, mSpriteHeight,
270              sx, sy);
271 #else
272   BlitBitmap(sp_objects, screenBitmap,
273              SR.left, SR.top,
274              mSpriteWidth, mSpriteHeight,
275              DR.left, DR.top);
276 #endif
277
278 #else
279   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
280 #endif
281 }
282
283 #if 0
284
285 static void OLD_Blt(int pX, int pY, int SpriteX, int SpriteY)
286 {
287   RECT DR, SR;
288 #if 0
289   long Tmp;
290 #endif
291
292 #if 1
293   int left = mScrollX;
294   int top  = mScrollY;
295 #else
296   int left = mScrollX / TILEX;
297   int top  = mScrollY / TILEY;
298 #endif
299
300   int sx = pX % (MAX_BUF_XSIZE * TILEX);
301   int sy = pY % (MAX_BUF_YSIZE * TILEY);
302
303 #if 0
304   printf("::: DDSpriteBuffer.c: Blt(): %d, %d [%ld, %ld] [%d, %d]\n",
305          pX, pY, mScrollX, mScrollY, left, top);
306 #endif
307
308   if (NoDisplayFlag)
309     return;
310
311   /* do not draw fields that are outside the visible screen area */
312   if (pX < left || pX >= left + MAX_BUF_XSIZE * TILEX ||
313       pY < top  || pY >= top  + MAX_BUF_YSIZE * TILEY)
314     return;
315
316   {
317     DR.left = pX + mDestXOff;
318     DR.top = pY + mDestYOff;
319     DR.right = pX + mSpriteWidth + mDestXOff;
320     DR.bottom = pY + mSpriteHeight + mDestYOff;
321   }
322   {
323     SR.left = mSpriteWidth * (SpriteX - 1);
324     SR.top = mSpriteHeight * (SpriteY - 1);
325     SR.right = SR.left + mSpriteWidth;
326     SR.bottom = SR.top + mSpriteHeight;
327   }
328
329 #if 0
330   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", DR.left, DR.top);
331 #endif
332
333 #if 0
334   if (pX == 0 * StretchWidth && pY == 0 * StretchWidth)
335     printf("::: TEST: drawing topleft corner ...\n");
336   if (pX == 59 * StretchWidth && pY == 23 * StretchWidth)
337     printf("::: TEST: drawing bottomright corner ...\n");
338 #endif
339
340 #if 1
341
342 #if 1
343   BlitBitmap(sp_objects, screenBitmap,
344              SR.left, SR.top,
345              mSpriteWidth, mSpriteHeight,
346              sx, sy);
347 #else
348   BlitBitmap(sp_objects, screenBitmap,
349              SR.left, SR.top,
350              mSpriteWidth, mSpriteHeight,
351              DR.left, DR.top);
352 #endif
353
354 #else
355   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
356 #endif
357 }
358
359 #endif
360
361 void DDSpriteBuffer_BltEx(int pX, int pY, int SpritePos)
362 {
363   int XPos, YPos;
364
365   if (NoDisplayFlag)
366     return;
367
368   XPos = (SpritePos % mXSpriteCount) + 1;
369   YPos = (SpritePos / mXSpriteCount) + 1;
370
371 #if 0
372   if (TEST_flag)
373     printf("::: DDSpriteBuffer_BltEx(): %d, %d [%d]\n",
374            pX, pY, SpritePos);
375 #endif
376
377   Blt(pX, pY, XPos, YPos);
378 }
379
380 // Public Function GetStretchCopy(Stretch!) As DDSpriteBuffer
381 // Dim SR As RECT, DR As RECT, Y%, X%, pX%, pY%, Tmp&
382 // //  Set GetStretchCopy = New DDSpriteBuffer // (handle this later, if needed)
383 //  If Not GetStretchCopy.CreateAtSize(Stretch * Width, Stretch * Height, mXSpriteCount, mYSpriteCount) Then
384 //    Set GetStretchCopy = Nothing
385 //  Else
386 //    For Y = 0 To mYSpriteCount - 1
387 //      pY = Y * Stretch * mSpriteHeight
388 //      For X = 0 To mXSpriteCount - 1
389 //        pX = X * Stretch * mSpriteWidth
390 //        With DR
391 //          .left = pX
392 //          .top = pY
393 //          .right = pX + mSpriteWidth * Stretch
394 //          .bottom = pY + mSpriteHeight * Stretch
395 //        End With
396 //        With SR
397 //          .left = mSpriteWidth * X
398 //          .top = mSpriteHeight * Y
399 //          .right = .left + mSpriteWidth
400 //          .bottom = .top + mSpriteHeight
401 //        End With
402 //        Tmp = GetStretchCopy.Surface.Blt(DR, Buffer, SR, DDBLT_WAIT)
403 //      Next X
404 //    Next Y
405 //    'GetStretchCopy.Surface.Blt DR, Buffer, DR, DDBLT_WAIT
406 //  End If
407 // End Function
408
409 #if 0
410
411 static void Class_Initialize()
412 {
413   mDestXOff = 0;
414   mDestYOff = 0;
415 }
416
417 #endif