Trying to add some debugging but it's not working
This commit is contained in:
parent
52a0002eb0
commit
5b4d4fe21e
2
Makefile
2
Makefile
@ -1,4 +1,4 @@
|
|||||||
CFLAGS=-g -fsanitize=address -fno-omit-frame-pointer -Wall -Wextra -pedantic -O0
|
CFLAGS=-g -fno-omit-frame-pointer -Wall -Wextra -pedantic -O0
|
||||||
CC=gcc
|
CC=gcc
|
||||||
|
|
||||||
.PHONY: build clean run all
|
.PHONY: build clean run all
|
||||||
|
64
floodfill.c
64
floodfill.c
@ -45,20 +45,28 @@ typedef struct GameState {
|
|||||||
int q_tail;
|
int q_tail;
|
||||||
Pixel *fill;
|
Pixel *fill;
|
||||||
int fillCount;
|
int fillCount;
|
||||||
|
bool once;
|
||||||
|
bool nomore;
|
||||||
|
Texture debugBfsTx;
|
||||||
|
Image debugBfsImg;
|
||||||
} GameState;
|
} GameState;
|
||||||
|
|
||||||
int PixelToIndex(Pixel p) {return p.y * IMG_W + p.x;}
|
int PixelToIndex(Pixel p) {return p.y * IMG_W + p.x;}
|
||||||
Pixel IndexToPixel(int idx) {return (Pixel) {idx % IMG_W, (int)(idx / IMG_W) }; }
|
Pixel IndexToPixel(int idx) {return (Pixel) {idx % IMG_W, (int)(idx / IMG_W) }; }
|
||||||
Pixel IsInBounds(int idx, Direction direction) {return (Pixel) {idx % IMG_W, (int)(idx / IMG_W) }; }
|
// Pixel IsInBounds(int idx, Direction direction) {return (Pixel) {idx % IMG_W, (int)(idx / IMG_W) }; }
|
||||||
|
|
||||||
GameState *Init() {
|
GameState *Init() {
|
||||||
|
SetTraceLogLevel(LOG_ERROR);
|
||||||
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Flood Fill");
|
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Flood Fill");
|
||||||
SetTargetFPS(TARGET_FPS);
|
SetTargetFPS(TARGET_FPS);
|
||||||
|
|
||||||
Image imageBuffer = GenImageColor(IMG_W, IMG_H, WHITE);
|
Image imageBuffer = GenImageColor(IMG_W, IMG_H, WHITE);
|
||||||
|
Image debugBfsImg = GenImageColor(IMG_W, IMG_H, BLACK);
|
||||||
GameState state = {
|
GameState state = {
|
||||||
.imageBuffer = imageBuffer,
|
.imageBuffer = imageBuffer,
|
||||||
.displayTexture = LoadTextureFromImage(imageBuffer),
|
.displayTexture = LoadTextureFromImage(imageBuffer),
|
||||||
|
.debugBfsImg = debugBfsImg,
|
||||||
|
.debugBfsTx = LoadTextureFromImage(debugBfsImg),
|
||||||
.playerPos = (Vector2){40, 40},
|
.playerPos = (Vector2){40, 40},
|
||||||
.paintTank = paintTankMax,
|
.paintTank = paintTankMax,
|
||||||
.colorUnderPlayer = BLUE,
|
.colorUnderPlayer = BLUE,
|
||||||
@ -125,17 +133,18 @@ void Update(GameState *S) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dirty) {
|
if (!dirty || S->once) {
|
||||||
|
// if (true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < IMG_SIZE; i++) {
|
for (int i = 0; i < IMG_SIZE; i++) {
|
||||||
Pixel pixel = IndexToPixel(i);
|
Pixel pixel = IndexToPixel(i);
|
||||||
Color color = GetImageColor(S->imageBuffer, pixel.x, pixel.y);
|
Color color = GetImageColor(S->imageBuffer, pixel.x, pixel.y);
|
||||||
bool sameColor = ColorIsEqual(color, playerColor);
|
bool sameColor = ColorIsEqual(color, playerColor);
|
||||||
// printf("X: %d Y: %d - C: %d IDX: %d \n", pixel.x, pixel.y, sameColor, S->bfsBuffer[i]);
|
if (S->bfsBuffer[i] != 0 || sameColor) {
|
||||||
if (S->bfsBuffer[i] != 0 || sameColor) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// printf("X: %d Y: %d - C: %d IDX: %d \n", pixel.x, pixel.y, sameColor, S->bfsBuffer[i]);
|
||||||
S->queue[0] = pixel;
|
S->queue[0] = pixel;
|
||||||
S->q_tail++;
|
S->q_tail++;
|
||||||
bool edgeDetected = false;
|
bool edgeDetected = false;
|
||||||
@ -148,15 +157,14 @@ void Update(GameState *S) {
|
|||||||
int top = idx - IMG_W;
|
int top = idx - IMG_W;
|
||||||
int bottom = idx + IMG_W;
|
int bottom = idx + IMG_W;
|
||||||
int directions[] = { left, right, top, bottom };
|
int directions[] = { left, right, top, bottom };
|
||||||
printf("=========================\n");
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
int j = directions[i];
|
int j = directions[i];
|
||||||
// printf("IDX: %d ||| %d %d %d %d \n", idx, left, right, top, bottom);
|
// printf("IDX: %d ||| %d %d %d %d \n", idx, left, right, top, bottom);
|
||||||
if ((i < 2 && j >= 0 && j < IMG_W) || (i >= 2 && j >= 0 && j < IMG_SIZE)) {
|
if ((i < 2 && j >= 0 && j < IMG_W) || (i >= 2 && j >= 0 && j < IMG_SIZE)) {
|
||||||
Pixel pixel = IndexToPixel(j);
|
Pixel pixel = IndexToPixel(j);
|
||||||
printf("PX: %d PY: %d\n", pixel.x, pixel.y);
|
|
||||||
Color c = GetImageColor(S->imageBuffer, pixel.x, pixel.y);
|
Color c = GetImageColor(S->imageBuffer, pixel.x, pixel.y);
|
||||||
if (S->bfsBuffer[j] == 0 && !ColorIsEqual(playerColor, c)) {
|
if (S->bfsBuffer[j] == 0 && !ColorIsEqual(playerColor, c)) {
|
||||||
|
// printf("IDX: %d J: %d PX: %d PY: %d\n", idx, j, pixel.x, pixel.y);
|
||||||
S->queue[S->q_tail++] = pixel;
|
S->queue[S->q_tail++] = pixel;
|
||||||
} else if (S->bfsBuffer[j] == 0) {
|
} else if (S->bfsBuffer[j] == 0) {
|
||||||
S->bfsBuffer[j] = -1;
|
S->bfsBuffer[j] = -1;
|
||||||
@ -168,11 +176,14 @@ void Update(GameState *S) {
|
|||||||
S->bfsBuffer[idx] = 1;
|
S->bfsBuffer[idx] = 1;
|
||||||
if (!edgeDetected) {
|
if (!edgeDetected) {
|
||||||
S->fill[S->fillCount++] = pixel;
|
S->fill[S->fillCount++] = pixel;
|
||||||
|
} else {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("------------------\n");
|
// This should only get called twice
|
||||||
|
// printf("PX: %d PY: %d\n", pixel.x, pixel.y);
|
||||||
// Fill that sucker
|
// Fill that sucker
|
||||||
if (!edgeDetected) {
|
if (!edgeDetected) {
|
||||||
|
printf("no dice\n");
|
||||||
for (int i = 0; i < S->fillCount; i++) {
|
for (int i = 0; i < S->fillCount; i++) {
|
||||||
Pixel p = S->fill[i];
|
Pixel p = S->fill[i];
|
||||||
ImageDrawPixel(&S->imageBuffer, p.x, p.y, playerColor);
|
ImageDrawPixel(&S->imageBuffer, p.x, p.y, playerColor);
|
||||||
@ -182,7 +193,9 @@ void Update(GameState *S) {
|
|||||||
S->fillCount = 0;
|
S->fillCount = 0;
|
||||||
S->q_head = S->q_tail = 0;
|
S->q_head = S->q_tail = 0;
|
||||||
}
|
}
|
||||||
memset(S->bfsBuffer, 0, IMG_SIZE * sizeof(int));
|
// memset(S->bfsBuffer, 0, IMG_SIZE * sizeof(int));
|
||||||
|
S->once = true;
|
||||||
|
// printf("===========================\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Draw2D(const GameState *S) {
|
void Draw2D(const GameState *S) {
|
||||||
@ -217,6 +230,39 @@ void Draw2DDebug(GameState *S) {
|
|||||||
Rectangle rect = {SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT - 100, 110, 50};
|
Rectangle rect = {SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT - 100, 110, 50};
|
||||||
DrawRectangleRec(rect, S->colorUnderPlayer);
|
DrawRectangleRec(rect, S->colorUnderPlayer);
|
||||||
DrawRectangleLinesEx(rect, 2, BLACK);
|
DrawRectangleLinesEx(rect, 2, BLACK);
|
||||||
|
if (S->once) {
|
||||||
|
if (!S->nomore) {
|
||||||
|
// Color color = BLACK;
|
||||||
|
for (int i = 0; i < IMG_W; i++) {
|
||||||
|
for (int j = 0; j < IMG_H; j++) {
|
||||||
|
// int idx = S->bfsBuffer[i];
|
||||||
|
// int idx = 2;
|
||||||
|
// if (idx == 0) {
|
||||||
|
// color = PINK;
|
||||||
|
// } else if (idx == 1) {
|
||||||
|
// color = MAGENTA;
|
||||||
|
// } else if (idx == -1) {
|
||||||
|
// color = LIME;
|
||||||
|
// } else {
|
||||||
|
// // printf("Not even one right? %d\n", idx);
|
||||||
|
// }
|
||||||
|
// Pixel p = IndexToPixel(i);
|
||||||
|
|
||||||
|
// printf("X: %d Y: %d\n", p.x, p.y);
|
||||||
|
// ImageDrawPixel(&S->debugBfsImg, i, j, RED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("What the hell is going on?\n");
|
||||||
|
ImageDrawPixel(&S->debugBfsImg, 0, 0, RED);
|
||||||
|
UpdateTexture(S->debugBfsTx, &S->debugBfsImg);
|
||||||
|
}
|
||||||
|
Rectangle source = {0, 0, IMG_W, IMG_H};
|
||||||
|
Vector2 pos = {SCREEN_WIDTH * 0.5f - IMG_W * 0.5f,SCREEN_HEIGHT * 0.5f - IMG_H * 0.5f};
|
||||||
|
Rectangle dest = {pos.x, pos.y, IMG_W, IMG_H};
|
||||||
|
// DrawTexturePro(S->debugBfsTx, source, dest, (Vector2){0,0}, 0, WHITE);
|
||||||
|
DrawTexture(S->debugBfsTx, 300, 200, WHITE);
|
||||||
|
S->nomore = true;
|
||||||
|
}
|
||||||
DrawFPS(rect.x + 17, SCREEN_HEIGHT - 33);
|
DrawFPS(rect.x + 17, SCREEN_HEIGHT - 33);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,6 +289,8 @@ int main(void) {
|
|||||||
|
|
||||||
UnloadImage(state->imageBuffer);
|
UnloadImage(state->imageBuffer);
|
||||||
UnloadTexture(state->displayTexture);
|
UnloadTexture(state->displayTexture);
|
||||||
|
UnloadImage(state->debugBfsImg);
|
||||||
|
UnloadTexture(state->debugBfsTx);
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
free(state->fill);
|
free(state->fill);
|
||||||
free(state->queue);
|
free(state->queue);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user