From 353b4ed1c5d5edb2cf3fb33aed86c6c5ba5d6b3f Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Fri, 3 Aug 2018 10:45:54 +0200 Subject: [PATCH] Adds raycasting debug Next step is to actually implement the Bresenham Line Algorithm to make line of sight and object shadowing a reality. --- CMakeLists.txt | 1 + src/main.c | 4 ++++ src/roommatrix.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/roommatrix.h | 6 +++++- 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c133be0..233e894 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -185,6 +185,7 @@ target_link_libraries(breakhack ${SDL2_IMAGE_LIBRARY} ${SDL2_TTF_LIBRARY} ${SDL2_MIXER_LIBRARY} + m # We need the math library ) if (NOT PHYSFS_FOUND) diff --git a/src/main.c b/src/main.c index 82f3002..7becb4b 100644 --- a/src/main.c +++ b/src/main.c @@ -573,6 +573,10 @@ run_game_render(void) } pointer_render(gPointer, gCamera); +#ifdef DEBUG + roommatrix_render_debug(gRoomMatrix, gCamera); +#endif + SDL_RenderPresent(gRenderer); } diff --git a/src/roommatrix.c b/src/roommatrix.c index a2f2ec4..3969c1a 100644 --- a/src/roommatrix.c +++ b/src/roommatrix.c @@ -17,6 +17,7 @@ */ #include +#include #include "defines.h" #include "roommatrix.h" #include "util.h" @@ -265,6 +266,51 @@ roommatrix_render_lightmap(RoomMatrix *matrix, Camera *cam) } } +#ifdef DEBUG +static void +draw_raycasts_from(int x, int y, Camera *cam) +{ + double length = 4*32; + double x1 = x * 32 + 16; + double y1 = y * 32 + 16; + double angle = 0; + + while (angle < 360) { + double x2 = x1 + length * cos(angle); + double y2 = y1 + length * sin(angle); + //printf("Drawing line: %fx%f -> %fx%f\n", x1, y1, x2, y2); + SDL_RenderDrawLine(cam->renderer, + (int) x1, + (int) y1, + (int) x2, + (int) y2); + angle += 7.5; + } +} + +void +roommatrix_render_debug(RoomMatrix *rm, Camera *cam) +{ + SDL_SetRenderDrawColor(cam->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + for (int i = 0; i < MAP_ROOM_HEIGHT; ++i) { + SDL_RenderDrawLine(cam->renderer, 0, i*32, MAP_ROOM_WIDTH*32, i*32); + } + for (int i = 0; i < MAP_ROOM_WIDTH; ++i) { + SDL_RenderDrawLine(cam->renderer, i*32, 0, i*32, MAP_ROOM_HEIGHT*32); + } + + // Draw raycasts + SDL_SetRenderDrawColor(cam->renderer, 255, 255, 255, 100); + for (int i = 0; i < MAP_ROOM_WIDTH; ++i) { + for (int j = 0; j < MAP_ROOM_HEIGHT; ++j) { + if (rm->spaces[i][j].lightsource) { + draw_raycasts_from(i, j, cam); + } + } + } +} +#endif + void roommatrix_destroy(RoomMatrix *m) { // Clear the list but don't destroy the items diff --git a/src/roommatrix.h b/src/roommatrix.h index e676c9b..0128f8e 100644 --- a/src/roommatrix.h +++ b/src/roommatrix.h @@ -39,7 +39,7 @@ typedef struct { bool occupied; bool lethal; bool lightsource; - unsigned int light; + int light; Monster *monster; Player *player; LinkedList *items; @@ -67,6 +67,10 @@ void roommatrix_render_mouse_square(RoomMatrix*, Camera*); void roommatrix_render_lightmap(RoomMatrix*, Camera*); +#ifdef DEBUG +void roommatrix_render_debug(RoomMatrix*, Camera*); +#endif + void roommatrix_destroy(RoomMatrix*); #endif // ROOMMATRIX_H_