-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
133 lines (112 loc) · 4.04 KB
/
main.cpp
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
#include "kamkar.h"
#if defined(WIN32) && defined(NDEBUG)
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif
static float clamp(float value, float min, float max) {
if (value < min) {
return min;
}
if (value > max) {
return max;
}
return value;
}
static float scroll = 50.0f;
static void handleScroll(GLFWwindow *window, double xOffset, double yOffset) {
scroll = clamp(scroll + float(yOffset), 0.0f, maxScroll);
}
static vec2<int> entities[] = {
0, 0,
0, 1,
};
static vec2<int> *selectedEntity = nullptr;
static float getZoom(float scroll) {
static const float factor = logf(maxZoom / minZoom) / maxScroll;
return minZoom * expf(factor * scroll);
}
static void handleClick(GLFWwindow *window, int button, int action, int mods) {
if (action == GLFW_PRESS) {
double x, y;
glfwGetCursorPos(window, &x, &y);
vec2<float> cursor = { float(x), float(y) };
vec2<int> windowSize;
glfwGetWindowSize(window, &windowSize.x, &windowSize.y);
cursor.x = cursor.x / (windowSize.x / 2.0f) - 1.0f;
cursor.y = 1.0f - cursor.y / (windowSize.y / 2.0f);
const vec2 camera = graphics::getCamera();
const float zoom = getZoom(scroll);
const float hexWidth = sqrt3 * windowSize.y / windowSize.x;
const float q = (cursor.x / hexWidth - cursor.y / hexHeight / 2) / zoom - camera.x + camera.y / 2;
const float r = (cursor.y / hexHeight) / zoom - camera.y;
int rx = iround(q);
const int ry = iround(-q - r);
int rz = iround(r);
const float dx = fabsf(rx - q);
const float dy = fabsf(ry + q + r);
const float dz = fabsf(rz - r);
if (dx > dy && dx > dz) {
rx = -ry-rz;
} else {
if (dy <= dz) {
rz = -rx-ry;
}
}
const vec2<int> result = vec2<int>{ rx, rz } + graphics::getCameraHex();
switch (button) {
case GLFW_MOUSE_BUTTON_LEFT:
for (vec2<int> &entity : entities) {
if (entity == result) {
selectedEntity = &entity;
return;
}
}
selectedEntity = nullptr;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
if (selectedEntity == nullptr) return;
*selectedEntity = result;
break;
}
}
}
int main() {
Tilemap *tilemap = createTilemap();
GLFWwindow *window = graphics::init("Kamkar");
glfwSetScrollCallback(window, handleScroll);
glfwSetMouseButtonCallback(window, handleClick);
float previousFrame = float(glfwGetTime());
float deltaTime = 0;
while (!glfwWindowShouldClose(window)) {
constexpr int radius = 10;
for (vec2<int> entity : entities) {
for (int x = -radius; x <= radius; ++x) {
for (int y = -radius - min(x, 0); y <= radius - max(x, 0); ++y) {
getTile(tilemap, entity + vec2<int>{ x, y })->isVisible = true;
}
}
}
const float zoom = getZoom(scroll);
graphics::render(window, tilemap, entities, lengthof(entities), zoom);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE)) {
return 0;
}
constexpr float cameraSpeed = 1.0f;
const float distance = deltaTime * cameraSpeed / zoom;
if (glfwGetKey(window, GLFW_KEY_DOWN)) {
graphics::moveCamera(Vertical, distance);
}
if (glfwGetKey(window, GLFW_KEY_UP)) {
graphics::moveCamera(Vertical, -distance);
}
if (glfwGetKey(window, GLFW_KEY_LEFT)) {
graphics::moveCamera(Horizontal, distance);
}
if (glfwGetKey(window, GLFW_KEY_RIGHT)) {
graphics::moveCamera(Horizontal, -distance);
}
const float currentFrame = float(glfwGetTime());
deltaTime = currentFrame - previousFrame;
previousFrame = currentFrame;
}
}