summaryrefslogtreecommitdiffstats
path: root/Tools/BiomeVisualiser/BiomeViewWnd.cpp
blob: 459a4323cd5fe6e0ae9e1b5937033031fae7b1b7 (plain) (blame)
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

// BiomeViewWnd.cpp

// Implements the cBiomeViewWnd class representing the window that displays biomes

#include "Globals.h"
#include "BiomeViewWnd.h"
#include "BiomeCache.h"
#include "GeneratorBiomeSource.h"
#include "iniFile/iniFile.h"





const int TIMER_RERENDER = 1200;





cBiomeViewWnd::cBiomeViewWnd(void) :
	m_Wnd(NULL),
	m_Thunk(&cBiomeViewWnd::WndProc, this),
	m_IsLButtonDown(false)
{
}





bool cBiomeViewWnd::Create(HWND a_ParentWnd, LPCTSTR a_Title)
{
	ASSERT(m_Wnd == NULL);
	
	InitBiomeView();
	
	// Create a regular STATIC window, then override its window procedure with our own. No need for obnoxious RegisterWindowClass() stuff.
	m_Wnd = CreateWindow("STATIC", a_Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 400, 300, a_ParentWnd, NULL, GetModuleHandle(NULL), NULL);
	if (m_Wnd == NULL)
	{
		LOGERROR("Cannot create main window: %d", GetLastError());
		return false;
	}
	SetWindowLongPtr(m_Wnd, GWLP_WNDPROC, m_Thunk);
	
	return true;
}





void cBiomeViewWnd::InitBiomeView(void)
{
	cIniFile IniFile;
	IniFile.ReadFile("world.ini");
	AString BiomeGenName = IniFile.GetValueSet("Generator", "BiomeGen", "");
	if (BiomeGenName.empty())
	{
		LOGWARN("[Generator] BiomeGen value not set in world.ini, using \"MultiStepMap\".");
		BiomeGenName = "MultiStepMap";
	}
	
	int Seed = IniFile.GetValueSetI("Generator", "Seed", 0);
	
	bool CacheOffByDefault = false;
	if (NoCaseCompare(BiomeGenName, "constant") == 0)
	{
		m_BiomeGen = new cBioGenConstant;
		CacheOffByDefault = true;  // we're generating faster than a cache would retrieve data :)
	}
	else if (NoCaseCompare(BiomeGenName, "checkerboard") == 0)
	{
		m_BiomeGen = new cBioGenCheckerboard;
		CacheOffByDefault = true;  // we're (probably) generating faster than a cache would retrieve data
	}
	else if (NoCaseCompare(BiomeGenName, "voronoi") == 0)
	{
		m_BiomeGen = new cBioGenVoronoi(Seed);
	}
	else if (NoCaseCompare(BiomeGenName, "distortedvoronoi") == 0)
	{
		m_BiomeGen = new cBioGenDistortedVoronoi(Seed);
	}
	else
	{
		if (NoCaseCompare(BiomeGenName, "multistepmap") != 0)
		{
			LOGWARNING("Unknown BiomeGen \"%s\", using \"MultiStepMap\" instead.", BiomeGenName.c_str());
		}
		m_BiomeGen = new cBioGenMultiStepMap(Seed);

		/*
		// Performance-testing:
		LOGINFO("Measuring performance of cBioGenMultiStepMap...");
		clock_t BeginTick = clock();
		for (int x = 0; x < 5000; x++)
		{
			cChunkDef::BiomeMap Biomes;
			m_BiomeGen->GenBiomes(x * 5, x * 5, Biomes);
		}
		clock_t Duration = clock() - BeginTick;
		LOGINFO("cBioGenMultiStepMap for 5000 chunks took %d ticks (%.02f sec)", Duration, (double)Duration / CLOCKS_PER_SEC);
		//*/
	}
	
	// Add a cache, if requested:
	int CacheSize = IniFile.GetValueSetI("Generator", "BiomeGenCacheSize", CacheOffByDefault ? 0 : 64);
	if (CacheSize > 0)
	{
		if (CacheSize < 4)
		{
			LOGWARNING("Biomegen cache size set too low, would hurt performance instead of helping. Increasing from %d to %d",
				CacheSize, 4
			);
			CacheSize = 4;
		}
		LOGD("Using a cache for biomegen of size %d.", CacheSize);
		m_BiomeGen = new cBioGenCache(m_BiomeGen, CacheSize);
	}
	m_BiomeGen->InitializeBiomeGen(IniFile);
	m_Renderer.SetSource(new cGeneratorBiomeSource(m_BiomeGen));
	IniFile.WriteFile("world.ini");
}





