File: patch_editor.cpp

package info (click to toggle)
boswars 2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 96,652 kB
  • sloc: cpp: 57,250; python: 1,715; sh: 25; makefile: 17
file content (701 lines) | stat: -rw-r--r-- 17,797 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//     ____                _       __               
//    / __ )____  _____   | |     / /___ ___________
//   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
//  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  ) 
// /_____/\____/____/     |__/|__/\__,_/_/  /____/  
//                                              
//       A futuristic real-time strategy game.
//          This file is part of Bos Wars.
//
/**@name patch_editor.cpp - The patch editor. */
//
//      (c) Copyright 2008 by Jimmy Salmon
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; only version 2 of the License.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//      02111-1307, USA.
//

//@{

#include "stratagus.h"

#include <sstream>

#include "sound.h"
#include "iolib.h"
#include "network.h"
#include "interface.h"
#include "cursor.h"
#include "map.h"
#include "guichan.h"
#include "patch_type.h"
#include "patch.h"
#include "patch_manager.h"
#include "ui.h"
#include "font.h"


extern void DrawGuichanWidgets();

bool PatchEditorRunning;

extern gcn::Gui *Gui;
static gcn::Container *PatchEditorContainer;

static CPatch *Patch;

static const int PatchMenuWidth = 150;

static float ScrollX;
static float ScrollY;

static CGraphic *TransparentG;
static CGraphic *TransparentSmallG;
static CGraphic *ImpassableG;
static CGraphic *ImpassableSmallG;
static CGraphic *CoastG;
static CGraphic *CoastSmallG;
static CGraphic *ShallowWaterG;
static CGraphic *ShallowWaterSmallG;
static CGraphic *DeepWaterG;
static CGraphic *DeepWaterSmallG;
#define NumSpeeds 7
static CGraphic *SpeedG[NumSpeeds + 1];
static CGraphic *SpeedSmallG[NumSpeeds + 1];

enum PatchButton {
	ButtonNone,
	ButtonImpassable,
	ButtonCoast,
	ButtonShallowWater,
	ButtonDeepWater,
	ButtonSpeed0,
	ButtonSpeed1,
	ButtonSpeed2,
	ButtonSpeed3,
	ButtonSpeed4,
	ButtonSpeed5,
	ButtonSpeed6,
	ButtonSpeed7,
	ButtonTransparent,
};

struct PatchIcon
{
	int X;
	int Y;
	CGraphic *G;
	PatchButton Button;
};

static std::vector<PatchIcon> PatchIcons;

static std::map<int, unsigned short> FlagMap;
static PatchButton CurrentButton;
static PatchButton MouseOverButton;
static int MouseOverTileX;
static int MouseOverTileY;

static unsigned short DraggingClearFlags;
static unsigned short DraggingSetFlags; // overrides DraggingClearFlags


static void DoScroll()
{
	int state = MouseScrollState | KeyScrollState;
	static Uint32 lastTicks = GetTicks() - 1;

	Uint32 ticks = GetTicks();
	float speed = (ticks - lastTicks) / 1000.f * 400;
	lastTicks = ticks;

	if (state == ScrollNone) {
		return;
	}

	if ((state & (ScrollLeft | ScrollRight)) &&
			(state & (ScrollLeft | ScrollRight)) != (ScrollLeft | ScrollRight)) {
		if (state & ScrollRight) {
			ScrollX += speed;
		} else {
			ScrollX -= speed;
		}
	}
	if ((state & (ScrollUp | ScrollDown)) &&
			(state & (ScrollUp | ScrollDown)) != (ScrollUp | ScrollDown)) {
		if (state & ScrollDown) {
			ScrollY += speed;
		} else {
			ScrollY -= speed;
		}
	}

	if (ScrollX < 0.f ||
			Patch->getType()->getGraphic()->Width < (Video.Width - PatchMenuWidth)) {
		ScrollX = 0.f;
	} else if (ScrollX > Patch->getType()->getGraphic()->Width - (Video.Width - PatchMenuWidth)) {
		ScrollX = (float)(Patch->getType()->getGraphic()->Width - (Video.Width - PatchMenuWidth));
	}
	if (ScrollY < 0.f ||
			Patch->getType()->getGraphic()->Height < Video.Height) {
		ScrollY = 0.f;
	} else if (ScrollY > Patch->getType()->getGraphic()->Height - Video.Height) {
		ScrollY = (float)(Patch->getType()->getGraphic()->Height - Video.Height);
	}
}

