summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2023-05-13 01:47:08 -0700
committerSchark <jordan@schark.online>2023-05-13 01:47:08 -0700
commitbf04fc602e53b250b3838579c1294f8573270a3b (patch)
treed14ac7c0c14c6c446f3b6660ee64f672fe2c997f /src/main.c
parent743d7f3ef9709a89c6ba48ed83e26a0bdfbdd200 (diff)
downloadgamedev-bf04fc602e53b250b3838579c1294f8573270a3b.tar.gz
gamedev-bf04fc602e53b250b3838579c1294f8573270a3b.zip
Added PBO, lower overall performance but no more drops near walls
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c66
1 files changed, 46 insertions, 20 deletions
diff --git a/src/main.c b/src/main.c
index 3069e66..0d08d16 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,3 +1,4 @@
+#include <GL/glew.h> // must come before glfw3
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
@@ -35,7 +36,7 @@ typedef struct {
} ColorRGB;
// honest- this function can from chatgpt- cross-reference this with other raycasting guides
-void raycast(Player *player, int width, int height) {
+void raycast(Player *player, GLubyte *pixels, int width, int height) {
int screen_width = width;
int screen_height = height;
@@ -102,7 +103,7 @@ void raycast(Player *player, int width, int height) {
else { perp_wall_dist = (map_y - player->pos_y + (1 - step_y) / 2) / ray_dir_y; }
// fog effect
- float fog_distance = 20.0f;
+ float fog_distance = 15.0f;
float scale_factor = 1.0f - perp_wall_dist / fog_distance;
if (scale_factor < 0.0f) { scale_factor = 0.0f; }
@@ -120,11 +121,6 @@ void raycast(Player *player, int width, int height) {
// colors :)
-
- // TODO: currently, this effects both inside and outside faces
- // which creates some awkward coloring issues with the outer walls.
- // this may not be an issue in the final release, depending how walls
- // are handled, but worth noting now.
ColorRGB color;
if (world_map[map_x][map_y] == 1) color = (ColorRGB){.r = 255, .g = 255, .b = 255};
else if (world_map[map_x][map_y] == 2) color = (ColorRGB){.r = 255, .g = 0, .b = 0};
@@ -147,15 +143,20 @@ void raycast(Player *player, int width, int height) {
color.g *= scale_factor;
color.b *= scale_factor;
- // 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
-
- 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();
+ int index;
+ for (int y = 0; y < screen_height; y++) {
+ index = y * screen_width * 3 + x * 3;
+ if (y >= start_y && y <= end_y) {
+ // draw something
+ pixels[index] = color.r;
+ pixels[index+1] = color.g;
+ pixels[index+2] = color.b;
+ } else {
+ // fill blank space
+ pixels[index] = 0;
+ pixels[index+1] = 0;
+ pixels[index+2] = 0;
+ }
}
}
@@ -163,6 +164,7 @@ void raycast(Player *player, int width, int height) {
int main(int argc, char *argv[]) {
GLFWwindow* window;
+
double mouse_x;
double mouse_y;
@@ -175,7 +177,7 @@ int main(int argc, char *argv[]) {
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.move_speed = 0.035f;
player.sensitivity = 0.001f;
// initiate glfw library
@@ -216,6 +218,19 @@ int main(int argc, char *argv[]) {
// TODO: render FPS with freetype or something as opposed to updating title
char title[200];
+ // setup pbo for optimized performance
+ if (glewInit() != GLEW_OK) {
+ printf("Failed to initiate GLEW.\n");
+ return -1;
+ }
+ GLuint pbo = 0;
+
+ glGenBuffers(1, &pbo);
+ glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
+ glBufferData(GL_PIXEL_UNPACK_BUFFER, mode->width * mode->height * 3, 0, GL_STREAM_DRAW);
+
+ int index = 0;
+ glViewport(0, 0, mode->width, mode->height);
while (!glfwWindowShouldClose(window)) {
// Stop loop if window isn't focused
@@ -233,22 +248,33 @@ int main(int argc, char *argv[]) {
last_time += 1.0;
}
+
// clear window
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
-
// main raycast function
move_player(&player, window, &mouse_x, &mouse_y, mode->width, mode->height);
- raycast(&player, mode->width, mode->height);
+ GLubyte* pixels = (GLubyte*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
+ raycast(&player, pixels, mode->width, mode->height);
+ // pbo
+ glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, mode->width);
+ glWindowPos2i(0, 0);
+ glDrawPixels(mode->width, mode->height, GL_RGB, GL_UNSIGNED_BYTE, 0);
+
+
glfwSwapBuffers(window);
glfwPollEvents();
}
+ printf("Terminating GLFW...\n");
glfwTerminate();
- printf("Terminating program...\n");
+ printf("Clearing buffer...\n");
+ glDeleteBuffers(1, &pbo);
return 0;
}