From af03f388cbcff795158ea8345b5c876470229e8b Mon Sep 17 00:00:00 2001 From: Schark Date: Mon, 18 Mar 2024 23:28:37 -0700 Subject: More granular square control --- height-map-display/src/main.c | 66 +++++++++++++++++++++++-------------- height-map-display/src/map/square.c | 12 +++++++ height-map-display/src/map/square.h | 6 +++- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/height-map-display/src/main.c b/height-map-display/src/main.c index de4d278..ad3f093 100644 --- a/height-map-display/src/main.c +++ b/height-map-display/src/main.c @@ -14,15 +14,15 @@ float camera_pos_x = 0.0f; float camera_pos_y = 0.0f; float camera_pos_z = 0.0f; -float camera_yaw = 50.0f; // rotation around y-axis -float camera_pitch = -50.0f; // rotation aroudn x-axis +float camera_yaw = 33.4f; // rotation around y-axis +float camera_pitch = -66.7f; // rotation aroudn x-axis float camera_speed = 0.1f; float yaw_speed = 0.5f; float pitch_speed = 0.5f; -int SELECTED_X = 6; -int SELECTED_Y = 6; +int SELECTED_X = 0; +int SELECTED_Y = 0; bool draw_grid = true; bool debug_spin = false; @@ -65,10 +65,7 @@ void renderHeightMap(Square **squares) { for (int y = 0; y < MAP_HEIGHT - 1; y++) { // TODO: Fix color to fill full square as opposed to line - float* color = colors[0]; - if (squares[x][y].terrain == TERRAIN_FAIRWAY) { color = colors[1]; } - if (squares[x][y].terrain == TERRAIN_ROUGH) { color = colors[2]; } - if (squares[x][y].terrain == TERRAIN_GREEN) { color = colors[3]; } + float* color = get_terrain_color(squares[x][y].terrain); glColor3fv(color); glBegin(GL_QUADS); @@ -199,7 +196,7 @@ void free_squares(Square **squares, int X) { } // TODO: Move into controller struct, avoid key rollover -void process_input(GLFWwindow* window, Square** squares) { +void process_input(GLFWwindow* window, Square** squares, Node*** nodes) { if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera_pos_y -= camera_speed; if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) @@ -212,16 +209,26 @@ void process_input(GLFWwindow* window, Square** squares) { debug_spin = !debug_spin; if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS) draw_grid = !draw_grid; - if (glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS) - squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_GREEN; - if (glfwGetKey(window, GLFW_KEY_H) == GLFW_PRESS) - squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_FAIRWAY; - if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS) - squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_ROUGH; + if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) { + for (int i = 0; i < MAP_WIDTH - 1; i++) { + for (int j = 0; j < MAP_HEIGHT - 1; j++) { + nodes[i][j]->elevation = 0.0f; + } + } + for (int i = 0; i < MAP_WIDTH - 2; i++) { + for (int j = 0; j < MAP_HEIGHT - 2; j++) { + squares[i][j].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_L) == GLFW_PRESS) + change_square_height(&squares[SELECTED_X][SELECTED_Y], -0.5f); + if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) + change_square_height(&squares[SELECTED_X][SELECTED_Y], 0.5f); if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { if (SELECTED_Y != 0) { @@ -251,6 +258,17 @@ void process_input(GLFWwindow* window, Square** squares) { squares[SELECTED_X][SELECTED_Y].selected = true; } } + + if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) + squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_ROUGH; + if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) + squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_FAIRWAY; + if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) + squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_GREEN; + if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) + squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_WATER; + if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS) + squares[SELECTED_X][SELECTED_Y].terrain = TERRAIN_SAND; } int main(int argc, char *argv[]) { @@ -291,18 +309,18 @@ int main(int argc, char *argv[]) { Square **squares = initialize_squares(nodes, MAP_WIDTH, MAP_HEIGHT); // Debug stuff - squares[1][1].node_tl->elevation = 5.0f; - change_square_height(&squares[3][3], 5.0f); - change_square_height(&squares[5][5], -5.0f); - change_square_terrain(&squares[1][6], TERRAIN_FAIRWAY); - 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[1][1].node_tl->elevation = 5.0f; + //change_square_height(&squares[3][3], 5.0f); + //change_square_height(&squares[5][5], -5.0f); + //change_square_terrain(&squares[1][6], TERRAIN_FAIRWAY); + //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[SELECTED_X][SELECTED_Y].selected = true; while (!glfwWindowShouldClose(window)) { logger("Processing input..."); - process_input(window, squares); + process_input(window, squares, nodes); logger("Entering main loop..."); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -316,7 +334,7 @@ int main(int argc, char *argv[]) { camera_yaw = camera_yaw + 0.1f; } // Poll for and process events, wait for events if none are pending - glfwWaitEventsTimeout(0.01); // Add a slight delay to reduce CPU usage + glfwWaitEventsTimeout(0.10); // Add a slight delay to reduce CPU usage } logger("Terminating GLFW..."); diff --git a/height-map-display/src/map/square.c b/height-map-display/src/map/square.c index 54add55..9109123 100644 --- a/height-map-display/src/map/square.c +++ b/height-map-display/src/map/square.c @@ -40,3 +40,15 @@ Square* find_selected_square(Square*** squares, int x, int y) { return NULL; } + +float* get_terrain_color(Terrain_Type terrain) { + static float colors[6][3] = { + {0.0f, 0.0f, 0.0f}, // init + {0.0f, 0.5f, 0.0f}, // rough + {0.2f, 0.7f, 0.2f}, // fairway + {0.3f, 0.8f, 0.3f}, // green + {0.1f, 0.2f, 0.8f}, // water + {0.9f, 0.7f, 0.7f}, // sand + }; + return colors[terrain]; +} diff --git a/height-map-display/src/map/square.h b/height-map-display/src/map/square.h index 132b43c..3852fc9 100644 --- a/height-map-display/src/map/square.h +++ b/height-map-display/src/map/square.h @@ -7,9 +7,12 @@ // Define the Terrain_Type enum typedef enum { - TERRAIN_FAIRWAY, + TERRAIN_INIT, TERRAIN_ROUGH, + TERRAIN_FAIRWAY, TERRAIN_GREEN, + TERRAIN_WATER, + TERRAIN_SAND, } Terrain_Type; // Define the Square struct @@ -26,6 +29,7 @@ typedef struct Square { 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); +float* get_terrain_color(Terrain_Type terrain); Square* find_selected_square(Square*** squares, int x, int y); #endif -- cgit v1.2.3-18-g5258