/**
**  Change the patch-tile flags at (::MouseOverTileX, ::MouseOverTileY),
**  as specified with ::DraggingClearFlags and ::DraggingSetFlags.
**
**  The patch editor has no real map, so this function does not
**  attempt to update any CMapField.
*/
static void ChangeFlagsAtMouse()
{
	CPatchType *patchType = Patch->getType();
	unsigned short flag = patchType->getFlag(MouseOverTileX, MouseOverTileY);
	flag &= ~DraggingClearFlags;
	flag |= DraggingSetFlags;
	patchType->setFlag(MouseOverTileX, MouseOverTileY, flag);
}

static void PatchEditorCallbackButtonDown(unsigned button)
{
	if ((1 << button) != LeftButton) {
		return;
	}

	// Icons
	if (MouseOverButton != ButtonNone) {
		CurrentButton = MouseOverButton;
	}

	// Patch area
	if (MouseOverTileX != -1) {
		unsigned short flag = Patch->getType()->getFlag(MouseOverTileX, MouseOverTileY);
		if (ButtonSpeed0 <= CurrentButton && CurrentButton <= ButtonSpeed7) {
			DraggingClearFlags = MapFieldSpeedMask;
			DraggingSetFlags = FlagMap[CurrentButton];
		} else if (CurrentButton == ButtonCoast
			|| CurrentButton == ButtonShallowWater
			|| CurrentButton == ButtonDeepWater) {
			// clear existing land/water flags then set the new flag
			DraggingClearFlags = (MapFieldLandAllowed
					      | MapFieldCoastAllowed
					      | MapFieldShallowWater
					      | MapFieldDeepWater
					      | MapFieldNoBuilding);
			if (flag & FlagMap[CurrentButton]) {
				// set default to land
				DraggingSetFlags = MapFieldLandAllowed;
			} else {
				DraggingSetFlags = FlagMap[CurrentButton];
			}
		} else {
			// toggle flag
			DraggingClearFlags = flag & FlagMap[CurrentButton];
			DraggingSetFlags = DraggingClearFlags ^ FlagMap[CurrentButton];
		}

		ChangeFlagsAtMouse();
	}
}

static void PatchEditorCallbackButtonUp(unsigned button)
{
	DraggingClearFlags = 0;
	DraggingSetFlags = 0;
}

static void PatchEditorCallbackKeyDown(unsigned key, unsigned keychar)
{
	if (HandleKeyModifiersDown(key, keychar)) {
		return;
	}

	switch (key) {
		case 'f': // ALT+F, CTRL+F toggle fullscreen
			if (!(KeyModifiers & (ModifierAlt | ModifierControl))) {
				break;
			}
			ToggleFullScreen();
			break;

		case 'x': // ALT+X, CTRL+X: Exit editor
			if (!(KeyModifiers & (ModifierAlt | ModifierControl))) {
				break;
			}
			Exit(0);

		case SDLK_UP: // Keyboard scrolling
		case SDLK_KP8:
			KeyScrollState |= ScrollUp;
			break;
		case SDLK_DOWN:
		case SDLK_KP2:
			KeyScrollState |= ScrollDown;
			break;
		case SDLK_LEFT:
		case SDLK_KP4:
			KeyScrollState |= ScrollLeft;
			break;
		case SDLK_RIGHT:
		case SDLK_KP6:
			KeyScrollState |= ScrollRight;
			break;

		default:
			HandleCommandKey(key);
			return;
	}
}

static void PatchEditorCallbackKeyUp(unsigned key, unsigned keychar)
{
	if (HandleKeyModifiersUp(key, keychar)) {
		return;
	}

	switch (key) {
		case SDLK_UP: // Keyboard scrolling
		case SDLK_KP8:
			KeyScrollState &= ~ScrollUp;
			break;
		case SDLK_DOWN:
		case SDLK_KP2:
			KeyScrollState &= ~ScrollDown;
			break;
		case SDLK_LEFT:
		case SDLK_KP4:
			KeyScrollState &= ~ScrollLeft;
			break;
		case SDLK_RIGHT:
		case SDLK_KP6:
			KeyScrollState &= ~ScrollRight;
			break;
		default:
			break;
	}
}

static void PatchEditorCallbackKeyRepeated(unsigned dummy1, unsigned dummy2)
{
}

