summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 7b2292849fdc1fc456edc070c9506c12ebdc1b71 (plain)
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
#include "bitmap.h"
#include "level.h"
#include "train.h"
#include "util.h"
#include "window.h"

int main(int argc, char** argv)
{
	constexpr int Height = 240;
	constexpr int Width = Height * 16 / 9;
	constexpr int Scale = 3;

	Window window("Nonortho", Width, Height, Scale);

	Level level(32, 32);
	Bitmap bitmap(Width, Height);
	Train car;

	int xOffs = 0, yOffs = 0;
	int xDrag, yDrag;
	bool isDragging = false;

	window.onMouseDown([&](int button, int x, int y) {
		if (button == 1) {
			int mx = x / Scale + xOffs;
			int my = y / Scale + yOffs;

			Point2D tilePos = ScreenToTile({ mx, my });
			level.toggleTile(tilePos.x, tilePos.y);
		} else if (button == 2 && !isDragging) {
			xDrag = x;
			yDrag = y;
			isDragging = true;
		} else {
			int mx = x / Scale + xOffs;
			int my = y / Scale + yOffs;
			auto pos = ScreenToTile({ mx, my });
			car.setPosition(pos.x, pos.y);
		}
	});

	window.onMouseUp([&](int button, int x, int y) {
		if (button == 2 && isDragging)
			isDragging = false;
	});

	window.onMouseMove([&](int x, int y) {
		if (isDragging) {
			xOffs -= (x - xDrag) / Scale;
			yOffs -= (y - yDrag) / Scale;
			xDrag = x;
			yDrag = y;
		}
	});

	while (!window.shouldClose()) {
		window.update();
		bitmap.clear(0xff224466);

		level.draw(bitmap, xOffs, yOffs);
		car.update(level);
		car.draw(bitmap, xOffs, yOffs);

		window.draw(bitmap);
	}

	return 0;
}