Debian Codegraph @

engine/video/cursor.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name cursor.cpp - The cursors. */
11 //
12 // (c) Copyright 1998-2008 by Lutz Sammer, Nehal Mistry,
13 // and Jimmy Salmon
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 //@{
31
32 /*----------------------------------------------------------------------------
33 -- Includes
34 ----------------------------------------------------------------------------*/
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "stratagus.h"
41 #include "video.h"
42 #include "unittype.h"
43 #include "player.h"
44 #include "unit.h"
45 #include "cursor.h"
46 #include "map.h"
47 #include "interface.h"
48 #include "ui.h"
49 #include "editor.h"
50
51 #include "intern_video.h"
52
53 /*----------------------------------------------------------------------------
54 -- Variables
55 ----------------------------------------------------------------------------*/
56
57 /**
58 ** Define cursor-types.
59 **
60 ** @todo FIXME: Should this be move to ui part?
61 */
62 std::vector<CCursor> AllCursors;
63
64 CursorStates CursorState; /// current cursor state (point,...)
65 int CursorAction; /// action for selection
66 int CursorValue; /// value for CursorAction (spell type f.e.)
67
68 // Event changed mouse position, can alter at any moment
69 int CursorX; /// cursor position on screen X
70 int CursorY; /// cursor position on screen Y
71
72 int CursorStartX; /// rectangle started on screen X
73 int CursorStartY; /// rectangle started on screen Y
74
75 int SubScrollX; /// pixels the mouse moved while scrolling
76 int SubScrollY; /// pixels the mouse moved while scrolling
77
78 /// X position of starting point of selection rectangle, in screen pixels.
79 int CursorStartScrMapX;
80 /// Y position of starting point of selection rectangle, in screen pixels.
81 int CursorStartScrMapY;
82
83
84 /*--- DRAW BUILDING CURSOR ------------------------------------------------*/
85 CUnitType *CursorBuilding; /// building cursor
86
87
88 /*--- DRAW SPRITE CURSOR ---------------------------------------------------*/
89 CCursor *GameCursor; /// current shown cursor-type
90
91 static SDL_Surface *HiddenSurface;
92
93 /*----------------------------------------------------------------------------
94 -- Functions
95 ----------------------------------------------------------------------------*/
96
97 /**
98 ** Load all cursor sprites.
99 */
100 void LoadCursors()
101 {
102 std::vector<CCursor>::iterator i;
103
104 for (i = AllCursors.begin(); i != AllCursors.end(); ++i) {
105 if ((*i).G && !(*i).G->IsLoaded()) {
106 ShowLoadProgress("Cursor %s", (*i).G->File.c_str());
107 (*i).G->Load();
108 (*i).G->UseDisplayFormat();
109 }
110 }
111 }
112
113 /**
114 ** Find the cursor of this identifier.
115 **
116 ** @param ident Identifier for the cursor (from config files).
117 **
118 ** @return Returns the matching cursor.
119 **
120 ** @note If we have more cursors, we should add hash to find them faster.
121 */
122 CCursor *CursorByIdent(const std::string &ident)
123 {
124 std::vector<CCursor>::iterator i;
125
126 for (i = AllCursors.begin(); i != AllCursors.end(); ++i) {
127 if ((*i).Ident != ident) {
128 continue;
129 }
130 return &(*i);
131 }
132 DebugPrint("Cursor `%s' not found, please check your code.\n" _C_ ident.c_str());
133 return NULL;
134 }
135
136 /**
137 ** Draw rectangle cursor when visible
138 **
139 ** @param x Screen x start position of rectangle
140 ** @param y Screen y start position of rectangle
141 ** @param x1 Screen x end position of rectangle
142 ** @param y1 Screen y end position of rectangle
143 */
144 static void DrawVisibleRectangleCursor(int x, int y, int x1, int y1)
145 {
146 int w;
147 int h;
148 const CViewport *vp;
149
150 //
151 // Clip to map window.
152 // FIXME: should re-use CLIP_RECTANGLE in some way from linedraw.c ?
153 //
154 vp = UI.SelectedViewport;
155 if (x1 < vp->X) {
156 x1 = vp->X;
157 } else if (x1 > vp->EndX) {
158 x1 = vp->EndX;
159 }
160 if (y1 < vp->Y) {
161 y1 = vp->Y;
162 } else if (y1 > vp->EndY) {
163 y1 = vp->EndY;
164 }
165
166 if (x > x1) {
167 w = x - x1 + 1;
168 x = x1;
169 } else {
170 w = x1 - x + 1;
171 }
172 if (y > y1) {
173 h = y - y1 + 1;
174 y = y1;
175 } else {
176 h = y1 - y + 1;
177 }
178
179 if (w && h) {
180 Video.DrawRectangleClip(ColorGreen, x, y, w, h);
181 }
182 }
183
184 /**
185 ** Draw cursor for selecting building position.
186 */
187 static void DrawBuildingCursor(void)
188 {
189 int i;
190 int x;
191 int y;
192 int mx;
193 int my;
194 Uint32 color;
195 int f;
196 int w;
197 int w0;
198 int h;
199 int mask;
200 const CViewport *vp;
201 CUnit *ontop;
202
203 // Align to grid
204 vp = UI.MouseViewport;
205 x = CursorX - (CursorX - vp->X + vp->OffsetX) % TileSizeX;
206 y = CursorY - (CursorY - vp->Y + vp->OffsetY) % TileSizeY;
207 mx = vp->Viewport2MapX(x);
208 my = vp->Viewport2MapY(y);
209 ontop = NULL;
210
211 //
212 // Draw building
213 //
214 PushClipping();
215 SetClipping(vp->X, vp->Y, vp->EndX, vp->EndY);
216 DrawShadow(NULL, CursorBuilding, CursorBuilding->StillFrame, x, y);
217 DrawUnitType(CursorBuilding, CursorBuilding->Sprite, ThisPlayer->Index,
218 CursorBuilding->StillFrame, x, y);
219
220 //
221 // Draw the allow overlay
222 //
223 if (NumSelected) {
224 f = 1;
225 for (i = 0; f && i < NumSelected; ++i) {
226 f = ((ontop = CanBuildHere(Selected[i], CursorBuilding, mx, my)) != NULL);
227 // Assign ontop or NULL
228 ontop = (ontop == Selected[i] ? NULL : ontop);
229 }
230 } else {
231 f = ((ontop = CanBuildHere(NoUnitP, CursorBuilding, mx, my)) != NULL);
232 if (!Editor.Running || (Editor.Running && ontop == (CUnit *)1)) {
233 ontop = NULL;
234 }
235 }
236
237 mask = CursorBuilding->MovementMask;
238 h = CursorBuilding->TileHeight;
239 // reduce to view limits
240 if (my + h > vp->MapY + vp->MapHeight) {
241 h = vp->MapY + vp->MapHeight - my;
242 }
243 w0 = CursorBuilding->TileWidth;
244 if (mx + w0 > vp->MapX + vp->MapWidth) {
245 w0 = vp->MapX + vp->MapWidth - mx;
246 }
247 while (h--) {
248 w = w0;
249 while (w--) {
250 if (f && (ontop ||
251 CanBuildOn(mx + w, my + h, MapFogFilterFlags(ThisPlayer, mx + w, my + h,
252 mask & ((NumSelected &&
253 Selected[0]->X == mx + w && Selected[0]->Y == my + h) ?
254 ~(MapFieldLandUnit | MapFieldSeaUnit) : -1)))) &&
255 Map.IsFieldExplored(ThisPlayer, mx + w, my + h)) {
256 color = ColorGreen;
257 } else {
258 color = ColorRed;
259 }
260 Video.FillTransRectangleClip(color, x + w * TileSizeX, y + h *
261 TileSizeY, TileSizeX, TileSizeY, 95);
262 }
263 }
264 PopClipping();
265 }
266
267
268 /**
269 ** Draw the cursor.
270 */
271 void DrawCursor(void)
272 {
273 // Selecting rectangle
274 if (CursorState == CursorStateRectangle &&
275 (CursorStartX != CursorX || CursorStartY != CursorY)) {
276 DrawVisibleRectangleCursor(
277 CursorStartScrMapX + UI.MouseViewport->X - TileSizeX * UI.MouseViewport->MapX - UI.MouseViewport->OffsetX,
278 CursorStartScrMapY + UI.MouseViewport->Y - TileSizeY * UI.MouseViewport->MapY - UI.MouseViewport->OffsetY,
279 CursorX, CursorY);
280 } else if (CursorBuilding && CursorOn == CursorOnMap) {
281 // Selecting position for building
282 DrawBuildingCursor();
283 }
284
285 if (!UseOpenGL && !GameRunning && !Editor.Running && !PatchEditorRunning && GameCursor) {
286 if (!HiddenSurface ||
287 HiddenSurface->w != GameCursor->G->getWidth() ||
288 HiddenSurface->h != GameCursor->G->getHeight())
289 {
290 if (HiddenSurface) {
291 SDL_FreeSurface(HiddenSurface);
292 }
293
294 HiddenSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
295 GameCursor->G->getWidth(),
296 GameCursor->G->getHeight(),
297 TheScreen->format->BitsPerPixel,
298 TheScreen->format->Rmask,
299 TheScreen->format->Gmask,
300 TheScreen->format->Bmask,
301 TheScreen->format->Amask);
302 }
303
304 SDL_Rect srcRect = {
305 static_cast<Sint16>(CursorX - GameCursor->HotX),
306 static_cast<Sint16>(CursorY - GameCursor->HotY),
307 static_cast<Uint16>(GameCursor->G->getWidth()),
308 static_cast<Uint16>(GameCursor->G->getHeight())
309 };
310 SDL_BlitSurface(TheScreen, &srcRect, HiddenSurface, NULL);
311 }
312
313 //
314 // Last, Normal cursor.
315 // Cursor may not exist if we are loading a game or something. Only
316 // draw it if it exists
317 //
318 if (GameCursor) {
319 GameCursor->G->DrawFrameClip(GameCursor->SpriteFrame,
320 CursorX - GameCursor->HotX, CursorY - GameCursor->HotY);
321 }
322 }
323
324 /**
325 ** Hide the cursor
326 */
327 void HideCursor(void)
328 {
329 if (!UseOpenGL && !GameRunning && !Editor.Running && !PatchEditorRunning && GameCursor) {
330 SDL_Rect dstRect = {
331 static_cast<Sint16>(CursorX - GameCursor->HotX),
332 static_cast<Sint16>(CursorY - GameCursor->HotY),
333 0,
334 0
335 };
336 SDL_BlitSurface(HiddenSurface, NULL, TheScreen, &dstRect);
337 }
338 }
339
340 /**
341 ** Animate the cursor.
342 **
343 ** @param ticks Current tick
344 */
345 void CursorAnimate(unsigned ticks)
346 {
347 static unsigned last = 0;
348
349 if (!GameCursor || !GameCursor->FrameRate) {
350 return;
351 }
352 if (ticks > last + GameCursor->FrameRate) {
353 last = ticks + GameCursor->FrameRate;
354 GameCursor->SpriteFrame++;
355 if ((GameCursor->SpriteFrame & 127) >= GameCursor->G->NumFrames) {
356 GameCursor->SpriteFrame = 0;
357 }
358 }
359 }
360
361 /**
362 ** Setup the cursor part.
363 */
364 void InitVideoCursors(void)
365 {
366 }
367
368 /**
369 ** Cleanup cursor module
370 */
371 void CleanCursors(void)
372 {
373 std::vector<CCursor>::iterator i;
374
375 for (i = AllCursors.begin(); i != AllCursors.end(); ++i) {
376 CGraphic::Free((*i).G);
377 }
378 AllCursors.clear();
379
380 CursorBuilding = NULL;
381 GameCursor = NULL;
382 UnitUnderCursor = NoUnitP;
383 }
384
385 //@}