static void PatchEditorCallbackMouse(int x, int y)
{
	HandleCursorMove(&x, &y);

	MouseScrollState = ScrollNone;
	GameCursor = UI.Point.Cursor;
	MouseOverButton = ButtonNone;
	MouseOverTileX = -1;

	// Icons
	std::vector<PatchIcon>::iterator i;
	for (i = PatchIcons.begin(); i != PatchIcons.end(); ++i) {
		if (i->X <= x && x <= i->X + i->G->Width &&
				i->Y <= y && y <= i->Y + i->G->Height)
		{
			MouseOverButton = i->Button;
		}
	}

	// Patch area
	if (0 <= x && x < Video.Width - PatchMenuWidth &&
		0 <= y && y < Video.Height)
	{
		int tileX = (x + (int)ScrollX) / TileSizeX;
		int tileY = (y + (int)ScrollY) / TileSizeY;
		if (0 <= tileX && tileX < Patch->getType()->getTileWidth() &&
			0 <= tileY && tileY < Patch->getType()->getTileHeight())
		{
			MouseOverTileX = tileX;
			MouseOverTileY = tileY;

			// If dragging did not begin in the patch area,
			// then DraggingClearFlags and DraggingSetFlags
			// are both zero, and this call doesn't change
			// anything.
			ChangeFlagsAtMouse();
		}
	}

	// Scroll area at edge of the screen
	if (HandleMouseScrollArea(x, y)) {
		return;
	}
}

static void PatchEditorCallbackExit(void)
{
	// Disabled
	if (!LeaveStops) {
		return;
	}

	// Prevent scrolling while out of focus (on other applications)
	KeyScrollState = MouseScrollState = ScrollNone;
}

static void DrawPatch()
{
	const CGraphic *g = Patch->getType()->getGraphic();
	g->DrawClip(-(int)ScrollX, -(int)ScrollY);
}

static void DrawPatchTileIcons()
{
	CGraphic *g;
	int x, y;

	for (int j = 0; j < Patch->getType()->getTileHeight(); ++j) {
		y = j * TileSizeY - (int)ScrollY;

		for (int i = 0; i < Patch->getType()->getTileWidth(); ++i) {
			x = i * TileSizeX - (int)ScrollX;

			unsigned short flags = Patch->getType()->getFlag(i, j);
			if (flags & MapFieldTransparent) {
				g = TransparentSmallG;
				g->DrawClip(x, y);
				continue;
			}
			if (flags & MapFieldUnpassable) {
				g = ImpassableSmallG;
				g->DrawClip(x, y);
			}
			if (flags & MapFieldCoastAllowed) {
				g = CoastSmallG;
				g->DrawClip(x + 16, y);
			}
			if (flags & MapFieldShallowWater) {
				g = ShallowWaterSmallG;
				g->DrawClip(x + 16, y);
			}
			if (flags & MapFieldDeepWater) {
				g = DeepWaterSmallG;
				g->DrawClip(x + 16, y);
			}
			if ((flags & MapFieldSpeedMask) != 3) {
				g = SpeedSmallG[(flags & MapFieldSpeedMask)];
				g->DrawClip(x + 16, y + 16);
			}
		}
	}
}

static void DrawGrids()
{
	int i;
	int width = Patch->getType()->getTileWidth();
	int height = Patch->getType()->getTileHeight();

	for (i = 1; i < width; ++i) {
		Video.DrawVLineClip(ColorBlack, i * TileSizeX - (int)ScrollX, 0, height * TileSizeY);
	}
	for (i = 1; i < height; ++i) {
		Video.DrawHLineClip(ColorBlack, 0, i * TileSizeY - (int)ScrollY, width * TileSizeX);
	}
}

static void DrawIcons()
{
	Uint32 color;
	std::vector<PatchIcon>::iterator i;

	for (i = PatchIcons.begin(); i != PatchIcons.end(); ++i) {
		i->G->DrawClip(i->X, i->Y);

		if (i->Button == MouseOverButton) {
			color = ColorBlue;
		} else if (i->Button == CurrentButton) {
			color = ColorGreen;
		} else {
			color = ColorBlack;
		}

		Video.DrawRectangleClip(color, i->X - 1, i->Y - 1, 50, 50);
		Video.DrawRectangleClip(color, i->X - 2, i->Y - 2, 52, 52);
	}
}

static void DrawCoordinates()
{
	if (MouseOverTileX == -1) {
		return;
	}

	std::ostringstream o;
	o << MouseOverTileX << "," << MouseOverTileY;
	VideoDrawTextCentered(Video.Width - PatchMenuWidth / 2, Video.Height - 15, GameFont, o.str());
}

