rnd-20100315-2-src
[rocksndiamonds.git] / src / game_sp / Marker.c
1 // ----------------------------------------------------------------------------
2 // Marker.c
3 // ----------------------------------------------------------------------------
4
5 #include "Marker.h"
6
7 // static boolean IsPort(long i);
8 static void LimitXY(int *X, int *Y);
9 static void SortData();
10
11 // --- VERSION 1.0 CLASS
12 // --- BEGIN
13 // ---   MultiUse = -1  'True  // True
14 // ---   Persistable = 0  'NotPersistable  // NotPersistable
15 // ---   DataBindingBehavior = 0  'vbNone  // vbNone
16 // ---   DataSourceBehavior  = 0  'vbNone  // vbNone
17 // ---   MTSTransactionMode  = 0  'NotAnMTSObject  // NotAnMTSObject
18 // --- END
19
20 // static char *VB_Name = "MarkerObject";
21 // static boolean VB_GlobalNameSpace = False;
22 // static boolean VB_Creatable = True;
23 // static boolean VB_PredeclaredId = False;
24 // static boolean VB_Exposed = False;
25
26 // --- Option Explicit
27
28 long mIndex1, mIndex2;
29 int X1, X2, Y1, Y2;
30 int XMin, YMin;
31 boolean mVisible;
32
33 byte *SelectionData;
34
35 int Marker_Get_Width()
36 {
37   int Width;
38
39   Width = Abs(X2 - X1) + 1;
40
41   return Width;
42 }
43
44 int Marker_Get_Height()
45 {
46   int Height;
47
48   Height = Abs(Y2 - Y1) + 1;
49
50   return Height;
51 }
52
53 int Marker_Get_Left()
54 {
55   int Left;
56
57   SortData();
58   Left = XMin;
59
60   return Left;
61 }
62
63 int Marker_Get_Top()
64 {
65   int Top;
66
67   SortData();
68   Top = YMin;
69
70   return Top;
71 }
72
73 static void LimitXY(int *X, int *Y)
74 {
75   if (*X < DisplayMinX)
76     *X = DisplayMinX;
77
78   if (DisplayMaxX < *X)
79     *X = DisplayMaxX;
80
81   if (*Y < DisplayMinY)
82     *Y = DisplayMinY;
83
84   if (DisplayMaxY < *Y)
85     *Y = DisplayMaxY;
86 }
87
88 void Marker_SetPoint1(int X, int Y)
89 {
90   LimitXY(&X, &Y);
91   X1 = X;
92   Y1 = Y;
93   X2 = X;
94   Y2 = Y;
95 }
96
97 void Marker_SetPoint2(int X, int Y)
98 {
99   char *T;
100
101   LimitXY(&X, &Y);
102   X2 = X;
103   Y2 = Y;
104   T = CAT("(", Marker_Get_Width(), " x ", Marker_Get_Height(), ")");
105   MainForm.lblFrameCount = T;
106 }
107
108 static void SortData()
109 {
110   // int Tmp;
111
112   XMin = (X2 < X1 ? X2 : X1);
113   YMin = (Y2 < Y1 ? Y2 : Y1);
114 }
115
116 #if 0
117
118 void Marker_ShowMarker(boolean ShowFlag)
119 {
120   mVisible = ShowFlag;
121   Marker_MoveMarker();
122 }
123
124 void Marker_RefreshMarker()
125 {
126   int L, T, R, B;
127   long Tmp;
128
129   if (! mVisible)
130     return;
131
132   LimitXY(&X1, &Y1);
133   LimitXY(&X2, &Y2);
134   SortData();
135   L = DigitXPos(XMin) - 1;
136   T = DigitYPos(YMin) - 1;
137   R = L + StretchWidth * Marker_Get_Width() + 1;
138   B = T + StretchWidth * Marker_Get_Height() + 1;
139   MainForm.picPane.Line(L, T, R, B, 0xFFFFFF, B);
140 }
141
142 void Marker_MoveMarker()
143 {
144   int L, T, R, B;
145   long Tmp;
146
147   if (! mVisible)
148     return;
149
150   LimitXY(&X1, &Y1);
151   LimitXY(&X2, &Y2);
152   SortData();
153   Stage.Blt();
154   Tmp = GetSI(XMin, YMin);
155   if (Marker_Get_Width() == 1 && Marker_Get_Height() == 1 && IsPort(Tmp))
156   {
157     SpLoadMenu();
158     MainForm.menSP.Enabled = True;
159   }
160   else
161   {
162     MainForm.menSP.Enabled = False;
163   }
164 }
165
166 static boolean IsPort(long i)
167 {
168   static boolean IsPort;
169
170   int ax;
171
172   IsPort = False;
173   ax = DisPlayField[i];
174   if (fiOrangeDisk < ax && ax < fiSnikSnak)
175     IsPort = True;
176
177   return IsPort;
178 }
179
180 void Marker_Copy()
181 {
182   int X, Y, MaxX, MaxY;
183   long Tmp;
184   char *TPath;
185   int FNum;
186
187   SortData();
188   MaxX = Marker_Get_Width() - 1;
189   MaxY = Marker_Get_Height() - 1;
190   SelectionData = REDIM_2D(sizeof(byte), 0, MaxX + 1 - 1, 0, MaxY + 1 - 1);
191   for (Y = 0; Y <= MaxY; Y++)
192   {
193     for (X = 0; X <= MaxX; X++)
194     {
195       Tmp = FieldWidth * (YMin + Y) + XMin + X;
196
197       // --- On Error GoTo CopyEH
198       SelectionData[X, Y] = DisPlayField[Tmp];
199       // --- On Error GoTo 0
200
201     }
202   }
203
204   TPath = CAT(App.Path, "/Mpx.clp");
205   if (FileExists(TPath))
206     MayKill(TPath);
207
208   FNum = FreeFile();
209
210   // --- On Error GoTo CopyEH
211   FNum = fopen(TPath, "wb");
212   FILE_PUT(FNum, -1, &MaxX, sizeof(MaxX));
213   FILE_PUT(FNum, -1, &MaxY, sizeof(MaxY));
214   FILE_PUT(FNum, -1, &SelectionData, sizeof(SelectionData));
215   fclose(FNum);
216   SelectionData = REDIM_1D(sizeof(byte), 0, 1 - 1);
217   return;
218
219   // CopyEH:
220   Beep();
221 }
222
223 void Marker_Paste()
224 {
225   int X, Y, MaxX, MaxY;
226   long Tmp;
227   char *TPath;
228   int FNum;
229
230   TPath = CAT(App.Path, "/Mpx.clp");
231   if (! FileExists(TPath))
232   {
233     Beep();
234     return;
235   }
236
237   FNum = FreeFile();
238
239   // --- On Error GoTo PasteEH
240   FNum = fopen(TPath, "rb");
241   FILE_GET(FNum, -1, &MaxX, sizeof(MaxX));
242   FILE_GET(FNum, -1, &MaxY, sizeof(MaxY));
243   SelectionData = REDIM_2D(sizeof(byte), 0, MaxX + 1 - 1, 0, MaxY + 1 - 1);
244   FILE_GET(FNum, -1, &SelectionData, sizeof(SelectionData));
245   fclose(FNum);
246   // --- On Error GoTo 0
247
248   SortData();
249   if (Marker_Get_Width() <= MaxX)
250     MaxX = Marker_Get_Width() - 1;
251
252   if (Marker_Get_Height() <= MaxY)
253     MaxY = Marker_Get_Height() - 1;
254
255   for (Y = 0; Y <= MaxY; Y++)
256   {
257     for (X = 0; X <= MaxX; X++)
258     {
259       Tmp = FieldWidth * (YMin + Y) + XMin + X;
260
261       // --- On Error GoTo PasteEH
262       DisPlayField[Tmp] = SelectionData[X][Y];
263       PlayField16[Tmp] = UnEdSprite(SelectionData[X][Y]);
264       // --- On Error GoTo 0
265
266     }
267   }
268
269   Let_ModifiedFlag(True);
270   // PasteEH:
271   Beep();
272 }
273
274 static void Class_Initialize()
275 {
276   mVisible = False;
277 }
278
279 #endif