summaryrefslogtreecommitdiff
path: root/height-map-display
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2024-03-18 01:44:32 -0700
committerSchark <jordan@schark.online>2024-03-18 01:44:32 -0700
commit8e2508626776d1300b351f2326773b5bb0cb528f (patch)
treeee7daf330a91c8770ed860564c2c2d77fb138fb4 /height-map-display
parent69de9b320ad3c45e0a19845c88ae8323cfb59bf1 (diff)
downloadgamedev-8e2508626776d1300b351f2326773b5bb0cb528f.tar.gz
gamedev-8e2508626776d1300b351f2326773b5bb0cb528f.zip
Adding selected square functionality and aesthetics
Diffstat (limited to '')
-rw-r--r--height-map-display/src/main.c54
-rw-r--r--height-map-display/src/map/node.c8
-rw-r--r--height-map-display/src/map/node.h4
-rw-r--r--height-map-display/src/map/square.c17
-rw-r--r--height-map-display/src/map/square.h6
5 files changed, 80 insertions, 9 deletions
diff --git a/height-map-display/src/main.c b/height-map-display/src/main.c
index 19c5c2b..aeb599d 100644
--- a/height-map-display/src/main.c
+++ b/height-map-display/src/main.c
@@ -82,12 +82,43 @@ void renderHeightMap(Square **squares) {
// draw grid
logger("Init grid...");
if (draw_grid) {
- glColor3f(0.0f, 0.0f, 0.0f);
+
+ for (int x = 0; x <= MAP_WIDTH - 2; x++) {
+ for (int y = 0; y < MAP_HEIGHT - 2; y++) {
+ if (squares[x][y].selected) {
+
+ glColor3f(1.0f, 1.0f, 0.0f);
+ glLineWidth(3.0f);
+ glBegin(GL_LINES);
+
+ // Top edge
+ glVertex3f(x, squares[x][y].node_tl->elevation * scale_factor, y);
+ glVertex3f(x + 1, squares[x][y].node_tr->elevation * scale_factor, y);
+
+ // Bottom edge
+ glVertex3f(x, squares[x][y].node_bl->elevation * scale_factor, y + 1);
+ glVertex3f(x + 1, squares[x][y].node_br->elevation * scale_factor, y + 1);
+
+ // Left edge
+ glVertex3f(x, squares[x][y].node_tl->elevation * scale_factor, y);
+ glVertex3f(x, squares[x][y].node_bl->elevation * scale_factor, y + 1);
+
+ // Right edge
+ glVertex3f(x + 1, squares[x][y].node_tr->elevation * scale_factor, y);
+ glVertex3f(x + 1, squares[x][y].node_br->elevation * scale_factor, y + 1);
+
+ glEnd();
+ }
+ }
+ }
+
glLineWidth(3.0f);
glBegin(GL_LINES);
+ glColor3f(0.0f, 0.0f, 0.0f);
for (int x = 0; x <= MAP_WIDTH - 2; x++) {
for (int y = 0; y < MAP_HEIGHT - 2; y++) {
+ if (squares[x][y].selected) { continue; }
glVertex3f(x, squares[x][y].node_tl->elevation * scale_factor, y);
glVertex3f(x, squares[x][y + 1].node_tl->elevation * scale_factor, y + 1);
}
@@ -95,16 +126,17 @@ void renderHeightMap(Square **squares) {
for (int y = 0; y <= MAP_HEIGHT - 2; y++) {
for (int x = 0; x < MAP_WIDTH - 2; x++) {
+ if (squares[x][y].selected) { continue; }
glVertex3f(x, squares[x][y].node_tl->elevation * scale_factor, y);
glVertex3f(x + 1, squares[x + 1][y].node_tl->elevation * scale_factor, y);
}
}
+ logger("Init end...");
+ glEnd();
// TODO: Draw final column of grid manually
}
- logger("Init end...");
- glEnd();
}
// Function to initialize nodes
@@ -118,6 +150,7 @@ Node*** initialize_nodes(int X, int Y) {
nodes[i][j]->x = i;
nodes[i][j]->y = j;
nodes[i][j]->elevation = 0.0f;
+ nodes[i][j]->selected = false;
}
}
return nodes;
@@ -135,6 +168,7 @@ Square** initialize_squares(Node ***nodes, int X, int Y) {
squares[i][j].node_bl = nodes[i][j+1];
squares[i][j].node_br = nodes[i+1][j+1];
squares[i][j].terrain = TERRAIN_ROUGH;
+ squares[i][j].selected = false;
}
}
return squares;
@@ -181,13 +215,18 @@ void process_input(GLFWwindow* window, Square* square) {
square->terrain = TERRAIN_FAIRWAY;
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS)
square->terrain = TERRAIN_ROUGH;
+ if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
+ camera_yaw -= yaw_speed;
+ if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
+ camera_yaw += yaw_speed;
+
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
- camera_pitch += pitch_speed;
+ camera_yaw += yaw_speed;
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
- camera_pitch -= pitch_speed;
+ camera_yaw += yaw_speed;
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
- camera_yaw -= yaw_speed;
- if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
+ camera_yaw += yaw_speed;
+ if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
camera_yaw += yaw_speed;
}
@@ -236,6 +275,7 @@ int main(int argc, char *argv[]) {
change_square_terrain(&squares[1][5], TERRAIN_FAIRWAY);
change_square_terrain(&squares[1][4], TERRAIN_FAIRWAY);
change_square_terrain(&squares[1][3], TERRAIN_GREEN);
+ squares[3][3].selected = true;
while (!glfwWindowShouldClose(window)) {
logger("Processing input...");
diff --git a/height-map-display/src/map/node.c b/height-map-display/src/map/node.c
index eaf2877..33d568d 100644
--- a/height-map-display/src/map/node.c
+++ b/height-map-display/src/map/node.c
@@ -18,3 +18,11 @@ void change_node_height(Node* node, float diff) {
if (node != NULL) { node->elevation = node->elevation + diff; }
}
+int* get_coords(Node* node) {
+ int *coordinate = malloc(2 * sizeof(int));
+ if (coordinate != NULL) {
+ coordinate[0] = node->x;
+ coordinate[1] = node->y;
+ }
+ return coordinate;
+}
diff --git a/height-map-display/src/map/node.h b/height-map-display/src/map/node.h
index 07054f6..03fb97c 100644
--- a/height-map-display/src/map/node.h
+++ b/height-map-display/src/map/node.h
@@ -1,14 +1,18 @@
#ifndef NODE_H
#define NODE_H
+#include <stdbool.h>
+
typedef struct Node {
int x;
int y;
float elevation;
+ bool selected;
} Node;
// TODO: get (x, y) as int arr
void change_node_height(Node* node, float diff);
+int* get_coords(Node* node);
#endif
diff --git a/height-map-display/src/map/square.c b/height-map-display/src/map/square.c
index f963fa3..54add55 100644
--- a/height-map-display/src/map/square.c
+++ b/height-map-display/src/map/square.c
@@ -4,14 +4,16 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
+#include <stdbool.h>
-Square* init_square(Node *tl, Node *tr, Node *bl, Node *br, Terrain_Type terrain) {
+Square* init_square(Node *tl, Node *tr, Node *bl, Node *br, Terrain_Type terrain, bool selected) {
Square* square;
square->node_tl = tl;
square->node_tr = tr;
square->node_bl = bl;
square->node_br = br;
square->terrain = terrain;
+ square->selected = selected;
return square;
}
@@ -25,3 +27,16 @@ void change_square_height(Square* square, float diff) {
void change_square_terrain(Square* square, Terrain_Type terrain) {
square->terrain = terrain;
}
+
+// TODO: Probably a better way of storing selected square tbh
+Square* find_selected_square(Square*** squares, int x, int y) {
+ for (int i = 0; i < x - 1; i++) {
+ for (int i = 0; i < x - 1; i++) {
+ if (squares[x][y]->selected) {
+ return squares[x][y];
+ }
+ }
+ }
+
+ return NULL;
+}
diff --git a/height-map-display/src/map/square.h b/height-map-display/src/map/square.h
index 2654eb5..132b43c 100644
--- a/height-map-display/src/map/square.h
+++ b/height-map-display/src/map/square.h
@@ -1,6 +1,8 @@
#ifndef SQUARE_H
#define SQUARE_H
+#include <stdbool.h>
+
#include "node.h" // Include node.h to use Node type.
// Define the Terrain_Type enum
@@ -17,12 +19,14 @@ typedef struct Square {
Node *node_tr;
Node *node_bl;
Node *node_br;
+ bool selected;
} Square;
// Function prototypes
-Square* init_square(Node *tl, Node *tr, Node *bl, Node *br, Terrain_Type terrain);
+Square* init_square(Node *tl, Node *tr, Node *bl, Node *br, Terrain_Type terrain, bool selected);
void change_square_height(Square* square, float diff);
void change_square_terrain(Square* square, Terrain_Type terrain);
+Square* find_selected_square(Square*** squares, int x, int y);
#endif