static void PatchEditorUpdateDisplay()
{
	// Patch area
	DrawPatch();
	DrawPatchTileIcons();
	DrawGrids();

	// Menu area
	Video.FillRectangle(ColorGray, Video.Width - PatchMenuWidth, 0, PatchMenuWidth, Video.Height);
	DrawIcons();
	DrawCoordinates();

	DrawGuichanWidgets();

	DrawCursor();

	Invalidate();
	RealizeVideoMemory();
}

static gcn::Button *PatchNewButton(const std::string &caption)
{
	gcn::Color darkColor(38, 38, 78, 128);
	gcn::Button *button;
	
	button = new gcn::Button(caption);
	button->setSize(106, 28);
	button->setBackgroundColor(darkColor);
	button->setBaseColor(darkColor);
	return button;
}

class PatchSaveButtonListener : public gcn::ActionListener
{
public:
	virtual void action(const std::string &eventId) {
		CFile file;
		std::string name = UserDirectory + "patches/" + Patch->getType()->getName() + ".lua";
		if (file.open(name.c_str(), CL_OPEN_WRITE) == -1) {
			// TODO: show error
			return;
		}
		file.printf("%s", Map.PatchManager.savePatchType(Patch->getType()).c_str());
		file.close();
		// TODO: show success

		Patch->getType()->setCustomPatch(false);
	}
};

class PatchExitButtonListener : public gcn::ActionListener
{
public:
	virtual void action(const std::string &eventId) {
		// TODO: prompt to save changes
		PatchEditorRunning = false;
	}
};

static void InitCallbacks(EventCallback *callbacks)
{
	callbacks->ButtonPressed = PatchEditorCallbackButtonDown;
	callbacks->ButtonReleased = PatchEditorCallbackButtonUp;
	callbacks->MouseMoved = PatchEditorCallbackMouse;
	callbacks->MouseExit = PatchEditorCallbackExit;
	callbacks->KeyPressed = PatchEditorCallbackKeyDown;
	callbacks->KeyReleased = PatchEditorCallbackKeyUp;
	callbacks->KeyRepeated = PatchEditorCallbackKeyRepeated;
	callbacks->NetworkEvent = NetworkEvent;
}

static void PatchEditorMainLoop()
{
	const EventCallback *old_callbacks = GetCallbacks();
	EventCallback callbacks;
	gcn::Button *saveButton;
	gcn::Button *exitButton;
	PatchSaveButtonListener *saveButtonListener;
	PatchExitButtonListener *exitButtonListener;

	InitCallbacks(&callbacks);
	SetCallbacks(&callbacks);

	ScrollX = 0.f;
	ScrollY = 0.f;

	CurrentButton = ButtonImpassable;
	MouseOverButton = ButtonNone;

	gcn::Widget *oldTop = Gui->getTop();

	PatchEditorContainer = new gcn::Container();
	PatchEditorContainer->setDimension(gcn::Rectangle(0, 0, Video.Width, Video.Height));
	PatchEditorContainer->setOpaque(false);
	Gui->setTop(PatchEditorContainer);

	saveButton = PatchNewButton(_("Save"));
	saveButtonListener = new PatchSaveButtonListener();
	saveButton->addActionListener(saveButtonListener);
	PatchEditorContainer->add(saveButton,
		Video.Width - PatchMenuWidth / 2 - saveButton->getWidth() / 2, 20);

	exitButton = PatchNewButton(_("Exit"));
	exitButtonListener = new PatchExitButtonListener();
	exitButton->addActionListener(exitButtonListener);
	PatchEditorContainer->add(exitButton,
		Video.Width - PatchMenuWidth / 2 - saveButton->getWidth() / 2, Video.Height - exitButton->getHeight() - 20);

	Map.PatchManager.load();

	PatchEditorRunning = true;
	while (PatchEditorRunning) {
		CheckMusicFinished();
		DoScroll();

		PatchEditorUpdateDisplay();

		WaitEventsOneFrame();
	}

	SetCallbacks(old_callbacks);

	Gui->setTop(oldTop);
	delete PatchEditorContainer;
	delete saveButton;
	delete saveButtonListener;
	delete exitButton;
	delete exitButtonListener;
}

static void PatchAddIcon(int x, int y, CGraphic *g, PatchButton b)
{
	PatchIcon icon;
	icon.X = x;
	icon.Y = y;
	icon.G = g;
	icon.Button = b;
	PatchIcons.push_back(icon);
}

