rnd-20091024-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 0
181   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", pX, pY);
182 #endif
183
184   if (NoDisplayFlag)
185     return;
186
187   {
188     DR.left = pX + mDestXOff;
189     DR.top = pY + mDestYOff;
190     DR.right = pX + mSpriteWidth + mDestXOff;
191     DR.bottom = pY + mSpriteHeight + mDestYOff;
192   }
193   {
194     SR.left = mSpriteWidth * (SpriteX - 1);
195     SR.top = mSpriteHeight * (SpriteY - 1);
196     SR.right = SR.left + mSpriteWidth;
197     SR.bottom = SR.top + mSpriteHeight;
198   }
199
200 #if 0
201   printf("::: DDSpriteBuffer.c: Blt(): %d, %d\n", DR.left, DR.top);
202 #endif
203
204 #if 0
205   if (pX == 0 * StretchWidth && pY == 0 * StretchWidth)
206     printf("::: TEST: drawing topleft corner ...\n");
207   if (pX == 59 * StretchWidth && pY == 23 * StretchWidth)
208     printf("::: TEST: drawing bottomright corner ...\n");
209 #endif
210
211 #if 1
212   BlitBitmap(sp_objects, screenBitmap,
213              SR.left, SR.top,
214              mSpriteWidth, mSpriteHeight,
215              DR.left, DR.top);
216 #else
217   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
218 #endif
219 }
220
221 void DDSpriteBuffer_BltEx(int pX, int pY, int SpritePos)
222 {
223   int XPos, YPos;
224
225   if (NoDisplayFlag)
226     return;
227
228   XPos = (SpritePos % mXSpriteCount) + 1;
229   YPos = (SpritePos / mXSpriteCount) + 1;
230   Blt(pX, pY, XPos, YPos);
231 }
232
233 // Public Function GetStretchCopy(Stretch!) As DDSpriteBuffer
234 // Dim SR As RECT, DR As RECT, Y%, X%, pX%, pY%, Tmp&
235 // //  Set GetStretchCopy = New DDSpriteBuffer // (handle this later, if needed)
236 //  If Not GetStretchCopy.CreateAtSize(Stretch * Width, Stretch * Height, mXSpriteCount, mYSpriteCount) Then
237 //    Set GetStretchCopy = Nothing
238 //  Else
239 //    For Y = 0 To mYSpriteCount - 1
240 //      pY = Y * Stretch * mSpriteHeight
241 //      For X = 0 To mXSpriteCount - 1
242 //        pX = X * Stretch * mSpriteWidth
243 //        With DR
244 //          .left = pX
245 //          .top = pY
246 //          .right = pX + mSpriteWidth * Stretch
247 //          .bottom = pY + mSpriteHeight * Stretch
248 //        End With
249 //        With SR
250 //          .left = mSpriteWidth * X
251 //          .top = mSpriteHeight * Y
252 //          .right = .left + mSpriteWidth
253 //          .bottom = .top + mSpriteHeight
254 //        End With
255 //        Tmp = GetStretchCopy.Surface.Blt(DR, Buffer, SR, DDBLT_WAIT)
256 //      Next X
257 //    Next Y
258 //    'GetStretchCopy.Surface.Blt DR, Buffer, DR, DDBLT_WAIT
259 //  End If
260 // End Function
261
262 #if 0
263
264 static void Class_Initialize()
265 {
266   mDestXOff = 0;
267   mDestYOff = 0;
268 }
269
270 #endif