Adds raycasting debug

Next step is to actually implement the Bresenham Line Algorithm to make
line of sight and object shadowing a reality.
This commit is contained in:
Linus Probert 2018-08-03 10:45:54 +02:00
parent 68cf24ddf8
commit 353b4ed1c5
4 changed files with 56 additions and 1 deletions

View File

@ -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)

View File

@ -573,6 +573,10 @@ run_game_render(void)
}
pointer_render(gPointer, gCamera);
#ifdef DEBUG
roommatrix_render_debug(gRoomMatrix, gCamera);
#endif
SDL_RenderPresent(gRenderer);
}

View File

@ -17,6 +17,7 @@
*/
#include <stdlib.h>
#include <math.h>
#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

View File

@ -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_