rnd-20090623-4-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   Buffer = DDraw.CreateSurfaceFromFile(Path, SD);
110   // --- On Error GoTo 0
111
112   Buffer.GetSurfaceDesc(SD);
113   mSpriteWidth = SD.LWidth / xSprites;
114   mSpriteHeight = SD.LHeight / ySprites;
115   mXSpriteCount = xSprites;
116   mYSpriteCount = ySprites;
117   CreateFromFile = True;
118   return CreateFromFile;
119
120   // CreateFromFileEH:
121   CreateFromFile = False;
122
123   return CreateFromFile;
124 }
125
126 boolean DDSpriteBuffer_CreateAtSize(long Width, long Height, long xSprites, long ySprites)
127 {
128   boolean CreateAtSize;
129
130   DDSURFACEDESC2 SD;
131
132   {
133     SD.lFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
134     SD.ddsCaps.lCaps = DDSCAPS_VIDEOMEMORY;
135     // SD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
136     SD.LWidth = Width;
137     SD.LHeight = Height;
138   }
139
140   // --- On Error GoTo CreateAtSizeEH
141   Buffer = DDraw.CreateSurface(SD);
142   // --- On Error GoTo 0
143
144   mSpriteWidth = Width / xSprites;
145   mSpriteHeight = Height / ySprites;
146   mXSpriteCount = xSprites;
147   mYSpriteCount = ySprites;
148   CreateAtSize = True;
149   return CreateAtSize;
150
151   // CreateAtSizeEH:
152   CreateAtSize = False;
153
154   return CreateAtSize;
155 }
156
157 void DDSpriteBuffer_Cls(int BackColor)
158 {
159   RECT EmptyRect;
160
161   Buffer.BltColorFill(EmptyRect, BackColor);
162 }
163
164 static void Blt(int pX, int pY, int SpriteX, int SpriteY)
165 {
166   RECT DR, SR;
167   long Tmp;
168
169   if (NoDisplayFlag)
170     return;
171
172   {
173     DR.left = pX + mDestXOff;
174     DR.top = pY + mDestYOff;
175     DR.right = pX + mSpriteWidth + mDestXOff;
176     DR.bottom = pY + mSpriteHeight + mDestYOff;
177   }
178   {
179     SR.left = mSpriteWidth * (SpriteX - 1);
180     SR.top = mSpriteHeight * (SpriteY - 1);
181     SR.right = SR.left + mSpriteWidth;
182     SR.bottom = SR.top + mSpriteHeight;
183   }
184   Tmp = mDest.Blt(DR, &Buffer, SR, DDBLT_WAIT);
185 }
186
187 void DDSpriteBuffer_BltEx(int pX, int pY, int SpritePos)
188 {
189   int XPos, YPos;
190
191   if (NoDisplayFlag)
192     return;
193
194   XPos = (SpritePos % mXSpriteCount) + 1;
195   YPos = (SpritePos / mXSpriteCount) + 1;
196   Blt(pX, pY, XPos, YPos);
197 }
198
199 // Public Function GetStretchCopy(Stretch!) As DDSpriteBuffer
200 // Dim SR As RECT, DR As RECT, Y%, X%, pX%, pY%, Tmp&
201 // //  Set GetStretchCopy = New DDSpriteBuffer // (handle this later, if needed)
202 //  If Not GetStretchCopy.CreateAtSize(Stretch * Width, Stretch * Height, mXSpriteCount, mYSpriteCount) Then
203 //    Set GetStretchCopy = Nothing
204 //  Else
205 //    For Y = 0 To mYSpriteCount - 1
206 //      pY = Y * Stretch * mSpriteHeight
207 //      For X = 0 To mXSpriteCount - 1
208 //        pX = X * Stretch * mSpriteWidth
209 //        With DR
210 //          .left = pX
211 //          .top = pY
212 //          .right = pX + mSpriteWidth * Stretch
213 //          .bottom = pY + mSpriteHeight * Stretch
214 //        End With
215 //        With SR
216 //          .left = mSpriteWidth * X
217 //          .top = mSpriteHeight * Y
218 //          .right = .left + mSpriteWidth
219 //          .bottom = .top + mSpriteHeight
220 //        End With
221 //        Tmp = GetStretchCopy.Surface.Blt(DR, Buffer, SR, DDBLT_WAIT)
222 //      Next X
223 //    Next Y
224 //    'GetStretchCopy.Surface.Blt DR, Buffer, DR, DDBLT_WAIT
225 //  End If
226 // End Function
227
228 #if 0
229
230 static void Class_Initialize()
231 {
232   mDestXOff = 0;
233   mDestYOff = 0;
234 }
235
236 #endif