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
|
#include "bitmap.h"
#include "level.h"
#include "train.h"
#include "util.h"
#include "window.h"
#include <SDL_keycode.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);
Bitmap tiles("res/tiles.png");
Level level(32, 32, tiles);
Bitmap bitmap(Width, Height);
int xOffs = 0, yOffs = 0;
int xDrag, yDrag;
Point2D hoveredTile = { 0, 0 };
bool isDragging = false;
bool up = false;
bool down = false;
bool left = false;
bool right = false;
const int cameraMoveSpeed = 3;
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 });
Train* train = level.addVehicle();
train->setPosition(pos.x, pos.y);
train->setSpeed(0.2f);
}
});
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;
}
int mx = x / Scale + xOffs;
int my = y / Scale + yOffs;
hoveredTile = ScreenToTile({ mx, my });
});
window.onKeyDown([&](int keycode) {
switch (keycode) {
case SDLK_UP:
up = true;
break;
case SDLK_DOWN:
down = true;
break;
case SDLK_LEFT:
left = true;
break;
case SDLK_RIGHT:
right = true;
break;
case 's':
level.save();
break;
case 'l':
level.load();
break;
}
});
window.onKeyUp([&](int keycode) {
switch (keycode) {
case SDLK_UP:
up = false;
break;
case SDLK_DOWN:
down = false;
break;
case SDLK_LEFT:
left = false;
break;
case SDLK_RIGHT:
right = false;
break;
}
});
while (!window.shouldClose()) {
window.update();
bitmap.clear(0xff224466);
if (up) yOffs -= cameraMoveSpeed;
if (down) yOffs += cameraMoveSpeed;
if (left) xOffs -= cameraMoveSpeed;
if (right) xOffs += cameraMoveSpeed;
level.update();
level.draw(bitmap, xOffs, yOffs);
int xx = (hoveredTile.x - hoveredTile.y) * (TileSize / 2) - xOffs;
int yy = (hoveredTile.x + hoveredTile.y) * (TileSize / 4) - yOffs;
bitmap.blit(tiles, xx, yy, 0, TileSize, TileSize, TileSize);
window.draw(bitmap);
}
return 0;
}
|