diff options
-rw-r--r-- | readme | 5 | ||||
-rw-r--r-- | src/main.c | 30 | ||||
-rw-r--r-- | src/player.c | 47 | ||||
-rw-r--r-- | src/player.h | 1 |
4 files changed, 36 insertions, 47 deletions
@@ -1,13 +1,14 @@ Current Todo: - Actually update README - - Use mouse to look around, as opposed to keys - - Y-Shearing eventually, but probably following refactor - Adjustable window size* * Currently fullscreen, add options later - Create bounds so player cannot enter undefined areas (outer walls, inner structures) - Experiment with warp-wrapping, just for fun :) - Move FPS counter from window title to rendered in-game (low priority) - Eventually clean code (move appropriate code to different files, headers) + - Optimize code near walls (seeing terrible performance post-yshearing) + - Increasing width of lines to decrease amount being drawn + - Look into PBOs? - Level editor Requirements: @@ -108,10 +108,15 @@ void raycast(Player *player, int width, int height) { // calculate height of line on screen int line_height = (int)(screen_height / perp_wall_dist); - int draw_start = -line_height / 2 + screen_height / 2; - if (draw_start < 0) { draw_start = 0; } - int draw_end = line_height / 2 + screen_height / 2; - if (draw_end >= screen_height) { draw_end = screen_height - 1; } + //int draw_start = -line_height / 2 + screen_height / 2; + //if (draw_start < 0) { draw_start = 0; } + //int draw_end = line_height / 2 + screen_height / 2; + //if (draw_end >= screen_height) { draw_end = screen_height - 1; } + int pitch = screen_height / 2 * player->look_angle; + int start_y = -line_height / 2 + screen_height / 2 + pitch; + if (start_y < 0) { start_y = 0; } + int end_y = line_height / 2 + screen_height / 2 + pitch; + if (end_y >= screen_height) { end_y = screen_height - 1; } // colors :) @@ -144,11 +149,15 @@ void raycast(Player *player, int width, int height) { // TODO: recommended to put this in its own function or something at some point, // as it deals with library calls outside of this function's scope - glBegin(GL_LINES); - glColor3ub(color.r, color.g, color.b); - glVertex2i(x, draw_start); - glVertex2i(x, draw_end); - glEnd(); + + for (int y = start_y; y < end_y; y++) { + glBegin(GL_LINES); + glColor3ub(color.r, color.g, color.b); + glVertex2i(x, start_y); + glVertex2i(x, end_y); + glEnd(); + } + } } @@ -165,6 +174,7 @@ int main(int argc, char *argv[]) { player.dir_y = 0.0f; // facing right player.plane_x = 0.0f; // FOV related player.plane_y = 0.66f; // FOV related + player.look_angle = 0.0f; player.move_speed = 0.02f; player.sensitivity = 0.001f; @@ -221,8 +231,6 @@ int main(int argc, char *argv[]) { glfwSetWindowTitle(window, title); nb_frames = 0; last_time += 1.0; - } else if (current_time - last_time >= 0.7) { - //glfwSetCursorPos(window, mode->width / 2, mode->height / 2); } // clear window diff --git a/src/player.c b/src/player.c index c06dfa4..512069a 100644 --- a/src/player.c +++ b/src/player.c @@ -12,41 +12,17 @@ void move_player(Player *player, GLFWwindow* window, double* mouse_x, double* mo player->pos_x -= player->dir_x * player->move_speed; player->pos_y -= player->dir_y * player->move_speed; } + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { + player->pos_x += player->dir_y * player->move_speed; + player->pos_y -= player->dir_x * player->move_speed; + } + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { + player->pos_x -= player->dir_y * player->move_speed; + player->pos_y += player->dir_x * player->move_speed; + } - // old implementation - // if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { - // // this changes where we're casting our rays - // double old_dir_x = player->dir_x; - // player->dir_x = player->dir_x * cos(-player->sensitivity) - player->dir_y * sin(-player->sensitivity); - // player->dir_y = old_dir_x * sin(-player->sensitivity) + player->dir_y * cos(-player->sensitivity); - // // this updates perspective - // double old_plane_x = player->plane_x; - // player->plane_x = player->plane_x * cos(-player->sensitivity) - player->plane_y * sin(-player->sensitivity); - // player->plane_y = old_plane_x * sin(-player->sensitivity) + player->plane_y * cos(-player->sensitivity); - // } - // if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { - // double old_dir_x = player->dir_x; - // player->dir_x = player->dir_x * cos(player->sensitivity) - player->dir_y * sin(player->sensitivity); - // player->dir_y = old_dir_x * sin(player->sensitivity) + player->dir_y * cos(player->sensitivity); - // double old_plane_x = player->plane_x; - // player->plane_x = player->plane_x * cos(player->sensitivity) - player->plane_y * sin(player->sensitivity); - // player->plane_y = old_plane_x * sin(player->sensitivity) + player->plane_y * cos(player->sensitivity); - // } - - // issues - //double new_mouse_x = 0, new_mouse_y = 0; - //glfwGetCursorPos(window, &new_mouse_x, &new_mouse_y); - //new_mouse_x -= width / 2; - //new_mouse_y -= height / 2; - //double dx = new_mouse_x - *mouse_x; - //double dy = new_mouse_y - *mouse_y; - //*mouse_x = new_mouse_x; - //*mouse_y = new_mouse_y; - //player->dir += dx * player->sensitivity; - //player->dir_x = cos(player->dir); - //player->dir_y = sin(player->dir); - - + // mouse controls (including y-shearing angle calculations + // DO NOT TOUCH, THIS TOOK A WHOLE NIGHT TO FIGURE OUT double mouse_dx, mouse_dy; double old_mouse_x, old_mouse_y; old_mouse_x = *mouse_x; @@ -61,5 +37,8 @@ void move_player(Player *player, GLFWwindow* window, double* mouse_x, double* mo double old_plane_x = player->plane_x; player->plane_x = player->plane_x * cos(rot_speed) - player->plane_y * sin(rot_speed); player->plane_y = old_plane_x * sin(rot_speed) + player->plane_y * cos(rot_speed); + player->look_angle += mouse_dy * -player->sensitivity; + if (player->look_angle > M_PI/4) { player->look_angle = M_PI/4; } + if (player->look_angle < -M_PI/4) { player->look_angle = -M_PI/4; } } diff --git a/src/player.h b/src/player.h index e45dfd9..2086c45 100644 --- a/src/player.h +++ b/src/player.h @@ -7,6 +7,7 @@ typedef struct Player { float plane_x, plane_y; float move_speed; float sensitivity; + float look_angle; } Player; void move_player(Player *player, GLFWwindow* window, double* mouse_x, double* mouse_y, double width, double height); |