diff --git a/Makefile b/Makefile index a5a86e0..52d43b1 100644 --- a/Makefile +++ b/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 .PHONY: build clean run all diff --git a/floodfill.c b/floodfill.c index 88fcce0..af78732 100644 --- a/floodfill.c +++ b/floodfill.c @@ -45,20 +45,28 @@ typedef struct GameState { int q_tail; Pixel *fill; int fillCount; + bool once; + bool nomore; + Texture debugBfsTx; + Image debugBfsImg; } GameState; 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 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() { + SetTraceLogLevel(LOG_ERROR); InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Flood Fill"); SetTargetFPS(TARGET_FPS); Image imageBuffer = GenImageColor(IMG_W, IMG_H, WHITE); + Image debugBfsImg = GenImageColor(IMG_W, IMG_H, BLACK); GameState state = { .imageBuffer = imageBuffer, .displayTexture = LoadTextureFromImage(imageBuffer), + .debugBfsImg = debugBfsImg, + .debugBfsTx = LoadTextureFromImage(debugBfsImg), .playerPos = (Vector2){40, 40}, .paintTank = paintTankMax, .colorUnderPlayer = BLUE, @@ -125,17 +133,18 @@ void Update(GameState *S) { } } - if (!dirty) { + if (!dirty || S->once) { + // if (true) { return; } for (int i = 0; i < IMG_SIZE; i++) { Pixel pixel = IndexToPixel(i); Color color = GetImageColor(S->imageBuffer, pixel.x, pixel.y); 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; } + // printf("X: %d Y: %d - C: %d IDX: %d \n", pixel.x, pixel.y, sameColor, S->bfsBuffer[i]); S->queue[0] = pixel; S->q_tail++; bool edgeDetected = false; @@ -148,15 +157,14 @@ void Update(GameState *S) { int top = idx - IMG_W; int bottom = idx + IMG_W; int directions[] = { left, right, top, bottom }; - printf("=========================\n"); for (int i = 0; i < 4; i++) { int j = directions[i]; // 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)) { Pixel pixel = IndexToPixel(j); - printf("PX: %d PY: %d\n", pixel.x, pixel.y); Color c = GetImageColor(S->imageBuffer, pixel.x, pixel.y); 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; } else if (S->bfsBuffer[j] == 0) { S->bfsBuffer[j] = -1; @@ -168,11 +176,14 @@ void Update(GameState *S) { S->bfsBuffer[idx] = 1; if (!edgeDetected) { 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 if (!edgeDetected) { + printf("no dice\n"); for (int i = 0; i < S->fillCount; i++) { Pixel p = S->fill[i]; ImageDrawPixel(&S->imageBuffer, p.x, p.y, playerColor); @@ -182,7 +193,9 @@ void Update(GameState *S) { S->fillCount = 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) { @@ -217,6 +230,39 @@ void Draw2DDebug(GameState *S) { Rectangle rect = {SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT - 100, 110, 50}; DrawRectangleRec(rect, S->colorUnderPlayer); 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); } @@ -243,6 +289,8 @@ int main(void) { UnloadImage(state->imageBuffer); UnloadTexture(state->displayTexture); + UnloadImage(state->debugBfsImg); + UnloadTexture(state->debugBfsTx); CloseWindow(); free(state->fill); free(state->queue);