rnd-20100317-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();
10
11 void DrawField(int X, int Y);
12 void DrawFieldAnimated(int X, int Y);
13 void DrawFieldNoAnimated(int X, int Y);
14
15 void DrawFrameIfNeeded()
16 {
17   DrawFrame(0);
18
19   /* !!! CHECK THIS !!! */
20 #if 1
21   if (! menBorder)
22     DrawFrame(1);
23 #endif
24 }
25
26 void DisplayLevel()
27 {
28   int X, Y;
29
30   if (! LevelLoaded)
31     return;
32
33   ClearRectangle(backbuffer, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
34   ClearRectangle(screenBitmap, 0, 0,
35                  MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY);
36
37 #if 1
38   SetDisplayRegion();
39 #endif
40
41   DrawFrameIfNeeded();
42
43   if (bPlaying)
44   {
45     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
46       for (X = DisplayMinX; X <= DisplayMaxX; X++)
47         DrawFieldNoAnimated(X, Y);
48
49     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
50       for (X = DisplayMinX; X <= DisplayMaxX; X++)
51         DrawFieldAnimated(X, Y);
52   }
53   else
54   {
55     for (Y = DisplayMinY; Y <= DisplayMaxY; Y++)
56       for (X = DisplayMinX; X <= DisplayMaxX; X++)
57         DrawField(X, Y);
58   }
59 }
60
61 void Form_Load()
62 {
63   InitGlobals();
64
65   ReStretch();
66 }
67
68 static void DrawFrame(int Delta)
69 {
70   int i, LX, tY, RX, BY;
71
72   LX = -1 + Delta;
73   tY = -1 + Delta;
74   RX = FieldWidth - Delta;
75   BY = FieldHeight - Delta;
76
77   DrawImage(LX, tY, imgFrameCorner);
78   DrawImage(LX, BY, imgFrameCorner);
79   DrawImage(RX, tY, imgFrameCorner);
80   DrawImage(RX, BY, imgFrameCorner);
81
82   for (i = LX + 1; i <= RX - 1; i++)
83   {
84     DrawImage(i, tY, imgFrameHorizontal);
85     DrawImage(i, BY, imgFrameHorizontal);
86   }
87
88   for (i = tY + 1; i <= BY - 1; i++)
89   {
90     DrawImage(LX, i, imgFrameVertical);
91     DrawImage(RX, i, imgFrameVertical);
92   }
93 }
94
95 static void RestoreFrame()
96 {
97   int i, LX, tY, RX, BY;
98
99   LX = 0;
100   tY = 0;
101   RX = FieldWidth - 1;
102   BY = FieldHeight - 1;
103
104   for (i = LX; i <= RX; i++)
105   {
106     DrawField(i, tY);
107     DrawField(i, BY);
108   }
109
110   for (i = tY + 1; i <= BY - 1; i++)
111   {
112     DrawField(LX, i);
113     DrawField(RX, i);
114   }
115 }
116
117 void SetDisplayRegion()
118 {
119   if (! menBorder)
120   {
121     DisplayMinX = 1;
122     DisplayMinY = 1;
123     DisplayMaxX = FieldWidth - 2;
124     DisplayMaxY = FieldHeight - 2;
125
126     if (LevelLoaded)
127       DrawFrame(1);
128   }
129   else
130   {
131     DisplayMinX = 0;
132     DisplayMinY = 0;
133     DisplayMaxX = FieldWidth - 1;
134     DisplayMaxY = FieldHeight - 1;
135
136     if (LevelLoaded)
137       RestoreFrame();
138   }
139 }
140
141 void menPlay_Click()
142 {
143   bPlaying = True;
144
145   subFetchAndInitLevelB();
146
147   ReStretch();
148
149   subMainGameLoop_Init();
150
151 #if 1
152   return;
153 #endif
154
155   bPlaying = False;
156
157   subFetchAndInitLevel();
158 }
159
160 static void ReStretch()
161 {
162   if (LevelLoaded)
163   {
164     SetDisplayRegion();
165
166     SetScrollEdges();
167
168     ScrollTo(ScrollX, ScrollY);
169
170     DisplayLevel();
171   }
172
173   subCalculateScreenScrollPos();
174
175   ScrollTo(ScreenScrollXPos, ScreenScrollYPos);
176 }
177
178 void SetScrollEdges()
179 {
180   ScrollMinX = (int)(DisplayMinX - 0.5) * BaseWidth;
181   ScrollMinY = (int)(DisplayMinY - 0.5) * BaseWidth;
182   ScrollMaxX = (int)(DisplayMaxX + 1.5) * BaseWidth - SXSIZE;
183   ScrollMaxY = (int)(DisplayMaxY + 1.5) * BaseWidth - SYSIZE;
184 }
185
186 void DrawField(int X, int Y)
187 {
188   int tsi = GetSI(X, Y);
189   int Tmp = LowByte(PlayField16[tsi]);
190
191   if (Tmp < fiFirst || Tmp > fiLast)
192     Tmp = fiSpace;
193
194   if (Tmp == fiRAM ||
195       Tmp == fiHardWare ||
196       Tmp == fiBug ||
197       Tmp == fiWallSpace)
198     Tmp = DisPlayField[tsi];
199
200   subCopyImageToScreen(tsi, fiGraphic[Tmp]);
201
202   if (Tmp != fiSpace &&
203       Tmp != fiSnikSnak &&
204       Tmp != fiElectron)
205     GfxGraphic[X][Y] = fiGraphic[Tmp];
206 }
207
208 void DrawFieldAnimated(int X, int Y)
209 {
210   int tsi = GetSI(X, Y);
211   int Tmp = LowByte(PlayField16[tsi]);
212
213   switch (Tmp)
214   {
215     case fiSnikSnak:
216       subDrawAnimatedSnikSnaks(tsi);
217       break;
218
219     case fiElectron:
220       subDrawAnimatedElectrons(tsi);
221       break;
222
223     default:
224       break;
225   }
226 }
227
228 void DrawFieldNoAnimated(int X, int Y)
229 {
230   int tsi = GetSI(X, Y);
231   int Tmp = LowByte(PlayField16[tsi]);
232
233   switch (Tmp)
234   {
235     case fiSnikSnak:
236       subCopyImageToScreen(tsi, aniSpace);
237       break;
238
239     case fiElectron:
240       subCopyImageToScreen(tsi, aniSpace);
241       break;
242
243     default:
244 #if 1
245       DrawField(X, Y);
246 #else
247       if (Tmp < fiFirst || Tmp > fiLast)
248         Tmp = fiSpace;
249
250       if (Tmp == fiRAM ||
251           Tmp == fiHardWare ||
252           Tmp == fiBug ||
253           Tmp == fiWallSpace)
254         Tmp = DisPlayField[tsi];
255
256       subCopyImageToScreen(tsi, fiGraphic[Tmp]);
257
258       if (Tmp != fiSpace &&
259           Tmp != fiSnikSnak &&
260           Tmp != fiElectron)
261         GfxGraphic[X][Y] = fiGraphic[Tmp];
262 #endif
263       break;
264   }
265 }
266
267 void DrawImage(int X, int Y, int graphic)
268 {
269   DDSpriteBuffer_BltImg(StretchWidth * X, StretchWidth * Y, graphic, 0);
270 }