]> PatchyGit - allbombson/PatchyCaster.git/commitdiff
Actual rendering!!!!! master
authorSam Smith <sam@patchysicp.xyz>
Tue, 10 Nov 2026 18:45:07 +0000 (11:45 -0700)
committerSam Smith <sam@patchysicp.xyz>
Tue, 10 Nov 2026 18:45:07 +0000 (11:45 -0700)
makefile [new file with mode: 0644]
src/game.c
src/game.h
src/sdl.c
src/sdl.h

diff --git a/makefile b/makefile
new file mode 100644 (file)
index 0000000..246189e
--- /dev/null
+++ b/makefile
@@ -0,0 +1,16 @@
+CFLAGS=-Wall -Wextra -Wpedantic -Wstrict-prototypes -Wold-style-definition -std=c89 -pedantic $(shell pkg-config sdl2 --cflags)
+LDLIBS=$(shell pkg-config sdl2 --libs) -lm
+
+VPATH=src
+OBJECTS=game.o sdl.o
+TARGET=patchycaster
+
+.PHONY: all clean
+all: $(TARGET)
+clean:
+       $(RM) $(TARGET) $(OBJECTS)
+
+$(TARGET): $(OBJECTS)
+       $(LINK.c) $(OBJECTS) $(LOADLIBES) $(LDLIBS) -o $(TARGET)
+game.o: game.c game.h sdl.h
+sdl.o: sdl.c game.h sdl.h
index 5607883..a5d8eb9 100644 (file)
@@ -122,6 +122,8 @@ ray *castRay(player *PlayerC, bam bearing) {
        /* init */
        Xray.wall_type = 0;
        Yray.wall_type = 0;
        /* init */
        Xray.wall_type = 0;
        Yray.wall_type = 0;
+       Xray.distance = 0;
+       Yray.distance = 0;
 
        /* TODO: force texture size to be a power of 2 and then have a macro for
         * the bits needed to do * and / */
 
        /* TODO: force texture size to be a power of 2 and then have a macro for
         * the bits needed to do * and / */
@@ -147,11 +149,13 @@ ray *castRay(player *PlayerC, bam bearing) {
 
                while(y/TEXTURE_SIZE >= 0 && y/TEXTURE_SIZE < 10 && x/TEXTURE_SIZE >= 0 && x/TEXTURE_SIZE < 10) {
                        if(map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE] != 0) {
 
                while(y/TEXTURE_SIZE >= 0 && y/TEXTURE_SIZE < 10 && x/TEXTURE_SIZE >= 0 && x/TEXTURE_SIZE < 10) {
                        if(map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE] != 0) {
-                               /* TODO: actual fill out properly lol */
                                Xray.wall_type = map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE];
                                Xray.wall_type = map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE];
-                               setColour(0xff,0xff,0);
+                               /* Debug */
+                               /* setColour(0xff,0xff,0);
                                drawLine2(PlayerC->x, PlayerC->y, x, y);
                                drawLine2(PlayerC->x, PlayerC->y, x, y);
-                               setColour(0,0xff,0xff);
+                               setColour(0,0xff,0xff); */
+
+                               Xray.distance = fabs((y-PlayerC->y)/bam_sin(bearing));
                                break;
                        }
                        y+=dy;
                                break;
                        }
                        y+=dy;
@@ -180,11 +184,13 @@ ray *castRay(player *PlayerC, bam bearing) {
 
                while(y/TEXTURE_SIZE >= 0 && y/TEXTURE_SIZE < 10 && x/TEXTURE_SIZE >= 0 && x/TEXTURE_SIZE < 10) {
                        if(map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE] != 0) {
 
                while(y/TEXTURE_SIZE >= 0 && y/TEXTURE_SIZE < 10 && x/TEXTURE_SIZE >= 0 && x/TEXTURE_SIZE < 10) {
                        if(map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE] != 0) {
-                               /* TODO: actual fill out properly lol */
                                Yray.wall_type = map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE];
                                Yray.wall_type = map[9-(y/TEXTURE_SIZE)][x/TEXTURE_SIZE];
-                               setColour(0xff,0xff,0);
+                               /* Debug */
+                               /* setColour(0xff,0xff,0);
                                drawLine2(PlayerC->x, PlayerC->y, x, y);
                                drawLine2(PlayerC->x, PlayerC->y, x, y);
-                               setColour(0,0xff,0xff);
+                               setColour(0,0xff,0xff); */
+
+                               Yray.distance = fabs((x-PlayerC->x)/bam_cos(bearing));
                                break;
                        }
                        y+=dy;
                                break;
                        }
                        y+=dy;
@@ -192,11 +198,16 @@ ray *castRay(player *PlayerC, bam bearing) {
                }
        }
 
                }
        }
 
