From 3f6caff7e64b4a2414fbd1e85a1e593abe8f0110 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Wed, 2 Sep 2026 18:53:11 +0200 Subject: [PATCH] Fix texture map Add door to map Added ability to "open" a door by pressing e Signed-off-by: Hans Kokx --- raycaster.c | 169 +++++++++++++++++++++++++++++----------------------- 1 file changed, 94 insertions(+), 75 deletions(-) diff --git a/raycaster.c b/raycaster.c index 1592a50..5c6a5d9 100644 --- a/raycaster.c +++ b/raycaster.c @@ -23,16 +23,18 @@ // 270° => 3π/2 radians (BOTTOM) // 360° => 2π radians (RIGHT) -// * Define the world +// *MARK: Define the world int mapX = 8, mapY = 8, mapSize = 32; +float movementRestriction; + // clang-format off int mapWalls[] = { 1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,1, 1,0,1,0,0,0,0,1, - 1,0,1,0,0,0,0,1, + 1,0,4,0,0,0,0,1, 1,0,0,0,0,0,0,1, 1,0,0,0,0,2,0,1, 1,0,0,0,0,0,0,1, @@ -118,78 +120,78 @@ int All_Textures[] = // All 32x32 textures 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, //Window - 1,1,1,1,1,1,1, 1,1,1,1,1,1,1, 1,1,1,1,1,1,1, 1,1,1,1,1,1,1, - 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, - 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, - 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, - 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, - 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, - 1,0,0,0,0,0,0, 0,0,0,0,0,0,1, 1,0,0,0,0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,0,0,0,0,0,0, - 0,1,1,1,1,1,1, 1,1,1,1,1,1,1, 1,1,1,1,1,1,1, 1,1,1,1,1,1,0, - 0,0,0,0,0,0,0, 0,0,0,0,0,0,1, 1,0,0,0,0,0,0, 0,0,0,0,0,0,0, - 0,1,1,1,1,1,1, 1,1,1,1,1,1,1, 1,1,1,1,1,1,1, 1,1,1,1,1,1,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,0, }; // clang-format on @@ -199,7 +201,7 @@ typedef struct } ButtonKeys; ButtonKeys Keys; -// * ---------------- HELPERS -------------------------------------------------- +// *MARK: HELPERS // Function to check if a number is a power of 2 int isPowerOfTwo(int n) @@ -232,7 +234,7 @@ float distance(float ax, float ay, float bx, float by, float ang) return (sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay))); } -// * ---------------- PLAYER --------------------------------------------------- +// *MARK: PLAYER // Player position (playerX, playerY), delta (playerDeltaX, playerDeltaY), and angle (pa) float playerX, playerY, playerDeltaX, playerDeltaY, playerAngle; @@ -255,7 +257,7 @@ void drawPlayer2D() glEnd(); } -// * ---------------- Draw Rays and Walls -------------------------------------- +// *MARK: Draw Rays and Walls void drawRays2D() { @@ -945,11 +947,12 @@ void drawFloor() glEnd(); } -// * ---------------- MAIN ----------------------------------------------------- +// *MARK: MAIN float frame1, frame2, fps; -// * Main rendering +// *MARK: Main rendering + void display() { // Frames per second @@ -1004,8 +1007,6 @@ void display() } // We need to check if we're going to bump into a wall and prevent that from happening. - float movementRestriction = mapSize / 4; - int xOffset = 0; if (playerDeltaX < 0) { @@ -1082,7 +1083,8 @@ void display() glutSwapBuffers(); } -// * Key input handling +// *MARK: Key input handling + void ButtonDown(unsigned char key, int x, int y) { if (key == 'a') @@ -1105,6 +1107,20 @@ void ButtonDown(unsigned char key, int x, int y) Keys.s = 1; } + if (key == 'e') + { + int xOffset = (playerDeltaX < 0) ? -25 : 25; + int yOffset = (playerDeltaY < 0) ? -25 : 25; + + int iPlayerX = playerX / mapSize, iPlayerX_add_xOffset = (playerX + xOffset) / mapSize; + int iPlayerY = playerY / mapSize, iPlayerY_add_yOffset = (playerY + yOffset) / mapSize; + + if (mapWalls[iPlayerY * mapX + iPlayerX_add_xOffset] == 4) + mapWalls[iPlayerY * mapX + iPlayerX_add_xOffset] = 0; + if (mapWalls[iPlayerY_add_yOffset * mapX + iPlayerX] == 4) + mapWalls[iPlayerY_add_yOffset * mapX + iPlayerX] = 0; + } + glutPostRedisplay(); } @@ -1134,6 +1150,7 @@ void ButtonUp(unsigned char key, int x, int y) } // * Window resizing + void resize(int w, int h) { // Prevent division by zero @@ -1153,7 +1170,7 @@ void resize(int w, int h) glLoadIdentity(); } -// * Check for key presses and move the "player" accordingly +// *MARK: Init and main void init() { @@ -1169,6 +1186,8 @@ void init() playerX = 1.5f * mapSize; playerY = 1.5f * mapSize; + movementRestriction = mapSize / 4; + playerAngle = 0; // Calculate the initial player deltaX and deltaY