-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
134 lines (125 loc) · 5.03 KB
/
main.c
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
#include <raylib.h>
#include "math.h"
#include "stdlib.h"
struct GridTile {
Vector2 screen_coordinates;
bool is_visible;
};
void GenerateHexGrid(double radius, double grid_width, double grid_height,
struct GridTile** grid) {
double offset_x = -((radius * 2)) * ((grid_height) * 0.25);
double offset_y = 100;
float horiz = sqrtf(3) * radius;
float vert = (3.0 / 2.0) * radius;
for (int y = 0; y < grid_height; y++) {
for (int x = -(int)((grid_height - y) + 8);
x < grid_width - (int)(y * 0.5); x++) {
int grid_x = x - (int)((grid_height - y) * 0.5);
if (grid_x >= 0) {
double hex_x = (horiz * (x + (y * 0.5))) + offset_x;
double hex_y = (vert * y) + offset_y;
// set coordinates
grid[x][y].screen_coordinates.x = hex_x;
grid[x][y].screen_coordinates.y = hex_y;
grid[x][y].is_visible = true;
// set visibility for right edge to be simetrical
if ((y % 2 == 1) && (x + 1 == grid_width - (int)(y * 0.5))) {
grid[x][y].is_visible = false;
}
}
}
}
};
void DrawVisibleFields(double radius, double grid_width, double grid_height,
struct GridTile** grid) {
for (int y = 0; y < grid_height; y++) {
for (int x = 0; x < grid_width; x++) {
if (grid[x][y].is_visible) {
DrawPoly((Vector2){grid[x][y].screen_coordinates.x,
grid[x][y].screen_coordinates.y},
6, radius, 90, BEIGE);
}
}
}
};
void DEBUG_DrawCoordinatesOnHexGrid(double radius, double grid_width,
double grid_height,
struct GridTile** grid) {
for (int y = 0; y < grid_height; y++) {
for (int x = 0; x < grid_width; x++) {
if (grid[x][y].is_visible) {
double hex_x = grid[x][y].screen_coordinates.x;
double hex_y = grid[x][y].screen_coordinates.y;
DrawText(TextFormat("y: %d", (int)y), hex_x - (radius / 2),
hex_y, 20, LIME);
DrawText(TextFormat("x: %d", (int)x), hex_x - (radius / 2),
hex_y - (radius / 2), 20, VIOLET);
}
}
}
};
void DrawHexGridOutline(double radius, double grid_width, double grid_height,
struct GridTile** grid) {
for (int y = 0; y < grid_height; y++) {
for (int x = 0; x < grid_width; x++) {
if (grid[x][y].is_visible) {
double hex_x = grid[x][y].screen_coordinates.x;
double hex_y = grid[x][y].screen_coordinates.y;
DrawPolyLines((Vector2){hex_x, hex_y}, 6, radius, 90, BROWN);
}
}
}
};
int main(void) {
// Initialization
// memory allocation
// consts
// 720p
const int screenWidth = 1280;
const int screenHeight = 720;
const double radius = 40;
const int width = 16;
const int height = 9;
const int range_in_hexes = 2;
const int grid_width = width + (height * 0.5);
const int grid_height = height;
//----------------------------------------------------------------------------------
// memory allocation
//----------------------------------------------------------------------------------
struct GridTile** grid =
(struct GridTile**)malloc(grid_width * sizeof(struct GridTile*));
for (int x = 0; x < grid_width; x++) {
grid[x] =
(struct GridTile*)malloc(grid_height * sizeof(struct GridTile));
}
//----------------------------------------------------------------------------------
// Setup
//----------------------------------------------------------------------------------
for (int y = 0; y < grid_height; y++) {
for (int x = 0; x < grid_width; x++) {
// toggle off visibility;
grid[x][y].is_visible = false;
}
}
GenerateHexGrid(radius, grid_width, grid_height, grid);
InitWindow(screenWidth, screenHeight, "hex grid demo");
SetTargetFPS(60);
while (!WindowShouldClose()) {
//----------------------------------------------------------------------------------
// Update
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BROWN);
DrawVisibleFields(radius, grid_width, grid_height, grid);
DrawHexGridOutline(radius, grid_width, grid_height, grid);
DEBUG_DrawCoordinatesOnHexGrid(radius, grid_width, grid_height, grid);
EndDrawing();
//----------------------------------------------------------------------------------
}
CloseWindow();
return 0;
}