rnd-20100316-1-src
[rocksndiamonds.git] / src / game_sp / MainForm.c
1 // ----------------------------------------------------------------------------
2 // MainForm.c
3 // ----------------------------------------------------------------------------
4
5 #include "MainForm.h"
6
7
8 static void DrawFrame(int Delta);
9 static void ReStretch(float NewStretch);
10 static void picViewPort_Resize();
11
12 void DrawField(int X, int Y);
13 void DrawFieldAnimated(int X, int Y);
14 void DrawFieldNoAnimated(int X, int Y);
15
16 boolean Loaded;
17
18 void DrawFrameIfNeeded()
19 {
20   DrawFrame(0);
21
22   /* !!! CHECK THIS !!! */
23 #if 1
24   if (! menBorder)
25     DrawFrame(1);
26 #endif
27 }
28
29 void DisplayLevel()
30 {
31   int X, Y;
32
33   if (! Loaded)
34     return;
35
36   if (! LevelLoaded)
37     return;
38
39   ClearRectangle(backbuffer, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
40   ClearRectangle(screenBitmap, 0, 0,
41                  MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY);
42
43 #if 1
44   SetDisplayRegion();
45 #endif
46
47   DrawFrame(0);
48
49   /* !!! CHECK THIS !!! */
50 #if 1
51   if (! menBorder)
52     DrawFrame(1);
53 #endif
54
55   if (bPlaying)
56   {
57 #if 0
58     printf("::: MainForm.c: DisplayLevel(): [%ld, %ld, %ld, %ld] [%d, %d]...\n",
59            DisplayMinX, DisplayMinY, DisplayMaxX, DisplayMaxY,
60            FieldWidth, FieldHeight);
61 #endif
62
63     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
64     {
65       for (X = DisplayMinX; X <= DisplayMaxX; X++)
66       {
67         DrawFieldNoAnimated(X, Y);
68       }
69     }
70
71     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
72     {
73       for (X = DisplayMinX; X <= DisplayMaxX; X++)
74       {
75         DrawFieldAnimated(X, Y);
76       }
77     }
78
79   }
80   else
81   {
82     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
83     {
84       for (X = DisplayMinX; X <= DisplayMaxX; X++)
85       {
86         DrawField(X, Y);
87       }
88     }
89   }
90 }
91
92 void Form_Load()
93 {
94   Loaded = False;
95
96   InitGlobals();
97
98   Loaded = True;
99
100   ReStretch(Stretch);
101 }
102
103 static void DrawFrame(int Delta)
104 {
105   int i, LX, tY, RX, BY;
106
107   LX = -1 + Delta;
108   tY = -1 + Delta;
109   RX = FieldWidth - Delta;
110   BY = FieldHeight - Delta;
111   DrawImage(LX, tY, imgFrameCorner);
112   DrawImage(LX, BY, imgFrameCorner);
113   DrawImage(RX, tY, imgFrameCorner);
114   DrawImage(RX, BY, imgFrameCorner);
115   for (i = LX + 1; i <= RX - 1; i++)
116   {
117     DrawImage(i, tY, imgFrameHorizontal);
118     DrawImage(i, BY, imgFrameHorizontal);
119   }
120
121   for (i = tY + 1; i <= BY - 1; i++)
122   {
123     DrawImage(LX, i, imgFrameVertical);
124     DrawImage(RX, i, imgFrameVertical);
125   }
126 }
127
128 static void RestoreFrame()
129 {
130   int i, LX, tY, RX, BY;
131
132   LX = 0;
133   tY = 0;
134   RX = FieldWidth - 1;
135   BY = FieldHeight - 1;
136
137   for (i = LX; i <= RX; i++)
138   {
139     DrawField(i, tY);
140     DrawField(i, BY);
141   }
142
143   for (i = tY + 1; i <= BY - 1; i++)
144   {
145     DrawField(LX, i);
146     DrawField(RX, i);
147   }
148 }
149
150 void SetDisplayRegion()
151 {
152   if (! menBorder)
153   {
154     DisplayMinX = 1;
155     DisplayMaxX = FieldWidth - 2;
156     DisplayWidth = FieldWidth;
157     DisplayMinY = 1;
158     DisplayMaxY = FieldHeight - 2;
159     DisplayHeight = FieldHeight;
160
161     if (LevelLoaded)
162       DrawFrame(1);
163   }
164   else
165   {
166     DisplayMinX = 0;
167     DisplayMaxX = FieldWidth - 1;
168     DisplayWidth = FieldWidth + 2;
169     DisplayMinY = 0;
170     DisplayMaxY = FieldHeight - 1;
171     DisplayHeight = FieldHeight + 2;
172
173     if (LevelLoaded)
174       RestoreFrame();
175   }
176 }
177
178 void menPlay_Click()
179 {
180   bPlaying = True;
181
182   subFetchAndInitLevelB();
183
184   ReStretch(Stretch);
185
186   subMainGameLoop_Init();
187
188 #if 1
189   return;
190 #endif
191
192   bPlaying = False;
193
194   subFetchAndInitLevel();
195 }
196
197 static void ReStretch(float NewStretch)
198 {
199   long BW2, LW, LH;
200
201   if (! Loaded)
202   {
203     Stretch = NewStretch;
204
205     return;
206   }
207
208   Stretch = NewStretch;
209
210   BW2 = StretchWidth / 2;
211   LW = (FieldWidth + 2) * BaseWidth; // StretchWidth
212   LH = (FieldHeight + 2) * BaseWidth; // StretchWidth
213
214   if (Loaded && LevelLoaded)
215   {
216     SetDisplayRegion();
217     picViewPort_Resize();
218     DisplayLevel();
219   }
220
221   subCalculateScreenScrollPos();
222
223   ScrollTo(ScreenScrollXPos, ScreenScrollYPos);
224 }
225
226 void SetScrollEdges()
227 {
228   ScrollMinX = (int)(DisplayMinX - 0.5) * Stretch * BaseWidth;
229   ScrollMinY = (int)(DisplayMinY - 0.5) * Stretch * BaseWidth;
230   ScrollMaxX = (int)(DisplayMaxX + 1.5) * Stretch * BaseWidth - SXSIZE;
231   ScrollMaxY = (int)(DisplayMaxY + 1.5) * Stretch * BaseWidth - SYSIZE;
232 }
233
234 void DrawField(int X, int Y)
235 {
236   int Tmp, tsi;
237
238   tsi = GetSI(X, Y);
239   Tmp = LowByte(PlayField16[tsi]);
240   if (Tmp > 40)
241     Tmp = 0;
242
243   if (Tmp == fiRAM || Tmp == fiHardWare)
244     Tmp = DisPlayField[tsi];
245
246   if (Tmp == fiBug || Tmp == 40)
247     Tmp = DisPlayField[tsi];
248
249 #if 1
250   if (Tmp >= 0 && Tmp <= 40)
251   {
252     subCopyImageToScreen(tsi, fiGraphic[Tmp]);
253
254 #if 1
255     if (Tmp != fiSpace && Tmp != fiSnikSnak && Tmp != fiElectron)
256       GfxGraphic[X][Y] = fiGraphic[Tmp];
257 #endif
258   }
259 #else
260   DDSpriteBuffer_BltEx(StretchWidth * X, StretchWidth * Y, Tmp);
261 #endif
262 }
263
264 void DrawFieldAnimated(int X, int Y)
265 {
266   int Tmp, tsi;
267
268   tsi = GetSI(X, Y);
269   Tmp = LowByte(PlayField16[tsi]);
270   switch (Tmp)
271   {
272     case fiSnikSnak:
273       subDrawAnimatedSnikSnaks(tsi);
274       break;
275
276     case fiElectron:
277       subDrawAnimatedElectrons(tsi);
278       break;
279
280     default:
281       //      If 40 < Tmp Then Tmp = 0
282       //      If Tmp = fiRAM Or Tmp = fiHardWare Then Tmp = DisPlayField(tsi)
283       //      If Tmp = fiBug Or Tmp = 40 Then Tmp = DisPlayField(tsi)
284       //      If EditFlag Then
285       //        If fiOrangeDisk < Tmp And Tmp < fiSnikSnak Then Tmp = DisPlayField(tsi)
286       //      End If
287       //      DDSpriteBuffer_BltEx StretchWidth * X, StretchWidth * Y, Tmp
288       break;
289   }
290 }
291
292 void DrawFieldNoAnimated(int X, int Y)
293 {
294   int Tmp, tsi;
295
296   tsi = GetSI(X, Y);
297   Tmp = LowByte(PlayField16[tsi]);
298   switch (Tmp)
299   {
300     case fiSnikSnak:
301       subCopyImageToScreen(tsi, aniSpace);
302       break;
303
304     case fiElectron:
305       subCopyImageToScreen(tsi, aniSpace);
306       break;
307
308     default:
309       if (Tmp > 40)
310         Tmp = 0;
311
312       if (Tmp == fiRAM || Tmp == fiHardWare)
313         Tmp = DisPlayField[tsi];
314
315       if (Tmp == fiBug || Tmp == 40)
316         Tmp = DisPlayField[tsi];
317
318 #if 1
319       if (Tmp >= 0 && Tmp <= 40)
320       {
321         subCopyImageToScreen(tsi, fiGraphic[Tmp]);
322
323 #if 1
324         if (Tmp != fiSpace && Tmp != fiSnikSnak && Tmp != fiElectron)
325           GfxGraphic[X][Y] = fiGraphic[Tmp];
326 #endif
327       }
328 #else
329       DDSpriteBuffer_BltEx(StretchWidth * X, StretchWidth * Y, Tmp);
330 #endif
331
332       break;
333   }
334 }
335
336 void DrawImage(int X, int Y, int graphic)
337 {
338   DDSpriteBuffer_BltImg(StretchWidth * X, StretchWidth * Y, graphic, 0);
339 }
340
341 static void picViewPort_Resize()
342 {
343   SetScrollEdges();
344
345   ScrollTo(ScrollX, ScrollY);
346 }