-       if(Xray.wall_type != 0) {
-               return &Xray;
-       }
-       if(Yray.wall_type != 0) {
+       if(Xray.wall_type !=0 && Yray.wall_type != 0) {
+               if (Xray.distance <= Yray.distance) {
+                       return &Xray;
+               } else {
+                       return &Yray;
+               }
+       } else if(Xray.wall_type != 0) {
                return &Xray;
                return &Xray;
+       } else if(Yray.wall_type != 0) {
+               return &Yray;
        }
 
        return NULL;
        }
 
        return NULL;
@@ -205,6 +216,7 @@ ray *castRay(player *PlayerC, bam bearing) {
 void game_loop(void) {
        int scanVector = Player.bearing+(FOV/2);
        int i;
 void game_loop(void) {
        int scanVector = Player.bearing+(FOV/2);
        int i;
+       ray *Ray;
        /* debug */
        int x, y;
 
        /* debug */
        int x, y;
 
@@ -221,11 +233,10 @@ void game_loop(void) {
        for (i=0;i<SCREEN_WIDTH;++i) {
                int newVector = scanVector - i*FOV/SCREEN_WIDTH;
                /* Debug */
        for (i=0;i<SCREEN_WIDTH;++i) {
                int newVector = scanVector - i*FOV/SCREEN_WIDTH;
                /* Debug */
-               drawLine2(Player.x, Player.y, Player.x+128*bam_cos(newVector), Player.y+128*bam_sin(newVector));
-               if(castRay(&Player, newVector) != NULL) {
-                       drawLine(i, 20, 180);
+               /*drawLine2(Player.x, Player.y, Player.x+128*bam_cos(newVector), Player.y+128*bam_sin(newVector));*/
+               if((Ray = castRay(&Player, newVector)) != NULL) {
+                       drawLine(i, TEXTURE_SIZE*((SCREEN_WIDTH/2)/bam_tan(FOV/2))/Ray->distance);
                }
                }
-               //scanVector-=FOV/SCREEN_WIDTH;
        }
 
        /* Debug */
        }
 
        /* Debug */
index bfa42b8..14b6af0 100644 (file)
@@ -1,6 +1,6 @@
 /* TODO: make adjustable */
 /* TODO: make adjustable */
-//#define SCREEN_WIDTH (320)
-//#define SCREEN_HEIGHT (200)
+/*#define SCREEN_WIDTH (320)*/
+/*#define SCREEN_HEIGHT (200)*/
 #define SCREEN_WIDTH (1280)
 #define SCREEN_HEIGHT (720)
 #define TEXTURE_SIZE (64)
 #define SCREEN_WIDTH (1280)
 #define SCREEN_HEIGHT (720)
 #define TEXTURE_SIZE (64)
index 00d207e..4c3ad11 100644 (file)
--- a/src/sdl.c
+++ b/src/sdl.c
@@ -6,12 +6,13 @@
 SDL_Renderer *render;
 int w, h;
 
 SDL_Renderer *render;
 int w, h;
 
-int drawLine(int x, int y, int y2) {
-       return SDL_RenderDrawLine(render, x, y, x, y2);
-}
 int drawLine2(int x, int y, int x2, int y2) {
        return SDL_RenderDrawLine(render, x, h-y, x2, h-y2);
 }
 int drawLine2(int x, int y, int x2, int y2) {
        return SDL_RenderDrawLine(render, x, h-y, x2, h-y2);
 }
+int drawLine(int x, int height) {
+       int startY = (SCREEN_HEIGHT/2)-(height/2);
+       return drawLine2(x, startY, x, startY+height);
+}
 
 int setColour(unsigned char r, unsigned char g, unsigned char b) {
        return SDL_SetRenderDrawColor(render, r,g,b, 255);
 
 int setColour(unsigned char r, unsigned char g, unsigned char b) {
        return SDL_SetRenderDrawColor(render, r,g,b, 255);
@@ -23,6 +24,9 @@ int main(int argc, char *argv[]) {
        SDL_Event event;
        int quit = 0;
 
        SDL_Event event;
        int quit = 0;
 
+       (void)argc;
+       (void)argv;
+
        game_init();
 
        /* SDL shit */
        game_init();
 
        /* SDL shit */
index 325afa5..9508c81 100644 (file)
--- a/src/sdl.h
+++ b/src/sdl.h
@@ -1,3 +1,3 @@
-int drawLine(int x, int y, int y2);
+int drawLine(int x, int height);
 int drawLine2(int x, int y, int x2, int y2);
 int setColour(unsigned char r, unsigned char g, unsigned char b);
 int drawLine2(int x, int y, int x2, int y2);
 int setColour(unsigned char r, unsigned char g, unsigned char b);