LRESULT cBiomeViewWnd::WndProc(HWND a_Wnd, UINT a_Msg, WPARAM wParam, LPARAM lParam)
{
	switch (a_Msg)
	{
		case WM_CLOSE:       return OnClose();
		case WM_COMMAND:     return OnCommand(wParam, lParam);
		case WM_LBUTTONDOWN: return OnLButtonDown(wParam, lParam);
		case WM_LBUTTONUP:   return OnLButtonUp  (wParam, lParam);
		case WM_MOUSEMOVE:   return OnMouseMove  (wParam, lParam);
		case WM_PAINT:       return OnPaint();
		case WM_TIMER:       return OnTimer(wParam);
	}
	return ::DefWindowProc(a_Wnd, a_Msg, wParam, lParam);
}





LRESULT cBiomeViewWnd::OnClose(void)
{
	PostQuitMessage(0);
	return 0;
}





LRESULT cBiomeViewWnd::OnCommand(WPARAM wParam, LPARAM lParam)
{
	// TODO: Handle menu commands, when we get menu
	return 0;
}





LRESULT cBiomeViewWnd::OnLButtonDown(WPARAM wParam, LPARAM lParam)
{
	m_IsLButtonDown = true;
	GetCursorPos(&m_MouseDown);
	return 0;
}





LRESULT cBiomeViewWnd::OnMouseMove(WPARAM wParam, LPARAM lParam)
{
	if (!m_IsLButtonDown)
	{
		return 0;
	}
	POINT pnt;
	GetCursorPos(&pnt);
	m_Renderer.MoveViewBy(pnt.x - m_MouseDown.x, pnt.y - m_MouseDown.y);
	if (m_Renderer.Render(m_Pixmap))
	{
		SetTimer(m_Wnd, TIMER_RERENDER, 200, NULL);
	}
	m_MouseDown = pnt;
	InvalidateRect(m_Wnd, NULL, FALSE);
	return 0;
}





LRESULT cBiomeViewWnd::OnLButtonUp(WPARAM wParam, LPARAM lParam)
{
	OnMouseMove(wParam, lParam);  // Last movement - if the mouse move hasn't been reported due to speed
	m_IsLButtonDown = false;
	InvalidateRect(m_Wnd, NULL, FALSE);
	return 0;
}





LRESULT cBiomeViewWnd::OnPaint(void)
{
	PAINTSTRUCT ps;
	HDC DC = BeginPaint(m_Wnd, &ps);
	
	RECT rc;
	GetClientRect(m_Wnd, &rc);
	int Wid = rc.right - rc.left;
	int Hei = rc.bottom - rc.top;
	if ((m_Pixmap.GetWidth() != Wid) || (m_Pixmap.GetHeight() != Hei))
	{
		m_Pixmap.SetSize(Wid, Hei);
		if (m_Renderer.Render(m_Pixmap))
		{
			SetTimer(m_Wnd, TIMER_RERENDER, 200, NULL);
		}
	}
	
	m_Pixmap.DrawToDC(DC, 0, 0);

	EndPaint(m_Wnd, &ps);
	return 0;
}





LRESULT cBiomeViewWnd::OnTimer(WPARAM wParam)
{
	switch (wParam)
	{
		case TIMER_RERENDER:
		{
			if (!m_Renderer.Render(m_Pixmap))
			{
				KillTimer(m_Wnd, TIMER_RERENDER);
			}
			InvalidateRect(m_Wnd, NULL, FALSE);
			break;
		}
	}
	return 0;
}