static void PatchLoadIcons()
{
	std::string patchEditorPath = "ui/patcheditor/";
	int spacing = 5;
	int leftX = Video.Width - PatchMenuWidth + spacing;
	int rightX = Video.Width - 48 - spacing;
	int topY = 60;

	ImpassableG = CGraphic::New(patchEditorPath + "impassable.png");
	ImpassableG->Load();
	ImpassableSmallG = CGraphic::New(patchEditorPath + "impassable-small.png");
	ImpassableSmallG->Load();
	PatchAddIcon(leftX, topY, ImpassableG, ButtonImpassable);
	FlagMap[ButtonImpassable] = MapFieldUnpassable;

	CoastSmallG = CGraphic::New(patchEditorPath + "coast-small.png");
	CoastSmallG->Load();
	CoastG = CGraphic::New(patchEditorPath + "coast.png");
	CoastG->Load();
	PatchAddIcon(rightX, topY, CoastG, ButtonCoast);
	FlagMap[ButtonCoast] = MapFieldCoastAllowed;

	ShallowWaterG = CGraphic::New(patchEditorPath + "water.png");
	ShallowWaterG->Load();
	ShallowWaterSmallG = CGraphic::New(patchEditorPath + "water-small.png");
	ShallowWaterSmallG->Load();
	PatchAddIcon(leftX, topY + (48 + spacing), ShallowWaterG, ButtonShallowWater);
	FlagMap[ButtonShallowWater] = MapFieldShallowWater;

	DeepWaterG = CGraphic::New(patchEditorPath + "deep-sea.png");
	DeepWaterG->Load();
	DeepWaterSmallG = CGraphic::New(patchEditorPath + "deep-sea-small.png");
	DeepWaterSmallG->Load();
	PatchAddIcon(rightX, topY + (48 + spacing), DeepWaterG, ButtonDeepWater);
	FlagMap[ButtonDeepWater] = MapFieldDeepWater;

	for (int i = 0; i <= NumSpeeds; ++i) {
		std::ostringstream o;
		o << patchEditorPath << "speed" << i << ".png";
		SpeedG[i] = CGraphic::New(o.str());
		SpeedG[i]->Load();
		o.str(std::string());
		o.clear();
		o << patchEditorPath << "speed" << i << "-small.png";
		SpeedSmallG[i] = CGraphic::New(o.str());
		SpeedSmallG[i]->Load();
		PatchAddIcon((!(i & 1) ? leftX : rightX), topY + (48 + spacing) * ((i + 4) / 2), SpeedG[i], (PatchButton)(ButtonSpeed0 + i));
		FlagMap[ButtonSpeed0 + i] = i;
	}

	TransparentG = CGraphic::New(patchEditorPath + "transparent.png");
	TransparentG->Load();
	TransparentSmallG = CGraphic::New(patchEditorPath + "transparent-small.png");
	TransparentSmallG->Load();
	PatchAddIcon(leftX, topY + (48 + spacing) * ((NumSpeeds + 5) / 2), TransparentG, ButtonTransparent);
	FlagMap[ButtonTransparent] = MapFieldTransparent;
}

static void PatchFreeIcons()
{
	CGraphic::Free(ImpassableG);
	ImpassableG = NULL;
	CGraphic::Free(ImpassableSmallG);
	ImpassableSmallG = NULL;

	CGraphic::Free(CoastG);
	CoastG = NULL;
	CGraphic::Free(CoastSmallG);
	CoastSmallG = NULL;

	CGraphic::Free(ShallowWaterG);
	ShallowWaterG = NULL;
	CGraphic::Free(ShallowWaterSmallG);
	ShallowWaterSmallG = NULL;

	CGraphic::Free(DeepWaterG);
	DeepWaterG = NULL;
	CGraphic::Free(DeepWaterSmallG);
	DeepWaterSmallG = NULL;

	PatchIcons.clear();
	FlagMap.clear();
}

void StartPatchEditor(const std::string &patchName)
{
	std::string name;
	if (patchName.substr(0, 5) != "user-") {
		name = "user-" + patchName;
		if (Map.PatchManager.getPatchType(name) == NULL) {
			CPatchType *patchType = Map.PatchManager.getPatchType(patchName);
			Map.PatchManager.newPatchType(name,
				patchType->getFile(),
				patchType->getTileWidth(), patchType->getTileHeight(),
				patchType->getFlags());
		}
	} else {
		name = patchName;
	}

	Patch = Map.PatchManager.add(name, 0, 0);
	if (!Patch) {
		fprintf(stderr, "Invalid patch name: %s\n", patchName.c_str());
		return;
	}

	PatchLoadIcons();

	PatchEditorMainLoop();

	PatchFreeIcons();

	Map.PatchManager.clear();
}

//@}